RowNumberPaginationContextEngine { public PaginationContext createPaginationContext(final ExpressionSegment where, final ProjectionsContext projectionsContext, final List<Object> parameters) { Optional<String> rowNumberAlias = isRowNumberAlias(projectionsContext); if (!rowNumberAlias.isPresent()) { return new PaginationContext(null, null, parameters); } Collection<AndPredicate> andPredicates = new ExpressionBuilder(where).extractAndPredicates().getAndPredicates(); Collection<BinaryOperationExpression> rowNumberPredicates = getRowNumberPredicates(andPredicates, rowNumberAlias.get()); return rowNumberPredicates.isEmpty() ? new PaginationContext(null, null, parameters) : createPaginationWithRowNumber(rowNumberPredicates, parameters); }  PaginationContext createPaginationContext(final ExpressionSegment where, final ProjectionsContext projectionsContext, final List<Object> parameters);  }
@Test public void assertCreatePaginationContextWhenRowNumberAliasNotPresent() { ProjectionsContext projectionsContext = new ProjectionsContext(0, 0, false, Collections.emptyList()); PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(null, projectionsContext, Collections.emptyList()); assertFalse(paginationContext.getOffsetSegment().isPresent()); assertFalse(paginationContext.getRowCountSegment().isPresent()); }
@Test public void assertCreatePaginationContextWhenRowNumberAliasIsPresentAndRowNumberPredicatesIsEmpty() { Projection projectionWithRowNumberAlias = new ColumnProjection(null, ROW_NUMBER_COLUMN_NAME, ROW_NUMBER_COLUMN_ALIAS); ProjectionsContext projectionsContext = new ProjectionsContext(0, 0, false, Collections.singleton(projectionWithRowNumberAlias)); PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(null, projectionsContext, Collections.emptyList()); assertFalse(paginationContext.getOffsetSegment().isPresent()); assertFalse(paginationContext.getRowCountSegment().isPresent()); }
@Test public void assertCreatePaginationContextWhenRowNumberAliasIsPresentAndRowNumberPredicatesNotEmpty() { Projection projectionWithRowNumberAlias = new ColumnProjection(null, ROW_NUMBER_COLUMN_NAME, ROW_NUMBER_COLUMN_ALIAS); ProjectionsContext projectionsContext = new ProjectionsContext(0, 0, false, Collections.singleton(projectionWithRowNumberAlias)); AndPredicate andPredicate = new AndPredicate(); ColumnSegment left = new ColumnSegment(0, 10, new IdentifierValue(ROW_NUMBER_COLUMN_NAME)); BinaryOperationExpression predicateSegment = new BinaryOperationExpression(0, 0, left, null, null, null); andPredicate.getPredicates().addAll(Collections.singleton(predicateSegment)); PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(null, projectionsContext, Collections.emptyList()); assertFalse(paginationContext.getOffsetSegment().isPresent()); assertFalse(paginationContext.getRowCountSegment().isPresent()); }
@Test public void assertCreatePaginationContextWhenParameterMarkerRowNumberValueSegment() { Projection projectionWithRowNumberAlias = new ColumnProjection(null, ROW_NUMBER_COLUMN_NAME, ROW_NUMBER_COLUMN_ALIAS); ProjectionsContext projectionsContext = new ProjectionsContext(0, 0, false, Collections.singleton(projectionWithRowNumberAlias)); ColumnSegment left = new ColumnSegment(0, 10, new IdentifierValue(ROW_NUMBER_COLUMN_NAME)); ParameterMarkerExpressionSegment right = new ParameterMarkerExpressionSegment(0, 10, 0); BinaryOperationExpression expression = new BinaryOperationExpression(0, 0, left, right, ">", null); PaginationContext paginationContext = new RowNumberPaginationContextEngine() .createPaginationContext(expression, projectionsContext, Collections.singletonList(1)); Optional<PaginationValueSegment> offSetSegmentPaginationValue = paginationContext.getOffsetSegment(); assertTrue(offSetSegmentPaginationValue.isPresent()); assertThat(offSetSegmentPaginationValue.get(), instanceOf(ParameterMarkerRowNumberValueSegment.class)); assertFalse(paginationContext.getRowCountSegment().isPresent()); }