bug fixed for sql parser

This commit is contained in:
温少
2012-03-06 11:21:47 +08:00
5 changed files with 82 additions and 54 deletions
@@ -59,7 +59,7 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
if (x.getTableNames().size() == 1) {
String ident = ((SQLIdentifierExpr) x.getTableNames().get(0)).getName();
setCurrentTable(ident);
setCurrentTable(x, ident);
}
for (SQLName tableName : x.getTableNames()) {
@@ -96,12 +96,9 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
setAliasMap();
String originalTable = currentTableLocal.get();
if (x.getTableName() instanceof SQLIdentifierExpr) {
String ident = ((SQLIdentifierExpr) x.getTableName()).getName();
setCurrentTable(ident);
x.putAttribute("_old_local_", originalTable);
setCurrentTable(x, ident);
TableStat stat = tableStats.get(ident);
if (stat == null) {
@@ -288,6 +288,10 @@ public class OracleStatementParser extends SQLStatementParser {
if (lexer.token() == Token.COMMIT) {
lexer.nextToken();
if (identifierEquals("WORK")) {
lexer.nextToken();
}
OracleCommitStatement stmt = new OracleCommitStatement();
statementList.add(stmt);
continue;
@@ -231,19 +231,15 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
accept(x.getFactoring());
accept(x.getQuery());
String originalTable = currentTableLocal.get();
setCurrentTable((String) x.getQuery().getAttribute("table"));
x.putAttribute("_old_local_", originalTable);
setCurrentTable(x, (String) x.getQuery().getAttribute("table"));
accept(x.getOrderBy());
setCurrentTable(originalTable);
return false;
}
public void endVisit(SQLSelect x) {
restoreCurrentTable(x);
}
public boolean visit(OracleUpdateStatement x) {
@@ -941,12 +937,12 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
setMode(x, Mode.Merge);
String originalTable = currentTableLocal.get();
String originalTable = getCurrentTable();
x.getUsing().accept(this);
String ident = x.getInto().toString();
setCurrentTable(ident);
setCurrentTable(x, ident);
x.putAttribute("_old_local_", originalTable);
TableStat stat = tableStats.get(ident);
@@ -1034,12 +1030,10 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
@Override
public boolean visit(InsertIntoClause x) {
String originalTable = currentTableLocal.get();
if (x.getTableName() instanceof SQLName) {
String ident = ((SQLName) x.getTableName()).toString();
setCurrentTable(ident);
x.putAttribute("_old_local_", originalTable);
setCurrentTable(x, ident);
TableStat stat = tableStats.get(ident);
if (stat == null) {
@@ -1111,7 +1105,6 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
@Override
public boolean visit(ConditionalInsertClauseItem x) {
String originalTable = currentTableLocal.get();
SQLObject parent = x.getParent();
if (parent instanceof ConditionalInsertClause) {
parent = parent.getParent();
@@ -1120,12 +1113,12 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
SQLSelect subQuery = ((OracleMultiInsertStatement) parent).getSubQuery();
if (subQuery != null) {
String table = (String) subQuery.getAttribute("_table_");
setCurrentTable(table);
setCurrentTable(x, table);
}
}
x.getWhen().accept(this);
x.getThen().accept(this);
setCurrentTable(originalTable);
restoreCurrentTable(x);
return false;
}
@@ -1136,6 +1129,12 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
@Override
public boolean visit(OracleBlockStatement x) {
for (OracleParameter param : x.getParameters()) {
param.setParent(x);
SQLExpr name = param.getName();
this.variants.put(name.toString(), name);
}
return true;
}
@@ -1388,7 +1387,9 @@ public class OracleSchemaStatVisitor extends SchemaStatVisitor implements Oracle
public boolean visit(OracleForStatement x) {
x.getRange().setParent(x);
x.getIndex().accept(this);
SQLName index = x.getIndex();
this.getVariants().put(index.toString(), x);
x.getRange().accept(this);
accept(x.getStatements());
@@ -193,7 +193,7 @@ public class PGSchemaStatVisitor extends SchemaStatVisitor implements PGASTVisit
x.putAttribute("_original_use_mode", getMode());
setMode(x, Mode.Insert);
String originalTable = currentTableLocal.get();
String originalTable = getCurrentTable();
if (x.getTableName() instanceof SQLName) {
String ident = ((SQLName) x.getTableName()).toString();
@@ -47,42 +47,62 @@ 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 Map<String, String> aliasLocal = new HashMap<String, String>();
protected final static ThreadLocal<String> currentTableLocal = new ThreadLocal<String>();
protected final Map<String, SQLObject> subQueryMap = new LinkedHashMap<String, SQLObject>();
public final static String ATTR_TABLE = "_table_";
public final static String ATTR_COLUMN = "_column_";
protected final Map<String, SQLObject> variants = new LinkedHashMap<String, SQLObject>();
protected Map<String, String> aliasMap = new HashMap<String, String>();
protected String currentTable;
public final static String ATTR_TABLE = "_table_";
public final static String ATTR_COLUMN = "_column_";
private Mode mode;
public Map<String, SQLObject> getVariants() {
return variants;
}
public void setAliasMap() {
this.setAliasMap(new HashMap<String, String>());
}
public void clearAliasMap() {
this.aliasLocal = null;
this.aliasMap = null;
}
public void setAliasMap(Map<String, String> aliasMap) {
this.aliasLocal = aliasMap;
this.aliasMap = aliasMap;
}
public Map<String, String> getAliasMap() {
return aliasLocal;
return aliasMap;
}
public void setCurrentTable(String table) {
currentTableLocal.set(table);
this.currentTable = table;
}
public void restoreCurrentTable(SQLObject x) {
String table = (String) x.getAttribute("_old_local_");
this.currentTable = table;
}
public void setCurrentTable(SQLObject x, String table) {
x.putAttribute("_old_local_", this.currentTable);
this.currentTable = table;
}
public String getCurrentTable() {
return currentTable;
}
protected Mode getMode() {
@@ -114,7 +134,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
public boolean visit(SQLIdentifierExpr x) {
String currentTable = currentTableLocal.get();
String currentTable = getCurrentTable();
if (subQueryMap.containsKey(currentTable)) {
return false;
@@ -277,7 +297,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
String column = ((SQLIdentifierExpr) expr).getName();
String table = currentTableLocal.get();
String table = getCurrentTable();
if (table != null) {
if (aliasMap.containsKey(table)) {
table = aliasMap.get(table);
@@ -303,7 +323,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
setAliasMap();
String originalTable = currentTableLocal.get();
String originalTable = getCurrentTable();
for (SQLName name : x.getTableNames()) {
String ident = name.toString();
@@ -332,7 +352,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
setAliasMap();
String originalTable = currentTableLocal.get();
String originalTable = getCurrentTable();
for (SQLName name : x.getTableNames()) {
String ident = name.toString();
@@ -361,7 +381,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
setAliasMap();
String originalTable = currentTableLocal.get();
String originalTable = getCurrentTable();
if (x.getTableName() instanceof SQLName) {
String ident = ((SQLName) x.getTableName()).toString();
@@ -410,7 +430,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
return false;
}
String originalTable = currentTableLocal.get();
String originalTable = getCurrentTable();
if (x.getFrom() instanceof SQLExprTableSource) {
SQLExprTableSource tableSource = (SQLExprTableSource) x.getFrom();
@@ -439,7 +459,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
public void endVisit(SQLSelectQueryBlock x) {
String originalTable = (String) x.getAttribute("_old_local_");
x.putAttribute("table", currentTableLocal.get());
x.putAttribute("table", getCurrentTable());
setCurrentTable(originalTable);
setModeOrigin(x);
@@ -513,18 +533,24 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
public boolean visit(SQLIdentifierExpr x) {
String currentTable = currentTableLocal.get();
String currentTable = getCurrentTable();
if (subQueryMap.containsKey(currentTable)) {
return false;
}
String ident = x.toString();
if (variants.containsKey(ident)) {
return false;
}
if (currentTable != null) {
Column column = new Column(currentTable, x.getName());
Column column = new Column(currentTable, ident);
columns.add(column);
x.putAttribute(ATTR_COLUMN, column);
} else {
Column column = handleUnkownColumn(x.getName());
Column column = handleUnkownColumn(ident);
if (column != null) {
x.putAttribute(ATTR_COLUMN, column);
}
@@ -539,7 +565,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
public boolean visit(SQLAllColumnExpr x) {
String currentTable = currentTableLocal.get();
String currentTable = getCurrentTable();
if (subQueryMap.containsKey(currentTable)) {
return false;
@@ -654,7 +680,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
accept(x.getQuery());
String originalTable = currentTableLocal.get();
String originalTable = getCurrentTable();
setCurrentTable((String) x.getQuery().getAttribute("table"));
x.putAttribute("_old_local_", originalTable);