Skip to content

Commit

Permalink
refactoring away the query engine
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Feb 6, 2024
1 parent c1456ca commit d403d5c
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 37 deletions.
19 changes: 11 additions & 8 deletions src/main/java/com/homihq/db2rest/mybatis/MyBatisTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,22 @@ public MyBatisTable(String schemaName, String tableName, Table table) {
sqlColumnList = new ArrayList<>();
}

public Class<?> findColumnType(String columnName) {
return table.getColumns()
.stream()
.filter(c -> StringUtils.equalsIgnoreCase(c.getName(), columnName))
.findFirst().orElseThrow(() -> new InvalidColumnException(tableName, columnName))
.getColumnDataType()
.getJavaSqlType().getDefaultMappedClass();
}


public MyBatisTable() {
super("MyBatisTable", MyBatisTable::new);
}


public Column findColumn(String columnName) {
return this.getTable()
.getColumns()
.stream()
.filter(col -> StringUtils.equalsIgnoreCase(columnName, col.getName()))
.findFirst()
.orElseThrow(() -> new InvalidColumnException(tableName, columnName));
}

public void addAllColumns() {
for(Column column : table.getColumns()) addColumn(column);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ else if(isCountQuery()) {

}

@Deprecated
private List<BasicColumn> getSelectionColumns(MyBatisTable rootTable, List<BasicColumn> columns) {
if(columns.isEmpty()) {
return
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/homihq/db2rest/rest/read/v2/ReadServiceV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

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

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

@Service
@Slf4j
Expand All @@ -24,15 +24,17 @@ public class ReadServiceV2 {

private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

private final List<ReadProcessor> processorList;

private final List<ReadPreProcessor> processorList;
private final QueryCreatorTemplate queryCreatorTemplate;
public Object find(ReadContextV2 readContextV2) {


for(ReadProcessor processor : processorList) {
for(ReadPreProcessor processor : processorList) {
processor.process(readContextV2);
}

queryCreatorTemplate.createQuery(readContextV2);

/*
selectBuilder.build(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlCriterion;

import java.util.List;

Expand All @@ -29,4 +30,13 @@ public class ReadContextV2 {
/* Processed attributes */
MyBatisTable rootTable;
List<BasicColumn> columns;
SqlCriterion whereCondition;

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

public void addColumns(BasicColumn column) {
columns.add(column);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.homihq.db2rest.rest.read.v2.processor;

import com.homihq.db2rest.rest.read.v2.dto.ReadContextV2;
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.springframework.stereotype.Component;

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

@Component
@Slf4j
public class QueryCreatorTemplate {

public void createQuery(ReadContextV2 readContextV2) {
QueryExpressionDSL<SelectModel> queryExpressionDSL = createProjection(readContextV2);
SelectStatementProvider selectStatementProvider = queryExpressionDSL.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER);

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

}

protected QueryExpressionDSL<SelectModel> createProjection(ReadContextV2 readContextV2) {
return
select(readContextV2.getColumns())
.from(readContextV2.getRootTable(), readContextV2.getRootTable().getAlias());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.homihq.db2rest.rest.read.v2.dto.ReadContextV2;

public interface ReadProcessor {
public interface ReadPreProcessor {

void process(ReadContextV2 readContextV2);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.homihq.db2rest.rest.read.v2.processor;

import com.homihq.db2rest.exception.InvalidColumnException;
import com.homihq.db2rest.mybatis.MyBatisTable;
import com.homihq.db2rest.rest.read.v2.dto.ReadContextV2;
import static com.homihq.db2rest.schema.TypeMapperUtil.getJdbcType;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -8,13 +10,16 @@
import org.mybatis.dynamic.sql.SqlColumn;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import schemacrawler.schema.Column;

import java.util.Arrays;
import java.util.List;


@Component
@Slf4j
@Order(2)
public class RootFieldProcessor implements ReadProcessor{
public class RootFieldPreProcessor implements ReadPreProcessor {
@Override
public void process(ReadContextV2 readContextV2) {
String fields = readContextV2.getFields();
Expand All @@ -26,29 +31,42 @@ public void process(ReadContextV2 readContextV2) {
fields = StringUtils.trim(fields);

log.info("Fields - {}", fields);

List<BasicColumn> columns;
if(StringUtils.equals("*", fields)) {

System.out.println("column - " + readContextV2.getRootTable()
.getTable().getColumns());

//include all fields of root table
List<BasicColumn> columns =
columns =
readContextV2.getRootTable()
.getTable().getColumns()
.stream()
/*
.peek(column -> {
System.out.println("column - " + column);
System.out.println("column - " + column.getColumnDataType().getName());
System.out.println("column jdbc type - " + getJdbcType(column));
})*/
.map(column -> (BasicColumn)SqlColumn.of(column.getName(), readContextV2.getRootTable(),
getJdbcType(column)))
.toList();

readContextV2.setColumns(columns);

}
else{ //query has specific columns so parse and map it.
columns =
Arrays.stream(readContextV2.getFields().split(","))
.map(col -> getColumn(col, readContextV2.getRootTable()))
.toList();

}

readContextV2.setColumns(columns);

}

private BasicColumn getColumn(String col, MyBatisTable rootTable) {
Column c =
rootTable.getTable()
.getColumns()
.stream()
.filter(column -> StringUtils.equalsIgnoreCase(col, column.getName()))
.findFirst()
.orElseThrow(()-> new InvalidColumnException(rootTable.getTableName(), col));

return (BasicColumn)SqlColumn.of(c.getName(), rootTable,
getJdbcType(c));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Slf4j
@RequiredArgsConstructor
@Order(1)
public class RootTableProcessor implements ReadProcessor{
public class RootTablePreProcessor implements ReadPreProcessor {

private final SchemaManager schemaManager;
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.homihq.db2rest.rest.read.v2.processor;

import com.homihq.db2rest.rest.read.v2.dto.ReadContextV2;
import com.homihq.db2rest.rsql.operators.SimpleRSQLOperators;
import com.homihq.db2rest.rsql.parser.MyBatisFilterVisitor;
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(3)
public class RootWhereProcessor implements ReadPreProcessor{
@Override
public void process(ReadContextV2 readContextV2) {
if(StringUtils.isNotBlank(readContextV2.getFilter())) {

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

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

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

readContextV2.addWhereCondition(condition);

}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/homihq/db2rest/rsql/operators/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ default SqlCriterion handle(SqlColumn<Object> column, List<String> value, Class<
return handle(column, value.get(0), type);
}

/*
default Object parseValue(String value, Class<?> type) {
if (String.class == type) {
Expand All @@ -31,4 +32,6 @@ else if (Integer.class == type || int.class == type) {
}
*/

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.homihq.db2rest.rsql.operators.handler;

import com.homihq.db2rest.rsql.operators.Operator;
import com.homihq.db2rest.schema.TypeMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
import org.mybatis.dynamic.sql.SqlColumn;
Expand All @@ -17,7 +18,7 @@ public SqlCriterion handle(SqlColumn<Object> column, String value, Class<?> type
log.info("Type - {}", type);

return ColumnAndConditionCriterion.withColumn(column)
.withCondition(isEqualTo(parseValue(value,type))).build();
.withCondition(isEqualTo(TypeMapperUtil.parseValue(value,type))).build();

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.homihq.db2rest.rsql.operators.handler;

import com.homihq.db2rest.rsql.operators.Operator;
import com.homihq.db2rest.schema.TypeMapperUtil;
import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlCriterion;
Expand All @@ -23,7 +24,7 @@ public SqlCriterion handle(SqlColumn<Object> column, String value, Class<?> type
@Override
public SqlCriterion handle(SqlColumn<Object> column, List<String> values, Class<?> type) {
Object [] v =
values.stream().map(value -> parseValue(value, type)).toList().toArray();
values.stream().map(value -> TypeMapperUtil.parseValue(value, type)).toList().toArray();

return ColumnAndConditionCriterion.withColumn(column)
.withCondition(isIn(v)).build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.homihq.db2rest.rsql.operators.handler;

import com.homihq.db2rest.rsql.operators.Operator;
import com.homihq.db2rest.schema.TypeMapperUtil;
import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlCriterion;
Expand All @@ -21,7 +22,7 @@ public SqlCriterion handle(SqlColumn<Object> column, String value, Class<?> type
@Override
public SqlCriterion handle(SqlColumn<Object> column, List<String> values, Class<?> type) {
Object [] v =
values.stream().map(value -> parseValue(value, type)).toList().toArray();
values.stream().map(value -> TypeMapperUtil.parseValue(value, type)).toList().toArray();

return ColumnAndConditionCriterion.withColumn(column)
.withCondition(isNotIn(v)).build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.homihq.db2rest.rsql.parser;


import static com.homihq.db2rest.schema.TypeMapperUtil.getJdbcType;
import static com.homihq.db2rest.schema.TypeMapperUtil.getColumnJavaType;
import static org.mybatis.dynamic.sql.SqlBuilder.*;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -47,20 +49,23 @@ public SqlCriterion visit(ComparisonNode comparisonNode, Object optionalParamete
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 = sqlTable.column(columnName);
SqlColumn<Object> column =
SqlColumn.of(columnName, this.sqlTable,
getJdbcType(this.sqlTable.findColumn(columnName)));

//Dummy type
Class<?> clazz = sqlTable.findColumnType(columnName);

log.info("Col - {} Clazz - {}",columnName , clazz);
//Java type
Class<?> clazz = getColumnJavaType(sqlTable.getTable(), columnName);

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

if (op.isMultiValue()) {

if (op.isMultiValue()) {
return operatorHandler.handle(column, comparisonNode.getArguments(), clazz);
}
else {
Expand Down
Loading

0 comments on commit d403d5c

Please sign in to comment.