From dc123820f869803949405692edfa3ac97cefd3b2 Mon Sep 17 00:00:00 2001 From: Minjae Lee Date: Sat, 26 Oct 2024 00:33:05 +0900 Subject: [PATCH] Add parsing functionality for MySQL CONVERT TO statement (#2097) Co-authored-by: mj-db --- .../statement/alter/AlterExpression.java | 18 +++++++++ .../statement/alter/AlterOperation.java | 2 +- .../net/sf/jsqlparser/parser/JSqlParserCC.jjt | 5 +++ .../jsqlparser/statement/alter/AlterTest.java | 37 +++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java b/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java index b8991ade3..c0f5ef5aa 100644 --- a/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java +++ b/src/main/java/net/sf/jsqlparser/statement/alter/AlterExpression.java @@ -57,6 +57,8 @@ public class AlterExpression implements Serializable { private List constraints; private List parameters; + private String characterSet; + private String collation; private String lockOption; private String commentText; @@ -396,6 +398,22 @@ public List 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; } diff --git a/src/main/java/net/sf/jsqlparser/statement/alter/AlterOperation.java b/src/main/java/net/sf/jsqlparser/statement/alter/AlterOperation.java index 23ee44b5f..d92ac2485 100644 --- a/src/main/java/net/sf/jsqlparser/statement/alter/AlterOperation.java +++ b/src/main/java/net/sf/jsqlparser/statement/alter/AlterOperation.java @@ -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()); diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index 1da9a6ec4..3f7780133 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -7322,6 +7322,11 @@ AlterExpression AlterExpression(): (tk2= | tk2=) { alterExp.setNewTableName(tk2.image);} ) | + ( { alterExp.setOperation(AlterOperation.CONVERT); } + tk= { alterExp.setCharacterSet(tk.image); } + [ tk2= { alterExp.setCollation(tk2.image); }] + ) + | ( {alterExp.setOperation(AlterOperation.COMMENT);} ["=" {alterExp.setOperation(AlterOperation.COMMENT_WITH_EQUAL_SIGN);} ] tk= { alterExp.setCommentText(tk.image); } diff --git a/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java b/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java index c08a96394..c240d6c69 100644 --- a/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/alter/AlterTest.java @@ -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 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 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()); + } }