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");
|
||||
|
||||
@@ -83,6 +83,9 @@ public class OracleCreatePackageTest5 extends OracleTest {
|
||||
System.out.println("relationships : " + visitor.getRelationships());
|
||||
System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
SQLUtils.toPGString(stmt);
|
||||
stmt.clone();
|
||||
|
||||
// Assert.assertEquals(2, visitor.getTables().size());
|
||||
//
|
||||
// Assert.assertEquals(5, visitor.getColumns().size());
|
||||
|
||||
@@ -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.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.stat.TableStat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleCreateProcedureTest5 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "CREATE OR REPLACE PROCEDURE proc_helloworld\n" +
|
||||
"IS\n" +
|
||||
"BEGIN\n" +
|
||||
" DBMS_OUTPUT.put_line ('Hello World!');\n" +
|
||||
"END;";
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
print(statementList);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
SQLStatement stmt = (SQLStatement) statementList.get(0);
|
||||
|
||||
OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
|
||||
stmt.accept(visitor);
|
||||
|
||||
assertEquals("CREATE OR REPLACE PROCEDURE proc_helloworld\n" +
|
||||
"IS\n" +
|
||||
"BEGIN\n" +
|
||||
"\tDBMS_OUTPUT.put_line('Hello World!');\n" +
|
||||
"END;", stmt.toString());
|
||||
|
||||
assertEquals("CREATE OR REPLACE PROCEDURE proc_helloworld\n" +
|
||||
"IS\n" +
|
||||
"BEGIN\n" +
|
||||
"\tDBMS_OUTPUT.put_line('Hello World!');\n" +
|
||||
"END;", SQLUtils.toPGString(stmt));
|
||||
|
||||
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());
|
||||
|
||||
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("fact_brand_provider")));
|
||||
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
assertEquals(0, visitor.getRelationships().size());
|
||||
|
||||
// assertTrue(visitor.containsColumn("fact_brand_provider", "gyscode"));
|
||||
// assertTrue(visitor.containsColumn("fact_brand_provider", "gysname"));
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ 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" +
|
||||
"TRIGGER RRP.TRG_HR_AK_AFTINST AFTER INSERT ON hr_structure FOR EACH ROW\n" +
|
||||
"DECLARE\n" +
|
||||
"BEGIN\n" +
|
||||
" INSERT INTO hr_structure_temp\n" +
|
||||
@@ -49,7 +49,7 @@ public class OracleCreateTriggerTest4 extends OracleTest {
|
||||
|
||||
Assert.assertEquals(1, statementList.size());
|
||||
|
||||
Assert.assertEquals("CREATE TRIGGER AUTH.TRG_HR_STRUCTURE_AFTINST\n" +
|
||||
Assert.assertEquals("CREATE TRIGGER RRP.TRG_HR_AK_AFTINST\n" +
|
||||
"\tAFTER INSERT\n" +
|
||||
"\tON hr_structure\n" +
|
||||
"\tFOR EACH ROW\n" +
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.oracle.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 OracleCreateTriggerTest5 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"CREATE OR REPLACE TRIGGER ECC.GAPM_PROJECT_INFO_SYNC_IOA_t\n" +
|
||||
"\tBEFORE INSERT\n" +
|
||||
"\tON ECC.GAPM_PROJECT_INFO_SYNC_IOA\n" +
|
||||
"\tFOR EACH ROW\n" +
|
||||
"BEGIN\n" +
|
||||
"\tSELECT ECC.GAPM_PROJECT_INFO_SYNC_IOA_S.Nextval\n" +
|
||||
"\tINTO :New.PROJECT_SYN_ID\n" +
|
||||
"\tFROM dual;\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 OR REPLACE TRIGGER ECC.GAPM_PROJECT_INFO_SYNC_IOA_t\n" +
|
||||
"\tBEFORE INSERT\n" +
|
||||
"\tON ECC.GAPM_PROJECT_INFO_SYNC_IOA\n" +
|
||||
"\tFOR EACH ROW\n" +
|
||||
"BEGIN\n" +
|
||||
"\tSELECT ECC.GAPM_PROJECT_INFO_SYNC_IOA_S.Nextval\n" +
|
||||
"\tINTO :New.PROJECT_SYN_ID\n" +
|
||||
"\tFROM dual;\n" +
|
||||
"END;",//
|
||||
SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
|
||||
|
||||
Assert.assertEquals("CREATE OR REPLACE TRIGGER ECC.GAPM_PROJECT_INFO_SYNC_IOA_t\n" +
|
||||
"\tBEFORE INSERT\n" +
|
||||
"\tON ECC.GAPM_PROJECT_INFO_SYNC_IOA\n" +
|
||||
"\tFOR EACH ROW\n" +
|
||||
"BEGIN\n" +
|
||||
"\tSELECT ECC.GAPM_PROJECT_INFO_SYNC_IOA_S.Nextval\n" +
|
||||
"\tINTO :New.PROJECT_SYN_ID\n" +
|
||||
"\tFROM dual;\n" +
|
||||
"END;",//
|
||||
SQLUtils.toSQLString(stmt, JdbcConstants.POSTGRESQL));
|
||||
|
||||
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,107 @@
|
||||
/*
|
||||
* 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 OracleCreateTypeTest12 extends OracleTest {
|
||||
|
||||
public void test_types() throws Exception {
|
||||
String sql = "CREATE OR REPLACE\n" +
|
||||
"TYPE mybb wrapped\n" +
|
||||
"a000000\n" +
|
||||
"1\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"d\n" +
|
||||
"c4 ae\n" +
|
||||
"4YeykduDwWpKI7JeaWrDQ5SWiVgwg5n0dLhcWvou/6GX6rh0K6W/m8Ayy8xQjwlppZmB8L8z\n" +
|
||||
"vbLLUrKeK2fhZ1JJscqkTZ4s6sZ86o5CkZQ1+fmoCEHRnn2mVV986ixuyTCSREZA25hXQFfU\n" +
|
||||
"W757xtr4B5ICysO43qOCpqYu0laZ;";
|
||||
|
||||
System.out.println(sql);
|
||||
|
||||
|
||||
OracleStatementParser parser = new OracleStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
assertEquals("CREATE OR REPLACE TYPE mybb WRAPPED\n" +
|
||||
"a000000\n" +
|
||||
"1\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"abcd\n" +
|
||||
"d\n" +
|
||||
"c4 ae\n" +
|
||||
"4YeykduDwWpKI7JeaWrDQ5SWiVgwg5n0dLhcWvou/6GX6rh0K6W/m8Ayy8xQjwlppZmB8L8z\n" +
|
||||
"vbLLUrKeK2fhZ1JJscqkTZ4s6sZ86o5CkZQ1+fmoCEHRnn2mVV986ixuyTCSREZA25hXQFfU\n" +
|
||||
"W757xtr4B5ICysO43qOCpqYu0laZ;",//
|
||||
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());
|
||||
|
||||
SQLUtils.toPGString(stmt);
|
||||
|
||||
// assertTrue(visitor.getColumns().contains(new TableStat.Column("orders", "order_total")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OracleDropTypeTest_0 extends OracleTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = //
|
||||
"DROP type dmcfck;";
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
print(statementList);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ORACLE);
|
||||
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("DROP TYPE dmcfck;"
|
||||
, SQLUtils.toSQLString(stmt, JdbcConstants.ORACLE));
|
||||
|
||||
assertEquals(0, visitor.getTables().size());
|
||||
|
||||
// assertTrue(visitor.getTables().containsKey(new TableStat.Name("t1")));
|
||||
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
|
||||
// assertTrue(visitor.getColumns().contains(new TableStat.Column("xwarehouses", "sales_rep_id")));
|
||||
// assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "YEAR")));
|
||||
// assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "order_mode")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user