improved sql parser.

This commit is contained in:
wenshao
2017-07-11 15:48:32 +08:00
parent 15ac64e501
commit bb833e499e
20 changed files with 564 additions and 95 deletions
@@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.ast.statement;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLCommentHint;
import com.alibaba.druid.sql.ast.SQLDataTypeImpl;
import com.alibaba.druid.sql.ast.SQLExpr;
@@ -99,4 +100,9 @@ public class SQLCharacterDataType extends SQLDataTypeImpl {
visitor.endVisit(this);
}
@Override
public String toString() {
return SQLUtils.toSQLString(this);
}
}
@@ -103,6 +103,9 @@ public class SQLColumnDefinition extends SQLObjectImpl implements SQLTableElemen
}
public void setDataType(SQLDataType dataType) {
if (dataType != null) {
dataType.setParent(this);
}
this.dataType = dataType;
}
@@ -227,4 +227,14 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
}
}
}
public SQLPrimaryKey findPrimaryKey() {
for (SQLTableElement element : this.tableElementList) {
if (element instanceof SQLPrimaryKey) {
return (SQLPrimaryKey) element;
}
}
return null;
}
}
@@ -22,6 +22,7 @@ import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery, SQLReplaceable {
@@ -89,6 +90,18 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
}
this.where = where;
}
public void addWhere(SQLExpr condition) {
if (condition == null) {
return;
}
if (where == null) {
where = condition;
} else {
where = SQLBinaryOpExpr.and(where, condition);
}
}
public SQLOrderBy getOrderBy() {
return orderBy;
@@ -448,4 +461,14 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
this.setWhere(SQLBinaryOpExpr.and(where, expr));
}
public void limit(int rowCount, int offset) {
SQLLimit limit = new SQLLimit();
limit.setRowCount(new SQLIntegerExpr(rowCount));
if (offset > 0) {
limit.setOffset(new SQLIntegerExpr(offset));
}
setLimit(limit);
}
}