improved sql parser.

This commit is contained in:
wenshao
2017-09-22 23:21:42 +08:00
parent ee6e9879bb
commit aa957fab13
34 changed files with 1861 additions and 20 deletions
@@ -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.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_64_negative extends MysqlTest {
public void test_0() throws Exception {
String sql = "select -4.7,3.5";
System.out.println(sql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL, SQLParserFeature.OptimizedForParameterized);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("SELECT -4.7, 3.5", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select -4.7, 3.5", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
assertEquals("SELECT ?, ?", //
output);
}
}
}
@@ -0,0 +1,85 @@
/*
* 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_65_orderby extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT number,event_cnt,country_code,area_code,carrier,country,province,city,type,subtype,displayname,slogan,logo,source,state,priority,is_change FROM ktv_number_20170921 WHERE day_modify > 20170921 AND priority >= 4499000 ORDER BY priority DESC LIMIT 500000";
System.out.println(sql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL, SQLParserFeature.OptimizedForParameterized);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("SELECT number, event_cnt, country_code, area_code, carrier\n" +
"\t, country, province, city, type, subtype\n" +
"\t, displayname, slogan, logo, source, state\n" +
"\t, priority, is_change\n" +
"FROM ktv_number_20170921\n" +
"WHERE day_modify > 20170921\n" +
"\tAND priority >= 4499000\n" +
"ORDER BY priority DESC\n" +
"LIMIT 500000", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select number, event_cnt, country_code, area_code, carrier\n" +
"\t, country, province, city, type, subtype\n" +
"\t, displayname, slogan, logo, source, state\n" +
"\t, priority, is_change\n" +
"from ktv_number_20170921\n" +
"where day_modify > 20170921\n" +
"\tand priority >= 4499000\n" +
"order by priority desc\n" +
"limit 500000", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
assertEquals("SELECT number, event_cnt, country_code, area_code, carrier\n" +
"\t, country, province, city, type, subtype\n" +
"\t, displayname, slogan, logo, source, state\n" +
"\t, priority, is_change\n" +
"FROM ktv_number\n" +
"WHERE day_modify > ?\n" +
"\tAND priority >= ?\n" +
"ORDER BY priority DESC\n" +
"LIMIT ?", //
output);
}
}
}
@@ -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.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_66 extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT * FROM (SELECT number_ios,type,subtype,displayname,source FROM hot_number_20170921 WHERE day_modify > 20170921 AND priority >= 4499000 ORDER BY priority DESC LIMIT 500000) ORDER BY number_ios ASC";
System.out.println(sql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL, SQLParserFeature.OptimizedForParameterized);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("SELECT *\n" +
"FROM (\n" +
"\tSELECT number_ios, type, subtype, displayname, source\n" +
"\tFROM hot_number_20170921\n" +
"\tWHERE day_modify > 20170921\n" +
"\t\tAND priority >= 4499000\n" +
"\tORDER BY priority DESC\n" +
"\tLIMIT 500000\n" +
")\n" +
"ORDER BY number_ios ASC", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select *\n" +
"from (\n" +
"\tselect number_ios, type, subtype, displayname, source\n" +
"\tfrom hot_number_20170921\n" +
"\twhere day_modify > 20170921\n" +
"\t\tand priority >= 4499000\n" +
"\torder by priority desc\n" +
"\tlimit 500000\n" +
")\n" +
"order by number_ios asc", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
assertEquals("SELECT *\n" +
"FROM (\n" +
"\tSELECT number_ios, type, subtype, displayname, source\n" +
"\tFROM hot_number\n" +
"\tWHERE day_modify > ?\n" +
"\t\tAND priority >= ?\n" +
"\tORDER BY priority DESC\n" +
"\tLIMIT ?\n" +
")\n" +
"ORDER BY number_ios ASC", //
output);
}
}
}
@@ -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.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_67 extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT\n" +
"t_aid,t_title,t_path,t_datetime,t_total\n" +
"FROM t_article WHERE t_state =\n" +
"? ORDER BY t_total DESC limit ?,?";
System.out.println(sql);
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL, SQLParserFeature.OptimizedForParameterized);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
stmt.accept(visitor);
{
String output = SQLUtils.toMySqlString(stmt);
assertEquals("SELECT t_aid, t_title, t_path, t_datetime, t_total\n" +
"FROM t_article\n" +
"WHERE t_state = ?\n" +
"ORDER BY t_total DESC\n" +
"LIMIT ?, ?", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select t_aid, t_title, t_path, t_datetime, t_total\n" +
"from t_article\n" +
"where t_state = ?\n" +
"order by t_total desc\n" +
"limit ?, ?", //
output);
}
{
String output = SQLUtils.toMySqlString(stmt, new SQLUtils.FormatOption(true, true, true));
assertEquals("SELECT t_aid, t_title, t_path, t_datetime, t_total\n" +
"FROM t_article\n" +
"WHERE t_state = ?\n" +
"ORDER BY t_total DESC\n" +
"LIMIT ?, ?", //
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.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 java.util.List;
public class OracleCreateFunctionTest_2 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"CREATE FUNCTION StockPivot(p refcur_pkg.refcur_t) \n" +
" RETURN TickerTypeSet PIPELINED USING StockPivotImpl;\n";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
assertEquals(1, statementList.size());
assertEquals("CREATE FUNCTION StockPivot (\n" +
"\tp refcur_pkg.refcur_t\n" +
")\n" +
"RETURN TickerTypeSetPIPELINED \n" +
"USING StockPivotImpl;",//
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());
assertEquals(0, visitor.getTables().size());
assertEquals(0, visitor.getColumns().size());
// assertTrue(visitor.getColumns().contains(new TableStat.Column("orders", "order_total")));
}
}
@@ -0,0 +1,141 @@
/*
* 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 java.util.List;
public class OracleCreateFunctionTest_3 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"FUNCTION FN_LGN_CHK(USERINFO IN VARCHAR2)\n" +
" RETURN VARCHAR2 IS\n" +
" -- Author : yuguoq\n" +
" -- Created : 2013-10-14\n" +
" -- Purpose : ??????\n" +
" RESULT VARCHAR2(20);\n" +
" TYPE TYPE_ARRAY IS VARRAY(3) OF VARCHAR2(50);\n" +
" VAR_ARRAY TYPE_ARRAY := TYPE_ARRAY();\n" +
" CURSOR VALUECURSOR IS\n" +
" SELECT *\n" +
" FROM TABLE(CAST(FN_SPLIT(USERINFO, ',') AS TY_STR_SPLIT))\n" +
" WHERE ROWNUM < 4;\n" +
" CURPOLICYINFO VALUECURSOR%ROWTYPE; ---??????\n" +
" I INTEGER := 1;\n" +
"BEGIN\n" +
" OPEN VALUECURSOR; ---open cursor\n" +
" LOOP\n" +
" --deal with extraction data from DB\n" +
" FETCH VALUECURSOR\n" +
" INTO CURPOLICYINFO;\n" +
" EXIT WHEN VALUECURSOR%NOTFOUND;\n" +
" VAR_ARRAY.EXTEND;\n" +
" VAR_ARRAY(I) := CURPOLICYINFO.COLUMN_VALUE;\n" +
" I := I + 1;\n" +
" END LOOP;\n" +
" if VAR_ARRAY.count <> 3 then\n" +
" RESULT := '1';\n" +
" else\n" +
" IF VAR_ARRAY(3) = md5(VAR_ARRAY(1) || VAR_ARRAY(2)) THEN\n" +
" RESULT := VAR_ARRAY(1);\n" +
" ELSE\n" +
" RESULT := '1';\n" +
" END IF;\n" +
" end if;\n" +
" RETURN(RESULT);\n" +
"EXCEPTION\n" +
" WHEN OTHERS THEN\n" +
" CLOSE VALUECURSOR;\n" +
" DBMS_OUTPUT.PUT_LINE(SQLERRM);\n" +
" IF VALUECURSOR%ISOPEN THEN\n" +
" --close cursor\n" +
" CLOSE VALUECURSOR;\n" +
" END IF;\n" +
"END FN_LGN_CHK;";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
assertEquals(1, statementList.size());
assertEquals("FUNCTION FN_LGN_CHK (\n" +
"\tUSERINFO IN VARCHAR2\n" +
")\n" +
"RETURN VARCHAR2\n" +
"IS\n" +
"RESULT VARCHAR2(20);\n" +
"\tTYPE TYPE_ARRAY IS VARRAY(3) OF VARCHAR2(50);\n" +
"\tVAR_ARRAY TYPE_ARRAY := TYPE_ARRAY();\n" +
"\tCURSOR VALUECURSOR IS\n" +
"\t\tSELECT *\n" +
"\t\tFROM TABLE(CAST(FN_SPLIT(USERINFO, ',') AS TY_STR_SPLIT))\n" +
"\t\tWHERE ROWNUM < 4;\n" +
"\tCURPOLICYINFO VALUECURSOR%ROWTYPE;\n" +
"\tI INTEGER := 1;\n" +
"BEGIN\n" +
"\tOPEN VALUECURSOR;\n" +
"\tLOOP\n" +
"\t\tFETCH VALUECURSOR INTO CURPOLICYINFO;\n" +
"\t\tEXIT WHEN VALUECURSOR%NOTFOUND;\n" +
"\t\tVAR_ARRAY.EXTEND;\n" +
"\t\tVAR_ARRAY(I) := CURPOLICYINFO.COLUMN_VALUE;\n" +
"\t\tI := I + 1;\n" +
"\tEND LOOP;\n" +
"\tIF VAR_ARRAY.count <> 3 THEN\n" +
"\t\tRESULT := '1';\n" +
"\tELSE\n" +
"\t\tIF VAR_ARRAY(3) = md5(VAR_ARRAY(1) || VAR_ARRAY(2)) THEN\n" +
"\t\t\tRESULT := VAR_ARRAY(1);\n" +
"\t\tELSE\n" +
"\t\t\tRESULT := '1';\n" +
"\t\tEND IF;\n" +
"\tEND IF;\n" +
"\tRETURN RESULT;\n" +
"EXCEPTION\n" +
"\tWHEN OTHERS THEN\n" +
"\t\tCLOSE VALUECURSOR;\n" +
"\t\tDBMS_OUTPUT.PUT_LINE(SQLERRM);\n" +
"\t\tIF VALUECURSOR % ISOPEN THEN\n" +
"\t\t\tCLOSE VALUECURSOR;\n" +
"\t\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());
assertEquals(1, visitor.getTables().size());
assertEquals(2, visitor.getColumns().size());
// assertTrue(visitor.getColumns().contains(new TableStat.Column("orders", "order_total")));
}
}
@@ -0,0 +1,121 @@
/*
* 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 java.util.List;
public class OracleCreateFunctionTest_4 extends OracleTest {
public void test_types() throws Exception {
String sql = //
"FUNCTION SPLITSTR(STR IN CLOB,\n" +
" I IN NUMBER := 0,\n" +
" SEP IN VARCHAR2 := ',') RETURN VARCHAR2\n" +
" DETERMINISTIC\n" +
"/**************************************\n" +
" * NAME: SPLITSTR\n" +
" * AUTHOR: SEAN ZHANG.\n" +
" * DATE: 2012-09-03.\n" +
" * FUNCTION: ??????????????????????\n" +
" * PARAMETERS: STR: ????????\n" +
" I: ?????????I?0??STR????????I ??????????????\n" +
" SEP: ????????????????????????????????STR????SEP?????\n" +
" * EXAMPLE: SELECT SPLITSTR('ABC,DEF', 1) AS STR FROM DUAL; ?? ABC\n" +
" SELECT SPLITSTR('ABC,DEF', 3) AS STR FROM DUAL; ?? ?\n" +
" **************************************/\n" +
" IS\n" +
" T_COUNT NUMBER;\n" +
" T_STR VARCHAR2(4000);\n" +
"BEGIN\n" +
" IF I = 0 THEN\n" +
" T_STR := STR;\n" +
" ELSIF INSTR(STR, SEP) = 0 THEN\n" +
" T_STR := SEP;\n" +
" ELSE\n" +
" SELECT COUNT(*) INTO T_COUNT FROM TABLE(SPLIT(STR, SEP));\n" +
" IF I <= T_COUNT THEN\n" +
" SELECT STR\n" +
" INTO T_STR\n" +
" FROM (SELECT ROWNUM AS ITEM, COLUMN_VALUE AS STR\n" +
" FROM TABLE(SPLIT(STR, SEP)))\n" +
" WHERE ITEM = I;\n" +
" END IF;\n" +
" END IF;\n" +
" RETURN T_STR;\n" +
"END;";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
assertEquals(1, statementList.size());
assertEquals("FUNCTION SPLITSTR (\n" +
"\tSTR IN CLOB, \n" +
"\tI IN NUMBER := 0, \n" +
"\tSEP IN VARCHAR2 := ','\n" +
")\n" +
"RETURN VARCHAR2DETERMINISTIC \n" +
"IS\n" +
"T_COUNT NUMBER;\n" +
"\tT_STR VARCHAR2(4000);\n" +
"BEGIN\n" +
"\tIF I = 0 THEN\n" +
"\t\tT_STR := STR;\n" +
"\tELSE IF INSTR(STR, SEP) = 0 THEN\n" +
"\t\tT_STR := SEP;\n" +
"\tELSE\n" +
"\t\tSELECT COUNT(*)\n" +
"\t\tINTO T_COUNT\n" +
"\t\tFROM TABLE(SPLIT(STR, SEP));\n" +
"\t\tIF I <= T_COUNT THEN\n" +
"\t\t\tSELECT STR\n" +
"\t\t\tINTO T_STR\n" +
"\t\t\tFROM (\n" +
"\t\t\t\tSELECT ROWNUM AS ITEM, COLUMN_VALUE AS STR\n" +
"\t\t\t\tFROM TABLE(SPLIT(STR, SEP))\n" +
"\t\t\t)\n" +
"\t\t\tWHERE ITEM = I;\n" +
"\t\tEND IF;\n" +
"\tEND IF;\n" +
"\tRETURN T_STR;\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());
assertEquals(1, visitor.getTables().size());
assertEquals(3, visitor.getColumns().size());
// assertTrue(visitor.getColumns().contains(new TableStat.Column("orders", "order_total")));
}
}
@@ -0,0 +1,71 @@
/*
* 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.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 org.junit.Assert;
import java.util.List;
public class OracleCreateProcedureTest3 extends OracleTest {
public void test_0() throws Exception {
String sql = "PROCEDURE PRC_DEALSMS IS\n" +
" LV_HOUR NUMBER; --??\n" +
"BEGIN\n" +
" SELECT TO_CHAR(SYSDATE, 'HH24') INTO LV_HOUR FROM DUAL;\n" +
" IF LV_HOUR > 8 AND LV_HOUR < 19 THEN\n" +
" UPDATE CRMSMS_MT_SEND T\n" +
" SET T.SENDFLAG = 0, T.FLAGID = NULL\n" +
" WHERE T.SENDFLAG = 2\n" +
" AND TRUNC(T.SENDREALTIME) = TRUNC(SYSDATE);\n" +
" COMMIT;\n" +
" END IF;\n" +
"END PRC_DEALSMS;"; //
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
print(statementList);
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
for (SQLStatement statement : statementList) {
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());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("CRMSMS_MT_SEND")));
Assert.assertEquals(3, visitor.getColumns().size());
Assert.assertEquals(2, visitor.getConditions().size());
Assert.assertEquals(0, visitor.getRelationships().size());
Assert.assertTrue(visitor.containsColumn("CRMSMS_MT_SEND", "SENDFLAG"));
Assert.assertTrue(visitor.containsColumn("CRMSMS_MT_SEND", "FLAGID"));
}
}
@@ -0,0 +1,199 @@
/*
* 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.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 org.junit.Assert;
import java.util.List;
public class OracleCreateProcedureTest4 extends OracleTest {
public void test_0() throws Exception {
String sql = "PROCEDURE abc_apply(in_json IN VARCHAR2,\n" +
" out_ret OUT NUMBER,\n" +
" out_desc OUT VARCHAR2) AS\n" +
" /*\n" +
" TRANSNAME :???????????\n" +
" AUTHOR :AARON\n" +
" VERSION :V1.1\n" +
" DATE :2016-01-15\n" +
" */\n" +
" --?????\n" +
" lv_module VARCHAR2(20) := '?????';\n" +
" lv_opertype VARCHAR2(20) := '?????????';\n" +
" lv_tradecode VARCHAR2(20) := 'ABC_APPLY';\n" +
" dbnum NUMBER;\n" +
" crudate DATE;\n" +
" db_code NUMBER;\n" +
" db_file var2varray;\n" +
" db_date abc_finance_apply%ROWTYPE;\n" +
" db_date2 fact_brand_provider%ROWTYPE;\n" +
" in_loginname VARCHAR2(20);\n" +
" in_json_str json;\n" +
" in_store VARCHAR2(2000);\n" +
"BEGIN\n" +
" crudate := SYSDATE;\n" +
" db_code := seq_abc.NEXTVAL;\n" +
" --??????JSON?,???????????\n" +
" in_json_str := json(in_json);\n" +
" -- ?????\n" +
" db_date.gyscode := intime_json.get_string(in_json_str, 'gyscode');\n" +
" db_date.amount := intime_json.get_string(in_json_str, 'amount');\n" +
" db_date.bankcode := intime_json.get_string(in_json_str, 'bankcode');\n" +
" db_date.productcode := intime_json.get_string(in_json_str, 'productcode');\n" +
" db_date.memo := intime_json.get_string(in_json_str, 'memo');\n" +
" db_date.status := '0';\n" +
" db_date.createdate := crudate;\n" +
" db_date.seqno := db_code;\n" +
" --???????\n" +
" db_date2.gyscode := intime_json.get_string(in_json_str, 'gyscode');\n" +
" db_date2.gysname := intime_json.get_string(in_json_str, 'gysname');\n" +
" db_date2.yyzz := intime_json.get_string(in_json_str, 'yyzz'); --????\n" +
" db_date2.gysqydm := intime_json.get_string(in_json_str, 'zzjg'); --????\n" +
" db_date2.frdb := intime_json.get_string(in_json_str, 'frdb'); --????\n" +
" db_date2.frdbtel := intime_json.get_string(in_json_str, 'frdblxfs'); --??????\n" +
" db_date2.people := intime_json.get_string(in_json_str, 'people'); --???\n" +
" db_date2.apply_tel := intime_json.get_string(in_json_str, 'lxrphone'); --?????\n" +
" in_store := intime_json.get_string(in_json_str, 'store'); --????\n" +
" SELECT COUNT(1)\n" +
" INTO dbnum\n" +
" FROM fact_brand_provider\n" +
" WHERE gyscode = db_date.gyscode;\n" +
" IF dbnum > 0 THEN\n" +
" --??\n" +
" UPDATE fact_brand_provider\n" +
" SET gyscode = db_date2.gyscode,\n" +
" gysname = db_date2.gysname,\n" +
" yyzz = db_date2.yyzz,\n" +
" gysqydm = db_date2.gysqydm,\n" +
" frdb = db_date2.frdb,\n" +
" frdbtel = db_date2.frdbtel,\n" +
" people = db_date2.people,\n" +
" apply_tel = db_date2.apply_tel\n" +
" WHERE gyscode = db_date.gyscode;\n" +
" ELSE\n" +
" out_ret := -1;\n" +
" out_desc := '?????';\n" +
" END IF;\n" +
" --????\n" +
" SELECT COUNT(1)\n" +
" INTO dbnum\n" +
" FROM abc_finance_apply\n" +
" WHERE gyscode = db_date.gyscode;\n" +
" IF dbnum > 0 THEN\n" +
" db_date.flag := '1';\n" +
" db_date.mflag := '1';\n" +
" ELSE\n" +
" db_date.flag := '0';\n" +
" db_date.mflag := '0';\n" +
" END IF;\n" +
" INSERT INTO abc_finance_apply VALUES db_date;\n" +
" --??????\n" +
" IF in_store IS NOT NULL THEN\n" +
" db_file := intime_common.strsplit(in_store, ',');\n" +
" FOR i IN 1 .. db_file.COUNT\n" +
" LOOP\n" +
" INSERT INTO abc_fv_info\n" +
" (seqno,\n" +
" gyscode,\n" +
" gys,\n" +
" gysname,\n" +
" storeno,\n" +
" NAME,\n" +
" rq)\n" +
" SELECT seq_abc_fav.NEXTVAL,\n" +
" a.cable,\n" +
" a.code,\n" +
" a.NAME,\n" +
" a.storeno,\n" +
" b.NAME,\n" +
" SYSDATE\n" +
" FROM gys_store_total a, area b\n" +
" WHERE a.storeno = b.id\n" +
" AND a.cable = db_date.gyscode\n" +
" AND a.storeno = db_file(i);\n" +
" END LOOP;\n" +
" ELSE\n" +
" out_ret := -1;\n" +
" out_desc := '?????';\n" +
" END IF;\n" +
" --??????\n" +
" out_ret := 1;\n" +
" out_desc := '????';\n" +
" intime_common.sys_log(in_loginname,\n" +
" '0',\n" +
" lv_module,\n" +
" lv_opertype,\n" +
" crudate,\n" +
" out_ret,\n" +
" out_desc,\n" +
" lv_tradecode,\n" +
" '???:' || in_loginname);\n" +
" COMMIT;\n" +
"EXCEPTION\n" +
" WHEN OTHERS THEN\n" +
" --????\n" +
" --??????\n" +
" out_ret := -1;\n" +
" out_desc := '????' || SQLERRM;\n" +
" dbms_output.put_line('sqlerrm : ' || SQLERRM);\n" +
" intime_common.sys_log(in_loginname,\n" +
" '0',\n" +
" lv_module,\n" +
" lv_opertype,\n" +
" crudate,\n" +
" out_ret,\n" +
" out_desc,\n" +
" lv_tradecode,\n" +
" '???:' || in_loginname);\n" +
" ROLLBACK;\n" +
" RETURN;\n" +
"END;"; //
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
print(statementList);
Assert.assertEquals(1, statementList.size());
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
for (SQLStatement statement : statementList) {
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());
Assert.assertEquals(5, visitor.getTables().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("fact_brand_provider")));
Assert.assertEquals(32, visitor.getColumns().size());
Assert.assertEquals(7, visitor.getConditions().size());
Assert.assertEquals(5, visitor.getRelationships().size());
Assert.assertTrue(visitor.containsColumn("fact_brand_provider", "gyscode"));
Assert.assertTrue(visitor.containsColumn("fact_brand_provider", "gysname"));
}
}
@@ -0,0 +1,86 @@
/*
* 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 OracleCreateTriggerTest4 extends OracleTest {
public void test_0() throws Exception {
String sql = //
"TRIGGER AUTH.TRG_HR_STRUCTURE_AFTINST AFTER INSERT ON hr_structure FOR EACH ROW\n" +
"DECLARE\n" +
"BEGIN\n" +
" INSERT INTO hr_structure_temp\n" +
" (com_code,CODE,NAME,status,sjcode,\n" +
" isdept,type,selfcode,POS,createdate,\n" +
" issync,syncdate,deptsale)\n" +
" VALUES\n" +
" (:NEW.com_code,:NEW.CODE,:NEW.NAME,:NEW.status,:NEW.sjcode,\n" +
" :NEW.isdept,:NEW.type,:NEW.selfcode,:NEW.POS,SYSDATE,\n" +
" 'N',NULL,:NEW.deptsale);\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 TRIGGER AUTH.TRG_HR_STRUCTURE_AFTINST\n" +
"\tAFTER INSERT\n" +
"\tON hr_structure\n" +
"\tFOR EACH ROW\n" +
"BEGIN\n" +
"\tINSERT INTO hr_structure_temp\n" +
"\t\t(com_code, CODE, NAME, status, sjcode\n" +
"\t\t, isdept, type, selfcode, POS, createdate\n" +
"\t\t, issync, syncdate, deptsale)\n" +
"\tVALUES (:NEW.com_code, :NEW.CODE, :NEW.NAME, :NEW.status, :NEW.sjcode\n" +
"\t\t, :NEW.isdept, :NEW.type, :NEW.selfcode, :NEW.POS, SYSDATE\n" +
"\t\t, 'N', NULL, :NEW.deptsale);\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(1, 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,100 @@
/*
* 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 java.util.List;
public class OracleCreateTypeTest2 extends OracleTest {
public void test_0() throws Exception {
String sql = //
"CREATE TYPE customer_typ_demo AS OBJECT\n" +
" ( customer_id NUMBER(6)\n" +
" , cust_first_name VARCHAR2(20)\n" +
" , cust_last_name VARCHAR2(20)\n" +
" , cust_address CUST_ADDRESS_TYP\n" +
" , phone_numbers PHONE_LIST_TYP\n" +
" , nls_language VARCHAR2(3)\n" +
" , nls_territory VARCHAR2(30)\n" +
" , credit_limit NUMBER(9,2)\n" +
" , cust_email VARCHAR2(30)\n" +
" , cust_orders ORDER_LIST_TYP\n" +
" ) ;"; //
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
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());
assertEquals(0, visitor.getTables().size());
assertEquals(0, visitor.getColumns().size());
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("CREATE TYPE customer_typ_demo AS OBJECT (\n" +
"\tcustomer_id NUMBER(6), \n" +
"\tcust_first_name VARCHAR2(20), \n" +
"\tcust_last_name VARCHAR2(20), \n" +
"\tcust_address CUST_ADDRESS_TYP, \n" +
"\tphone_numbers PHONE_LIST_TYP, \n" +
"\tnls_language VARCHAR2(3), \n" +
"\tnls_territory VARCHAR2(30), \n" +
"\tcredit_limit NUMBER(9, 2), \n" +
"\tcust_email VARCHAR2(30), \n" +
"\tcust_orders ORDER_LIST_TYP\n" +
");", text);
}
{
String text = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("create type customer_typ_demo AS OBJECT (\n" +
"\tcustomer_id NUMBER(6), \n" +
"\tcust_first_name VARCHAR2(20), \n" +
"\tcust_last_name VARCHAR2(20), \n" +
"\tcust_address CUST_ADDRESS_TYP, \n" +
"\tphone_numbers PHONE_LIST_TYP, \n" +
"\tnls_language VARCHAR2(3), \n" +
"\tnls_territory VARCHAR2(30), \n" +
"\tcredit_limit NUMBER(9, 2), \n" +
"\tcust_email VARCHAR2(30), \n" +
"\tcust_orders ORDER_LIST_TYP\n" +
");", text);
}
// assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
// assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}
}
@@ -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.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 java.util.List;
public class OracleCreateTypeTest3 extends OracleTest {
public void test_0() throws Exception {
String sql = //
"CREATE TYPE data_typ1 AS OBJECT \n" +
" ( year NUMBER, \n" +
" MEMBER FUNCTION prod(invent NUMBER) RETURN NUMBER \n" +
" ); "; //
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
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());
assertEquals(0, visitor.getTables().size());
assertEquals(0, visitor.getColumns().size());
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("CREATE TYPE data_typ1 AS OBJECT (\n" +
"\tyear NUMBER, \n" +
"\tFUNCTION prod (invent NUMBER) RETURN NUMBER\n" +
");", text);
}
{
String text = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("create type data_typ1 AS OBJECT (\n" +
"\tyear NUMBER, \n" +
"\tfunction prod (invent NUMBER) return NUMBER\n" +
");", text);
}
// assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
// assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}
}
@@ -0,0 +1,58 @@
/*
* 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 java.util.List;
public class OraclePipeRowStatementTest extends OracleTest {
public void test_types() throws Exception {
String sql = //
"PIPE ROW(substr(p_string, v_start));";
OracleStatementParser parser = new OracleStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
print(statementList);
assertEquals(1, statementList.size());
assertEquals("PIPE ROW(substr(p_string, v_start));",//
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());
assertEquals(0, visitor.getTables().size());
assertEquals(0, visitor.getColumns().size());
// assertTrue(visitor.getColumns().contains(new TableStat.Column("orders", "order_total")));
}
}
@@ -0,0 +1,139 @@
/*
* 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.pl;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import java.util.List;
public class Oracle_pl_3 extends OracleTest {
public void test_0() throws Exception {
String sql = "FUNCTION F_PINYIN(P_NAME IN VARCHAR2) RETURN VARCHAR2 AS\n" +
" V_COMPARE VARCHAR2(100);\n" +
" V_RETURN VARCHAR2(4000);\n" +
" FUNCTION F_NLSSORT(P_WORD IN VARCHAR2) RETURN VARCHAR2 AS\n" +
" BEGIN\n" +
" RETURN NLSSORT(P_WORD, 'NLS_SORT=SCHINESE_PINYIN_M');\n" +
" END;\n" +
" BEGIN\n" +
" FOR I IN 1..NVL(LENGTH(P_NAME), 0) LOOP\n" +
" V_COMPARE := F_NLSSORT(SUBSTR(P_NAME, I, 1));\n" +
" IF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'A';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'B';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'C';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'D';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'E';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'F';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'G';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'H';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'J';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'K';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'L';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'M';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'N';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'O';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'P';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'Q';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'R';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'S';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'T';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'W';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'X';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'Y';\n" +
" ELSIF V_COMPARE >= F_NLSSORT('?') AND V_COMPARE <= F_NLSSORT('?') THEN\n" +
" V_RETURN := V_RETURN || 'Z';\n" +
" END IF;\n" +
" END LOOP;\n" +
" RETURN V_RETURN;\n"; //
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
SQLStatement stmt = statementList.get(0);
assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ORACLE);
for (SQLStatement statement : statementList) {
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")));
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("emp_name")));
// Assert.assertEquals(7, visitor.getColumns().size());
// Assert.assertEquals(3, visitor.getConditions().size());
// Assert.assertEquals(1, visitor.getRelationships().size());
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
{
String output = SQLUtils.toOracleString(stmt);
assertEquals("CREATE OR REPLACE PROCEDURE reassign (\n" +
"\tp IN OUT NOCOPY hr.person_typ, \n" +
"\tnew_job VARCHAR2\n" +
")\n" +
"BEGIN\n" +
"\tp.change_job(new_job);\n" +
"END;", //
output);
}
{
String output = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("create or replace procedure reassign (\n" +
"\tp in out nocopy hr.person_typ, \n" +
"\tnew_job VARCHAR2\n" +
")\n" +
"begin\n" +
"\tp.change_job(new_job);\n" +
"end;", //
output);
}
}
}
@@ -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.pl;
import com.alibaba.druid.sql.OracleTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcConstants;
import java.util.List;
public class oracle_pl_open_1 extends OracleTest {
public void test_0() throws Exception {
String sql = "OPEN cur_user(lv_batchid);"; //
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
SQLStatement stmt = statementList.get(0);
assertEquals(1, statementList.size());
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ORACLE);
for (SQLStatement statement : statementList) {
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")));
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("emp_name")));
// Assert.assertEquals(7, visitor.getColumns().size());
// Assert.assertEquals(3, visitor.getConditions().size());
// Assert.assertEquals(1, visitor.getRelationships().size());
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees", "salary")));
{
String output = SQLUtils.toOracleString(stmt);
assertEquals("OPEN cur_user(lv_batchid);", //
output);
}
{
String output = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("open cur_user(lv_batchid);", //
output);
}
}
}
@@ -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.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import org.junit.Assert;
import java.util.List;
public class OracleSelectTest83_number_negative extends OracleTest {
public void test_0() throws Exception {
String sql = //
"select -3.5,4.7 from dual"; //
System.out.println(sql);
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);
{
String text = SQLUtils.toOracleString(stmt);
assertEquals("SELECT -3.5, 4.7\n" +
"FROM dual", 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(0, visitor.getColumns().size());
assertEquals(0, visitor.getConditions().size());
assertEquals(0, visitor.getRelationships().size());
assertEquals(0, visitor.getOrderByColumns().size());
// Assert.assertTrue(visitor.containsTable("sup_registration"));
// Assert.assertTrue(visitor.containsTable("sup_task"));
// Assert.assertTrue(visitor.containsTable("sys_org"));
//
// Assert.assertTrue(visitor.containsColumn("sup_task", "orgid"));
// Assert.assertTrue(visitor.containsColumn("sup_task", "orgid"));
//
}
}
@@ -0,0 +1,58 @@
/*
* 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.sqlite;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.util.JdbcConstants;
import org.junit.Assert;
import java.util.List;
public class SQLite_SelectTest_1 extends MysqlTest {
public void test_0() throws Exception {
String sql = "SELECT xiaojie,event_cnt,country_code,area_code,carrier,country,province,city,type,subtype,displayname,slogan,logo,source,state,priority,is_change FROM ktv_xiaojie_20170921 WHERE day_modify > 20170921 AND priority >= 4499000 ORDER BY priority DESC LIMIT 500000";
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.SQLITE);
SQLStatement stmt = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
SchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
// System.out.println("coditions : " + visitor.getConditions());
// System.out.println("orderBy : " + visitor.getOrderByColumns());
Assert.assertEquals(1, visitor.getTables().size());
Assert.assertEquals(18, visitor.getColumns().size());
Assert.assertEquals(2, visitor.getConditions().size());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ktv_xiaojie_20170921")));
Assert.assertTrue(visitor.getColumns().contains(new Column("ktv_xiaojie_20170921", "xiaojie")));
Assert.assertTrue(visitor.getColumns().contains(new Column("ktv_xiaojie_20170921", "event_cnt")));
}
}
@@ -0,0 +1,45 @@
/*
* 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.sqlserver;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import org.junit.Assert;
import java.util.List;
public class SQLServerSelectTest28 extends TestCase {
public void test_simple() throws Exception {
String sql = //
"select next value for orderId"; //
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.SQL_SERVER);
{
String text = SQLUtils.toSQLString(stmtList, JdbcConstants.SQL_SERVER);
assertEquals("SELECT NEXT VALUE FOR orderId", text);
}
{
String text = SQLUtils.toSQLString(stmtList, JdbcConstants.SQL_SERVER, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("select next value for orderId", text);
}
}
}