IDEMPIERE-4447 Add offset to Query (#243)

Add method Query.setRecordstoSkip
This commit is contained in:
Carlos Ruiz 2020-09-04 21:42:14 +02:00 committed by GitHub
parent e93cdcb38c
commit be525d178c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 15 deletions

View File

@ -89,9 +89,9 @@ public class Query
private int pageSize; 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;
/** /**
* *
@ -765,7 +765,7 @@ public class Query
} }
// If have pagination // If have pagination
if (pageSize > 0) { if (pageSize > 0 || recordsToSkip > 0) {
sql = appendPagination(sql); sql = appendPagination(sql);
} }
@ -800,8 +800,25 @@ public class Query
* @return current Query * @return current Query
*/ */
public Query setPage(int pPageSize, int pPagesToSkip) { public Query setPage(int pPageSize, int pPagesToSkip) {
this.pageSize = pPageSize; if (pPageSize > 0) {
this.pagesToSkip = pPagesToSkip; 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; return this;
} }
@ -819,9 +836,9 @@ public class Query
String query = pQuery; String query = pQuery;
if (pageSize > 0) { if (pageSize > 0 || recordsToSkip > 0) {
if (DB.getDatabase().isPagingSupported()) { 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 { } else {
throw new IllegalArgumentException("Pagination not supported by database"); throw new IllegalArgumentException("Pagination not supported by database");
} }

View File

@ -1209,10 +1209,12 @@ public class DB_Oracle implements AdempiereDatabase
.append(" select tb.*, ROWNUM oracle_native_rownum_ from (") .append(" select tb.*, ROWNUM oracle_native_rownum_ from (")
.append(sql) .append(sql)
.append(") tb) where oracle_native_rownum_ >= ") .append(") tb) where oracle_native_rownum_ >= ")
.append(start) .append(start);
.append(" AND oracle_native_rownum_ <= ") if (end > 0) {
.append(end) newSql.append(" AND oracle_native_rownum_ <= ")
.append(" order by oracle_native_rownum_"); .append(end);
}
newSql.append(" order by oracle_native_rownum_");
return newSql.toString(); return newSql.toString();
} }

View File

@ -1045,10 +1045,12 @@ public class DB_PostgreSQL implements AdempiereDatabase
*/ */
public String addPagingSQL(String sql, int start, int end) { public String addPagingSQL(String sql, int start, int end) {
StringBuilder newSql = new StringBuilder(sql); StringBuilder newSql = new StringBuilder(sql);
if (end > 0) {
newSql.append(" ")
.append(markNativeKeyword("LIMIT "))
.append(( end - start + 1 ));
}
newSql.append(" ") newSql.append(" ")
.append(markNativeKeyword("LIMIT "))
.append(( end - start + 1 ))
.append(" ")
.append(markNativeKeyword("OFFSET ")) .append(markNativeKeyword("OFFSET "))
.append((start - 1)); .append((start - 1));
return newSql.toString(); return newSql.toString();