From 8d9e0f35cc8b780f2a1f3e9cba442330aa0245bf Mon Sep 17 00:00:00 2001 From: grabdoc Date: Wed, 7 Feb 2024 12:25:17 -0600 Subject: [PATCH] #259 - add limit and offset --- .../homihq/db2rest/rest/read/ReadService.java | 26 ++----------------- .../v2/processor/QueryCreatorTemplate.java | 14 ++++++++++ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/homihq/db2rest/rest/read/ReadService.java b/src/main/java/com/homihq/db2rest/rest/read/ReadService.java index 75b23e7c..91ad17af 100644 --- a/src/main/java/com/homihq/db2rest/rest/read/ReadService.java +++ b/src/main/java/com/homihq/db2rest/rest/read/ReadService.java @@ -1,8 +1,6 @@ package com.homihq.db2rest.rest.read; -import com.homihq.db2rest.rest.read.dto.FindOneResponse; import com.homihq.db2rest.rest.read.helper.*; -import com.homihq.db2rest.rest.read.dto.QueryRequest; import com.homihq.db2rest.rest.read.v2.dto.ReadContextV2; import com.homihq.db2rest.rest.read.v2.processor.QueryCreatorTemplate; import com.homihq.db2rest.rest.read.v2.processor.ReadPreProcessor; @@ -15,7 +13,7 @@ import java.util.List; import java.util.Map; -import java.util.Objects; + @Service @@ -42,7 +40,7 @@ public Object findAll(String schemaName, String tableName, String select, String .tableName(tableName).select(select).filter(filter).build(); selectBuilder.build(ctx); - joinBuilder.build(ctx); + //joinBuilder.build(ctx); whereBuilder.build(ctx); limitPaginationBuilder.build(ctx); sortBuilder.build(ctx); @@ -59,32 +57,12 @@ public Object findAll(String schemaName, String tableName, String select, String public Object findAll(ReadContextV2 readContextV2) { - for(ReadPreProcessor processor : processorList) { processor.process(readContextV2); } queryCreatorTemplate.createQuery(readContextV2); - /* - - selectBuilder.build(ctx); - joinBuilder.build(ctx); - whereBuilder.build(ctx); - limitPaginationBuilder.build(ctx); - sortBuilder.build(ctx); - - String sql = ctx.prepareSQL(); - Map bindValues = ctx.prepareParameters(); - - log.info("SQL - {}", sql); - log.info("Bind variables - {}", bindValues); - - return namedParameterJdbcTemplate.queryForList(sql, bindValues); - - - */ - return null; } diff --git a/src/main/java/com/homihq/db2rest/rest/read/v2/processor/QueryCreatorTemplate.java b/src/main/java/com/homihq/db2rest/rest/read/v2/processor/QueryCreatorTemplate.java index cd1a8903..eb2ed5f1 100644 --- a/src/main/java/com/homihq/db2rest/rest/read/v2/processor/QueryCreatorTemplate.java +++ b/src/main/java/com/homihq/db2rest/rest/read/v2/processor/QueryCreatorTemplate.java @@ -23,6 +23,8 @@ public void createQuery(ReadContextV2 readContextV2) { queryExpressionDSL.where(readContextV2.getWhereCondition()); } + addPagination(queryExpressionDSL, readContextV2.getOffset(), readContextV2.getLimit()); + SelectStatementProvider selectStatementProvider = queryExpressionDSL.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER); log.info("SQL - {}", selectStatementProvider.getSelectStatement()); @@ -30,6 +32,18 @@ public void createQuery(ReadContextV2 readContextV2) { } + protected void addPagination(QueryExpressionDSL queryExpressionDSL, long offset, int limit) { + + if(offset > -1) { + queryExpressionDSL.offset(offset); + } + + if(limit > -1) { + queryExpressionDSL.limit(limit); + } + + } + protected QueryExpressionDSL createProjection(ReadContextV2 readContextV2) { return select(readContextV2.getColumns())