Skip to content

Commit 404b08f

Browse files
committed
fix for passing test
1 parent 0bd163a commit 404b08f

File tree

3 files changed

+65
-18
lines changed

3 files changed

+65
-18
lines changed

src/main/java/net/sf/jsqlparser/statement/create/table/Index.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,15 @@ public String toString() {
154154
String idxSpecText = PlainSelect.getStringList(idxSpec, false, false);
155155
String keyword = (indexKeyword != null) ? " " + indexKeyword : "";
156156
String head =
157-
(type != null ? type : "") + keyword + (!name.isEmpty() ? " " + getName() : "");
158-
String tail = PlainSelect.getStringList(columns, true, true)
159-
+ (!"".equals(idxSpecText) ? " " + idxSpecText : "");
157+
(type != null ? type : "") +
158+
keyword +
159+
(!name.isEmpty() ? " " + getName() : "") +
160+
(using != null ? " USING " + using : "");
160161

161-
if ("".equals(tail)) {
162-
return head;
163-
}
162+
String tail = PlainSelect.getStringList(columns, true, true)
163+
+ (!idxSpecText.isEmpty() ? " " + idxSpecText : "");
164164

165-
return head + " " + tail;
165+
return tail.isEmpty() ? head : head + " " + tail;
166166
}
167167

168168
public Index withType(String type) {

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6690,7 +6690,7 @@ CreateIndex CreateIndex():
66906690
CreateIndex createIndex = new CreateIndex();
66916691
Table table = null;
66926692
List<Index.ColumnParams> colNames;
6693-
Token using;
6693+
String indexType;
66946694
Index index = null;
66956695
List<String> parameter = new ArrayList<String>();
66966696
List<String> tailParameters = new ArrayList<String>();
@@ -6705,12 +6705,12 @@ CreateIndex CreateIndex():
67056705
(
67066706
LOOKAHEAD(3)(
67076707
<K_ON> table=Table()
6708-
[ <K_USING> using=<S_IDENTIFIER> { index.setUsing(using.image); } ]
6708+
[ indexType=UsingIndexType() { index.setUsing(indexType); } ]
67096709
)
67106710
|
67116711
(
6712-
[ <K_USING> using=<S_IDENTIFIER> {
6713-
index.setUsing(using.image);
6712+
[ indexType=UsingIndexType() {
6713+
index.setUsing(indexType);
67146714
createIndex.setIndexTypeBeforeOn(true);
67156715
}
67166716
]
@@ -7663,10 +7663,22 @@ void IndexOptionList(List<String> list) :
76637663
)*
76647664
}
76657665

7666+
String UsingIndexType() :
7667+
{
7668+
String sk = null;
7669+
}
7670+
{
7671+
<K_USING> ( sk = RelObjectName() )
7672+
{
7673+
return sk;
7674+
}
7675+
}
7676+
76667677
void IndexOption(List<String> list) :
76677678
{
76687679
Token tk1 = null;
76697680
Token tk2 = null;
7681+
String sk1 = null;
76707682
boolean useEqual = false;
76717683
}
76727684
{
@@ -7695,6 +7707,10 @@ void IndexOption(List<String> list) :
76957707
{
76967708
list.add("INVISIBLE");
76977709
}
7710+
|
7711+
sk1 = UsingIndexType(){
7712+
list.add("USING " + sk1);
7713+
}
76987714
)
76997715
}
77007716

@@ -7819,19 +7835,26 @@ AlterExpression AlterExpression():
78197835
LOOKAHEAD(2) (
78207836
(tk=<K_KEY> { alterExp.setUk(true); } | tk=<K_INDEX>)
78217837
(
7822-
LOOKAHEAD(2)
7838+
LOOKAHEAD(3)
78237839
sk3 = RelObjectName()
7840+
[ sk4 = UsingIndexType() ]
78247841
columnNames = ColumnsNamesList()
78257842
|
7843+
[ sk4 = UsingIndexType() ]
78267844
columnNames = ColumnsNamesList()
78277845
)
7846+
IndexOptionList(indexSpec = new ArrayList<String>())
78287847
{
7829-
index = new Index().withType(tk.image).withName(sk3).withColumnsNames(columnNames);
7848+
index = new Index()
7849+
.withIndexKeyword(tk.image)
7850+
.withName(sk3)
7851+
.withUsing(sk4)
7852+
.withColumnsNames(columnNames)
7853+
.withIndexSpec(indexSpec);
7854+
78307855
alterExp.setIndex(index);
78317856
}
78327857
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
7833-
[ <K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
7834-
[ LOOKAHEAD(2) index = IndexWithComment(index) { alterExp.setIndex(index); } ]
78357858
)
78367859
|
78377860
(

src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,18 +1964,42 @@ public void testAlterTableAddIndexWithOptions() throws JSQLParserException {
19641964

19651965
Index index = indexExp.getIndex();
19661966
assertNotNull(index);
1967-
assertEquals("INDEX", index.getIndexKeyword()); // 명시적 "INDEX" 키워드
1967+
assertEquals("INDEX", index.getIndexKeyword());
19681968
assertEquals("idx_lastname", index.getName());
19691969
assertEquals("last_name", index.getColumnsNames().get(0));
19701970

19711971
List<String> indexSpec = index.getIndexSpec();
19721972
assertNotNull(indexSpec);
1973-
assertEquals(3, indexSpec.size());
1973+
assertEquals(4, indexSpec.size());
19741974
assertEquals("USING BTREE", indexSpec.get(0));
1975-
assertEquals("KEY_BLOCK_SIZE=16", indexSpec.get(1));
1975+
assertEquals("KEY_BLOCK_SIZE = 16", indexSpec.get(1));
19761976
assertEquals("COMMENT 'Performance tuning'", indexSpec.get(2));
19771977
assertEquals("VISIBLE", indexSpec.get(3));
19781978

19791979
assertSqlCanBeParsedAndDeparsed(sql);
19801980
}
1981+
1982+
@Test
1983+
public void testAlterTableAddIndex_UsingBeforeColumns() throws JSQLParserException {
1984+
String sql = "ALTER TABLE t ADD INDEX idx_name USING BTREE (col)";
1985+
1986+
Statement stmt = CCJSqlParserUtil.parse(sql);
1987+
Alter alter = (Alter) stmt;
1988+
1989+
assertEquals("t", alter.getTable().getFullyQualifiedName());
1990+
1991+
List<AlterExpression> alterExpressions = alter.getAlterExpressions();
1992+
assertEquals(1, alterExpressions.size());
1993+
1994+
AlterExpression expr = alterExpressions.get(0);
1995+
assertEquals(AlterOperation.ADD, expr.getOperation());
1996+
1997+
Index index = expr.getIndex();
1998+
assertEquals("idx_name", index.getName());
1999+
assertEquals("INDEX", index.getIndexKeyword());
2000+
assertEquals("BTREE", index.getUsing());
2001+
assertEquals(List.of("col"), index.getColumnsNames());
2002+
2003+
assertSqlCanBeParsedAndDeparsed(sql);
2004+
}
19812005
}

0 commit comments

Comments
 (0)