Merge pull request #1063 from wenshao/master

mysql sql parser 'create database' support charset/collate
This commit is contained in:
wenshao
2016-01-26 17:47:54 +08:00
3 changed files with 68 additions and 0 deletions
@@ -2705,6 +2705,21 @@ public class MySqlStatementParser extends SQLStatementParser {
if (lexer.token() == Token.HINT) {
stmt.setHints(this.exprParser.parseHints());
}
if (identifierEquals("CHARACTER")) {
lexer.nextToken();
accept(Token.SET);
String charset = lexer.stringVal();
accept(Token.IDENTIFIER);
stmt.setCharacterSet(charset);
}
if (identifierEquals("COLLATE")) {
lexer.nextToken();
String collate = lexer.stringVal();
accept(Token.IDENTIFIER);
stmt.setCollate(collate);
}
return stmt;
}
@@ -1609,6 +1609,17 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
print0(ucase ? "IF NOT EXISTS " : "if not exists ");
}
x.getName().accept(this);
if (x.getCharacterSet() != null) {
print0(ucase ? " CHARACTER SET " : " character set ");
print0(x.getCharacterSet());
}
if (x.getCollate() != null) {
print0(ucase ? " COLLATE " : " collate ");
print0(x.getCollate());
}
return false;
}
@@ -0,0 +1,42 @@
/*
* Copyright 1999-2101 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.mysql;
import org.junit.Assert;
import org.junit.Test;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
public class MySqlCreateDatabaseTest2 extends MysqlTest {
@Test
public void test_one() throws Exception {
String sql = "CREATE DATABASE testRep 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 testRep CHARACTER SET utf8 COLLATE utf8_general_ci", output);
}
}