-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor to new version of query engine.
- Loading branch information
Showing
9 changed files
with
291 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/homihq/db2rest/rest/read/v2/ReadControllerV2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
src/main/java/com/homihq/db2rest/rest/read/v2/ReadServiceV2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/homihq/db2rest/rest/read/v2/dto/ReadContextV2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/homihq/db2rest/rest/read/v2/processor/ReadProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/homihq/db2rest/rest/read/v2/processor/RootFieldProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/homihq/db2rest/rest/read/v2/processor/RootTableProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/com/homihq/db2rest/schema/TypeMapperUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |