diff --git a/org.adempiere.base/src/org/compiere/model/Query.java b/org.adempiere.base/src/org/compiere/model/Query.java index 3a3ec73677..b6036c6cef 100644 --- a/org.adempiere.base/src/org/compiere/model/Query.java +++ b/org.adempiere.base/src/org/compiere/model/Query.java @@ -89,10 +89,10 @@ public class Query private int pageSize; /** - * Number of pages will be skipped on query run. + * Number of records will be skipped on query run. */ - private int pagesToSkip; - + private int recordsToSkip; + /** * * @param table @@ -765,7 +765,7 @@ public class Query } // If have pagination - if (pageSize > 0) { + if (pageSize > 0 || recordsToSkip > 0) { sql = appendPagination(sql); } @@ -800,8 +800,25 @@ public class Query * @return current Query */ public Query setPage(int pPageSize, int pPagesToSkip) { - this.pageSize = pPageSize; - this.pagesToSkip = pPagesToSkip; + if (pPageSize > 0) { + this.pageSize = pPageSize; + this.recordsToSkip = pPagesToSkip * pageSize; + } else { + log.warning("Wrong PageSize <= 0"); + } + return this; + } + + /** + * Set the number of records to skip (a.k.a. OFFSET) + * + * @param pRecordsToSkip + * Limit current query rows return. + * + * @return current Query + */ + public Query setRecordstoSkip(int pRecordsToSkip) { + this.recordsToSkip = pRecordsToSkip; return this; } @@ -819,9 +836,9 @@ public class Query String query = pQuery; - if (pageSize > 0) { + if (pageSize > 0 || recordsToSkip > 0) { if (DB.getDatabase().isPagingSupported()) { - query = DB.getDatabase().addPagingSQL(query, (pageSize*pagesToSkip) + 1, pageSize * (pagesToSkip+1)); + query = DB.getDatabase().addPagingSQL(query, recordsToSkip+1, pageSize <= 0 ? 0 : recordsToSkip + pageSize); } else { throw new IllegalArgumentException("Pagination not supported by database"); } 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 3323291ca9..1a200851c5 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 @@ -1209,10 +1209,12 @@ public class DB_Oracle implements AdempiereDatabase .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_"); + .append(start); + if (end > 0) { + newSql.append(" AND oracle_native_rownum_ <= ") + .append(end); + } + newSql.append(" order by oracle_native_rownum_"); return newSql.toString(); } diff --git a/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java b/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java index b8bec39070..e87a276024 100755 --- a/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java +++ b/org.compiere.db.postgresql.provider/src/org/compiere/db/DB_PostgreSQL.java @@ -1045,10 +1045,12 @@ public class DB_PostgreSQL implements AdempiereDatabase */ public String addPagingSQL(String sql, int start, int end) { StringBuilder newSql = new StringBuilder(sql); + if (end > 0) { + newSql.append(" ") + .append(markNativeKeyword("LIMIT ")) + .append(( end - start + 1 )); + } newSql.append(" ") - .append(markNativeKeyword("LIMIT ")) - .append(( end - start + 1 )) - .append(" ") .append(markNativeKeyword("OFFSET ")) .append((start - 1)); return newSql.toString();