mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve sql parser
This commit is contained in:
@@ -3,7 +3,6 @@ package com.alibaba.druid.bvt.sql.mysql;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
|
||||
@@ -199,7 +198,6 @@ public class MySqlASTVisitorAdapterTest extends TestCase {
|
||||
new MySqlCharExpr().accept(adapter);
|
||||
new MySqlAlterTableAddUnique().accept(adapter);
|
||||
new MySqlUnique().accept(adapter);
|
||||
new MySqlForeignKey().accept(adapter);
|
||||
new MySqlAlterTableModifyColumn().accept(adapter);
|
||||
new MySqlAlterTableDiscardTablespace().accept(adapter);
|
||||
new MySqlAlterTableImportTablespace().accept(adapter);
|
||||
|
||||
@@ -47,7 +47,7 @@ public class MySqlAlterTableTest_addFk extends TestCase {
|
||||
"\n\tADD INDEX FK4CF5DC0F5DD7C31 (test1_name)," + //
|
||||
"\n\tADD CONSTRAINT FK4CF5DC0F5DD7C31 FOREIGN KEY (name) REFERENCES Test1 (name)", output);
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(0, visitor.getColumns().size());
|
||||
Assert.assertEquals(2, visitor.getTables().size());
|
||||
Assert.assertEquals(2, visitor.getColumns().size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,8 +54,8 @@ public class MySqlCreateTableTest7 extends MysqlTest {
|
||||
System.out.println("coditions : " + visitor.getConditions());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(3, visitor.getColumns().size());
|
||||
Assert.assertEquals(2, visitor.getTables().size());
|
||||
Assert.assertEquals(4, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("Orders")));
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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 OracleAlterTableTest20 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"ALTER TABLE sales " //
|
||||
+ " ADD CONSTRAINT sales_pk PRIMARY KEY (prod_id, cust_id) DISABLE; ";
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
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("ALTER TABLE sales" //
|
||||
+ "\n\tADD CONSTRAINT sales_pk PRIMARY KEY (prod_id, cust_id) DIABLE", //
|
||||
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("sales")));
|
||||
|
||||
Assert.assertEquals(2, visitor.getColumns().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("PRODUCT_IDS_ZZJ_TBD0209",
|
||||
// "discount_amount")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("ws_affiliate_tran_product",
|
||||
// "commission_amount")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "order_mode")));
|
||||
}
|
||||
}
|
||||
@@ -58,20 +58,21 @@ public class OracleCreateTableTest19 extends OracleTest {
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
Assert.assertEquals("CREATE TABLE \"SONAR\".\"ACTIVE_DASHBOARDS\" (" //
|
||||
+ "\n\t\"ID\" NUMBER(38, 0) NOT NULL ENABLE," //
|
||||
+ "\n\t\"DASHBOARD_ID\" NUMBER(38, 0) NOT NULL ENABLE," //
|
||||
+ "\n\t\"USER_ID\" NUMBER(38, 0)," //
|
||||
+ "\n\t\"ORDER_INDEX\" NUMBER(38, 0)," //
|
||||
+ "\n\tPRIMARY KEY (\"ID\")"
|
||||
+ "\n\tUSING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE \"USERS\" ENABLE"
|
||||
+ "\n)"
|
||||
+ "\nPCTFREE 10"
|
||||
+ "\nINITRANS 1"
|
||||
+ "\nMAXTRANS 255"
|
||||
+ "\nNOCOMPRESS"
|
||||
+ "\nLOGGING"
|
||||
+ "\nTABLESPACE \"USERS\""
|
||||
+ "\nSTORAGE (INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)",
|
||||
+ "\n\t\"ID\" NUMBER(38, 0) NOT NULL ENABLE," //
|
||||
+ "\n\t\"DASHBOARD_ID\" NUMBER(38, 0) NOT NULL ENABLE," //
|
||||
+ "\n\t\"USER_ID\" NUMBER(38, 0)," //
|
||||
+ "\n\t\"ORDER_INDEX\" NUMBER(38, 0)," //
|
||||
+ "\n\tPRIMARY KEY (\"ID\")" //
|
||||
+ "\n\tUSING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE \"USERS\" ENABLE"
|
||||
+ "\n\tSTORAGE (INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)" //
|
||||
+ "\n)" //
|
||||
+ "\nPCTFREE 10" //
|
||||
+ "\nINITRANS 1" //
|
||||
+ "\nMAXTRANS 255" //
|
||||
+ "\nNOCOMPRESS" //
|
||||
+ "\nLOGGING" //
|
||||
+ "\nTABLESPACE \"USERS\"" //
|
||||
+ "\nSTORAGE (INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)", //
|
||||
SQLUtils.toSQLString(statement, JdbcConstants.ORACLE));
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
|
||||
@@ -31,8 +31,11 @@ public class OracleCreateTableTest21 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = //
|
||||
"CREATE TABLE divisions " + " (div_no NUMBER(2), " + " div_name VARCHAR2(14), "
|
||||
+ " location VARCHAR2(13) ) " + " STORAGE ( INITIAL 8M MAXSIZE 1G );";
|
||||
"CREATE TABLE divisions " //
|
||||
+ " (div_no NUMBER(2), " //
|
||||
+ " div_name VARCHAR2(14), "//
|
||||
+ " location VARCHAR2(13) ) " //
|
||||
+ " STORAGE ( INITIAL 8M MAXSIZE 1G );";
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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 OracleCreateTableTest22 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = //
|
||||
"CREATE TABLE promotions_var2" //
|
||||
+ " ( promo_id NUMBER(6)"//
|
||||
+ " , promo_name VARCHAR2(20)"//
|
||||
+ " , promo_category VARCHAR2(15)"//
|
||||
+ " , promo_cost NUMBER(10,2)"//
|
||||
+ " , promo_begin_date DATE"//
|
||||
+ " , promo_end_date DATE"//
|
||||
+ " , CONSTRAINT promo_id_u UNIQUE (promo_id)"//
|
||||
+ " USING INDEX PCTFREE 20"//
|
||||
+ " TABLESPACE stocks"//
|
||||
+ " STORAGE (INITIAL 8M) );";
|
||||
|
||||
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 promotions_var2 ("
|
||||
+ "\n\tpromo_id NUMBER(6),"
|
||||
+ "\n\tpromo_name VARCHAR2(20),"
|
||||
+ "\n\tpromo_category VARCHAR2(15),"
|
||||
+ "\n\tpromo_cost NUMBER(10, 2),"
|
||||
+ "\n\tpromo_begin_date DATE,"
|
||||
+ "\n\tpromo_end_date DATE,"
|
||||
+ "\n\tCONSTRAINT promo_id_u UNIQUE (promo_id)"
|
||||
+ "\n\tUSING INDEX PCTFREE 20 TABLESPACE stocks"
|
||||
+ "\n\tSTORAGE (INITIAL 8M)"
|
||||
+ "\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("promotions_var2", "promo_id")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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 OracleCreateTableTest23 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = //
|
||||
"CREATE TABLE promotions_var1" //
|
||||
+ " ( promo_id NUMBER(6)" //
|
||||
+ " CONSTRAINT promo_id_u UNIQUE" //
|
||||
+ " , promo_name VARCHAR2(20)" //
|
||||
+ " , promo_category VARCHAR2(15)" //
|
||||
+ " , promo_cost NUMBER(10,2)" //
|
||||
+ " , promo_begin_date DATE" //
|
||||
+ " , promo_end_date DATE" //
|
||||
+ " ) ;";
|
||||
|
||||
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 promotions_var1 (" //
|
||||
+ "\n\tpromo_id NUMBER(6) CONSTRAINT promo_id_u UNIQUE," //
|
||||
+ "\n\tpromo_name VARCHAR2(20)," //
|
||||
+ "\n\tpromo_category VARCHAR2(15)," //
|
||||
+ "\n\tpromo_cost NUMBER(10, 2)," //
|
||||
+ "\n\tpromo_begin_date DATE," //
|
||||
+ "\n\tpromo_end_date DATE" //
|
||||
+ "\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("promotions_var1", "promo_id")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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 OracleCreateTableTest24 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = //
|
||||
"CREATE TABLE locations_demo" //
|
||||
+ " ( location_id NUMBER(4) CONSTRAINT loc_id_pk PRIMARY KEY" //
|
||||
+ " , street_address VARCHAR2(40)" //
|
||||
+ " , postal_code VARCHAR2(12)" //
|
||||
+ " , city VARCHAR2(30)" //
|
||||
+ " , state_province VARCHAR2(25)" //
|
||||
+ " , country_id CHAR(2)" //
|
||||
+ " ) ;";
|
||||
|
||||
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 locations_demo (" //
|
||||
+ "\n\tlocation_id NUMBER(4) CONSTRAINT loc_id_pk PRIMARY KEY," //
|
||||
+ "\n\tstreet_address VARCHAR2(40)," //
|
||||
+ "\n\tpostal_code VARCHAR2(12)," //
|
||||
+ "\n\tcity VARCHAR2(30)," //
|
||||
+ "\n\tstate_province VARCHAR2(25)," //
|
||||
+ "\n\tcountry_id CHAR(2)" //
|
||||
+ "\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("locations_demo", "location_id")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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 OracleCreateTableTest25 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = //
|
||||
"CREATE TABLE locations_demo" //
|
||||
+ " ( location_id NUMBER(4) "//
|
||||
+ " , street_address VARCHAR2(40)"//
|
||||
+ " , postal_code VARCHAR2(12)"//
|
||||
+ " , city VARCHAR2(30)"//
|
||||
+ " , state_province VARCHAR2(25)"//
|
||||
+ " , country_id CHAR(2)"//
|
||||
+ " , CONSTRAINT loc_id_pk PRIMARY KEY (location_id));";
|
||||
|
||||
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 locations_demo (" //
|
||||
+ "\n\tlocation_id NUMBER(4)," //
|
||||
+ "\n\tstreet_address VARCHAR2(40)," //
|
||||
+ "\n\tpostal_code VARCHAR2(12)," //
|
||||
+ "\n\tcity VARCHAR2(30)," //
|
||||
+ "\n\tstate_province VARCHAR2(25)," //
|
||||
+ "\n\tcountry_id CHAR(2),"
|
||||
+ "\n\tCONSTRAINT loc_id_pk PRIMARY KEY (location_id)" //
|
||||
+ "\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("locations_demo", "location_id")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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 OracleCreateTableTest26 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = //
|
||||
"CREATE TABLE dept_20 " //
|
||||
+ " (employee_id NUMBER(4), "//
|
||||
+ " last_name VARCHAR2(10), "//
|
||||
+ " job_id VARCHAR2(9), "//
|
||||
+ " manager_id NUMBER(4), "//
|
||||
+ " hire_date DATE, "//
|
||||
+ " salary NUMBER(7,2), "//
|
||||
+ " commission_pct NUMBER(7,2), "//
|
||||
+ " department_id CONSTRAINT fk_deptno "//
|
||||
+ " REFERENCES departments(department_id) ); ";
|
||||
|
||||
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),"//
|
||||
+ "\n\tlast_name VARCHAR2(10),"//
|
||||
+ "\n\tjob_id VARCHAR2(9),"//
|
||||
+ "\n\tmanager_id NUMBER(4),"//
|
||||
+ "\n\thire_date DATE,"//
|
||||
+ "\n\tsalary NUMBER(7, 2),"//
|
||||
+ "\n\tcommission_pct NUMBER(7, 2),"//
|
||||
+ "\n\tdepartment_id"//
|
||||
+ "\n\t\tCONSTRAINT fk_deptno REFERENCES departments (department_id)"//
|
||||
+ "\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,82 @@
|
||||
/*
|
||||
* 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 OracleCreateTableTest27 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = //
|
||||
"CREATE TABLE dept_20 " //
|
||||
+ " (employee_id NUMBER(4), " //
|
||||
+ " last_name VARCHAR2(10), " //
|
||||
+ " job_id VARCHAR2(9), " //
|
||||
+ " manager_id NUMBER(4), " //
|
||||
+ " hire_date DATE, " //
|
||||
+ " salary NUMBER(7,2), " //
|
||||
+ " commission_pct NUMBER(7,2), " //
|
||||
+ " department_id, " //
|
||||
+ " CONSTRAINT fk_deptno " //
|
||||
+ " FOREIGN KEY (department_id) " //
|
||||
+ " REFERENCES departments(department_id) ); ";
|
||||
|
||||
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),"//
|
||||
+ "\n\tlast_name VARCHAR2(10),"//
|
||||
+ "\n\tjob_id VARCHAR2(9),"//
|
||||
+ "\n\tmanager_id NUMBER(4),"//
|
||||
+ "\n\thire_date DATE,"//
|
||||
+ "\n\tsalary NUMBER(7, 2),"//
|
||||
+ "\n\tcommission_pct NUMBER(7, 2),"//
|
||||
+ "\n\tdepartment_id,"//
|
||||
+ "\n\tCONSTRAINT fk_deptno FOREIGN KEY (department_id) REFERENCES departments (department_id)"//
|
||||
+ "\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(2, visitor.getTables().size());
|
||||
|
||||
Assert.assertEquals(9, visitor.getColumns().size());
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("dept_20", "employee_id")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user