mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql parser support oracle merge
This commit is contained in:
@@ -28,6 +28,7 @@ import com.alibaba.druid.sql.ast.statement.SQLJoinTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
|
||||
@@ -46,6 +47,7 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
protected final Set<Column> orderByColumns = new LinkedHashSet<Column>();
|
||||
protected final Set<Column> groupByColumns = new LinkedHashSet<Column>();
|
||||
|
||||
private final Map<String, SQLSelectQuery> subQueryMap = new LinkedHashMap<String, SQLSelectQuery>();
|
||||
protected final static ThreadLocal<Map<String, String>> aliasLocal = new ThreadLocal<Map<String, String>>();
|
||||
protected final static ThreadLocal<String> currentTableLocal = new ThreadLocal<String>();
|
||||
protected final static ThreadLocal<Mode> modeLocal = new ThreadLocal<Mode>();
|
||||
@@ -159,7 +161,8 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
String column = ((SQLPropertyExpr) expr).getName();
|
||||
|
||||
if (owner instanceof SQLIdentifierExpr) {
|
||||
String table = ((SQLIdentifierExpr) owner).getName();
|
||||
String tableName = ((SQLIdentifierExpr) owner).getName();
|
||||
String table = tableName;
|
||||
if (aliasMap.containsKey(table)) {
|
||||
table = aliasMap.get(table);
|
||||
}
|
||||
@@ -169,6 +172,14 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
condition.setColumn(new Column(table, column));
|
||||
condition.setOperator(operator);
|
||||
this.conditions.add(condition);
|
||||
} else {
|
||||
Column sqlColumn = handleSubQueryColumn(tableName, column);
|
||||
if (sqlColumn != null) {
|
||||
Condition condition = new Condition();
|
||||
condition.setColumn(sqlColumn);
|
||||
condition.setOperator(operator);
|
||||
this.conditions.add(condition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -357,7 +368,9 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
// table == null时是SubQuery
|
||||
if (table != null) {
|
||||
columns.add(new Column(table, x.getName()));
|
||||
Column column = new Column(table, x.getName());
|
||||
columns.add(column);
|
||||
x.putAttribute("_column_", column);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -365,11 +378,48 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected Column handleSubQueryColumn(String owner, String alias) {
|
||||
SQLSelectQuery query = subQueryMap.get(owner);
|
||||
|
||||
if (query == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<SQLSelectItem> selectList = null;
|
||||
if (query instanceof SQLSelectQueryBlock) {
|
||||
selectList = ((SQLSelectQueryBlock) query).getSelectList();
|
||||
}
|
||||
|
||||
if (selectList != null) {
|
||||
for (SQLSelectItem item : selectList) {
|
||||
String itemAlias = item.getAlias();
|
||||
SQLExpr itemExpr = item.getExpr();
|
||||
if (itemAlias == null) {
|
||||
if (itemExpr instanceof SQLIdentifierExpr) {
|
||||
itemAlias = itemExpr.toString();
|
||||
} else if (itemExpr instanceof SQLPropertyExpr) {
|
||||
itemAlias = ((SQLPropertyExpr) itemExpr).getName();
|
||||
}
|
||||
}
|
||||
|
||||
if (alias.equalsIgnoreCase(itemAlias)) {
|
||||
Column column = (Column) itemExpr.getAttribute("_column_");
|
||||
return column;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean visit(SQLIdentifierExpr x) {
|
||||
String currentTable = currentTableLocal.get();
|
||||
|
||||
if (currentTable != null) {
|
||||
columns.add(new Column(currentTable, x.getName()));
|
||||
Column column = new Column(currentTable, x.getName());
|
||||
columns.add(column);
|
||||
x.putAttribute("_column_", column);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -405,13 +455,18 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
}
|
||||
|
||||
public boolean visit(SQLSubqueryTableSource x) {
|
||||
x.getSelect().accept(this);
|
||||
|
||||
SQLSelectQuery query = x.getSelect().getQuery();
|
||||
|
||||
Map<String, String> aliasMap = aliasLocal.get();
|
||||
if (aliasMap != null) {
|
||||
if (x.getAlias() != null) {
|
||||
aliasMap.put(x.getAlias(), null);
|
||||
subQueryMap.put(x.getAlias(), query);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
protected boolean isSimpleExprTableSource(SQLExprTableSource x) {
|
||||
@@ -448,6 +503,9 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
case Select:
|
||||
stat.incrementSelectCount();
|
||||
break;
|
||||
case Merge:
|
||||
stat.incrementMergeCount();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -465,6 +523,11 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectItem x) {
|
||||
x.getExpr().setParent(x);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelect x) {
|
||||
if (x.getOrderBy() != null) {
|
||||
x.getOrderBy().setParent(x);
|
||||
|
||||
Reference in New Issue
Block a user