PaginationContextEngine { public PaginationContext createPaginationContext(final SelectStatement selectStatement, final ProjectionsContext projectionsContext, final List<Object> parameters) { Optional<LimitSegment> limitSegment = SelectStatementHandler.getLimitSegment(selectStatement); if (limitSegment.isPresent()) { return new LimitPaginationContextEngine().createPaginationContext(limitSegment.get(), parameters); } Optional<TopProjectionSegment> topProjectionSegment = findTopProjection(selectStatement); Optional<WhereSegment> whereSegment = selectStatement.getWhere(); if (topProjectionSegment.isPresent()) { return new TopPaginationContextEngine().createPaginationContext( topProjectionSegment.get(), whereSegment.isPresent() ? whereSegment.get().getExpr() : null, parameters); } if (whereSegment.isPresent()) { return new RowNumberPaginationContextEngine().createPaginationContext(whereSegment.get().getExpr(), projectionsContext, parameters); } return new PaginationContext(null, null, parameters); }  PaginationContext createPaginationContext(final SelectStatement selectStatement, final ProjectionsContext projectionsContext, final List<Object> parameters);  }
@Test public void assertCreatePaginationContextWhenLimitSegmentIsPresentForMySQL() { MySQLSelectStatement selectStatement = new MySQLSelectStatement(); selectStatement.setLimit(new LimitSegment(0, 10, new NumberLiteralLimitValueSegment(0, 10, 100L), new NumberLiteralLimitValueSegment(0, 10, 100L))); PaginationContext paginationContext = new PaginationContextEngine().createPaginationContext(selectStatement, null, Collections.emptyList()); assertTrue(paginationContext.getOffsetSegment().isPresent()); assertTrue(paginationContext.getRowCountSegment().isPresent()); }
@Test public void assertCreatePaginationContextWhenLimitSegmentIsPresentForPostgreSQL() { PostgreSQLSelectStatement selectStatement = new PostgreSQLSelectStatement(); selectStatement.setLimit(new LimitSegment(0, 10, new NumberLiteralLimitValueSegment(0, 10, 100L), new NumberLiteralLimitValueSegment(0, 10, 100L))); PaginationContext paginationContext = new PaginationContextEngine().createPaginationContext(selectStatement, null, Collections.emptyList()); assertTrue(paginationContext.getOffsetSegment().isPresent()); assertTrue(paginationContext.getRowCountSegment().isPresent()); }
@Test public void assertCreatePaginationContextWhenLimitSegmentIsPresentForSQL92() { SQL92SelectStatement selectStatement = new SQL92SelectStatement(); selectStatement.setLimit(new LimitSegment(0, 10, new NumberLiteralLimitValueSegment(0, 10, 100L), new NumberLiteralLimitValueSegment(0, 10, 100L))); PaginationContext paginationContext = new PaginationContextEngine().createPaginationContext(selectStatement, null, Collections.emptyList()); assertTrue(paginationContext.getOffsetSegment().isPresent()); assertTrue(paginationContext.getRowCountSegment().isPresent()); }
@Test public void assertCreatePaginationContextWhenLimitSegmentIsPresentForSQLServer() { SQLServerSelectStatement selectStatement = new SQLServerSelectStatement(); selectStatement.setLimit(new LimitSegment(0, 10, new NumberLiteralLimitValueSegment(0, 10, 100L), new NumberLiteralLimitValueSegment(0, 10, 100L))); PaginationContext paginationContext = new PaginationContextEngine().createPaginationContext(selectStatement, null, Collections.emptyList()); assertTrue(paginationContext.getOffsetSegment().isPresent()); assertTrue(paginationContext.getRowCountSegment().isPresent()); }
@Test public void assertCreatePaginationContextWhenLimitSegmentAbsentAndTopSegmentPresent() { SQLServerSelectStatement subquerySelectStatement = new SQLServerSelectStatement(); subquerySelectStatement.setProjections(new ProjectionsSegment(0, 0)); subquerySelectStatement.getProjections().getProjections().add(new TopProjectionSegment(0, 10, null, "rowNumberAlias")); SQLServerSelectStatement selectStatement = new SQLServerSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(0, 0)); selectStatement.getProjections().getProjections().add(new SubqueryProjectionSegment(new SubquerySegment(0, 0, subquerySelectStatement))); PaginationContext paginationContext = new PaginationContextEngine().createPaginationContext(selectStatement, null, Collections.emptyList()); assertFalse(paginationContext.getOffsetSegment().isPresent()); assertFalse(paginationContext.getRowCountSegment().isPresent()); }
@Test public void assertCreatePaginationContextWhenLimitSegmentTopSegmentAbsentAndWhereSegmentPresent() { SQLServerSelectStatement selectStatement = new SQLServerSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(0, 0)); selectStatement.setWhere(new WhereSegment(0, 10, null)); ProjectionsContext projectionsContext = new ProjectionsContext(0, 0, false, Collections.emptyList()); PaginationContext paginationContext = new PaginationContextEngine().createPaginationContext(selectStatement, projectionsContext, Collections.emptyList()); assertFalse(paginationContext.getOffsetSegment().isPresent()); assertFalse(paginationContext.getRowCountSegment().isPresent()); }