mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -839,7 +839,8 @@ public class SQLUtils {
|
||||
if (MySqlUtils.isKeyword(normalizeName)) {
|
||||
return name;
|
||||
}
|
||||
} else if (JdbcConstants.POSTGRESQL.equals(dbType)) {
|
||||
} else if (JdbcConstants.POSTGRESQL.equals(dbType)
|
||||
|| JdbcConstants.ENTERPRISEDB.equals(dbType)) {
|
||||
if (PGUtils.isKeyword(normalizeName)) {
|
||||
return name;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,18 @@ public class SQLArgument extends SQLObjectImpl {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLArgument clone() {
|
||||
SQLArgument x = new SQLArgument();
|
||||
|
||||
x.type = type;
|
||||
|
||||
if (expr != null) {
|
||||
x.setExpr(expr.clone());
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public SQLParameter.ParameterType getType() {
|
||||
return type;
|
||||
}
|
||||
@@ -45,10 +57,10 @@ public class SQLArgument extends SQLObjectImpl {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public void setExpr(SQLExpr expr) {
|
||||
if (expr != null) {
|
||||
expr.setParent(this);
|
||||
public void setExpr(SQLExpr x) {
|
||||
if (x != null) {
|
||||
x.setParent(this);
|
||||
}
|
||||
this.expr = expr;
|
||||
this.expr = x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SQLDropTypeStatement extends SQLStatementImpl implements SQLDropStatement {
|
||||
|
||||
private SQLExpr name;
|
||||
private boolean ifExists;
|
||||
|
||||
public SQLDropTypeStatement() {
|
||||
|
||||
}
|
||||
|
||||
public SQLDropTypeStatement(String dbType) {
|
||||
super (dbType);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, name);
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLExpr getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(SQLExpr x) {
|
||||
if (x != null) {
|
||||
x.setParent(this);
|
||||
}
|
||||
this.name = x;
|
||||
}
|
||||
|
||||
public boolean isIfExists() {
|
||||
return ifExists;
|
||||
}
|
||||
|
||||
public void setIfExists(boolean ifExists) {
|
||||
this.ifExists = ifExists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SQLObject> getChildren() {
|
||||
List<SQLObject> children = new ArrayList<SQLObject>();
|
||||
if (name != null) {
|
||||
children.add(name);
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -49,6 +49,24 @@ public class OracleCreatePackageStatement extends OracleStatementImpl implements
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public OracleCreatePackageStatement clone() {
|
||||
OracleCreatePackageStatement x = new OracleCreatePackageStatement();
|
||||
|
||||
x.orReplace = orReplace;
|
||||
if (name != null) {
|
||||
x.setName(name.clone());
|
||||
}
|
||||
x.body = body;
|
||||
|
||||
for (SQLStatement stmt : statements) {
|
||||
SQLStatement s2 = stmt.clone();
|
||||
s2.setParent(x);
|
||||
x.statements.add(s2);
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public boolean isOrReplace() {
|
||||
return orReplace;
|
||||
}
|
||||
|
||||
+10
@@ -48,6 +48,8 @@ public class OracleCreateTypeStatement extends OracleStatementImpl implements SQ
|
||||
private SQLExpr varraySizeLimit;
|
||||
private SQLDataType varrayDataType;
|
||||
|
||||
private String wrappedSource;
|
||||
|
||||
@Override
|
||||
public void accept0(OracleASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
@@ -199,4 +201,12 @@ public class OracleCreateTypeStatement extends OracleStatementImpl implements SQ
|
||||
public void setParen(boolean paren) {
|
||||
this.paren = paren;
|
||||
}
|
||||
|
||||
public String getWrappedSource() {
|
||||
return wrappedSource;
|
||||
}
|
||||
|
||||
public void setWrappedSource(String wrappedSource) {
|
||||
this.wrappedSource = wrappedSource;
|
||||
}
|
||||
}
|
||||
|
||||
+28
@@ -48,6 +48,34 @@ public class OracleExecuteImmediateStatement extends OracleStatementImpl {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public OracleExecuteImmediateStatement clone() {
|
||||
OracleExecuteImmediateStatement x = new OracleExecuteImmediateStatement();
|
||||
|
||||
if (dynamicSql != null) {
|
||||
x.setDynamicSql(dynamicSql.clone());
|
||||
}
|
||||
|
||||
for (SQLArgument arg : arguments) {
|
||||
SQLArgument a2 = arg.clone();
|
||||
a2.setParent(x);
|
||||
x.arguments.add(a2);
|
||||
}
|
||||
|
||||
for (SQLExpr e : into) {
|
||||
SQLExpr e2 = e.clone();
|
||||
e2.setParent(x);
|
||||
x.into.add(e2);
|
||||
}
|
||||
|
||||
for (SQLExpr e : returnInto) {
|
||||
SQLExpr e2 = e.clone();
|
||||
e2.setParent(x);
|
||||
x.returnInto.add(e2);
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public SQLExpr getDynamicSql() {
|
||||
return dynamicSql;
|
||||
}
|
||||
|
||||
@@ -597,6 +597,15 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lexer.identifierEquals(FnvHash.Constants.TYPE)) {
|
||||
lexer.reset(savePoint);
|
||||
|
||||
SQLStatement stmt = parseDropType();
|
||||
stmt.setParent(parent);
|
||||
statementList.add(stmt);
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new ParserException("TODO : " + lexer.info());
|
||||
}
|
||||
|
||||
@@ -689,6 +698,19 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
}
|
||||
}
|
||||
|
||||
public SQLStatement parseDropType() {
|
||||
if (lexer.token() == Token.DROP) {
|
||||
lexer.nextToken();
|
||||
}
|
||||
SQLDropTypeStatement stmt = new SQLDropTypeStatement();
|
||||
stmt.setDbType(dbType);
|
||||
|
||||
acceptIdentifier("TYPE");
|
||||
|
||||
stmt.setName(this.exprParser.name());
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public SQLStatement parseDropSynonym() {
|
||||
if (lexer.token() == Token.DROP) {
|
||||
lexer.nextToken();
|
||||
@@ -2819,6 +2841,14 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
accept(Token.OF);
|
||||
SQLDataType dataType = this.exprParser.parseDataType();
|
||||
stmt.setVarrayDataType(dataType);
|
||||
} else if (lexer.identifierEquals(FnvHash.Constants.WRAPPED)) {
|
||||
int pos = lexer.text.indexOf(';', lexer.pos());
|
||||
if (pos != -1) {
|
||||
String wrappedString = lexer.subString(lexer.pos(), pos - lexer.pos());
|
||||
stmt.setWrappedSource(wrappedString);
|
||||
lexer.reset(pos, ';', Token.LITERAL_CHARS);
|
||||
lexer.nextToken();
|
||||
}
|
||||
} else {
|
||||
if (lexer.token() == Token.LPAREN) {
|
||||
lexer.nextToken();
|
||||
|
||||
+6
-129
@@ -1293,58 +1293,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBlockStatement x) {
|
||||
if (x.getParameters().size() != 0) {
|
||||
this.indentCount++;
|
||||
if (x.getParent() instanceof SQLCreateProcedureStatement) {
|
||||
SQLCreateProcedureStatement procedureStatement = (SQLCreateProcedureStatement) x.getParent();
|
||||
if (procedureStatement.isCreate()) {
|
||||
printIndent();
|
||||
}
|
||||
}
|
||||
if (!(x.getParent() instanceof SQLCreateProcedureStatement
|
||||
|| x.getParent() instanceof SQLCreateFunctionStatement
|
||||
|| x.getParent() instanceof OracleFunctionDataType
|
||||
|| x.getParent() instanceof OracleProcedureDataType)
|
||||
) {
|
||||
print0(ucase ? "DECLARE" : "declare");
|
||||
println();
|
||||
}
|
||||
|
||||
for (int i = 0, size = x.getParameters().size(); i < size; ++i) {
|
||||
if (i != 0) {
|
||||
println();
|
||||
}
|
||||
SQLParameter param = x.getParameters().get(i);
|
||||
param.accept(this);
|
||||
print(';');
|
||||
}
|
||||
|
||||
this.indentCount--;
|
||||
println();
|
||||
}
|
||||
print0(ucase ? "BEGIN" : "begin");
|
||||
this.indentCount++;
|
||||
|
||||
for (int i = 0, size = x.getStatementList().size(); i < size; ++i) {
|
||||
println();
|
||||
SQLStatement stmt = x.getStatementList().get(i);
|
||||
stmt.accept(this);
|
||||
}
|
||||
this.indentCount--;
|
||||
|
||||
SQLStatement exception = x.getException();
|
||||
if (exception != null) {
|
||||
println();
|
||||
exception.accept(this);
|
||||
}
|
||||
|
||||
println();
|
||||
print0(ucase ? "END;" : "end;");
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLBlockStatement x) {
|
||||
|
||||
@@ -2534,84 +2482,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateProcedureStatement x) {
|
||||
boolean create = x.isCreate();
|
||||
if (!create) {
|
||||
print0(ucase ? "PROCEDURE " : "procedure ");
|
||||
} else if (x.isOrReplace()) {
|
||||
print0(ucase ? "CREATE OR REPLACE PROCEDURE " : "create or replace procedure ");
|
||||
} else {
|
||||
print0(ucase ? "CREATE PROCEDURE " : "create procedure ");
|
||||
}
|
||||
x.getName().accept(this);
|
||||
|
||||
int paramSize = x.getParameters().size();
|
||||
|
||||
if (paramSize > 0) {
|
||||
print0(" (");
|
||||
this.indentCount++;
|
||||
println();
|
||||
|
||||
for (int i = 0; i < paramSize; ++i) {
|
||||
if (i != 0) {
|
||||
print0(", ");
|
||||
println();
|
||||
}
|
||||
SQLParameter param = x.getParameters().get(i);
|
||||
param.accept(this);
|
||||
}
|
||||
|
||||
this.indentCount--;
|
||||
println();
|
||||
print(')');
|
||||
}
|
||||
|
||||
SQLName authid = x.getAuthid();
|
||||
if (authid != null) {
|
||||
print(ucase ? " AUTHID " : " authid ");
|
||||
authid.accept(this);
|
||||
}
|
||||
|
||||
SQLStatement block = x.getBlock();
|
||||
|
||||
if (block != null && !create) {
|
||||
println();
|
||||
print("IS");
|
||||
println();
|
||||
} else {
|
||||
println();
|
||||
if (block instanceof SQLBlockStatement) {
|
||||
SQLBlockStatement blockStatement = (SQLBlockStatement) block;
|
||||
if (blockStatement.getParameters().size() > 0 || authid != null) {
|
||||
println(ucase ? "AS" : "as");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String javaCallSpec = x.getJavaCallSpec();
|
||||
if (javaCallSpec != null) {
|
||||
print0(ucase ? "LANGUAGE JAVA NAME '" : "language java name '");
|
||||
print0(javaCallSpec);
|
||||
print('\'');
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean afterSemi = false;
|
||||
if (block != null) {
|
||||
block.accept(this);
|
||||
|
||||
if (block instanceof SQLBlockStatement
|
||||
&& ((SQLBlockStatement) block).getStatementList().size() > 0) {
|
||||
afterSemi = ((SQLBlockStatement) block).getStatementList().get(0).isAfterSemi();
|
||||
}
|
||||
}
|
||||
|
||||
if ((!afterSemi) && x.getParent() instanceof OracleCreatePackageStatement) {
|
||||
print(';');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateFunctionStatement x) {
|
||||
@@ -3410,6 +3281,12 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
|
||||
}
|
||||
}
|
||||
|
||||
String wrappedSource = x.getWrappedSource();
|
||||
if (wrappedSource != null) {
|
||||
print0(ucase ? " WRAPPED" : " wrapped");
|
||||
print0(wrappedSource);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1896,15 +1896,16 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
|
||||
|
||||
this.indentCount++;
|
||||
|
||||
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
|
||||
List<SQLStatement> statements = x.getStatements();
|
||||
for (int i = 0, size = statements.size(); i < size; ++i) {
|
||||
println();
|
||||
SQLStatement item = x.getStatements().get(i);
|
||||
item.accept(this);
|
||||
SQLStatement stmt = statements.get(i);
|
||||
stmt.accept(this);
|
||||
}
|
||||
|
||||
this.indentCount--;
|
||||
|
||||
if (x.isBody()) {
|
||||
if (x.isBody() || statements.size() > 0) {
|
||||
println();
|
||||
print0(ucase ? "END " : "end ");
|
||||
x.getName().accept(this);
|
||||
|
||||
@@ -3096,6 +3096,17 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropTypeStatement x) {
|
||||
print0(ucase ? "DROP TYPE " : "drop type ");
|
||||
if (x.isIfExists()) {
|
||||
print0(ucase ? "IF EXISTS " : "if exists ");
|
||||
}
|
||||
x.getName().accept(this);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropSynonymStatement x) {
|
||||
if (x.isPublic()) {
|
||||
@@ -5797,4 +5808,139 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
x.getExpr().accept(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLBlockStatement x) {
|
||||
if (x.getParameters().size() != 0) {
|
||||
this.indentCount++;
|
||||
if (x.getParent() instanceof SQLCreateProcedureStatement) {
|
||||
SQLCreateProcedureStatement procedureStatement = (SQLCreateProcedureStatement) x.getParent();
|
||||
if (procedureStatement.isCreate()) {
|
||||
printIndent();
|
||||
}
|
||||
}
|
||||
if (!(x.getParent() instanceof SQLCreateProcedureStatement
|
||||
|| x.getParent() instanceof SQLCreateFunctionStatement
|
||||
|| x.getParent() instanceof OracleFunctionDataType
|
||||
|| x.getParent() instanceof OracleProcedureDataType)
|
||||
) {
|
||||
print0(ucase ? "DECLARE" : "declare");
|
||||
println();
|
||||
}
|
||||
|
||||
for (int i = 0, size = x.getParameters().size(); i < size; ++i) {
|
||||
if (i != 0) {
|
||||
println();
|
||||
}
|
||||
SQLParameter param = x.getParameters().get(i);
|
||||
param.accept(this);
|
||||
print(';');
|
||||
}
|
||||
|
||||
this.indentCount--;
|
||||
println();
|
||||
}
|
||||
print0(ucase ? "BEGIN" : "begin");
|
||||
this.indentCount++;
|
||||
|
||||
for (int i = 0, size = x.getStatementList().size(); i < size; ++i) {
|
||||
println();
|
||||
SQLStatement stmt = x.getStatementList().get(i);
|
||||
stmt.accept(this);
|
||||
}
|
||||
this.indentCount--;
|
||||
|
||||
SQLStatement exception = x.getException();
|
||||
if (exception != null) {
|
||||
println();
|
||||
exception.accept(this);
|
||||
}
|
||||
|
||||
println();
|
||||
print0(ucase ? "END;" : "end;");
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateProcedureStatement x) {
|
||||
boolean create = x.isCreate();
|
||||
if (!create) {
|
||||
print0(ucase ? "PROCEDURE " : "procedure ");
|
||||
} else if (x.isOrReplace()) {
|
||||
print0(ucase ? "CREATE OR REPLACE PROCEDURE " : "create or replace procedure ");
|
||||
} else {
|
||||
print0(ucase ? "CREATE PROCEDURE " : "create procedure ");
|
||||
}
|
||||
x.getName().accept(this);
|
||||
|
||||
int paramSize = x.getParameters().size();
|
||||
|
||||
if (paramSize > 0) {
|
||||
print0(" (");
|
||||
this.indentCount++;
|
||||
println();
|
||||
|
||||
for (int i = 0; i < paramSize; ++i) {
|
||||
if (i != 0) {
|
||||
print0(", ");
|
||||
println();
|
||||
}
|
||||
SQLParameter param = x.getParameters().get(i);
|
||||
param.accept(this);
|
||||
}
|
||||
|
||||
this.indentCount--;
|
||||
println();
|
||||
print(')');
|
||||
}
|
||||
|
||||
SQLName authid = x.getAuthid();
|
||||
if (authid != null) {
|
||||
print(ucase ? " AUTHID " : " authid ");
|
||||
authid.accept(this);
|
||||
}
|
||||
|
||||
SQLStatement block = x.getBlock();
|
||||
|
||||
if (block != null && !create) {
|
||||
println();
|
||||
print("IS");
|
||||
println();
|
||||
} else {
|
||||
println();
|
||||
if (block instanceof SQLBlockStatement) {
|
||||
SQLBlockStatement blockStatement = (SQLBlockStatement) block;
|
||||
if (blockStatement.getParameters().size() > 0 || authid != null) {
|
||||
println(ucase ? "AS" : "as");
|
||||
} else {
|
||||
println(ucase ? "IS" : "is");
|
||||
}
|
||||
} else {
|
||||
println(ucase ? "IS" : "is");
|
||||
}
|
||||
}
|
||||
|
||||
String javaCallSpec = x.getJavaCallSpec();
|
||||
if (javaCallSpec != null) {
|
||||
print0(ucase ? "LANGUAGE JAVA NAME '" : "language java name '");
|
||||
print0(javaCallSpec);
|
||||
print('\'');
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean afterSemi = false;
|
||||
if (block != null) {
|
||||
block.accept(this);
|
||||
|
||||
if (block instanceof SQLBlockStatement
|
||||
&& ((SQLBlockStatement) block).getStatementList().size() > 0) {
|
||||
afterSemi = ((SQLBlockStatement) block).getStatementList().get(0).isAfterSemi();
|
||||
}
|
||||
}
|
||||
|
||||
if ((!afterSemi) && x.getParent() instanceof OracleCreatePackageStatement) {
|
||||
print(';');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -796,4 +796,7 @@ public interface SQLASTVisitor {
|
||||
|
||||
boolean visit(SQLRecordDataType x);
|
||||
void endVisit(SQLRecordDataType x);
|
||||
|
||||
boolean visit(SQLDropTypeStatement x);
|
||||
void endVisit(SQLDropTypeStatement x);
|
||||
}
|
||||
|
||||
@@ -1846,6 +1846,16 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropTypeStatement x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLDropTypeStatement x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLRecordDataType x) {
|
||||
return true;
|
||||
|
||||
@@ -2411,4 +2411,9 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLDropTypeStatement x) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -530,6 +530,7 @@ public final class FnvHash {
|
||||
long INSTANTIABLE = fnv1a_64_lower("INSTANTIABLE");
|
||||
long UNSUPPORTED = fnv1a_64_lower("UNSUPPORTED");
|
||||
long VARRAY = fnv1a_64_lower("VARRAY");
|
||||
long WRAPPED = fnv1a_64_lower("WRAPPED");
|
||||
long AUTHID = fnv1a_64_lower("AUTHID");
|
||||
long UNDER = fnv1a_64_lower("UNDER");
|
||||
long USERENV = fnv1a_64_lower("USERENV");
|
||||
|
||||
Reference in New Issue
Block a user