Skip to content

Commit

Permalink
support MySQL create database with character set and collate hint
Browse files Browse the repository at this point in the history
  • Loading branch information
zrlw authored and lizongbo committed Dec 18, 2023
1 parent 4a42dca commit ccc90d0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8126,8 +8126,14 @@ public SQLStatement parseCreateDatabase() {
List<SQLCommentHint> hints = this.exprParser.parseHints();
if (hints.size() == 1) {
String text = hints.get(0).getText();
if (text.endsWith(" IF NOT EXISTS") && text.charAt(0) == '!') {
stmt.setIfNotExists(true);
if (text.charAt(0) == '!') {
String[] words = text.trim().split("\\s+");
if (words.length > 2
&& words[words.length - 3].equalsIgnoreCase("IF")
&& words[words.length - 2].equalsIgnoreCase("NOT")
&& words[words.length - 1].equalsIgnoreCase("EXISTS")) {
stmt.setIfNotExists(true);
}
}
}
}
Expand All @@ -8146,7 +8152,25 @@ public SQLStatement parseCreateDatabase() {
}

if (lexer.token() == Token.HINT) {
stmt.setHints(this.exprParser.parseHints());
List<SQLCommentHint> hints = this.exprParser.parseHints();
if (hints.size() == 1) {
String text = hints.get(0).getText();
if (text.charAt(0) == '!') {
String[] words = text.trim().split("\\s+");
int idx = 0;
for (; idx < words.length; idx++) {
if (words[idx].equalsIgnoreCase("CHARACTER")
&& idx < words.length - 2 && words[idx + 1].equalsIgnoreCase("SET")) {
stmt.setCharacterSet(words[idx + 2]);
idx += 2;
} else if (words[idx].equalsIgnoreCase("COLLATE")
&& idx < words.length - 1) {
stmt.setCollate(words[idx + 1]);
idx += 1;
}
}
}
}
}

if (lexer.token() == Token.DEFAULT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,46 @@ public void test_3() throws Exception {
+ " STORED BY 'OTS'\n"
+ " WITH (column_mapping = 'pk:pk,a:col1,b:col2', serializer = 'default')", output);
}

@Test
public void test_4() throws Exception {
String sql = "create database /*!32312 if not exists */ test4 /*!40100 default character set utf8 */;";

MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatement();

MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);

String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE DATABASE IF NOT EXISTS test4 CHARACTER SET utf8", output);
}

@Test
public void test_5() throws Exception {
String sql = "create database /*!32312 if not exists */ test5 /*!40100 default character set utf8 collate utf8_general_ci */;";

MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatement();

MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);

String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE DATABASE IF NOT EXISTS test5 CHARACTER SET utf8 COLLATE utf8_general_ci", output);
}

@Test
public void test_6() throws Exception {
String sql = "create database /*!32312 if not exists */ test6 /*!40100 collate utf8_general_ci character set utf8 */;";

MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatement();

MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);

String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE DATABASE IF NOT EXISTS test6 CHARACTER SET utf8 COLLATE utf8_general_ci", output);
}
}

0 comments on commit ccc90d0

Please sign in to comment.