mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add testcase & remove unused code
This commit is contained in:
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.visitor;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
|
||||
@@ -93,6 +94,7 @@ public class ParameterizedOutputVisitorUtils {
|
||||
}
|
||||
|
||||
public static SQLBinaryOpExpr merge(SQLBinaryOpExpr x) {
|
||||
SQLObject parent = x.getParent();
|
||||
if (x.getLeft() instanceof SQLLiteralExpr && x.getRight() instanceof SQLLiteralExpr) {
|
||||
if (x.getOperator() == SQLBinaryOperator.Equality || x.getOperator() == SQLBinaryOperator.NotEqual) {
|
||||
x.getLeft().getAttributes().put(ATTR_PARAMS_SKIP, true);
|
||||
@@ -103,10 +105,12 @@ public class ParameterizedOutputVisitorUtils {
|
||||
|
||||
if (x.getRight() instanceof SQLLiteralExpr) {
|
||||
x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), new SQLVariantRefExpr("?"));
|
||||
x.setParent(parent);
|
||||
}
|
||||
|
||||
if (x.getLeft() instanceof SQLLiteralExpr) {
|
||||
x = new SQLBinaryOpExpr(new SQLVariantRefExpr("?"), x.getOperator(), x.getRight());
|
||||
x.setParent(parent);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
@@ -119,6 +123,7 @@ public class ParameterizedOutputVisitorUtils {
|
||||
}
|
||||
}
|
||||
x = new SQLBinaryOpExpr(x.getLeft(), x.getOperator(), merge((SQLBinaryOpExpr) x.getRight()));
|
||||
x.setParent(parent);
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -126,6 +131,7 @@ public class ParameterizedOutputVisitorUtils {
|
||||
|
||||
if (x.getLeft() instanceof SQLBinaryOpExpr) {
|
||||
x = new SQLBinaryOpExpr(merge((SQLBinaryOpExpr) x.getLeft()), x.getOperator(), x.getRight());
|
||||
x.setParent(parent);
|
||||
}
|
||||
|
||||
// ID = ? OR ID = ? => ID = ?
|
||||
|
||||
@@ -58,7 +58,6 @@ import com.alibaba.druid.sql.ast.expr.SQLNCharExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNotExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNullExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLObjectCreateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLPropertyExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLSomeExpr;
|
||||
@@ -88,7 +87,6 @@ import com.alibaba.druid.sql.ast.statement.SQLColumnCheck;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnConstraint;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnPrimaryKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnUniqueIndex;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
@@ -554,10 +552,6 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLObjectCreateExpr x) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
x.getOwner().accept(this);
|
||||
print(".");
|
||||
@@ -1352,12 +1346,6 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLColumnUniqueIndex x) {
|
||||
print(" UNIQUE INDEX");
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLWithSubqueryClause x) {
|
||||
print("WITH");
|
||||
|
||||
@@ -33,12 +33,10 @@ import com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLDefaultExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLExistsExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLHexExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLHexStringLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntervalLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLListExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNCharExpr;
|
||||
@@ -72,7 +70,6 @@ import com.alibaba.druid.sql.ast.statement.SQLCheck;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnCheck;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnPrimaryKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnUniqueIndex;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
@@ -207,10 +204,6 @@ public interface SQLASTVisitor {
|
||||
|
||||
void endVisit(SQLHexExpr x);
|
||||
|
||||
boolean visit(SQLHexStringLiteralExpr x);
|
||||
|
||||
void endVisit(SQLHexStringLiteralExpr x);
|
||||
|
||||
boolean visit(SQLSelect x);
|
||||
|
||||
void endVisit(SQLSelect select);
|
||||
@@ -223,10 +216,6 @@ public interface SQLASTVisitor {
|
||||
|
||||
void endVisit(SQLExprTableSource x);
|
||||
|
||||
boolean visit(SQLIntervalLiteralExpr x);
|
||||
|
||||
void endVisit(SQLIntervalLiteralExpr x);
|
||||
|
||||
boolean visit(SQLOrderBy x);
|
||||
|
||||
void endVisit(SQLOrderBy x);
|
||||
@@ -411,10 +400,6 @@ public interface SQLASTVisitor {
|
||||
|
||||
boolean visit(SQLColumnPrimaryKey x);
|
||||
|
||||
void endVisit(SQLColumnUniqueIndex x);
|
||||
|
||||
boolean visit(SQLColumnUniqueIndex x);
|
||||
|
||||
void endVisit(SQLWithSubqueryClause x);
|
||||
|
||||
boolean visit(SQLWithSubqueryClause x);
|
||||
|
||||
@@ -33,12 +33,10 @@ import com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLDefaultExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLExistsExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLHexExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLHexStringLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLInListExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntegerExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLIntervalLiteralExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLListExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNCharExpr;
|
||||
@@ -72,7 +70,6 @@ import com.alibaba.druid.sql.ast.statement.SQLCheck;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnCheck;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnPrimaryKey;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnUniqueIndex;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCommentStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
@@ -268,13 +265,6 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
public void endVisit(SQLQueryExpr x) {
|
||||
}
|
||||
|
||||
public boolean visit(SQLHexStringLiteralExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void endVisit(SQLHexStringLiteralExpr x) {
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelect x) {
|
||||
return true;
|
||||
}
|
||||
@@ -296,13 +286,6 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
public void endVisit(SQLExprTableSource x) {
|
||||
}
|
||||
|
||||
public boolean visit(SQLIntervalLiteralExpr x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void endVisit(SQLIntervalLiteralExpr x) {
|
||||
}
|
||||
|
||||
public boolean visit(SQLOrderBy x) {
|
||||
return true;
|
||||
}
|
||||
@@ -726,15 +709,6 @@ public class SQLASTVisitorAdapter implements SQLASTVisitor {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLColumnUniqueIndex x) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(SQLColumnUniqueIndex x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(SQLWithSubqueryClause x) {
|
||||
}
|
||||
|
||||
@@ -107,7 +107,13 @@ public class SQLEvalVisitorUtils {
|
||||
}
|
||||
|
||||
public static Object eval(String dbType, SQLObject sqlObject, Object... parameters) {
|
||||
return eval(dbType, sqlObject, Arrays.asList(parameters));
|
||||
Object value = eval(dbType, sqlObject, Arrays.asList(parameters));
|
||||
|
||||
if (value == EVAL_VALUE_NULL) {
|
||||
value = null;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public static Object getValue(SQLObject sqlObject) {
|
||||
@@ -383,7 +389,7 @@ public class SQLEvalVisitorUtils {
|
||||
}
|
||||
|
||||
double doubleValue = castToDouble(paramValue);
|
||||
int result = (int) Math.cos(doubleValue);
|
||||
double result = Math.cos(doubleValue);
|
||||
|
||||
if (Double.isNaN(result)) {
|
||||
x.putAttribute(EVAL_VALUE, null);
|
||||
@@ -404,7 +410,7 @@ public class SQLEvalVisitorUtils {
|
||||
}
|
||||
|
||||
double doubleValue = castToDouble(paramValue);
|
||||
int result = (int) Math.sin(doubleValue);
|
||||
double result = Math.sin(doubleValue);
|
||||
|
||||
if (Double.isNaN(result)) {
|
||||
x.putAttribute(EVAL_VALUE, null);
|
||||
@@ -425,7 +431,7 @@ public class SQLEvalVisitorUtils {
|
||||
}
|
||||
|
||||
double doubleValue = castToDouble(paramValue);
|
||||
int result = (int) Math.log(doubleValue);
|
||||
double result = Math.log(doubleValue);
|
||||
|
||||
if (Double.isNaN(result)) {
|
||||
x.putAttribute(EVAL_VALUE, null);
|
||||
@@ -446,7 +452,7 @@ public class SQLEvalVisitorUtils {
|
||||
}
|
||||
|
||||
double doubleValue = castToDouble(paramValue);
|
||||
int result = (int) Math.log10(doubleValue);
|
||||
double result = Math.log10(doubleValue);
|
||||
|
||||
if (Double.isNaN(result)) {
|
||||
x.putAttribute(EVAL_VALUE, null);
|
||||
@@ -467,7 +473,7 @@ public class SQLEvalVisitorUtils {
|
||||
}
|
||||
|
||||
double doubleValue = castToDouble(paramValue);
|
||||
int result = (int) Math.tan(doubleValue);
|
||||
double result = Math.tan(doubleValue);
|
||||
|
||||
if (Double.isNaN(result)) {
|
||||
x.putAttribute(EVAL_VALUE, null);
|
||||
@@ -488,14 +494,14 @@ public class SQLEvalVisitorUtils {
|
||||
}
|
||||
|
||||
double doubleValue = castToDouble(paramValue);
|
||||
int result = (int) Math.sqrt(doubleValue);
|
||||
double result = Math.sqrt(doubleValue);
|
||||
|
||||
if (Double.isNaN(result)) {
|
||||
x.putAttribute(EVAL_VALUE, null);
|
||||
} else {
|
||||
x.putAttribute(EVAL_VALUE, result);
|
||||
}
|
||||
} else if ("power".equals(methodName)) {
|
||||
} else if ("power".equals(methodName) || "pow".equals(methodName)) {
|
||||
if (x.getParameters().size() != 2) {
|
||||
return false;
|
||||
}
|
||||
@@ -992,6 +998,10 @@ public class SQLEvalVisitorUtils {
|
||||
return (Byte) val;
|
||||
}
|
||||
|
||||
if (val instanceof String) {
|
||||
return Byte.parseByte((String) val);
|
||||
}
|
||||
|
||||
return ((Number) val).byteValue();
|
||||
}
|
||||
|
||||
@@ -1171,42 +1181,6 @@ public class SQLEvalVisitorUtils {
|
||||
return BigDecimal.valueOf(((Number) val).longValue());
|
||||
}
|
||||
|
||||
public static Object sum(Object a, Object b) {
|
||||
if (a == null) {
|
||||
return b;
|
||||
}
|
||||
|
||||
if (b == null) {
|
||||
return a;
|
||||
}
|
||||
|
||||
if (a instanceof BigDecimal || b instanceof BigDecimal) {
|
||||
return castToDecimal(a).add(castToDecimal(b));
|
||||
}
|
||||
|
||||
if (a instanceof BigInteger || b instanceof BigInteger) {
|
||||
return castToBigInteger(a).add(castToBigInteger(b));
|
||||
}
|
||||
|
||||
if (a instanceof Long || b instanceof Long) {
|
||||
return castToLong(a) + castToLong(b);
|
||||
}
|
||||
|
||||
if (a instanceof Integer || b instanceof Integer) {
|
||||
return castToInteger(a) + castToInteger(b);
|
||||
}
|
||||
|
||||
if (a instanceof Short || b instanceof Short) {
|
||||
return castToShort(a) + castToShort(b);
|
||||
}
|
||||
|
||||
if (a instanceof Byte || b instanceof Byte) {
|
||||
return castToByte(a) + castToByte(b);
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException(a.getClass() + " and " + b.getClass() + " not supported.");
|
||||
}
|
||||
|
||||
public static Object div(Object a, Object b) {
|
||||
if (a == null || b == null) {
|
||||
return null;
|
||||
@@ -1216,6 +1190,14 @@ public class SQLEvalVisitorUtils {
|
||||
return castToDecimal(a).divide(castToDecimal(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 BigInteger || b instanceof BigInteger) {
|
||||
return castToBigInteger(a).divide(castToBigInteger(b));
|
||||
}
|
||||
@@ -1445,6 +1427,10 @@ public class SQLEvalVisitorUtils {
|
||||
return a;
|
||||
}
|
||||
|
||||
if (a == EVAL_VALUE_NULL || b == EVAL_VALUE_NULL) {
|
||||
return EVAL_VALUE_NULL;
|
||||
}
|
||||
|
||||
if (a instanceof BigDecimal || b instanceof BigDecimal) {
|
||||
return castToDecimal(a).add(castToDecimal(b));
|
||||
}
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLObject;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
|
||||
|
||||
public class SQLShardingVisitor extends SQLASTVisitorAdapter {
|
||||
|
||||
public void postVisit(SQLObject x) {
|
||||
}
|
||||
|
||||
public void preVisit(SQLObject x) {
|
||||
}
|
||||
|
||||
public boolean visit(SQLInsertStatement x) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user