Skip to content

Commit

Permalink
refactor to new version of query engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
grabdoc committed Feb 6, 2024
1 parent 2a0703f commit 4703a05
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ private void createJoins(ReadContext context, List<MyBatisTable> tableList, int
MyBatisTable root = tableList.get(i);
MyBatisTable child = tableList.get(i + 1);

/*
List<ForeignKey> foreignKeys = schemaManager.getForeignKeysBetween(context.getSchemaName(),
root.getTableName(),
child.getTableName());
*/

List<ForeignKey> foreignKeys = schemaManager.getForeignKeysBetween(root, child);

log.debug("Foreign keys - {}", foreignKeys);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.homihq.db2rest.rest.read.v2;

import com.homihq.db2rest.rest.read.ReadService;

import com.homihq.db2rest.rest.read.v2.dto.ReadContextV2;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequiredArgsConstructor
public class ReadControllerV2 {

private final ReadServiceV2 readServiceV2;

@GetMapping(value = "/V2/{tableName}" , 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,
@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) {

log.info("fields - {}", fields);
log.info("filter - {}", filter);
log.info("sort - {}", sorts);
log.info("limit - {}", limit);
log.info("offset - {}", offset);

ReadContextV2 readContextV2 = ReadContextV2.builder()
.tableName(tableName)
.fields(fields)
.filter(filter)
.sorts(sorts)
.limit(limit)
.offset(offset).build();


return readServiceV2.find(readContextV2);

}



}
59 changes: 59 additions & 0 deletions src/main/java/com/homihq/db2rest/rest/read/v2/ReadServiceV2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.homihq.db2rest.rest.read.v2;

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 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
@RequiredArgsConstructor
public class ReadServiceV2 {

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<ReadProcessor> processorList;

public Object find(ReadContextV2 readContextV2) {


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

/*
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);
*/

return null;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.homihq.db2rest.rest.read.v2.dto;

import com.homihq.db2rest.mybatis.MyBatisTable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.dynamic.sql.BasicColumn;

import java.util.List;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Data
@Slf4j
public class ReadContextV2 {

/* Input Attributes */
String tableName;
String fields;
String filter;
List<String> sorts;
int limit;
long offset;


/* Processed attributes */
MyBatisTable rootTable;
List<BasicColumn> columns;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.homihq.db2rest.rest.read.v2.processor;

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

public interface ReadProcessor {

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

import com.homihq.db2rest.rest.read.v2.dto.ReadContextV2;
import static com.homihq.db2rest.schema.TypeMapperUtil.getJdbcType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlColumn;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
@Order(2)
public class RootFieldProcessor implements ReadProcessor{
@Override
public void process(ReadContextV2 readContextV2) {
String fields = readContextV2.getFields();

//There are 2 possibilities
// - field can be *
// - can be set of fields from the root table

fields = StringUtils.trim(fields);

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

if(StringUtils.equals("*", fields)) {

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

//include all fields of root table
List<BasicColumn> 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);
}

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

import com.homihq.db2rest.mybatis.MyBatisTable;
import com.homihq.db2rest.rest.read.v2.dto.ReadContextV2;
import com.homihq.db2rest.schema.SchemaManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
@Order(1)
public class RootTableProcessor implements ReadProcessor{

private final SchemaManager schemaManager;
@Override
public void process(ReadContextV2 readContextV2) {
MyBatisTable table =
schemaManager.getTable(readContextV2.getTableName());

readContextV2.setRootTable(table);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/homihq/db2rest/schema/SchemaManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private String getSchemaName(Table table) {

schemaName = table.getSchema().getName();
}
log.info("schemaName -> {}", schemaName);
log.debug("schemaName -> {}", schemaName);
return schemaName;
}

Expand Down
63 changes: 63 additions & 0 deletions src/main/java/com/homihq/db2rest/schema/TypeMapperUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.homihq.db2rest.schema;

import schemacrawler.schema.Column;

import java.sql.Types;
import java.sql.JDBCType;

public class TypeMapperUtil {

private TypeMapperUtil() {}

public static JDBCType getJdbcType(Column column) {
switch (column.getColumnDataType().getJavaSqlType().getVendorTypeNumber()) {
case Types.VARCHAR, 1111 -> {
return JDBCType.VARCHAR;
}
case Types.INTEGER, 2001 -> {
return JDBCType.INTEGER;
}
case Types.BIGINT -> {
return JDBCType.BIGINT;
}
case Types.CHAR -> {
return JDBCType.CHAR;
}
case Types.DATE -> {
return JDBCType.DATE;
}
case Types.TIMESTAMP -> {
return JDBCType.TIMESTAMP;
}
case Types.DECIMAL, Types.NUMERIC -> {
return JDBCType.DECIMAL;
}
case Types.SMALLINT, Types.BIT -> {
return JDBCType.SMALLINT;
}
case Types.BLOB, Types.BINARY -> {
return JDBCType.BLOB;
}
case Types.CLOB -> {
return JDBCType.CLOB;
}
case 2147483647 -> {
JDBCType jt = getJdbcTypeForUnknownJavaSqlType(column);
if (jt != null) {
return jt;
}
}
}
throw new IllegalArgumentException("Unsupported column type " + column.getColumnDataType().getJavaSqlType() + " (" + column.getColumnDataType().getJavaSqlType() + ")");
}

protected static JDBCType getJdbcTypeForUnknownJavaSqlType(Column column) {
/*
* unknown type
*/
if (column.getColumnDataType().getName().contains("TIMESTAMP")) {
return JDBCType.TIMESTAMP;
}
return null;
}
}

0 comments on commit 4703a05

Please sign in to comment.