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);
}
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);
}
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);
}
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;
}
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);
}
Aggregations