improved sql parser.

This commit is contained in:
wenshao
2017-07-20 01:50:02 +08:00
parent 80bf7f3d4c
commit 82fe08109f
23 changed files with 577 additions and 45 deletions
@@ -902,5 +902,17 @@ public class SQLUtils {
long hash = Utils.fnv_64_lower(tableName);
return Utils.hex_t(hash);
}
/**
* 重新排序建表语句
* @param sql
* @param dbType
* @return
*/
public static String sort(String sql, String dbType) {
List stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
SQLCreateTableStatement.sort(stmtList);
return SQLUtils.toSQLString(stmtList, dbType);
}
}
@@ -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;
}
}
@@ -58,4 +58,17 @@ public class OracleDataTypeIntervalDay extends SQLDataTypeImpl implements Oracle
return fractionalSeconds;
}
public OracleDataTypeIntervalDay clone() {
OracleDataTypeIntervalDay x = new OracleDataTypeIntervalDay();
super.cloneTo(x);
for (SQLExpr arg : fractionalSeconds) {
arg.setParent(x);
x.fractionalSeconds.add(arg);
}
return x;
}
}
@@ -16,6 +16,7 @@
package com.alibaba.druid.sql.dialect.oracle.ast;
import com.alibaba.druid.sql.ast.SQLDataTypeImpl;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
@@ -38,4 +39,11 @@ public class OracleDataTypeIntervalYear extends SQLDataTypeImpl implements Oracl
visitor.endVisit(this);
}
public OracleDataTypeIntervalYear clone() {
OracleDataTypeIntervalYear x = new OracleDataTypeIntervalYear();
super.cloneTo(x);
return x;
}
}
@@ -91,6 +91,10 @@ public class OracleCreateTableParser extends SQLCreateTableParser {
lexer.nextToken();
stmt.setParallel(false);
continue;
} else if (identifierEquals("PARALLEL")) {
lexer.nextToken();
stmt.setParallel(true);
continue;
} else if (lexer.token() == Token.CACHE) {
lexer.nextToken();
stmt.setCache(Boolean.TRUE);
@@ -2053,7 +2053,7 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
if (enable.booleanValue()) {
print0(ucase ? " ENABLE" : " enable");
} else {
print0(ucase ? " DIABLE" : " diable");
print0(ucase ? " DISABLE" : " disable");
}
}
@@ -26,6 +26,16 @@ package com.alibaba.druid.util;
import java.util.*;
public class ListDG {
public static class Edge {
public Object from;
public Object to;
public Edge(Object from, Object to) {
this.from = from;
this.to = to;
}
}
// 邻接表中表对应的链表的顶点
private class ENode {
int ivex; // 该边所指向的顶点的位置
@@ -47,7 +57,7 @@ public class ListDG {
* vexs -- 顶点数组
* edges -- 边数组
*/
public ListDG(List vexs, List<Object[]> edges) {
public ListDG(List vexs, List<Edge> edges) {
// 初始化"顶点数"和"边数"
int vlen = vexs.size();
@@ -67,11 +77,11 @@ public class ListDG {
// 初始化"边"
for (int i = 0; i < elen; i++) {
// 读取边的起始顶点和结束顶点
Object c1 = edges.get(i)[0];
Object c2 = edges.get(i)[1];
Object c1 = edges.get(i).from;
Object c2 = edges.get(i).to;
// 读取边的起始顶点和结束顶点
int p1 = getPosition(edges.get(i)[0]);
int p2 = getPosition(edges.get(i)[1]);
int p1 = getPosition(edges.get(i).from);
int p2 = getPosition(edges.get(i).to);
// 初始化node1
ENode node1 = new ENode();
@@ -112,7 +122,6 @@ public class ListDG {
ENode node;
visited[i] = true;
System.out.printf("%c ", mVexs.get(i).data);
node = mVexs.get(i).firstEdge;
while (node != null) {
if (!visited[node.ivex])
@@ -131,12 +140,10 @@ public class ListDG {
for (int i = 0; i < mVexs.size(); i++)
visited[i] = false;
System.out.printf("== DFS: ");
for (int i = 0; i < mVexs.size(); i++) {
if (!visited[i])
DFS(i, visited);
}
System.out.printf("\n");
}
/*
@@ -151,7 +158,6 @@ public class ListDG {
for (int i = 0; i < mVexs.size(); i++)
visited[i] = false;
System.out.printf("== BFS: ");
for (int i = 0; i < mVexs.size(); i++) {
if (!visited[i]) {
visited[i] = true;
@@ -174,7 +180,6 @@ public class ListDG {
}
}
}
System.out.printf("\n");
}
/*
@@ -189,11 +194,10 @@ public class ListDG {
System.out.printf("%d(%c) ", node.ivex, mVexs.get(node.ivex).data);
node = node.nextEdge;
}
System.out.printf("\n");
}
}
public int topologicalSort() {
public boolean topologicalSort() {
return topologicalSort(new Object[mVexs.size()]);
}
@@ -201,11 +205,10 @@ public class ListDG {
* 拓扑排序
*
* 返回值:
* -1 -- 失败(由于内存不足等原因导致)
* 0 -- 成功排序,并输入结果
* 1 -- 失败(该有向图是有环的)
* true 成功排序,并输入结果
* false 失败(该有向图是有环的)
*/
public int topologicalSort(Object[] tops) {
public boolean topologicalSort(Object[] tops) {
int index = 0;
int num = mVexs.size();
int[] ins; // 入度数组
@@ -250,30 +253,22 @@ public class ListDG {
}
if(index != num) {
// System.out.printf("Graph has a cycle\n");
return 1;
return false;
}
// 打印拓扑排序结果
// System.out.printf("== TopSort: ");
// for(int i = 0; i < num; i ++) {
// System.out.printf("%c ", tops[i]);
// }
// System.out.printf("\n");
return 0;
return true;
}
public static void main(String[] args) {
Object[] vexs = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
Object[][] edges = new Object[][]{
{vexs[0], vexs[6]},
{vexs[1], vexs[0]},
{vexs[1], vexs[3]},
{vexs[2], vexs[5]},
{vexs[2], vexs[6]},
{vexs[3], vexs[4]},
{vexs[3], vexs[5]}};
Edge[] edges = new Edge[]{
new Edge(vexs[0], vexs[6]),
new Edge(vexs[1], vexs[0]),
new Edge(vexs[1], vexs[3]),
new Edge(vexs[2], vexs[5]),
new Edge(vexs[2], vexs[6]),
new Edge(vexs[3], vexs[4]),
new Edge(vexs[3], vexs[5])};
ListDG pG;
// 自定义"图"(输入矩阵队列)