Skip to content

Commit

Permalink
fix: CREATE TABLE UNIQUE vs. UNIQUE KEY
Browse files Browse the repository at this point in the history
- fixes #2082
- reformat the `CreateTable` production

Signed-off-by: Andreas Reichel <[email protected]>
  • Loading branch information
manticore-projects committed Oct 24, 2024
1 parent 229099c commit 3030c35
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 97 deletions.
191 changes: 94 additions & 97 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -6217,78 +6217,85 @@ CreateTable CreateTable(boolean isUsingOrReplace):
[ LOOKAHEAD(2) <K_IF> <K_NOT> <K_EXISTS> { createTable.setIfNotExists(true); }]
table=Table()
[ LOOKAHEAD(2) (
LOOKAHEAD(3)
("(" tableColumn=RelObjectName() { columns.add(tableColumn); } ("," tableColumn=RelObjectName() { columns.add(tableColumn); } )* ")")
LOOKAHEAD(3) (
"(" tableColumn=RelObjectName() { columns.add(tableColumn); } ("," tableColumn=RelObjectName() { columns.add(tableColumn); } )* ")"
)
|
("("
coldef = ColumnDefinition()

{ columnDefinitions.add(coldef); }

(
","
"("
coldef = ColumnDefinition() { columnDefinitions.add(coldef); }

(
LOOKAHEAD(3) (
tk=<K_INDEX>
","
(
LOOKAHEAD(3) (
{
idxSpec.clear();
}
tk=<K_INDEX>
sk3=RelObjectName()
/* colNames=ColumnsNamesList() */
colNames = ColumnNamesWithParamsList()
{ idxSpec.clear(); }
( parameter=CreateParameter() { idxSpec.addAll(parameter); } )*
{
index = new Index().withType(tk.image).withName(sk3).withColumns(colNames).withIndexSpec(new ArrayList<String>(idxSpec));
indexes.add(index);
}
)
|
LOOKAHEAD(3) (
{
index = new NamedConstraint();
}
[<K_CONSTRAINT> sk3=RelObjectName() {index.setName(sk3);} ]
)
|
LOOKAHEAD(3) (
{
index = new NamedConstraint();
tk2=null;
idxSpec.clear();
}
[ <K_CONSTRAINT> sk3=RelObjectName() {index.setName(sk3);} ]

(
tk=<K_PRIMARY> tk2=<K_KEY>
|
tk=<K_UNIQUE> [ tk2=<K_KEY> ]
)
{
index.setType( tk.image + ( tk2!=null ? " " + tk2.image : "" ));
tk2=null;
}

(tk=<K_PRIMARY> tk2=<K_KEY> {index.setType(tk.image + " " + tk2.image);}
| tk=<K_UNIQUE> [ tk2=<K_KEY> ] {index.setType(tk.image + (tk2!=null?" " + tk2.image:""));}
)
/* colNames=ColumnsNamesList() */
colNames = ColumnNamesWithParamsList()
{ idxSpec.clear(); }
( parameter=CreateParameter() { idxSpec.addAll(parameter); } )*
{
index.withColumns(colNames).withIndexSpec(new ArrayList<String>(idxSpec));
indexes.add(index);
}
// reset Token to null forcefullly
)
|
LOOKAHEAD(3) (
{
tk2=null;
}
)
|
LOOKAHEAD(3) ( {tk=null;}
[ tk=<K_UNIQUE> ] [ tk3=<K_FULLTEXT> ] tk2=<K_KEY>
tk=null;
idxSpec.clear();
}
[ tk=<K_UNIQUE> ]
[ tk3=<K_FULLTEXT> ] tk2=<K_KEY>
sk3=RelObjectName()
/* colNames=ColumnsNamesList() */
colNames = ColumnNamesWithParamsList()
{ idxSpec.clear(); }
( parameter=CreateParameter() { idxSpec.addAll(parameter); } )*
{
index = new Index()
.withType((tk!=null?tk.image + " ":"") + (tk3!=null?tk3.image + " ":"") + tk2.image)
.withName(sk3)
.withColumns(colNames)
.withIndexSpec(new ArrayList<String>(idxSpec));
.withType( ( tk!=null ? tk.image + " " : "") + ( tk3!=null ? tk3.image + " ":"" ) + tk2.image)
.withName(sk3)
.withColumns(colNames)
.withIndexSpec(new ArrayList<String>(idxSpec));
indexes.add(index);
}
)
|
LOOKAHEAD(3)(
{
fkIndex = new ForeignKeyIndex();
}
[<K_CONSTRAINT> sk3=RelObjectName() {fkIndex.setName(sk3);} ]
tk=<K_FOREIGN> tk2=<K_KEY>
/* colNames=ColumnsNamesList() */
)
|
LOOKAHEAD(3)(
{
fkIndex = new ForeignKeyIndex();
sk3=null;

}
[ <K_CONSTRAINT> sk3=RelObjectName() { fkIndex.setName(sk3); } ]
tk=<K_FOREIGN> tk2=<K_KEY>
colNames = ColumnNamesWithParamsList()
{
fkIndex.withType(tk.image + " " + tk2.image).withColumns(colNames);
Expand All @@ -6299,60 +6306,50 @@ CreateTable CreateTable(boolean isUsingOrReplace):
fkIndex.setReferencedColumnNames(colNames2);
indexes.add(fkIndex);
}
[LOOKAHEAD(2) (<K_ON> (tk=<K_DELETE> | tk=<K_UPDATE>) action = Action()
{ fkIndex.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)]
[LOOKAHEAD(2) (<K_ON> (tk=<K_DELETE> | tk=<K_UPDATE>) action = Action()
{ fkIndex.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)]
)
|
LOOKAHEAD(3)(
[<K_CONSTRAINT> sk3 = RelObjectName()]
{Expression exp = null;}
<K_CHECK> ("(" exp = Expression() ")")* {
checkCs = new CheckConstraint().withName(sk3).withExpression(exp);
indexes.add(checkCs);
}
)
|
LOOKAHEAD(2) tk=<K_EXCLUDE> {excludeC = new ExcludeConstraint(); Expression exp = null;}
(tk2=<K_WHERE>
("(" exp = Expression() ")")* {excludeC.setExpression(exp);})
{
indexes.add(excludeC);
}
|
(

coldef = ColumnDefinition()

/*
columnName=RelObjectName()

colDataType = ColDataType()
{
columnSpecs = new ArrayList<String>();
}

( parameter=CreateParameter() { columnSpecs.addAll(parameter); } )*

{
coldef = new ColumnDefinition();
coldef.setColumnName(columnName);
coldef.setColDataType(colDataType);
if (columnSpecs.size() > 0)
coldef.setColumnSpecs(columnSpecs);
columnDefinitions.add(coldef);
} */
{ columnDefinitions.add(coldef); }
[ LOOKAHEAD(2) (
<K_ON>
( tk=<K_DELETE> | tk=<K_UPDATE> ) action = Action()
{ fkIndex.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)
]
[ LOOKAHEAD(2) (
<K_ON>
( tk=<K_DELETE> | tk=<K_UPDATE>) action = Action()
{ fkIndex.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)
]
)
|
LOOKAHEAD(3)(
{
sk3 = null;
Expression exp = null;
}
[ <K_CONSTRAINT> sk3 = RelObjectName() ]
<K_CHECK> ( "(" exp = Expression() ")" )*
{
checkCs = new CheckConstraint().withName(sk3).withExpression(exp);
indexes.add(checkCs);
}
)
|
LOOKAHEAD(2) tk=<K_EXCLUDE> {excludeC = new ExcludeConstraint(); Expression exp = null;}
(tk2=<K_WHERE>
("(" exp = Expression() ")")* {excludeC.setExpression(exp);})
{
indexes.add(excludeC);
}
|
(
coldef = ColumnDefinition()
{ columnDefinitions.add(coldef); }
)
)
)
)*
)*

")"
")"
)
)
)
]
( LOOKAHEAD(2, { getToken(1).kind != K_AS }) parameter=CreateParameter() { tableOptions.addAll(parameter); } )*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1059,4 +1059,18 @@ void testIssue1864() throws JSQLParserException {
+ " CHARACTER SET armscii8 COLLATE armscii8_bin NULL DEFAULT NULL FIRST";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@Test
void testUniqueAfterForeignKeyIssue2082() throws JSQLParserException {
String sqlStr =
"CREATE TABLE employees (\n" +
"employee_number int NOT NULL\n" +
", employee_name char (50) NOT NULL\n" +
", department_id int\n" +
", salary int\n" +
", PRIMARY KEY (employee_number)\n" +
", FOREIGN KEY (department_id) REFERENCES departments(id)\n" +
", UNIQUE (employee_name));";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}
}

0 comments on commit 3030c35

Please sign in to comment.