Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ALTER INDEX COMMENT #1932

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,11 @@ public String toString() {
b.append(' ').append(PlainSelect.getStringList(parameters, false, false));
}

if (index != null && index.getCommentText() != null) {
// `USING` is a parameters
b.append(" COMMENT ").append(index.getCommentText());
}

return b.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ public String toString() {
return head;
}

// MYSQL: ALTER TABLE ADD INDEX COMMENT 'comment'
if (getCommentText() != null) {
return head + " " + tail + " COMMENT " + getCommentText();
}

return head + " " + tail;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ public String toString() {
String head = getName() != null ? "CONSTRAINT " + getName() + " " : "";
String tail = getType() + " " + PlainSelect.getStringList(getColumnsNames(), true, true) +
(!"".equals(idxSpecText) ? " " + idxSpecText : "");

// MYSQL: ALTER TABLE ADD CONSTRAINT COMMENT 'comment'
if (getCommentText() != null) {
return head + tail + " COMMENT " + getCommentText();
}

return head + tail;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -6256,9 +6256,9 @@ AlterExpression AlterExpression():
index = new Index().withType(tk.image).withName(sk3).withColumnsNames(columnNames);
alterExp.setIndex(index);
}
[ index = IndexWithComment(index) { alterExp.setIndex(index); } ]
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
[<K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
[ index = IndexWithComment(index) { alterExp.setIndex(index); } ]
)
|
LOOKAHEAD(3) (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2023 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;

import net.sf.jsqlparser.JSQLParserException;
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,23 @@ public void testIssue679() throws JSQLParserException {
"ALTER TABLE tb_session_status ADD INDEX idx_user_id_name (user_id, user_name(10)), ADD INDEX idx_user_name (user_name)");
}

@Test
public void testAlterTableColumnCommentIssue1926() throws JSQLParserException {
String statement =
"ALTER TABLE `student` ADD INDEX `idx_age` (`age`) USING BTREE COMMENT 'index age'";
assertSqlCanBeParsedAndDeparsed(statement);

String stmt2 =
"ALTER TABLE `student` ADD INDEX `idx_name` (`name`) COMMENT 'index name', " +
"ADD INDEX `idx_age` (`age`) USING BTREE COMMENT 'index age'";
assertSqlCanBeParsedAndDeparsed(stmt2);

// TODO NOT SUPPORT MYSQL: ADD {INDEX | KEY} `idx_age` USING BTREE (`age`)
// String stmt3 = "ALTER TABLE `student` ADD INDEX `idx_age` USING BTREE (`age`) COMMENT
// 'index age'";
// assertSqlCanBeParsedAndDeparsed(stmt3);
}

@Test
public void testAlterTableIndex586() throws Exception {
Statement result =
Expand Down