Search in sources :

Example 1 with SQLLimit

use of com.alibaba.druid.sql.ast.SQLLimit in project druid by alibaba.

the class OdpsSelectParser method query.

@Override
public SQLSelectQuery query() {
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        SQLSelectQuery select = query();
        accept(Token.RPAREN);
        return queryRest(select);
    }
    OdpsSelectQueryBlock queryBlock = new OdpsSelectQueryBlock();
    if (lexer.hasComment() && lexer.isKeepComments()) {
        queryBlock.addBeforeComment(lexer.readAndResetComments());
    }
    accept(Token.SELECT);
    if (lexer.token() == Token.HINT) {
        this.exprParser.parseHints(queryBlock.getHints());
    }
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
    }
    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);
    queryBlock.setOrderBy(this.exprParser.parseOrderBy());
    if (lexer.token() == Token.DISTRIBUTE) {
        lexer.nextToken();
        accept(Token.BY);
        this.exprParser.exprList(queryBlock.getDistributeBy(), queryBlock);
        if (identifierEquals("SORT")) {
            lexer.nextToken();
            accept(Token.BY);
            for (; ; ) {
                SQLExpr expr = this.expr();
                SQLSelectOrderByItem sortByItem = new SQLSelectOrderByItem(expr);
                if (lexer.token() == Token.ASC) {
                    sortByItem.setType(SQLOrderingSpecification.ASC);
                    lexer.nextToken();
                } else if (lexer.token() == Token.DESC) {
                    sortByItem.setType(SQLOrderingSpecification.DESC);
                    lexer.nextToken();
                }
                queryBlock.getSortBy().add(sortByItem);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                } else {
                    break;
                }
            }
        }
    }
    if (lexer.token() == Token.LIMIT) {
        lexer.nextToken();
        queryBlock.setLimit(new SQLLimit(this.expr()));
    }
    return queryRest(queryBlock);
}
Also used : SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 with SQLLimit

use of com.alibaba.druid.sql.ast.SQLLimit in project druid by alibaba.

the class PagerUtils method limitPostgreSQLQueryBlock.

private static String limitPostgreSQLQueryBlock(PGSelectQueryBlock queryBlock, String dbType, int offset, int count) {
    if (queryBlock.getLimit() != null) {
        throw new IllegalArgumentException("limit already exists.");
    }
    SQLLimit limit = new SQLLimit();
    if (offset > 0) {
        limit.setOffset(new SQLIntegerExpr(offset));
    }
    limit.setRowCount(new SQLIntegerExpr(count));
    queryBlock.setLimit(limit);
    return SQLUtils.toSQLString(queryBlock, dbType);
}
Also used : SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)

Example 3 with SQLLimit

use of com.alibaba.druid.sql.ast.SQLLimit in project druid by alibaba.

the class PagerUtils method limitMySqlQueryBlock.

private static String limitMySqlQueryBlock(MySqlSelectQueryBlock queryBlock, String dbType, int offset, int count) {
    if (queryBlock.getLimit() != null) {
        throw new IllegalArgumentException("limit already exists.");
    }
    SQLLimit limit = new SQLLimit();
    if (offset > 0) {
        limit.setOffset(new SQLNumberExpr(offset));
    }
    limit.setRowCount(new SQLNumberExpr(count));
    queryBlock.setLimit(limit);
    return SQLUtils.toSQLString(queryBlock, dbType);
}
Also used : SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr)

Example 4 with SQLLimit

use of com.alibaba.druid.sql.ast.SQLLimit in project druid by alibaba.

the class PagerUtils method getLimit.

/**
     * 
     * @param sql
     * @param dbType
     * @return if not exists limit, return -1;
     */
public static int getLimit(String sql, String dbType) {
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    if (stmtList.size() != 1) {
        return -1;
    }
    SQLStatement stmt = stmtList.get(0);
    if (stmt instanceof SQLSelectStatement) {
        SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
        SQLSelectQuery query = selectStmt.getSelect().getQuery();
        if (query instanceof SQLSelectQueryBlock) {
            if (query instanceof MySqlSelectQueryBlock) {
                SQLLimit limit = ((MySqlSelectQueryBlock) query).getLimit();
                if (limit == null) {
                    return -1;
                }
                SQLExpr rowCountExpr = limit.getRowCount();
                if (rowCountExpr instanceof SQLNumericLiteralExpr) {
                    int rowCount = ((SQLNumericLiteralExpr) rowCountExpr).getNumber().intValue();
                    return rowCount;
                }
                return Integer.MAX_VALUE;
            }
            if (query instanceof OdpsSelectQueryBlock) {
                SQLLimit limit = ((OdpsSelectQueryBlock) query).getLimit();
                SQLExpr rowCountExpr = limit != null ? limit.getRowCount() : null;
                if (rowCountExpr instanceof SQLNumericLiteralExpr) {
                    int rowCount = ((SQLNumericLiteralExpr) rowCountExpr).getNumber().intValue();
                    return rowCount;
                }
                return Integer.MAX_VALUE;
            }
            return -1;
        }
    }
    return -1;
}
Also used : SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 5 with SQLLimit

use of com.alibaba.druid.sql.ast.SQLLimit in project druid by alibaba.

the class MySqlSelectTest_clearLimit method test_getLimit.

public void test_getLimit() {
    SQLLimit limit = SQLUtils.getLimit("insert into t1 select * from t2 limit 10,10", DbType.mysql);
    assertEquals("LIMIT 10, 10", limit.toString());
    limit = SQLUtils.getLimit("select a from t limit 1,2", DbType.mysql);
    assertEquals("LIMIT 1, 2", limit.toString());
    limit = SQLUtils.getLimit("DUMP DATA OVERWRITE INTO 'oss://oss-cn-hangzhou.aliyuncs.com/barca-platform/c' " + "   SELECT item_id,item_title FROM tyx.r_hjc_yun_item_info limit 1", DbType.mysql);
    assertEquals("LIMIT 1", limit.toString());
    limit = SQLUtils.getLimit("select * from 1", DbType.mysql);
    assertNull(limit);
}
Also used : SQLLimit(com.alibaba.druid.sql.ast.SQLLimit)

Aggregations

SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)15 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)7 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)4 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)3 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)3 OdpsSelectQueryBlock (com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock)3 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)2 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)1 SQLBinaryExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryExpr)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLIntervalExpr (com.alibaba.druid.sql.ast.expr.SQLIntervalExpr)1 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)1 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)1