refactor sql parser. sqlite support limit clause. #1556

This commit is contained in:
wenshao
2016-12-26 20:11:49 +08:00
parent 74b456d659
commit 77bfab01f5
32 changed files with 396 additions and 270 deletions
@@ -992,12 +992,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
}
protected void printFetchFirst(SQLSelectQueryBlock x) {
SQLExpr offset = x.getOffset();
SQLExpr first = x.getFirst();
SQLLimit limit = x.getLimit();
if (limit == null) {
return;
}
boolean informix = JdbcConstants.INFORMIX.equals(dbType);
if (first != null) {
if (informix) {
SQLExpr offset = limit.getOffset();
SQLExpr first = limit.getRowCount();
if (limit != null) {
if (JdbcConstants.INFORMIX.equals(dbType)) {
if (offset != null) {
print0(ucase ? "SKIP " : "skip ");
offset.accept(this);
@@ -1006,7 +1010,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
print0(ucase ? " FIRST " : " first ");
first.accept(this);
print(' ');
} else {
} else if (JdbcConstants.DB2.equals(dbType)
|| JdbcConstants.ORACLE.equals(dbType)
|| JdbcConstants.SQL_SERVER.equals(dbType)) {
//order by 语句必须在FETCH FIRST ROWS ONLY之前
SQLObject parent = x.getParent();
if (parent instanceof SQLSelect) {
@@ -1028,6 +1034,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
print0(ucase ? "FETCH FIRST " : "fetch first ");
first.accept(this);
print0(ucase ? " ROWS ONLY" : " rows only");
} else {
println();
limit.accept(this);
}
}
}
@@ -53,12 +53,15 @@ import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
import com.alibaba.druid.sql.ast.expr.SQLSequenceExpr;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitorAdapter;
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleExpr;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGASTVisitorAdapter;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.stat.TableStat.Column;
import com.alibaba.druid.stat.TableStat.Condition;
import com.alibaba.druid.stat.TableStat.Mode;
import com.alibaba.druid.stat.TableStat.Relationship;
import com.alibaba.druid.util.JdbcConstants;
public class SchemaStatVisitor extends SQLASTVisitorAdapter {
@@ -235,7 +238,55 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
return oldMode;
}
public class OrderByStatVisitor extends SQLASTVisitorAdapter {
private boolean visitOrderBy(SQLIdentifierExpr x) {
if (containsSubQuery(currentTable)) {
return false;
}
String identName = x.getName();
if (aliasMap != null && aliasMap.containsKey(identName) && aliasMap.get(identName) == null) {
return false;
}
if (currentTable != null) {
orderByAddColumn(currentTable, identName, x);
} else {
orderByAddColumn("UNKOWN", identName, x);
}
return false;
}
private boolean visitOrderBy(SQLPropertyExpr x) {
if (x.getOwner() instanceof SQLIdentifierExpr) {
String owner = ((SQLIdentifierExpr) x.getOwner()).getName();
if (containsSubQuery(owner)) {
return false;
}
owner = aliasWrap(owner);
if (owner != null) {
orderByAddColumn(owner, x.getName(), x);
}
}
return false;
}
private void orderByAddColumn(String table, String columnName, SQLObject expr) {
Column column = new Column(table, columnName);
SQLObject parent = expr.getParent();
if (parent instanceof SQLSelectOrderByItem) {
SQLOrderingSpecification type = ((SQLSelectOrderByItem) parent).getType();
column.getAttributes().put("orderBy.type", type);
}
orderByColumns.add(column);
}
protected class OrderByStatVisitor extends SQLASTVisitorAdapter {
private final SQLOrderBy orderBy;
@@ -251,56 +302,89 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
public boolean visit(SQLIdentifierExpr x) {
if (containsSubQuery(currentTable)) {
return false;
}
String identName = x.getName();
if (aliasMap != null && aliasMap.containsKey(identName) && aliasMap.get(identName) == null) {
return false;
}
if (currentTable != null) {
addOrderByColumn(currentTable, identName, x);
} else {
addOrderByColumn("UNKOWN", identName, x);
}
return false;
return visitOrderBy(x);
}
public boolean visit(SQLPropertyExpr x) {
if (x.getOwner() instanceof SQLIdentifierExpr) {
String owner = ((SQLIdentifierExpr) x.getOwner()).getName();
return visitOrderBy(x);
}
}
if (containsSubQuery(owner)) {
return false;
}
protected class MySqlOrderByStatVisitor extends MySqlASTVisitorAdapter {
owner = aliasWrap(owner);
private final SQLOrderBy orderBy;
if (owner != null) {
addOrderByColumn(owner, x.getName(), x);
}
public MySqlOrderByStatVisitor(SQLOrderBy orderBy){
this.orderBy = orderBy;
for (SQLSelectOrderByItem item : orderBy.getItems()) {
item.getExpr().setParent(item);
}
return false;
}
public void addOrderByColumn(String table, String columnName, SQLObject expr) {
Column column = new Column(table, columnName);
public SQLOrderBy getOrderBy() {
return orderBy;
}
SQLObject parent = expr.getParent();
if (parent instanceof SQLSelectOrderByItem) {
SQLOrderingSpecification type = ((SQLSelectOrderByItem) parent).getType();
column.getAttributes().put("orderBy.type", type);
public boolean visit(SQLIdentifierExpr x) {
return visitOrderBy(x);
}
public boolean visit(SQLPropertyExpr x) {
return visitOrderBy(x);
}
}
protected class PGOrderByStatVisitor extends PGASTVisitorAdapter {
private final SQLOrderBy orderBy;
public PGOrderByStatVisitor(SQLOrderBy orderBy){
this.orderBy = orderBy;
for (SQLSelectOrderByItem item : orderBy.getItems()) {
item.getExpr().setParent(item);
}
}
orderByColumns.add(column);
public SQLOrderBy getOrderBy() {
return orderBy;
}
public boolean visit(SQLIdentifierExpr x) {
return visitOrderBy(x);
}
public boolean visit(SQLPropertyExpr x) {
return visitOrderBy(x);
}
}
protected class OracleOrderByStatVisitor extends PGASTVisitorAdapter {
private final SQLOrderBy orderBy;
public OracleOrderByStatVisitor(SQLOrderBy orderBy){
this.orderBy = orderBy;
for (SQLSelectOrderByItem item : orderBy.getItems()) {
item.getExpr().setParent(item);
}
}
public SQLOrderBy getOrderBy() {
return orderBy;
}
public boolean visit(SQLIdentifierExpr x) {
return visitOrderBy(x);
}
public boolean visit(SQLPropertyExpr x) {
return visitOrderBy(x);
}
}
public boolean visit(SQLOrderBy x) {
OrderByStatVisitor orderByVisitor = new OrderByStatVisitor(x);
final SQLASTVisitor orderByVisitor = createOrderByVisitor(x);
SQLSelectQueryBlock query = null;
if (x.getParent() instanceof SQLSelectQueryBlock) {
query = (SQLSelectQueryBlock) x.getParent();
@@ -323,6 +407,20 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
return true;
}
protected SQLASTVisitor createOrderByVisitor(SQLOrderBy x) {
final SQLASTVisitor orderByVisitor;
if (JdbcConstants.MYSQL.equals(getDbType())) {
orderByVisitor = new MySqlOrderByStatVisitor(x);
} else if (JdbcConstants.POSTGRESQL.equals(getDbType())) {
orderByVisitor = new PGOrderByStatVisitor(x);
} else if (JdbcConstants.ORACLE.equals(getDbType())) {
orderByVisitor = new OracleOrderByStatVisitor(x);
} else {
orderByVisitor = new OrderByStatVisitor(x);
}
return orderByVisitor;
}
public Set<Relationship> getRelationships() {
return relationships;
}