From e4c872275bb1e79ab58c5564a5372e0ffdde285d Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Fri, 19 Jan 2018 12:06:46 +0100 Subject: [PATCH] IDEMPIERE-3586 Add pagination to Query / implement in generic way - info window now can paginate also in oracle --- .../src/org/compiere/model/Query.java | 36 ++++++------------- .../src/org/compiere/db/DB_Oracle.java | 14 ++++++-- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/Query.java b/org.adempiere.base/src/org/compiere/model/Query.java index 1d81022d81..3a3ec73677 100644 --- a/org.adempiere.base/src/org/compiere/model/Query.java +++ b/org.adempiere.base/src/org/compiere/model/Query.java @@ -89,9 +89,9 @@ public class Query private int pageSize; /** - * Number of pages will be skiped on query run. + * Number of pages will be skipped on query run. */ - private int skip; + private int pagesToSkip; /** * @@ -794,14 +794,14 @@ public class Query * @param pPageSize * Limit current query rows return. * - * @param pSkip - * Number of pages will be skiped on query run. ZERO to first page + * @param pPagesToSkip + * Number of pages will be skipped on query run. ZERO for first page * * @return current Query */ - public Query setPage(int pPageSize, int pSkip) { + public Query setPage(int pPageSize, int pPagesToSkip) { this.pageSize = pPageSize; - this.skip = pSkip; + this.pagesToSkip = pPagesToSkip; return this; } @@ -820,25 +820,11 @@ public class Query String query = pQuery; if (pageSize > 0) { - - StringBuilder sql = new StringBuilder(); - - if (DB.isOracle()) { - - sql.append("select * from ("); - sql.append(" select ROWNUM pRow, tb.* from ("); - sql.append(query); - sql.append(") tb) where pRow > "); - sql.append(pageSize * skip); - sql.append(" AND pRow <= "); - sql.append(((pageSize * skip) + pageSize)); - - return sql.toString(); - - } - else { - query = query.concat(" FETCH FIRST " + pageSize + " ROWS ONLY OFFSET " + (pageSize * skip)); - } + if (DB.getDatabase().isPagingSupported()) { + query = DB.getDatabase().addPagingSQL(query, (pageSize*pagesToSkip) + 1, pageSize * (pagesToSkip+1)); + } else { + throw new IllegalArgumentException("Pagination not supported by database"); + } } return query; diff --git a/org.compiere.db.oracle.provider/src/org/compiere/db/DB_Oracle.java b/org.compiere.db.oracle.provider/src/org/compiere/db/DB_Oracle.java index f6d8c22c18..3f796308b0 100644 --- a/org.compiere.db.oracle.provider/src/org/compiere/db/DB_Oracle.java +++ b/org.compiere.db.oracle.provider/src/org/compiere/db/DB_Oracle.java @@ -1287,12 +1287,20 @@ public class DB_Oracle implements AdempiereDatabase } public String addPagingSQL(String sql, int start, int end) { - //not supported, too many corner case that doesn't work using rownum. to investigate later - return sql; + StringBuilder newSql = new StringBuilder("select * from (") + .append(" select tb.*, ROWNUM oracle_native_rownum_ from (") + .append(sql) + .append(") tb) where oracle_native_rownum_ >= ") + .append(start) + .append(" AND oracle_native_rownum_ <= ") + .append(end) + .append(" order by oracle_native_rownum_"); + + return newSql.toString(); } public boolean isPagingSupported() { - return false; + return true; } private int getIntProperty(Properties properties, String key, int defaultValue)