Search in sources :

Example 26 with SQLSelect

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

the class OracleToMySqlOutputVisitor method visit.

public boolean visit(OracleSelectQueryBlock x) {
    boolean parentIsSelectStatment = false;
    {
        if (x.getParent() instanceof SQLSelect) {
            SQLSelect select = (SQLSelect) x.getParent();
            if (select.getParent() instanceof SQLSelectStatement || select.getParent() instanceof SQLSubqueryTableSource) {
                parentIsSelectStatment = true;
            }
        }
    }
    if (!parentIsSelectStatment) {
        return super.visit(x);
    }
    if (//
    x.getWhere() instanceof SQLBinaryOpExpr && //
    x.getFrom() instanceof SQLSubqueryTableSource) {
        int rownum;
        String ident;
        SQLBinaryOpExpr where = (SQLBinaryOpExpr) x.getWhere();
        if (where.getRight() instanceof SQLIntegerExpr && where.getLeft() instanceof SQLIdentifierExpr) {
            rownum = ((SQLIntegerExpr) where.getRight()).getNumber().intValue();
            ident = ((SQLIdentifierExpr) where.getLeft()).getName();
        } else {
            return super.visit(x);
        }
        SQLSelect select = ((SQLSubqueryTableSource) x.getFrom()).getSelect();
        SQLSelectQueryBlock queryBlock = null;
        SQLSelect subSelect = null;
        SQLBinaryOpExpr subWhere = null;
        boolean isSubQueryRowNumMapping = false;
        if (select.getQuery() instanceof SQLSelectQueryBlock) {
            queryBlock = (SQLSelectQueryBlock) select.getQuery();
            if (queryBlock.getWhere() instanceof SQLBinaryOpExpr) {
                subWhere = (SQLBinaryOpExpr) queryBlock.getWhere();
            }
            for (SQLSelectItem selectItem : queryBlock.getSelectList()) {
                if (isRowNumber(selectItem.getExpr())) {
                    if (where.getLeft() instanceof SQLIdentifierExpr && ((SQLIdentifierExpr) where.getLeft()).getName().equals(selectItem.getAlias())) {
                        isSubQueryRowNumMapping = true;
                    }
                }
            }
            SQLTableSource subTableSource = queryBlock.getFrom();
            if (subTableSource instanceof SQLSubqueryTableSource) {
                subSelect = ((SQLSubqueryTableSource) subTableSource).getSelect();
            }
        }
        if ("ROWNUM".equalsIgnoreCase(ident)) {
            SQLBinaryOperator op = where.getOperator();
            Integer limit = null;
            if (op == SQLBinaryOperator.LessThanOrEqual) {
                limit = rownum;
            } else if (op == SQLBinaryOperator.LessThan) {
                limit = rownum - 1;
            }
            if (limit != null) {
                select.accept(this);
                println();
                print0(ucase ? "LIMIT " : "limit ");
                print(limit);
                return false;
            }
        } else if (isSubQueryRowNumMapping) {
            SQLBinaryOperator op = where.getOperator();
            SQLBinaryOperator subOp = subWhere.getOperator();
            if (//
            isRowNumber(subWhere.getLeft()) && subWhere.getRight() instanceof SQLIntegerExpr) {
                int subRownum = ((SQLIntegerExpr) subWhere.getRight()).getNumber().intValue();
                Integer offset = null;
                if (op == SQLBinaryOperator.GreaterThanOrEqual) {
                    offset = rownum + 1;
                } else if (op == SQLBinaryOperator.GreaterThan) {
                    offset = rownum;
                }
                if (offset != null) {
                    Integer limit = null;
                    if (subOp == SQLBinaryOperator.LessThanOrEqual) {
                        limit = subRownum - offset;
                    } else if (subOp == SQLBinaryOperator.LessThan) {
                        limit = subRownum - 1 - offset;
                    }
                    if (limit != null) {
                        subSelect.accept(this);
                        println();
                        print0(ucase ? "LIMIT " : "limit ");
                        print(offset);
                        print0(", ");
                        print(limit);
                        return false;
                    }
                }
            }
        }
    }
    return super.visit(x);
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLBinaryOperator(com.alibaba.druid.sql.ast.expr.SQLBinaryOperator) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)

Example 27 with SQLSelect

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

the class SQLSelectParser method parseTableSource.

public SQLTableSource parseTableSource() {
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLTableSource tableSource;
        if (lexer.token() == Token.SELECT || lexer.token() == Token.WITH || lexer.token == Token.SEL) {
            SQLSelect select = select();
            accept(Token.RPAREN);
            SQLSelectQuery query = queryRest(select.getQuery());
            if (query instanceof SQLUnionQuery) {
                tableSource = new SQLUnionQueryTableSource((SQLUnionQuery) query);
            } else {
                tableSource = new SQLSubqueryTableSource(select);
            }
        } else if (lexer.token() == Token.LPAREN) {
            tableSource = parseTableSource();
            accept(Token.RPAREN);
        } else {
            tableSource = parseTableSource();
            accept(Token.RPAREN);
        }
        return parseTableSourceRest(tableSource);
    }
    if (lexer.token() == Token.SELECT) {
        throw new ParserException("TODO");
    }
    SQLExprTableSource tableReference = new SQLExprTableSource();
    parseTableSourceQueryTableExpr(tableReference);
    SQLTableSource tableSrc = parseTableSourceRest(tableReference);
    if (lexer.hasComment() && lexer.isKeepComments()) {
        tableSrc.addAfterComment(lexer.readAndResetComments());
    }
    return tableSrc;
}
Also used : SQLSubqueryTableSource(com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLUnionQueryTableSource(com.alibaba.druid.sql.ast.statement.SQLUnionQueryTableSource) SQLUnionQuery(com.alibaba.druid.sql.ast.statement.SQLUnionQuery) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource)

Example 28 with SQLSelect

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

the class SQLSelectParser method select.

public SQLSelect select() {
    SQLSelect select = new SQLSelect();
    withSubquery(select);
    select.setQuery(query());
    select.setOrderBy(parseOrderBy());
    if (select.getOrderBy() == null) {
        select.setOrderBy(parseOrderBy());
    }
    while (lexer.token() == Token.HINT) {
        this.exprParser.parseHints(select.getHints());
    }
    return select;
}
Also used : SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect)

Example 29 with SQLSelect

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

the class OdpsStatementParser method parseOdpsInsert.

public OdpsInsert parseOdpsInsert() {
    OdpsInsert insert = new OdpsInsert();
    if (lexer.isKeepComments() && lexer.hasComment()) {
        insert.addBeforeComment(lexer.readAndResetComments());
    }
    SQLSelectParser selectParser = createSQLSelectParser();
    accept(Token.INSERT);
    if (lexer.token() == Token.INTO) {
        lexer.nextToken();
    } else {
        accept(Token.OVERWRITE);
        insert.setOverwrite(true);
    }
    accept(Token.TABLE);
    insert.setTableSource(this.exprParser.name());
    if (lexer.token() == Token.PARTITION) {
        lexer.nextToken();
        accept(Token.LPAREN);
        for (; ; ) {
            SQLAssignItem ptExpr = new SQLAssignItem();
            ptExpr.setTarget(this.exprParser.name());
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
                SQLExpr ptValue = this.exprParser.expr();
                ptExpr.setValue(ptValue);
            }
            insert.addPartition(ptExpr);
            if (!(lexer.token() == (Token.COMMA))) {
                break;
            } else {
                lexer.nextToken();
            }
        }
        accept(Token.RPAREN);
    }
    SQLSelect query = selectParser.select();
    insert.setQuery(query);
    return insert;
}
Also used : SQLAssignItem(com.alibaba.druid.sql.ast.statement.SQLAssignItem) SQLSelectParser(com.alibaba.druid.sql.parser.SQLSelectParser) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) OdpsInsert(com.alibaba.druid.sql.dialect.odps.ast.OdpsInsert) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 30 with SQLSelect

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

the class MySqlSelectIntoParser method parseSelectInto.

public MySqlSelectIntoStatement parseSelectInto() {
    SQLSelect select = select();
    MySqlSelectIntoStatement stmt = new MySqlSelectIntoStatement();
    stmt.setSelect(select);
    stmt.setVarList(argsList);
    return stmt;
}
Also used : SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) MySqlSelectIntoStatement(com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlSelectIntoStatement)

Aggregations

SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)40 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)21 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)19 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)16 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)15 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)15 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)8 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)7 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)6 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)6 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLName (com.alibaba.druid.sql.ast.SQLName)3 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)3 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)3 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)3 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)3 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)3 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)3