PagingWriter { public void write(final QueryStatement queryStatement, final int pageNumber, final int pageSize) throws QueryBuildException { try { queryStatement.getBodyWriter().write("LIMIT(?)OFFSET(?)"); queryStatement.pushParameterSetter((statement, index) -> { int size = pageSize; int page = pageNumber - 1; page = (page < 0) ? 0 : page; size = (size > MAX_PAGE_SIZE) ? MAX_PAGE_SIZE : ((size < MIN_PAGE_SIZE) ? MIN_PAGE_SIZE : size); statement.setInt(index++, size); statement.setInt(index++, size * page); return index; }); } catch (IOException e) { throw new QueryBuildException("Error while writing PAGING clause of search query", e); } } PagingWriter(); void write(final QueryStatement queryStatement, final int pageNumber, final int pageSize);  }
@Test public void should_WritesPAGINGClauseIntoQueryStatement() throws QueryBuildException, SQLException { pagingWriter.write(query, 3, 8); assertEquals("LIMIT(?)OFFSET(?)", body.toString()); verify(query, times(1)).pushParameterSetter(any()); for (SQLQueryParameterSetter setter : setters) { PreparedStatement statement = mock(PreparedStatement.class); int finalIndex = setter.setParameters(statement, 1); assertEquals(3, finalIndex); verify(statement).setInt(eq(1), eq(8)); verify(statement).setInt(eq(2), eq(16)); } }