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)) {
|
||||
|
||||
@@ -548,6 +548,27 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
}
|
||||
}
|
||||
|
||||
public boolean apply(SQLDropIndexStatement x) {
|
||||
for (int i = tableElementList.size() - 1; i >= 0; i--) {
|
||||
SQLTableElement e = tableElementList.get(i);
|
||||
if (e instanceof SQLUniqueConstraint) {
|
||||
SQLUniqueConstraint unique = (SQLUniqueConstraint) e;
|
||||
if (SQLUtils.nameEquals(unique.getName(), x.getIndexName())) {
|
||||
tableElementList.remove(i);
|
||||
return true;
|
||||
}
|
||||
|
||||
} else if (e instanceof MySqlTableIndex) {
|
||||
MySqlTableIndex tableIndex = (MySqlTableIndex) e;
|
||||
if (SQLUtils.nameEquals(tableIndex.getName(), x.getIndexName())) {
|
||||
tableElementList.remove(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean apply(SQLAlterTableStatement alter) {
|
||||
if (!SQLUtils.nameEquals(alter.getName(), this.getName())) {
|
||||
return false;
|
||||
@@ -716,7 +737,16 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (SQLTableElement e : tableElementList) {
|
||||
if (e instanceof SQLUnique) {
|
||||
((SQLUnique) e).applyDropColumn(column);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,12 +16,13 @@
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLStatementImpl;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLDropIndexStatement extends SQLStatementImpl implements SQLDDLStatement {
|
||||
|
||||
private SQLExpr indexName;
|
||||
private SQLName indexName;
|
||||
private SQLExprTableSource tableName;
|
||||
|
||||
public SQLDropIndexStatement() {
|
||||
@@ -32,11 +33,11 @@ public class SQLDropIndexStatement extends SQLStatementImpl implements SQLDDLSta
|
||||
super (dbType);
|
||||
}
|
||||
|
||||
public SQLExpr getIndexName() {
|
||||
public SQLName getIndexName() {
|
||||
return indexName;
|
||||
}
|
||||
|
||||
public void setIndexName(SQLExpr indexName) {
|
||||
public void setIndexName(SQLName indexName) {
|
||||
this.indexName = indexName;
|
||||
}
|
||||
|
||||
@@ -44,7 +45,7 @@ public class SQLDropIndexStatement extends SQLStatementImpl implements SQLDDLSta
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public void setTableName(SQLExpr tableName) {
|
||||
public void setTableName(SQLName tableName) {
|
||||
this.setTableName(new SQLExprTableSource(tableName));
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLReplaceable;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
@@ -404,4 +405,75 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean match(String alias_a, String alias_b) {
|
||||
if (left == null || right == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (left.containsAlias(alias_a) && right.containsAlias(alias_b)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return right.containsAlias(alias_a) && left.containsAlias(alias_b);
|
||||
}
|
||||
|
||||
/**
|
||||
* a, b left join c ->
|
||||
*/
|
||||
// public boolean addCondition(SQLBinaryOpExpr condition, JoinType joinType) {
|
||||
// if (this.left == null || this.right == null) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// if (!(condition.getLeft() instanceof SQLPropertyExpr
|
||||
// && condition.getRight() instanceof SQLPropertyExpr)) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// SQLPropertyExpr left = (SQLPropertyExpr) condition.getLeft();
|
||||
// SQLPropertyExpr right = (SQLPropertyExpr) condition.getRight();
|
||||
//
|
||||
// String leftOwner = left.getOwnernName();
|
||||
// String rightOwner = right.getOwnernName();
|
||||
//
|
||||
// if (this.left.containsAlias(leftOwner) && this.right.containsAlias(rightOwner)) {
|
||||
// if (this.joinType == joinType) {
|
||||
// this.addConditionnIfAbsent(condition);
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// if (this.joinType == null || this.joinType == JoinType.COMMA) {
|
||||
// this.joinType = joinType;
|
||||
// this.addConditionnIfAbsent(condition);
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// if (this.joinType == JoinType.LEFT_OUTER_JOIN && joinType == JoinType.RIGHT_OUTER_JOIN) {
|
||||
// if (this.left instanceof SQLJoinTableSource && ((SQLJoinTableSource) this.left).joinType == JoinType.COMMA) {
|
||||
// SQLJoinTableSource leftJoin = (SQLJoinTableSource) this.left;
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
public boolean conditionContainsTable(String alias) {
|
||||
if (condition == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (condition instanceof SQLBinaryOpExpr) {
|
||||
return ((SQLBinaryOpExpr) condition).conditionContainsTable(alias);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public SQLJoinTableSource join(SQLTableSource right, JoinType joinType, SQLExpr condition) {
|
||||
SQLJoinTableSource joined = new SQLJoinTableSource(this, joinType, right, condition);
|
||||
return joined;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -549,18 +549,4 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public SQLColumnDefinition computeColumn(SQLIdentifierExpr identExpr) {
|
||||
SQLColumnDefinition column = identExpr.getResolvedColumn();
|
||||
if (column != null) {
|
||||
return column;
|
||||
}
|
||||
|
||||
column = from.findColumn(identExpr.getName());
|
||||
if (column != null) {
|
||||
identExpr.setResolvedColumn(column);
|
||||
}
|
||||
|
||||
return column;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,9 @@ public class SQLSetStatement extends SQLStatementImpl {
|
||||
|
||||
public SQLSetStatement(SQLExpr target, SQLExpr value, String dbType){
|
||||
super (dbType);
|
||||
this.items.add(new SQLAssignItem(target, value));
|
||||
SQLAssignItem item = new SQLAssignItem(target, value);
|
||||
item.setParent(this);
|
||||
this.items.add(item);
|
||||
}
|
||||
|
||||
public static SQLSetStatement plus(SQLName target) {
|
||||
|
||||
@@ -101,4 +101,28 @@ public class SQLUnique extends SQLConstraintImpl implements SQLUniqueConstraint,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean applyColumnRename(SQLName columnName, SQLName to) {
|
||||
for (SQLSelectOrderByItem orderByItem : columns) {
|
||||
SQLExpr expr = orderByItem.getExpr();
|
||||
if (expr instanceof SQLName
|
||||
&& SQLUtils.nameEquals((SQLName) expr, columnName)) {
|
||||
orderByItem.setExpr(to.clone());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean applyDropColumn(SQLName columnName) {
|
||||
for (int i = columns.size() - 1; i >= 0; i--) {
|
||||
SQLExpr expr = columns.get(i).getExpr();
|
||||
if (expr instanceof SQLName
|
||||
&& SQLUtils.nameEquals((SQLName) expr, columnName)) {
|
||||
columns.remove(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user