mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser schema repository.
This commit is contained in:
@@ -190,7 +190,19 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
|
||||
}
|
||||
|
||||
public SQLBinaryOpExpr clone() {
|
||||
return new SQLBinaryOpExpr(left, operator, right, dbType);
|
||||
SQLBinaryOpExpr x = new SQLBinaryOpExpr();
|
||||
|
||||
if (left != null) {
|
||||
x.setLeft(left.clone());
|
||||
}
|
||||
if (right != null) {
|
||||
x.setRight(right.clone());
|
||||
}
|
||||
x.operator = operator;
|
||||
x.dbType = dbType;
|
||||
x.bracket = bracket;
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
@@ -452,4 +464,30 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean conditionContainsTable(String alias) {
|
||||
if (left == null || right == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (left instanceof SQLPropertyExpr) {
|
||||
if (((SQLPropertyExpr) left).matchOwner(alias)) {
|
||||
return true;
|
||||
}
|
||||
} else if (left instanceof SQLBinaryOpExpr) {
|
||||
if (((SQLBinaryOpExpr) left).conditionContainsTable(alias)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (right instanceof SQLPropertyExpr) {
|
||||
if (((SQLPropertyExpr) right).matchOwner(alias)) {
|
||||
return true;
|
||||
}
|
||||
} else if (right instanceof SQLBinaryOpExpr) {
|
||||
return ((SQLBinaryOpExpr) right).conditionContainsTable(alias);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExprImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
|
||||
@@ -30,6 +30,7 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
|
||||
private transient Boolean parameter;
|
||||
|
||||
private transient SQLColumnDefinition resolvedColumn;
|
||||
private transient SQLTableSource resolvedTableSource;
|
||||
|
||||
public SQLIdentifierExpr(){
|
||||
|
||||
@@ -149,11 +150,32 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
|
||||
this.resolvedColumn = resolvedColumn;
|
||||
}
|
||||
|
||||
public SQLTableSource getResolvedTableSource() {
|
||||
return resolvedTableSource;
|
||||
}
|
||||
|
||||
public void setResolvedTableSource(SQLTableSource resolvedTableSource) {
|
||||
this.resolvedTableSource = resolvedTableSource;
|
||||
}
|
||||
|
||||
public SQLDataType computeDataType() {
|
||||
if (resolvedColumn != null) {
|
||||
return resolvedColumn.getDataType();
|
||||
}
|
||||
|
||||
if (resolvedTableSource != null
|
||||
&& resolvedTableSource instanceof SQLSubqueryTableSource) {
|
||||
SQLSelect select = ((SQLSubqueryTableSource) resolvedTableSource).getSelect();
|
||||
SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock();
|
||||
if (queryBlock == null) {
|
||||
return null;
|
||||
}
|
||||
SQLSelectItem selectItem = queryBlock.findSelectItem(name);
|
||||
if (selectItem != null) {
|
||||
return selectItem.computeDataType();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLExprImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLReplaceable;
|
||||
@@ -177,6 +178,9 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
|
||||
|
||||
@Override
|
||||
public boolean replace(SQLExpr expr, SQLExpr target) {
|
||||
if (target == null) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < parameters.size(); ++i) {
|
||||
if (parameters.get(i) == expr) {
|
||||
parameters.set(i, target);
|
||||
@@ -210,4 +214,24 @@ public class SQLMethodInvokeExpr extends SQLExprImpl implements SQLReplaceable,
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public SQLDataType computeDataType() {
|
||||
if (parameters.size() == 1) {
|
||||
if (SQLUtils.nameEquals("trunc", methodName)) {
|
||||
return parameters.get(0).computeDataType();
|
||||
}
|
||||
} else if (parameters.size() == 2) {
|
||||
SQLExpr param0 = parameters.get(0);
|
||||
SQLExpr param1 = parameters.get(1);
|
||||
if (SQLUtils.nameEquals("nvl", methodName) || SQLUtils.nameEquals("ifnull", methodName)) {
|
||||
SQLDataType dataType = param0.computeDataType();
|
||||
if (dataType != null) {
|
||||
return dataType;
|
||||
}
|
||||
|
||||
return param1.computeDataType();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import com.alibaba.druid.sql.ast.SQLDataType;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLExprImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
@@ -30,6 +30,7 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
|
||||
private String name;
|
||||
|
||||
private transient SQLColumnDefinition resolvedColumn;
|
||||
private transient SQLTableSource resolvedTableSource;
|
||||
|
||||
public SQLPropertyExpr(String owner, String name){
|
||||
this(new SQLIdentifierExpr(owner), name);
|
||||
@@ -170,11 +171,32 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
|
||||
this.resolvedColumn = resolvedColumn;
|
||||
}
|
||||
|
||||
public SQLTableSource getResolvedTableSource() {
|
||||
return resolvedTableSource;
|
||||
}
|
||||
|
||||
public void setResolvedTableSource(SQLTableSource resolvedTableSource) {
|
||||
this.resolvedTableSource = resolvedTableSource;
|
||||
}
|
||||
|
||||
public SQLDataType computeDataType() {
|
||||
if (resolvedColumn != null) {
|
||||
return resolvedColumn.getDataType();
|
||||
}
|
||||
|
||||
if (resolvedTableSource != null
|
||||
&& resolvedTableSource instanceof SQLSubqueryTableSource) {
|
||||
SQLSelect select = ((SQLSubqueryTableSource) resolvedTableSource).getSelect();
|
||||
SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock();
|
||||
if (queryBlock == null) {
|
||||
return null;
|
||||
}
|
||||
SQLSelectItem selectItem = queryBlock.findSelectItem(name);
|
||||
if (selectItem != null) {
|
||||
return selectItem.computeDataType();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,15 @@ public class SQLSequenceExpr extends SQLExprImpl {
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
public SQLSequenceExpr clone() {
|
||||
SQLSequenceExpr x = new SQLSequenceExpr();
|
||||
if (sequence != null) {
|
||||
x.setSequence(sequence.clone());
|
||||
}
|
||||
x.function = function;
|
||||
return x;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
|
||||
Reference in New Issue
Block a user