Skip to content

Commit

Permalink
#11 - refactoring count query
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Jan 19, 2024
1 parent 0a1cdb7 commit 6719dba
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.homihq.db2rest.rest.read;

import com.homihq.db2rest.rest.read.model.CountResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequiredArgsConstructor
public class CountQueryController {
private final CountQueryService countQueryService;
@GetMapping("/{tableName}/count")
public CountResponse count(@PathVariable String tableName
, @RequestParam(name = "filter", required = false, defaultValue = "") String filter) {

log.info("tableName - {}", tableName);
log.info("filter - {}", filter);

return countQueryService.count(tableName, filter);
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/homihq/db2rest/rest/read/CountQueryService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.homihq.db2rest.rest.read;

import com.homihq.db2rest.rest.read.helper.*;

import com.homihq.db2rest.rest.read.model.CountResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
@Slf4j
@RequiredArgsConstructor
public class CountQueryService {

private final SelectBuilder selectBuilder;
private final WhereBuilder whereBuilder;
private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;


public CountResponse count(String tableName, String filter) {
ReadContext ctx = ReadContext.builder()
.select("count(*)")
.tableName(tableName)
.filter(filter)
.build();
selectBuilder.build(ctx);
whereBuilder.build(ctx);

String sql = ctx.prepareSQL();
Map<String, Object> bindValues = ctx.prepareParameters();

log.info("SQL count - {}", sql);
log.info("Bind variables - {}", bindValues);

Long itemCount = namedParameterJdbcTemplate.queryForObject(sql, bindValues, Long.class);

return new CountResponse(itemCount);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,5 @@ public ResponseEntity<Object> findByCustomQuery(@RequestBody @Valid QueryRequest
return ResponseEntity.ok(readService.findByCustomQuery(queryRequest));
}

@GetMapping(value = "/query/{tableName}/count", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Integer> getCount(@PathVariable String tableName) {
return ResponseEntity.ok(readService.findCount(tableName));
}

}
15 changes: 0 additions & 15 deletions src/main/java/com/homihq/db2rest/rest/read/ReadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,6 @@ public Object findAll(String schemaName, String tableName, String select, String

}

public int findCount(String tableName) {
ReadContext ctx = ReadContext.builder()
.tableName(tableName)
.select("*")
.build();
selectBuilder.build(ctx);

String sql = ctx.prepareSQL();
Map<String, Object> bindValues = ctx.prepareParameters();

log.info("SQL count - {}", sql);
log.info("Bind variables - {}", bindValues);

return namedParameterJdbcTemplate.queryForList(sql, bindValues).size();
}

Object findByCustomQuery(QueryRequest queryRequest) {
return queryRequest.isSingle() ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.homihq.db2rest.exception.GenericDataAccessException;
import com.homihq.db2rest.mybatis.MyBatisTable;
import lombok.*;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlCriterion;
Expand All @@ -20,6 +21,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.mybatis.dynamic.sql.SqlBuilder.count;
import static org.mybatis.dynamic.sql.select.SelectDSL.select;


Expand Down Expand Up @@ -48,7 +50,9 @@ public void addWhereClause(SqlCriterion condition) {
queryExpressionDSL.where(condition);
}


public boolean isCountQuery() {
return StringUtils.equals(select, "count(*)");
}

public SqlColumn<?> getSortColumn(String columnName) {
//for now just support root table
Expand All @@ -65,6 +69,10 @@ public void createSelect() {
if(union) {
createUnionQuery();
}
else if(isCountQuery()) {
from = getRootTable();
queryExpressionDSL = select(count()).from(from, from.getAlias());
}
else{
from = getRootTable();
queryExpressionDSL = select(columns).from(from, from.getAlias());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,35 @@ public class SelectBuilder{
private final Db2RestConfigProperties db2RestConfigProperties;

public void build(ReadContext context) {
context.setTables(createTables(context));
context.setTables(prepareTables(context));
context.createSelect();

}

private List<MyBatisTable> createTables(ReadContext context) {
private List<MyBatisTable> prepareTables(ReadContext context) {
List<MyBatisTable> tables = new ArrayList<>();
int counter = 0;

if(context.isCountQuery()) {
tables = createTables(
context.schemaName,
context.tableName, null, counter);

for(MyBatisTable table : tables) {
table.setRoot(true);
}

return tables;
}



log.info("context.select - {}", context.select);

//split to get all fragments
String [] tabCols = context.select.split(";");

int counter = 0;


log.info("tabCols - {}", tabCols.length);

Expand Down Expand Up @@ -90,15 +107,19 @@ private List<MyBatisTable> createTables(String schemaName, String tableName, St
if(StringUtils.isNotBlank(sName)) {
MyBatisTable table = schemaManager.findTable(sName, tName, counter);

addColumns(table, colStr);
if(StringUtils.isNotBlank(colStr)) {
addColumns(table, colStr);
}

return List.of(table);
}
else {
List<MyBatisTable> tables = schemaManager.findTables(tName);

for(MyBatisTable table : tables) {
addColumns(table, colStr);
if(StringUtils.isNotBlank(colStr)) {
addColumns(table, colStr);
}
}

return tables;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.homihq.db2rest.rest.read.model;

public record CountResponse(long count) {
}

0 comments on commit 6719dba

Please sign in to comment.