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 issue 2089: Add parsing functionality for MySQL CONVERT TO statement #2097

Merged
merged 1 commit into from
Oct 25, 2024
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 @@ -57,6 +57,8 @@ public class AlterExpression implements Serializable {

private List<ConstraintState> constraints;
private List<String> parameters;
private String characterSet;
private String collation;
private String lockOption;
private String commentText;

Expand Down Expand Up @@ -396,6 +398,22 @@ public List<String> getParameters() {
return parameters;
}

public String getCharacterSet() {
return characterSet;
}

public void setCharacterSet(String characterSet) {
this.characterSet = characterSet;
}

public String getCollation() {
return collation;
}

public void setCollation(String collation) {
this.collation = collation;
}

public String getLockOption() {
return lockOption;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
package net.sf.jsqlparser.statement.alter;

public enum AlterOperation {
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, TRUNCATE_PARTITION, LOCK;
ADD, ALTER, DROP, DROP_PRIMARY_KEY, DROP_UNIQUE, DROP_FOREIGN_KEY, MODIFY, CHANGE, CONVERT, ALGORITHM, RENAME, RENAME_TABLE, RENAME_INDEX, RENAME_KEY, RENAME_CONSTRAINT, COMMENT, COMMENT_WITH_EQUAL_SIGN, UNSPECIFIC, TRUNCATE_PARTITION, LOCK;

public static AlterOperation from(String operation) {
return Enum.valueOf(AlterOperation.class, operation.toUpperCase());
Expand Down
5 changes: 5 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -7322,6 +7322,11 @@ AlterExpression AlterExpression():
(tk2=<S_IDENTIFIER> | tk2=<S_QUOTED_IDENTIFIER>) { alterExp.setNewTableName(tk2.image);}
)
|
(<K_CONVERT> { alterExp.setOperation(AlterOperation.CONVERT); }
<K_TO> <K_CHARACTER> <K_SET> tk=<S_IDENTIFIER> { alterExp.setCharacterSet(tk.image); }
[<K_COLLATE> tk2=<S_IDENTIFIER> { alterExp.setCollation(tk2.image); }]
)
|
(<K_COMMENT> {alterExp.setOperation(AlterOperation.COMMENT);}
["=" {alterExp.setOperation(AlterOperation.COMMENT_WITH_EQUAL_SIGN);} ]
tk=<S_CHAR_LITERAL> { alterExp.setCommentText(tk.image); }
Expand Down
37 changes: 37 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 @@ -1097,4 +1097,41 @@ public void testIssue2090LockExclusive() throws JSQLParserException {
assertEquals(AlterOperation.LOCK, lockExp.getOperation());
assertEquals("EXCLUSIVE", lockExp.getLockOption());
}

@Test
public void testIssue2089() throws JSQLParserException {
String sql = "ALTER TABLE test_table CONVERT TO CHARACTER SET utf8mb4";
Statement stmt = CCJSqlParserUtil.parse(sql);
assertTrue(stmt instanceof Alter);
Alter alter = (Alter) stmt;
assertEquals("test_table", alter.getTable().getFullyQualifiedName());

List<AlterExpression> alterExpressions = alter.getAlterExpressions();
assertNotNull(alterExpressions);
assertEquals(1, alterExpressions.size());

AlterExpression convertExp = alterExpressions.get(0);
assertEquals(AlterOperation.CONVERT, convertExp.getOperation());
assertEquals("utf8mb4", convertExp.getCharacterSet());
assertNull(convertExp.getCollation());
}

@Test
public void testIssue2089WithCollation() throws JSQLParserException {
String sql =
"ALTER TABLE test_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci";
Statement stmt = CCJSqlParserUtil.parse(sql);
assertTrue(stmt instanceof Alter);
Alter alter = (Alter) stmt;
assertEquals("test_table", alter.getTable().getFullyQualifiedName());

List<AlterExpression> alterExpressions = alter.getAlterExpressions();
assertNotNull(alterExpressions);
assertEquals(1, alterExpressions.size());

AlterExpression convertExp = alterExpressions.get(0);
assertEquals(AlterOperation.CONVERT, convertExp.getOperation());
assertEquals("utf8mb4", convertExp.getCharacterSet());
assertEquals("utf8mb4_general_ci", convertExp.getCollation());
}
}
Loading