improved sql parser. support show columns.

This commit is contained in:
wenshao
2017-07-28 14:44:02 +08:00
parent 3c7efab522
commit 9aa9406bfa
25 changed files with 1274 additions and 10 deletions
@@ -366,4 +366,14 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
identExpr.setName(columnName);
}
}
public boolean containsNotNullConstaint() {
for (SQLColumnConstraint constraint : this.constraints) {
if (constraint instanceof SQLNotNullConstraint) {
return true;
}
}
return false;
}
}
@@ -15,18 +15,20 @@
*/
package com.alibaba.druid.sql.ast.statement;
import java.io.IOException;
import java.util.*;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlShowColumnOutpuVisitor;
import com.alibaba.druid.util.ListDG;
import com.alibaba.druid.util.lang.Consumer;
import org.apache.ibatis.jdbc.SQL;
public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLStatement {
@@ -192,6 +194,73 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
return null;
}
public boolean isPrimaryColumn(String columnName) {
SQLPrimaryKey pk = this.findPrimaryKey();
if (pk == null) {
return false;
}
return pk.containsColumn(columnName);
}
/**
* only for show columns
*/
public boolean isMUL(String columnName) {
for (SQLTableElement element : this.tableElementList) {
if (element instanceof MySqlUnique) {
MySqlUnique unique = (MySqlUnique) element;
SQLExpr column = unique.getColumns().get(0);
if (column instanceof SQLIdentifierExpr
&& SQLUtils.nameEquals(columnName, ((SQLIdentifierExpr) column).getName())) {
return unique.columns.size() > 1;
}
} else if (element instanceof MySqlKey) {
MySqlKey unique = (MySqlKey) element;
SQLExpr column = unique.getColumns().get(0);
if (column instanceof SQLIdentifierExpr
&& SQLUtils.nameEquals(columnName, ((SQLIdentifierExpr) column).getName())) {
return true;
}
}
}
return false;
}
/**
* only for show columns
*/
public boolean isUNI(String columnName) {
for (SQLTableElement element : this.tableElementList) {
if (element instanceof MySqlUnique) {
MySqlUnique unique = (MySqlUnique) element;
SQLExpr column = unique.getColumns().get(0);
if (column instanceof SQLIdentifierExpr
&& SQLUtils.nameEquals(columnName, ((SQLIdentifierExpr) column).getName())) {
return unique.columns.size() == 1;
}
}
}
return false;
}
public MySqlUnique findUnique(String columnName) {
for (SQLTableElement element : this.tableElementList) {
if (element instanceof MySqlUnique) {
MySqlUnique unique = (MySqlUnique) element;
if (unique.containsColumn(columnName)) {
return unique;
}
}
}
return null;
}
public SQLTableElement findIndex(String columnName) {
for (SQLTableElement element : tableElementList) {
List<SQLExpr> keyColumns = null;
@@ -418,7 +487,6 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
}
}
}
}
public void simplify() {
@@ -453,4 +521,83 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
}
}
}
public boolean apply(SQLAlterTableStatement alter) {
if (!SQLUtils.nameEquals(alter.getName(), this.getName())) {
return false;
}
int applyCount = 0;
for (SQLAlterTableItem item : alter.getItems()) {
if (item instanceof SQLAlterTableDropColumnItem) {
if (apply((SQLAlterTableDropColumnItem) item)) {
applyCount++;
}
} else if (item instanceof SQLAlterTableAddColumn) {
if (apply((SQLAlterTableAddColumn) item)) {
applyCount++;
}
}
}
return applyCount > 0;
}
private boolean apply(SQLAlterTableDropColumnItem item) {
for (SQLName column : item.getColumns()) {
String columnName = column.getSimpleName();
for (int i = tableElementList.size() - 1; i >= 0; --i) {
SQLTableElement e = tableElementList.get(i);
if (e instanceof SQLColumnDefinition) {
if (SQLUtils.nameEquals(columnName, ((SQLColumnDefinition) e).getName().getSimpleName())) {
tableElementList.remove(i);
}
}
}
}
return true;
}
private boolean apply(SQLAlterTableAddColumn item) {
int startIndex = tableElementList.size();
if (item.isFirst()) {
startIndex = 0;
}
int afterIndex = columnIndexOf(item.getAfterColumn());
if (afterIndex != -1) {
startIndex = afterIndex + 1;
}
int beforeIndex = columnIndexOf(item.getFirstColumn());
if (beforeIndex != -1) {
startIndex = beforeIndex;
}
for (int i = 0; i < item.getColumns().size(); i++) {
SQLColumnDefinition column = item.getColumns().get(i);
tableElementList.add(i + startIndex, column);
column.setParent(this);
}
return true;
}
private int columnIndexOf(SQLName column) {
if (column == null) {
return -1;
}
String columnName = column.getSimpleName();
for (int i = tableElementList.size() - 1; i >= 0; --i) {
SQLTableElement e = tableElementList.get(i);
if (e instanceof SQLColumnDefinition) {
if (SQLUtils.nameEquals(columnName, ((SQLColumnDefinition) e).getName().getSimpleName())) {
return i;
}
}
}
return -1;
}
}
@@ -21,6 +21,8 @@ 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.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.repository.SchemaObject;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -150,4 +152,23 @@ public class SQLExprTableSource extends SQLTableSourceImpl {
public void setSchemaObject(SchemaObject schemaObject) {
this.schemaObject = schemaObject;
}
public boolean containsAlias(String alias) {
if (SQLUtils.nameEquals(this.alias, alias)) {
return true;
}
String name = null;
if (expr instanceof SQLIdentifierExpr) {
name = ((SQLIdentifierExpr) expr).getName();
} else if (expr instanceof SQLPropertyExpr) {
name = ((SQLPropertyExpr) expr).getName();
}
if (name != null && SQLUtils.nameEquals(name, alias)) {
return true;
}
return false;
}
}
@@ -18,9 +18,13 @@ 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.SQLReplaceable;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplaceable {
@@ -121,6 +125,33 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
this.condition = condition;
}
public void addConditionn(SQLExpr condition) {
this.condition = SQLBinaryOpExpr.and(this.condition, condition);
}
public void addConditionnIfAbsent(SQLExpr condition) {
if (this.containsCondition(condition)) {
return;
}
this.condition = SQLBinaryOpExpr.and(this.condition, condition);
}
public boolean containsCondition(SQLExpr condition) {
if (this.condition == null) {
return false;
}
if (this.condition.equals(condition)) {
return false;
}
if (this.condition instanceof SQLBinaryOpExpr) {
return ((SQLBinaryOpExpr) this.condition).contains(condition);
}
return false;
}
public List<SQLExpr> getUsing() {
return this.using;
}
@@ -247,6 +278,9 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
* a inner_join (b inner_join c) -&lt; a inner_join b innre_join c
*/
public void rearrangement() {
if (joinType != JoinType.COMMA && joinType != JoinType.INNER_JOIN) {
return;
}
if (right instanceof SQLJoinTableSource) {
SQLJoinTableSource rightJoin = (SQLJoinTableSource) right;
@@ -259,10 +293,79 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
setLeft(rightJoin);
rightJoin.setLeft(a);
rightJoin.setRight(b);
rightJoin.setCondition(on_ab);
boolean on_ab_match = false;
if (on_ab instanceof SQLBinaryOpExpr) {
SQLBinaryOpExpr on_ab_binaryOpExpr = (SQLBinaryOpExpr) on_ab;
if (on_ab_binaryOpExpr.getLeft() instanceof SQLPropertyExpr
&& on_ab_binaryOpExpr.getRight() instanceof SQLPropertyExpr) {
String leftOwnerName = ((SQLPropertyExpr) on_ab_binaryOpExpr.getLeft()).getOwnernName();
String rightOwnerName = ((SQLPropertyExpr) on_ab_binaryOpExpr.getRight()).getOwnernName();
if (rightJoin.containsAlias(leftOwnerName) && rightJoin.containsAlias(rightOwnerName)) {
on_ab_match = true;
}
}
}
if (on_ab_match) {
rightJoin.setCondition(on_ab);
} else {
rightJoin.setCondition(null);
on_bc = SQLBinaryOpExpr.and(on_bc, on_ab);
}
setRight(c);
setCondition(on_bc);
}
}
public boolean contains(SQLTableSource tableSource, SQLExpr conditionn) {
if (right.equals(tableSource)) {
if (this.condition == conditionn) {
return true;
}
return this.condition != null && this.condition.equals(conditionn);
}
if (left instanceof SQLJoinTableSource) {
return ((SQLJoinTableSource) left).contains(tableSource, conditionn);
}
return false;
}
public SQLJoinTableSource findJoin(SQLTableSource tableSource, JoinType joinType) {
if (right.equals(tableSource)) {
if (this.joinType == joinType) {
return this;
}
return null;
}
if (left instanceof SQLJoinTableSource) {
return ((SQLJoinTableSource) left).findJoin(tableSource, joinType);
}
return null;
}
public boolean containsAlias(String alias) {
if (SQLUtils.nameEquals(this.alias, alias)) {
return true;
}
if (left != null && left.containsAlias(alias)) {
return true;
}
if (right != null && right.containsAlias(alias)) {
return true;
}
return false;
}
}
@@ -162,6 +162,26 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
this.from = from;
}
public void setFrom(SQLSelectQueryBlock queryBlock, String alias) {
if (queryBlock == null) {
this.from = null;
return;
}
this.setFrom(new SQLSelect(queryBlock), alias);
}
public void setFrom(SQLSelect select, String alias) {
if (select == null) {
this.from = null;
return;
}
SQLSubqueryTableSource from = new SQLSubqueryTableSource(select);
from.setAlias(alias);
this.setFrom(from);
}
public void setFrom(String tableName, String alias) {
SQLExprTableSource from;
if (tableName == null || tableName.length() == 0) {
@@ -436,6 +456,10 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
return findTableSource(join.getRight(), alias);
}
if (from instanceof SQLExprTableSource && from.containsAlias(alias)) {
return from;
}
return null;
}
@@ -481,4 +505,32 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
setLimit(limit);
}
public int selectIndexOf(SQLExpr expr) {
String ident = null;
if (expr instanceof SQLIdentifierExpr) {
ident = ((SQLIdentifierExpr) expr).getName();
}
for (int i = 0; i < selectList.size(); i++) {
SQLSelectItem selectItem = selectList.get(i);
if (ident != null && SQLUtils.nameEquals(ident, selectItem.alias)) {
return i;
}
SQLExpr selectItemExpr = selectItem.getExpr();
if (selectItemExpr instanceof SQLName) {
String name = ((SQLName) selectItemExpr).getSimpleName();
if (SQLUtils.nameEquals(ident, name)) {
return i;
}
}
if (expr.equals(selectItemExpr)) {
return i;
}
}
return -1;
}
}
@@ -32,6 +32,7 @@ public interface SQLTableSource extends SQLObject {
SQLTableSource clone();
String computeAlias();
boolean containsAlias(String alias);
SQLExpr getFlashback();
void setFlashback(SQLExpr flashback);
@@ -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.SQLHint;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
@@ -84,4 +85,13 @@ public abstract class SQLTableSourceImpl extends SQLObjectImpl implements SQLTab
}
this.flashback = flashback;
}
public boolean containsAlias(String alias) {
if (SQLUtils.nameEquals(this.alias, alias)) {
return true;
}
return false;
}
}
@@ -52,6 +52,17 @@ public class SQLUnique extends SQLConstraintImpl implements SQLUniqueConstraint,
visitor.endVisit(this);
}
public boolean containsColumn(String column) {
for (SQLExpr expr : columns) {
if (expr instanceof SQLIdentifierExpr) {
if (SQLUtils.nameEquals(((SQLIdentifierExpr) expr).getName(), column)) {
return true;
}
}
}
return false;
}
public void cloneTo(SQLUnique x) {
super.cloneTo(x);
@@ -23,4 +23,5 @@ public interface SQLUniqueConstraint extends SQLConstraint {
List<SQLExpr> getColumns();
boolean containsColumn(String column);
}