TopPaginationContextEngine { public PaginationContext createPaginationContext(final TopProjectionSegment topProjectionSegment, final ExpressionSegment where, final List<Object> parameters) { Collection<AndPredicate> andPredicates = new ExpressionBuilder(where).extractAndPredicates().getAndPredicates(); Optional<ExpressionSegment> rowNumberPredicate = null != where ? getRowNumberPredicate(andPredicates, topProjectionSegment.getAlias()) : Optional.empty(); Optional<PaginationValueSegment> offset = rowNumberPredicate.isPresent() ? createOffsetWithRowNumber(rowNumberPredicate.get()) : Optional.empty(); PaginationValueSegment rowCount = topProjectionSegment.getTop(); return new PaginationContext(offset.orElse(null), rowCount, parameters); }  PaginationContext createPaginationContext(final TopProjectionSegment topProjectionSegment, final ExpressionSegment where, final List<Object> parameters);  }
@Test public void assertCreatePaginationContextWhenRowNumberPredicateNotPresent() { TopProjectionSegment topProjectionSegment = new TopProjectionSegment(0, 10, null, "rowNumberAlias"); Collection<AndPredicate> andPredicates = Collections.emptyList(); List<Object> parameters = Collections.emptyList(); PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(topProjectionSegment, null, parameters); assertFalse(paginationContext.getOffsetSegment().isPresent()); assertFalse(paginationContext.getRowCountSegment().isPresent()); }
@Test public void assertCreatePaginationContextWhenPredicateInRightValue() { String name = "rowNumberAlias"; ColumnSegment columnSegment = new ColumnSegment(0, 10, new IdentifierValue(name)); InExpression inExpression = new InExpression(0, 0, columnSegment, new ListExpression(0, 0), false); PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(new TopProjectionSegment(0, 10, null, name), inExpression, Collections.emptyList()); assertFalse(paginationContext.getOffsetSegment().isPresent()); assertFalse(paginationContext.getRowCountSegment().isPresent()); }
@Test public void assertCreatePaginationContextWhenParameterMarkerRowNumberValueSegment() { String name = "rowNumberAlias"; ColumnSegment left = new ColumnSegment(0, 10, new IdentifierValue(name)); ParameterMarkerExpressionSegment right = new ParameterMarkerExpressionSegment(0, 10, 0); BinaryOperationExpression expression = new BinaryOperationExpression(0, 0, left, right, ">", null); PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(new TopProjectionSegment(0, 10, null, name), expression, Collections.singletonList(1)); assertTrue(paginationContext.getOffsetSegment().isPresent()); PaginationValueSegment paginationValueSegment = paginationContext.getOffsetSegment().get(); assertThat(paginationValueSegment, instanceOf(ParameterMarkerRowNumberValueSegment.class)); ParameterMarkerRowNumberValueSegment parameterMarkerRowNumberValueSegment = (ParameterMarkerRowNumberValueSegment) paginationValueSegment; assertThat(parameterMarkerRowNumberValueSegment.getStartIndex(), is(0)); assertThat(parameterMarkerRowNumberValueSegment.getStopIndex(), is(10)); assertThat(parameterMarkerRowNumberValueSegment.getParameterIndex(), is(0)); assertFalse(paginationContext.getRowCountSegment().isPresent()); }