Search in sources :

Example 1 with SQLEvalVisitor

use of com.alibaba.druid.sql.visitor.SQLEvalVisitor in project sharding-jdbc by dangdangdotcom.

the class ParseContext method evalExpression.

private ValuePair evalExpression(final DatabaseType databaseType, final SQLObject sqlObject, final List<Object> parameters) {
    if (sqlObject instanceof SQLMethodInvokeExpr) {
        // TODO 解析函数中的sharingValue不支持
        return null;
    }
    SQLEvalVisitor visitor;
    switch(databaseType.name().toLowerCase()) {
        case JdbcUtils.MYSQL:
        case JdbcUtils.H2:
            visitor = new MySQLEvalVisitor();
            break;
        default:
            visitor = SQLEvalVisitorUtils.createEvalVisitor(databaseType.name());
    }
    visitor.setParameters(parameters);
    sqlObject.accept(visitor);
    Object value = SQLEvalVisitorUtils.getValue(sqlObject);
    if (null == value) {
        // TODO 对于NULL目前解析为空字符串,此处待考虑解决方法
        return null;
    }
    Comparable<?> finalValue;
    if (value instanceof Comparable<?>) {
        finalValue = (Comparable<?>) value;
    } else {
        finalValue = "";
    }
    Integer index = (Integer) sqlObject.getAttribute(MySQLEvalVisitor.EVAL_VAR_INDEX);
    if (null == index) {
        index = -1;
    }
    return new ValuePair(finalValue, index);
}
Also used : SQLEvalVisitor(com.alibaba.druid.sql.visitor.SQLEvalVisitor) MySQLEvalVisitor(com.dangdang.ddframe.rdb.sharding.parser.visitor.basic.mysql.MySQLEvalVisitor) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) MySQLEvalVisitor(com.dangdang.ddframe.rdb.sharding.parser.visitor.basic.mysql.MySQLEvalVisitor) SQLObject(com.alibaba.druid.sql.ast.SQLObject)

Example 2 with SQLEvalVisitor

use of com.alibaba.druid.sql.visitor.SQLEvalVisitor in project druid by alibaba.

the class WallVisitorUtils method eval.

public static Object eval(WallVisitor wallVisitor, String dbType, SQLObject sqlObject, List<Object> parameters) {
    SQLEvalVisitor visitor = SQLEvalVisitorUtils.createEvalVisitor(dbType);
    visitor.setParameters(parameters);
    visitor.registerFunction("rand", Nil.instance);
    visitor.registerFunction("sin", Nil.instance);
    visitor.registerFunction("cos", Nil.instance);
    visitor.registerFunction("asin", Nil.instance);
    visitor.registerFunction("acos", Nil.instance);
    sqlObject.accept(visitor);
    if (sqlObject instanceof SQLNumericLiteralExpr) {
        return ((SQLNumericLiteralExpr) sqlObject).getNumber();
    }
    return getValueFromAttributes(wallVisitor, sqlObject);
}
Also used : SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLEvalVisitor(com.alibaba.druid.sql.visitor.SQLEvalVisitor)

Aggregations

SQLEvalVisitor (com.alibaba.druid.sql.visitor.SQLEvalVisitor)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 MySQLEvalVisitor (com.dangdang.ddframe.rdb.sharding.parser.visitor.basic.mysql.MySQLEvalVisitor)1