From aa43554d6e604d4a0d7618a651bb29d09a4d305f Mon Sep 17 00:00:00 2001 From: Sourav Roy Date: Sun, 21 Jan 2024 00:00:11 +0000 Subject: [PATCH] added function to pg script, replaced QueryRequest class with record, moved QueryRequest and CountResponse to dto --- .../rest/read/CountQueryController.java | 2 +- .../db2rest/rest/read/CountQueryService.java | 2 +- .../db2rest/rest/read/ReadController.java | 4 ++-- .../homihq/db2rest/rest/read/ReadService.java | 8 +++---- .../read/{model => dto}/CountResponse.java | 2 +- .../db2rest/rest/read/dto/QueryRequest.java | 8 +++++++ .../db2rest/rest/read/model/QueryRequest.java | 19 ----------------- src/test/resources/mysql/mysql-sakila.sql | 7 +++++-- src/test/resources/pg/postgres-sakila.sql | 21 +++++++++++++++++-- 9 files changed, 41 insertions(+), 32 deletions(-) rename src/main/java/com/homihq/db2rest/rest/read/{model => dto}/CountResponse.java (50%) create mode 100644 src/main/java/com/homihq/db2rest/rest/read/dto/QueryRequest.java delete mode 100644 src/main/java/com/homihq/db2rest/rest/read/model/QueryRequest.java diff --git a/src/main/java/com/homihq/db2rest/rest/read/CountQueryController.java b/src/main/java/com/homihq/db2rest/rest/read/CountQueryController.java index aa8f9118..c83a17f2 100644 --- a/src/main/java/com/homihq/db2rest/rest/read/CountQueryController.java +++ b/src/main/java/com/homihq/db2rest/rest/read/CountQueryController.java @@ -1,6 +1,6 @@ package com.homihq.db2rest.rest.read; -import com.homihq.db2rest.rest.read.model.CountResponse; +import com.homihq.db2rest.rest.read.dto.CountResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; diff --git a/src/main/java/com/homihq/db2rest/rest/read/CountQueryService.java b/src/main/java/com/homihq/db2rest/rest/read/CountQueryService.java index 8cc0a859..54b0b784 100644 --- a/src/main/java/com/homihq/db2rest/rest/read/CountQueryService.java +++ b/src/main/java/com/homihq/db2rest/rest/read/CountQueryService.java @@ -2,7 +2,7 @@ import com.homihq.db2rest.rest.read.helper.*; -import com.homihq.db2rest.rest.read.model.CountResponse; +import com.homihq.db2rest.rest.read.dto.CountResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; diff --git a/src/main/java/com/homihq/db2rest/rest/read/ReadController.java b/src/main/java/com/homihq/db2rest/rest/read/ReadController.java index 10f46480..f683c2f0 100644 --- a/src/main/java/com/homihq/db2rest/rest/read/ReadController.java +++ b/src/main/java/com/homihq/db2rest/rest/read/ReadController.java @@ -1,6 +1,6 @@ package com.homihq.db2rest.rest.read; -import com.homihq.db2rest.rest.read.model.QueryRequest; +import com.homihq.db2rest.rest.read.dto.QueryRequest; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; @@ -47,7 +47,7 @@ public Object findByJoinTable(@PathVariable String tableName, @PostMapping(value = "/query", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity findByCustomQuery(@RequestBody @Valid QueryRequest queryRequest) { - log.debug("Execute SQL statement {} with params {}", queryRequest.getSql(), queryRequest.getParams()); + log.debug("Execute SQL statement {} with params {}", queryRequest.sql(), queryRequest.params()); return ResponseEntity.ok(readService.findByCustomQuery(queryRequest)); } 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 41b43e65..987c623b 100644 --- a/src/main/java/com/homihq/db2rest/rest/read/ReadService.java +++ b/src/main/java/com/homihq/db2rest/rest/read/ReadService.java @@ -1,7 +1,7 @@ package com.homihq.db2rest.rest.read; import com.homihq.db2rest.rest.read.helper.*; -import com.homihq.db2rest.rest.read.model.QueryRequest; +import com.homihq.db2rest.rest.read.dto.QueryRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Pageable; @@ -49,8 +49,8 @@ public Object findAll(String schemaName, String tableName, String select, String Object findByCustomQuery(QueryRequest queryRequest) { - return queryRequest.isSingle() ? - namedParameterJdbcTemplate.queryForMap(queryRequest.getSql(), queryRequest.getParams()) : - namedParameterJdbcTemplate.queryForList(queryRequest.getSql(), queryRequest.getParams()); + return queryRequest.single() ? + namedParameterJdbcTemplate.queryForMap(queryRequest.sql(), queryRequest.params()) : + namedParameterJdbcTemplate.queryForList(queryRequest.sql(), queryRequest.params()); } } diff --git a/src/main/java/com/homihq/db2rest/rest/read/model/CountResponse.java b/src/main/java/com/homihq/db2rest/rest/read/dto/CountResponse.java similarity index 50% rename from src/main/java/com/homihq/db2rest/rest/read/model/CountResponse.java rename to src/main/java/com/homihq/db2rest/rest/read/dto/CountResponse.java index 76ef5b96..35c99672 100644 --- a/src/main/java/com/homihq/db2rest/rest/read/model/CountResponse.java +++ b/src/main/java/com/homihq/db2rest/rest/read/dto/CountResponse.java @@ -1,4 +1,4 @@ -package com.homihq.db2rest.rest.read.model; +package com.homihq.db2rest.rest.read.dto; public record CountResponse(long count) { } diff --git a/src/main/java/com/homihq/db2rest/rest/read/dto/QueryRequest.java b/src/main/java/com/homihq/db2rest/rest/read/dto/QueryRequest.java new file mode 100644 index 00000000..9c8103c9 --- /dev/null +++ b/src/main/java/com/homihq/db2rest/rest/read/dto/QueryRequest.java @@ -0,0 +1,8 @@ +package com.homihq.db2rest.rest.read.dto; + +import jakarta.validation.constraints.NotEmpty; +import java.util.Map; + +public record QueryRequest(@NotEmpty(message = "Sql statement cannot be empty") String sql, Map params, + boolean single) { +} diff --git a/src/main/java/com/homihq/db2rest/rest/read/model/QueryRequest.java b/src/main/java/com/homihq/db2rest/rest/read/model/QueryRequest.java deleted file mode 100644 index 6ef5457b..00000000 --- a/src/main/java/com/homihq/db2rest/rest/read/model/QueryRequest.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.homihq.db2rest.rest.read.model; - -import jakarta.validation.constraints.NotEmpty; -import lombok.Builder; -import lombok.Data; - -import java.util.Map; - -@Data -@Builder -public class QueryRequest { - - @NotEmpty(message = "Sql statement cannot be empty") - private String sql; - - private Map params; - - private boolean isSingle; -} diff --git a/src/test/resources/mysql/mysql-sakila.sql b/src/test/resources/mysql/mysql-sakila.sql index b0dc7829..478b03bf 100644 --- a/src/test/resources/mysql/mysql-sakila.sql +++ b/src/test/resources/mysql/mysql-sakila.sql @@ -147,6 +147,9 @@ CREATE PROCEDURE GetMovieRentalRateProc(IN movieTitle varchar(100), OUT rentalRa SELECT rental_rate INTO rentalRate FROM film WHERE title = movieTitle; END; +-- +-- Function +-- CREATE FUNCTION GetMovieRentalRateFunc(movieTitle varchar(100)) RETURNS DECIMAL(4, 2) @@ -154,6 +157,6 @@ CREATE FUNCTION GetMovieRentalRateFunc(movieTitle varchar(100)) BEGIN DECLARE rentalRate DECIMAL(4, 2); SET rentalRate = 0.00; -SELECT rental_rate INTO rentalRate FROM film WHERE title = movieTitle; -RETURN (rentalRate); + SELECT rental_rate INTO rentalRate FROM film WHERE title = movieTitle; + RETURN (rentalRate); END; diff --git a/src/test/resources/pg/postgres-sakila.sql b/src/test/resources/pg/postgres-sakila.sql index 06c35dc3..9a4fae3e 100644 --- a/src/test/resources/pg/postgres-sakila.sql +++ b/src/test/resources/pg/postgres-sakila.sql @@ -411,5 +411,22 @@ AS $$ BEGIN SELECT rental_rate INTO rentalRate FROM film WHERE title = movieTitle; END; -$$ --- call GetMovieRentalRateProc('ACADEMY DINOSAUR', null); \ No newline at end of file +$$; +-- call GetMovieRentalRateProc('ACADEMY DINOSAUR', null); + +-- +-- Function; Schema: public; Owner: postgres +-- + +CREATE OR REPLACE FUNCTION GetMovieRentalRateFunc(movieTitle varchar) + RETURNS numeric + LANGUAGE plpgsql +AS $$ + DECLARE + rentalRate numeric; + BEGIN + SELECT rental_rate INTO rentalRate FROM film WHERE title = movieTitle; + return rentalRate; + END; +$$; +-- select GetMovieRentalRateFunc('ACADEMY DINOSAUR'); \ No newline at end of file