mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser
This commit is contained in:
@@ -125,7 +125,7 @@ public class MySqlCreateTableTest54 extends MysqlTest {
|
||||
+ "\n\tPRIMARY KEY (`id`), "
|
||||
+ "\n\tKEY `canvas_id` (`canvas_id`), "
|
||||
+ "\n\tKEY `sid_ty_time` (`sellerId`, `node_type`, `start_time`), "
|
||||
+ "\n\tCONSTRAINT `t_activity_node_ibfk_1` FOREIGN KEY (`canvas_id`) REFERENCES `t_activity_canvas` (`id`) ON DELETE CASCADE"
|
||||
+ "\n\tCONSTRAINT `t_activity_node_ibfk_1` FOREIGN KEY (`canvas_id`) REFERENCES `t_activity_canvas` (`id`) ON DELETE CASCADE ON UPDATE CASCADE"
|
||||
+ "\n) ENGINE = InnoDB CHARSET = utf8", output);
|
||||
}
|
||||
{
|
||||
@@ -157,7 +157,7 @@ public class MySqlCreateTableTest54 extends MysqlTest {
|
||||
+ "\n\tprimary key (`id`), "
|
||||
+ "\n\tkey `canvas_id` (`canvas_id`), "
|
||||
+ "\n\tkey `sid_ty_time` (`sellerId`, `node_type`, `start_time`), "
|
||||
+ "\n\tconstraint `t_activity_node_ibfk_1` foreign key (`canvas_id`) references `t_activity_canvas` (`id`) on delete cascade"
|
||||
+ "\n\tconstraint `t_activity_node_ibfk_1` foreign key (`canvas_id`) references `t_activity_canvas` (`id`) on delete cascade on update cascade"
|
||||
+ "\n) engine = InnoDB charset = utf8", output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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 MySqlCreateTableTest70 extends MysqlTest {
|
||||
|
||||
@Test
|
||||
public void test_one() throws Exception {
|
||||
String sql = "CREATE TABLE `app_customer_license` ("
|
||||
+ " `id` bigint(20) NOT NULL AUTO_INCREMENT ,"
|
||||
+ " `created_by` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,"
|
||||
+ " `created_date` datetime NOT NULL ,"
|
||||
+ " `last_modified_by` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,"
|
||||
+ " `last_modified_date` datetime NULL DEFAULT NULL ,"
|
||||
+ " `version` bigint(20) NOT NULL ,"
|
||||
+ " `device_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,"
|
||||
+ " `customer_info` bigint(20) NULL DEFAULT NULL ,"
|
||||
+ " PRIMARY KEY (`id`),"
|
||||
+ " FOREIGN KEY (`customer_info`) REFERENCES `app_customer_info` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,"
|
||||
+ " INDEX `fk_app_customer_info_id` (`customer_info`) USING BTREE,"
|
||||
+ " UNIQUE `idx_app_customer_license_deviceId` (`device_id`) USING BTREE"
|
||||
+ ")"
|
||||
+ "ENGINE=InnoDB "
|
||||
+ "DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci "
|
||||
+ "AUTO_INCREMENT=1 "
|
||||
+ "ROW_FORMAT=DYNAMIC "
|
||||
+ ";;";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseCreateTable();
|
||||
|
||||
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("CREATE TABLE `app_customer_license` ("
|
||||
+ "\n\t`id` bigint(20) NOT NULL AUTO_INCREMENT, "
|
||||
+ "\n\t`created_by` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, "
|
||||
+ "\n\t`created_date` datetime NOT NULL, "
|
||||
+ "\n\t`last_modified_by` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL, "
|
||||
+ "\n\t`last_modified_date` datetime NULL, "
|
||||
+ "\n\t`version` bigint(20) NOT NULL, "
|
||||
+ "\n\t`device_id` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, "
|
||||
+ "\n\t`customer_info` bigint(20) NULL, "
|
||||
+ "\n\tPRIMARY KEY (`id`), "
|
||||
+ "\n\tFOREIGN KEY (`customer_info`) REFERENCES `app_customer_info` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT, "
|
||||
+ "\n\tINDEX `fk_app_customer_info_id` USING BTREE(`customer_info`), "
|
||||
+ "\n\tUNIQUE `idx_app_customer_license_deviceId` USING BTREE (`device_id`)"
|
||||
+ "\n) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE utf8_general_ci AUTO_INCREMENT = 1 ROW_FORMAT = DYNAMIC", output);
|
||||
}
|
||||
|
||||
{
|
||||
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
|
||||
Assert.assertEquals("create table `app_customer_license` ("
|
||||
+ "\n\t`id` bigint(20) not null auto_increment, "
|
||||
+ "\n\t`created_by` varchar(50) character set utf8 collate utf8_general_ci not null, "
|
||||
+ "\n\t`created_date` datetime not null, "
|
||||
+ "\n\t`last_modified_by` varchar(50) character set utf8 collate utf8_general_ci null, "
|
||||
+ "\n\t`last_modified_date` datetime null, "
|
||||
+ "\n\t`version` bigint(20) not null, "
|
||||
+ "\n\t`device_id` varchar(20) character set utf8 collate utf8_general_ci not null, "
|
||||
+ "\n\t`customer_info` bigint(20) null, "
|
||||
+ "\n\tprimary key (`id`), "
|
||||
+ "\n\tforeign key (`customer_info`) references `app_customer_info` (`id`) on delete restrict on update restrict, "
|
||||
+ "\n\tindex `fk_app_customer_info_id` using BTREE(`customer_info`), "
|
||||
+ "\n\tunique `idx_app_customer_license_deviceId` using BTREE (`device_id`)"
|
||||
+ "\n) engine = InnoDB character set = utf8 collate utf8_general_ci auto_increment = 1 row_format = DYNAMIC", output);
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
@@ -42,6 +42,7 @@ public class OracleSchemaStatVisitorTest1 extends TestCase {
|
||||
System.out.println(sql);
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("relationShip : " + visitor.getRelationships());
|
||||
|
||||
Assert.assertEquals(2, visitor.getTables().size());
|
||||
Assert.assertEquals(true, visitor.containsTable("users"));
|
||||
@@ -53,6 +54,9 @@ public class OracleSchemaStatVisitorTest1 extends TestCase {
|
||||
Assert.assertEquals(true, visitor.getColumns().contains(new Column("usergroups", "id")));
|
||||
Assert.assertEquals(true, visitor.getColumns().contains(new Column("usergroups", "name")));
|
||||
|
||||
Assert.assertEquals(1, visitor.getRelationships().size());
|
||||
Assert.assertEquals("users.groupId = usergroups.id", visitor.getRelationships().iterator().next().toString());
|
||||
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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.oracle.visitor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat.Condition;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class OracleSchemaStatVisitorTest5 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT B.OBJ_ID AS BDZID,B.BDZMC,count(A.*) "
|
||||
+ "FROM ("
|
||||
+ " SELECT BDZ "
|
||||
+ " FROM ("
|
||||
+ " SELECT JLZB.OBJ_ID,JLZB.BDZ, JLB.BHSBID, JLB.CLZ "
|
||||
+ " FROM SCYW.T_YJ_DWYJ_YXRZ_CDBHCLJL JLB "
|
||||
+ " INNER JOIN SCYW.T_YJ_DWYJ_YXRZ_YXJLZB JLZB ON JLB.YXJLID = JLZB.OBJ_ID) T "
|
||||
+ " WHERE T.OBJ_ID IN('SYS_B_02','SYS_B_03','SYS_B_04') "
|
||||
+ " AND 'SYS_B_05'='SYS_B_06'"
|
||||
+ ") A, SCYW.T_SB_ZNYC_DZ B "
|
||||
+ "WHERE A.BDZ=B.OBJ_ID "
|
||||
+ " AND B.OBJ_ID IN ("
|
||||
+ " SELECT DZ.OBJ_ID "
|
||||
+ " FROM T_SB_ZNYC_DZ DZ "
|
||||
+ " WHERE DZ.WHBZ = 'SYS_B_07' "
|
||||
+ " OR DZ.OBJ_ID IN ("
|
||||
+ " SELECT ZFBZ.ZFID "
|
||||
+ " FROM T_SB_ZNYC_ZFBZ ZFBZ "
|
||||
+ " WHERE ZFBZ.BZID = 'SYS_B_08' AND ZFBZ.BZLB = 'SYS_B_09'"
|
||||
+ " ) "
|
||||
+ " AND DZ.FBZT = 'SYS_B_10'"
|
||||
+ " ) "
|
||||
+ "GROUP BY B.OBJ_ID,B.BDZMC";
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println(sql);
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("relationShip : " + visitor.getRelationships());
|
||||
System.out.println("where : " + visitor.getConditions());
|
||||
System.out.println("groupBy : " + visitor.getGroupByColumns());
|
||||
|
||||
for (Condition condition : visitor.getConditions()) {
|
||||
String table = condition.getColumn().getTable();
|
||||
Assert.assertTrue("table not exists : " + table, visitor.containsTable(table));
|
||||
}
|
||||
|
||||
Assert.assertEquals(5, visitor.getTables().size());
|
||||
Assert.assertEquals(true, visitor.containsTable("T_SB_ZNYC_ZFBZ"));
|
||||
|
||||
Assert.assertEquals(13, visitor.getColumns().size());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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.oracle.visitor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat.Column;
|
||||
|
||||
public class OracleSchemaStatVisitorTest6 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT 'SYS_B_0',COUNT('$SYS_B_1') AS DEVICECOUNT "
|
||||
+ "FROM ("
|
||||
+ " SELECT OID,SSJG,OID "
|
||||
+ " FROM ("
|
||||
+ " SELECT A.OID,A.SSJG,A.OID "
|
||||
+ " FROM T_TX_ZLSB_ZLDYHGQ A " //
|
||||
+ " WHERE (A.VERSIONID IS NULL OR A.VERSIONID='$SYS_B_2')"
|
||||
+ " ) T1 JOIN ("
|
||||
+ " SELECT B.OID,B.SSJG,B.OID "
|
||||
+ " FROM T_TX_ZLSB_ZLDYHGQ_VER B WHERE B.ADDFLAG<'SYS_B_3'"
|
||||
+ " ) T2 ON T1.OID=T2.OID"
|
||||
+ ") WHERE SSJG='$SYS_B_4'";
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println(sql);
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("relationShip : " + visitor.getRelationships());
|
||||
System.out.println("where : " + visitor.getConditions());
|
||||
System.out.println("groupBy : " + visitor.getGroupByColumns());
|
||||
|
||||
// Assert.assertEquals(2, visitor.getTables().size());
|
||||
// Assert.assertEquals(true, visitor.containsTable("users"));
|
||||
//
|
||||
// Assert.assertEquals(2, visitor.getColumns().size());
|
||||
// Assert.assertEquals(true, visitor.getColumns().contains(new Column("users", "id")));
|
||||
// Assert.assertEquals(true, visitor.getColumns().contains(new Column("users", "name")));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.oracle.visitor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat.Column;
|
||||
|
||||
public class OracleSchemaStatVisitorTest7 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "SELECT 'SYS_B_0',COUNT('$SYS_B_1') AS DEVICECOUNT "
|
||||
+ "FROM (SELECT A.OID,A.SSJG,A.OID FROM T_TX_ZLSB_ZLDYHGQ A WHERE (A.VERSIONID IS NULL OR A.VERSIONID='$SYS_B_2') UNION ALL SELECT B.OID,B.SSJG,B.OID "
|
||||
+ "FROM T_TX_ZLSB_ZLDYHGQ_VER B WHERE B.ADDFLAG<'SYS_B_3') WHERE SSJG='$SYS_B_4'";
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println(sql);
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
System.out.println("relationShip : " + visitor.getRelationships());
|
||||
System.out.println("where : " + visitor.getConditions());
|
||||
System.out.println("groupBy : " + visitor.getGroupByColumns());
|
||||
|
||||
// Assert.assertEquals(2, visitor.getTables().size());
|
||||
// Assert.assertEquals(true, visitor.containsTable("users"));
|
||||
//
|
||||
// Assert.assertEquals(2, visitor.getColumns().size());
|
||||
// Assert.assertEquals(true, visitor.getColumns().contains(new Column("users", "id")));
|
||||
// Assert.assertEquals(true, visitor.getColumns().contains(new Column("users", "name")));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user