improved sql parsre.

This commit is contained in:
wenshao
2017-10-11 19:59:45 +08:00
parent 2d88d76e80
commit c6cbb328ea
233 changed files with 3589 additions and 182 deletions
@@ -56,11 +56,11 @@ public class DB2SelectTest_23 extends DB2Test {
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
// Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
Assert.assertEquals("SELECT CURRENT DATE, CURRENT DATE\n" +
Assert.assertEquals("SELECT CURRENT DATE, CURRENT TIMESTAMP\n" +
"FROM dual", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2));
Assert.assertEquals("select CURRENT DATE, CURRENT DATE\n" +
Assert.assertEquals("select CURRENT DATE, CURRENT TIMESTAMP\n" +
"from dual", //
SQLUtils.toSQLString(stmt, JdbcConstants.DB2, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
}
@@ -71,7 +71,7 @@ public class MySqlSelectTest_42_with_cte extends MysqlTest {
"\t\tSELECT c, d\n" +
"\t\tFROM table2\n" +
"\t)\n" +
"SELECT cte1.b, cte2.d\n" +
"SELECT b, d\n" +
"FROM cte1\n" +
"\tJOIN cte2\n" +
"WHERE cte1.a = cte2.c;", //
@@ -87,7 +87,7 @@ public class MySqlSelectTest_42_with_cte extends MysqlTest {
"\t\tselect c, d\n" +
"\t\tfrom table2\n" +
"\t)\n" +
"select cte1.b, cte2.d\n" +
"select b, d\n" +
"from cte1\n" +
"\tjoin cte2\n" +
"where cte1.a = cte2.c;", //
@@ -104,7 +104,7 @@ public class MySqlSelectTest_42_with_cte extends MysqlTest {
"\t\tSELECT c, d\n" +
"\t\tFROM table2\n" +
"\t)\n" +
"SELECT cte1.b, cte2.d\n" +
"SELECT b, d\n" +
"FROM cte1\n" +
"\tJOIN cte2\n" +
"WHERE cte1.a = cte2.c;", //
@@ -62,7 +62,7 @@ public class MySqlSelectTest_43_with_cte extends MysqlTest {
"\t\tSELECT c, d\n" +
"\t\tFROM table2\n" +
"\t)\n" +
"SELECT cte1.b, cte2.d\n" +
"SELECT b, d\n" +
"FROM cte1\n" +
"\tJOIN cte2\n" +
"WHERE cte1.a = cte2.c;", //
@@ -78,7 +78,7 @@ public class MySqlSelectTest_43_with_cte extends MysqlTest {
"\t\tselect c, d\n" +
"\t\tfrom table2\n" +
"\t)\n" +
"select cte1.b, cte2.d\n" +
"select b, d\n" +
"from cte1\n" +
"\tjoin cte2\n" +
"where cte1.a = cte2.c;", //
@@ -95,7 +95,7 @@ public class MySqlSelectTest_43_with_cte extends MysqlTest {
"\t\tSELECT c, d\n" +
"\t\tFROM table2\n" +
"\t)\n" +
"SELECT cte1.b, cte2.d\n" +
"SELECT b, d\n" +
"FROM cte1\n" +
"\tJOIN cte2\n" +
"WHERE cte1.a = cte2.c;", //
@@ -0,0 +1,101 @@
/*
* 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.select;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.parser.SQLParserFeature;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import java.util.List;
public class MySqlSelectTest_69_mysql8_windows_function extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT\n" +
" val,\n" +
" ROW_NUMBER() OVER w AS 'row_number',\n" +
" CUME_DIST() OVER w AS 'cume_dist',\n" +
" PERCENT_RANK() OVER w AS 'percent_rank'\n" +
" FROM numbers\n" +
" WINDOW w AS (ORDER BY val);";
System.out.println(sql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL, SQLParserFeature.OptimizedForParameterized);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
System.out.println(stmt);
assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("WITH RECURSIVE ancestors AS (\n" +
"\t\tSELECT *\n" +
"\t\tFROM org\n" +
"\t\tWHERE 1 = 1\n" +
"\t\t\tAND deleted = 1\n" +
"\t\tUNION\n" +
"\t\tSELECT f.*\n" +
"\t\tFROM org f, ancestors a\n" +
"\t\tWHERE f.id = a.parent_id\n" +
"\t)\n" +
"SELECT *\n" +
"FROM ancestors;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("with recursive ancestors as (\n" +
"\t\tselect *\n" +
"\t\tfrom org\n" +
"\t\twhere 1 = 1\n" +
"\t\t\tand deleted = 1\n" +
"\t\tunion\n" +
"\t\tselect f.*\n" +
"\t\tfrom org f, ancestors a\n" +
"\t\twhere f.id = a.parent_id\n" +
"\t)\n" +
"select *\n" +
"from ancestors;", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
assertEquals("WITH RECURSIVE ancestors AS (\n" +
"\t\tSELECT *\n" +
"\t\tFROM org\n" +
"\t\tWHERE 1 = 1\n" +
"\t\t\tAND deleted = ?\n" +
"\t\tUNION\n" +
"\t\tSELECT f.*\n" +
"\t\tFROM org f, ancestors a\n" +
"\t\tWHERE f.id = a.parent_id\n" +
"\t)\n" +
"SELECT *\n" +
"FROM ancestors;", //
output);
}
}
}
@@ -0,0 +1,22 @@
package com.alibaba.druid.bvt.sql.oracle;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
public class AntlrExamplesTest extends TestCase {
public void test_for_antlr_examples() throws Exception {
String path = "bvt/parser/antlr_grammers_v4_plsql/examples/";
URL resource = Thread.currentThread().getContextClassLoader().getResource(path);
File dir = new File(resource.getFile());
for (File file : dir.listFiles()) {
System.out.println(file);
String sql = FileUtils.readFileToString(file);
SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
}
}
}
@@ -0,0 +1,61 @@
/*
* 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;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLMergeStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import org.junit.Assert;
import java.util.List;
public class OracleMergeTest10 extends OracleTest {
public void test_0() throws Exception {
String sql = "merge into bonuses d \n" +
" using (select employee_id.* from employees) s \n" +
" on (employee_id = a) \n" +
" when not matched then insert (d.employee_id, d.bonus) \n" +
" values (s.employee_id, s.salary)\n" +
" where (s.salary <= 8000)\n" +
" when matched then update set d.bonus = bonus \n" +
" delete where (salary > 8000)";
SQLStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> stmtList = parser.parseStatementList();
SQLMergeStatement mergeStatement = (SQLMergeStatement) stmtList.get(0);
String result = SQLUtils.toOracleString(mergeStatement);
Assert.assertEquals("MERGE INTO bonuses d\n" +
"USING (\n" +
"\tSELECT employee_id.*\n" +
"\tFROM employees\n" +
") s ON (employee_id = a) \n" +
"WHEN MATCHED THEN UPDATE SET d.bonus = bonus\n" +
"\tWHERE salary > 8000\n" +
"WHEN NOT MATCHED THEN INSERT (d.employee_id, d.bonus) VALUES (s.employee_id, s.salary)\n" +
"\tWHERE s.salary <= 8000",
result);
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "employee_id")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "department_id")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("bonuses", "employee_id")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("bonuses", "bonus")));
}
}
@@ -55,7 +55,7 @@ public class OracleSelectTest61 extends OracleTest {
Assert.assertEquals("SELECT column_expression\n" +
"FROM sys.ALL_IND_EXPRESSIONS\n" +
"WHERE column_position = :cp", text);
"WHERE column_position = : cp", text);
}
{
@@ -63,7 +63,7 @@ public class OracleSelectTest61 extends OracleTest {
Assert.assertEquals("select column_expression\n" +
"from sys.ALL_IND_EXPRESSIONS\n" +
"where column_position = :cp", text);
"where column_position = : cp", text);
}
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
@@ -81,7 +81,7 @@ public class OracleSelectTest64_with_recursive extends OracleTest {
"\t\tWHERE parent_id IS NULL\n" +
"\t\tUNION ALL\n" +
"\t\t-- Recursive member.\n" +
"\t\tSELECT t2.id, t2.parent_id, t1.lvl + 1\n" +
"\t\tSELECT t2.id, t2.parent_id, lvl + 1\n" +
"\t\tFROM tab1 t2, t1\n" +
"\t\tWHERE t2.parent_id = t1.id\n" +
"\t)\n" +
@@ -103,7 +103,7 @@ public class OracleSelectTest64_with_recursive extends OracleTest {
"\t\twhere parent_id is null\n" +
"\t\tunion all\n" +
"\t\t-- Recursive member.\n" +
"\t\tselect t2.id, t2.parent_id, t1.lvl + 1\n" +
"\t\tselect t2.id, t2.parent_id, lvl + 1\n" +
"\t\tfrom tab1 t2, t1\n" +
"\t\twhere t2.parent_id = t1.id\n" +
"\t)\n" +
@@ -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.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest87_bindvar extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select *\n" +
"from a\n" +
"where a=:3\n" +
"and b= : 4\n" +
"and c= :5and :a = :b"; //
System.out.println(sql);
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT *\n" +
"FROM a\n" +
"WHERE a = :3\n" +
"\tAND b = : 4\n" +
"\tAND c = :5\n" +
"\tAND :a = :b", text);
}
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());
assertEquals(4, visitor.getColumns().size());
assertEquals(3, visitor.getConditions().size());
assertEquals(0, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
}
}
@@ -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.bvt.sql.oracle.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest88_bindvar extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select *\n" +
"from \n" +
"(\n" +
" select *\n" +
" from \"rme\" \"rm\" \n" +
" where \"rm\".a-interval:\"sys_b_07\" day(:\"sys_b_08\") to second(:\"sys_b_09\")\n" +
")"; //
System.out.println(sql);
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT *\n" +
"FROM (\n" +
"\tSELECT *\n" +
"\tFROM \"rme\" \"rm\"\n" +
"\tWHERE \"rm\".a - (:\"sys_b_07\") DAY(:\"sys_b_08\") TO SECOND(:\"sys_b_09\")\n" +
")", text);
}
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());
assertEquals(2, visitor.getColumns().size());
assertEquals(0, visitor.getConditions().size());
assertEquals(0, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
}
}
@@ -0,0 +1,88 @@
/*
* 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.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest89_condition extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select * from v.e\n" +
"where\n" +
"\tcid <> rid\n" +
"\tand rid not in\n" +
"\t(\n" +
"\t\t(select distinct rid from v.s )\n" +
"\t\tunion\n" +
"\t\t(select distinct rid from v.p )\n" +
"\t)\n" +
"\tand \"timestamp\" <= 1298505600000\n" +
"\n"; //
System.out.println(sql);
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT *\n" +
"FROM v.e\n" +
"WHERE cid <> rid\n" +
"\tAND rid NOT IN (\n" +
"\t\tSELECT DISTINCT rid\n" +
"\t\tFROM v.s\n" +
"\t\tUNION\n" +
"\t\tSELECT DISTINCT rid\n" +
"\t\tFROM v.p\n" +
"\t)\n" +
"\tAND \"timestamp\" <= 1298505600000", text);
}
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(3, visitor.getTables().size());
assertEquals(6, visitor.getColumns().size());
assertEquals(4, visitor.getConditions().size());
assertEquals(1, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
}
}
@@ -0,0 +1,69 @@
/*
* 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.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest90_isof extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select * from persons p\n" +
" where value(p) is of type(only employee_t)"; //
System.out.println(sql);
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT *\n" +
"FROM persons p\n" +
"WHERE value(p) IS OF TYPE (ONLY employee_t)", text);
}
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());
assertEquals(1, visitor.getColumns().size());
assertEquals(0, visitor.getConditions().size());
assertEquals(0, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
}
}
@@ -0,0 +1,74 @@
/*
* 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.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest91 extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select *\n" +
"from hdr a\n" +
"inner join sh s\n" +
"inner join ca c\n" +
"on c.id = s.id\n" +
"on a.va = s.va"; //
System.out.println(sql);
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT *\n" +
"FROM hdr a\n" +
"INNER JOIN sh s ON a.va = s.va \n" +
"\tINNER JOIN ca c ON c.id = s.id ", text);
}
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(3, visitor.getTables().size());
assertEquals(4, visitor.getColumns().size());
assertEquals(4, visitor.getConditions().size());
assertEquals(2, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
}
}
@@ -0,0 +1,70 @@
/*
* 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.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest92_lexer extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select * from dual where 1 < > 2 and 1 ! = 2 and 1 ^ /*aaa */ = 2"; //
System.out.println(sql);
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT *\n" +
"FROM dual\n" +
"WHERE 1 <> 2\n" +
"\tAND 1 != 2\n" +
"\tAND 1 ^= 2", text);
}
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());
assertEquals(1, visitor.getColumns().size());
assertEquals(0, visitor.getConditions().size());
assertEquals(0, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
}
}
@@ -0,0 +1,90 @@
/*
* 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.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest93_model extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select country,prod,year,s\n" +
"from sales_view_ref\n" +
"model\n" +
"partition by (country)\n" +
"dimension by (prod, year)\n" +
"measures (sale s)\n" +
"ignore nav\n" +
"-- cell_reference_options\n" +
"unique dimension\n" +
"-- here starts model_rules_clause\n" +
"rules upsert sequential order\n" +
"(\n" +
"s[prod='mouse pad', year=2001] = s['mouse pad', 1999] + s['mouse pad', 2000],\n" +
"s['standard mouse', 2002] = s['standard mouse', 2001]\n" +
")\n" +
"order by country, prod, year"; //
System.out.println(sql);
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT country, prod, year, s\n" +
"FROM sales_view_ref\n" +
"MODEL\n" +
"\tPARTITION BY (country)\n" +
"\tDIMENSION BY (prod, year)\n" +
"\tMEASURES (sale s)\n" +
"\tIGNORE NAV\n" +
"\tUNIQUE DIMENSION\n" +
"\tRULES UPSERT SEQUENTIAL ORDER (s[prod = 'mouse pad', year = 2001] = s['mouse pad', 1999] + s['mouse pad', 2000], s['standard mouse', 2002] = s['standard mouse', 2001])\n" +
"ORDER BY country, prod, year", text);
}
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());
assertEquals(4, visitor.getColumns().size());
assertEquals(0, visitor.getConditions().size());
assertEquals(0, visitor.getRelationships().size());
assertEquals(3, visitor.getOrderByColumns().size());
}
}
@@ -0,0 +1,83 @@
/*
* 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.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest94 extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select * from (\n" +
" select times_purchased as \"puchase frequency\", state_code\n" +
" from customers t\n" +
" )\n" +
" pivot xml\n" +
" (\n" +
" count(state_code)\n" +
" for state_code in (select state_code from preferred_states)\n" +
" )\n" +
" order by 1"; //
System.out.println(sql);
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT *\n" +
"FROM (\n" +
"\tSELECT times_purchased AS \"puchase frequency\", state_code\n" +
"\tFROM customers t\n" +
")\n" +
"PIVOT XML (COUNT(state_code) FOR state_code IN (\n" +
"\tSELECT state_code\n" +
"\tFROM preferred_states))\n" +
"ORDER BY 1", text);
}
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(2, visitor.getTables().size());
assertEquals(3, visitor.getColumns().size());
assertEquals(0, visitor.getConditions().size());
assertEquals(0, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
}
}
@@ -0,0 +1,88 @@
/*
* 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.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest95_pivot extends OracleTest {
public void test_0() throws Exception {
String sql = //
" select *\n" +
" from (s join d using(c))\n" +
" pivot\n" +
" (\n" +
" max(c_c_p) as max_ccp\n" +
" , max(d_c_p) max_dcp\n" +
" , max(d_x_p) dxp\n" +
" , count(1) cnt\n" +
" for (i, p) in\n" +
" (\n" +
" (1,1) as one_one,\n" +
" (1,2) as one_two,\n" +
" (1,3) as one_three,\n" +
" (2,1) as two_one,\n" +
" (2,2) as two_two,\n" +
" (2,3) as two_three\n" +
" )\n" +
" )\n" +
" where d_t = 'p'\n"; //
System.out.println(sql);
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT *\n" +
"FROM s\n" +
"\tJOIN d USING (c)\n" +
"PIVOT (MAX(c_c_p) AS max_ccp, MAX(d_c_p) AS max_dcp, MAX(d_x_p) AS dxp, COUNT(1) AS cnt FOR (i, p) IN ((1, 1) AS one_one, (1, 2) AS one_two, (1, 3) AS one_three, (2, 1) AS two_one, (2, 2) AS two_two, (2, 3) AS two_three))\n" +
"WHERE d_t = 'p'", text);
}
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(2, visitor.getTables().size());
assertEquals(3, visitor.getColumns().size());
assertEquals(3, visitor.getConditions().size());
assertEquals(1, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
}
}
@@ -0,0 +1,88 @@
/*
* 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.select;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest96_pivot extends OracleTest {
public void test_0() throws Exception {
String sql = //
" select *\n" +
" from s join d using(c)\n" +
" pivot\n" +
" (\n" +
" max(c_c_p) as max_ccp\n" +
" , max(d_c_p) max_dcp\n" +
" , max(d_x_p) dxp\n" +
" , count(1) cnt\n" +
" for (i, p) in\n" +
" (\n" +
" (1,1) as one_one,\n" +
" (1,2) as one_two,\n" +
" (1,3) as one_three,\n" +
" (2,1) as two_one,\n" +
" (2,2) as two_two,\n" +
" (2,3) as two_three\n" +
" )\n" +
" )\n" +
" where d_t = 'p'\n"; //
System.out.println(sql);
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
System.out.println(stmt.toString());
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
stmt.accept(visitor);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT *\n" +
"FROM s\n" +
"\tJOIN d USING (c)\n" +
"PIVOT (MAX(c_c_p) AS max_ccp, MAX(d_c_p) AS max_dcp, MAX(d_x_p) AS dxp, COUNT(1) AS cnt FOR (i, p) IN ((1, 1) AS one_one, (1, 2) AS one_two, (1, 3) AS one_three, (2, 1) AS two_one, (2, 2) AS two_two, (2, 3) AS two_three))\n" +
"WHERE d_t = 'p'", text);
}
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(2, visitor.getTables().size());
assertEquals(3, visitor.getColumns().size());
assertEquals(3, visitor.getConditions().size());
assertEquals(1, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
}
}