mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
add PagerUtils
This commit is contained in:
@@ -1,3 +1,18 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql;
|
||||
|
||||
import java.util.List;
|
||||
@@ -5,6 +20,7 @@ import java.util.List;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAggregateExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr;
|
||||
import com.alibaba.druid.sql.ast.expr.SQLNumberExpr;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
|
||||
@@ -13,6 +29,7 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSubqueryTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.expr.OracleAggregateExpr;
|
||||
import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock;
|
||||
@@ -35,15 +52,63 @@ public class PagerUtils {
|
||||
}
|
||||
|
||||
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
|
||||
|
||||
return count(selectStmt, dbType);
|
||||
return count(selectStmt.getSelect(), dbType);
|
||||
}
|
||||
|
||||
public static String limit(String sql, String dbType, int offset, int count) {
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
|
||||
|
||||
private static String count(SQLSelectStatement stmt, String dbType) {
|
||||
SQLSelect select = stmt.getSelect();
|
||||
if (stmtList.size() != 1) {
|
||||
throw new IllegalArgumentException("sql not support count : " + sql);
|
||||
}
|
||||
|
||||
SQLStatement stmt = stmtList.get(0);
|
||||
|
||||
if (!(stmt instanceof SQLSelectStatement)) {
|
||||
throw new IllegalArgumentException("sql not support count : " + sql);
|
||||
}
|
||||
|
||||
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
|
||||
|
||||
return limit(selectStmt.getSelect(), dbType, offset, count);
|
||||
}
|
||||
|
||||
public static String limit(SQLSelect select, String dbType, int offset, int count) {
|
||||
SQLSelectQuery query = select.getQuery();
|
||||
|
||||
if (query instanceof SQLSelectQueryBlock) {
|
||||
return limit((SQLSelectQueryBlock) query, dbType, offset, count);
|
||||
}
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static String limit(SQLSelectQueryBlock queryBlock, String dbType, int offset, int count) {
|
||||
if (JdbcConstants.MYSQL.equals(dbType)) {
|
||||
return limitMySql((MySqlSelectQueryBlock) queryBlock, dbType, offset, count);
|
||||
}
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static String limitMySql(MySqlSelectQueryBlock queryBlock, String dbType, int offset, int count) {
|
||||
if (queryBlock.getLimit() != null) {
|
||||
throw new IllegalArgumentException("limit already exists.");
|
||||
}
|
||||
|
||||
Limit limit = new Limit();
|
||||
if (offset > 0) {
|
||||
limit.setOffset(new SQLNumberExpr(offset));
|
||||
}
|
||||
limit.setRowCount(new SQLNumberExpr(count));
|
||||
queryBlock.setLimit(limit);
|
||||
|
||||
return SQLUtils.toSQLString(queryBlock, dbType);
|
||||
}
|
||||
|
||||
private static String count(SQLSelect select, String dbType) {
|
||||
if (select.getOrderBy() != null) {
|
||||
select.setOrderBy(null);
|
||||
}
|
||||
|
||||
SQLSelectQuery query = select.getQuery();
|
||||
clearOrderBy(query);
|
||||
|
||||
@@ -51,26 +116,35 @@ public class PagerUtils {
|
||||
SQLSelectItem countItem = createCountItem(dbType);
|
||||
|
||||
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
|
||||
|
||||
if (queryBlock.getGroupBy() != null && queryBlock.getGroupBy().getItems().size() > 0) {
|
||||
return createCountUseSubQuery(select, dbType);
|
||||
}
|
||||
|
||||
queryBlock.getSelectList().clear();
|
||||
queryBlock.getSelectList().add(countItem);
|
||||
return SQLUtils.toSQLString(stmt, dbType);
|
||||
return SQLUtils.toSQLString(select, dbType);
|
||||
} else if (query instanceof SQLUnionQuery) {
|
||||
SQLSelectQueryBlock countSelectQuery = createQueryBlock(dbType);
|
||||
|
||||
SQLSelectItem countItem = createCountItem(dbType);
|
||||
countSelectQuery.getSelectList().add(countItem);
|
||||
|
||||
countSelectQuery.setFrom(new SQLSubqueryTableSource(select));
|
||||
|
||||
SQLSelect countSelect = new SQLSelect(countSelectQuery);
|
||||
SQLSelectStatement countStmt = new SQLSelectStatement(countSelect);
|
||||
|
||||
return SQLUtils.toSQLString(countStmt, dbType);
|
||||
return createCountUseSubQuery(select, dbType);
|
||||
}
|
||||
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
private static String createCountUseSubQuery(SQLSelect select, String dbType) {
|
||||
SQLSelectQueryBlock countSelectQuery = createQueryBlock(dbType);
|
||||
|
||||
SQLSelectItem countItem = createCountItem(dbType);
|
||||
countSelectQuery.getSelectList().add(countItem);
|
||||
|
||||
countSelectQuery.setFrom(new SQLSubqueryTableSource(select));
|
||||
|
||||
SQLSelect countSelect = new SQLSelect(countSelectQuery);
|
||||
SQLSelectStatement countStmt = new SQLSelectStatement(countSelect);
|
||||
|
||||
return SQLUtils.toSQLString(countStmt, dbType);
|
||||
}
|
||||
|
||||
private static SQLSelectQueryBlock createQueryBlock(String dbType) {
|
||||
if (JdbcConstants.MYSQL.equals(dbType)) {
|
||||
return new MySqlSelectQueryBlock();
|
||||
|
||||
Reference in New Issue
Block a user