Search in sources :

Example 11 with SQLVariantRefExpr

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

the class ExportParameterVisitorUtils method exportParameter.

public static SQLExpr exportParameter(final List<Object> parameters, final SQLExpr param) {
    Object value = null;
    boolean replace = false;
    if (param instanceof SQLCharExpr) {
        value = ((SQLCharExpr) param).getText();
        replace = true;
    }
    if (param instanceof SQLBooleanExpr) {
        value = ((SQLBooleanExpr) param).getValue();
        replace = true;
    }
    if (param instanceof SQLNumericLiteralExpr) {
        value = ((SQLNumericLiteralExpr) param).getNumber();
        replace = true;
    }
    if (replace) {
        SQLObject parent = param.getParent();
        if (parent != null) {
            List<SQLObject> mergedList = (List<SQLObject>) parent.getAttribute(ParameterizedOutputVisitorUtils.ATTR_MERGED);
            if (mergedList != null) {
                List<Object> mergedListParams = new ArrayList<Object>(mergedList.size() + 1);
                for (int i = 0; i < mergedList.size(); ++i) {
                    SQLObject item = mergedList.get(i);
                    if (item instanceof SQLBinaryOpExpr) {
                        SQLBinaryOpExpr binaryOpItem = (SQLBinaryOpExpr) item;
                        exportParameter(mergedListParams, binaryOpItem.getRight());
                    }
                }
                if (mergedListParams.size() > 0) {
                    mergedListParams.add(0, value);
                    value = mergedListParams;
                }
            }
        }
        parameters.add(value);
        return new SQLVariantRefExpr("?");
    }
    return param;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) ArrayList(java.util.ArrayList) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLObject(com.alibaba.druid.sql.ast.SQLObject) ArrayList(java.util.ArrayList) List(java.util.List) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 12 with SQLVariantRefExpr

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

the class ParameterizedOutputVisitorUtils method mergeEqual.

private static boolean mergeEqual(SQLExpr a, SQLExpr b) {
    if (!(a instanceof SQLBinaryOpExpr)) {
        return false;
    }
    if (!(b instanceof SQLBinaryOpExpr)) {
        return false;
    }
    SQLBinaryOpExpr binaryA = (SQLBinaryOpExpr) a;
    SQLBinaryOpExpr binaryB = (SQLBinaryOpExpr) b;
    if (binaryA.getOperator() != SQLBinaryOperator.Equality) {
        return false;
    }
    if (binaryB.getOperator() != SQLBinaryOperator.Equality) {
        return false;
    }
    if (!(binaryA.getRight() instanceof SQLLiteralExpr || binaryA.getRight() instanceof SQLVariantRefExpr)) {
        return false;
    }
    if (!(binaryB.getRight() instanceof SQLLiteralExpr || binaryB.getRight() instanceof SQLVariantRefExpr)) {
        return false;
    }
    return binaryA.getLeft().toString().equals(binaryB.getLeft().toString());
}
Also used : SQLLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLLiteralExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 13 with SQLVariantRefExpr

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

the class SQLStatementParser method parseCall.

public SQLCallStatement parseCall() {
    boolean brace = false;
    if (lexer.token() == Token.LBRACE) {
        lexer.nextToken();
        brace = true;
    }
    SQLCallStatement stmt = new SQLCallStatement(getDbType());
    if (lexer.token() == Token.QUES) {
        lexer.nextToken();
        accept(Token.EQ);
        stmt.setOutParameter(new SQLVariantRefExpr("?"));
    }
    acceptIdentifier("CALL");
    stmt.setProcedureName(exprParser.name());
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        exprParser.exprList(stmt.getParameters(), stmt);
        accept(Token.RPAREN);
    }
    if (brace) {
        accept(Token.RBRACE);
        stmt.setBrace(true);
    }
    return stmt;
}
Also used : SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)

Example 14 with SQLVariantRefExpr

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

the class WallVisitorUtils method check.

public static void check(WallVisitor visitor, SQLSelectItem x) {
    SQLExpr expr = x.getExpr();
    if (expr instanceof SQLVariantRefExpr) {
        if (!isTopSelectItem(expr) && "@".equals(((SQLVariantRefExpr) expr).getName())) {
            addViolation(visitor, ErrorCode.EVIL_NAME, "@ not allow", x);
        }
    }
    if (visitor.getConfig().isSelectAllColumnAllow()) {
        return;
    }
    if (//
    expr instanceof SQLAllColumnExpr && x.getParent() instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getParent();
        SQLTableSource from = queryBlock.getFrom();
        if (from instanceof SQLExprTableSource) {
            addViolation(visitor, ErrorCode.SELECT_NOT_ALLOW, "'SELECT *' not allow", x);
        }
    }
}
Also used : SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 15 with SQLVariantRefExpr

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

the class Demo1 method test_0.

public void test_0() throws Exception {
    String sql = "select * from user where uid = ? and uname = ?";
    List<Object> parameters = new ArrayList<Object>();
    parameters.add(1);
    parameters.add("wenshao");
    SQLStatementParser parser = new OracleStatementParser(sql);
    //
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement first = (SQLStatement) stmtList.get(0);
    GetVariantVisitor variantVisitor = new GetVariantVisitor();
    first.accept(variantVisitor);
    SQLVariantRefExpr firstVar = variantVisitor.getVariantList().get(0);
    int varIndex = (Integer) firstVar.getAttribute("varIndex");
    Integer param = (Integer) parameters.get(varIndex);
    String tableName;
    if (param.intValue() == 1) {
        tableName = "user_1";
    } else {
        tableName = "user_x";
    }
    MyOracleVisitor visitor = new MyOracleVisitor(tableName);
    first.accept(visitor);
    String realSql = SQLUtils.toOracleString(first);
    System.out.println(realSql);
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) ArrayList(java.util.ArrayList) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)

Aggregations

SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)22 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)11 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)9 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)5 Test (org.junit.Test)5 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)4 ParserException (com.alibaba.druid.sql.parser.ParserException)4 ArrayList (java.util.ArrayList)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)3 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLUnaryExpr (com.alibaba.druid.sql.ast.expr.SQLUnaryExpr)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)2 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)2 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)2 SQLAssignItem (com.alibaba.druid.sql.ast.statement.SQLAssignItem)2 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)2