Skip to content

Commit cabfedc

Browse files
committed
fix: allow UNIQUE INDEX in CREATE TABLE
Accept an optional UNIQUE prefix on the INDEX branch of CreateTableConstraint, so MySQL-style CREATE TABLE ... UNIQUE INDEX name (cols) ... parses and round-trips. UNIQUE INDEX is disambiguated from UNIQUE KEY and the bare UNIQUE constraint by the LOOKAHEAD. Fixes #1893 Signed-off-by: 付典 <fudianchn@gmail.com>
1 parent 18b12f1 commit cabfedc

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10456,14 +10456,15 @@ Index CreateTableConstraint():
1045610456
}
1045710457
{
1045810458
(
10459-
LOOKAHEAD(3) (
10460-
{ idxSpec.clear(); }
10459+
LOOKAHEAD(4) (
10460+
{ idxSpec.clear(); tk3=null; }
10461+
[ tk3=<K_UNIQUE> ]
1046110462
tk=<K_INDEX>
1046210463
sk3=RelObjectName()
1046310464
colNames = IndexColumnsWithParamsList()
1046410465
( parameter=CreateParameter() { idxSpec.addAll(parameter); } )*
1046510466
{
10466-
index = new Index().withType(tk.image).withName(sk3).withColumns(colNames).withIndexSpec(new ArrayList<String>(idxSpec));
10467+
index = new Index().withType((tk3!=null ? tk3.image + " " : "") + tk.image).withName(sk3).withColumns(colNames).withIndexSpec(new ArrayList<String>(idxSpec));
1046710468
}
1046810469
)
1046910470
|

src/test/java/net/sf/jsqlparser/statement/create/CreateTableTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,4 +1206,15 @@ void testCreateTableQuotedColumnNamedVisible() throws JSQLParserException {
12061206
assertSqlCanBeParsedAndDeparsed(
12071207
"CREATE TABLE t1 (`visible` int, `invisible` varchar (10))", true);
12081208
}
1209+
1210+
@Test
1211+
void testUniqueIndexIssue1893() throws JSQLParserException {
1212+
// MySQL `UNIQUE INDEX name (cols) ...` was rejected (only INDEX / [UNIQUE] KEY were
1213+
// accepted).
1214+
assertSqlCanBeParsedAndDeparsed(
1215+
"CREATE TABLE t (a int, b int, UNIQUE INDEX idx (a, b) USING BTREE COMMENT 'unique index')",
1216+
true);
1217+
// A plain INDEX must still parse unchanged.
1218+
assertSqlCanBeParsedAndDeparsed("CREATE TABLE t (a int, INDEX idx (a))", true);
1219+
}
12091220
}

0 commit comments

Comments
 (0)