解决PagerUtils.limit 不支持PostgreSQL的bug

参考MySQL的Limit实现,增加PGLimit实现PagerUtils.limit 支持生成PostgreSQL的分页查询sql语句
This commit is contained in:
lizongbo
2014-09-14 14:44:18 +08:00
parent b51f0e7a30
commit 3cbc8558c6
6 changed files with 135 additions and 0 deletions
@@ -40,6 +40,7 @@ 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.stmt.OracleSelectQueryBlock;
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock;
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock.PGLimit;
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
import com.alibaba.druid.util.JdbcConstants;
@@ -112,9 +113,30 @@ public class PagerUtils {
return limitMySqlQueryBlock((MySqlSelectQueryBlock) queryBlock, dbType, offset, count);
}
if (JdbcConstants.POSTGRESQL.equals(dbType))
{
return limitPostgreSQLQueryBlock((PGSelectQueryBlock) queryBlock, dbType, offset, count);
}
throw new UnsupportedOperationException();
}
private static String limitPostgreSQLQueryBlock(PGSelectQueryBlock queryBlock, String dbType,
int offset, int count) {
if (queryBlock.getLimit() != null)
{
throw new IllegalArgumentException("limit already exists.");
}
PGLimit limit = new PGLimit();
if (offset > 0)
{
limit.setOffset(new SQLNumberExpr(offset));
}
limit.setRowCount(new SQLNumberExpr(count));
queryBlock.setLimit(limit);
return SQLUtils.toSQLString(queryBlock, dbType);
}
private static String limitDB2(SQLSelect select, String dbType, int offset, int count) {
SQLSelectQuery query = select.getQuery();