improved sql parser.

This commit is contained in:
wenshao
2017-06-08 15:16:22 +08:00
parent 41a56b3c92
commit 7c4a43d2c5
29 changed files with 375 additions and 23 deletions
@@ -2134,6 +2134,30 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
x.getInitial().accept(this);
}
if (x.getNext() != null) {
println();
print0(ucase ? "NEXT " : "next ");
x.getNext().accept(this);
}
if (x.getMinExtents() != null) {
println();
print0(ucase ? "MINEXTENTS " : "minextents ");
x.getMinExtents().accept(this);
}
if (x.getMaxExtents() != null) {
println();
print0(ucase ? "MAXEXTENTS " : "maxextents ");
x.getMaxExtents().accept(this);
}
if (x.getPctIncrease() != null) {
println();
print0(ucase ? "PCTINCREASE " : "pctincrease ");
x.getPctIncrease().accept(this);
}
if (x.getMaxSize() != null) {
println();
print0(ucase ? "MAXSIZE " : "maxsize ");
@@ -30,7 +30,6 @@ import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentSkipListMap;
/**
@@ -128,10 +127,21 @@ public class SchemaRepository {
public SchemaObject findTable(SQLTableSource tableSource, String alias) {
if (tableSource instanceof SQLExprTableSource) {
if (alias.equalsIgnoreCase(tableSource.computeAlias())) {
SQLExpr expr = ((SQLExprTableSource) tableSource).getExpr();
SQLExprTableSource exprTableSource = (SQLExprTableSource) tableSource;
SchemaObject tableObject = exprTableSource.getSchemaObject();
if (tableObject != null) {
return tableObject;
}
SQLExpr expr = exprTableSource.getExpr();
if (expr instanceof SQLIdentifierExpr) {
String tableName = ((SQLIdentifierExpr) expr).getName();
SchemaObject tableObject = findTable(tableName);
tableObject = findTable(tableName);
if (tableObject != null) {
exprTableSource.setSchemaObject(tableObject);
}
return tableObject;
}
}
@@ -220,20 +230,31 @@ public class SchemaRepository {
public Map<String, SchemaObject> getTables(SQLTableSource x) {
Map<String, SchemaObject> tables = new LinkedHashMap<String, SchemaObject>();
computeTable(x, tables);
computeTables(x, tables);
return tables;
}
private void computeTable(SQLTableSource x, Map<String, SchemaObject> tables) {
private void computeTables(SQLTableSource x, Map<String, SchemaObject> tables) {
if (x == null) {
return;
}
if (x instanceof SQLExprTableSource) {
SQLExpr expr = ((SQLExprTableSource) x).getExpr();
SQLExprTableSource exprTableSource = (SQLExprTableSource) x;
SQLExpr expr = exprTableSource.getExpr();
if (expr instanceof SQLIdentifierExpr) {
String tableName = ((SQLIdentifierExpr) expr).getName();
SchemaObject table = findTable(tableName);
SchemaObject table = exprTableSource.getSchemaObject();
if (table == null) {
table = findTable(tableName);
if (table != null) {
exprTableSource.setSchemaObject(table);
}
}
if (table != null) {
tables.put(tableName, table);
@@ -249,8 +270,8 @@ public class SchemaRepository {
if (x instanceof SQLJoinTableSource) {
SQLJoinTableSource join = (SQLJoinTableSource) x;
computeTable(join.getLeft(), tables);
computeTable(join.getRight(), tables);
computeTables(join.getLeft(), tables);
computeTables(join.getRight(), tables);
}
}
@@ -184,6 +184,8 @@ public class TableStat {
private String name;
private int hash;
public Name(String name){
this.name = name;
}
@@ -193,7 +195,11 @@ public class TableStat {
}
public int hashCode() {
return StringUtils.lowerHashCode(name);
int h = hash;
if (h == 0 && name != null && name.length() > 0) {
hash = h = StringUtils.lowerHashCode(name);
}
return h;
}
public boolean equals(Object o) {