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: [MySQL] Add COMMENT for ALTER ADD #1919

Merged
merged 4 commits into from
Dec 15, 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
14 changes: 14 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/create/table/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Index implements Serializable {
private List<ColumnParams> columns;
private final List<String> name = new ArrayList<>();
private List<String> idxSpec;
private String commentText;

public List<String> getColumnsNames() {
return columns.stream()
Expand Down Expand Up @@ -142,6 +143,11 @@ 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 Expand Up @@ -192,4 +198,12 @@ public String toString() {
return columnName + (params != null ? " " + String.join(" ", params) : "");
}
}

public String getCommentText() {
return commentText;
}

public void setCommentText(String commentText) {
this.commentText = commentText;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ public class NamedConstraint extends Index {
@Override
public String toString() {
String idxSpecText = PlainSelect.getStringList(getIndexSpec(), false, false);
return (getName() != null ? "CONSTRAINT " + getName() + " " : "")
+ getType() + " " + PlainSelect.getStringList(getColumnsNames(), true, true) + (!"".
equals(idxSpecText) ? " " + idxSpecText : "");
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;
}

@Override
Expand Down
138 changes: 75 additions & 63 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -6160,6 +6160,18 @@ List<ConstraintState> AlterExpressionConstraintState():
}
}

Index IndexWithComment(Index index):
{
Token tk = null;
}
{
<K_COMMENT> tk=<S_CHAR_LITERAL> {
index.setCommentText(tk.image);
}
{
return index;
}
}

/**
* This production needs refactoring to multiple smaller productions. The target class should
Expand Down Expand Up @@ -6191,17 +6203,15 @@ AlterExpression AlterExpression():
(
(
(
<K_ADD> { alterExp.setOperation(AlterOperation.ADD); }
<K_ADD> { alterExp.setOperation(AlterOperation.ADD); }
|
<K_ALTER> { alterExp.setOperation(AlterOperation.ALTER); }
<K_ALTER> { alterExp.setOperation(AlterOperation.ALTER); }
|
<K_MODIFY> { alterExp.setOperation(AlterOperation.MODIFY); }
)

(
LOOKAHEAD(2) (
<K_PRIMARY> <K_KEY> columnNames=ColumnsNamesList() { alterExp.setPkColumns(columnNames); }
)
LOOKAHEAD(2) (<K_PRIMARY> <K_KEY> columnNames=ColumnsNamesList() { alterExp.setPkColumns(columnNames); })

constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
[<K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
Expand All @@ -6214,6 +6224,7 @@ 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); }]
)
Expand All @@ -6223,47 +6234,50 @@ AlterExpression AlterExpression():

(
LOOKAHEAD(4) (
"(" { alterExp.useBrackets(true);}
"("
{ alterExp.useBrackets(true);}
alterExpressionColumnDataType = AlterExpressionColumnDataType() {
alterExp.addColDataType(alterExpressionColumnDataType);
}
(
","
alterExpressionColumnDataType = AlterExpressionColumnDataType() {
alterExp.addColDataType(alterExpressionColumnDataType);
}
alterExp.addColDataType(alterExpressionColumnDataType);
}
)*
")"
)
")"
)
|
LOOKAHEAD(2) alterExpressionColumnDataType = AlterExpressionColumnDataType() {
alterExp.addColDataType(alterExpressionColumnDataType);
}
LOOKAHEAD(2) alterExpressionColumnDataType = AlterExpressionColumnDataType()
{ alterExp.addColDataType(alterExpressionColumnDataType); }
|
LOOKAHEAD(3) alterExpressionColumnDropNotNull = AlterExpressionColumnDropNotNull() {
alterExp.addColDropNotNull( alterExpressionColumnDropNotNull);
}
LOOKAHEAD(3) alterExpressionColumnDropNotNull = AlterExpressionColumnDropNotNull()
{ alterExp.addColDropNotNull( alterExpressionColumnDropNotNull);}
|
alterExpressionColumnDropDefault = AlterExpressionColumnDropDefault() {
alterExp.addColDropDefault( alterExpressionColumnDropDefault);
}
alterExpressionColumnDropDefault = AlterExpressionColumnDropDefault()
{ alterExp.addColDropDefault( alterExpressionColumnDropDefault); }
)
)
|
(
"(" alterExpressionColumnDataType = AlterExpressionColumnDataType() { alterExp.addColDataType(alterExpressionColumnDataType); }
("," alterExpressionColumnDataType = AlterExpressionColumnDataType() { alterExp.addColDataType(alterExpressionColumnDataType); } )* ")"
(","
alterExpressionColumnDataType = AlterExpressionColumnDataType() { alterExp.addColDataType(alterExpressionColumnDataType); }
)*
")"
)
|
( <K_UNIQUE> ((<K_KEY> { alterExp.setUk(true); } | <K_INDEX>) (tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER>) { alterExp.setUkName(tk.image); } )?
columnNames=ColumnsNamesList() { alterExp.setUkColumns(columnNames); }
[<K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }])
[<K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
[ index = IndexWithComment(index) { alterExp.setIndex(index); } ]
)
|
//following two choices regarding foreign keys should be merged
( <K_FOREIGN> <K_KEY> columnNames=ColumnsNamesList() { alterExp.setFkColumns(columnNames); columnNames = null; }
/*
<K_REFERENCES> tk=<S_IDENTIFIER> [ columnNames=ColumnsNamesList() ]
{ alterExp.setFkSourceTable(tk.image); alterExp.setFkSourceColumns(columnNames); }
{ alterExp.setFkSourceTable(tk.image); alterExp.setFkSourceColumns(columnNames); }
*/
<K_REFERENCES> fkTable=Table() [ columnNames=ColumnsNamesList() ]
{
Expand All @@ -6273,17 +6287,17 @@ AlterExpression AlterExpression():
}

[LOOKAHEAD(2) (<K_ON> (tk=<K_DELETE> | tk=<K_UPDATE>) action = Action()
{ alterExp.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)]
{ alterExp.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)]
[LOOKAHEAD(2) (<K_ON> (tk=<K_DELETE> | tk=<K_UPDATE>) action = Action()
{ alterExp.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)]
{ alterExp.setReferentialAction(ReferentialAction.Type.from(tk.image), action); }
)]
)
|
(
<K_CONSTRAINT> sk3=RelObjectName()

( ( tk=<K_FOREIGN> tk2=<K_KEY>
(
( tk=<K_FOREIGN> tk2=<K_KEY>
columnNames=ColumnsNamesList()
{
fkIndex = new ForeignKeyIndex()
Expand All @@ -6298,12 +6312,12 @@ AlterExpression AlterExpression():
alterExp.setIndex(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(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); }
)]
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
)
|
Expand All @@ -6318,13 +6332,14 @@ AlterExpression AlterExpression():
}
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
[<K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
[ index = IndexWithComment(index) { alterExp.setIndex(index); } ]
)
|
(
<K_CHECK> {Expression exp = null;} ("(" exp = Expression() ")")* {
CheckConstraint checkCs = new CheckConstraint().withName(sk3).withExpression(exp);
alterExp.setIndex(checkCs);
}
CheckConstraint checkCs = new CheckConstraint().withName(sk3).withExpression(exp);
alterExp.setIndex(checkCs);
}
)
|
(
Expand All @@ -6339,28 +6354,25 @@ AlterExpression AlterExpression():
}
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
[<K_USING> sk4=RelObjectName() { alterExp.addParameters("USING", sk4); }]
[ index = IndexWithComment(index) { alterExp.setIndex(index); } ]
)
|
(
tk=<K_KEY>
columnNames=ColumnsNamesList()
{
index = new NamedConstraint()
.withName(sk3)
.withType(tk.image)
.withColumnsNames(columnNames);
alterExp.setIndex(index);
}
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
columnNames=ColumnsNamesList()
{
index = new NamedConstraint()
.withName(sk3)
.withType(tk.image)
.withColumnsNames(columnNames);
alterExp.setIndex(index);
}
constraints=AlterExpressionConstraintState() { alterExp.setConstraints(constraints); }
)
)
)
)
|
( sk3=RelObjectName() <K_COMMENT>
tk=<S_CHAR_LITERAL> {
alterExp.withColumnName(sk3).withCommentText(tk.image);
}
)
( sk3=RelObjectName() <K_COMMENT> tk=<S_CHAR_LITERAL> { alterExp.withColumnName(sk3).withCommentText(tk.image); } )
)
)
|
Expand All @@ -6373,7 +6385,7 @@ AlterExpression AlterExpression():
)
)
|
<K_DROP> { alterExp.setOperation(AlterOperation.DROP); }
<K_DROP> { alterExp.setOperation(AlterOperation.DROP); }
(
(
(
Expand Down Expand Up @@ -6406,9 +6418,9 @@ AlterExpression AlterExpression():
(
( tk=<K_INDEX> | tk=<K_KEY> )
( tk2=<S_IDENTIFIER> | tk2=<S_QUOTED_IDENTIFIER> ) {
index = new Index().withType(tk.image).withName(tk2.image);
alterExp.setIndex(index);
}
index = new Index().withType(tk.image).withName(tk2.image);
alterExp.setIndex(index);
}
)
|
(
Expand All @@ -6435,18 +6447,18 @@ AlterExpression AlterExpression():
)
)
|
(<K_ALGORITHM>
{
(
<K_ALGORITHM> {
alterExp.setOperation(AlterOperation.ALGORITHM);
}
["=" { alterExp.setUseEqual(true);} ]
sk3 = RelObjectName() {alterExp.addParameters(sk3); }
)
)
|
LOOKAHEAD(2) <K_RENAME> { alterExp.setOperation(AlterOperation.RENAME); } [ <K_COLUMN> { alterExp.hasColumn(true);} ]
( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER> ) { alterExp.setColOldName(tk.image); }
LOOKAHEAD(2) <K_RENAME> { alterExp.setOperation(AlterOperation.RENAME); } [ <K_COLUMN> { alterExp.hasColumn(true);} ]
( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER> ) { alterExp.setColOldName(tk.image); }
<K_TO>
(tk2=<S_IDENTIFIER> | tk2=<S_QUOTED_IDENTIFIER>) { alterExp.setColumnName(tk2.image); }
(tk2=<S_IDENTIFIER> | tk2=<S_QUOTED_IDENTIFIER>) { alterExp.setColumnName(tk2.image); }
|
(
<K_RENAME> <K_TO> {alterExp.setOperation(AlterOperation.RENAME_TABLE);}
Expand All @@ -6471,7 +6483,7 @@ AlterExpression AlterExpression():
}

alterExp.setOptionalSpecifier( optionalSpecifier.toString() );
}
}
)

{
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 @@ -873,6 +873,23 @@ public void testAlterTableDropMultipleColumnsIfExists() throws JSQLParserExcepti
"ALTER TABLE test DROP COLUMN IF EXISTS name, DROP COLUMN IF EXISTS surname");
}

@Test
public void testAlterTableAddIndexWithComment1906() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"ALTER TABLE `student` ADD KEY `idx_name` (`name`) COMMENT 'name'");
}

@Test
public void testAlterTableAddIndexWithComment2() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"ALTER TABLE team_phases ADD CONSTRAINT team_phases_id_key UNIQUE (id) COMMENT 'name'");
assertSqlCanBeParsedAndDeparsed(
"ALTER TABLE team_phases ADD CONSTRAINT team_phases_id_key UNIQUE KEY (c1, c2) COMMENT 'name'");

assertSqlCanBeParsedAndDeparsed(
"ALTER TABLE team_phases ADD CONSTRAINT team_phases_id_key PRIMARY KEY (id) COMMENT 'name'");
}

@Test
public void testAlterTableDropMultipleColumnsIfExistsWithParams() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
Expand Down