mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser.
This commit is contained in:
@@ -52,4 +52,8 @@ public class SQLCharExpr extends SQLTextLiteralExpr implements SQLValuableExpr{
|
||||
public String toString() {
|
||||
return SQLUtils.toSQLString(this);
|
||||
}
|
||||
|
||||
public SQLCharExpr clone() {
|
||||
return new SQLCharExpr(this.text);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,4 +100,15 @@ public class SQLExistsExpr extends SQLExprImpl implements Serializable {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public SQLExistsExpr clone () {
|
||||
SQLExistsExpr x = new SQLExistsExpr();
|
||||
|
||||
x.not = not;
|
||||
if (subQuery != null) {
|
||||
x.setSubQuery(subQuery.clone());
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.ast.expr;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExprImpl;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
@@ -102,6 +103,28 @@ public class SQLIdentifierExpr extends SQLExprImpl implements SQLName {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean equalsIgnoreCase(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof SQLIdentifierExpr)) {
|
||||
return false;
|
||||
}
|
||||
SQLIdentifierExpr other = (SQLIdentifierExpr) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else if (!SQLUtils.normalize(name).equalsIgnoreCase(SQLUtils.normalize(other.name))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@@ -71,7 +71,14 @@ public abstract class SQLInsertInto extends SQLObjectImpl {
|
||||
return query;
|
||||
}
|
||||
|
||||
public void setQuery(SQLSelectQuery query) {
|
||||
this.setQuery(new SQLSelect(query));
|
||||
}
|
||||
|
||||
public void setQuery(SQLSelect query) {
|
||||
if (query != null) {
|
||||
query.setParent(this);
|
||||
}
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
|
||||
@@ -154,7 +154,10 @@ public class SQLSelect extends SQLObjectImpl {
|
||||
SQLSelect x = new SQLSelect();
|
||||
|
||||
x.withSubQuery = this.withSubQuery;
|
||||
x.query = this.query;
|
||||
if (query != null) {
|
||||
x.setQuery(query.clone());
|
||||
}
|
||||
|
||||
if (orderBy != null) {
|
||||
x.setOrderBy(this.orderBy.clone());
|
||||
}
|
||||
|
||||
@@ -21,4 +21,5 @@ public interface SQLSelectQuery extends SQLObject {
|
||||
boolean isBracket();
|
||||
void setBracket(boolean bracket);
|
||||
|
||||
SQLSelectQuery clone();
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.List;
|
||||
|
||||
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.visitor.SQLASTVisitor;
|
||||
|
||||
@@ -439,4 +440,12 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addCondition(SQLExpr expr) {
|
||||
if (expr == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.setWhere(SQLBinaryOpExpr.and(where, expr));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,12 @@ public class SQLUnionQuery extends SQLObjectImpl implements SQLSelectQuery {
|
||||
|
||||
}
|
||||
|
||||
public SQLUnionQuery(SQLSelectQuery left, SQLUnionOperator operator, SQLSelectQuery right){
|
||||
this.setLeft(left);
|
||||
this.operator = operator;
|
||||
this.setRight(right);
|
||||
}
|
||||
|
||||
public SQLSelectQuery getLeft() {
|
||||
return left;
|
||||
}
|
||||
@@ -106,4 +112,27 @@ public class SQLUnionQuery extends SQLObjectImpl implements SQLSelectQuery {
|
||||
public void setBracket(boolean bracket) {
|
||||
this.bracket = bracket;
|
||||
}
|
||||
|
||||
public SQLUnionQuery clone() {
|
||||
SQLUnionQuery x = new SQLUnionQuery();
|
||||
|
||||
x.bracket = bracket;
|
||||
if (left != null) {
|
||||
x.setLeft(left.clone());
|
||||
}
|
||||
if (right != null) {
|
||||
x.setRight(right.clone());
|
||||
}
|
||||
x.operator = operator;
|
||||
|
||||
if (orderBy != null) {
|
||||
x.setOrderBy(orderBy.clone());
|
||||
}
|
||||
|
||||
if (limit != null) {
|
||||
x.setLimit(limit.clone());
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -1703,7 +1703,8 @@ public class OracleStatementParser extends SQLStatementParser {
|
||||
while (lexer.token() == Token.INTO) {
|
||||
OracleMultiInsertStatement.InsertIntoClause clause = new OracleMultiInsertStatement.InsertIntoClause();
|
||||
|
||||
parseInsert0(clause);
|
||||
boolean acceptSubQuery = stmt.getEntries().size() == 0;
|
||||
parseInsert0(clause, acceptSubQuery);
|
||||
|
||||
clause.setReturning(parseReturningClause());
|
||||
clause.setErrorLogging(parseErrorLoggingClause());
|
||||
|
||||
@@ -49,4 +49,17 @@ public class PGValuesQuery extends PGSQLObjectImpl implements SQLSelectQuery {
|
||||
public void setBracket(boolean bracket) {
|
||||
this.bracket = bracket;
|
||||
}
|
||||
|
||||
public PGValuesQuery clone() {
|
||||
PGValuesQuery x = new PGValuesQuery();
|
||||
x.bracket = bracket;
|
||||
|
||||
for (int i = 0; i < values.size(); ++i) {
|
||||
SQLExpr value = values.get(i).clone();
|
||||
value.setParent(x);
|
||||
x.values.add(value);
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -707,9 +707,18 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
|
||||
private boolean printName(SQLName x, String name) {
|
||||
boolean shardingSupport = this.shardingSupport && parameterized;
|
||||
return printName(x, name, shardingSupport);
|
||||
}
|
||||
|
||||
private boolean printName(SQLName x, String name, boolean shardingSupport) {
|
||||
|
||||
if (shardingSupport) {
|
||||
SQLObject parent = x.getParent();
|
||||
shardingSupport = parent instanceof SQLExprTableSource || parent instanceof SQLPropertyExpr;
|
||||
|
||||
if (parent instanceof SQLPropertyExpr && parent.getParent() instanceof SQLExprTableSource) {
|
||||
shardingSupport = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (shardingSupport) {
|
||||
@@ -1133,11 +1142,7 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
if (mapTableName != null) {
|
||||
print0(mapTableName);
|
||||
} else {
|
||||
if (owner instanceof SQLIdentifierExpr && x.getParent() instanceof SQLExprTableSource) {
|
||||
print0(((SQLIdentifierExpr) owner).getName());
|
||||
} else {
|
||||
owner.accept(this);
|
||||
}
|
||||
owner.accept(this);
|
||||
}
|
||||
print('.');
|
||||
|
||||
@@ -1321,6 +1326,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
printFetchFirst(x);
|
||||
}
|
||||
|
||||
if (x.isForUpdate()) {
|
||||
println();
|
||||
print0(ucase ? "FOR UPDATE" : "for update");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user