improved sql parser.

This commit is contained in:
wenshao
2017-07-23 16:44:43 +08:00
parent 82fe08109f
commit 6829a23bce
41 changed files with 1183 additions and 514 deletions
@@ -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.SQLName;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
@@ -58,6 +59,20 @@ public class SQLCreateViewStatement extends SQLStatementImpl implements SQLDDLSt
super(dbType);
}
public String computeName() {
if (tableSource == null) {
return null;
}
SQLExpr expr = tableSource.getExpr();
if (expr instanceof SQLName) {
String name = ((SQLName) expr).getSimpleName();
return SQLUtils.normalize(name);
}
return null;
}
public boolean isOrReplace() {
return orReplace;
}
@@ -79,22 +79,22 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
this.left = left;
}
public void setLeft(String tableName) {
public void setLeft(String tableName, String alias) {
SQLExprTableSource tableSource;
if (tableName == null || tableName.length() == 0) {
tableSource = null;
} else {
tableSource = new SQLExprTableSource(new SQLIdentifierExpr(tableName));
tableSource = new SQLExprTableSource(new SQLIdentifierExpr(tableName), alias);
}
this.setLeft(tableSource);
}
public void setRight(String tableName) {
public void setRight(String tableName, String alias) {
SQLExprTableSource tableSource;
if (tableName == null || tableName.length() == 0) {
tableSource = null;
} else {
tableSource = new SQLExprTableSource(new SQLIdentifierExpr(tableName));
tableSource = new SQLExprTableSource(new SQLIdentifierExpr(tableName), alias);
}
this.setRight(tableSource);
}
@@ -228,4 +228,41 @@ public class SQLJoinTableSource extends SQLTableSourceImpl implements SQLReplace
cloneTo(x);
return x;
}
public void reverse() {
SQLTableSource temp = left;
left = right;
right = temp;
if (left instanceof SQLJoinTableSource) {
((SQLJoinTableSource) left).reverse();
}
if (right instanceof SQLJoinTableSource) {
((SQLJoinTableSource) right).reverse();
}
}
/**
* a inner_join (b inner_join c) -> a inner_join b innre_join c
*/
public void rearrangement() {
if (right instanceof SQLJoinTableSource) {
SQLJoinTableSource rightJoin = (SQLJoinTableSource) right;
SQLTableSource a = left;
SQLTableSource b = rightJoin.getLeft();
SQLTableSource c = rightJoin.getRight();
SQLExpr on_ab = condition;
SQLExpr on_bc = rightJoin.condition;
setLeft(rightJoin);
rightJoin.setLeft(a);
rightJoin.setRight(b);
rightJoin.setCondition(on_ab);
setRight(c);
setCondition(on_bc);
}
}
}
@@ -162,12 +162,12 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
this.from = from;
}
public void setFrom(String tableName) {
public void setFrom(String tableName, String alias) {
SQLExprTableSource from;
if (tableName == null || tableName.length() == 0) {
from = null;
} else {
from = new SQLExprTableSource(new SQLIdentifierExpr(tableName));
from = new SQLExprTableSource(new SQLIdentifierExpr(tableName), alias);
}
this.setFrom(from);
}