improve sql parser

This commit is contained in:
wenshao
2013-08-03 20:52:07 +08:00
parent 29d3f93e6b
commit 32a43cc549
29 changed files with 811 additions and 57 deletions
@@ -61,7 +61,7 @@ public class OracleAlterTableTest19 extends OracleTest {
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("warehouses")));
Assert.assertEquals(4, visitor.getColumns().size());
Assert.assertEquals(2, visitor.getColumns().size());
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("PRODUCT_IDS_ZZJ_TBD0209",
// "discount_amount")));
@@ -74,7 +74,7 @@ public class OracleCreateTableTest22 extends OracleTest {
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(7, visitor.getColumns().size());
Assert.assertEquals(6, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("promotions_var2", "promo_id")));
}
@@ -49,7 +49,8 @@ public class OracleCreateTableTest23 extends OracleTest {
Assert.assertEquals(1, statementList.size());
Assert.assertEquals("CREATE TABLE promotions_var1 (" //
+ "\n\tpromo_id NUMBER(6) CONSTRAINT promo_id_u UNIQUE," //
+ "\n\tpromo_id NUMBER(6)" //
+ "\n\t\tCONSTRAINT promo_id_u UNIQUE," //
+ "\n\tpromo_name VARCHAR2(20)," //
+ "\n\tpromo_category VARCHAR2(15)," //
+ "\n\tpromo_cost NUMBER(10, 2)," //
@@ -48,7 +48,8 @@ public class OracleCreateTableTest24 extends OracleTest {
Assert.assertEquals(1, statementList.size());
Assert.assertEquals("CREATE TABLE locations_demo (" //
+ "\n\tlocation_id NUMBER(4) CONSTRAINT loc_id_pk PRIMARY KEY," //
+ "\n\tlocation_id NUMBER(4)"//
+ "\n\t\tCONSTRAINT loc_id_pk PRIMARY KEY," //
+ "\n\tstreet_address VARCHAR2(40)," //
+ "\n\tpostal_code VARCHAR2(12)," //
+ "\n\tcity VARCHAR2(30)," //
@@ -0,0 +1,78 @@
/*
* 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.oracle;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
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;
import com.alibaba.druid.util.JdbcConstants;
public class OracleCreateTableTest28 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE divisions " //
+ " (div_no NUMBER CONSTRAINT check_divno" //
+ " CHECK (div_no BETWEEN 10 AND 99) " //
+ " DISABLE, " //
+ " div_name VARCHAR2(9) CONSTRAINT check_divname" //
+ " CHECK (div_name = UPPER(div_name)) " //
+ " DISABLE, " //
+ " office VARCHAR2(10) CONSTRAINT check_office" //
+ " CHECK (office IN ('DALLAS','BOSTON'," //
+ " 'PARIS','TOKYO')) " //
+ " DISABLE); ";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
Assert.assertEquals("CREATE TABLE divisions (" //
+ "\n\tdiv_no NUMBER" //
+ "\n\t\tCONSTRAINT check_divno CHECK (div_no BETWEEN 10 AND 99) DISABLE," //
+ "\n\tdiv_name VARCHAR2(9)" //
+ "\n\t\tCONSTRAINT check_divname CHECK (div_name = UPPER(div_name)) DISABLE," //
+ "\n\toffice VARCHAR2(10)" //
+ "\n\t\tCONSTRAINT check_office CHECK (office IN ('DALLAS', 'BOSTON', 'PARIS', 'TOKYO')) DISABLE" //
+ "\n)",//
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(3, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("divisions", "div_no")));
}
}
@@ -0,0 +1,78 @@
/*
* 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.oracle;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
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;
import com.alibaba.druid.util.JdbcConstants;
public class OracleCreateTableTest29 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE dept_20" //
+ " (employee_id NUMBER(4) PRIMARY KEY, "//
+ " last_name VARCHAR2(10), "//
+ " job_id VARCHAR2(9), "//
+ " manager_id NUMBER(4), "//
+ " salary NUMBER(7,2), "//
+ " commission_pct NUMBER(7,2), "//
+ " department_id NUMBER(2),"//
+ " CONSTRAINT check_sal CHECK (salary * commission_pct <= 5000));";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
Assert.assertEquals("CREATE TABLE dept_20 (" //
+ "\n\temployee_id NUMBER(4) PRIMARY KEY," //
+ "\n\tlast_name VARCHAR2(10)," //
+ "\n\tjob_id VARCHAR2(9)," //
+ "\n\tmanager_id NUMBER(4)," //
+ "\n\tsalary NUMBER(7, 2)," //
+ "\n\tcommission_pct NUMBER(7, 2)," //
+ "\n\tdepartment_id NUMBER(2)," //
+ "\n\tCONSTRAINT check_sal CHECK (salary * commission_pct <= 5000)" //
+ "\n)",//
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(8, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("dept_20", "employee_id")));
}
}
@@ -0,0 +1,84 @@
/*
* 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.oracle;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
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;
import com.alibaba.druid.util.JdbcConstants;
public class OracleCreateTableTest30 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE order_detail " //
+ " (CONSTRAINT pk_od PRIMARY KEY (order_id, part_no), " //
+ " order_id NUMBER " //
+ " CONSTRAINT fk_oid " //
+ " REFERENCES oe.orders(order_id), " //
+ " part_no NUMBER " //
+ " CONSTRAINT fk_pno " //
+ " REFERENCES oe.product_information(product_id), " //
+ " quantity NUMBER " //
+ " CONSTRAINT nn_qty NOT NULL " //
+ " CONSTRAINT check_qty CHECK (quantity > 0), " //
+ " cost NUMBER " //
+ " CONSTRAINT check_cost CHECK (cost > 0) ); ";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
Assert.assertEquals("CREATE TABLE order_detail (" //
+ "\n\tCONSTRAINT pk_od PRIMARY KEY (order_id, part_no)," //
+ "\n\torder_id NUMBER" //
+ "\n\t\tCONSTRAINT fk_oid REFERENCES oe.orders (order_id)," //
+ "\n\tpart_no NUMBER" //
+ "\n\t\tCONSTRAINT fk_pno REFERENCES oe.product_information (product_id)," //
+ "\n\tquantity NUMBER" //
+ "\n\t\tCONSTRAINT nn_qty NOT NULL" //
+ "\n\t\tCONSTRAINT check_qty CHECK (quantity > 0)," //
+ "\n\tcost NUMBER" //
+ "\n\t\tCONSTRAINT check_cost CHECK (cost > 0)" //
+ "\n)",//
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(4, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("order_detail", "order_id")));
}
}
@@ -0,0 +1,68 @@
/*
* 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.oracle;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
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;
import com.alibaba.druid.util.JdbcConstants;
public class OracleCreateTableTest31 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE students (name person_name, age INTEGER," //
+ " CHECK (name.first_name IS NOT NULL AND " //
+ " name.last_name IS NOT NULL));";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
Assert.assertEquals("CREATE TABLE students ("
+ "\n\tname person_name,"
+ "\n\tage INTEGER,"
+ "\n\tCHECK (name.first_name IS NOT NULL"
+ "\n\t\tAND name.last_name IS NOT NULL)"
+ "\n)",//
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(4, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("students", "name")));
}
}
@@ -0,0 +1,64 @@
/*
* 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.oracle;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
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;
import com.alibaba.druid.util.JdbcConstants;
public class OracleCreateTableTest32 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE games (scores NUMBER CHECK (scores >= 0));";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
Assert.assertEquals("CREATE TABLE games (" //
+ "\n\tscores NUMBER" //
+ "\n\t\tCHECK (scores >= 0)" //
+ "\n)",//
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(1, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("games", "scores")));
}
}
@@ -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.oracle;
import java.util.List;
import org.junit.Assert;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
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;
import com.alibaba.druid.util.JdbcConstants;
public class OracleCreateTableTest33 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE games" //
+ " (scores NUMBER, CONSTRAINT unq_num UNIQUE (scores)" //
+ " INITIALLY DEFERRED DEFERRABLE);";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
Assert.assertEquals("CREATE TABLE games (" //
+ "\n\tscores NUMBER," //
+ "\n\tCONSTRAINT unq_num UNIQUE (scores) INITIALLY DEFERRED DEFERRABLE" //
+ "\n)",//
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(1, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("games", "scores")));
}
}