mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve mysql parser ddl support
This commit is contained in:
+10
-3
@@ -31,7 +31,7 @@ import com.alibaba.druid.sql.parser.Token;
|
||||
|
||||
public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
|
||||
public MySqlCreateTableParser(String sql) {
|
||||
public MySqlCreateTableParser(String sql){
|
||||
super(new MySqlExprParser(sql));
|
||||
}
|
||||
|
||||
@@ -155,7 +155,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
stmt.setPartitioning(clause);
|
||||
|
||||
|
||||
if (identifierEquals("PARTITIONS")) {
|
||||
lexer.nextToken();
|
||||
clause.setPartitionCount(this.exprParser.expr());
|
||||
@@ -202,6 +202,10 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
name = this.exprParser.name();
|
||||
}
|
||||
|
||||
accept(Token.LPAREN);
|
||||
for (;;) {
|
||||
key.getColumns().add(this.exprParser.expr());
|
||||
@@ -212,6 +216,10 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
if (name != null) {
|
||||
key.setName(name);
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
@@ -223,5 +231,4 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
throw new ParserException("TODO");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -455,7 +455,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
public boolean visit(MySqlKey x) {
|
||||
if (x.getName() != null) {
|
||||
print("CONSTRAINT ");
|
||||
x.accept(this);
|
||||
x.getName().accept(this);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 1999-2011 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 java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
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;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.stat.TableStat.Column;
|
||||
|
||||
public class MySqlCreateTableTest4 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "create table t_temp_11593685fc3244cf81468209484078a0 (" + //
|
||||
"id int not null auto_increment, " + //
|
||||
"score float not null, " + //
|
||||
"student bigint not null, " + //
|
||||
"primary key(id)," + //
|
||||
"key score(score), " + //
|
||||
"key student(student))";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(3, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_temp_11593685fc3244cf81468209484078a0")));
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().contains(new Column("t_temp_11593685fc3244cf81468209484078a0", "id")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new Column("t_temp_11593685fc3244cf81468209484078a0", "score")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new Column("t_temp_11593685fc3244cf81468209484078a0", "student")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user