improved sql parser.

This commit is contained in:
wenshao
2017-08-01 23:33:56 +08:00
parent bd5f1712ba
commit 7042c7aff3
37 changed files with 427 additions and 50 deletions
@@ -196,14 +196,7 @@ public class Schema {
select.accept(
repository.createResolveVisitor());
SQLSelectQuery query = select.getQuery();
SQLSelectQueryBlock queryBlock = null;
if (query instanceof SQLSelectQueryBlock) {
queryBlock = (SQLSelectQueryBlock) query;
} else if (query instanceof SQLUnionQuery) {
queryBlock = ((SQLUnionQuery) query).getFirstQueryBlock();
}
SQLSelectQueryBlock queryBlock = select.getFirstQueryBlock();
if (queryBlock != null) {
List<SQLSelectItem> selectList = queryBlock.getSelectList();
for (SQLSelectItem selectItem : selectList) {
@@ -266,32 +266,69 @@ public class SchemaRepository {
public class MySqlResolveVisitor extends MySqlASTVisitorAdapter {
public boolean visit(SQLExprTableSource x) {
if (x.getSchemaObject() != null) {
return false;
}
SQLExpr expr = x.getExpr();
if (expr instanceof SQLName) {
SchemaObject table = findTable((SQLName) expr);
if (table != null) {
x.setSchemaObject(table);
}
}
return false;
}
public boolean visit(MySqlSelectQueryBlock x) {
SQLTableSource from = x.getFrom();
from.accept(this);
List<SQLSelectItem> selectList = x.getSelectList();
List<SQLSelectItem> columns = new ArrayList<SQLSelectItem>();
for (int i = selectList.size() - 1; i >= 0; i--) {
SQLSelectItem selectItem = selectList.get(i);
SQLExpr expr = selectItem.getExpr();
if (expr instanceof SQLAllColumnExpr) {
SQLTableSource from = x.getFrom();
List<SQLSelectItem> columns = new ArrayList<SQLSelectItem>();
if (from instanceof SQLExprTableSource) {
SchemaObject table = findTable((SQLExprTableSource) from);
if (table != null) {
SQLCreateTableStatement createTableStmt = (SQLCreateTableStatement) table.getStatement();
for (SQLTableElement e : createTableStmt.getTableElementList()) {
if (e instanceof SQLColumnDefinition) {
SQLColumnDefinition column = (SQLColumnDefinition) e;
SQLIdentifierExpr name = (SQLIdentifierExpr) column.getName().clone();
name.setResolvedColumn(column);
columns.add(new SQLSelectItem(name));
}
}
}
extractColumns(from, columns);
} else if (expr instanceof SQLPropertyExpr) {
SQLPropertyExpr propertyExpr = (SQLPropertyExpr) expr;
String ownerName = propertyExpr.getOwnernName();
if (propertyExpr.getName().equals("*")) {
SQLTableSource tableSource = x.findTableSource(ownerName);
extractColumns(tableSource, columns);
}
SQLColumnDefinition column = propertyExpr.getResolvedColumn();
if (column != null) {
continue;
}
SQLTableSource tableSource = x.findTableSource(propertyExpr.getOwnernName());
if (tableSource != null) {
column = tableSource.findColumn(propertyExpr.getName());
if (column != null) {
propertyExpr.setResolvedColumn(column);
}
}
} else if (expr instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
SQLColumnDefinition column = identExpr.getResolvedColumn();
if (column != null) {
continue;
}
column = from.findColumn(identExpr.getName());
if (column != null) {
identExpr.setResolvedColumn(column);
}
}
if (columns.size() > 0) {
for (SQLSelectItem column : columns) {
column.setParent(x);
}
selectList.remove(i);
selectList.addAll(i, columns);
}
@@ -301,6 +338,23 @@ public class SchemaRepository {
}
}
private void extractColumns(SQLTableSource from, List<SQLSelectItem> columns) {
if (from instanceof SQLExprTableSource) {
SchemaObject table = findTable((SQLExprTableSource) from);
if (table != null) {
SQLCreateTableStatement createTableStmt = (SQLCreateTableStatement) table.getStatement();
for (SQLTableElement e : createTableStmt.getTableElementList()) {
if (e instanceof SQLColumnDefinition) {
SQLColumnDefinition column = (SQLColumnDefinition) e;
SQLIdentifierExpr name = (SQLIdentifierExpr) column.getName().clone();
name.setResolvedColumn(column);
columns.add(new SQLSelectItem(name));
}
}
}
}
}
public SchemaObject findTable(SQLExprTableSource x) {
if (x == null) {
return null;