mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
merge
This commit is contained in:
@@ -24,6 +24,7 @@ import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLHexExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLLiteralExpr;
|
||||
@@ -55,13 +56,18 @@ public class ParameterizedOutputVisitorUtils {
|
||||
return sql;
|
||||
}
|
||||
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
ParameterizedVisitor visitor = createParameterizedOutputVisitor(out, dbType);
|
||||
stmt.accept(visitor);
|
||||
|
||||
if (visitor.getReplaceCount() == 0) {
|
||||
for (int i = 0; i < statementList.size(); i++) {
|
||||
if (i > 0) {
|
||||
out.append(";\n");
|
||||
}
|
||||
SQLStatement stmt = statementList.get(i);
|
||||
stmt.accept(visitor);
|
||||
}
|
||||
|
||||
if (visitor.getReplaceCount() == 0 && !parser.getLexer().isHasComment()) {
|
||||
return sql;
|
||||
}
|
||||
|
||||
@@ -190,6 +196,18 @@ public class ParameterizedOutputVisitorUtils {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean visit(ParameterizedVisitor v, SQLVariantRefExpr x) {
|
||||
v.print('?');
|
||||
v.incrementReplaceCunt();
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean visit(ParameterizedVisitor v, SQLHexExpr x) {
|
||||
v.print('?');
|
||||
v.incrementReplaceCunt();
|
||||
return false;
|
||||
}
|
||||
|
||||
public static SQLBinaryOpExpr merge(ParameterizedVisitor v, SQLBinaryOpExpr x) {
|
||||
SQLExpr left = x.getLeft();
|
||||
SQLExpr right = x.getRight();
|
||||
@@ -198,8 +216,17 @@ public class ParameterizedOutputVisitorUtils {
|
||||
if (left instanceof SQLLiteralExpr && right instanceof SQLLiteralExpr) {
|
||||
if (x.getOperator() == SQLBinaryOperator.Equality //
|
||||
|| x.getOperator() == SQLBinaryOperator.NotEqual) {
|
||||
left.putAttribute(ATTR_PARAMS_SKIP, true);
|
||||
right.putAttribute(ATTR_PARAMS_SKIP, true);
|
||||
if((left instanceof SQLIntegerExpr) && (right instanceof SQLIntegerExpr) ) {
|
||||
if (((SQLIntegerExpr) left).getNumber().intValue() < 100) {
|
||||
left.putAttribute(ATTR_PARAMS_SKIP, true);
|
||||
}
|
||||
if (((SQLIntegerExpr) right).getNumber().intValue() < 100) {
|
||||
right.putAttribute(ATTR_PARAMS_SKIP, true);
|
||||
}
|
||||
} else {
|
||||
left.putAttribute(ATTR_PARAMS_SKIP, true);
|
||||
right.putAttribute(ATTR_PARAMS_SKIP, true);
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTriggerStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTriggerStatement.TriggerEvent;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTriggerStatement.TriggerType;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropDatabaseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLDropFunctionStatement;
|
||||
@@ -608,7 +609,9 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
decrementIndent();
|
||||
} else if (parent instanceof ValuesClause) {
|
||||
println();
|
||||
print("(");
|
||||
x.getSubQuery().accept(this);
|
||||
print(")");
|
||||
println();
|
||||
} else {
|
||||
print("(");
|
||||
@@ -1418,6 +1421,27 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
|
||||
x.getName().accept(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLCreateViewStatement x) {
|
||||
print("CREATE ");
|
||||
if(x.isOrReplace()) {
|
||||
print("OR REPLACE ");
|
||||
}
|
||||
print("VIEW ");
|
||||
x.getName().accept(this);
|
||||
|
||||
if (x.getColumns().size() > 0) {
|
||||
print(" (");
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(")");
|
||||
}
|
||||
|
||||
print(" AS ");
|
||||
|
||||
x.getSubQuery().accept(this);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLAlterTableDropIndex x) {
|
||||
|
||||
@@ -1282,6 +1282,49 @@ public class SQLEvalVisitorUtils {
|
||||
|
||||
return BigInteger.valueOf(((Number) val).longValue());
|
||||
}
|
||||
|
||||
public static Number castToNumber(String val) {
|
||||
if (val == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return Byte.parseByte(val);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return Short.parseShort(val);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return Integer.parseInt(val);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return Long.parseLong(val);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return Float.parseFloat(val);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return Double.parseDouble(val);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
|
||||
try {
|
||||
return new BigInteger(val);
|
||||
} catch (NumberFormatException e) {
|
||||
}
|
||||
|
||||
return new BigDecimal(val);
|
||||
}
|
||||
|
||||
public static Date castToDate(Object val) {
|
||||
if (val == null) {
|
||||
@@ -1387,6 +1430,14 @@ public class SQLEvalVisitorUtils {
|
||||
if (a == null || b == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (a instanceof String) {
|
||||
a = castToNumber((String) a);
|
||||
}
|
||||
|
||||
if (b instanceof String) {
|
||||
b = castToNumber((String) b);
|
||||
}
|
||||
|
||||
if (a instanceof BigDecimal || b instanceof BigDecimal) {
|
||||
BigDecimal decimalA = castToDecimal(a);
|
||||
@@ -1664,6 +1715,14 @@ public class SQLEvalVisitorUtils {
|
||||
if (a == EVAL_VALUE_NULL || b == EVAL_VALUE_NULL) {
|
||||
return EVAL_VALUE_NULL;
|
||||
}
|
||||
|
||||
if (a instanceof String && !(b instanceof String)) {
|
||||
a = castToNumber((String) a);
|
||||
}
|
||||
|
||||
if (b instanceof String && !(a instanceof String)) {
|
||||
b = castToNumber((String) b);
|
||||
}
|
||||
|
||||
if (a instanceof BigDecimal || b instanceof BigDecimal) {
|
||||
return castToDecimal(a).add(castToDecimal(b));
|
||||
@@ -1704,7 +1763,7 @@ public class SQLEvalVisitorUtils {
|
||||
return castToByte(a) + castToByte(b);
|
||||
}
|
||||
|
||||
if (a instanceof String || b instanceof String) {
|
||||
if (a instanceof String && b instanceof String) {
|
||||
return castToString(a) + castToString(b);
|
||||
}
|
||||
|
||||
@@ -1727,6 +1786,14 @@ public class SQLEvalVisitorUtils {
|
||||
if (a instanceof Date || b instanceof Date) {
|
||||
return SQLEvalVisitor.EVAL_ERROR;
|
||||
}
|
||||
|
||||
if (a instanceof String) {
|
||||
a = castToNumber((String) a);
|
||||
}
|
||||
|
||||
if (b instanceof String) {
|
||||
b = castToNumber((String) b);
|
||||
}
|
||||
|
||||
if (a instanceof BigDecimal || b instanceof BigDecimal) {
|
||||
return castToDecimal(a).subtract(castToDecimal(b));
|
||||
@@ -1767,10 +1834,6 @@ public class SQLEvalVisitorUtils {
|
||||
return castToByte(a) - castToByte(b);
|
||||
}
|
||||
|
||||
if (a instanceof String && b instanceof String) {
|
||||
return castToLong(a) - castToLong(b);
|
||||
}
|
||||
|
||||
// return SQLEvalVisitor.EVAL_ERROR;
|
||||
throw new IllegalArgumentException(a.getClass() + " and " + b.getClass() + " not supported.");
|
||||
}
|
||||
@@ -1780,6 +1843,14 @@ public class SQLEvalVisitorUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (a instanceof String) {
|
||||
a = castToNumber((String) a);
|
||||
}
|
||||
|
||||
if (b instanceof String) {
|
||||
b = castToNumber((String) b);
|
||||
}
|
||||
|
||||
if (a instanceof BigDecimal || b instanceof BigDecimal) {
|
||||
return castToDecimal(a).multiply(castToDecimal(b));
|
||||
}
|
||||
@@ -1787,6 +1858,14 @@ public class SQLEvalVisitorUtils {
|
||||
if (a instanceof BigInteger || b instanceof BigInteger) {
|
||||
return castToBigInteger(a).multiply(castToBigInteger(b));
|
||||
}
|
||||
|
||||
if (a instanceof Double || b instanceof Double) {
|
||||
return castToDouble(a) * castToDouble(b);
|
||||
}
|
||||
|
||||
if (a instanceof Float || b instanceof Float) {
|
||||
return castToFloat(a) * castToFloat(b);
|
||||
}
|
||||
|
||||
if (a instanceof Long || b instanceof Long) {
|
||||
return castToLong(a) * castToLong(b);
|
||||
|
||||
Reference in New Issue
Block a user