improved sql parser schema repository.

This commit is contained in:
wenshao
2017-08-07 16:18:13 +08:00
parent b0c38aef68
commit ea8017382f
25 changed files with 722 additions and 66 deletions
@@ -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;
}
}