Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/266 compound join #269

Merged
merged 7 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


<dependency>
<groupId>org.springdoc</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.homihq.db2rest.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;


@Configuration
public class ThymeleafConfiguration {

@Bean("textTemplateResolver")
public SpringResourceTemplateResolver textTemplateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("classpath:/sql-templates/");
templateResolver.setSuffix(".sql");
templateResolver.setTemplateMode(TemplateMode.TEXT);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setOrder(3);
templateResolver.setCheckExistence(true);
templateResolver.setCacheable(false);

return templateResolver;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.homihq.db2rest.config.Db2RestConfigProperties;
import com.homihq.db2rest.exception.GenericDataAccessException;
import com.homihq.db2rest.mybatis.MyBatisTable;
import com.homihq.db2rest.rsql.operators.SimpleRSQLOperators;
import com.homihq.db2rest.rsql.parser.MyBatisFilterVisitorParser;
import com.homihq.db2rest.rsql.v1.operators.SimpleRSQLOperators;
import com.homihq.db2rest.rsql.v1.parser.WhereFilterVisitor;
import com.homihq.db2rest.schema.SchemaManager;
import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
Expand Down Expand Up @@ -78,7 +78,7 @@ private void addWhere(String filter, MyBatisTable table, DeleteDSL<DeleteModel>
Node rootNode = new RSQLParser(SimpleRSQLOperators.customOperators()).parse(filter);

SqlCriterion condition = rootNode
.accept(new MyBatisFilterVisitorParser(table));
.accept(new WhereFilterVisitor(table));

deleteDSL.where(condition);
}
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/homihq/db2rest/rest/read/ReadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ public Object findAll(String schemaName, String tableName, String select, String
}

public Object findAll(ReadContextV2 readContextV2) {

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

queryCreatorTemplate.createQuery(readContextV2);

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;


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

public JoinType getJoinType() {
return StringUtils.isBlank(type) ? JoinType.INNER :
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/homihq/db2rest/rest/read/dto/ReadContextV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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.DbTable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -11,6 +13,7 @@
import org.mybatis.dynamic.sql.SqlCriterion;

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

@Builder
@AllArgsConstructor
Expand All @@ -35,10 +38,18 @@ public class ReadContextV2 {
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;
}



public void addColumns(List<BasicColumn> columnList) {
this.columns.addAll(columnList);
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/homihq/db2rest/rest/read/helper/TableUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public static DbTable getTable(String tableName) {
if(tableParts.length == 2) {
String table = tableParts[1];
DbAlias alias = getAlias(table);
return new DbTable(tableParts[0], alias.name(), alias.alias());
// return new DbTable(tableParts[0], alias.name(), alias.alias());
return null;
}
else{
String table = tableParts[0];
DbAlias alias = getAlias(table);
return new DbTable(null, alias.name(), alias.alias());
//return new DbTable(null, alias.name(), alias.alias());
return null;
}
}

Expand All @@ -33,12 +35,14 @@ public static DbColumn getColumn(String columName) {
if(columnParts.length == 2) {
String col = columnParts[1];
DbAlias alias = getAlias(col);
return new DbColumn(columnParts[0], alias.name(), alias.alias());
//return new DbColumn(columnParts[0], alias.name(), alias.alias());
return null;
}
else{
String col = columnParts[0];
DbAlias alias = getAlias(col);
return new DbColumn(null, alias.name(), alias.alias());
//return new DbColumn(null, alias.name(), alias.alias());
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.homihq.db2rest.rest.read.helper;

import com.homihq.db2rest.rsql.operators.SimpleRSQLOperators;
import com.homihq.db2rest.rsql.parser.MyBatisFilterVisitorParser;
import com.homihq.db2rest.rsql.v1.operators.SimpleRSQLOperators;
import com.homihq.db2rest.rsql.v1.parser.WhereFilterVisitor;
import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,7 +28,7 @@ public void build(ReadContext context) {
Node rootNode = new RSQLParser(SimpleRSQLOperators.customOperators()).parse(context.filter);

SqlCriterion condition = rootNode
.accept(new MyBatisFilterVisitorParser(context.from));
.accept(new WhereFilterVisitor(context.from));

context.addWhereClause(condition);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.homihq.db2rest.rest.read.model;


public record DbColumn(String tableName, String name, String alias) {
import schemacrawler.schema.Column;

import java.sql.JDBCType;

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


}
17 changes: 16 additions & 1 deletion src/main/java/com/homihq/db2rest/rest/read/model/DbTable.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
package com.homihq.db2rest.rest.read.model;

public record DbTable(String schema, String name, String alias) { }
import com.homihq.db2rest.exception.InvalidColumnException;
import org.apache.commons.lang3.StringUtils;
import schemacrawler.schema.Column;
import schemacrawler.schema.Table;

public record DbTable(String schema, String name, String alias, Table table) {

public Column lookupColumn(String columnName) {
return table
.getColumns()
.stream()
.filter(column -> StringUtils.equalsIgnoreCase(columnName, column.getName()))
.findFirst()
.orElseThrow(()-> new InvalidColumnException(name, columnName));
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/homihq/db2rest/rest/read/model/DbWhere.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.homihq.db2rest.rest.read.model;


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

public record DbWhere(String tableName, DbTable table, List<DbColumn> columns, Map<String,Object> paramMap) { }
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.homihq.db2rest.rest.read.dto.ReadContextV2;
import com.homihq.db2rest.rest.read.processor.post.ReadPostProcessor;
import com.homihq.db2rest.template.SqlRenderer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
Expand All @@ -10,7 +11,9 @@
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.springframework.stereotype.Component;

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;
Expand All @@ -21,8 +24,20 @@
public class QueryCreatorTemplate {

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

public void createQuery(ReadContextV2 readContextV2) {

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

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


/*
QueryExpressionDSL<SelectModel> queryExpressionDSL = createProjection(readContextV2);

if(Objects.nonNull(readContextV2.getWhereCondition())) {
Expand All @@ -35,10 +50,12 @@ public void createQuery(ReadContextV2 readContextV2) {
processor.process(queryExpressionDSL, readContextV2);
}

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

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

log.info("SQL - {}", selectStatementProvider.getSelectStatement());
log.info("Bind Variables - {}", selectStatementProvider.getParameters());
//log.info("SQL - {}", selectStatementProvider.getSelectStatement());
//log.info("Bind Variables - {}", selectStatementProvider.getParameters());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.homihq.db2rest.mybatis.MyBatisTable;
import com.homihq.db2rest.rest.read.dto.JoinDetail;
import com.homihq.db2rest.rest.read.dto.ReadContextV2;
import com.homihq.db2rest.rsql.parser.JoinOnParser;
import com.homihq.db2rest.rsql.v1.parser.JoinOnParser;
import com.homihq.db2rest.schema.SchemaManager;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
Expand Down Expand Up @@ -51,20 +52,22 @@ public void process(QueryExpressionDSL<SelectModel> queryExpressionDSL, ReadCont
}
}

private void createInnerJoin(MyBatisTable rootTable, MyBatisTable childTable, JoinDetail join, QueryExpressionDSL<SelectModel> queryExpressionDSL) {
private void createInnerJoin(MyBatisTable rootTable, MyBatisTable childTable, JoinDetail joinDetail, QueryExpressionDSL<SelectModel> queryExpressionDSL) {
log.info("Processing inner join");

joinOnParser.parse(rootTable, childTable, join.on(), queryExpressionDSL);
joinOnParser.parse(rootTable, childTable, joinDetail, queryExpressionDSL);



}

private void createFullJoin(MyBatisTable rootTable, MyBatisTable myBatisTable, JoinDetail join, QueryExpressionDSL<SelectModel> queryExpressionDSL) {
private void createFullJoin(MyBatisTable rootTable, MyBatisTable myBatisTable, JoinDetail joinDetail, QueryExpressionDSL<SelectModel> queryExpressionDSL) {
}

private void createLeftJoin(MyBatisTable rootTable, MyBatisTable myBatisTable, JoinDetail join, QueryExpressionDSL<SelectModel> queryExpressionDSL) {
private void createLeftJoin(MyBatisTable rootTable, MyBatisTable myBatisTable, JoinDetail joinDetail, QueryExpressionDSL<SelectModel> queryExpressionDSL) {
}

private void createRightJoin(MyBatisTable rootTable, MyBatisTable myBatisTable, JoinDetail join, QueryExpressionDSL<SelectModel> queryExpressionDSL) {
private void createRightJoin(MyBatisTable rootTable, MyBatisTable myBatisTable, JoinDetail joinDetail, QueryExpressionDSL<SelectModel> queryExpressionDSL) {
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import static com.homihq.db2rest.schema.TypeMapperUtil.getJdbcType;


@Component
//@Component
@Slf4j
@Order(3)
@RequiredArgsConstructor
Expand Down
Loading
Loading