Skip to content

Commit

Permalink
added function to pg script, replaced QueryRequest class with record,…
Browse files Browse the repository at this point in the history
… moved QueryRequest and CountResponse to dto
  • Loading branch information
souravroy committed Jan 21, 2024
1 parent 3c0ccaa commit aa43554
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Object> 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));
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/homihq/db2rest/rest/read/ReadService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.homihq.db2rest.rest.read.model;
package com.homihq.db2rest.rest.read.dto;

public record CountResponse(long count) {
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> params,
boolean single) {
}
19 changes: 0 additions & 19 deletions src/main/java/com/homihq/db2rest/rest/read/model/QueryRequest.java

This file was deleted.

7 changes: 5 additions & 2 deletions src/test/resources/mysql/mysql-sakila.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 19 additions & 2 deletions src/test/resources/pg/postgres-sakila.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aa43554

Please sign in to comment.