diff --git a/src/main/java/com/alibaba/druid/sql/SQLUtils.java b/src/main/java/com/alibaba/druid/sql/SQLUtils.java index 81c0dab8c8..106a405772 100644 --- a/src/main/java/com/alibaba/druid/sql/SQLUtils.java +++ b/src/main/java/com/alibaba/druid/sql/SQLUtils.java @@ -5,13 +5,10 @@ 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.dialect.mysql.parser.MySqlExprParser; -import com.alibaba.druid.sql.dialect.mysql.parser.MySqlLexer; import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor; import com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor; import com.alibaba.druid.sql.dialect.postgresql.visitor.PGOutputVisitor; import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerOutputVisitor; -import com.alibaba.druid.sql.parser.Lexer; import com.alibaba.druid.sql.parser.ParserException; import com.alibaba.druid.sql.parser.SQLExprParser; import com.alibaba.druid.sql.parser.SQLParserUtils; @@ -21,6 +18,21 @@ import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; import com.alibaba.druid.util.JdbcUtils; public class SQLUtils { + public static String toSQLString(SQLObject sqlObject, String dbType) { + if (JdbcUtils.MYSQL.equals(dbType)) { + return toMySqlString(sqlObject); + } + + if (JdbcUtils.ORACLE.equals(dbType)) { + return toOracleString(sqlObject); + } + + if (JdbcUtils.POSTGRESQL.equals(dbType)) { + return toPGString(sqlObject); + } + + return toSQLServerString(sqlObject); + } public static String toSQLString(SQLObject sqlObject) { StringBuilder out = new StringBuilder(); @@ -39,17 +51,7 @@ public class SQLUtils { } public static SQLExpr toMySqlExpr(String sql) { - MySqlLexer lexer = new MySqlLexer(sql); - lexer.nextToken(); - - MySqlExprParser parser = new MySqlExprParser(lexer); - SQLExpr expr = parser.expr(); - - if (lexer.token() != Token.EOF) { - throw new ParserException("illegal sql expr : " + sql); - } - - return expr; + return toSQLExpr(sql, JdbcUtils.MYSQL); } public static String formatMySql(String sql) { @@ -87,21 +89,22 @@ public class SQLUtils { public static String formatPGSql(String sql) { return format(sql, JdbcUtils.POSTGRESQL); } - - public static SQLExpr toSQLExpr(String sql) { - Lexer lexer = new Lexer(sql); - lexer.nextToken(); - - SQLExprParser parser = new SQLExprParser(lexer); + + public static SQLExpr toSQLExpr(String sql, String dbType) { + SQLExprParser parser = SQLParserUtils.createExprParser(sql, dbType); SQLExpr expr = parser.expr(); - if (lexer.token() != Token.EOF) { + if (parser.getLexer().token() != Token.EOF) { throw new ParserException("illegal sql expr : " + sql); } return expr; } + public static SQLExpr toSQLExpr(String sql) { + return toSQLExpr(sql, null); + } + public static String format(String sql, String dbType) { SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType); List statementList = parser.parseStatementList(); diff --git a/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java new file mode 100644 index 0000000000..bbb98b1ddd --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/dialect/mysql/visitor/MySqlEvalVisitorImpl.java @@ -0,0 +1,53 @@ +package com.alibaba.druid.sql.dialect.mysql.visitor; + +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; +import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr; +import com.alibaba.druid.sql.visitor.SQLEvalVisitor; +import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils; + +public class MySqlEvalVisitorImpl extends MySqlASTVisitorAdapter implements SQLEvalVisitor { + + private List parameters = new ArrayList(); + + private int variantIndex = -1; + + public MySqlEvalVisitorImpl(){ + this(new ArrayList(1)); + } + + public MySqlEvalVisitorImpl(List parameters){ + this.parameters = parameters; + } + + public List getParameters() { + return parameters; + } + + public void setParameters(List parameters) { + this.parameters = parameters; + } + + public boolean visit(SQLCharExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } + + public int incrementAndGetVariantIndex() { + return ++variantIndex; + } + + public int getVariantIndex() { + return variantIndex; + } + + public boolean visit(SQLVariantRefExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } + + public boolean visit(SQLBinaryOpExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } +} diff --git a/src/main/java/com/alibaba/druid/sql/parser/SQLParserUtils.java b/src/main/java/com/alibaba/druid/sql/parser/SQLParserUtils.java index f19c203df5..0587fa0833 100644 --- a/src/main/java/com/alibaba/druid/sql/parser/SQLParserUtils.java +++ b/src/main/java/com/alibaba/druid/sql/parser/SQLParserUtils.java @@ -1,8 +1,12 @@ package com.alibaba.druid.sql.parser; +import com.alibaba.druid.sql.dialect.mysql.parser.MySqlExprParser; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; +import com.alibaba.druid.sql.dialect.oracle.parser.OracleExprParser; import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser; +import com.alibaba.druid.sql.dialect.postgresql.parser.PGExprParser; import com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser; +import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerExprParser; import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser; import com.alibaba.druid.util.JdbcUtils; @@ -27,4 +31,24 @@ public class SQLParserUtils { return new SQLStatementParser(sql); } + + public static SQLExprParser createExprParser(String sql, String dbType) { + if (JdbcUtils.ORACLE.equals(dbType)) { + return new OracleExprParser(sql); + } + + if (JdbcUtils.MYSQL.equals(dbType)) { + return new MySqlExprParser(sql); + } + + if (JdbcUtils.POSTGRESQL.equals(dbType)) { + return new PGExprParser(sql); + } + + if (JdbcUtils.SQL_SERVER.equals(dbType)) { + return new SQLServerExprParser(sql); + } + + return new SQLExprParser(sql); + } } diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitor.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitor.java new file mode 100644 index 0000000000..a53412a214 --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitor.java @@ -0,0 +1,16 @@ +package com.alibaba.druid.sql.visitor; + +import java.util.List; + +public interface SQLEvalVisitor extends SQLASTVisitor { + + public static final String EVAL_VAR_INDEX = "eval.varIndex"; + + public static final String EVAL_VALUE = "eval.value"; + + List getParameters(); + + void setParameters(List parameters); + + int incrementAndGetVariantIndex(); +} diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorImpl.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorImpl.java new file mode 100644 index 0000000000..7d79df348f --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorImpl.java @@ -0,0 +1,51 @@ +package com.alibaba.druid.sql.visitor; + +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr; +import com.alibaba.druid.sql.ast.expr.SQLCharExpr; +import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr; + +public class SQLEvalVisitorImpl extends SQLASTVisitorAdapter implements SQLEvalVisitor { + + private List parameters = new ArrayList(); + + private int variantIndex = -1; + + public SQLEvalVisitorImpl(){ + this(new ArrayList(1)); + } + + public SQLEvalVisitorImpl(List parameters){ + this.parameters = parameters; + } + + public List getParameters() { + return parameters; + } + + public void setParameters(List parameters) { + this.parameters = parameters; + } + + public boolean visit(SQLCharExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } + + public int incrementAndGetVariantIndex() { + return ++variantIndex; + } + + public int getVariantIndex() { + return variantIndex; + } + + public boolean visit(SQLVariantRefExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } + + public boolean visit(SQLBinaryOpExpr x) { + return SQLEvalVisitorUtils.visit(this, x); + } +} diff --git a/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java new file mode 100644 index 0000000000..51773120cf --- /dev/null +++ b/src/main/java/com/alibaba/druid/sql/visitor/SQLEvalVisitorUtils.java @@ -0,0 +1,657 @@ +package com.alibaba.druid.sql.visitor; + +import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_VALUE; +import static com.alibaba.druid.sql.visitor.SQLEvalVisitor.EVAL_VAR_INDEX; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.RoundingMode; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Map; + +import com.alibaba.druid.DruidRuntimeException; +import com.alibaba.druid.sql.SQLUtils; +import com.alibaba.druid.sql.ast.SQLExpr; +import com.alibaba.druid.sql.ast.SQLObject; +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.SQLVariantRefExpr; +import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlEvalVisitorImpl; +import com.alibaba.druid.util.JdbcUtils; + +public class SQLEvalVisitorUtils { + + public static Object evalExpr(String dbType, String expr, Object... parameters) { + SQLExpr sqlExpr = SQLUtils.toSQLExpr(expr, dbType); + return eval(dbType, sqlExpr, parameters); + } + + public static Object evalExpr(String dbType, String expr, List parameters) { + SQLExpr sqlExpr = SQLUtils.toSQLExpr(expr); + return eval(dbType, sqlExpr, parameters); + } + + public static Object eval(String dbType, SQLObject sqlObject, Object... parameters) { + return eval(dbType, sqlObject, Arrays.asList(parameters)); + } + + public static Object eval(String dbType, SQLObject sqlObject, List parameters) { + SQLEvalVisitor visitor = createEvalVisitor(dbType); + visitor.setParameters(parameters); + sqlObject.accept(visitor); + + Object value = sqlObject.getAttributes().get(EVAL_VALUE); + if (value == null) { + if (!sqlObject.getAttributes().containsKey(EVAL_VALUE)) { + throw new DruidRuntimeException("eval error : " + SQLUtils.toSQLString(sqlObject, dbType)); + } + } + + return value; + } + + public static SQLEvalVisitor createEvalVisitor(String dbType) { + if (JdbcUtils.MYSQL.equals(dbType)) { + return new MySqlEvalVisitorImpl(); + } + + return new SQLEvalVisitorImpl(); + } + + public static boolean visit(SQLEvalVisitor visitor, SQLCharExpr x) { + x.putAttribute(EVAL_VALUE, x.getText()); + return true; + } + + public static boolean visit(SQLEvalVisitor visitor, SQLBinaryOpExpr x) { + x.getLeft().accept(visitor); + x.getRight().accept(visitor); + + if (!x.getLeft().getAttributes().containsKey(EVAL_VALUE)) { + return false; + } + + if (!x.getRight().getAttributes().containsKey(EVAL_VALUE)) { + return false; + } + + if (SQLBinaryOperator.Add.equals(x.getOperator())) { + Object value = _add(x.getLeft().getAttribute(EVAL_VALUE), x.getRight().getAttributes().get(EVAL_VALUE)); + x.putAttribute(EVAL_VALUE, value); + } + + return false; + } + + public static boolean visit(SQLEvalVisitor visitor, SQLVariantRefExpr x) { + Map attributes = x.getAttributes(); + Integer varIndex = (Integer) attributes.get(EVAL_VAR_INDEX); + if (varIndex == null) { + varIndex = visitor.incrementAndGetVariantIndex(); + x.putAttribute(EVAL_VAR_INDEX, varIndex); + } + + boolean containsValue = attributes.containsKey(EVAL_VALUE); + if (!containsValue) { + Object value = visitor.getParameters().get(varIndex.intValue()); + attributes.put(EVAL_VALUE, value); + } + + return false; + } + + public static Boolean _bool(Object val) { + if (val == null) { + return null; + } + + if (val instanceof Boolean) { + return (Boolean) val; + } + + if (val instanceof Number) { + return ((Number) val).intValue() == 1; + } + + throw new IllegalArgumentException(); + } + + public static String _string(Object val) { + Object value = val; + + if (value == null) { + return null; + } + + return value.toString(); + } + + public static Byte _byte(Object val) { + if (val == null) { + return null; + } + + if (val instanceof Byte) { + return (Byte) val; + } + + return ((Number) val).byteValue(); + } + + public static Short _short(Object val) { + if (val == null) { + return null; + } + + if (val instanceof Short) { + return (Short) val; + } + + return ((Number) val).shortValue(); + } + + public static Integer _int(Object val) { + if (val == null) { + return null; + } + + if (val instanceof Integer) { + return (Integer) val; + } + + return ((Number) val).intValue(); + } + + public static Long _long(Object val) { + if (val == null) { + return null; + } + + if (val instanceof Long) { + return (Long) val; + } + + return ((Number) val).longValue(); + } + + public static Float _float(Object val) { + if (val == null) { + return null; + } + + if (val instanceof Float) { + return (Float) val; + } + + return ((Number) val).floatValue(); + } + + public static Double _double(Object val) { + if (val == null) { + return null; + } + + if (val instanceof Double) { + return (Double) val; + } + + return ((Number) val).doubleValue(); + } + + public static BigInteger _bigInt(Object val) { + if (val == null) { + return null; + } + + if (val instanceof BigInteger) { + return (BigInteger) val; + } + + if (val instanceof String) { + return new BigInteger((String) val); + } + + return BigInteger.valueOf(((Number) val).longValue()); + } + + public static Date _date(Object val) { + if (val == null) { + return null; + } + + if (val instanceof Date) { + return (Date) val; + } + + if (val instanceof Number) { + return new Date(((Number) val).longValue()); + } + + if (val instanceof String) { + return _date((String) val); + } + + throw new DruidRuntimeException("can cast to date"); + } + + public static Date _date(String text) { + if (text == null || text.length() == 0) { + return null; + } + + String format; + + if (text.length() == "yyyy-MM-dd".length()) { + format = "yyyy-MM-dd"; + } else { + format = "yyyy-MM-dd HH:mm:ss"; + } + + try { + return new SimpleDateFormat(format).parse(text); + } catch (ParseException e) { + throw new DruidRuntimeException("format : " + format + ", value : " + text); + } + } + + public static BigDecimal _decimal(Object val) { + if (val == null) { + return null; + } + + if (val instanceof BigDecimal) { + return (BigDecimal) val; + } + + if (val instanceof String) { + return new BigDecimal((String) val); + } + + if (val instanceof Float) { + return new BigDecimal((Float) val); + } + + if (val instanceof Double) { + return new BigDecimal((Double) val); + } + + 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 _decimal(a).add(_decimal(b)); + } + + if (a instanceof BigInteger || b instanceof BigInteger) { + return _bigInt(a).add(_bigInt(b)); + } + + if (a instanceof Long || b instanceof Long) { + return _long(a) + _long(b); + } + + if (a instanceof Integer || b instanceof Integer) { + return _int(a) + _int(b); + } + + if (a instanceof Short || b instanceof Short) { + return _short(a) + _short(b); + } + + if (a instanceof Byte || b instanceof Byte) { + return _byte(a) + _byte(b); + } + + throw new IllegalArgumentException(); + } + + public static Object _div(Object a, Object b) { + if (a == null || b == null) { + return null; + } + + BigDecimal decimalA = _decimal(a); + BigDecimal decimalB = _decimal(b); + + try { + return decimalA.divide(decimalB); + } catch (ArithmeticException ex) { + return decimalA.divide(decimalB, 4, RoundingMode.CEILING); + } + } + + public static Object _div2(Object a, Object b) { + if (a == null || b == null) { + return null; + } + + if (a instanceof BigDecimal || b instanceof BigDecimal) { + return _decimal(a).divide(_decimal(b)); + } + + if (a instanceof BigInteger || b instanceof BigInteger) { + return _bigInt(a).divide(_bigInt(b)); + } + + if (a instanceof Long || b instanceof Long) { + return _long(a) / _long(b); + } + + if (a instanceof Integer || b instanceof Integer) { + return _int(a) / _int(b); + } + + if (a instanceof Short || b instanceof Short) { + return _short(a) / _short(b); + } + + if (a instanceof Byte || b instanceof Byte) { + return _byte(a) / _byte(b); + } + + throw new IllegalArgumentException(); + } + + public static boolean _gt(Object a, Object b) { + if (a == null) { + return false; + } + + if (b == null) { + return true; + } + + if (a instanceof BigDecimal || b instanceof BigDecimal) { + return _decimal(a).compareTo(_decimal(b)) > 0; + } + + if (a instanceof BigInteger || b instanceof BigInteger) { + return _bigInt(a).compareTo(_bigInt(b)) > 0; + } + + if (a instanceof Long || b instanceof Long) { + return _long(a) > _long(b); + } + + if (a instanceof Integer || b instanceof Integer) { + return _int(a) > _int(b); + } + + if (a instanceof Short || b instanceof Short) { + return _short(a) > _short(b); + } + + if (a instanceof Byte || b instanceof Byte) { + return _byte(a) > _byte(b); + } + + if (a instanceof Date || b instanceof Date) { + Date d1 = _date(a); + Date d2 = _date(b); + + if (d1 == d2) { + return false; + } + + if (d1 == null) { + return false; + } + + if (d2 == null) { + return true; + } + + return d1.compareTo(d2) > 0; + } + + throw new IllegalArgumentException(); + } + + public static boolean _gteq(Object a, Object b) { + if (_eq(a, b)) { + return true; + } + + return _gt(a, b); + } + + public static boolean _lt(Object a, Object b) { + if (a == null) { + return true; + } + + if (b == null) { + return false; + } + + if (a instanceof BigDecimal || b instanceof BigDecimal) { + return _decimal(a).compareTo(_decimal(b)) < 0; + } + + if (a instanceof BigInteger || b instanceof BigInteger) { + return _bigInt(a).compareTo(_bigInt(b)) < 0; + } + + if (a instanceof Long || b instanceof Long) { + return _long(a) < _long(b); + } + + if (a instanceof Integer || b instanceof Integer) { + return _int(a) < _int(b); + } + + if (a instanceof Short || b instanceof Short) { + return _short(a) < _short(b); + } + + if (a instanceof Byte || b instanceof Byte) { + return _byte(a) < _byte(b); + } + + if (a instanceof Date || b instanceof Date) { + Date d1 = _date(a); + Date d2 = _date(b); + + if (d1 == d2) { + return false; + } + + if (d1 == null) { + return true; + } + + if (d2 == null) { + return false; + } + + return d1.compareTo(d2) < 0; + } + + throw new IllegalArgumentException(); + } + + public static boolean _lteq(Object a, Object b) { + if (_eq(a, b)) { + return true; + } + + return _lt(a, b); + } + + public static boolean _eq(Object a, Object b) { + if (a == b) { + return true; + } + + if (a == null || b == null) { + return false; + } + + if (a.equals(b)) { + return true; + } + + if (a instanceof BigDecimal || b instanceof BigDecimal) { + return _decimal(a).compareTo(_decimal(b)) == 0; + } + + if (a instanceof BigInteger || b instanceof BigInteger) { + return _bigInt(a).compareTo(_bigInt(b)) == 0; + } + + if (a instanceof Long || b instanceof Long) { + return _long(a) == _long(b); + } + + if (a instanceof Integer || b instanceof Integer) { + return _int(a) == _int(b); + } + + if (a instanceof Short || b instanceof Short) { + return _short(a) == _short(b); + } + + if (a instanceof Byte || b instanceof Byte) { + return _byte(a) == _byte(b); + } + + if (a instanceof Date || b instanceof Date) { + Date d1 = _date(a); + Date d2 = _date(b); + + if (d1 == d2) { + return true; + } + + if (d1 == null || d2 == null) { + return false; + } + + return d1.equals(d2); + } + + throw new IllegalArgumentException(); + } + + public static Object _add(Object a, Object b) { + if (a == null) { + return b; + } + + if (b == null) { + return a; + } + + if (a instanceof BigDecimal || b instanceof BigDecimal) { + return _decimal(a).add(_decimal(b)); + } + + if (a instanceof BigInteger || b instanceof BigInteger) { + return _bigInt(a).add(_bigInt(b)); + } + + if (a instanceof Long || b instanceof Long) { + return _long(a) + _long(b); + } + + if (a instanceof Integer || b instanceof Integer) { + return _int(a) + _int(b); + } + + if (a instanceof Short || b instanceof Short) { + return _short(a) + _short(b); + } + + if (a instanceof Byte || b instanceof Byte) { + return _byte(a) + _byte(b); + } + + if (a instanceof String || b instanceof String) { + return _string(a) + _string(b); + } + + throw new IllegalArgumentException(); + } + + public static Object _sub(Object a, Object b) { + if (a == null) { + return null; + } + + if (b == null) { + return a; + } + + if (a instanceof BigDecimal || b instanceof BigDecimal) { + return _decimal(a).subtract(_decimal(b)); + } + + if (a instanceof BigInteger || b instanceof BigInteger) { + return _bigInt(a).subtract(_bigInt(b)); + } + + if (a instanceof Long || b instanceof Long) { + return _long(a) - _long(b); + } + + if (a instanceof Integer || b instanceof Integer) { + return _int(a) - _int(b); + } + + if (a instanceof Short || b instanceof Short) { + return _short(a) - _short(b); + } + + if (a instanceof Byte || b instanceof Byte) { + return _byte(a) - _byte(b); + } + + throw new IllegalArgumentException(); + } + + public static Object _multi(Object a, Object b) { + if (a == null || b == null) { + return null; + } + + if (a instanceof BigDecimal || b instanceof BigDecimal) { + return _decimal(a).multiply(_decimal(b)); + } + + if (a instanceof BigInteger || b instanceof BigInteger) { + return _bigInt(a).multiply(_bigInt(b)); + } + + if (a instanceof Long || b instanceof Long) { + return _long(a) * _long(b); + } + + if (a instanceof Integer || b instanceof Integer) { + return _int(a) * _int(b); + } + + if (a instanceof Short || b instanceof Short) { + return _short(a) * _short(b); + } + + if (a instanceof Byte || b instanceof Byte) { + return _byte(a) * _byte(b); + } + + throw new IllegalArgumentException(); + } +} diff --git a/src/test/java/com/alibaba/druid/bvt/sql/eval/EvalTest.java b/src/test/java/com/alibaba/druid/bvt/sql/eval/EvalTest.java new file mode 100644 index 0000000000..d3f3da6bd0 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/sql/eval/EvalTest.java @@ -0,0 +1,20 @@ +package com.alibaba.druid.bvt.sql.eval; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils; +import com.alibaba.druid.util.JdbcUtils; + +public class EvalTest extends TestCase { + + public void testEval() throws Exception { + Assert.assertEquals("A", SQLEvalVisitorUtils.evalExpr(JdbcUtils.MYSQL, "?", "A")); + Assert.assertEquals(123, SQLEvalVisitorUtils.evalExpr(JdbcUtils.MYSQL, "?", 123)); + } + + public void testEval_1() throws Exception { + Assert.assertEquals("AB", SQLEvalVisitorUtils.evalExpr(JdbcUtils.MYSQL, "? + ?", "A", "B")); + Assert.assertEquals(234, SQLEvalVisitorUtils.evalExpr(JdbcUtils.MYSQL, "? + ?", 123, 111)); + } +}