Skip to content

Commit dce6a20

Browse files
Fix: ALTER INDEX COMMENT (#1932)
* Fix: ALTER INDEX COMMENT * Fix: ALTER INDEX COMMENT
1 parent 14637ce commit dce6a20

File tree

6 files changed

+32
-12
lines changed

6 files changed

+32
-12
lines changed

src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,11 @@ public String toString() {
538538
b.append(' ').append(PlainSelect.getStringList(parameters, false, false));
539539
}
540540

541+
if (index != null && index.getCommentText() != null) {
542+
// `USING` is a parameters
543+
b.append(" COMMENT ").append(index.getCommentText());
544+
}
545+
541546
return b.toString();
542547
}
543548

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,6 @@ public String toString() {
143143
return head;
144144
}
145145

146-
// MYSQL: ALTER TABLE ADD INDEX COMMENT 'comment'
147-
if (getCommentText() != null) {
148-
return head + " " + tail + " COMMENT " + getCommentText();
149-
}
150-
151146
return head + " " + tail;
152147
}
153148

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ public String toString() {
2121
String head = getName() != null ? "CONSTRAINT " + getName() + " " : "";
2222
String tail = getType() + " " + PlainSelect.getStringList(getColumnsNames(), true, true) +
2323
(!"".equals(idxSpecText) ? " " + idxSpecText : "");
24-
25-
// MYSQL: ALTER TABLE ADD CONSTRAINT COMMENT 'comment'
26-
if (getCommentText() != null) {
27-
return head + tail + " COMMENT " + getCommentText();
28-
}
29-
3024
return head + tail;
3125
}
3226

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6256,9 +6256,9 @@ AlterExpression AlterExpression():
62566256
index = new Index().withType(tk.image).withName(sk3).withColumnsNames(columnNames);
62576257
alterExp.setIndex(index);
62586258
}
6259-
[ index = IndexWithComment(index) { alterExp.setIndex(index); } ]
62606259
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
62616260
[<K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
6261+
[ index = IndexWithComment(index) { alterExp.setIndex(index); } ]
62626262
)
62636263
|
62646264
LOOKAHEAD(3) (

src/test/java/net/sf/jsqlparser/expression/OracleHintTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2023 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
110
package net.sf.jsqlparser.expression;
211

312
import net.sf.jsqlparser.JSQLParserException;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,23 @@ public void testIssue679() throws JSQLParserException {
483483
"ALTER TABLE tb_session_status ADD INDEX idx_user_id_name (user_id, user_name(10)), ADD INDEX idx_user_name (user_name)");
484484
}
485485

486+
@Test
487+
public void testAlterTableColumnCommentIssue1926() throws JSQLParserException {
488+
String statement =
489+
"ALTER TABLE `student` ADD INDEX `idx_age` (`age`) USING BTREE COMMENT 'index age'";
490+
assertSqlCanBeParsedAndDeparsed(statement);
491+
492+
String stmt2 =
493+
"ALTER TABLE `student` ADD INDEX `idx_name` (`name`) COMMENT 'index name', " +
494+
"ADD INDEX `idx_age` (`age`) USING BTREE COMMENT 'index age'";
495+
assertSqlCanBeParsedAndDeparsed(stmt2);
496+
497+
// TODO NOT SUPPORT MYSQL: ADD {INDEX | KEY} `idx_age` USING BTREE (`age`)
498+
// String stmt3 = "ALTER TABLE `student` ADD INDEX `idx_age` USING BTREE (`age`) COMMENT
499+
// 'index age'";
500+
// assertSqlCanBeParsedAndDeparsed(stmt3);
501+
}
502+
486503
@Test
487504
public void testAlterTableIndex586() throws Exception {
488505
Statement result =

0 commit comments

Comments
 (0)