improved sql parser.

This commit is contained in:
wenshao
2017-08-01 21:22:50 +08:00
parent bd125342e2
commit bd5f1712ba
4 changed files with 42 additions and 30 deletions
@@ -700,27 +700,8 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
return true;
}
private boolean apply(SQLAlterTableAddIndex item) {
if (item.isKey()) {
MySqlPrimaryKey x = new MySqlPrimaryKey();
item.cloneTo(x);
x.setParent(this);
this.tableElementList.add(x);
return true;
}
if (item.isUnique()) {
MySqlUnique x = new MySqlUnique();
item.cloneTo(x);
x.setParent(this);
this.tableElementList.add(x);
return true;
}
MySqlTableIndex x = new MySqlTableIndex();
item.cloneTo(x);
x.setParent(this);
this.tableElementList.add(x);
return true;
protected boolean apply(SQLAlterTableAddIndex item) {
return false;
}
private boolean apply(SQLAlterTableAddColumn item) {
@@ -135,4 +135,16 @@ public class SQLUnionQuery extends SQLObjectImpl implements SQLSelectQuery {
return x;
}
public SQLSelectQueryBlock getFirstQueryBlock() {
if (left instanceof SQLSelectQueryBlock) {
return (SQLSelectQueryBlock) left;
}
if (left instanceof SQLUnionQuery) {
return ((SQLUnionQuery) left).getFirstQueryBlock();
}
return null;
}
}
@@ -29,6 +29,8 @@ import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLPartitionBy;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObjectImpl;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlShowColumnOutpuVisitor;
@@ -211,16 +213,26 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
return super.alterApply(item);
}
public boolean apply(SQLAlterTableAddIndex x) {
MySqlTableIndex index = new MySqlTableIndex();
index.setName(x.getName());
for (SQLSelectOrderByItem orderByItem : x.getItems()) {
index.addColumn(orderByItem.clone());
public boolean apply(SQLAlterTableAddIndex item) {
if (item.isKey()) {
MySqlPrimaryKey x = new MySqlPrimaryKey();
item.cloneTo(x);
x.setParent(this);
this.tableElementList.add(x);
return true;
}
if (item.isUnique()) {
MySqlUnique x = new MySqlUnique();
item.cloneTo(x);
x.setParent(this);
this.tableElementList.add(x);
return true;
}
index.setParent(this);
this.tableElementList.add(index);
MySqlTableIndex x = new MySqlTableIndex();
item.cloneTo(x);
x.setParent(this);
this.tableElementList.add(x);
return true;
}
@@ -196,7 +196,14 @@ public class Schema {
select.accept(
repository.createResolveVisitor());
SQLSelectQueryBlock queryBlock = select.getQueryBlock();
SQLSelectQuery query = select.getQuery();
SQLSelectQueryBlock queryBlock = null;
if (query instanceof SQLSelectQueryBlock) {
queryBlock = (SQLSelectQueryBlock) query;
} else if (query instanceof SQLUnionQuery) {
queryBlock = ((SQLUnionQuery) query).getFirstQueryBlock();
}
if (queryBlock != null) {
List<SQLSelectItem> selectList = queryBlock.getSelectList();
for (SQLSelectItem selectItem : selectList) {