Search in sources :

Example 26 with SQLSelectQueryBlock

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

the class PGSelectParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == Token.VALUES) {
        lexer.nextToken();
        accept(Token.LPAREN);
        PGValuesQuery valuesQuery = new PGValuesQuery();
        this.exprParser.exprList(valuesQuery.getValues(), valuesQuery);
        accept(Token.RPAREN);
        return queryRest(valuesQuery);
    }
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        if (select instanceof SQLSelectQueryBlock) {
            ((SQLSelectQueryBlock) select).setParenthesized(true);
        }
        accept(Token.RPAREN);
        return queryRest(select);
    }
    PGSelectQueryBlock queryBlock = new PGSelectQueryBlock();
    if (lexer.token() == Token.SELECT) {
        lexer.nextToken();
        if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
        }
        if (lexer.token() == Token.DISTINCT) {
            queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
            lexer.nextToken();
            if (lexer.token() == Token.ON) {
                lexer.nextToken();
                for (; ; ) {
                    SQLExpr expr = this.createExprParser().expr();
                    queryBlock.getDistinctOn().add(expr);
                    if (lexer.token() == Token.COMMA) {
                        lexer.nextToken();
                        continue;
                    } else {
                        break;
                    }
                }
            }
        } else if (lexer.token() == Token.ALL) {
            queryBlock.setDistionOption(SQLSetQuantifier.ALL);
            lexer.nextToken();
        }
        parseSelectList(queryBlock);
        if (lexer.token() == Token.INTO) {
            lexer.nextToken();
            if (lexer.token() == Token.TEMPORARY) {
                lexer.nextToken();
                queryBlock.setIntoOption(IntoOption.TEMPORARY);
            } else if (lexer.token() == Token.TEMP) {
                lexer.nextToken();
                queryBlock.setIntoOption(IntoOption.TEMP);
            } else if (lexer.token() == Token.UNLOGGED) {
                lexer.nextToken();
                queryBlock.setIntoOption(IntoOption.UNLOGGED);
            }
            if (lexer.token() == Token.TABLE) {
                lexer.nextToken();
            }
            SQLExpr name = this.createExprParser().name();
            queryBlock.setInto(new SQLExprTableSource(name));
        }
    }
    parseFrom(queryBlock);
    parseWhere(queryBlock);
    parseGroupBy(queryBlock);
    if (lexer.token() == Token.WINDOW) {
        lexer.nextToken();
        PGSelectQueryBlock.WindowClause window = new PGSelectQueryBlock.WindowClause();
        window.setName(this.expr());
        accept(Token.AS);
        for (; ; ) {
            SQLExpr expr = this.createExprParser().expr();
            window.getDefinition().add(expr);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            } else {
                break;
            }
        }
        queryBlock.setWindow(window);
    }
    queryBlock.setOrderBy(this.createExprParser().parseOrderBy());
    for (; ; ) {
        if (lexer.token() == Token.LIMIT) {
            SQLLimit limit = new SQLLimit();
            lexer.nextToken();
            if (lexer.token() == Token.ALL) {
                limit.setRowCount(new SQLIdentifierExpr("ALL"));
                lexer.nextToken();
            } else {
                limit.setRowCount(expr());
            }
            queryBlock.setLimit(limit);
        } else if (lexer.token() == Token.OFFSET) {
            SQLLimit limit = queryBlock.getLimit();
            if (limit == null) {
                limit = new SQLLimit();
                queryBlock.setLimit(limit);
            }
            lexer.nextToken();
            SQLExpr offset = expr();
            limit.setOffset(offset);
            if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
                lexer.nextToken();
            }
        } else {
            break;
        }
    }
    if (lexer.token() == Token.FETCH) {
        lexer.nextToken();
        PGSelectQueryBlock.FetchClause fetch = new PGSelectQueryBlock.FetchClause();
        if (lexer.token() == Token.FIRST) {
            fetch.setOption(PGSelectQueryBlock.FetchClause.Option.FIRST);
        } else if (lexer.token() == Token.NEXT) {
            fetch.setOption(PGSelectQueryBlock.FetchClause.Option.NEXT);
        } else {
            throw new ParserException("expect 'FIRST' or 'NEXT'");
        }
        SQLExpr count = expr();
        fetch.setCount(count);
        if (lexer.token() == Token.ROW || lexer.token() == Token.ROWS) {
            lexer.nextToken();
        } else {
            throw new ParserException("expect 'ROW' or 'ROWS'");
        }
        if (lexer.token() == Token.ONLY) {
            lexer.nextToken();
        } else {
            throw new ParserException("expect 'ONLY'");
        }
        queryBlock.setFetch(fetch);
    }
    if (lexer.token() == Token.FOR) {
        lexer.nextToken();
        PGSelectQueryBlock.ForClause forClause = new PGSelectQueryBlock.ForClause();
        if (lexer.token() == Token.UPDATE) {
            forClause.setOption(PGSelectQueryBlock.ForClause.Option.UPDATE);
            lexer.nextToken();
        } else if (lexer.token() == Token.SHARE) {
            forClause.setOption(PGSelectQueryBlock.ForClause.Option.SHARE);
            lexer.nextToken();
        } else {
            throw new ParserException("expect 'FIRST' or 'NEXT'");
        }
        if (lexer.token() == Token.OF) {
            for (; ; ) {
                SQLExpr expr = this.createExprParser().expr();
                forClause.getOf().add(expr);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else {
                    break;
                }
            }
        }
        if (lexer.token() == Token.NOWAIT) {
            lexer.nextToken();
            forClause.setNoWait(true);
        }
        queryBlock.setForClause(forClause);
    }
    return queryRest(queryBlock);
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) PGValuesQuery(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGValuesQuery) PGSelectQueryBlock(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource)

Example 27 with SQLSelectQueryBlock

use of com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock 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 28 with SQLSelectQueryBlock

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

the class SQLSelectParser method query.

public SQLSelectQuery query() {
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        accept(Token.RPAREN);
        return queryRest(select);
    }
    SQLSelectQueryBlock queryBlock = new SQLSelectQueryBlock();
    if (lexer.hasComment() && lexer.isKeepComments()) {
        queryBlock.addBeforeComment(lexer.readAndResetComments());
    }
    accept(Token.SELECT);
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
    }
    if (JdbcConstants.INFORMIX.equals(dbType)) {
        if (identifierEquals("SKIP")) {
            lexer.nextToken();
            SQLExpr offset = this.exprParser.primary();
            queryBlock.setOffset(offset);
        }
        if (identifierEquals("FIRST")) {
            lexer.nextToken();
            SQLExpr first = this.exprParser.primary();
            queryBlock.setFirst(first);
        }
    }
    if (lexer.token() == Token.DISTINCT) {
        queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
        lexer.nextToken();
    } else if (lexer.token() == Token.UNIQUE) {
        queryBlock.setDistionOption(SQLSetQuantifier.UNIQUE);
        lexer.nextToken();
    } else if (lexer.token() == Token.ALL) {
        queryBlock.setDistionOption(SQLSetQuantifier.ALL);
        lexer.nextToken();
    }
    parseSelectList(queryBlock);
    parseFrom(queryBlock);
    parseWhere(queryBlock);
    parseGroupBy(queryBlock);
    parseFetchClause(queryBlock);
    return queryRest(queryBlock);
}
Also used : SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)

Example 29 with SQLSelectQueryBlock

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

the class DruidDataSource method oracleValidationQueryCheck.

private void oracleValidationQueryCheck() {
    if (validationQuery == null) {
        return;
    }
    if (validationQuery.length() == 0) {
        return;
    }
    SQLStatementParser sqlStmtParser = SQLParserUtils.createSQLStatementParser(validationQuery, this.dbType);
    List<SQLStatement> stmtList = sqlStmtParser.parseStatementList();
    if (stmtList.size() != 1) {
        return;
    }
    SQLStatement stmt = stmtList.get(0);
    if (!(stmt instanceof SQLSelectStatement)) {
        return;
    }
    SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        if (((SQLSelectQueryBlock) query).getFrom() == null) {
            LOG.error("invalid oracle validationQuery. " + validationQuery + ", may should be : " + validationQuery + " FROM DUAL");
        }
    }
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 30 with SQLSelectQueryBlock

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

the class DruidDataSource method db2ValidationQueryCheck.

private void db2ValidationQueryCheck() {
    if (validationQuery == null) {
        return;
    }
    if (validationQuery.length() == 0) {
        return;
    }
    SQLStatementParser sqlStmtParser = SQLParserUtils.createSQLStatementParser(validationQuery, this.dbType);
    List<SQLStatement> stmtList = sqlStmtParser.parseStatementList();
    if (stmtList.size() != 1) {
        return;
    }
    SQLStatement stmt = stmtList.get(0);
    if (!(stmt instanceof SQLSelectStatement)) {
        return;
    }
    SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
    if (query instanceof SQLSelectQueryBlock) {
        if (((SQLSelectQueryBlock) query).getFrom() == null) {
            LOG.error("invalid db2 validationQuery. " + validationQuery + ", may should be : " + validationQuery + " FROM SYSDUMMY");
        }
    }
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)33 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)13 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)13 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)10 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)9 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)8 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)8 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)7 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 SQLSubqueryTableSource (com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource)5 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)3 SQLOver (com.alibaba.druid.sql.ast.SQLOver)3 SQLAggregateExpr (com.alibaba.druid.sql.ast.expr.SQLAggregateExpr)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 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)3 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)3 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)3 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)2