Skip to content

Commit

Permalink
Add parsing functionality for MySQL CONVERT TO statement
Browse files Browse the repository at this point in the history
  • Loading branch information
mj-db committed Oct 25, 2024
1 parent f3f6b72 commit 21db281
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
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());
}
}

0 comments on commit 21db281

Please sign in to comment.