bug fixed for sql parser

This commit is contained in:
wenshao
2013-04-22 17:13:14 +08:00
parent 68e7059b2c
commit 3ee310705f
3 changed files with 103 additions and 12 deletions
@@ -163,6 +163,14 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
lexer.nextToken();
continue;
}
if (identifierEquals("COMMENT")) {
lexer.nextToken();
accept(Token.EQ);
stmt.getTableOptions().put("COMMENT", lexer.stringVal());
lexer.nextToken();
continue;
}
if (identifierEquals("PARTITION")) {
lexer.nextToken();
@@ -202,6 +210,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
}
stmt.getTableOptions().put("CHARACTER SET", lexer.stringVal());
lexer.nextToken();
continue;
} else if (identifierEquals("CHARSET")) {
lexer.nextToken();
if (lexer.token() == Token.EQ) {
@@ -209,6 +218,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
}
stmt.getTableOptions().put("CHARSET", lexer.stringVal());
lexer.nextToken();
continue;
}
}
@@ -266,6 +276,12 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
if (name != null) {
key.setName(name);
}
if (identifierEquals("USING")) {
lexer.nextToken();
key.setIndexType(lexer.stringVal());
lexer.nextToken();
}
return key;
}
@@ -431,10 +431,19 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
print(")");
for (Map.Entry<String, String> option : x.getTableOptions().entrySet()) {
String key = option.getKey();
print(" ");
print(option.getKey());
print(key);
print(" = ");
print(option.getValue());
if ("COMMENT".equals(key)) {
print('\'');
print(option.getValue());
print('\'');
} else {
print(option.getValue());
}
}
if (x.getQuery() != null) {
@@ -2595,7 +2604,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
public void endVisit(MySqlAlterTableChangeColumn x) {
}
@Override
public boolean visit(MySqlAlterTableModifyColumn x) {
print("MODIFY COLUMN ");
@@ -2612,7 +2621,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
return false;
}
@Override
public void endVisit(MySqlAlterTableModifyColumn x) {
@@ -2761,7 +2770,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
@Override
public void endVisit(MySqlUnique x) {
}
@Override
@@ -2775,10 +2784,10 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
print("FOREIGN KEY (");
printAndAccept(x.getReferencedColumns(), ", ");
print(")");
print(" REFERENCES ");
x.getReferencedTableName().accept(this);
print(" (");
printAndAccept(x.getReferencedColumns(), ", ");
print(")");
@@ -2787,7 +2796,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
@Override
public void endVisit(MySqlForeignKey x) {
}
@Override
@@ -2798,9 +2807,9 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
@Override
public void endVisit(MySqlAlterTableDiscardTablespace x) {
}
@Override
public boolean visit(MySqlAlterTableImportTablespace x) {
print("IMPORT TABLESPACE");
@@ -2809,9 +2818,9 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
@Override
public void endVisit(MySqlAlterTableImportTablespace x) {
}
@Override
public boolean visit(SQLAssignItem x) {
x.getTarget().accept(this);
@@ -0,0 +1,66 @@
/*
* 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 org.junit.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 MySqlCreateTableTest14 extends MysqlTest {
public void test_0() throws Exception {
String sql = " CREATE TABLE `OptionList` ( " + //
"`ID` int(11) NOT NULL AUTO_INCREMENT, " + //
"`OptionID` int(11) DEFAULT NULL COMMENT '选项ID', " + //
"`QuizID` int(11) DEFAULT NULL COMMENT '竞猜题目ID', " + //
"`OptionName` varchar(500) DEFAULT NULL COMMENT '选项名称', " + //
"`OptionCount` int(11) DEFAULT NULL COMMENT '选择的人数', " + //
"PRIMARY KEY (`ID`), KEY `quizId` (`QuizID`) USING BTREE, " + //
"KEY `optionId` (`OptionID`) USING BTREE" + //
") ENGINE=InnoDB DEFAULT CHARSET=gbk COMMENT=''";
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(5, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("OptionList")));
Assert.assertTrue(visitor.getColumns().contains(new Column("OptionList", "ID")));
Assert.assertTrue(visitor.getColumns().contains(new Column("OptionList", "OptionCount")));
}
}