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:
@@ -0,0 +1,26 @@
|
||||
package com.alibaba.druid.bvt.filter.wall.mysql;
|
||||
|
||||
import com.alibaba.druid.wall.WallProvider;
|
||||
import com.alibaba.druid.wall.spi.MySqlWallProvider;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
public class MySqlWallTest146 extends TestCase {
|
||||
|
||||
public void test_false() throws Exception {
|
||||
WallProvider provider = new MySqlWallProvider();
|
||||
|
||||
String sql = "ALTER TABLE project_measures\n" +
|
||||
" DROP COLUMN diff_value_1,\n" +
|
||||
" DROP COLUMN diff_value_2,\n" +
|
||||
" DROP COLUMN diff_value_3,\n" +
|
||||
" ADD COLUMN variation_value_1 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
" ADD COLUMN variation_value_2 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
" ADD COLUMN variation_value_3 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
" ADD COLUMN variation_value_4 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
" ADD COLUMN variation_value_5 DECIMAL(30, 20) NULL DEFAULT NULL";
|
||||
Assert.assertTrue(
|
||||
provider.checkValid(sql)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.alter;
|
||||
|
||||
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.parser.Token;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
public class MySqlAlterTableTest28 extends TestCase {
|
||||
|
||||
public void test_alter_add_key() throws Exception {
|
||||
String sql = "ALTER TABLE project_measures MODIFY COLUMN value DECIMAL (38,20) NULL, MODIFY COLUMN variation_value_1 DECIMAL (38,20) NULL, MODIFY COLUMN variation_value_2 DECIMAL (38,20) NULL, MODIFY COLUMN variation_value_3 DECIMAL (38,20) NULL, MODIFY COLUMN variation_value_4 DECIMAL (38,20) NULL, MODIFY COLUMN variation_value_5 DECIMAL (38,20) NULL";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("ALTER TABLE project_measures\n" +
|
||||
"\tMODIFY COLUMN value DECIMAL(38, 20) NULL,\n" +
|
||||
"\tMODIFY COLUMN variation_value_1 DECIMAL(38, 20) NULL,\n" +
|
||||
"\tMODIFY COLUMN variation_value_2 DECIMAL(38, 20) NULL,\n" +
|
||||
"\tMODIFY COLUMN variation_value_3 DECIMAL(38, 20) NULL,\n" +
|
||||
"\tMODIFY COLUMN variation_value_4 DECIMAL(38, 20) NULL,\n" +
|
||||
"\tMODIFY COLUMN variation_value_5 DECIMAL(38, 20) NULL", output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.alter;
|
||||
|
||||
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.parser.Token;
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Assert;
|
||||
|
||||
public class MySqlAlterTableTest29 extends TestCase {
|
||||
|
||||
public void test_alter_add_key() throws Exception {
|
||||
String sql = "alter TABLE project_measures\n" +
|
||||
" DROP COLUMN diff_value_1,\n" +
|
||||
" DROP COLUMN diff_value_2,\n" +
|
||||
" DROP COLUMN diff_value_3,\n" +
|
||||
" ADD COLUMN variation_value_1 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
" ADD COLUMN variation_value_2 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
" ADD COLUMN variation_value_3 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
" ADD COLUMN variation_value_4 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
" ADD COLUMN variation_value_5 DECIMAL(30, 20) NULL DEFAULT NULL";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("ALTER TABLE project_measures\n" +
|
||||
"\tDROP COLUMN diff_value_1,\n" +
|
||||
"\tDROP COLUMN diff_value_2,\n" +
|
||||
"\tDROP COLUMN diff_value_3,\n" +
|
||||
"\tADD COLUMN variation_value_1 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
"\tADD COLUMN variation_value_2 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
"\tADD COLUMN variation_value_3 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
"\tADD COLUMN variation_value_4 DECIMAL(30, 20) NULL DEFAULT NULL,\n" +
|
||||
"\tADD COLUMN variation_value_5 DECIMAL(30, 20) NULL DEFAULT NULL", output);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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.oracle.alter;
|
||||
|
||||
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;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleAlterTableTest24 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"RENAME new_duplications_index TO duplications_index";
|
||||
|
||||
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("RENAME new_duplications_index TO duplications_index", //
|
||||
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("new_duplications_index")));
|
||||
|
||||
Assert.assertEquals(0, visitor.getColumns().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "commission_pct")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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.oracle.block;
|
||||
|
||||
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.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleBlockTest16 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "DECLARE\n" +
|
||||
" s PLS_INTEGER := 0;\n" +
|
||||
" i PLS_INTEGER := 0;\n" +
|
||||
" j PLS_INTEGER;\n" +
|
||||
"BEGIN\n" +
|
||||
" <<outer_loop>>\n" +
|
||||
" LOOP\n" +
|
||||
" i := i + 1;\n" +
|
||||
" j := 0;\n" +
|
||||
" <<inner_loop>>\n" +
|
||||
" LOOP\n" +
|
||||
" j := j + 1;\n" +
|
||||
" s := s + i * j; -- Sum several products\n" +
|
||||
" EXIT inner_loop WHEN (j > 5);\n" +
|
||||
" EXIT outer_loop WHEN ((i * j) > 15);\n" +
|
||||
" END LOOP inner_loop;\n" +
|
||||
" END LOOP outer_loop;\n" +
|
||||
" DBMS_OUTPUT.PUT_LINE\n" +
|
||||
" ('The sum of products equals: ' || TO_CHAR(s));\n" +
|
||||
"END;"; //
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
String result = SQLUtils.toOracleString(stmtList.get(0));
|
||||
System.out.println(result);
|
||||
assertEquals("DECLARE\n" +
|
||||
"\ts PLS_INTEGER := 0;\n" +
|
||||
"\ti PLS_INTEGER := 0;\n" +
|
||||
"\tj PLS_INTEGER;\n" +
|
||||
"BEGIN\n" +
|
||||
"\t<<outer_loop>>\n" +
|
||||
"\tLOOP\n" +
|
||||
"\t\ti := i + 1;\n" +
|
||||
"\t\tj := 0;\n" +
|
||||
"\t\t<<inner_loop>>\n" +
|
||||
"\t\tLOOP\n" +
|
||||
"\t\t\tj := j + 1;\n" +
|
||||
"\t\t\ts := s + i * j;\n" +
|
||||
"\t\t\tEXIT inner_loop WHEN j > 5;\n" +
|
||||
"\t\t\tEXIT outer_loop WHEN i * j > 15;\n" +
|
||||
"\t\tEND LOOP inner_loop;\n" +
|
||||
"\tEND LOOP outer_loop;\n" +
|
||||
"\tDBMS_OUTPUT.PUT_LINE('The sum of products equals: ' || TO_CHAR(s));\n" +
|
||||
"END;", result);
|
||||
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
for (SQLStatement statement : stmtList) {
|
||||
statement.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());
|
||||
|
||||
assertEquals(0, visitor.getTables().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("employees")));
|
||||
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
assertEquals(0, visitor.getRelationships().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* 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.oracle.block;
|
||||
|
||||
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.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleBlockTest17 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "DECLARE\n" +
|
||||
" x NUMBER := 0;\n" +
|
||||
"BEGIN\n" +
|
||||
" LOOP -- After CONTINUE statement, control resumes here\n" +
|
||||
" DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || TO_CHAR(x));\n" +
|
||||
" x := x + 1;\n" +
|
||||
" IF x < 3 THEN\n" +
|
||||
" CONTINUE;\n" +
|
||||
" END IF;\n" +
|
||||
" DBMS_OUTPUT.PUT_LINE\n" +
|
||||
" ('Inside loop, after CONTINUE: x = ' || TO_CHAR(x));\n" +
|
||||
" EXIT WHEN x = 5;\n" +
|
||||
" END LOOP;\n" +
|
||||
" \n" +
|
||||
" DBMS_OUTPUT.PUT_LINE (' After loop: x = ' || TO_CHAR(x));\n" +
|
||||
"END;"; //
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
String result = SQLUtils.toOracleString(stmtList.get(0));
|
||||
System.out.println(result);
|
||||
assertEquals("DECLARE\n" +
|
||||
"\tx NUMBER := 0;\n" +
|
||||
"BEGIN\n" +
|
||||
"\tLOOP\n" +
|
||||
"\t\tDBMS_OUTPUT.PUT_LINE('Inside loop: x = ' || TO_CHAR(x));\n" +
|
||||
"\t\tx := x + 1;\n" +
|
||||
"\t\tIF x < 3 THEN\n" +
|
||||
"\t\t\tCONTINUE;\n" +
|
||||
"\t\tEND IF;\n" +
|
||||
"\t\tDBMS_OUTPUT.PUT_LINE('Inside loop, after CONTINUE: x = ' || TO_CHAR(x));\n" +
|
||||
"\t\tEXIT WHEN x = 5;\n" +
|
||||
"\tEND LOOP;\n" +
|
||||
"\tDBMS_OUTPUT.PUT_LINE(' After loop: x = ' || TO_CHAR(x));\n" +
|
||||
"END;", result);
|
||||
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
for (SQLStatement statement : stmtList) {
|
||||
statement.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());
|
||||
|
||||
assertEquals(0, visitor.getTables().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("employees")));
|
||||
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
assertEquals(0, visitor.getRelationships().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* 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.oracle.block;
|
||||
|
||||
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.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleBlockTest18 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "CREATE PROCEDURE p (\n" +
|
||||
" sales NUMBER,\n" +
|
||||
" quota NUMBER,\n" +
|
||||
" emp_id NUMBER\n" +
|
||||
" )\n" +
|
||||
" IS\n" +
|
||||
" bonus NUMBER := 0;\n" +
|
||||
" BEGIN\n" +
|
||||
" IF sales > (quota + 200) THEN\n" +
|
||||
" bonus := (sales - quota)/4;\n" +
|
||||
" ELSE\n" +
|
||||
" bonus := 50;\n" +
|
||||
" END IF;\n" +
|
||||
" \n" +
|
||||
" DBMS_OUTPUT.PUT_LINE('bonus = ' || bonus);\n" +
|
||||
" \n" +
|
||||
" UPDATE employees\n" +
|
||||
" SET salary = salary + bonus \n" +
|
||||
" WHERE employee_id = emp_id;\n" +
|
||||
" END p;"; //
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
String result = SQLUtils.toOracleString(stmtList.get(0));
|
||||
System.out.println(result);
|
||||
assertEquals("CREATE PROCEDURE p (\n" +
|
||||
"\tsales NUMBER, \n" +
|
||||
"\tquota NUMBER, \n" +
|
||||
"\temp_id NUMBER\n" +
|
||||
")\n" +
|
||||
"AS\n" +
|
||||
"\tbonus NUMBER := 0;\n" +
|
||||
"BEGIN\n" +
|
||||
"\tIF sales > quota + 200 THEN\n" +
|
||||
"\t\tbonus := (sales - quota) / 4;\n" +
|
||||
"\tELSE\n" +
|
||||
"\t\tbonus := 50;\n" +
|
||||
"\tEND IF;\n" +
|
||||
"\tDBMS_OUTPUT.PUT_LINE('bonus = ' || bonus);\n" +
|
||||
"\tUPDATE employees\n" +
|
||||
"\tSET salary = salary + bonus\n" +
|
||||
"\tWHERE employee_id = emp_id;\n" +
|
||||
"END;", result);
|
||||
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
for (SQLStatement statement : stmtList) {
|
||||
statement.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());
|
||||
|
||||
assertEquals(1, visitor.getTables().size());
|
||||
|
||||
assertTrue(visitor.getTables().containsKey(new TableStat.Name("employees")));
|
||||
|
||||
assertEquals(5, visitor.getColumns().size());
|
||||
assertEquals(3, visitor.getConditions().size());
|
||||
assertEquals(1, visitor.getRelationships().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.oracle.block;
|
||||
|
||||
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.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleBlockTest19 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "BEGIN\n" +
|
||||
" EXECUTE IMMEDIATE 'DROP SEQUENCE properties_seq';\n" +
|
||||
"EXCEPTION\n" +
|
||||
" WHEN OTHERS THEN\n" +
|
||||
" IF SQLCODE != -2289 THEN\n" +
|
||||
" RAISE;\n" +
|
||||
" END IF;\n" +
|
||||
"END;"; //
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
String result = SQLUtils.toOracleString(stmtList.get(0));
|
||||
System.out.println(result);
|
||||
assertEquals("BEGIN\n" +
|
||||
"\tEXECUTE IMMEDIATE 'DROP SEQUENCE properties_seq';\n" +
|
||||
"EXCEPTION\n" +
|
||||
"\tWHEN OTHERS THEN\n" +
|
||||
"\t\tIF SQLCODE != -2289 THEN\n" +
|
||||
"\t\t\tRAISE;\n" +
|
||||
"\t\tEND IF;\n" +
|
||||
"END;", result);
|
||||
|
||||
assertEquals(1, stmtList.size());
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
for (SQLStatement statement : stmtList) {
|
||||
statement.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());
|
||||
|
||||
assertEquals(0, visitor.getTables().size());
|
||||
|
||||
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("employees")));
|
||||
|
||||
// assertEquals(5, visitor.getColumns().size());
|
||||
// assertEquals(0, visitor.getConditions().size());
|
||||
assertEquals(0, visitor.getRelationships().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
|
||||
}
|
||||
}
|
||||
@@ -112,8 +112,8 @@ public class OracleCreatePackageTest0 extends OracleTest {
|
||||
|
||||
Assert.assertEquals("CREATE OR REPLACE PACKAGE BODY emp_mgmt\n" +
|
||||
"BEGIN\n" +
|
||||
"\ttot_emps NUMBER\n" +
|
||||
"\ttot_depts NUMBER\n" +
|
||||
"\ttot_emps NUMBER;\n" +
|
||||
"\ttot_depts NUMBER;\n" +
|
||||
"\tFUNCTION hire (\n" +
|
||||
"\t\tlast_name VARCHAR2, \n" +
|
||||
"\t\tjob_id VARCHAR2, \n" +
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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.oracle.create;
|
||||
|
||||
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;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleCreateTableTest67 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = //
|
||||
" CREATE TABLE new_duplications_index (\"ID\" PRIMARY KEY, project_snapshot_id, snapshot_id, hash, index_in_file, start_line, end_line) NOLOGGING AS SELECT CAST(duplications_index_seq.nextval AS NUMBER(38)) \"ID\", project_snapshot_id, snapshot_id, hash, index_in_file, start_line, end_line FROM duplications_index ";
|
||||
|
||||
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 new_duplications_index (\n" +
|
||||
"\t\"ID\" PRIMARY KEY,\n" +
|
||||
"\tproject_snapshot_id,\n" +
|
||||
"\tsnapshot_id,\n" +
|
||||
"\thash,\n" +
|
||||
"\tindex_in_file,\n" +
|
||||
"\tstart_line,\n" +
|
||||
"\tend_line\n" +
|
||||
")\n" +
|
||||
"NOLOGGING\n" +
|
||||
"AS\n" +
|
||||
"SELECT CAST(duplications_index_seq.NEXTVAL AS NUMBER(38)) AS \"ID\", project_snapshot_id, snapshot_id, hash, index_in_file\n" +
|
||||
"\t, start_line, end_line\n" +
|
||||
"FROM duplications_index",//
|
||||
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(13, visitor.getColumns().size());
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("duplications_index", "project_snapshot_id")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.oracle.create;
|
||||
|
||||
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.util.JdbcConstants;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleCreateTriggerTest2 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"CREATE OR REPLACE TRIGGER projects_idt\n" +
|
||||
" BEFORE INSERT ON projects\n" +
|
||||
" FOR EACH ROW\n" +
|
||||
" BEGIN\n" +
|
||||
" IF :new.id IS null THEN\n" +
|
||||
" SELECT projects_seq.nextval INTO :new.id FROM dual;\n" +
|
||||
" END IF;\n" +
|
||||
" END;";
|
||||
|
||||
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 OR REPLEACE TRIGGER projects_idt\n" +
|
||||
"\tBEFORE INSERT\n" +
|
||||
"\tON projects\n" +
|
||||
"\tFOR EACH ROW\n" +
|
||||
"BEGIN\n" +
|
||||
"\tIF :new.id IS NULL THEN\n" +
|
||||
"\t\tSELECT projects_seq.NEXTVAL\n" +
|
||||
"\t\tINTO :new.id\n" +
|
||||
"\t\tFROM dual;\n" +
|
||||
"\tEND IF;\n" +
|
||||
"END;",//
|
||||
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(0, visitor.getTables().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("cdc.en_complaint_ipr_stat_fdt0")));
|
||||
|
||||
Assert.assertEquals(0, visitor.getColumns().size());
|
||||
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "*")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "YEAR")));
|
||||
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "order_mode")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.sql.oracle.demo;
|
||||
|
||||
import com.alibaba.druid.sql.OracleTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.util.Utils;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.util.List;
|
||||
|
||||
public class OracleResourceTest extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
File file = new File("/Users/wenshao/Downloads/eoa_tmp_sqltext_1.csv");
|
||||
|
||||
String sql = FileUtils.readFileToString(file);
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
|
||||
// Assert.assertEquals(1, statementList.size());
|
||||
|
||||
System.out.println(sql);
|
||||
|
||||
print(statementList);
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
|
||||
for (int i = 0, size = statementList.size(); i < size; ++i) {
|
||||
SQLStatement statement = statementList.get(i);
|
||||
statement.accept(visitor);
|
||||
}
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
void mergValidate(String sql, String expect) {
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out, true);
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println(out.toString());
|
||||
|
||||
Assert.assertEquals(expect, out.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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.sql.oracle.demo;
|
||||
|
||||
import com.alibaba.druid.sql.OracleTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Assert;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
public class OracleResourceTest2 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
File file = new File("/Users/wenshao/Downloads/Procedures.sql");
|
||||
|
||||
String sql = FileUtils.readFileToString(file);
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
|
||||
// Assert.assertEquals(1, statementList.size());
|
||||
|
||||
System.out.println(sql);
|
||||
|
||||
print(statementList);
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
|
||||
for (int i = 0, size = statementList.size(); i < size; ++i) {
|
||||
SQLStatement statement = statementList.get(i);
|
||||
statement.accept(visitor);
|
||||
}
|
||||
|
||||
System.out.println("Tables : " + visitor.getTables());
|
||||
System.out.println("fields : " + visitor.getColumns());
|
||||
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
void mergValidate(String sql, String expect) {
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement statemen = statementList.get(0);
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out, true);
|
||||
statemen.accept(visitor);
|
||||
|
||||
System.out.println(out.toString());
|
||||
|
||||
Assert.assertEquals(expect, out.toString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user