improved sql parser.

This commit is contained in:
wenshao
2017-07-23 16:44:43 +08:00
parent 82fe08109f
commit 6829a23bce
41 changed files with 1183 additions and 514 deletions
@@ -15,6 +15,8 @@
*/
package com.alibaba.druid.sql.ast;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -50,10 +52,18 @@ public class SQLLimit extends SQLObjectImpl {
this.rowCount = rowCount;
}
public void setRowCount(int rowCount) {
this.setRowCount(new SQLIntegerExpr(rowCount));
}
public SQLExpr getOffset() {
return offset;
}
public void setOffset(int offset) {
this.setOffset(new SQLIntegerExpr(offset));
}
public void setOffset(SQLExpr offset) {
if (offset != null) {
offset.setParent(this);
@@ -53,6 +53,19 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
setLeft(left);
setRight(right);
this.operator = operator;
if (dbType == null) {
if (left instanceof SQLBinaryOpExpr) {
dbType = ((SQLBinaryOpExpr) left).dbType;
}
}
if (dbType == null) {
if (right instanceof SQLBinaryOpExpr) {
dbType = ((SQLBinaryOpExpr) right).dbType;
}
}
this.dbType = dbType;
}
@@ -392,4 +405,16 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
return false;
}
public SQLExpr other(SQLExpr x) {
if (x == left) {
return right;
}
if (x == right) {
return left;
}
return null;
}
}
@@ -19,6 +19,7 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLExprImpl;
import com.alibaba.druid.sql.ast.SQLReplaceable;
@@ -185,4 +186,28 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
}
return false;
}
public boolean match(String owner, String function) {
if (function == null) {
return false;
}
if (!SQLUtils.nameEquals(function, methodName)) {
return false;
}
if (owner == null && this.owner == null) {
return true;
}
if (owner == null || this.owner == null) {
return false;
}
if (this.owner instanceof SQLIdentifierExpr) {
return SQLUtils.nameEquals(((SQLIdentifierExpr) this.owner).name, owner);
}
return false;
}
}
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.ast.statement;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
@@ -58,6 +59,20 @@ public class SQLCreateViewStatement extends SQLStatementImpl implements SQLDDLSt
super(dbType);
}
public String computeName() {
if (tableSource == null) {
return null;
}
SQLExpr expr = tableSource.getExpr();
if (expr instanceof SQLName) {
String name = ((SQLName) expr).getSimpleName();
return SQLUtils.normalize(name);
}
return null;
}
public boolean isOrReplace() {
return orReplace;
}
@@ -79,22 +79,22 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
this.left = left;
}
public void setLeft(String tableName) {
public void setLeft(String tableName, String alias) {
SQLExprTableSource tableSource;
if (tableName == null || tableName.length() == 0) {
tableSource = null;
} else {
tableSource = new SQLExprTableSource(new SQLIdentifierExpr(tableName));
tableSource = new SQLExprTableSource(new SQLIdentifierExpr(tableName), alias);
}
this.setLeft(tableSource);
}
public void setRight(String tableName) {
public void setRight(String tableName, String alias) {
SQLExprTableSource tableSource;
if (tableName == null || tableName.length() == 0) {
tableSource = null;
} else {
tableSource = new SQLExprTableSource(new SQLIdentifierExpr(tableName));
tableSource = new SQLExprTableSource(new SQLIdentifierExpr(tableName), alias);
}
this.setRight(tableSource);
}
@@ -228,4 +228,41 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
cloneTo(x);
return x;
}
public void reverse() {
SQLTableSource temp = left;
left = right;
right = temp;
if (left instanceof SQLJoinTableSource) {
((SQLJoinTableSource) left).reverse();
}
if (right instanceof SQLJoinTableSource) {
((SQLJoinTableSource) right).reverse();
}
}
/**
* a inner_join (b inner_join c) -> a inner_join b innre_join c
*/
public void rearrangement() {
if (right instanceof SQLJoinTableSource) {
SQLJoinTableSource rightJoin = (SQLJoinTableSource) right;
SQLTableSource a = left;
SQLTableSource b = rightJoin.getLeft();
SQLTableSource c = rightJoin.getRight();
SQLExpr on_ab = condition;
SQLExpr on_bc = rightJoin.condition;
setLeft(rightJoin);
rightJoin.setLeft(a);
rightJoin.setRight(b);
rightJoin.setCondition(on_ab);
setRight(c);
setCondition(on_bc);
}
}
}
@@ -162,12 +162,12 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
this.from = from;
}
public void setFrom(String tableName) {
public void setFrom(String tableName, String alias) {
SQLExprTableSource from;
if (tableName == null || tableName.length() == 0) {
from = null;
} else {
from = new SQLExprTableSource(new SQLIdentifierExpr(tableName));
from = new SQLExprTableSource(new SQLIdentifierExpr(tableName), alias);
}
this.setFrom(from);
}