improved sql parser.

This commit is contained in:
wenshao
2017-10-10 10:31:50 +08:00
parent 141aca0cd3
commit cef99c1738
10 changed files with 269 additions and 19 deletions
@@ -0,0 +1,90 @@
/*
* Copyright 1999-2017 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.db2;
import com.alibaba.druid.sql.DB2Test;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import org.junit.Assert;
import java.util.List;
public class DB2CreateTableTest_04 extends DB2Test {
public void test_0() throws Exception {
String sql = "CREATE TABLE CUSTOMER\n" +
" (CUSTOMERNUM INTEGER,\n" +
" CUSTOMERNAME VARCHAR(80),\n" +
" ADDRESS VARCHAR(200),\n" +
" CITY VARCHAR(50),\n" +
" COUNTRY VARCHAR(50),\n" +
" CODE VARCHAR(15),\n" +
" CUSTOMERNUMDIM INTEGER)\n" +
" COMPRESS YES;";
DB2StatementParser parser = new DB2StatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
System.out.println(SQLUtils.toDB2String(stmt));
Assert.assertEquals(1, statementList.size());
DB2SchemaStatVisitor visitor = new DB2SchemaStatVisitor();
stmt.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(7, visitor.getColumns().size());
Assert.assertEquals(0, visitor.getConditions().size());
Assert.assertTrue(visitor.containsTable("CUSTOMER"));
// Assert.assertTrue(visitor.getColumns().contains(new Column("DSN8B10.EMP", "WORKDEPT")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
Assert.assertEquals("CREATE TABLE CUSTOMER (\n" +
"\tCUSTOMERNUM INTEGER,\n" +
"\tCUSTOMERNAME VARCHAR(80),\n" +
"\tADDRESS VARCHAR(200),\n" +
"\tCITY VARCHAR(50),\n" +
"\tCOUNTRY VARCHAR(50),\n" +
"\tCODE VARCHAR(15),\n" +
"\tCUSTOMERNUMDIM INTEGER\n" +
")\n" +
"COMPRESS YES;", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
Assert.assertEquals("create table CUSTOMER (\n" +
"\tCUSTOMERNUM INTEGER,\n" +
"\tCUSTOMERNAME VARCHAR(80),\n" +
"\tADDRESS VARCHAR(200),\n" +
"\tCITY VARCHAR(50),\n" +
"\tCOUNTRY VARCHAR(50),\n" +
"\tCODE VARCHAR(15),\n" +
"\tCUSTOMERNUMDIM INTEGER\n" +
")\n" +
"compress yes;", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
}
@@ -0,0 +1,59 @@
/*
* Copyright 1999-2017 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.createTable;
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 MySqlCreateTableTest82 extends MysqlTest {
public void test_one() throws Exception {
String sql = "CREATE TABLE t1 (\n" +
" ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n" +
" dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\n" +
");";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseCreateTable();
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
//
// Column column = visitor.getColumn("tb_custom_vip_show_message", "custom_vip_show_message_seq");
// assertNotNull(column);
// assertEquals("INT", column.getDataType());
System.out.println(stmt);
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("CREATE TABLE t1 (\n" +
"\tts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n" +
"\tdt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\n" +
")", output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("create table t1 (\n" +
"\tts TIMESTAMP default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,\n" +
"\tdt DATETIME default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP\n" +
")", output);
}
}
}
@@ -0,0 +1,60 @@
/*
* Copyright 1999-2017 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.createTable;
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;
import com.alibaba.druid.sql.parser.SQLParserFeature;
public class MySqlCreateTableTest83 extends MysqlTest {
public void test_one() throws Exception {
String sql = "CREATE TABLE t1 (\n" +
" ts1 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- default 0\n" +
" ts2 TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL\n" +
");";
MySqlStatementParser parser = new MySqlStatementParser(sql, SQLParserFeature.KeepComments);
SQLStatement stmt = parser.parseCreateTable();
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
//
// Column column = visitor.getColumn("tb_custom_vip_show_message", "custom_vip_show_message_seq");
// assertNotNull(column);
// assertEquals("INT", column.getDataType());
System.out.println(stmt);
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("CREATE TABLE t1 (\n" +
"\tts1 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- default 0\n" +
"\tts2 TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL\n" +
")", output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("create table t1 (\n" +
"\tts1 TIMESTAMP on update CURRENT_TIMESTAMP, -- default 0\n" +
"\tts2 TIMESTAMP null on update CURRENT_TIMESTAMP -- default NULL\n" +
")", output);
}
}
}