improved sql parser.

This commit is contained in:
wenshao
2017-10-11 20:53:12 +08:00
parent c6cbb328ea
commit 7f630d879f
37 changed files with 701 additions and 10 deletions
@@ -50,6 +50,7 @@ public class SQLCreateFunctionStatement extends SQLStatementImpl implements SQLC
private boolean aggregate;
private SQLName using;
private boolean pipelined;
private boolean resultCache;
public SQLCreateFunctionStatement clone() {
SQLCreateFunctionStatement x = new SQLCreateFunctionStatement();
@@ -248,4 +249,12 @@ public class SQLCreateFunctionStatement extends SQLStatementImpl implements SQLC
public void setPipelined(boolean pipelined) {
this.pipelined = pipelined;
}
public boolean isResultCache() {
return resultCache;
}
public void setResultCache(boolean resultCache) {
this.resultCache = resultCache;
}
}
@@ -0,0 +1,12 @@
package com.alibaba.druid.sql.ast.statement;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLShowErrorsStatement extends SQLStatementImpl {
@Override
protected void accept0(SQLASTVisitor visitor) {
visitor.visit(this);
visitor.endVisit(this);
}
}
@@ -1035,4 +1035,14 @@ public class OracleToMySqlOutputVisitor extends MySqlOutputVisitor implements Or
public void endVisit(OracleIsOfTypeExpr x) {
}
@Override
public boolean visit(OracleRunStatement x) {
return false;
}
@Override
public void endVisit(OracleRunStatement x) {
}
}
@@ -0,0 +1,65 @@
/*
* Copyright 1999-2017 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.oracle.ast.stmt;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatementImpl;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.ast.statement.SQLExplainStatement;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.JdbcConstants;
public class OracleRunStatement extends SQLStatementImpl implements OracleStatement {
private SQLExpr expr;
public OracleRunStatement() {
super (JdbcConstants.ORACLE);
}
public OracleRunStatement(SQLExpr expr) {
super (JdbcConstants.ORACLE);
this.setExpr(expr);
}
@Override
public void accept0(OracleASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, expr);
}
visitor.endVisit(this);
}
protected void accept0(SQLASTVisitor visitor) {
accept0((OracleASTVisitor) visitor);
}
public String toString() {
return SQLUtils.toOracleString(this);
}
public SQLExpr getExpr() {
return expr;
}
public void setExpr(SQLExpr expr) {
if (expr != null) {
expr.setParent(this);
}
this.expr = expr;
}
}
@@ -224,7 +224,13 @@ public class OracleLexer extends Lexer {
protected void scanVariable_at() {
scanChar();
token = Token.MONKEYS_AT;
if (ch == '@') {
scanChar();
token = Token.MONKEYS_AT_AT;
} else {
token = Token.MONKEYS_AT;
}
return;
}
@@ -309,6 +309,23 @@ public class OracleStatementParser extends SQLStatementParser {
continue;
}
if (strVal.equalsIgnoreCase("SHOW")) {
// Lexer.SavePoint savePoint = lexer.mark();
lexer.nextToken();
if (lexer.identifierEquals("ERR")) {
lexer.nextToken();
} else {
accept(Token.ERRORS);
}
SQLShowErrorsStatement stmt = new SQLShowErrorsStatement();
stmt.setDbType(dbType);
stmt.setParent(parent);
statementList.add(stmt);
continue;
}
SQLExpr expr = exprParser.expr();
if (expr instanceof SQLBinaryOpExpr) {
@@ -646,6 +663,17 @@ public class OracleStatementParser extends SQLStatementParser {
continue;
}
if (lexer.token() == Token.MONKEYS_AT_AT) {
lexer.nextToken();
SQLExpr expr = exprParser.primary();
OracleRunStatement stmt = new OracleRunStatement(expr);
stmt.setParent(parent);
statementList.add(stmt);
continue;
}
throw new ParserException("TODO : " + lexer.info());
}
}
@@ -845,6 +873,11 @@ public class OracleStatementParser extends SQLStatementParser {
stmt.setAuthid(authid);
}
if (identifierEquals("RESULT_CACHE")) {
lexer.nextToken();
stmt.setResultCache(true);
}
if (lexer.token() == Token.SEMI) {
lexer.nextToken();
return stmt;
@@ -1739,7 +1772,7 @@ public class OracleStatementParser extends SQLStatementParser {
SQLSelect select = this.createSQLSelectParser().select();
parameter.setDefaultValue(new SQLQueryExpr(select));
} else if (lexer.token() == Token.PROCEDURE) {
} else if (lexer.token() == Token.PROCEDURE || lexer.token() == Token.END) {
break;
} else if (lexer.identifierEquals("TYPE")) {
lexer.nextToken();
@@ -1809,7 +1842,16 @@ public class OracleStatementParser extends SQLStatementParser {
}
} else if (lexer.token() == Token.OUT) {
lexer.nextToken();
parameter.setParamType(SQLParameter.ParameterType.OUT);
if (lexer.token() == Token.IN) {
lexer.nextToken();
parameter.setParamType(SQLParameter.ParameterType.INOUT);
} else {
parameter.setParamType(SQLParameter.ParameterType.OUT);
}
} else if (lexer.token() == Token.INOUT) {
lexer.nextToken();
parameter.setParamType(SQLParameter.ParameterType.INOUT);
}
if (lexer.identifierEquals("NOCOPY")) {
@@ -1830,11 +1872,13 @@ public class OracleStatementParser extends SQLStatementParser {
lexer.nextToken();
functionDataType.setName(lexer.stringVal());
accept(Token.IDENTIFIER);
accept(Token.LPAREN);
this.parserParameters(functionDataType.getParameters(), functionDataType);
accept(Token.RPAREN);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
this.parserParameters(functionDataType.getParameters(), functionDataType);
accept(Token.RPAREN);
}
accept(Token.RETURN);
functionDataType.setReturnDataType(this.exprParser.parseDataType());
functionDataType.setReturnDataType(this.exprParser.parseDataType(false));
dataType = functionDataType;
name = null;
@@ -2612,9 +2656,16 @@ public class OracleStatementParser extends SQLStatementParser {
if (lexer.identifierEquals(FnvHash.Constants.STATIC)) {
this.parserParameters(stmt.getParameters(), stmt);
} else {
accept(Token.LPAREN);
this.parserParameters(stmt.getParameters(), stmt);
accept(Token.RPAREN);
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
this.parserParameters(stmt.getParameters(), stmt);
accept(Token.RPAREN);
} else {
this.parserParameters(stmt.getParameters(), stmt);
if (lexer.token() == Token.END) {
lexer.nextToken();
}
}
}
if (lexer.token() == Token.SEMI) {
@@ -441,4 +441,8 @@ public interface OracleASTVisitor extends SQLASTVisitor {
boolean visit(OracleIsOfTypeExpr x);
void endVisit(OracleIsOfTypeExpr x);
boolean visit(OracleRunStatement x);
void endVisit(OracleRunStatement x);
}
@@ -1093,4 +1093,14 @@ public class OracleASTVisitorAdapter extends SQLASTVisitorAdapter implements Ora
public void endVisit(OracleIsOfTypeExpr x) {
}
@Override
public boolean visit(OracleRunStatement x) {
return true;
}
@Override
public void endVisit(OracleRunStatement x) {
}
}
@@ -3374,4 +3374,16 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
public void endVisit(OracleIsOfTypeExpr x) {
}
@Override
public boolean visit(OracleRunStatement x) {
print0("@@");
printExpr(x.getExpr());
return false;
}
@Override
public void endVisit(OracleRunStatement x) {
}
}
@@ -1385,4 +1385,14 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
public void endVisit(OracleIsOfTypeExpr x) {
}
@Override
public boolean visit(OracleRunStatement x) {
return true;
}
@Override
public void endVisit(OracleRunStatement x) {
}
}
@@ -2795,4 +2795,16 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
public void endVisit(OracleIsOfTypeExpr x) {
}
@Override
public boolean visit(OracleRunStatement x) {
print0("@@");
printExpr(x.getExpr());
return false;
}
@Override
public void endVisit(OracleRunStatement x) {
}
}
@@ -2516,6 +2516,10 @@ public class SQLExprParser extends SQLParser {
lexer.nextToken();
item.setValue(new SQLBooleanExpr(true));
return item;
} else if (lexer.token == Token.ON) {
lexer.nextToken();
item.setValue(new SQLIdentifierExpr("ON"));
return item;
} else {
accept(Token.EQ);
}
@@ -5477,4 +5477,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
this.indentCount--;
return false;
}
@Override
public boolean visit(SQLShowErrorsStatement x) {
print0(ucase ? "SHOW ERRORS" : "show errors");
return true;
}
}
@@ -766,4 +766,7 @@ public interface SQLASTVisitor {
boolean visit(SQLLateralViewTableSource x);
void endVisit(SQLLateralViewTableSource x);
boolean visit(SQLShowErrorsStatement x);
void endVisit(SQLShowErrorsStatement x);
}
@@ -1759,6 +1759,16 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
}
@Override
public boolean visit(SQLShowErrorsStatement x) {
return true;
}
@Override
public void endVisit(SQLShowErrorsStatement x) {
}
public final boolean isEnabled(VisitorFeature feature) {
return VisitorFeature.isEnabled(this.features, feature);
}
@@ -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 AntlrScriptExamplesTest extends TestCase {
public void test_for_antlr_examples() throws Exception {
String path = "bvt/parser/antlr_grammers_v4_plsql/examples-sql-script/";
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,105 @@
/*
* 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 = "create or replace type type_body_elements\n" +
"as object\n" +
"(\n" +
" some_string varchar2(64),\n" +
" member function function_one\n" +
" return varchar2,\n" +
" member function function_two\n" +
" return varchar2\n" +
");\n" +
"/\n" +
"\n" +
"create or replace type body type_body_elements\n" +
"is\n" +
"\n" +
" member function function_one\n" +
" return varchar2\n" +
" is\n" +
" begin\n" +
" return 'the function_one result';\n" +
" end function_one;\n" +
"\n" +
" member function function_two\n" +
" return varchar2\n" +
" is\n" +
" begin\n" +
" return 'the function_two result';\n" +
" end function_two;\n" +
"\n" +
"end;\n" +
"/\n"; //
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
assertEquals(4, statementList.size());
SQLStatement stmt = statementList.get(0);
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 TYPE type_body_elements AS OBJECT (\n" +
"\tsome_string varchar2(64), \n" +
"\tFUNCTION function_one () RETURN varchar2, \n" +
"\tFUNCTION function_two () RETURN varchar2\n" +
");", //
output);
}
{
String output = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
assertEquals("create or replace type type_body_elements AS OBJECT (\n" +
"\tsome_string varchar2(64), \n" +
"\tfunction function_one () return varchar2, \n" +
"\tfunction function_two () return varchar2\n" +
");", //
output);
}
}
}
@@ -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.postgresql;
import com.alibaba.druid.sql.PGTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import java.util.List;
public class PGCreateSequenceTest0 extends PGTest {
public void test_0() throws Exception {
String sql = "\n" +
"create sequence SEQ_CONTRACT_OPER_ID \n" +
"INCREMENT BY 1\n" +
"START WITH 1\n" +
"NOMAXVALUE\n" +
"NOCYCLE\n" +
"CACHE 10; ";
PGSQLStatementParser parser = new PGSQLStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement stmt = statementList.get(0);
assertEquals("CREATE SEQUENCE SEQ_CONTRACT_OPER_ID START WITH 1 INCREMENT BY 1 NO MAXVALUE NO CYCLE CACHE 10;", SQLUtils.toPGString(stmt));
assertEquals("create sequence SEQ_CONTRACT_OPER_ID start with 1 increment by 1 no maxvalue no cycle cache 10;", SQLUtils.toPGString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
assertEquals(1, statementList.size());
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("test_site_data_select_111")));
//
// assertTrue(visitor.getTables().get(new TableStat.Name("test_site_data_select_111")).getCreateCount() == 1);
//
// assertEquals(4, visitor.getColumns().size() );
}
}
@@ -0,0 +1,11 @@
CREATE OR REPLACE PACKAGE BODY FOO
AS
PROCEDURE BAR(COLL COLL_TYPE)
IS
BEGIN
COLL.DELETE;
COLL.DELETE();
COLL.DELETE(42);
COLL.DELETE(42, 64);
END BAR;
END FOO;
@@ -0,0 +1,7 @@
create or replace package deterministic_function
is
function fixed_value return varchar2 deterministic;
end deterministic_function;
/
@@ -0,0 +1,29 @@
create or replace package e_in_out_parameters
is
procedure some_in_nocopy_procedure( p_string in nocopy varchar2 );
end e_in_out_parameters;
/
show errors
create or replace package e_in_out_parameters
is
procedure some_out_in_procedure( p_string out in varchar2 );
end e_in_out_parameters;
/
show errors
create or replace package e_in_out_parameters
is
procedure some_inout_procedure( p_string inout varchar2 );
end e_in_out_parameters;
/
show errors
@@ -0,0 +1,6 @@
@@green_table.sql
@@green_tools.pks
@@green_tools.pkb
@@green_tools_review.sql
@@ -0,0 +1,17 @@
drop table green_table;
create table green_table
(
green_col_one number(10)
, green_col_two varchar2(64)
);
insert into green_table (green_col_one, green_col_two)
values (100021, 'green-varchar-21');
insert into green_table (green_col_one, green_col_two)
values (100022, 'green-varchar-22');
insert into green_table (green_col_one, green_col_two)
values (100023, 'green-varchar-23');
select *
from green_table;
@@ -0,0 +1,32 @@
create or replace package body green_tools
is
procedure no_params_procedure
is
begin
dbms_output.put_line('no_params_procedure : begin');
end no_params_procedure;
function add_brackets( p_string in varchar2 ) return varchar2
as
begin
return '[' || p_string || ']';
end add_brackets;
procedure print_green_info( p_with_brackets in boolean )
is
begin
for r in ( select green_col_two
from green_table )
loop
if p_with_brackets then
dbms_output.put_line('print_green_info : with brackets : ' || add_brackets(r.green_col_two));
else
dbms_output.put_line('print_green_info : no brackets : ' || r.green_col_two);
end if;
end loop;
end print_green_info;
end green_tools;
/
@@ -0,0 +1,11 @@
create or replace package green_tools
is
procedure no_params_procedure;
function add_brackets( p_string in varchar2 ) return varchar2;
procedure print_green_info( p_with_brackets in boolean );
end green_tools;
/
@@ -0,0 +1,14 @@
set serveroutput on
select green_tools.add_brackets('to-be-bracketed') as with_brackets
from dual;
begin
green_tools.print_green_info( p_with_brackets => false );
end;
/
begin
green_tools.print_green_info( p_with_brackets => true );
end;
/
@@ -0,0 +1,15 @@
create or replace package in_out_parameters
is
procedure some_procedure( p_string varchar2 );
procedure some_in_procedure( p_string in varchar2 );
procedure some_out_procedure( p_string out varchar2 );
procedure some_in_out_procedure( p_string in out varchar2 );
procedure some_in_out_nocopy_procedure( p_string in out nocopy varchar2 );
end in_out_parameters;
/
@@ -0,0 +1,13 @@
create or replace package body login
as
function fun return varchar2
as
raw_key2 raw(128);
raw_userpass raw(128);
raw_encrypted raw(2048);
begin
dbms_obfuscation_toolkit.desencrypt(input => raw_userpass, key => raw_key2, encrypted_data => raw_encrypted);
end fun;
end;
@@ -0,0 +1,8 @@
CREATE OR REPLACE PACKAGE BODY HR.PACK
AS
PROCEDURE PROC1
AS
BEGIN
NULL;
END;
END;
@@ -0,0 +1,4 @@
CREATE OR REPLACE PACKAGE HR.PACK
AS
PROCEDURE PROC1;
END;
@@ -0,0 +1,24 @@
create or replace package body pipe_row_test as
function fun return t_tab pipelined
as
vRow t_tab_row;
begin
for rec in ( select Id from tbl )
loop
vrow.Id := rec.Id;
pipe row (vrow);
end loop;
end;
function prodfunc(n number) return mytabletype pipelined is
begin
for i in 1 .. 5 loop
pipe row (myobjectformat(n,sysdate+i,'row '||i));
end loop;
return;
end;
end;
@@ -0,0 +1,8 @@
create or replace package pragma_exception_init
is
some_exception exception;
pragma exception_init (some_exception, -20001);
end pragma_exception_init;
/
@@ -0,0 +1,14 @@
create or replace package body pack as
procedure test(inId number)
as
vSum number;
vPaymentsDate number;
begin
vSum := nvl(pack_1.getSum@dblink(inId=>inId, vPaymentsDate => pack_2.getLastDay(4, 3)), 0);
end;
end;
@@ -0,0 +1,7 @@
create or replace package result_cache_function
is
function some_function( p_string in varchar2 ) return varchar2 result_cache;
end result_cache_function;
/
@@ -0,0 +1,19 @@
create or replace package show_errors
is
procedure some_procedure;
end show_errors;
/
show errors
create or replace package show_errors
is
procedure some_procedure;
end show_errors;
/
show err
@@ -0,0 +1,10 @@
create or replace package body testPack as
function blob_to_clob (blob_in in blob) return clob
as
begin
utl_raw.cast_to_varchar2(dbms_lob.substr(a, b, c));
return null;
end blob_to_clob;
end testPack;
@@ -0,0 +1,30 @@
create or replace type type_body_elements
as object
(
some_string varchar2(64),
member function function_one
return varchar2,
member function function_two
return varchar2
);
/
create or replace type body type_body_elements
is
member function function_one
return varchar2
is
begin
return 'the function_one result';
end function_one;
member function function_two
return varchar2
is
begin
return 'the function_two result';
end function_two;
end;
/