Search in sources :

Example 91 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class Left method eval.

public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
    if (x.getParameters().size() != 2) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    SQLExpr param0 = x.getParameters().get(0);
    SQLExpr param1 = x.getParameters().get(1);
    param0.accept(visitor);
    param1.accept(visitor);
    Object param0Value = param0.getAttributes().get(EVAL_VALUE);
    Object param1Value = param1.getAttributes().get(EVAL_VALUE);
    if (param0Value == null || param1Value == null) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    String strValue = param0Value.toString();
    int intValue = SQLEvalVisitorUtils.castToInteger(param1Value);
    if (intValue > strValue.length()) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    String result = strValue.substring(0, intValue);
    return result;
}
Also used : SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 92 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class Lpad method eval.

public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
    List<SQLExpr> params = x.getParameters();
    int paramSize = params.size();
    if (paramSize != 3) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    SQLExpr param0 = params.get(0);
    SQLExpr param1 = params.get(1);
    SQLExpr param2 = params.get(2);
    param0.accept(visitor);
    param1.accept(visitor);
    param2.accept(visitor);
    Object param0Value = param0.getAttributes().get(EVAL_VALUE);
    Object param1Value = param1.getAttributes().get(EVAL_VALUE);
    Object param2Value = param2.getAttributes().get(EVAL_VALUE);
    if (param0Value == null || param1Value == null || param2Value == null) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    String strValue0 = param0Value.toString();
    int len = ((Number) param1Value).intValue();
    String strValue1 = param2Value.toString();
    String result = strValue0;
    if (result.length() > len) {
        return result.substring(0, len);
    }
    while (result.length() < len) {
        result = strValue1 + result;
    }
    return result;
}
Also used : SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 93 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class Ltrim method eval.

public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
    if (x.getParameters().size() != 1) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    SQLExpr param0 = x.getParameters().get(0);
    param0.accept(visitor);
    Object param0Value = param0.getAttributes().get(EVAL_VALUE);
    if (param0Value == null) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    String strValue = param0Value.toString();
    int index = -1;
    for (int i = 0; i < strValue.length(); ++i) {
        if (!Character.isWhitespace(strValue.charAt(i))) {
            index = i;
            break;
        }
    }
    if (index <= 0) {
        return strValue;
    } else {
        return strValue.substring(index);
    }
}
Also used : SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 94 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class OneParamFunctions method eval.

public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
    if (x.getParameters().size() == 0) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    SQLExpr param = x.getParameters().get(0);
    param.accept(visitor);
    Object paramValue = param.getAttributes().get(EVAL_VALUE);
    if (paramValue == null) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    if (paramValue == EVAL_VALUE_NULL) {
        return EVAL_VALUE_NULL;
    }
    String method = x.getMethodName();
    if ("md5".equalsIgnoreCase(method)) {
        String text = paramValue.toString();
        return Utils.md5(text);
    }
    if ("bit_count".equalsIgnoreCase(method)) {
        if (paramValue instanceof BigInteger) {
            return ((BigInteger) paramValue).bitCount();
        }
        if (paramValue instanceof BigDecimal) {
            BigDecimal decimal = (BigDecimal) paramValue;
            BigInteger bigInt = decimal.setScale(0, BigDecimal.ROUND_HALF_UP).toBigInteger();
            return bigInt.bitCount();
        }
        Long val = SQLEvalVisitorUtils.castToLong(paramValue);
        return Long.bitCount(val);
    }
    if ("soundex".equalsIgnoreCase(method)) {
        String text = paramValue.toString();
        return soundex(text);
    }
    if ("space".equalsIgnoreCase(method)) {
        int intVal = SQLEvalVisitorUtils.castToInteger(paramValue);
        char[] chars = new char[intVal];
        for (int i = 0; i < chars.length; ++i) {
            chars[i] = ' ';
        }
        return new String(chars);
    }
    throw new UnsupportedOperationException(method);
}
Also used : BigInteger(java.math.BigInteger) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) BigDecimal(java.math.BigDecimal)

Example 95 with SQLExpr

use of com.alibaba.druid.sql.ast.SQLExpr in project druid by alibaba.

the class WallVisitorUtils method checkSelelct.

public static void checkSelelct(WallVisitor visitor, SQLSelectQueryBlock x) {
    for (SQLSelectItem item : x.getSelectList()) {
        item.setParent(x);
    }
    if (x.getInto() != null) {
        checkReadOnly(visitor, x.getInto());
    }
    if (!visitor.getConfig().isSelectIntoAllow() && x.getInto() != null) {
        addViolation(visitor, ErrorCode.SELECT_INTO_NOT_ALLOW, "select into not allow", x);
        return;
    }
    if (x.getFrom() != null) {
        x.getFrom().setParent(x);
    }
    SQLExpr where = x.getWhere();
    if (where != null) {
        where.setParent(x);
        checkCondition(visitor, x.getWhere());
        Object whereValue = getConditionValue(visitor, where, visitor.getConfig().isSelectWhereAlwayTrueCheck());
        if (Boolean.TRUE == whereValue) {
            if (visitor.getConfig().isSelectWhereAlwayTrueCheck() && visitor.isSqlEndOfComment() && !isSimpleConstExpr(where)) {
                // 简单表达式
                addViolation(visitor, ErrorCode.ALWAYS_TRUE, "select alway true condition not allow", x);
            }
        }
    }
    checkSelectForMultiTenant(visitor, x);
// checkConditionForMultiTenant(visitor, x.getWhere(), x);
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)225 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)42 SQLName (com.alibaba.druid.sql.ast.SQLName)33 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)30 ParserException (com.alibaba.druid.sql.parser.ParserException)23 SQLObject (com.alibaba.druid.sql.ast.SQLObject)22 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)17 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)16 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)13 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)13 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)12 ArrayList (java.util.ArrayList)12 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)11 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)10 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)10 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)10 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)8 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)8 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)7 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)7