mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -29,4 +29,6 @@ public interface SQLDataType extends SQLObject {
|
||||
void setWithTimeZone(Boolean value);
|
||||
|
||||
boolean isWithLocalTimeZone();
|
||||
|
||||
SQLDataType clone();
|
||||
}
|
||||
|
||||
@@ -101,4 +101,23 @@ public class SQLDataTypeImpl extends SQLObjectImpl implements SQLDataType {
|
||||
public void setWithLocalTimeZone(boolean withLocalTimeZone) {
|
||||
this.withLocalTimeZone = withLocalTimeZone;
|
||||
}
|
||||
|
||||
public SQLDataTypeImpl clone() {
|
||||
SQLDataTypeImpl x = new SQLDataTypeImpl();
|
||||
|
||||
cloneTo(x);
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
public void cloneTo(SQLDataTypeImpl x) {
|
||||
x.name = name;
|
||||
|
||||
for (SQLExpr arg : arguments) {
|
||||
x.addArgument(arg.clone());
|
||||
}
|
||||
|
||||
x.withTimeZone = withTimeZone;
|
||||
x.withLocalTimeZone = withLocalTimeZone;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +101,20 @@ public class SQLCharacterDataType extends SQLDataTypeImpl {
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
|
||||
public SQLCharacterDataType clone() {
|
||||
SQLCharacterDataType x = new SQLCharacterDataType(name);
|
||||
|
||||
super.cloneTo(x);
|
||||
|
||||
x.charSetName = charSetName;
|
||||
x.collate = collate;
|
||||
x.charType = charType;
|
||||
x.hasBinary = hasBinary;
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return SQLUtils.toSQLString(this);
|
||||
|
||||
@@ -50,4 +50,16 @@ public class SQLColumnCheck extends SQLConstraintImpl implements SQLColumnConstr
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLColumnCheck clone() {
|
||||
SQLColumnCheck x = new SQLColumnCheck();
|
||||
|
||||
super.cloneTo(x);
|
||||
|
||||
if (expr != null) {
|
||||
x.setExpr(expr.clone());
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,5 +16,5 @@
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
public interface SQLColumnConstraint extends SQLConstraint {
|
||||
|
||||
SQLColumnConstraint clone();
|
||||
}
|
||||
|
||||
@@ -264,6 +264,13 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public Identity clone () {
|
||||
Identity x = new Identity();
|
||||
x.seed = seed;
|
||||
x.increment = increment;
|
||||
x.notForReplication = notForReplication;
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
public String computeAlias() {
|
||||
@@ -277,4 +284,61 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
|
||||
|
||||
return SQLUtils.normalize(alias);
|
||||
}
|
||||
|
||||
public SQLColumnDefinition clone() {
|
||||
SQLColumnDefinition x = new SQLColumnDefinition();
|
||||
|
||||
if(name != null) {
|
||||
x.setName(name.clone());
|
||||
}
|
||||
|
||||
if (dataType != null) {
|
||||
x.setDataType(dataType.clone());
|
||||
}
|
||||
|
||||
if (defaultExpr != null) {
|
||||
x.setDefaultExpr(defaultExpr.clone());
|
||||
}
|
||||
|
||||
for (SQLColumnConstraint item : constraints) {
|
||||
SQLColumnConstraint itemCloned = item.clone();
|
||||
itemCloned.setParent(x);
|
||||
x.constraints.add(itemCloned);
|
||||
}
|
||||
|
||||
if (comment != null) {
|
||||
x.setComment(comment.clone());
|
||||
}
|
||||
|
||||
x.enable = enable;
|
||||
x.validate = validate;
|
||||
x.rely = rely;
|
||||
|
||||
x.autoIncrement = autoIncrement;
|
||||
|
||||
if (onUpdate != null) {
|
||||
x.setOnUpdate(onUpdate.clone());
|
||||
}
|
||||
|
||||
if (storage != null) {
|
||||
x.setStorage(storage.clone());
|
||||
}
|
||||
|
||||
if (charsetExpr != null) {
|
||||
x.setCharsetExpr(charsetExpr.clone());
|
||||
}
|
||||
|
||||
if (asExpr != null) {
|
||||
x.setAsExpr(asExpr.clone());
|
||||
}
|
||||
|
||||
x.sorted = sorted;
|
||||
x.virtual = virtual;
|
||||
|
||||
if (identity != null) {
|
||||
x.setIdentity(identity.clone());
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,4 +27,11 @@ public class SQLColumnPrimaryKey extends SQLConstraintImpl implements SQLColumnC
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLColumnPrimaryKey clone() {
|
||||
SQLColumnPrimaryKey x = new SQLColumnPrimaryKey();
|
||||
|
||||
super.cloneTo(x);
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,4 +50,22 @@ public class SQLColumnReference extends SQLConstraintImpl implements SQLColumnCo
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
public SQLColumnReference clone() {
|
||||
SQLColumnReference x = new SQLColumnReference();
|
||||
|
||||
super.cloneTo(x);
|
||||
|
||||
if (table != null) {
|
||||
x.setTable(table.clone());
|
||||
}
|
||||
|
||||
for (SQLName column : columns) {
|
||||
SQLName columnCloned = column.clone();
|
||||
columnCloned.setParent(x);
|
||||
x.columns.add(columnCloned);
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,4 +27,9 @@ public class SQLColumnUniqueKey extends SQLConstraintImpl implements SQLColumnCo
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLColumnUniqueKey clone() {
|
||||
SQLColumnUniqueKey x = new SQLColumnUniqueKey();
|
||||
super.cloneTo(x);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,15 @@ public abstract class SQLConstraintImpl extends SQLObjectImpl implements SQLCons
|
||||
|
||||
}
|
||||
|
||||
public void cloneTo(SQLConstraintImpl x) {
|
||||
if (name != null) {
|
||||
x.setName(name.clone());
|
||||
}
|
||||
|
||||
x.enable = enable;
|
||||
x.validate = validate;
|
||||
x.rely = rely;
|
||||
}
|
||||
|
||||
public List<SQLCommentHint> getHints() {
|
||||
return hints;
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.alibaba.druid.sql.ast.*;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
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.util.ListDG;
|
||||
import com.alibaba.druid.util.lang.Consumer;
|
||||
|
||||
@@ -283,8 +284,32 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
return false;
|
||||
}
|
||||
|
||||
public SQLAlterTableStatement foreignKeyToAlterTable() {
|
||||
SQLAlterTableStatement stmt = new SQLAlterTableStatement();
|
||||
for (int i = this.tableElementList.size() - 1; i >= 0; --i) {
|
||||
SQLTableElement element = this.tableElementList.get(i);
|
||||
if (element instanceof SQLForeignKeyConstraint) {
|
||||
SQLForeignKeyConstraint fk = (SQLForeignKeyConstraint) element;
|
||||
this.tableElementList.remove(i);
|
||||
stmt.addItem(new SQLAlterTableAddConstraint(fk));
|
||||
}
|
||||
}
|
||||
|
||||
if (stmt.getItems().size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
stmt.setDbType(getDbType());
|
||||
stmt.setTableSource(this.tableSource.clone());
|
||||
|
||||
Collections.reverse(stmt.getItems());
|
||||
|
||||
return stmt;
|
||||
}
|
||||
|
||||
public static void sort(List<SQLStatement> stmtList) {
|
||||
Map<String, SQLCreateTableStatement> tables = new HashMap<String, SQLCreateTableStatement>();
|
||||
Map<String, List<SQLCreateTableStatement>> referencedTables = new HashMap<String, List<SQLCreateTableStatement>>();
|
||||
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
if (stmt instanceof SQLCreateTableStatement) {
|
||||
@@ -295,7 +320,7 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
}
|
||||
}
|
||||
|
||||
List<Object[]> edges = new ArrayList<Object[]>();
|
||||
List<ListDG.Edge> edges = new ArrayList<ListDG.Edge>();
|
||||
|
||||
for (SQLCreateTableStatement stmt : tables.values()) {
|
||||
for (SQLTableElement element : stmt.getTableElementList()) {
|
||||
@@ -306,9 +331,15 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
|
||||
SQLCreateTableStatement refTable = tables.get(refTableName);
|
||||
if (refTable != null) {
|
||||
Object[] edge = new Object[] {stmt, refTable};
|
||||
edges.add(edge);
|
||||
edges.add(new ListDG.Edge(stmt, refTable));
|
||||
}
|
||||
|
||||
List<SQLCreateTableStatement> referencedList = referencedTables.get(refTableName);
|
||||
if (referencedList == null) {
|
||||
referencedList = new ArrayList<SQLCreateTableStatement>();
|
||||
referencedTables.put(refTableName, referencedList);
|
||||
}
|
||||
referencedList.add(stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -316,9 +347,71 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
ListDG dg = new ListDG(stmtList, edges);
|
||||
|
||||
SQLStatement[] tops = new SQLStatement[stmtList.size()];
|
||||
dg.topologicalSort(tops);
|
||||
for (int i = 0, size = stmtList.size(); i < size; ++i) {
|
||||
stmtList.set(i, tops[size - i - 1]);
|
||||
if (dg.topologicalSort(tops)) {
|
||||
for (int i = 0, size = stmtList.size(); i < size; ++i) {
|
||||
stmtList.set(i, tops[size - i - 1]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
List<SQLAlterTableStatement> alterList = new ArrayList<SQLAlterTableStatement>();
|
||||
|
||||
for (int i = edges.size() - 1; i >= 0; --i) {
|
||||
ListDG.Edge edge = edges.get(i);
|
||||
SQLCreateTableStatement from = (SQLCreateTableStatement) edge.from;
|
||||
String fromTableName = from.getName().getSimpleName();
|
||||
fromTableName = SQLUtils.normalize(fromTableName).toLowerCase();
|
||||
if (referencedTables.containsKey(fromTableName)) {
|
||||
edges.remove(i);
|
||||
|
||||
Arrays.fill(tops, null);
|
||||
tops = new SQLStatement[stmtList.size()];
|
||||
|
||||
dg = new ListDG(stmtList, edges);
|
||||
if (dg.topologicalSort(tops)) {
|
||||
for (int j = 0, size = stmtList.size(); j < size; ++j) {
|
||||
SQLStatement stmt = tops[size - j - 1];
|
||||
stmtList.set(j, stmt);
|
||||
}
|
||||
|
||||
SQLAlterTableStatement alter = from.foreignKeyToAlterTable();
|
||||
alterList.add(alter);
|
||||
|
||||
stmtList.add(alter);
|
||||
return;
|
||||
}
|
||||
edges.add(i, edge);
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = edges.size() - 1; i >= 0; --i) {
|
||||
ListDG.Edge edge = edges.get(i);
|
||||
SQLCreateTableStatement from = (SQLCreateTableStatement) edge.from;
|
||||
String fromTableName = from.getName().getSimpleName();
|
||||
fromTableName = SQLUtils.normalize(fromTableName).toLowerCase();
|
||||
if (referencedTables.containsKey(fromTableName)) {
|
||||
SQLAlterTableStatement alter = from.foreignKeyToAlterTable();
|
||||
|
||||
edges.remove(i);
|
||||
if (alter != null) {
|
||||
alterList.add(alter);
|
||||
}
|
||||
|
||||
Arrays.fill(tops, null);
|
||||
tops = new SQLStatement[stmtList.size()];
|
||||
|
||||
dg = new ListDG(stmtList, edges);
|
||||
if (dg.topologicalSort(tops)) {
|
||||
for (int j = 0, size = stmtList.size(); j < size; ++j) {
|
||||
SQLStatement stmt = tops[size - j - 1];
|
||||
stmtList.set(j, stmt);
|
||||
}
|
||||
|
||||
stmtList.addAll(alterList);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ public class SQLForeignKeyImpl extends SQLConstraintImpl implements SQLForeignKe
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public void cloneTo(SQLForeignKeyConstraint x) {
|
||||
public void cloneTo(SQLForeignKeyImpl x) {
|
||||
super.cloneTo(x);
|
||||
|
||||
if (referencedTableName != null) {
|
||||
@@ -99,4 +99,10 @@ public class SQLForeignKeyImpl extends SQLConstraintImpl implements SQLForeignKe
|
||||
x.getReferencedColumns().add(columnClone);
|
||||
}
|
||||
}
|
||||
|
||||
public SQLForeignKeyImpl clone() {
|
||||
SQLForeignKeyImpl x = new SQLForeignKeyImpl();
|
||||
cloneTo(x);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ import java.util.List;
|
||||
|
||||
public class SQLNotNullConstraint extends SQLConstraintImpl implements SQLColumnConstraint {
|
||||
|
||||
|
||||
|
||||
public SQLNotNullConstraint(){
|
||||
|
||||
}
|
||||
@@ -34,4 +32,9 @@ public class SQLNotNullConstraint extends SQLConstraintImpl implements SQLColumn
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLNotNullConstraint clone() {
|
||||
SQLNotNullConstraint x = new SQLNotNullConstraint();
|
||||
super.cloneTo(x);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,4 +27,10 @@ public class SQLNullConstraint extends SQLConstraintImpl implements SQLColumnCon
|
||||
visitor.visit(this);
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
public SQLNullConstraint clone() {
|
||||
SQLNullConstraint x = new SQLNullConstraint();
|
||||
super.cloneTo(x);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user