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);
}