Skip to content

Commit

Permalink
#266 - working on where clause
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Feb 11, 2024
1 parent 5f0df24 commit 94d0d00
Show file tree
Hide file tree
Showing 19 changed files with 386 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,14 @@ public class ReadContextV2 {
DbTable root;
List<DbColumn> cols;



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



public void addColumns(List<BasicColumn> columnList) {
this.columns.addAll(columnList);
}
Expand Down
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
@@ -1,33 +1,47 @@
package com.homihq.db2rest.rest.read.processor.pre;

import com.homihq.db2rest.rest.read.dto.ReadContextV2;
import com.homihq.db2rest.rsql.v1.operators.SimpleRSQLOperators;
import com.homihq.db2rest.rsql.v1.parser.WhereFilterVisitor;
import cz.jirutka.rsql.parser.RSQLParser;


import com.homihq.db2rest.rest.read.model.DbWhere;
import com.homihq.db2rest.rest.read.processor.rsql.parser.RSQLParserBuilder;
import com.homihq.db2rest.rest.read.processor.rsql.visitor.BaseRSQLVisitor;
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
import java.util.HashMap;
import java.util.Map;

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

Map<String,Object> paramMap = new HashMap<>();

DbWhere dbWhere = new DbWhere(
readContextV2.getTableName(),
readContextV2.getRoot(),readContextV2.getCols(),paramMap);

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

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

String where = rootNode
.accept(new BaseRSQLVisitor(
dbWhere));

SqlCriterion condition = rootNode
.accept(new WhereFilterVisitor(readContextV2.getRootTable()));
log.info("Where - {}", where);

log.info("param map - {}", paramMap);

readContextV2.addWhereCondition(condition);
//readContextV2.addWhereCondition(condition);

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator;

import cz.jirutka.rsql.parser.ast.ComparisonOperator;
import cz.jirutka.rsql.parser.ast.RSQLOperators;

import java.util.Arrays;
import java.util.Set;

public class CustomRSQLOperators extends RSQLOperators {

public static final ComparisonOperator LIKE = new ComparisonOperator("=like=", false);
public static final ComparisonOperator START_WITH = new ComparisonOperator("=startWith=", false);
public static final ComparisonOperator END_WITH = new ComparisonOperator("=endWith=", false);

public static Set<ComparisonOperator> customOperators() {
Set<ComparisonOperator> comparisonOperators = defaultOperators();
comparisonOperators.addAll(Arrays.asList(LIKE, START_WITH, END_WITH));
return comparisonOperators;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Map;

public class EndWithOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " like ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {
return columnName + OPERATOR + "'%" + value + "'";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Map;

public class EqualToOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " = ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {

Object vo = parseValue(value, type);

paramMap.put(columnName, vo);

return columnName + OPERATOR + PREFIX + columnName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Map;

public class GreaterThanEqualToOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " >= ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {
return columnName + OPERATOR + parseValue(value, type);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Map;

public class GreaterThanOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " > ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {
return columnName + OPERATOR + parseValue(value, type);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class InOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " in ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {
return handle(columnName, Arrays.asList(value), type, paramMap);
}

@Override
public String handle(String columnName, List<String> values, Class type, Map<String, Object> paramMap) {
return columnName + " in (" +
values.stream().map(value -> parseValue(value, type)).collect(Collectors.joining(",")) + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Map;

public class LessThanEqualToOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " <= ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {
return columnName + OPERATOR + parseValue(value, type);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Map;

public class LessThanOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " < ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {
return columnName + OPERATOR + parseValue(value, type);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Map;

public class LikeOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " like ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {
return columnName + OPERATOR + "'%" + value + "%'";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Map;

public class NotEqualToOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " != ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {
return columnName + OPERATOR + parseValue(value, type);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class NotInOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " not in ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {
return handle(columnName, Arrays.asList(value), type, paramMap);
}

@Override
public String handle(String columnName, List<String> values, Class type, Map<String, Object> paramMap) {
return columnName + " not in (" +
values.stream().map(value -> parseValue(value, type)).collect(Collectors.joining(",")) + ")";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

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

public interface OperatorHandler {

String PREFIX = ":";

String handle(String columnName, String value, Class type, Map<String, Object> paramMap);

default String handle(String columnName, List<String> value, Class type, Map<String, Object> paramMap) {
return handle(columnName, value.get(0), type, paramMap);
}

default String parseValue(String value, Class type) {

if (String.class == type) {
return "'" + value + "'";
}
else if (Boolean.class == type || boolean.class == type) {
Boolean aBoolean = Boolean.valueOf(value);
return aBoolean ? "1" : "0";
}
else {
return value;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;


import static com.homihq.db2rest.rest.read.processor.rsql.operator.CustomRSQLOperators.*;
import static cz.jirutka.rsql.parser.ast.RSQLOperators.*;

import java.util.HashMap;
import java.util.Map;

public class RSQLOperatorHandlers {

private static final Map<String, OperatorHandler> OPERATOR_HANDLER_MAP = new HashMap<>();

static {
OPERATOR_HANDLER_MAP.put(EQUAL.getSymbol(), new EqualToOperatorHandler());
OPERATOR_HANDLER_MAP.put(NOT_EQUAL.getSymbol(), new NotEqualToOperatorHandler());
OPERATOR_HANDLER_MAP.put(IN.getSymbol(), new InOperatorHandler());
OPERATOR_HANDLER_MAP.put(NOT_IN.getSymbol(), new NotInOperatorHandler());
OPERATOR_HANDLER_MAP.put(GREATER_THAN.getSymbol(), new GreaterThanOperatorHandler());
OPERATOR_HANDLER_MAP.put(GREATER_THAN_OR_EQUAL.getSymbol(), new GreaterThanEqualToOperatorHandler());
OPERATOR_HANDLER_MAP.put(LESS_THAN.getSymbol(), new LessThanOperatorHandler());
OPERATOR_HANDLER_MAP.put(LESS_THAN_OR_EQUAL.getSymbol(), new LessThanEqualToOperatorHandler());
OPERATOR_HANDLER_MAP.put(LIKE.getSymbol(), new LikeOperatorHandler());
OPERATOR_HANDLER_MAP.put(START_WITH.getSymbol(), new StartWithOperatorHandler());
OPERATOR_HANDLER_MAP.put(END_WITH.getSymbol(), new EndWithOperatorHandler());

}

public static OperatorHandler getOperatorHandler(String symbol) {
return OPERATOR_HANDLER_MAP.get(symbol);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.homihq.db2rest.rest.read.processor.rsql.operator.handler;

import java.util.Map;

public class StartWithOperatorHandler implements OperatorHandler {

private static final String OPERATOR = " like ";

@Override
public String handle(String columnName, String value, Class type, Map<String, Object> paramMap) {
return columnName + OPERATOR + "'" + value + "%'";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.homihq.db2rest.rest.read.processor.rsql.parser;

import com.homihq.db2rest.rest.read.processor.rsql.operator.CustomRSQLOperators;

import cz.jirutka.rsql.parser.RSQLParser;

public class RSQLParserBuilder {

public static RSQLParser newRSQLParser(){
return new RSQLParser(CustomRSQLOperators.customOperators());
}

}
Loading

0 comments on commit 94d0d00

Please sign in to comment.