Merge pull request #1087 from wenshao/master

improved sql parser support create table partition syntax
This commit is contained in:
wenshao
2016-02-26 21:50:05 +08:00
49 changed files with 2288 additions and 1336 deletions
@@ -30,13 +30,7 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlKillStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLoadXmlStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlLockTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByList;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByRange;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.InValues;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitioningDef.LessThanValues;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPrepareStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement;
@@ -184,11 +178,5 @@ public class MySqlASTVisitorAdapterTest extends TestCase {
new MySqlAlterTableDiscardTablespace().accept(adapter);
new MySqlAlterTableImportTablespace().accept(adapter);
new TableSpaceOption().accept(adapter);
new MySqlPartitionByHash ().accept(adapter);
new MySqlPartitionByRange ().accept(adapter);
new MySqlPartitioningDef ().accept(adapter);
new LessThanValues ().accept(adapter);
new InValues ().accept(adapter);
new MySqlPartitionByList ().accept(adapter);
}
}
@@ -57,7 +57,8 @@ public class MySqlCreateTableTest40 extends MysqlTest {
Assert.assertEquals("CREATE TABLE t1 (" + //
"\n\tcol1 INT, " + //
"\n\tcol2 CHAR(5)" + //
"\n) PARTITION BY HASH (col1)", output);
"\n)"
+ "\nPARTITION BY HASH (col1)", output);
}
}
@@ -59,7 +59,8 @@ public class MySqlCreateTableTest41 extends MysqlTest {
"\n\tcol1 INT, " + //
"\n\tcol2 CHAR(5), " + //
"\n\tcol3 DATE" + //
"\n) PARTITION BY LINEAR KEY (col3) PARTITIONS 5", output);
"\n)"
+ "\nPARTITION BY LINEAR KEY (col3) PARTITIONS 5", output);
}
}
@@ -65,10 +65,12 @@ public class MySqlCreateTableTest42 extends MysqlTest {
Assert.assertEquals("CREATE TABLE rc (" + //
"\n\ta INT NOT NULL, " + //
"\n\tb INT NOT NULL" + //
"\n) PARTITION BY RANGE COLUMNS (a, b)(" + //
"\n\tPARTITION p0 VALUES LESS THAN (10, 5), " + //
"\n\tPARTITION p1 VALUES LESS THAN (20, 10), " + //
"\n\tPARTITION p2 VALUES LESS THAN (MAXVALUE, 15), " + //
"\n)"
+ "\nPARTITION BY RANGE COLUMNS (a, b)"
+ "\n(" + //
"\n\tPARTITION p0 VALUES LESS THAN (10, 5)," + //
"\n\tPARTITION p1 VALUES LESS THAN (20, 10)," + //
"\n\tPARTITION p2 VALUES LESS THAN (MAXVALUE, 15)," + //
"\n\tPARTITION p3 VALUES LESS THAN (MAXVALUE, MAXVALUE)" + //
"\n)", output);
@@ -65,7 +65,9 @@ public class MySqlCreateTableTest43 extends MysqlTest {
Assert.assertEquals("CREATE TABLE client_firms (" + //
"\n\tid INT, " + //
"\n\tname VARCHAR(35)" + //
"\n) PARTITION BY LIST (id) (" + //
"\n)"
+ "\nPARTITION BY LIST (id)"
+ "\n(" + //
"\n\tPARTITION r0 VALUES IN (1, 5, 9, 13, 17, 21), " + //
"\n\tPARTITION r1 VALUES IN (2, 6, 10, 14, 18, 22), " + //
"\n\tPARTITION r2 VALUES IN (3, 7, 11, 15, 19, 23), " + //
@@ -65,7 +65,9 @@ public class MySqlCreateTableTest44 extends MysqlTest {
Assert.assertEquals("CREATE TABLE lc (" + //
"\n\ta INT NULL, " + //
"\n\tb INT NULL" + //
"\n) PARTITION BY LIST COLUMNS (a, b) (" + //
"\n)" + //
"\nPARTITION BY LIST COLUMNS (a, b)"
+ "\n(" + //
"\n\tPARTITION p0 VALUES IN ((0, 0), (NULL, NULL)), " + //
"\n\tPARTITION p1 VALUES IN ((0, 1), (0, 2), (0, 3), (1, 1), (1, 2)), " + //
"\n\tPARTITION p2 VALUES IN ((1, 0), (2, 0), (2, 1), (3, 0), (3, 1)), " + //
@@ -72,7 +72,9 @@ public class MySqlCreateTableTest45 extends MysqlTest {
"\n\tid INT, " + //
"\n\tname VARCHAR(30), " + //
"\n\tadate DATE" + //
"\n) PARTITION BY LIST (YEAR(adate)) (" + //
"\n)" + //
"\nPARTITION BY LIST (YEAR(adate))" + //
"\n(" + //
"\n\tPARTITION p1999 VALUES IN (1995, 1999, 2003)" + //
"\n\t\tDATA DIRECTORY '/var/appdata/95/data'" + //
"\n\t\tINDEX DIRECTORY '/var/appdata/95/idx', " + //
@@ -51,7 +51,8 @@ public class MySqlCreateTableTest66 extends MysqlTest {
+ "\n\tb_iden_string varchar, "
+ "\n\tcounter bigint, "
+ "\n\tdurationtime bigint"
+ "\n) PARTITION BY HASH KEY(a_iden_string) PARTITION NUM 100"
+ "\n)"
+ "\nPARTITION BY HASH KEY(a_iden_string) PARTITION NUM 100"
+ "\nSUBPARTITION BY LIST (bdt bigint)"
+ "\nSUBPARTITION OPTIONS (available_Partition_Num = 90)"
+ "\nTABLEGROUP g20_test_group", output);
@@ -0,0 +1,57 @@
/*
* 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 MySqlCreateTableTest67 extends MysqlTest {
@Test
public void test_one() throws Exception {
String sql = "CREATE TABLE t1 ( a INT NOT NULL, PRIMARY KEY (a))"
+ " ENGINE=InnoDB TABLESPACE ts1 "
+ " PARTITION BY RANGE (a) PARTITIONS 3 ("
+ " PARTITION P1 VALUES LESS THAN (2),"
+ " PARTITION P2 VALUES LESS THAN (4) TABLESPACE ts2,"
+ " PARTITION P3 VALUES LESS THAN (6) TABLESPACE ts3);";
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 t1 ("
+ "\n\ta INT NOT NULL, "
+ "\n\tPRIMARY KEY (a)"
+ "\n) ENGINE = InnoDB TABLESPACE ts1"
+ "\nPARTITION BY RANGE (a) PARTITIONS 3"
+ "\n("
+ "\n\tPARTITION P1 VALUES LESS THAN (2),"
+ "\n\tPARTITION P2 VALUES LESS THAN (4) TABLESPACE ts2,"
+ "\n\tPARTITION P3 VALUES LESS THAN (6) TABLESPACE ts3"
+ "\n)", output);
}
}
@@ -0,0 +1,66 @@
/*
* 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 MySqlCreateTableTest68 extends MysqlTest {
@Test
public void test_one() throws Exception {
String sql = "CREATE TABLE t1 ("
+ "\n\tyear_col INT,"
+ "\n\tsome_data INT"
+ "\n)"
+ "\nPARTITION BY RANGE (year_col) ("
+ " PARTITION p0 VALUES LESS THAN (1991),"
+ " PARTITION p1 VALUES LESS THAN (1995),"
+ " PARTITION p2 VALUES LESS THAN (1999),"
+ " PARTITION p3 VALUES LESS THAN (2002),"
+ " PARTITION p4 VALUES LESS THAN (2006),"
+ " PARTITION p5 VALUES LESS THAN MAXVALUE"
+ ");";
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 t1 ("
+ "\n\tyear_col INT, "
+ "\n\tsome_data INT"
+ "\n)"
+ "\nPARTITION BY RANGE (year_col)"
+ "\n("
+ "\n\tPARTITION p0 VALUES LESS THAN (1991),"
+ "\n\tPARTITION p1 VALUES LESS THAN (1995),"
+ "\n\tPARTITION p2 VALUES LESS THAN (1999),"
+ "\n\tPARTITION p3 VALUES LESS THAN (2002),"
+ "\n\tPARTITION p4 VALUES LESS THAN (2006),"
+ "\n\tPARTITION p5 VALUES LESS THAN MAXVALUE"
+ "\n)", output);
}
}
@@ -0,0 +1,124 @@
/*
* 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;
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 OracleCreateTableTest36 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE composite_sales"
+ " ( prod_id NUMBER(6)"
+ " , cust_id NUMBER"
+ " , time_id DATE"
+ " , channel_id CHAR(1)"
+ " , promo_id NUMBER(6)"
+ " , quantity_sold NUMBER(3)"
+ " , amount_sold NUMBER(10,2)"
+ " ) "
+ "PARTITION BY RANGE (time_id)"
+ "SUBPARTITION BY HASH (channel_id)"
+ " (PARTITION SALES_Q1_1998 VALUES LESS THAN (TO_DATE('01-APR-1998','DD-MON-YYYY')),"
+ " PARTITION SALES_Q2_1998 VALUES LESS THAN (TO_DATE('01-JUL-1998','DD-MON-YYYY')),"
+ " PARTITION SALES_Q3_1998 VALUES LESS THAN (TO_DATE('01-OCT-1998','DD-MON-YYYY')),"
+ " PARTITION SALES_Q4_1998 VALUES LESS THAN (TO_DATE('01-JAN-1999','DD-MON-YYYY')),"
+ " PARTITION SALES_Q1_1999 VALUES LESS THAN (TO_DATE('01-APR-1999','DD-MON-YYYY')),"
+ " PARTITION SALES_Q2_1999 VALUES LESS THAN (TO_DATE('01-JUL-1999','DD-MON-YYYY')),"
+ " PARTITION SALES_Q3_1999 VALUES LESS THAN (TO_DATE('01-OCT-1999','DD-MON-YYYY')),"
+ " PARTITION SALES_Q4_1999 VALUES LESS THAN (TO_DATE('01-JAN-2000','DD-MON-YYYY')),"
+ " PARTITION SALES_Q1_2000 VALUES LESS THAN (TO_DATE('01-APR-2000','DD-MON-YYYY')),"
+ " PARTITION SALES_Q2_2000 VALUES LESS THAN (TO_DATE('01-JUL-2000','DD-MON-YYYY'))"
+ " SUBPARTITIONS 8,"
+ " PARTITION SALES_Q3_2000 VALUES LESS THAN (TO_DATE('01-OCT-2000','DD-MON-YYYY'))"
+ " (SUBPARTITION ch_c,"
+ " SUBPARTITION ch_i,"
+ " SUBPARTITION ch_p,"
+ " SUBPARTITION ch_s,"
+ " SUBPARTITION ch_t),"
+ " PARTITION SALES_Q4_2000 VALUES LESS THAN (MAXVALUE)"
+ " SUBPARTITIONS 4)"
+ ";";
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 composite_sales ("
+ "\n\tprod_id NUMBER(6),"
+ "\n\tcust_id NUMBER,"
+ "\n\ttime_id DATE,"
+ "\n\tchannel_id CHAR(1),"
+ "\n\tpromo_id NUMBER(6),"
+ "\n\tquantity_sold NUMBER(3),"
+ "\n\tamount_sold NUMBER(10, 2)"
+ "\n)"
+ "\nPARTITION BY RANGE (time_id)"
+ "\nSUBPARTITION BY HASH (channel_id)"
+ "\n("
+ "\n\tPARTITION SALES_Q1_1998 VALUES LESS THAN (TO_DATE('01-APR-1998', 'DD-MON-YYYY')),"
+ "\n\tPARTITION SALES_Q2_1998 VALUES LESS THAN (TO_DATE('01-JUL-1998', 'DD-MON-YYYY')),"
+ "\n\tPARTITION SALES_Q3_1998 VALUES LESS THAN (TO_DATE('01-OCT-1998', 'DD-MON-YYYY')),"
+ "\n\tPARTITION SALES_Q4_1998 VALUES LESS THAN (TO_DATE('01-JAN-1999', 'DD-MON-YYYY')),"
+ "\n\tPARTITION SALES_Q1_1999 VALUES LESS THAN (TO_DATE('01-APR-1999', 'DD-MON-YYYY')),"
+ "\n\tPARTITION SALES_Q2_1999 VALUES LESS THAN (TO_DATE('01-JUL-1999', 'DD-MON-YYYY')),"
+ "\n\tPARTITION SALES_Q3_1999 VALUES LESS THAN (TO_DATE('01-OCT-1999', 'DD-MON-YYYY')),"
+ "\n\tPARTITION SALES_Q4_1999 VALUES LESS THAN (TO_DATE('01-JAN-2000', 'DD-MON-YYYY')),"
+ "\n\tPARTITION SALES_Q1_2000 VALUES LESS THAN (TO_DATE('01-APR-2000', 'DD-MON-YYYY')),"
+ "\n\tPARTITION SALES_Q2_2000 VALUES LESS THAN (TO_DATE('01-JUL-2000', 'DD-MON-YYYY'))"
+ "\n\t\tSUBPARTITIONS 8,"
+ "\n\tPARTITION SALES_Q3_2000 VALUES LESS THAN (TO_DATE('01-OCT-2000', 'DD-MON-YYYY'))"
+ "\n\t("
+ "\n\t\tSUBPARTITION ch_c,"
+ "\n\t\tSUBPARTITION ch_i,"
+ "\n\t\tSUBPARTITION ch_p,"
+ "\n\t\tSUBPARTITION ch_s,"
+ "\n\t\tSUBPARTITION ch_t"
+ "\n\t),"
+ "\n\tPARTITION SALES_Q4_2000 VALUES LESS THAN (MAXVALUE)"
+ "\n\t\tSUBPARTITIONS 4"
+ "\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(7, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("composite_sales", "prod_id")));
}
}
@@ -0,0 +1,96 @@
/*
* 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;
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 OracleCreateTableTest37 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE hash_products "
+ " ( product_id NUMBER(6) PRIMARY KEY"
+ " , product_name VARCHAR2(50) "
+ " , product_description VARCHAR2(2000) "
+ " , category_id NUMBER(2) "
+ " , weight_class NUMBER(1) "
+ " , warranty_period INTERVAL YEAR TO MONTH "
+ " , supplier_id NUMBER(6) "
+ " , product_status VARCHAR2(20) "
+ " , list_price NUMBER(8,2) "
+ " , min_price NUMBER(8,2) "
+ " , catalog_url VARCHAR2(50) "
+ " , CONSTRAINT product_status_lov_demo "
+ " CHECK (product_status in ('orderable' "
+ " ,'planned' "
+ " ,'under development' "
+ " ,'obsolete') "
+ ") ) "
+ " PARTITION BY HASH (product_id) "
+ " PARTITIONS 4 "
+ " STORE IN (tbs_01, tbs_02, tbs_03, tbs_04);";
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 hash_products ("
+ "\n\tproduct_id NUMBER(6) PRIMARY KEY,"
+ "\n\tproduct_name VARCHAR2(50),"
+ "\n\tproduct_description VARCHAR2(2000),"
+ "\n\tcategory_id NUMBER(2),"
+ "\n\tweight_class NUMBER(1),"
+ "\n\twarranty_period INTERVAL YEAR TO MONTH,"
+ "\n\tsupplier_id NUMBER(6),"
+ "\n\tproduct_status VARCHAR2(20),"
+ "\n\tlist_price NUMBER(8, 2),"
+ "\n\tmin_price NUMBER(8, 2),"
+ "\n\tcatalog_url VARCHAR2(50),"
+ "\n\tCONSTRAINT product_status_lov_demo CHECK (product_status IN ('orderable', 'planned', 'under development', 'obsolete'))"
+ "\n)"
+ "\nPARTITION BY HASH (product_id) PARTITIONS 4"
+ "\nSTORE IN (tbs_01, tbs_02, tbs_03, tbs_04)",//
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(11, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("hash_products", "product_id")));
}
}
@@ -0,0 +1,88 @@
/*
* 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;
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 OracleCreateTableTest38 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE list_customers "
+ " ( customer_id NUMBER(6)"
+ " , cust_first_name VARCHAR2(20) "
+ " , cust_last_name VARCHAR2(20)"
+ " , cust_address CUST_ADDRESS_TYP"
+ " , nls_territory VARCHAR2(30)"
+ " , cust_email VARCHAR2(40))"
+ " PARTITION BY LIST (nls_territory) ("
+ " PARTITION asia VALUES ('CHINA', 'THAILAND'),"
+ " PARTITION europe VALUES ('GERMANY', 'ITALY', 'SWITZERLAND'),"
+ " PARTITION west VALUES ('AMERICA'),"
+ " PARTITION east VALUES ('INDIA'),"
+ " PARTITION rest VALUES (DEFAULT));";
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 list_customers ("
+ "\n\tcustomer_id NUMBER(6),"
+ "\n\tcust_first_name VARCHAR2(20),"
+ "\n\tcust_last_name VARCHAR2(20),"
+ "\n\tcust_address CUST_ADDRESS_TYP,"
+ "\n\tnls_territory VARCHAR2(30),"
+ "\n\tcust_email VARCHAR2(40)"
+ "\n)"
+ "\nPARTITION BY LIST (nls_territory)"
+ "\n("
+ "\n\tPARTITION asia VALUES ('CHINA', 'THAILAND'), "
+ "\n\tPARTITION europe VALUES ('GERMANY', 'ITALY', 'SWITZERLAND'), "
+ "\n\tPARTITION west VALUES ('AMERICA'), "
+ "\n\tPARTITION east VALUES ('INDIA'), "
+ "\n\tPARTITION rest VALUES (DEFAULT)"
+ "\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(6, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("list_customers", "customer_id")));
}
}
@@ -0,0 +1,77 @@
/*
* 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;
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 OracleCreateTableTest39 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE customers_demo ("
+ " customer_id number(6),"
+ " cust_first_name varchar2(20),"
+ " cust_last_name varchar2(20),"
+ " credit_limit number(9,2))"
+ "PARTITION BY RANGE (credit_limit)"
+ "INTERVAL (1000)"
+ "(PARTITION p1 VALUES LESS THAN (5001));";
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 customers_demo ("
+ "\n\tcustomer_id number(6),"
+ "\n\tcust_first_name varchar2(20),"
+ "\n\tcust_last_name varchar2(20),"
+ "\n\tcredit_limit number(9, 2)"
+ "\n)"
+ "\nPARTITION BY RANGE (credit_limit) INTERVAL 1000"
+ "\n("
+ "\n\tPARTITION p1 VALUES LESS THAN (5001)"
+ "\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("customers_demo", "customer_id")));
}
}
@@ -0,0 +1,95 @@
/*
* 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;
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 OracleCreateTableTest40 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE TABLE customers_part ("
+ " customer_id NUMBER(6),"
+ " cust_first_name VARCHAR2(20),"
+ " cust_last_name VARCHAR2(20),"
+ " nls_territory VARCHAR2(30),"
+ " credit_limit NUMBER(9,2)) "
+ " PARTITION BY RANGE (credit_limit)"
+ " SUBPARTITION BY LIST (nls_territory)"
+ " SUBPARTITION TEMPLATE "
+ " (SUBPARTITION east VALUES "
+ " ('CHINA', 'JAPAN', 'INDIA', 'THAILAND'),"
+ " SUBPARTITION west VALUES "
+ " ('AMERICA', 'GERMANY', 'ITALY', 'SWITZERLAND'),"
+ " SUBPARTITION other VALUES (DEFAULT))"
+ " (PARTITION p1 VALUES LESS THAN (1000),"
+ " PARTITION p2 VALUES LESS THAN (2500),"
+ " PARTITION p3 VALUES LESS THAN (MAXVALUE));";
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 customers_part ("
+ "\n\tcustomer_id NUMBER(6),"
+ "\n\tcust_first_name VARCHAR2(20),"
+ "\n\tcust_last_name VARCHAR2(20),"
+ "\n\tnls_territory VARCHAR2(30),"
+ "\n\tcredit_limit NUMBER(9, 2)"
+ "\n)"
+ "\nPARTITION BY RANGE (credit_limit)"
+ "\nSUBPARTITION BY HASH (nls_territory)"
+ "\n\tSUBPARTITION TEMPLATE ("
+ "\n\t\tSUBPARTITION east VALUES ('CHINA', 'JAPAN', 'INDIA', 'THAILAND'),"
+ "\n\t\tSUBPARTITION west VALUES ('AMERICA', 'GERMANY', 'ITALY', 'SWITZERLAND'),"
+ "\n\t\tSUBPARTITION other VALUES (DEFAULT)"
+ "\n\t)"
+ "\n("
+ "\n\tPARTITION p1 VALUES LESS THAN (1000),"
+ "\n\tPARTITION p2 VALUES LESS THAN (2500),"
+ "\n\tPARTITION p3 VALUES LESS THAN (MAXVALUE)"
+ "\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(5, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("customers_part", "customer_id")));
}
}
@@ -26,8 +26,6 @@ import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.ModelRulesCla
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.QueryPartitionClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.ModelClause.ReturnRowsClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleErrorLoggingClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OraclePartitionByRangeClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleRangeValuesClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleReturningClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleStorageClause;
import com.alibaba.druid.sql.dialect.oracle.ast.clause.OracleWithSubqueryEntry;
@@ -132,8 +130,6 @@ public class OracleASTVisitorAdapterTest extends TestCase {
new OracleAlterTablespaceAddDataFile().accept(adapter);
new OracleAlterTablespaceStatement().accept(adapter);
new OracleCreateSequenceStatement().accept(adapter);
new OracleRangeValuesClause().accept(adapter);
new OraclePartitionByRangeClause().accept(adapter);
new SQLLoopStatement().accept(adapter);
new OracleIntervalExpr().accept(adapter);
new OracleDeleteStatement().accept(adapter);