mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
SQL参数合并
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@215 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
+55
-25
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.druid.sql.dialect.mysql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
@@ -49,7 +50,7 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
|
||||
x.getRight().getAttributes().put(ATTR_PARAMS_SKIP, true);
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
if (x.getRight() instanceof SQLLiteralExpr) {
|
||||
x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), new SQLVariantRefExpr("?"));
|
||||
}
|
||||
@@ -72,27 +73,13 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
|
||||
SQLBinaryOpExpr left = (SQLBinaryOpExpr) x.getLeft();
|
||||
SQLBinaryOpExpr right = (SQLBinaryOpExpr) x.getRight();
|
||||
|
||||
if (left.getOperator() == SQLBinaryOperator.Equality && right.getOperator() == SQLBinaryOperator.Equality) {
|
||||
if ((left.getLeft() instanceof SQLIdentifierExpr) && (right.getLeft() instanceof SQLIdentifierExpr)) {
|
||||
String leftColumn = left.getLeft().toString();
|
||||
String rightColumn = right.getLeft().toString();
|
||||
if (mergeEqual(left, right)) {
|
||||
return left;
|
||||
}
|
||||
|
||||
if (leftColumn.equals(rightColumn)) {
|
||||
if ((left.getRight() instanceof SQLVariantRefExpr) && (right.getRight() instanceof SQLVariantRefExpr)) {
|
||||
return left; // merge
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((left.getLeft() instanceof SQLPropertyExpr) && (right.getLeft() instanceof SQLPropertyExpr)) {
|
||||
String leftColumn = left.getLeft().toString();
|
||||
String rightColumn = right.getLeft().toString();
|
||||
|
||||
if (leftColumn.equals(rightColumn)) {
|
||||
if ((left.getRight() instanceof SQLVariantRefExpr) && (right.getRight() instanceof SQLVariantRefExpr)) {
|
||||
return left; // merge
|
||||
}
|
||||
}
|
||||
if (isLiteralExpr(left.getLeft()) && left.getOperator() == SQLBinaryOperator.BooleanOr) {
|
||||
if (mergeEqual(left.getRight(), right)) {
|
||||
return left;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,11 +88,54 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
|
||||
return x;
|
||||
}
|
||||
|
||||
private boolean mergeEqual(SQLExpr a, SQLExpr b) {
|
||||
if (!(a instanceof SQLBinaryOpExpr)) {
|
||||
return false;
|
||||
}
|
||||
if (!(b instanceof SQLBinaryOpExpr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SQLBinaryOpExpr binaryA = (SQLBinaryOpExpr) a;
|
||||
SQLBinaryOpExpr binaryB = (SQLBinaryOpExpr) b;
|
||||
|
||||
if (binaryA.getOperator() != SQLBinaryOperator.Equality) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (binaryB.getOperator() != SQLBinaryOperator.Equality) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(binaryA.getRight() instanceof SQLLiteralExpr || binaryA.getRight() instanceof SQLVariantRefExpr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(binaryB.getRight() instanceof SQLLiteralExpr || binaryB.getRight() instanceof SQLVariantRefExpr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return binaryA.getLeft().toString().equals(binaryB.getLeft().toString());
|
||||
}
|
||||
|
||||
private boolean isLiteralExpr(SQLExpr expr) {
|
||||
if (expr instanceof SQLLiteralExpr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (expr instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr binary = (SQLBinaryOpExpr) expr;
|
||||
return isLiteralExpr(binary.getLeft()) && isLiteralExpr(binary.getRight());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLIntegerExpr x) {
|
||||
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
|
||||
print('?');
|
||||
return false;
|
||||
}
|
||||
@@ -114,7 +144,7 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
|
||||
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
|
||||
print('?');
|
||||
return false;
|
||||
}
|
||||
@@ -123,7 +153,7 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
|
||||
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
|
||||
print('?');
|
||||
return false;
|
||||
}
|
||||
@@ -132,7 +162,7 @@ public class MySqlParameterizedOutputVisitor extends MySqlOutputVisitor {
|
||||
if (Boolean.TRUE.equals(x.getAttribute(ATTR_PARAMS_SKIP))) {
|
||||
return super.visit(x);
|
||||
}
|
||||
|
||||
|
||||
print('?');
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user