improved sql parser.

This commit is contained in:
wenshao
2017-10-20 14:47:02 +08:00
parent 7715c7ee15
commit bb2bbd833c
11 changed files with 588 additions and 40 deletions
@@ -44,13 +44,13 @@ public class SQLCreateFunctionStatement extends SQLStatementImpl implements SQLC
// for mysql
private String comment;
private boolean deterministic = false;
private boolean parallelEnable;
private boolean aggregate;
private SQLName using;
private boolean pipelined;
private boolean resultCache;
private String wrappedSource;
public SQLCreateFunctionStatement clone() {
SQLCreateFunctionStatement x = new SQLCreateFunctionStatement();
@@ -257,4 +257,12 @@ public class SQLCreateFunctionStatement extends SQLStatementImpl implements SQLC
public void setResultCache(boolean resultCache) {
this.resultCache = resultCache;
}
public String getWrappedSource() {
return wrappedSource;
}
public void setWrappedSource(String wrappedSource) {
this.wrappedSource = wrappedSource;
}
}
@@ -46,6 +46,8 @@ public class SQLCreateProcedureStatement extends SQLStatementImpl implements SQL
private boolean readSqlData;
private boolean modifiesSqlData;
private String wrappedSource;
@Override
public void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -176,4 +178,12 @@ public class SQLCreateProcedureStatement extends SQLStatementImpl implements SQL
return null;
}
public String getWrappedSource() {
return wrappedSource;
}
public void setWrappedSource(String wrappedSource) {
this.wrappedSource = wrappedSource;
}
}
@@ -904,6 +904,25 @@ public class OracleStatementParser extends SQLStatementParser {
accept(Token.RPAREN);
}
if (lexer.identifierEquals(FnvHash.Constants.WRAPPED)) {
lexer.nextToken();
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();
stmt.setAfterSemi(true);
} else {
String wrappedString = lexer.text.substring(lexer.pos());
stmt.setWrappedSource(wrappedString);
lexer.reset(lexer.text.length(),(char) LayoutCharacters.EOI, Token.EOF);
return stmt;
}
return stmt;
}
accept(Token.RETURN);
SQLDataType returnDataType = this.exprParser.parseDataType(false);
stmt.setReturnDataType(returnDataType);
@@ -2588,6 +2607,23 @@ public class OracleStatementParser extends SQLStatementParser {
stmt.setAuthid(authid);
}
if (lexer.identifierEquals(FnvHash.Constants.WRAPPED)) {
lexer.nextToken();
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();
stmt.setAfterSemi(true);
} else {
String wrappedString = lexer.text.substring(lexer.pos());
stmt.setWrappedSource(wrappedString);
lexer.reset(lexer.text.length(),(char) LayoutCharacters.EOI, Token.EOF);
}
return stmt;
}
if (lexer.token() == Token.SEMI) {
lexer.nextToken();
return stmt;
@@ -2517,6 +2517,17 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
print(')');
}
String wrappedSource = x.getWrappedSource();
if (wrappedSource != null) {
print0(ucase ? " WRAPPED " : " wrapped ");
print0(wrappedSource);
if (x.isAfterSemi()) {
print(';');
}
return false;
}
println();
print(ucase ? "RETURN " : "return ");
x.getReturnDataType().accept(this);
@@ -2580,11 +2591,6 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
return false;
}
@Override
public void endVisit(SQLCreateProcedureStatement x) {
}
@Override
public boolean visit(OracleCreateDatabaseDbLinkStatement x) {
print0(ucase ? "CREATE " : "create ");
@@ -619,14 +619,20 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
SQLExpr target = item.getTarget();
target.accept(this);
if (target instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) target).getName().equalsIgnoreCase("TIME ZONE")) {
SQLExpr value = item.getValue();
if (target instanceof SQLIdentifierExpr
&& ((SQLIdentifierExpr) target).getName().equalsIgnoreCase("TIME ZONE")) {
print(' ');
} else {
print0(" TO ");
if (value instanceof SQLPropertyExpr
&& ((SQLPropertyExpr) value).getOwner() instanceof SQLVariantRefExpr) {
print0(" := ");
} else {
print0(" TO ");
}
}
SQLExpr value = item.getValue();
if (value instanceof SQLListExpr) {
SQLListExpr listExpr = (SQLListExpr) value;
printAndAccept(listExpr.getItems(), ", ");
@@ -2865,4 +2871,76 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
public void endVisit(OracleRunStatement x) {
}
@Override
public boolean visit(SQLIfStatement.Else x) {
print0(ucase ? "ELSE" : "else");
this.indentCount++;
println();
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
if (i != 0) {
println();
}
SQLStatement item = x.getStatements().get(i);
item.accept(this);
}
this.indentCount--;
return false;
}
@Override
public boolean visit(SQLIfStatement.ElseIf x) {
print0(ucase ? "ELSE IF " : "else if ");
x.getCondition().accept(this);
print0(ucase ? " THEN" : " then");
this.indentCount++;
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
println();
SQLStatement item = x.getStatements().get(i);
item.accept(this);
}
this.indentCount--;
return false;
}
@Override
public boolean visit(SQLIfStatement x) {
print0(ucase ? "IF " : "if ");
int lines = this.lines;
this.indentCount++;
x.getCondition().accept(this);
this.indentCount--;
if (lines != this.lines) {
println();
} else {
print(' ');
}
print0(ucase ? "THEN" : "then");
this.indentCount++;
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
println();
SQLStatement item = x.getStatements().get(i);
item.accept(this);
}
this.indentCount--;
for (SQLIfStatement.ElseIf elseIf : x.getElseIfList()) {
println();
elseIf.accept(this);
}
if (x.getElseItem() != null) {
println();
x.getElseItem().accept(this);
}
println();
print0(ucase ? "END IF" : "end if");
return false;
}
}
@@ -5901,31 +5901,34 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
}
SQLStatement block = x.getBlock();
if (block != null && !create) {
println();
print("IS");
println();
String wrappedSource = x.getWrappedSource();
if (wrappedSource != null) {
print0(ucase ? " WRAPPED " : " wrapped ");
print0(wrappedSource);
} 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");
}
if (block != null && !create) {
println();
print("IS");
println();
} else {
println(ucase ? "IS" : "is");
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");
}
}
}
}
String javaCallSpec = x.getJavaCallSpec();
if (javaCallSpec != null) {
print0(ucase ? "LANGUAGE JAVA NAME '" : "language java name '");
print0(javaCallSpec);
print('\'');
return false;
String javaCallSpec = x.getJavaCallSpec();
if (javaCallSpec != null) {
print0(ucase ? "LANGUAGE JAVA NAME '" : "language java name '");
print0(javaCallSpec);
print('\'');
return false;
}
}
boolean afterSemi = false;