Search in sources :

Example 1 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLEvalVisitorTest method testVisit.

@Test
public void testVisit() throws Exception {
    SQLVariantRefExpr expr = new SQLVariantRefExpr("?");
    expr.setIndex(1);
    MySQLEvalVisitor visitor = new MySQLEvalVisitor();
    visitor.setParameters(Lists.<Object>newArrayList(1, 2));
    expr.accept(visitor);
    assertThat((Integer) SQLEvalVisitorUtils.getValue(expr), is(2));
    assertThat((Integer) expr.getAttribute(MySQLEvalVisitor.EVAL_VAR_INDEX), is(1));
}
Also used : SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) Test(org.junit.Test)

Example 2 with SQLVariantRefExpr

use of com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr in project sharding-jdbc by dangdangdotcom.

the class MySQLEvalVisitorTest method testVisitErrorIndex.

@Test
public void testVisitErrorIndex() throws Exception {
    SQLVariantRefExpr expr = new SQLVariantRefExpr("?");
    expr.setIndex(2);
    MySQLEvalVisitor visitor = new MySQLEvalVisitor();
    visitor.setParameters(Lists.<Object>newArrayList(1, 2));
    expr.accept(visitor);
    assertThat(SQLEvalVisitorUtils.getValue(expr), nullValue());
}
Also used : SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) Test(org.junit.Test)

Example 3 with SQLVariantRefExpr

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

the class MySqlMockExecuteHandlerImpl method executeQueryFromDual.

public ResultSet executeQueryFromDual(MockStatementBase statement, SQLSelectQueryBlock query) throws SQLException {
    MockResultSet rs = statement.getConnection().getDriver().createMockResultSet(statement);
    MockResultSetMetaData metaData = rs.getMockMetaData();
    Object[] row = new Object[query.getSelectList().size()];
    for (int i = 0, size = query.getSelectList().size(); i < size; ++i) {
        ColumnMetaData column = new ColumnMetaData();
        SQLSelectItem item = query.getSelectList().get(i);
        SQLExpr expr = item.getExpr();
        if (expr instanceof SQLIntegerExpr) {
            row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
            column.setColumnType(Types.INTEGER);
        } else if (expr instanceof SQLNumberExpr) {
            row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
            column.setColumnType(Types.DECIMAL);
        } else if (expr instanceof SQLCharExpr) {
            row[i] = ((SQLCharExpr) expr).getText();
            column.setColumnType(Types.VARCHAR);
        } else if (expr instanceof SQLNCharExpr) {
            row[i] = ((SQLNCharExpr) expr).getText();
            column.setColumnType(Types.NVARCHAR);
        } else if (expr instanceof SQLBooleanExpr) {
            row[i] = ((SQLBooleanExpr) expr).getValue();
            column.setColumnType(Types.NVARCHAR);
        } else if (expr instanceof SQLNullExpr) {
            row[i] = null;
        } else if (expr instanceof SQLMethodInvokeExpr) {
            SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
            if ("NOW".equalsIgnoreCase(methodInvokeExpr.getMethodName())) {
                row[i] = new Timestamp(System.currentTimeMillis());
            } else {
                throw new SQLException("TODO");
            }
        } else if (expr instanceof SQLVariantRefExpr) {
            SQLVariantRefExpr varExpr = (SQLVariantRefExpr) expr;
            int varIndex = varExpr.getIndex();
            if (statement instanceof MockPreparedStatement) {
                MockPreparedStatement mockPstmt = (MockPreparedStatement) statement;
                row[i] = mockPstmt.getParameters().get(varIndex);
            } else {
                row[i] = null;
            }
        } else {
            row[i] = null;
        }
        metaData.getColumns().add(column);
    }
    rs.getRows().add(row);
    return rs;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLException(java.sql.SQLException) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) MockResultSet(com.alibaba.druid.mock.MockResultSet) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) Timestamp(java.sql.Timestamp) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) ColumnMetaData(com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData)

Example 4 with SQLVariantRefExpr

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

the class MySqlSelectIntoParser method parseIntoArgs.

/**
     * parser the select into arguments
     * @return
     */
protected List<SQLExpr> parseIntoArgs() {
    List<SQLExpr> args = new ArrayList<SQLExpr>();
    if (lexer.token() == (Token.INTO)) {
        accept(Token.INTO);
        //lexer.nextToken();
        for (; ; ) {
            SQLExpr var = exprParser.primary();
            if (var instanceof SQLIdentifierExpr) {
                var = new SQLVariantRefExpr(((SQLIdentifierExpr) var).getName());
            }
            args.add(var);
            if (lexer.token() == Token.COMMA) {
                accept(Token.COMMA);
                continue;
            } else {
                break;
            }
        }
    }
    return args;
}
Also used : ArrayList(java.util.ArrayList) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 5 with SQLVariantRefExpr

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

the class MySqlExprParser method primaryRest.

public final SQLExpr primaryRest(SQLExpr expr) {
    if (expr == null) {
        throw new IllegalArgumentException("expr");
    }
    if (lexer.token() == Token.LITERAL_CHARS) {
        if (expr instanceof SQLIdentifierExpr) {
            SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
            String ident = identExpr.getName();
            if (ident.equalsIgnoreCase("x")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                expr = new SQLHexExpr(charValue);
                return primaryRest(expr);
            } else if (ident.equalsIgnoreCase("b")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                expr = new SQLBinaryExpr(charValue);
                return primaryRest(expr);
            } else if (ident.startsWith("_")) {
                String charValue = lexer.stringVal();
                lexer.nextToken();
                MySqlCharExpr mysqlCharExpr = new MySqlCharExpr(charValue);
                mysqlCharExpr.setCharset(identExpr.getName());
                if (identifierEquals("COLLATE")) {
                    lexer.nextToken();
                    String collate = lexer.stringVal();
                    mysqlCharExpr.setCollate(collate);
                    accept(Token.IDENTIFIER);
                }
                expr = mysqlCharExpr;
                return primaryRest(expr);
            }
        } else if (expr instanceof SQLCharExpr) {
            SQLMethodInvokeExpr concat = new SQLMethodInvokeExpr("CONCAT");
            concat.addParameter(expr);
            do {
                String chars = lexer.stringVal();
                concat.addParameter(new SQLCharExpr(chars));
                lexer.nextToken();
            } while (lexer.token() == Token.LITERAL_CHARS || lexer.token() == Token.LITERAL_ALIAS);
            expr = concat;
        }
    } else if (lexer.token() == Token.IDENTIFIER) {
        if (expr instanceof SQLHexExpr) {
            if ("USING".equalsIgnoreCase(lexer.stringVal())) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error, illegal hex");
                }
                String charSet = lexer.stringVal();
                lexer.nextToken();
                expr.getAttributes().put("USING", charSet);
                return primaryRest(expr);
            }
        } else if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("syntax error");
            }
            String collate = lexer.stringVal();
            lexer.nextToken();
            SQLBinaryOpExpr binaryExpr = new SQLBinaryOpExpr(expr, SQLBinaryOperator.COLLATE, new SQLIdentifierExpr(collate), JdbcConstants.MYSQL);
            expr = binaryExpr;
            return primaryRest(expr);
        } else if (expr instanceof SQLVariantRefExpr) {
            if ("COLLATE".equalsIgnoreCase(lexer.stringVal())) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error");
                }
                String collate = lexer.stringVal();
                lexer.nextToken();
                expr.putAttribute("COLLATE", collate);
                return primaryRest(expr);
            }
        } else if (expr instanceof SQLIntegerExpr) {
            SQLIntegerExpr intExpr = (SQLIntegerExpr) expr;
            String binaryString = lexer.stringVal();
            if (intExpr.getNumber().intValue() == 0 && binaryString.startsWith("b")) {
                lexer.nextToken();
                expr = new SQLBinaryExpr(binaryString.substring(1));
                return primaryRest(expr);
            }
        }
    }
    if (lexer.token() == Token.LPAREN && expr instanceof SQLIdentifierExpr) {
        SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
        String ident = identExpr.getName();
        if ("EXTRACT".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("syntax error");
            }
            String unitVal = lexer.stringVal();
            MySqlIntervalUnit unit = MySqlIntervalUnit.valueOf(unitVal.toUpperCase());
            lexer.nextToken();
            accept(Token.FROM);
            SQLExpr value = expr();
            MySqlExtractExpr extract = new MySqlExtractExpr();
            extract.setValue(value);
            extract.setUnit(unit);
            accept(Token.RPAREN);
            expr = extract;
            return primaryRest(expr);
        } else if ("SUBSTRING".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            for (; ; ) {
                SQLExpr param = expr();
                methodInvokeExpr.addParameter(param);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else if (lexer.token() == Token.FROM) {
                    lexer.nextToken();
                    SQLExpr from = expr();
                    methodInvokeExpr.addParameter(from);
                    if (lexer.token() == Token.FOR) {
                        lexer.nextToken();
                        SQLExpr forExpr = expr();
                        methodInvokeExpr.addParameter(forExpr);
                    }
                    break;
                } else if (lexer.token() == Token.RPAREN) {
                    break;
                } else {
                    throw new ParserException("syntax error");
                }
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("TRIM".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            if (lexer.token() == Token.IDENTIFIER) {
                String flagVal = lexer.stringVal();
                if ("LEADING".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.getAttributes().put("TRIM_TYPE", "LEADING");
                } else if ("BOTH".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.getAttributes().put("TRIM_TYPE", "BOTH");
                } else if ("TRAILING".equalsIgnoreCase(flagVal)) {
                    lexer.nextToken();
                    methodInvokeExpr.putAttribute("TRIM_TYPE", "TRAILING");
                }
            }
            SQLExpr param = expr();
            methodInvokeExpr.addParameter(param);
            if (lexer.token() == Token.FROM) {
                lexer.nextToken();
                SQLExpr from = expr();
                methodInvokeExpr.putAttribute("FROM", from);
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("MATCH".equalsIgnoreCase(ident)) {
            lexer.nextToken();
            MySqlMatchAgainstExpr matchAgainstExpr = new MySqlMatchAgainstExpr();
            if (lexer.token() == Token.RPAREN) {
                lexer.nextToken();
            } else {
                exprList(matchAgainstExpr.getColumns(), matchAgainstExpr);
                accept(Token.RPAREN);
            }
            acceptIdentifier("AGAINST");
            accept(Token.LPAREN);
            SQLExpr against = primary();
            matchAgainstExpr.setAgainst(against);
            if (lexer.token() == Token.IN) {
                lexer.nextToken();
                if (identifierEquals("NATURAL")) {
                    lexer.nextToken();
                    acceptIdentifier("LANGUAGE");
                    acceptIdentifier("MODE");
                    if (lexer.token() == Token.WITH) {
                        lexer.nextToken();
                        acceptIdentifier("QUERY");
                        acceptIdentifier("EXPANSION");
                        matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE_WITH_QUERY_EXPANSION);
                    } else {
                        matchAgainstExpr.setSearchModifier(SearchModifier.IN_NATURAL_LANGUAGE_MODE);
                    }
                } else if (identifierEquals("BOOLEAN")) {
                    lexer.nextToken();
                    acceptIdentifier("MODE");
                    matchAgainstExpr.setSearchModifier(SearchModifier.IN_BOOLEAN_MODE);
                } else {
                    throw new ParserException("TODO");
                }
            } else if (lexer.token() == Token.WITH) {
                throw new ParserException("TODO");
            }
            accept(Token.RPAREN);
            expr = matchAgainstExpr;
            return primaryRest(expr);
        } else if (("CONVERT".equalsIgnoreCase(ident)) || ("CHAR".equalsIgnoreCase(ident))) {
            lexer.nextToken();
            SQLMethodInvokeExpr methodInvokeExpr = new SQLMethodInvokeExpr(ident);
            if (lexer.token() != Token.RPAREN) {
                exprList(methodInvokeExpr.getParameters(), methodInvokeExpr);
            }
            if (identifierEquals("USING")) {
                lexer.nextToken();
                if (lexer.token() != Token.IDENTIFIER) {
                    throw new ParserException("syntax error");
                }
                String charset = lexer.stringVal();
                lexer.nextToken();
                methodInvokeExpr.putAttribute("USING", charset);
            }
            accept(Token.RPAREN);
            expr = methodInvokeExpr;
            return primaryRest(expr);
        } else if ("POSITION".equalsIgnoreCase(ident)) {
            accept(Token.LPAREN);
            SQLExpr subStr = this.primary();
            accept(Token.IN);
            SQLExpr str = this.expr();
            accept(Token.RPAREN);
            SQLMethodInvokeExpr locate = new SQLMethodInvokeExpr("LOCATE");
            locate.addParameter(subStr);
            locate.addParameter(str);
            expr = locate;
            return primaryRest(expr);
        }
    }
    if (lexer.token() == Token.VARIANT && "@".equals(lexer.stringVal())) {
        lexer.nextToken();
        MySqlUserName userName = new MySqlUserName();
        if (expr instanceof SQLCharExpr) {
            userName.setUserName(((SQLCharExpr) expr).toString());
        } else {
            userName.setUserName(((SQLIdentifierExpr) expr).getName());
        }
        if (lexer.token() == Token.LITERAL_CHARS) {
            userName.setHost("'" + lexer.stringVal() + "'");
        } else {
            userName.setHost(lexer.stringVal());
        }
        lexer.nextToken();
        return userName;
    }
    if (lexer.token() == Token.ERROR) {
        throw new ParserException("syntax error, token: " + lexer.token() + " " + lexer.stringVal() + ", pos : " + lexer.pos());
    }
    return super.primaryRest(expr);
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLBinaryExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryExpr) SQLHexExpr(com.alibaba.druid.sql.ast.expr.SQLHexExpr) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) MySqlMatchAgainstExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlMatchAgainstExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MySqlIntervalUnit(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlIntervalUnit) MySqlCharExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) MySqlExtractExpr(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlExtractExpr) MySqlUserName(com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlUserName)

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