mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed for oracle sql parser
This commit is contained in:
@@ -47,26 +47,48 @@ import com.alibaba.druid.stat.TableStat.Relationship;
|
||||
|
||||
public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
protected final HashMap<TableStat.Name, TableStat> tableStats = new LinkedHashMap<TableStat.Name, TableStat>();
|
||||
protected final Set<Column> columns = new LinkedHashSet<Column>();
|
||||
protected final Set<Condition> conditions = new LinkedHashSet<Condition>();
|
||||
protected final Set<Relationship> relationships = new LinkedHashSet<Relationship>();
|
||||
protected final Set<Column> orderByColumns = new LinkedHashSet<Column>();
|
||||
protected final Set<Column> groupByColumns = new LinkedHashSet<Column>();
|
||||
protected final HashMap<TableStat.Name, TableStat> tableStats = new LinkedHashMap<TableStat.Name, TableStat>();
|
||||
protected final Set<Column> columns = new LinkedHashSet<Column>();
|
||||
protected final Set<Condition> conditions = new LinkedHashSet<Condition>();
|
||||
protected final Set<Relationship> relationships = new LinkedHashSet<Relationship>();
|
||||
protected final Set<Column> orderByColumns = new LinkedHashSet<Column>();
|
||||
protected final Set<Column> groupByColumns = new LinkedHashSet<Column>();
|
||||
|
||||
protected final Map<String, SQLObject> subQueryMap = new LinkedHashMap<String, SQLObject>();
|
||||
protected final static ThreadLocal<Map<String, String>> aliasLocal = new ThreadLocal<Map<String, String>>();
|
||||
protected final static ThreadLocal<String> currentTableLocal = new ThreadLocal<String>();
|
||||
protected final Map<String, SQLObject> subQueryMap = new LinkedHashMap<String, SQLObject>();
|
||||
|
||||
protected Map<String, String> aliasLocal = new HashMap<String, String>();
|
||||
|
||||
protected final static ThreadLocal<String> currentTableLocal = new ThreadLocal<String>();
|
||||
|
||||
public final static String ATTR_TABLE = "_table_";
|
||||
public final static String ATTR_COLUMN = "_column_";
|
||||
public final static String ATTR_TABLE = "_table_";
|
||||
public final static String ATTR_COLUMN = "_column_";
|
||||
|
||||
private Mode mode;
|
||||
private Mode mode;
|
||||
|
||||
public void setAliasMap() {
|
||||
this.setAliasMap(new HashMap<String, String>());
|
||||
}
|
||||
|
||||
public void clearAliasMap() {
|
||||
this.aliasLocal = null;
|
||||
}
|
||||
|
||||
public void setAliasMap(Map<String, String> aliasMap) {
|
||||
this.aliasLocal = aliasMap;
|
||||
}
|
||||
|
||||
public Map<String, String> getAliasMap() {
|
||||
return aliasLocal;
|
||||
}
|
||||
|
||||
public void setCurrentTable(String table) {
|
||||
currentTableLocal.set(table);
|
||||
}
|
||||
|
||||
protected Mode getMode() {
|
||||
return mode;
|
||||
}
|
||||
|
||||
|
||||
protected void setModeOrigin(SQLObject x) {
|
||||
Mode originalMode = (Mode) x.getAttribute("_original_use_mode");
|
||||
mode = originalMode;
|
||||
@@ -222,7 +244,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
protected Column getColumn(SQLExpr expr) {
|
||||
Map<String, String> aliasMap = aliasLocal.get();
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
if (aliasMap == null) {
|
||||
return null;
|
||||
}
|
||||
@@ -279,13 +301,13 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
public boolean visit(SQLTruncateStatement x) {
|
||||
setMode(x, Mode.Insert);
|
||||
|
||||
aliasLocal.set(new HashMap<String, String>());
|
||||
setAliasMap();
|
||||
|
||||
String originalTable = currentTableLocal.get();
|
||||
|
||||
for (SQLName name : x.getTableNames()) {
|
||||
String ident = name.toString();
|
||||
currentTableLocal.set(ident);
|
||||
setCurrentTable(ident);
|
||||
x.putAttribute("_old_local_", originalTable);
|
||||
|
||||
TableStat stat = tableStats.get(ident);
|
||||
@@ -295,7 +317,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
stat.incrementInsertCount();
|
||||
|
||||
Map<String, String> aliasMap = aliasLocal.get();
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
if (aliasMap != null) {
|
||||
aliasMap.put(ident, ident);
|
||||
}
|
||||
@@ -308,13 +330,13 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
public boolean visit(SQLDropTableStatement x) {
|
||||
setMode(x, Mode.Insert);
|
||||
|
||||
aliasLocal.set(new HashMap<String, String>());
|
||||
setAliasMap();
|
||||
|
||||
String originalTable = currentTableLocal.get();
|
||||
|
||||
for (SQLName name : x.getTableNames()) {
|
||||
String ident = name.toString();
|
||||
currentTableLocal.set(ident);
|
||||
setCurrentTable(ident);
|
||||
x.putAttribute("_old_local_", originalTable);
|
||||
|
||||
TableStat stat = tableStats.get(ident);
|
||||
@@ -324,7 +346,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
stat.incrementDropCount();
|
||||
|
||||
Map<String, String> aliasMap = aliasLocal.get();
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
if (aliasMap != null) {
|
||||
aliasMap.put(ident, ident);
|
||||
}
|
||||
@@ -337,13 +359,13 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
public boolean visit(SQLInsertStatement x) {
|
||||
setMode(x, Mode.Insert);
|
||||
|
||||
aliasLocal.set(new HashMap<String, String>());
|
||||
setAliasMap();
|
||||
|
||||
String originalTable = currentTableLocal.get();
|
||||
|
||||
if (x.getTableName() instanceof SQLName) {
|
||||
String ident = ((SQLName) x.getTableName()).toString();
|
||||
currentTableLocal.set(ident);
|
||||
setCurrentTable(ident);
|
||||
x.putAttribute("_old_local_", originalTable);
|
||||
|
||||
TableStat stat = tableStats.get(ident);
|
||||
@@ -353,7 +375,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
stat.incrementInsertCount();
|
||||
|
||||
Map<String, String> aliasMap = aliasLocal.get();
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
if (aliasMap != null) {
|
||||
if (x.getAlias() != null) {
|
||||
aliasMap.put(x.getAlias(), ident);
|
||||
@@ -382,7 +404,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
public boolean visit(SQLSelectQueryBlock x) {
|
||||
setMode(x, Mode.Select);
|
||||
|
||||
|
||||
if (x.getFrom() instanceof SQLSubqueryTableSource) {
|
||||
x.getFrom().accept(this);
|
||||
return false;
|
||||
@@ -395,7 +417,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
if (tableSource.getExpr() instanceof SQLName) {
|
||||
String ident = tableSource.getExpr().toString();
|
||||
|
||||
currentTableLocal.set(ident);
|
||||
setCurrentTable(ident);
|
||||
x.putAttribute(ATTR_TABLE, ident);
|
||||
if (x.getParent() instanceof SQLSelect) {
|
||||
x.getParent().putAttribute(ATTR_TABLE, ident);
|
||||
@@ -407,7 +429,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
if (x.getFrom() != null) {
|
||||
x.getFrom().accept(this); // 提前执行,获得aliasMap
|
||||
}
|
||||
|
||||
|
||||
if (x.getWhere() != null) {
|
||||
x.getWhere().setParent(x);
|
||||
}
|
||||
@@ -418,7 +440,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
public void endVisit(SQLSelectQueryBlock x) {
|
||||
String originalTable = (String) x.getAttribute("_old_local_");
|
||||
x.putAttribute("table", currentTableLocal.get());
|
||||
currentTableLocal.set(originalTable);
|
||||
setCurrentTable(originalTable);
|
||||
|
||||
setModeOrigin(x);
|
||||
}
|
||||
@@ -447,7 +469,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
protected String aliasWrap(String name) {
|
||||
Map<String, String> aliasMap = aliasLocal.get();
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
if (aliasMap.containsKey(name)) {
|
||||
return aliasMap.get(name);
|
||||
}
|
||||
@@ -542,12 +564,12 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectStatement x) {
|
||||
aliasLocal.set(new HashMap<String, String>());
|
||||
setAliasMap();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void endVisit(SQLSelectStatement x) {
|
||||
aliasLocal.set(null);
|
||||
clearAliasMap();
|
||||
}
|
||||
|
||||
public boolean visit(SQLSubqueryTableSource x) {
|
||||
@@ -555,7 +577,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
SQLSelectQuery query = x.getSelect().getQuery();
|
||||
|
||||
Map<String, String> aliasMap = aliasLocal.get();
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
if (aliasMap != null) {
|
||||
if (x.getAlias() != null) {
|
||||
aliasMap.put(x.getAlias(), null);
|
||||
@@ -577,7 +599,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
Map<String, String> aliasMap = aliasLocal.get();
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
@@ -634,12 +656,12 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
String originalTable = currentTableLocal.get();
|
||||
|
||||
currentTableLocal.set((String) x.getQuery().getAttribute("table"));
|
||||
setCurrentTable((String) x.getQuery().getAttribute("table"));
|
||||
x.putAttribute("_old_local_", originalTable);
|
||||
|
||||
accept(x.getOrderBy());
|
||||
|
||||
currentTableLocal.set(originalTable);
|
||||
setCurrentTable(originalTable);
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -655,10 +677,10 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
public boolean visit(SQLUpdateStatement x) {
|
||||
aliasLocal.set(new HashMap<String, String>());
|
||||
setAliasMap();
|
||||
|
||||
String ident = x.getTableName().toString();
|
||||
currentTableLocal.set(ident);
|
||||
setCurrentTable(ident);
|
||||
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
@@ -667,7 +689,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
stat.incrementUpdateCount();
|
||||
|
||||
Map<String, String> aliasMap = aliasLocal.get();
|
||||
Map<String, String> aliasMap = getAliasMap();
|
||||
aliasMap.put(ident, ident);
|
||||
|
||||
accept(x.getItems());
|
||||
@@ -677,12 +699,12 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
public boolean visit(SQLDeleteStatement x) {
|
||||
aliasLocal.set(new HashMap<String, String>());
|
||||
setAliasMap();
|
||||
|
||||
setMode(x, Mode.Delete);
|
||||
|
||||
String ident = x.getTableName().toString();
|
||||
currentTableLocal.set(ident);
|
||||
setCurrentTable(ident);
|
||||
|
||||
TableStat stat = tableStats.get(ident);
|
||||
if (stat == null) {
|
||||
@@ -717,11 +739,11 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
public void endVisit(SQLDeleteStatement x) {
|
||||
aliasLocal.set(null);
|
||||
clearAliasMap();
|
||||
}
|
||||
|
||||
public void endVisit(SQLUpdateStatement x) {
|
||||
aliasLocal.set(null);
|
||||
clearAliasMap();
|
||||
}
|
||||
|
||||
public boolean visit(SQLCreateTableStatement x) {
|
||||
@@ -757,15 +779,15 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCallStatement x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLCommentStatement x) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user