improved sql parser.

This commit is contained in:
wenshao
2017-08-03 10:41:23 +08:00
parent 9df6894d83
commit b0c38aef68
70 changed files with 1091 additions and 109 deletions
@@ -32,6 +32,8 @@ public class SQLBlockStatement extends SQLStatementImpl {
private List<SQLStatement> statementList = new ArrayList<SQLStatement>();
public SQLStatement exception;
public List<SQLStatement> getStatementList() {
return statementList;
}
@@ -48,8 +50,6 @@ public class SQLBlockStatement extends SQLStatementImpl {
this.labelName = labelName;
}
public SQLStatement exception;
@Override
public void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -86,4 +86,28 @@ public class SQLBlockStatement extends SQLStatementImpl {
public void setEndLabel(String endLabel) {
this.endLabel = endLabel;
}
public SQLBlockStatement clone() {
SQLBlockStatement x = new SQLBlockStatement();
x.labelName = labelName;
x.endLabel = endLabel;
for (SQLParameter p : parameters) {
SQLParameter p2 = p.clone();
p2.setParent(x);
x.parameters.add(p2);
}
for (SQLStatement stmt : statementList) {
SQLStatement stmt2 = stmt.clone();
stmt2.setParent(x);
x.statementList.add(stmt2);
}
if (exception != null) {
x.setException(exception.clone());
}
return x;
}
}
@@ -35,6 +35,24 @@ public class SQLCommitStatement extends SQLStatementImpl {
private SQLExpr transactionName;
private SQLExpr delayedDurability;
public SQLCommitStatement clone() {
SQLCommitStatement x = new SQLCommitStatement();
x.write = write;
x.wait = wait;
x.immediate = immediate;
x.work = work;
x.chain = chain;
x.release = release;
if(transactionName != null) {
x.setTransactionName(transactionName.clone());
}
if (delayedDurability != null) {
x.setDelayedDurability(delayedDurability.clone());
}
return x;
}
public void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, transactionName);
@@ -25,7 +25,7 @@ import java.util.List;
/**
* Created by wenshao on 23/05/2017.
*/
public class SQLCreateFunctionStatement extends SQLStatementImpl {
public class SQLCreateFunctionStatement extends SQLStatementImpl implements SQLDDLStatement {
private SQLName definer;
private boolean create = true;
@@ -47,6 +47,38 @@ public class SQLCreateFunctionStatement extends SQLStatementImpl {
private boolean deterministic = false;
public SQLCreateFunctionStatement clone() {
SQLCreateFunctionStatement x = new SQLCreateFunctionStatement();
if (definer != null) {
x.setDefiner(definer.clone());
}
x.create = create;
x.orReplace = orReplace;
if (name != null) {
x.setName(name.clone());
}
if (block != null) {
x.setBlock(block.clone());
}
for (SQLParameter p : parameters) {
SQLParameter p2 = p.clone();
p2.setParent(x);
x.parameters.add(p2);
}
x.javaCallSpec = javaCallSpec;
if (authid != null) {
x.setAuthid(authid.clone());
}
if (returnDataType != null) {
x.setReturnDataType(returnDataType.clone());
}
x.comment = comment;
x.deterministic = deterministic;
return x;
}
@Override
public void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -135,4 +135,23 @@ public class SQLCreateIndexStatement extends SQLStatementImpl implements SQLDDLS
return null;
}
public SQLCreateIndexStatement clone() {
SQLCreateIndexStatement x = new SQLCreateIndexStatement();
if (name != null) {
x.setName(name.clone());
}
if (table != null) {
x.setTable(table.clone());
}
for (SQLSelectOrderByItem item : items) {
SQLSelectOrderByItem item2 = item.clone();
item2.setParent(x);
x.items.add(item2);
}
x.type = type;
x.using = using;
return x;
}
}
@@ -26,7 +26,7 @@ import com.alibaba.druid.sql.visitor.SQLASTVisitor;
/**
* Created by wenshao on 16/9/14.
*/
public class SQLCreateSequenceStatement extends SQLStatementImpl {
public class SQLCreateSequenceStatement extends SQLStatementImpl implements SQLDDLStatement {
private SQLName name;
private SQLExpr startWith;
@@ -280,4 +280,40 @@ public class SQLCreateViewStatement extends SQLStatementImpl implements SQLDDLSt
}
}
}
public SQLCreateViewStatement clone() {
SQLCreateViewStatement x = new SQLCreateViewStatement();
x.orReplace = orReplace;
x.force = force;
if (subQuery != null) {
x.setSubQuery(subQuery.clone());
}
x.ifNotExists = ifNotExists;
x.algorithm = algorithm;
if (definer != null) {
x.setDefiner(definer.clone());
}
x.sqlSecurity = sqlSecurity;
if (tableSource != null) {
x.setTableSource(tableSource.clone());
}
for (SQLTableElement column : columns) {
SQLTableElement column2 = column.clone();
column2.setParent(x);
x.columns.add(column2);
}
x.withCheckOption = withCheckOption;
x.withCascaded = withCascaded;
x.withLocal = withLocal;
x.withReadOnly = withReadOnly;
if (comment != null) {
x.setComment(comment.clone());
}
return x;
}
}
@@ -60,4 +60,18 @@ public class SQLErrorLoggingClause extends SQLObjectImpl {
this.limit = limit;
}
public SQLErrorLoggingClause clone() {
SQLErrorLoggingClause x = new SQLErrorLoggingClause();
if (into != null) {
x.setInto(into.clone());
}
if (simpleExpression != null) {
x.setSimpleExpression(simpleExpression.clone());
}
if (limit != null) {
x.setLimit(limit.clone());
}
return x;
}
}
@@ -52,4 +52,11 @@ public class SQLExprHint extends SQLObjectImpl implements SQLHint {
visitor.endVisit(this);
}
public SQLExprHint clone() {
SQLExprHint x = new SQLExprHint();
if (expr != null) {
x.setExpr(expr.clone());
}
return x;
}
}
@@ -223,4 +223,20 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
}
return null;
}
public SQLTableSource findTableSourceWithColumn(String columnName) {
if (schemaObject == null) {
return null;
}
SQLStatement stmt = schemaObject.getStatement();
if (stmt instanceof SQLCreateTableStatement) {
SQLCreateTableStatement createTableStmt = (SQLCreateTableStatement) stmt;
if (createTableStmt.findColumn(columnName) != null) {
return this;
}
}
return null;
}
}
@@ -31,6 +31,26 @@ public class SQLIfStatement extends SQLStatementImpl {
private List<ElseIf> elseIfList = new ArrayList<ElseIf>();
private Else elseItem;
public SQLIfStatement clone() {
SQLIfStatement x = new SQLIfStatement();
for (SQLStatement stmt : statements) {
SQLStatement stmt2 = stmt.clone();
stmt2.setParent(x);
x.statements.add(stmt2);
}
for (ElseIf o : elseIfList) {
ElseIf o2 = o.clone();
o2.setParent(x);
x.elseIfList.add(o2);
}
if (elseItem != null) {
x.setElseItem(elseItem.clone());
}
return x;
}
@Override
public void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -111,6 +131,21 @@ public class SQLIfStatement extends SQLStatementImpl {
}
this.condition = condition;
}
public ElseIf clone() {
ElseIf x = new ElseIf();
if (condition != null) {
x.setCondition(condition.clone());
}
for (SQLStatement stmt : statements) {
SQLStatement stmt2 = stmt.clone();
stmt2.setParent(x);
x.statements.add(stmt2);
}
return x;
}
}
public static class Else extends SQLObjectImpl {
@@ -133,5 +168,14 @@ public class SQLIfStatement extends SQLStatementImpl {
this.statements = statements;
}
public Else clone() {
Else x = new Else();
for (SQLStatement stmt : statements) {
SQLStatement stmt2 = stmt.clone();
stmt2.setParent(x);
x.statements.add(stmt2);
}
return x;
}
}
}
@@ -36,6 +36,27 @@ public abstract class SQLInsertInto extends SQLObjectImpl {
}
public void cloneTo(SQLInsertInto x) {
if (tableSource != null) {
x.setTableSource(tableSource.clone());
}
for (SQLExpr column : columns) {
SQLExpr column2 = column.clone();
column2.setParent(x);
x.columns.add(column2);
}
if (query != null) {
x.setQuery(query.clone());
}
for (ValuesClause v : valuesList) {
ValuesClause v2 = v.clone();
v2.setParent(x);
x.valuesList.add(v2);
}
}
public abstract SQLInsertInto clone();
public String getAlias() {
return tableSource.getAlias();
}
@@ -34,6 +34,19 @@ public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
}
public void cloneTo(SQLInsertStatement x) {
super.cloneTo(x);
x.dbType = dbType;
x.upsert = upsert;
x.afterSemi = afterSemi;
}
public SQLInsertStatement clone() {
SQLInsertStatement x = new SQLInsertStatement();
cloneTo(x);
return x;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
@@ -62,6 +75,14 @@ public class SQLInsertStatement extends SQLInsertInto implements SQLStatement {
this(new ArrayList<SQLExpr>());
}
public ValuesClause clone() {
ValuesClause x = new ValuesClause(new ArrayList<SQLExpr>(this.values.size()));
for (SQLExpr v : values) {
x.addValue(v);
}
return x;
}
public ValuesClause(List<SQLExpr> values){
this.values = values;
for (int i = 0; i < values.size(); ++i) {
@@ -285,6 +285,10 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
if (right instanceof SQLJoinTableSource) {
SQLJoinTableSource rightJoin = (SQLJoinTableSource) right;
if (rightJoin.joinType != JoinType.COMMA && rightJoin.joinType != JoinType.INNER_JOIN) {
return;
}
SQLTableSource a = left;
SQLTableSource b = rightJoin.getLeft();
SQLTableSource c = rightJoin.getRight();
@@ -384,4 +388,20 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
return null;
}
@Override
public SQLTableSource findTableSourceWithColumn(String columnName) {
if (left != null) {
SQLTableSource tableSource = left.findTableSourceWithColumn(columnName);
if (tableSource != null) {
return tableSource;
}
}
if (right != null) {
return right.findTableSourceWithColumn(columnName);
}
return null;
}
}
@@ -49,4 +49,12 @@ public class SQLReturnStatement extends SQLStatementImpl {
}
visitor.endVisit(this);
}
public SQLReturnStatement clone() {
SQLReturnStatement x = new SQLReturnStatement();
if (expr != null) {
x.setExpr(expr.clone());
}
return x;
}
}
@@ -83,6 +83,17 @@ public class SQLSelectGroupByClause extends SQLObjectImpl {
}
public SQLSelectGroupByClause clone() {
throw new UnsupportedOperationException();
SQLSelectGroupByClause x = new SQLSelectGroupByClause();
for (SQLExpr item : items) {
SQLExpr item2 = item.clone();
item2.setParent(x);
x.items.add(item2);
}
if (having != null) {
x.setHaving(having.clone());
}
x.withRollUp = withRollUp;
x.withCube = withCube;
return x;
}
}
@@ -62,10 +62,10 @@ public class SQLSelectItem extends SQLObjectImpl implements SQLReplaceable {
}
public void setExpr(SQLExpr expr) {
this.expr = expr;
if (expr != null) {
expr.setParent(this);
}
this.expr = expr;
}
public String computeAlias() {
@@ -151,7 +151,7 @@ public class SQLSelectItem extends SQLObjectImpl implements SQLReplaceable {
SQLSelectItem x = new SQLSelectItem();
x.alias = alias;
if (expr != null) {
x.expr = expr.clone();
x.setExpr(expr.clone());
}
x.connectByRoot = connectByRoot;
return x;
@@ -438,6 +438,13 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
return findTableSource(from, alias);
}
public SQLTableSource findTableSourceWithColumn(String column) {
if (from == null) {
return null;
}
return from.findTableSourceWithColumn(column);
}
private static SQLTableSource findTableSource(SQLTableSource from, String alias) {
if (from == null || alias == null) {
return null;
@@ -489,6 +496,14 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
return null;
}
public SQLColumnDefinition findColumn(String columnName) {
if (from == null) {
return null;
}
return from.findColumn(columnName);
}
public void addCondition(SQLExpr expr) {
if (expr == null) {
return;
@@ -73,4 +73,19 @@ public class SQLSelectStatement extends SQLStatementImpl {
public void setHeadHints(List<SQLCommentHint> headHints) {
this.headHints = headHints;
}
public SQLSelectStatement clone() {
SQLSelectStatement x = new SQLSelectStatement();
if (select != null) {
x.setSelect(select.clone());
}
if (headHints != null) {
for (SQLCommentHint h : headHints) {
SQLCommentHint h2 = h.clone();
h2.setParent(x);
x.headHints.add(h2);
}
}
return x;
}
}
@@ -91,4 +91,21 @@ public class SQLSetStatement extends SQLStatementImpl {
item.output(buf);
}
}
public SQLSetStatement clone() {
SQLSetStatement x = new SQLSetStatement();
for (SQLAssignItem item : items) {
SQLAssignItem item2 = item.clone();
item2.setParent(x);
x.items.add(item2);
}
if (hints != null) {
for (SQLCommentHint hint : hints) {
SQLCommentHint h2 = hint.clone();
h2.setParent(x);
x.hints.add(h2);
}
}
return x;
}
}
@@ -38,4 +38,5 @@ public interface SQLTableSource extends SQLObject {
void setFlashback(SQLExpr flashback);
SQLColumnDefinition findColumn(String columnName);
SQLTableSource findTableSourceWithColumn(String columnName);
}
@@ -98,4 +98,8 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
public SQLColumnDefinition findColumn(String columnName) {
return null;
}
public SQLTableSource findTableSourceWithColumn(String columnName) {
return null;
}
}
@@ -68,4 +68,19 @@ public class SQLWhileStatement extends SQLStatementImpl {
public void setCondition(SQLExpr condition) {
this.condition = condition;
}
public SQLWhileStatement clone() {
SQLWhileStatement x = new SQLWhileStatement();
if (condition != null) {
x.setCondition(condition.clone());
}
for (SQLStatement stmt : statements) {
SQLStatement stmt2 = stmt.clone();
stmt2.setParent(x);
x.statements.add(stmt2);
}
x.labelName = labelName;
return x;
}
}