Search in sources :

Example 21 with SQLExprTableSource

use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project druid by alibaba.

the class OdpsStatementParser method parseInsert.

public SQLStatement parseInsert() {
    OdpsInsertStatement stmt = new OdpsInsertStatement();
    if (lexer.token() == Token.FROM) {
        lexer.nextToken();
        if (lexer.token() == Token.IDENTIFIER) {
            SQLName tableName = this.exprParser.name();
            SQLExprTableSource from = new SQLExprTableSource(tableName);
            stmt.setFrom(from);
        } else {
            accept(Token.LPAREN);
            SQLSelectParser selectParser = createSQLSelectParser();
            SQLSelect select = selectParser.select();
            accept(Token.RPAREN);
            String alias = lexer.stringVal();
            accept(Token.IDENTIFIER);
            SQLSubqueryTableSource from = new SQLSubqueryTableSource(select, alias);
            stmt.setFrom(from);
        }
    }
    for (; ; ) {
        OdpsInsert insert = parseOdpsInsert();
        stmt.addItem(insert);
        if (lexer.token() != Token.INSERT) {
            break;
        }
    }
    return stmt;
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLSelectParser(com.alibaba.druid.sql.parser.SQLSelectParser) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) OdpsInsert(com.alibaba.druid.sql.dialect.odps.ast.OdpsInsert) OdpsInsertStatement(com.alibaba.druid.sql.dialect.odps.ast.OdpsInsertStatement)

Example 22 with SQLExprTableSource

use of com.alibaba.druid.sql.ast.statement.SQLExprTableSource in project druid by alibaba.

the class Demo3 method convert.

private String convert(String sql, List<Object> parameters) {
    SQLStatementParser parser = new MySqlStatementParser(sql);
    //
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement first = (SQLStatement) stmtList.get(0);
    MyVisitor visitor = new MyVisitor();
    first.accept(visitor);
    if (visitor.getVariantList().size() > 0) {
        SQLExpr firstVar = visitor.getVariantList().get(0);
        int userId;
        if (firstVar instanceof SQLVariantRefExpr) {
            int varIndex = (Integer) firstVar.getAttribute("varIndex");
            userId = (Integer) parameters.get(varIndex);
        } else {
            userId = ((SQLNumericLiteralExpr) firstVar).getNumber().intValue();
        }
        String tableName;
        if (userId == 1) {
            tableName = "user_1";
        } else {
            tableName = "user_x";
        }
        for (SQLExprTableSource tableSource : visitor.getTableSourceList()) {
            SQLExpr expr = tableSource.getExpr();
            if (expr instanceof SQLIdentifierExpr) {
                SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
                String ident = identExpr.getName();
                if (ident.equals("user")) {
                    identExpr.setName(tableName);
                }
            } else if (expr instanceof SQLPropertyExpr) {
                SQLPropertyExpr proExpr = (SQLPropertyExpr) expr;
                String ident = proExpr.getName();
                if (ident.equals("user")) {
                    proExpr.setName(tableName);
                }
            }
        }
    }
    String realSql = SQLUtils.toOracleString(first);
    return realSql;
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)

Aggregations

SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)22 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)8 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)7 SQLName (com.alibaba.druid.sql.ast.SQLName)6 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 TableStat (com.alibaba.druid.stat.TableStat)3 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)2 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)2 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)2 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)2 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)2