Skip to content

Commit

Permalink
Merge pull request #271 from kdhrubo/feature/270_revamp_query_engine
Browse files Browse the repository at this point in the history
Feature/270 revamp query engine
  • Loading branch information
kdhrubo authored Feb 12, 2024
2 parents 97349b2 + 4d68d66 commit 26faf05
Show file tree
Hide file tree
Showing 46 changed files with 510 additions and 436 deletions.
46 changes: 15 additions & 31 deletions src/main/java/com/homihq/db2rest/rest/read/ReadController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

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;


@RestController
@Slf4j
@RequiredArgsConstructor
Expand All @@ -23,28 +17,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}/_expand" , 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 +46,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: 9 additions & 38 deletions src/main/java/com/homihq/db2rest/rest/read/ReadService.java
Original file line number Diff line number Diff line change
@@ -1,71 +1,42 @@
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 com.homihq.db2rest.rest.read.processor.ReadProcessor;
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 List<ReadProcessor> 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);

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

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

return namedParameterJdbcTemplate.queryForList(sql, bindValues);

}

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

String sql = queryCreatorTemplate.createQuery(readContextV2);
log.info("{}", sql);
log.info("{}", readContextV2.getParamMap());
return namedParameterJdbcTemplate.queryForList(sql, readContextV2.getParamMap());
}
catch (Exception e) {
e.printStackTrace();
}

return null;
}

Expand Down
18 changes: 12 additions & 6 deletions src/main/java/com/homihq/db2rest/rest/read/dto/JoinDetail.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.homihq.db2rest.rest.read.dto;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.dynamic.sql.select.join.JoinType;

import java.util.List;
import java.util.Objects;


public record JoinDetail (String table, List<String> fields,String on, List<String> andFilters, String type){
public record JoinDetail (String table, String with, List<String> fields,
List<String> on, String filter, String type){

public JoinType getJoinType() {
return StringUtils.isBlank(type) ? JoinType.INNER :
JoinType.valueOf(StringUtils.upperCase(type));
public String getJoinType() {
return StringUtils.isBlank(type) ? "INNER" :
StringUtils.upperCase(type);

}
public boolean hasOn() {
return Objects.nonNull(on) && !on.isEmpty();
}


public boolean hasFilter() {
return StringUtils.isNotBlank(filter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.validation.constraints.NotEmpty;
import java.util.Map;

@Deprecated
public record QueryRequest(@NotEmpty(message = "Sql statement cannot be empty") String sql, Map<String, Object> params,
boolean single) {
}
37 changes: 20 additions & 17 deletions src/main/java/com/homihq/db2rest/rest/read/dto/ReadContextV2.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.homihq.db2rest.rest.read.dto;

import com.homihq.db2rest.mybatis.MyBatisTable;
import com.homihq.db2rest.rest.read.dto.JoinDetail;

import com.homihq.db2rest.rest.read.model.DbColumn;
import com.homihq.db2rest.rest.read.model.DbJoin;
import com.homihq.db2rest.rest.read.model.DbTable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlCriterion;

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

import java.util.*;

@Builder
@AllArgsConstructor
Expand All @@ -32,25 +30,30 @@ public class ReadContextV2 {
List<JoinDetail> joins;


/* Processed attributes */
MyBatisTable rootTable;
List<BasicColumn> columns;
SqlCriterion whereCondition;


/* Attributes to replace the ones above */
DbTable root;
List<DbColumn> cols;
String rootWhere;
Map<String,Object> paramMap;

public void addWhereCondition(SqlCriterion whereCondition) {
this.whereCondition = whereCondition;
List<DbJoin> dbJoins;

public boolean createParamMap() {
if(Objects.isNull(paramMap)) {
paramMap = new HashMap<>();
return true;
}
else{
return false;
}
}

public void addColumns(List<DbColumn> columnList) {
this.cols.addAll(columnList);
}

public void addJoin(DbJoin join) {
if(Objects.isNull(dbJoins)) dbJoins = new ArrayList<>();

public void addColumns(List<BasicColumn> columnList) {
this.columns.addAll(columnList);
dbJoins.add(join);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.homihq.db2rest.rest.read.helper;


import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class AliasGenerator {
private Random random = new Random();
public String getAlias(String prefix, int length, String sqlIdentifier) {
return
sqlIdentifier.length() > length ?
prefix + sqlIdentifier.substring(0,length) + "_" +random.nextInt(2000)
: prefix + sqlIdentifier + "_" +random.nextInt(2000);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@Component
@Slf4j
@RequiredArgsConstructor
@Deprecated
public class JoinBuilder {

private final SchemaManager schemaManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@Component
@Slf4j
@RequiredArgsConstructor
@Deprecated
public class LimitPaginationBuilder {

public void build(ReadContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
@NoArgsConstructor
@Data
@Slf4j
@Deprecated
public class ReadContext {

String schemaName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@Component
@Slf4j
@RequiredArgsConstructor
@Deprecated
public class SelectBuilder{

private final SchemaManager schemaManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@Component
@Slf4j
@RequiredArgsConstructor
@Deprecated
public class SortBuilder {

public void build(ReadContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public abstract class TableUtil {

private TableUtil() {}



public static DbTable getTable(String tableName) {
//check if table contains schema name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@Component
@RequiredArgsConstructor
@Slf4j
@Deprecated
public class WhereBuilder{


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

import java.sql.JDBCType;

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

public String render() {
return tableAlias + "."+ name + " " + alias;
}

public String getAliasedName() {return tableAlias + "."+ name;}

public String getAliasedNameParam() {return tableAlias + "_"+ name;}
}
Loading

0 comments on commit 26faf05

Please sign in to comment.