Skip to content

Commit

Permalink
#266 - creating base for compound join
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Feb 10, 2024
1 parent 722f3ef commit 787e886
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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.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
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, String andFilter, String type){

public JoinType getJoinType() {
return StringUtils.isBlank(type) ? JoinType.INNER :
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.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
Expand Up @@ -3,12 +3,20 @@
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.operators.SimpleRSQLOperators;
import com.homihq.db2rest.rsql.parser.JoinOnParser;
import com.homihq.db2rest.rsql.parser.JoinWhereFilterVisitor;
import com.homihq.db2rest.rsql.parser.WhereFilterVisitor;
import com.homihq.db2rest.schema.SchemaManager;
import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.dynamic.sql.SqlCriterion;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.join.JoinCriterion;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -56,6 +64,17 @@ private void createInnerJoin(MyBatisTable rootTable, MyBatisTable childTable, Jo

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

if(StringUtils.isNotBlank(join.andFilter())) {

log.info("**** Creating JOIN where condition *****");

Node rootNode = new RSQLParser(SimpleRSQLOperators.customOperators()).parse(join.andFilter());

JoinCriterion condition = rootNode
.accept(new JoinWhereFilterVisitor(childTable));

}

}

private void createFullJoin(MyBatisTable rootTable, MyBatisTable myBatisTable, JoinDetail join, QueryExpressionDSL<SelectModel> queryExpressionDSL) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.homihq.db2rest.rest.read.processor.post;

import com.homihq.db2rest.rest.read.dto.ReadContextV2;
import com.homihq.db2rest.rest.read.processor.pre.ReadPreProcessor;
import com.homihq.db2rest.rsql.operators.SimpleRSQLOperators;
import com.homihq.db2rest.rsql.parser.WhereFilterVisitor;
import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.dynamic.sql.SqlCriterion;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//@Component
@Slf4j
//@Order(6)
public class JoinWhereProcessor implements ReadPreProcessor {
@Override
public void process(ReadContextV2 readContextV2) {
if(StringUtils.isNotBlank(readContextV2.getFilter())) {

log.info("**** Creating JOIN where condition *****");

Node rootNode = new RSQLParser(SimpleRSQLOperators.customOperators()).parse(readContextV2.getFilter());

SqlCriterion condition = rootNode
.accept(new WhereFilterVisitor(readContextV2.getRootTable()));

readContextV2.addWhereCondition(condition);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.homihq.db2rest.rest.read.dto.ReadContextV2;
import com.homihq.db2rest.rsql.operators.SimpleRSQLOperators;
import com.homihq.db2rest.rsql.parser.MyBatisFilterVisitorParser;
import com.homihq.db2rest.rsql.parser.JoinWhereFilterVisitor;
import com.homihq.db2rest.rsql.parser.WhereFilterVisitor;
import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.dynamic.sql.SqlCriterion;
import org.mybatis.dynamic.sql.select.join.JoinCriterion;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

Expand All @@ -19,14 +21,15 @@ public class RootWhereProcessor implements ReadPreProcessor {
public void process(ReadContextV2 readContextV2) {
if(StringUtils.isNotBlank(readContextV2.getFilter())) {

log.info("-Creating where condition -");
log.info("-Creating root where condition -");

Node rootNode = new RSQLParser(SimpleRSQLOperators.customOperators()).parse(readContextV2.getFilter());

SqlCriterion condition = rootNode
.accept(new MyBatisFilterVisitorParser(readContextV2.getRootTable()));
JoinCriterion condition = rootNode
.accept(new JoinWhereFilterVisitor(readContextV2.getRootTable()));

readContextV2.addWhereCondition(condition);
log.info("condition - {}", condition);
//readContextV2.addWhereCondition(condition);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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.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 @@ -75,7 +75,7 @@ private void addWhere(String filter, MyBatisTable table, UpdateDSL<UpdateModel>
Node rootNode = new RSQLParser(SimpleRSQLOperators.customOperators()).parse(filter);

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

updateDSL.where(condition);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.homihq.db2rest.rsql.parser;


import com.homihq.db2rest.mybatis.MyBatisTable;
import com.homihq.db2rest.rsql.operators.Operator;
import com.homihq.db2rest.rsql.operators.RSQLOperatorHandlers;
import cz.jirutka.rsql.parser.ast.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.AndOrCriteriaGroup;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlCriterion;
import org.mybatis.dynamic.sql.select.join.JoinCriterion;

import java.util.ArrayList;
import java.util.List;

import static com.homihq.db2rest.schema.TypeMapperUtil.getColumnJavaType;
import static com.homihq.db2rest.schema.TypeMapperUtil.getJdbcType;
import static org.mybatis.dynamic.sql.SqlBuilder.*;


@RequiredArgsConstructor
@Slf4j
public class JoinWhereFilterVisitor implements RSQLVisitor<JoinCriterion, Object> {
private final MyBatisTable sqlTable;

@Override
public JoinCriterion visit(AndNode node, Object optionalParameter) {
log.info("AndNode - {}", node);

List<AndOrCriteriaGroup> criterionList = new ArrayList<>();

for (Node child : node.getChildren()) {
//criterionList.add(and(child.accept(this, optionalParameter)));
}

//return processCriteriaGroup(criterionList);

return null;
}

@Override
public JoinCriterion visit(OrNode node, Object optionalParameter) {
log.info("OrNode - {}", node);

List<AndOrCriteriaGroup> criterionList = new ArrayList<>();

for (Node child : node.getChildren()) {
//criterionList.add(or(child.accept(this, optionalParameter)));
}

//return processCriteriaGroup(criterionList);

return null;
}

@Override
public JoinCriterion visit(ComparisonNode comparisonNode, Object optionalParameter) {
log.info("ComparisonNode - {}", comparisonNode);

ComparisonOperator op = comparisonNode.getOperator();
String columnName = comparisonNode.getSelector();

Operator operatorHandler = RSQLOperatorHandlers.getOperatorHandler(op.getSymbol());

if (operatorHandler == null) {
throw new IllegalArgumentException(String.format("Operator '%s' is invalid", op.getSymbol()));
}

SqlColumn<Object> column =
SqlColumn.of(columnName, this.sqlTable,
getJdbcType(this.sqlTable.findColumn(columnName)));


//Java type
Class<?> clazz = getColumnJavaType(sqlTable.getTable(), columnName);

log.debug("Col - {} Clazz - {}",columnName , clazz);

/*
if (op.isMultiValue()) {
return operatorHandler.handle(column, comparisonNode.getArguments(), clazz);
}
else {
return operatorHandler.handle(column, comparisonNode.getArguments().get(0), clazz);
}
*/

return null;

}

private SqlCriterion processCriteriaGroup(List<AndOrCriteriaGroup> criterionList){
if(criterionList.isEmpty()){
throw new InternalError("criterionList should never be empty here");
}

AndOrCriteriaGroup initialCriterion = criterionList.get(0);
List<AndOrCriteriaGroup> criterionSiblings = criterionList.subList(1, criterionList.size());
if(!initialCriterion.subCriteria().isEmpty()){
throw new InternalError("Unexpected subCriteria found. Initial criterion should never contain subCriteria. This is most likely a coding bug.");
}
return group(initialCriterion.initialCriterion().orElseThrow(), criterionSiblings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

@RequiredArgsConstructor
@Slf4j
public class MyBatisFilterVisitorParser implements RSQLVisitor<SqlCriterion, Object> {
public class WhereFilterVisitor implements RSQLVisitor<SqlCriterion, Object> {
private final MyBatisTable sqlTable;

@Override
Expand Down

0 comments on commit 787e886

Please sign in to comment.