Search in sources :

Example 11 with SQLMethodInvokeExpr

use of com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr in project druid by alibaba.

the class OracleExprParser method methodRest.

@Override
protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) {
    if (acceptLPAREN) {
        accept(Token.LPAREN);
    }
    if (lexer.token() == Token.PLUS) {
        lexer.nextToken();
        accept(Token.RPAREN);
        return new OracleOuterExpr(expr);
    }
    if (expr instanceof SQLIdentifierExpr) {
        String methodName = ((SQLIdentifierExpr) expr).getName();
        SQLMethodInvokeExpr methodExpr = new SQLMethodInvokeExpr(methodName);
        if ("trim".equalsIgnoreCase(methodName)) {
            if (//
            identifierEquals("LEADING") || //
            identifierEquals("TRAILING") || identifierEquals("BOTH")) {
                methodExpr.putAttribute("trim_option", lexer.stringVal());
                lexer.nextToken();
            }
            if (lexer.token() != Token.FROM) {
                SQLExpr trim_character = this.primary();
                trim_character.setParent(methodExpr);
                if (trim_character instanceof SQLCharExpr) {
                    methodExpr.putAttribute("trim_character", trim_character);
                } else {
                    methodExpr.getParameters().add(trim_character);
                }
            }
            if (lexer.token() == Token.FROM) {
                lexer.nextToken();
                SQLExpr trim_source = this.expr();
                methodExpr.addParameter(trim_source);
            }
            accept(Token.RPAREN);
            return primaryRest(methodExpr);
        }
    }
    return super.methodRest(expr, false);
}
Also used : OracleOuterExpr(com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleOuterExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 12 with SQLMethodInvokeExpr

use of com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr in project druid by alibaba.

the class Unhex method eval.

public Object eval(SQLEvalVisitor visitor, SQLMethodInvokeExpr x) {
    if (x.getParameters().size() != 1) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    SQLExpr param0 = x.getParameters().get(0);
    if (param0 instanceof SQLMethodInvokeExpr) {
        SQLMethodInvokeExpr paramMethodExpr = (SQLMethodInvokeExpr) param0;
        if (paramMethodExpr.getMethodName().equalsIgnoreCase("hex")) {
            SQLExpr subParamExpr = paramMethodExpr.getParameters().get(0);
            subParamExpr.accept(visitor);
            Object param0Value = subParamExpr.getAttributes().get(EVAL_VALUE);
            if (param0Value == null) {
                x.putAttribute(EVAL_EXPR, subParamExpr);
                return SQLEvalVisitor.EVAL_ERROR;
            }
            return param0Value;
        }
    }
    param0.accept(visitor);
    Object param0Value = param0.getAttributes().get(EVAL_VALUE);
    if (param0Value == null) {
        return SQLEvalVisitor.EVAL_ERROR;
    }
    if (param0Value instanceof String) {
        byte[] bytes = HexBin.decode((String) param0Value);
        if (bytes == null) {
            return SQLEvalVisitor.EVAL_VALUE_NULL;
        }
        String result;
        try {
            result = new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
        return result;
    }
    return SQLEvalVisitor.EVAL_ERROR;
}
Also used : SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 13 with SQLMethodInvokeExpr

use of com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr in project druid by alibaba.

the class WallVisitorUtils method check.

public static boolean check(WallVisitor visitor, SQLBinaryOpExpr x) {
    if (x.getOperator() == SQLBinaryOperator.BooleanOr || x.getOperator() == SQLBinaryOperator.BooleanAnd) {
        List<SQLExpr> groupList = SQLUtils.split(x);
        for (SQLExpr item : groupList) {
            item.accept(visitor);
        }
        return false;
    }
    if (x.getOperator() == SQLBinaryOperator.Add || x.getOperator() == SQLBinaryOperator.Concat) {
        List<SQLExpr> groupList = SQLUtils.split(x);
        if (groupList.size() >= 4) {
            int chrCount = 0;
            for (int i = 0; i < groupList.size(); ++i) {
                SQLExpr item = groupList.get(i);
                if (item instanceof SQLMethodInvokeExpr) {
                    SQLMethodInvokeExpr methodExpr = (SQLMethodInvokeExpr) item;
                    String methodName = methodExpr.getMethodName().toLowerCase();
                    if ("chr".equals(methodName) || "char".equals(methodName)) {
                        if (methodExpr.getParameters().get(0) instanceof SQLLiteralExpr) {
                            chrCount++;
                        }
                    }
                } else if (item instanceof SQLCharExpr) {
                    if (((SQLCharExpr) item).getText().length() > 5) {
                        chrCount = 0;
                        continue;
                    }
                }
                if (chrCount >= 4) {
                    addViolation(visitor, ErrorCode.EVIL_CONCAT, "evil concat", x);
                    break;
                }
            }
        }
    }
    return true;
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint)

Example 14 with SQLMethodInvokeExpr

use of com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr in project druid by alibaba.

the class SQLASTVisitorAdapterTest method test_adapter.

public void test_adapter() throws Exception {
    SQLASTVisitorAdapter adapter = new SQLASTVisitorAdapter();
    new SQLBinaryOpExpr().accept(adapter);
    new SQLInListExpr().accept(adapter);
    new SQLSelectQueryBlock().accept(adapter);
    new SQLDropTableStatement().accept(adapter);
    new SQLCreateTableStatement().accept(adapter);
    new SQLDeleteStatement().accept(adapter);
    new SQLCurrentOfCursorExpr().accept(adapter);
    new SQLInsertStatement().accept(adapter);
    new SQLUpdateStatement().accept(adapter);
    new SQLNotNullConstraint().accept(adapter);
    new SQLMethodInvokeExpr().accept(adapter);
    new SQLCallStatement().accept(adapter);
    new SQLSomeExpr().accept(adapter);
    new SQLAnyExpr().accept(adapter);
    new SQLAllExpr().accept(adapter);
    new SQLDefaultExpr().accept(adapter);
    new SQLCommentStatement().accept(adapter);
    new SQLDropViewStatement().accept(adapter);
    new SQLSavePointStatement().accept(adapter);
    new SQLReleaseSavePointStatement().accept(adapter);
    new SQLCreateDatabaseStatement().accept(adapter);
    new SQLAlterTableDropIndex().accept(adapter);
    new SQLOver().accept(adapter);
    new SQLWithSubqueryClause().accept(adapter);
    new SQLAlterTableAlterColumn().accept(adapter);
    new SQLAlterTableStatement().accept(adapter);
    new SQLAlterTableDisableConstraint().accept(adapter);
    new SQLAlterTableEnableConstraint().accept(adapter);
    new SQLColumnCheck().accept(adapter);
    new SQLExprHint().accept(adapter);
    new SQLAlterTableDropConstraint().accept(adapter);
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLAlterTableDropIndex(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex) SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) SQLAllExpr(com.alibaba.druid.sql.ast.expr.SQLAllExpr) SQLReleaseSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement) SQLNotNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLDropTableStatement(com.alibaba.druid.sql.ast.statement.SQLDropTableStatement) SQLCreateDatabaseStatement(com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement) SQLDropViewStatement(com.alibaba.druid.sql.ast.statement.SQLDropViewStatement) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAlterTableDisableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint) SQLSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLSavePointStatement) SQLAlterTableDropConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLCallStatement(com.alibaba.druid.sql.ast.statement.SQLCallStatement) SQLSomeExpr(com.alibaba.druid.sql.ast.expr.SQLSomeExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLExprHint(com.alibaba.druid.sql.ast.statement.SQLExprHint) SQLWithSubqueryClause(com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLCurrentOfCursorExpr(com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr) SQLCommentStatement(com.alibaba.druid.sql.ast.statement.SQLCommentStatement) SQLColumnCheck(com.alibaba.druid.sql.ast.statement.SQLColumnCheck) SQLAnyExpr(com.alibaba.druid.sql.ast.expr.SQLAnyExpr) SQLAlterTableAlterColumn(com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLDefaultExpr(com.alibaba.druid.sql.ast.expr.SQLDefaultExpr) SQLAlterTableEnableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint)

Aggregations

SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)14 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)10 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 ParserException (com.alibaba.druid.sql.parser.ParserException)3 SQLName (com.alibaba.druid.sql.ast.SQLName)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)2 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)2 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)1 MockResultSet (com.alibaba.druid.mock.MockResultSet)1 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)1 SQLCommentHint (com.alibaba.druid.sql.ast.SQLCommentHint)1 SQLOver (com.alibaba.druid.sql.ast.SQLOver)1