Skip to content

Commit

Permalink
#270 - executing query with single tables
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Feb 12, 2024
1 parent 97349b2 commit 86eb630
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 144 deletions.
45 changes: 15 additions & 30 deletions src/main/java/com/homihq/db2rest/rest/read/ReadController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

import com.homihq.db2rest.rest.read.dto.JoinDetail;
import com.homihq.db2rest.rest.read.dto.ReadContextV2;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import static org.springframework.web.bind.ServletRequestUtils.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -23,28 +18,26 @@ public class ReadController {

@GetMapping(value = "/{tableName}" , produces = "application/json")
public Object findAll(@PathVariable String tableName,
@RequestHeader(name = "Accept-Profile", required = false) String schemaName,
@RequestParam(name = "select", required = false, defaultValue = "") String select,
@RequestParam(name = "filter", required = false, defaultValue = "") String filter,
Sort sort,
Pageable pageable, HttpServletRequest httpServletRequest) {

log.info("schemaName - {}", schemaName);
log.info("select - {}", select);
log.info("filter - {}", filter);
log.info("pageable - {}", pageable);
@RequestParam(name = "fields", required = false, defaultValue = "*") String fields,
@RequestParam(name = "filter", required = false, defaultValue = "") String filter,
@RequestParam(name = "sort", required = false, defaultValue = "") List<String> sorts,
@RequestParam(name = "limit", required = false, defaultValue = "-1") int limit,
@RequestParam(name = "offset", required = false, defaultValue = "-1") long offset) {


if( getIntParameter(httpServletRequest, "page", -1) == -1 &&
getIntParameter(httpServletRequest, "size", -1) == -1) {
pageable = Pageable.unpaged(sort);
}
ReadContextV2 readContextV2 = ReadContextV2.builder()
.tableName(tableName)
.fields(fields)
.filter(filter)
.sorts(sorts)
.limit(limit)
.offset(offset)
.build();


return readService.findAll(schemaName, tableName,select, filter, pageable, sort);
return readService.findAll(readContextV2);
}

@PostMapping(value = "/V2/{tableName}" , produces = "application/json")
@PostMapping(value = "/{tableName}" , produces = "application/json")
public Object find(@PathVariable String tableName,
@RequestParam(name = "fields", required = false, defaultValue = "*") String fields,
@RequestParam(name = "filter", required = false, defaultValue = "") String filter,
Expand All @@ -54,14 +47,6 @@ public Object find(@PathVariable String tableName,
@RequestBody List<JoinDetail> joins
) {

log.info("fields - {}", fields);
log.info("filter - {}", filter);
log.info("sort - {}", sorts);
log.info("limit - {}", limit);
log.info("offset - {}", offset);

log.info("join - {}", joins);

ReadContextV2 readContextV2 = ReadContextV2.builder()
.tableName(tableName)
.fields(fields)
Expand Down
47 changes: 6 additions & 41 deletions src/main/java/com/homihq/db2rest/rest/read/ReadService.java
Original file line number Diff line number Diff line change
@@ -1,72 +1,37 @@
package com.homihq.db2rest.rest.read;

import com.homihq.db2rest.rest.read.helper.*;
import com.homihq.db2rest.rest.read.dto.ReadContextV2;
import com.homihq.db2rest.rest.read.processor.QueryCreatorTemplate;
import com.homihq.db2rest.rest.read.processor.pre.ReadPreProcessor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;



@Service
@Slf4j
@RequiredArgsConstructor
public class ReadService {

private final SelectBuilder selectBuilder;
private final JoinBuilder joinBuilder;
private final WhereBuilder whereBuilder;
private final LimitPaginationBuilder limitPaginationBuilder;
private final SortBuilder sortBuilder;

private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

private final List<ReadPreProcessor> processorList;
private final QueryCreatorTemplate queryCreatorTemplate;

public Object findAll(String schemaName, String tableName, String select, String filter,
Pageable pageable, Sort sort) {
ReadContext ctx = ReadContext.builder()
.pageable(pageable).sort(sort)
.schemaName(schemaName)
.tableName(tableName).select(select).filter(filter).build();

selectBuilder.build(ctx);
//joinBuilder.build(ctx);
whereBuilder.build(ctx);
limitPaginationBuilder.build(ctx);
sortBuilder.build(ctx);
public Object findAll(ReadContextV2 readContextV2) {

String sql = ctx.prepareSQL();
Map<String,Object> bindValues = ctx.prepareParameters();
for (ReadPreProcessor processor : processorList) {
processor.process(readContextV2);
}

log.info("SQL - {}", sql);
log.info("Bind variables - {}", bindValues);
String sql = queryCreatorTemplate.createQuery(readContextV2);

return namedParameterJdbcTemplate.queryForList(sql, bindValues);
return namedParameterJdbcTemplate.queryForList(sql, readContextV2.getParamMap());

}

public Object findAll(ReadContextV2 readContextV2) {
try {
for (ReadPreProcessor processor : processorList) {
processor.process(readContextV2);
}
log.info("** TIME TO GENERATE QUERY **");
queryCreatorTemplate.createQuery(readContextV2);
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ public class ReadContextV2 {
String rootWhere;
Map<String,Object> paramMap;

public void addWhereCondition(SqlCriterion whereCondition) {
this.whereCondition = whereCondition;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@

public record DbColumn(String tableName, String name, JDBCType jdbcType, Column column, String alias) {


public String render() {
return name + " " + alias;
}
}
Original file line number Diff line number Diff line change
@@ -1,71 +1,48 @@
package com.homihq.db2rest.rest.read.processor;

import com.homihq.db2rest.rest.read.dto.ReadContextV2;
import com.homihq.db2rest.rest.read.processor.post.ReadPostProcessor;
import com.homihq.db2rest.template.SqlRenderer;
import com.homihq.db2rest.rest.read.model.DbColumn;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

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

@Component
@Slf4j
@RequiredArgsConstructor
public class QueryCreatorTemplate {

private final List<ReadPostProcessor> postProcessors;
private final SqlRenderer sqlRenderer;

public void createQuery(ReadContextV2 readContextV2) {
private final SpringTemplateEngine templateEngine;
public String createQuery(ReadContextV2 readContextV2) {

log.info("**** Preparing to render ****");

Map<String,Object> data = new HashMap<>();
data.put("columns", readContextV2.getCols());
data.put("columns", createProjections(readContextV2.getCols()));
data.put("rootTable", readContextV2.getRoot());
data.put("rootWhere", readContextV2.getRootWhere());
sqlRenderer.render("read", data);

Context context = new Context();
context.setVariables(data);
return templateEngine.process("read", context);

/*
QueryExpressionDSL<SelectModel> queryExpressionDSL = createProjection(readContextV2);
if(Objects.nonNull(readContextV2.getWhereCondition())) {
queryExpressionDSL.where(readContextV2.getWhereCondition());
}
addSort(queryExpressionDSL, readContextV2.getSorts());
for(ReadPostProcessor processor : postProcessors) {
processor.process(queryExpressionDSL, readContextV2);
}
*/

//SelectStatementProvider selectStatementProvider = queryExpressionDSL.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);
}

//log.info("SQL - {}", selectStatementProvider.getSelectStatement());
//log.info("Bind Variables - {}", selectStatementProvider.getParameters());
public String createProjections(List<DbColumn> columns) {
List<String> columList =
columns.stream().map(DbColumn::render).toList();

return StringUtils.join(columList, "\n\t,");
}

private void addSort(QueryExpressionDSL<SelectModel> queryExpressionDSL, List<String> sorts) {
}


protected QueryExpressionDSL<SelectModel> createProjection(ReadContextV2 readContextV2) {
return
select(readContextV2.getColumns())
.from(readContextV2.getRootTable(), readContextV2.getRootTable().getAlias());
}
}
14 changes: 11 additions & 3 deletions src/main/java/com/homihq/db2rest/schema/TypeMapperUtil.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.homihq.db2rest.schema;

import com.homihq.db2rest.exception.InvalidColumnException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import schemacrawler.schema.Column;
import schemacrawler.schema.Table;

import java.sql.Types;
import java.sql.JDBCType;

@Slf4j
public class TypeMapperUtil {

private TypeMapperUtil() {}
Expand Down Expand Up @@ -41,8 +43,9 @@ public static Class<?> getColumnJavaType(Table table, String columnName) {
}

public static JDBCType getJdbcType(Column column) {

switch (column.getColumnDataType().getJavaSqlType().getVendorTypeNumber()) {
case Types.VARCHAR, 1111 -> {
case Types.VARCHAR, 1111, Types.LONGNVARCHAR -> {
return JDBCType.VARCHAR;
}
case Types.INTEGER, 2001 -> {
Expand All @@ -63,7 +66,7 @@ public static JDBCType getJdbcType(Column column) {
case Types.DECIMAL, Types.NUMERIC -> {
return JDBCType.DECIMAL;
}
case Types.SMALLINT, Types.BIT -> {
case Types.SMALLINT, Types.BIT, Types.TINYINT -> {
return JDBCType.SMALLINT;
}
case Types.BLOB, Types.BINARY -> {
Expand All @@ -72,7 +75,8 @@ public static JDBCType getJdbcType(Column column) {
case Types.CLOB -> {
return JDBCType.CLOB;
}
case 2147483647 -> {
case 2147483647, -1 -> {
log.info("here");
JDBCType jt = getJdbcTypeForUnknownJavaSqlType(column);
if (jt != null) {
return jt;
Expand All @@ -86,9 +90,13 @@ protected static JDBCType getJdbcTypeForUnknownJavaSqlType(Column column) {
/*
* unknown type
*/

if (column.getColumnDataType().getName().contains("TIMESTAMP")) {
return JDBCType.TIMESTAMP;
}
else if (column.getColumnDataType().getName().contains("TEXT")) {
return JDBCType.VARCHAR;
}
return null;
}
}
26 changes: 0 additions & 26 deletions src/main/java/com/homihq/db2rest/template/SqlRenderer.java

This file was deleted.

3 changes: 1 addition & 2 deletions src/main/resources/sql-templates/read.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
SELECT
[# th:each="column : ${columns}"]
[(${column.name})][/]
[(${columns})]
FROM
[(${rootTable.name})]
[# th:if="${rootWhere}"]WHERE
Expand Down

0 comments on commit 86eb630

Please sign in to comment.