mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
select/update/delete add method 'addCondition & removeCondition'. for issue #1865
This commit is contained in:
@@ -15,7 +15,11 @@
|
||||
*/
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
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.SQLBinaryOpExprGroup;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
@@ -210,4 +214,82 @@ public class SQLDeleteStatement extends SQLStatementImpl implements SQLReplaceab
|
||||
}
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
public void addCondition(String conditionSql) {
|
||||
if (conditionSql == null || conditionSql.length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
SQLExpr condition = SQLUtils.toSQLExpr(conditionSql, dbType);
|
||||
addCondition(condition);
|
||||
}
|
||||
|
||||
public void addCondition(SQLExpr expr) {
|
||||
if (expr == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.setWhere(SQLBinaryOpExpr.and(where, expr));
|
||||
}
|
||||
|
||||
public boolean removeCondition(String conditionSql) {
|
||||
if (conditionSql == null || conditionSql.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SQLExpr condition = SQLUtils.toSQLExpr(conditionSql, dbType);
|
||||
|
||||
return removeCondition(condition);
|
||||
}
|
||||
|
||||
public boolean removeCondition(SQLExpr condition) {
|
||||
if (condition == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (where instanceof SQLBinaryOpExprGroup) {
|
||||
SQLBinaryOpExprGroup group = (SQLBinaryOpExprGroup) where;
|
||||
|
||||
int removedCount = 0;
|
||||
List<SQLExpr> items = group.getItems();
|
||||
for (int i = items.size() - 1; i >= 0; i--) {
|
||||
if (items.get(i).equals(condition)) {
|
||||
items.remove(i);
|
||||
removedCount++;
|
||||
}
|
||||
}
|
||||
if (items.size() == 0) {
|
||||
where = null;
|
||||
}
|
||||
|
||||
return removedCount > 0;
|
||||
}
|
||||
|
||||
if (where instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr binaryOpWhere = (SQLBinaryOpExpr) where;
|
||||
SQLBinaryOperator operator = binaryOpWhere.getOperator();
|
||||
if (operator == SQLBinaryOperator.BooleanAnd || operator == SQLBinaryOperator.BooleanOr) {
|
||||
List<SQLExpr> items = SQLBinaryOpExpr.split(binaryOpWhere);
|
||||
|
||||
int removedCount = 0;
|
||||
for (int i = items.size() - 1; i >= 0; i--) {
|
||||
SQLExpr item = items.get(i);
|
||||
if (item.equals(condition)) {
|
||||
if (SQLUtils.replaceInParent(item, null)) {
|
||||
removedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return removedCount > 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (condition.equals(where)) {
|
||||
where = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,8 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
|
||||
protected String cachedSelectList; // optimized for SelectListCache
|
||||
|
||||
protected String dbType;
|
||||
|
||||
public SQLSelectQueryBlock(){
|
||||
|
||||
}
|
||||
@@ -580,6 +582,67 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
this.setWhere(SQLBinaryOpExpr.and(where, expr));
|
||||
}
|
||||
|
||||
public boolean removeCondition(String conditionSql) {
|
||||
if (conditionSql == null || conditionSql.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SQLExpr condition = SQLUtils.toSQLExpr(conditionSql, dbType);
|
||||
|
||||
return removeCondition(condition);
|
||||
}
|
||||
|
||||
public boolean removeCondition(SQLExpr condition) {
|
||||
if (condition == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (where instanceof SQLBinaryOpExprGroup) {
|
||||
SQLBinaryOpExprGroup group = (SQLBinaryOpExprGroup) where;
|
||||
|
||||
int removedCount = 0;
|
||||
List<SQLExpr> items = group.getItems();
|
||||
for (int i = items.size() - 1; i >= 0; i--) {
|
||||
if (items.get(i).equals(condition)) {
|
||||
items.remove(i);
|
||||
removedCount++;
|
||||
}
|
||||
}
|
||||
if (items.size() == 0) {
|
||||
where = null;
|
||||
}
|
||||
|
||||
return removedCount > 0;
|
||||
}
|
||||
|
||||
if (where instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr binaryOpWhere = (SQLBinaryOpExpr) where;
|
||||
SQLBinaryOperator operator = binaryOpWhere.getOperator();
|
||||
if (operator == SQLBinaryOperator.BooleanAnd || operator == SQLBinaryOperator.BooleanOr) {
|
||||
List<SQLExpr> items = SQLBinaryOpExpr.split(binaryOpWhere);
|
||||
|
||||
int removedCount = 0;
|
||||
for (int i = items.size() - 1; i >= 0; i--) {
|
||||
SQLExpr item = items.get(i);
|
||||
if (item.equals(condition)) {
|
||||
if (SQLUtils.replaceInParent(item, null)) {
|
||||
removedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return removedCount > 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (condition.equals(where)) {
|
||||
where = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void limit(int rowCount, int offset) {
|
||||
SQLLimit limit = new SQLLimit();
|
||||
limit.setRowCount(new SQLIntegerExpr(rowCount));
|
||||
@@ -597,4 +660,12 @@ public class SQLSelectQueryBlock extends SQLObjectImpl implements SQLSelectQuery
|
||||
public void setCachedSelectList(String cachedSelectList) {
|
||||
this.cachedSelectList = cachedSelectList;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@ 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.SQLBinaryOpExprGroup;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
@@ -179,4 +182,82 @@ public class SQLUpdateStatement extends SQLStatementImpl implements SQLReplaceab
|
||||
}
|
||||
this.with = with;
|
||||
}
|
||||
|
||||
public void addCondition(String conditionSql) {
|
||||
if (conditionSql == null || conditionSql.length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
SQLExpr condition = SQLUtils.toSQLExpr(conditionSql, dbType);
|
||||
addCondition(condition);
|
||||
}
|
||||
|
||||
public void addCondition(SQLExpr expr) {
|
||||
if (expr == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.setWhere(SQLBinaryOpExpr.and(where, expr));
|
||||
}
|
||||
|
||||
public boolean removeCondition(String conditionSql) {
|
||||
if (conditionSql == null || conditionSql.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SQLExpr condition = SQLUtils.toSQLExpr(conditionSql, dbType);
|
||||
|
||||
return removeCondition(condition);
|
||||
}
|
||||
|
||||
public boolean removeCondition(SQLExpr condition) {
|
||||
if (condition == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (where instanceof SQLBinaryOpExprGroup) {
|
||||
SQLBinaryOpExprGroup group = (SQLBinaryOpExprGroup) where;
|
||||
|
||||
int removedCount = 0;
|
||||
List<SQLExpr> items = group.getItems();
|
||||
for (int i = items.size() - 1; i >= 0; i--) {
|
||||
if (items.get(i).equals(condition)) {
|
||||
items.remove(i);
|
||||
removedCount++;
|
||||
}
|
||||
}
|
||||
if (items.size() == 0) {
|
||||
where = null;
|
||||
}
|
||||
|
||||
return removedCount > 0;
|
||||
}
|
||||
|
||||
if (where instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr binaryOpWhere = (SQLBinaryOpExpr) where;
|
||||
SQLBinaryOperator operator = binaryOpWhere.getOperator();
|
||||
if (operator == SQLBinaryOperator.BooleanAnd || operator == SQLBinaryOperator.BooleanOr) {
|
||||
List<SQLExpr> items = SQLBinaryOpExpr.split(binaryOpWhere);
|
||||
|
||||
int removedCount = 0;
|
||||
for (int i = items.size() - 1; i >= 0; i--) {
|
||||
SQLExpr item = items.get(i);
|
||||
if (item.equals(condition)) {
|
||||
if (SQLUtils.replaceInParent(item, null)) {
|
||||
removedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return removedCount > 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (condition.equals(where)) {
|
||||
where = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user