mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser. support show columns.
This commit is contained in:
@@ -18,10 +18,7 @@ package com.alibaba.druid.sql;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.*;
|
||||
import com.alibaba.druid.sql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.db2.visitor.DB2OutputVisitor;
|
||||
@@ -803,6 +800,10 @@ public class SQLUtils {
|
||||
return name;
|
||||
}
|
||||
|
||||
public static boolean nameEquals(SQLName a, SQLName b) {
|
||||
return nameEquals(a.getSimpleName(), b.getSimpleName());
|
||||
}
|
||||
|
||||
public static boolean nameEquals(String a, String b) {
|
||||
if (a == b) {
|
||||
return true;
|
||||
|
||||
@@ -417,4 +417,16 @@ public class SQLBinaryOpExpr extends SQLExprImpl implements SQLReplaceable, Seri
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean contains(SQLExpr item) {
|
||||
if (item instanceof SQLBinaryOpExpr) {
|
||||
if (this.equalsIgoreOrder((SQLBinaryOpExpr) item)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return left.equals(item) || right.equals(item);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.ast.expr;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
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.visitor.SQLASTVisitor;
|
||||
|
||||
public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
|
||||
@@ -27,6 +28,8 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
|
||||
private transient String lowerName;
|
||||
private transient Boolean parameter;
|
||||
|
||||
private transient SQLColumnDefinition resolvedColumn;
|
||||
|
||||
public SQLIdentifierExpr(){
|
||||
|
||||
}
|
||||
@@ -136,4 +139,12 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
|
||||
public String normalizedName() {
|
||||
return SQLUtils.normalize(name);
|
||||
}
|
||||
|
||||
public SQLColumnDefinition getResolvedColumn() {
|
||||
return resolvedColumn;
|
||||
}
|
||||
|
||||
public void setResolvedColumn(SQLColumnDefinition resolvedColumn) {
|
||||
this.resolvedColumn = resolvedColumn;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ 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.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
@@ -27,6 +28,8 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
|
||||
private SQLExpr owner;
|
||||
private String name;
|
||||
|
||||
private transient SQLColumnDefinition resolvedColumn;
|
||||
|
||||
public SQLPropertyExpr(String owner, String name){
|
||||
this(new SQLIdentifierExpr(owner), name);
|
||||
}
|
||||
@@ -153,4 +156,12 @@ public class SQLPropertyExpr extends SQLExprImpl implements SQLName {
|
||||
|
||||
return ownerName + '.' + SQLUtils.normalize(name);
|
||||
}
|
||||
|
||||
public SQLColumnDefinition getResolvedColumn() {
|
||||
return resolvedColumn;
|
||||
}
|
||||
|
||||
public void setResolvedColumn(SQLColumnDefinition resolvedColumn) {
|
||||
this.resolvedColumn = resolvedColumn;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) -< 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);
|
||||
}
|
||||
|
||||
+25
@@ -15,11 +15,13 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLCommentHint;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
@@ -30,6 +32,7 @@ import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObjectImpl;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlShowColumnOutpuVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
|
||||
@@ -174,4 +177,26 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
|
||||
tableOptions.clear();
|
||||
super.simplify();
|
||||
}
|
||||
|
||||
public void showCoumns(Appendable out) throws IOException {
|
||||
this.accept(new MySqlShowColumnOutpuVisitor(out));
|
||||
}
|
||||
|
||||
public boolean apply(MySqlRenameTableStatement x) {
|
||||
for (MySqlRenameTableStatement.Item item : x.getItems()) {
|
||||
if (apply(item)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean apply(MySqlRenameTableStatement.Item item) {
|
||||
if (!SQLUtils.nameEquals((SQLName) item.getName(), this.getName())) {
|
||||
return false;
|
||||
}
|
||||
this.setName((SQLName) item.getTo().clone());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ public class MySqlSelectParser extends SQLSelectParser {
|
||||
|
||||
queryBlock.setForUpdate(true);
|
||||
|
||||
if (identifierEquals("NO_WAIT")) {
|
||||
if (identifierEquals("NO_WAIT") || identifierEquals("NOWAIT")) {
|
||||
lexer.nextToken();
|
||||
queryBlock.setNoWait(true);
|
||||
} else if (identifierEquals("WAIT")) {
|
||||
|
||||
@@ -306,7 +306,7 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
println();
|
||||
print0(ucase ? "FOR UPDATE" : "for update");
|
||||
if (x.isNoWait()) {
|
||||
print0(ucase ? " NO_WAIT" : " no_wait");
|
||||
print0(ucase ? " NOWAIT" : " nowait");
|
||||
} else if (x.getWaitTime() != null) {
|
||||
print0(ucase ? " WAIT " : " wait ");
|
||||
x.getWaitTime().accept(this);
|
||||
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
package com.alibaba.druid.sql.dialect.mysql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 27/07/2017.
|
||||
*/
|
||||
public class MySqlShowColumnOutpuVisitor extends MySqlOutputVisitor {
|
||||
public MySqlShowColumnOutpuVisitor(Appendable appender) {
|
||||
super(appender);
|
||||
}
|
||||
|
||||
public boolean visit(MySqlCreateTableStatement x) {
|
||||
List<SQLColumnDefinition> columns = new ArrayList<SQLColumnDefinition>();
|
||||
List<String> dataTypes = new ArrayList<String>();
|
||||
List<String> defaultValues = new ArrayList<String>();
|
||||
|
||||
int name_len = -1, dataType_len = -1, defaultVal_len = -1, extra_len = 5;
|
||||
for (SQLTableElement element : x.getTableElementList()) {
|
||||
if (element instanceof SQLColumnDefinition) {
|
||||
SQLColumnDefinition column = (SQLColumnDefinition) element;
|
||||
columns.add(column);
|
||||
|
||||
String name = SQLUtils.normalize(column.getName().getSimpleName());
|
||||
if (name_len < name.length()) {
|
||||
name_len = name.length();
|
||||
}
|
||||
|
||||
String dataType = column.getDataType().getName();
|
||||
if (column.getDataType().getArguments().size() > 0) {
|
||||
dataType += "(";
|
||||
for (int i = 0; i < column.getDataType().getArguments().size(); i++) {
|
||||
if (i != 0) {
|
||||
dataType += ",";
|
||||
}
|
||||
SQLExpr arg = column.getDataType().getArguments().get(i);
|
||||
dataType += arg.toString();
|
||||
}
|
||||
dataType += ")";
|
||||
}
|
||||
|
||||
if (dataType_len < dataType.length()) {
|
||||
dataType_len = dataType.length();
|
||||
}
|
||||
dataTypes.add(dataType);
|
||||
|
||||
if (column.getDefaultExpr() == null) {
|
||||
defaultValues.add(null);
|
||||
} else {
|
||||
String defaultVal = SQLUtils.toMySqlString(column.getDefaultExpr());
|
||||
if (defaultVal.length() > 2 && defaultVal.charAt(0) == '\'' && defaultVal.charAt(defaultVal.length() - 1) == '\'') {
|
||||
defaultVal = defaultVal.substring(1, defaultVal.length() - 1);
|
||||
}
|
||||
defaultValues.add(defaultVal);
|
||||
|
||||
if (defaultVal_len < defaultVal.length()) {
|
||||
defaultVal_len = defaultVal.length();
|
||||
}
|
||||
}
|
||||
|
||||
if (column.isAutoIncrement()) {
|
||||
extra_len = "auto_increment".length();
|
||||
} else if (column.getOnUpdate() != null) {
|
||||
extra_len = "on update CURRENT_TIMESTAMP".length();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
print("+-");
|
||||
print('-', name_len);
|
||||
print("-+-");
|
||||
print('-', dataType_len);
|
||||
print("-+------+-----+-");
|
||||
print('-', defaultVal_len);
|
||||
print("-+-");
|
||||
print('-', extra_len);
|
||||
print("-+\n");
|
||||
|
||||
print("| ");
|
||||
print("Field", name_len, ' ');
|
||||
print(" | ");
|
||||
print("Type", dataType_len, ' ');
|
||||
print(" | Null | Key | ");
|
||||
print("Default", defaultVal_len, ' ');
|
||||
print(" | ");
|
||||
print("Extra", extra_len, ' ');
|
||||
print(" |\n");
|
||||
|
||||
print("+-");
|
||||
print('-', name_len);
|
||||
print("-+-");
|
||||
print('-', dataType_len);
|
||||
print("-+------+-----+-");
|
||||
print('-', defaultVal_len);
|
||||
print("-+-");
|
||||
print('-', extra_len);
|
||||
print("-+\n");
|
||||
|
||||
for (int i = 0; i < columns.size(); i++) {
|
||||
SQLColumnDefinition column = columns.get(i);
|
||||
String name = SQLUtils.normalize(column.getName().getSimpleName());
|
||||
|
||||
print("| ");
|
||||
print(name, name_len, ' ');
|
||||
print(" | ");
|
||||
|
||||
print(dataTypes.get(i), dataType_len, ' ');
|
||||
print(" | ");
|
||||
|
||||
if (column.containsNotNullConstaint()) {
|
||||
print("YES");
|
||||
} else {
|
||||
print("NO ");
|
||||
}
|
||||
print(" | ");
|
||||
|
||||
MySqlUnique unique = null;
|
||||
if (x.isPrimaryColumn(name)) {
|
||||
print("PRI");
|
||||
} else if (x.isUNI(name)) {
|
||||
print("UNI");
|
||||
} else if (x.isMUL(name)) {
|
||||
print("MUL");
|
||||
} else {
|
||||
print(" ");
|
||||
}
|
||||
print(" | ");
|
||||
|
||||
String defaultVal = defaultValues.get(i);
|
||||
if (defaultVal == null) {
|
||||
print("NULL", defaultVal_len, ' ');
|
||||
} else {
|
||||
print(defaultVal, defaultVal_len, ' ');
|
||||
}
|
||||
print(" | ");
|
||||
|
||||
if (column.isAutoIncrement()) {
|
||||
print("auto_increment", extra_len, ' ');
|
||||
} else if (column.getOnUpdate() != null) {
|
||||
print("on update CURRENT_TIMESTAMP", extra_len, ' ');
|
||||
} else {
|
||||
print(' ', extra_len);
|
||||
}
|
||||
print(" |");
|
||||
print("\n");
|
||||
}
|
||||
print("+-");
|
||||
print('-', name_len);
|
||||
print("-+-");
|
||||
print('-', dataType_len);
|
||||
print("-+------+-----+-");
|
||||
print('-', defaultVal_len);
|
||||
print("-+-");
|
||||
print('-', extra_len);
|
||||
print("-+\n");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void print(char ch, int count) {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
print(ch);
|
||||
}
|
||||
}
|
||||
|
||||
void print(String text, int columnSize, char ch) {
|
||||
print(text);
|
||||
print(' ', columnSize - text.length());
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.visitor.transform;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.expr.*;
|
||||
import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 26/07/2017.
|
||||
*/
|
||||
public class NameResolveVisitor extends OracleASTVisitorAdapter {
|
||||
public boolean visit(SQLIdentifierExpr x) {
|
||||
SQLObject parent = x.getParent();
|
||||
String name = x.getName();
|
||||
|
||||
if ("ROWNUM".equalsIgnoreCase(name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (parent instanceof SQLPropertyExpr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (; parent != null; parent = parent.getParent()) {
|
||||
if (parent instanceof SQLTableSource) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (parent instanceof SQLSelectQueryBlock) {
|
||||
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) parent;
|
||||
|
||||
if (queryBlock.getInto() != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (queryBlock.getParent() instanceof SQLSelect) {
|
||||
SQLObject pp = queryBlock.getParent().getParent();
|
||||
if (pp instanceof SQLInSubQueryExpr || pp instanceof SQLExistsExpr) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
SQLTableSource from = queryBlock.getFrom();
|
||||
if (from instanceof SQLExprTableSource || from instanceof SQLSubqueryTableSource) {
|
||||
String alias = from.getAlias();
|
||||
if (alias != null) {
|
||||
SQLUtils.replaceInParent(x, new SQLPropertyExpr(alias, name));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.visitor.transform;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLOrderBy;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 27/07/2017.
|
||||
*/
|
||||
public class OrderByResolve extends OracleASTVisitorAdapter {
|
||||
public boolean visit(SQLSelect x) {
|
||||
SQLSelectQueryBlock queryBlock = x.getQueryBlock();
|
||||
if (queryBlock == null) {
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
if (x.getOrderBy() != null && queryBlock.isForUpdate() && queryBlock.getOrderBy() == null) {
|
||||
queryBlock.setOrderBy(x.getOrderBy());
|
||||
x.setOrderBy(null);
|
||||
}
|
||||
|
||||
SQLOrderBy orderBy = queryBlock.getOrderBy();
|
||||
if (orderBy == null) {
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
List<SQLSelectOrderByItem> notContainsOrderBy = new ArrayList<SQLSelectOrderByItem>();
|
||||
for (SQLSelectOrderByItem orderByItem : orderBy.getItems()) {
|
||||
SQLExpr orderByExpr = orderByItem.getExpr();
|
||||
if (queryBlock.selectIndexOf(orderByExpr) == -1) {
|
||||
notContainsOrderBy.add(orderByItem);
|
||||
}
|
||||
}
|
||||
|
||||
if (notContainsOrderBy.size() > 0) {
|
||||
for (SQLSelectOrderByItem orderByItem : notContainsOrderBy) {
|
||||
queryBlock.addSelectItem(orderByItem.getExpr());
|
||||
}
|
||||
|
||||
OracleSelectQueryBlock queryBlock1 = new OracleSelectQueryBlock();
|
||||
queryBlock1.setFrom(queryBlock, "x");
|
||||
x.setQuery(queryBlock1);
|
||||
}
|
||||
|
||||
return super.visit(x);
|
||||
}
|
||||
}
|
||||
@@ -134,6 +134,10 @@ public class PGExprParser extends SQLExprParser {
|
||||
|
||||
if (expr.getClass() == SQLIdentifierExpr.class) {
|
||||
String ident = ((SQLIdentifierExpr)expr).getName();
|
||||
|
||||
if (lexer.token() == Token.COMMA) {
|
||||
return super.primaryRest(expr);
|
||||
}
|
||||
|
||||
if ("TIMESTAMP".equalsIgnoreCase(ident)) {
|
||||
if (lexer.token() != Token.LITERAL_ALIAS //
|
||||
|
||||
Reference in New Issue
Block a user