-
-
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.
#270 - executing query with single tables
- Loading branch information
Showing
8 changed files
with
51 additions
and
144 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
47 changes: 6 additions & 41 deletions
47
src/main/java/com/homihq/db2rest/rest/read/ReadService.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 |
---|---|---|
@@ -1,72 +1,37 @@ | ||
package com.homihq.db2rest.rest.read; | ||
|
||
import com.homihq.db2rest.rest.read.helper.*; | ||
import com.homihq.db2rest.rest.read.dto.ReadContextV2; | ||
import com.homihq.db2rest.rest.read.processor.QueryCreatorTemplate; | ||
import com.homihq.db2rest.rest.read.processor.pre.ReadPreProcessor; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
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 ReadService { | ||
|
||
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<ReadPreProcessor> processorList; | ||
private final QueryCreatorTemplate queryCreatorTemplate; | ||
|
||
public Object findAll(String schemaName, String tableName, String select, String filter, | ||
Pageable pageable, Sort sort) { | ||
ReadContext ctx = ReadContext.builder() | ||
.pageable(pageable).sort(sort) | ||
.schemaName(schemaName) | ||
.tableName(tableName).select(select).filter(filter).build(); | ||
|
||
selectBuilder.build(ctx); | ||
//joinBuilder.build(ctx); | ||
whereBuilder.build(ctx); | ||
limitPaginationBuilder.build(ctx); | ||
sortBuilder.build(ctx); | ||
public Object findAll(ReadContextV2 readContextV2) { | ||
|
||
String sql = ctx.prepareSQL(); | ||
Map<String,Object> bindValues = ctx.prepareParameters(); | ||
for (ReadPreProcessor processor : processorList) { | ||
processor.process(readContextV2); | ||
} | ||
|
||
log.info("SQL - {}", sql); | ||
log.info("Bind variables - {}", bindValues); | ||
String sql = queryCreatorTemplate.createQuery(readContextV2); | ||
|
||
return namedParameterJdbcTemplate.queryForList(sql, bindValues); | ||
return namedParameterJdbcTemplate.queryForList(sql, readContextV2.getParamMap()); | ||
|
||
} | ||
|
||
public Object findAll(ReadContextV2 readContextV2) { | ||
try { | ||
for (ReadPreProcessor processor : processorList) { | ||
processor.process(readContextV2); | ||
} | ||
log.info("** TIME TO GENERATE QUERY **"); | ||
queryCreatorTemplate.createQuery(readContextV2); | ||
} | ||
catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
} |
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
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
53 changes: 15 additions & 38 deletions
53
src/main/java/com/homihq/db2rest/rest/read/processor/QueryCreatorTemplate.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 |
---|---|---|
@@ -1,71 +1,48 @@ | ||
package com.homihq.db2rest.rest.read.processor; | ||
|
||
import com.homihq.db2rest.rest.read.dto.ReadContextV2; | ||
import com.homihq.db2rest.rest.read.processor.post.ReadPostProcessor; | ||
import com.homihq.db2rest.template.SqlRenderer; | ||
import com.homihq.db2rest.rest.read.model.DbColumn; | ||
import lombok.RequiredArgsConstructor; | ||
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.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
import org.thymeleaf.context.Context; | ||
import org.thymeleaf.spring6.SpringTemplateEngine; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import static org.mybatis.dynamic.sql.select.SelectDSL.select; | ||
|
||
@Component | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class QueryCreatorTemplate { | ||
|
||
private final List<ReadPostProcessor> postProcessors; | ||
private final SqlRenderer sqlRenderer; | ||
|
||
public void createQuery(ReadContextV2 readContextV2) { | ||
private final SpringTemplateEngine templateEngine; | ||
public String createQuery(ReadContextV2 readContextV2) { | ||
|
||
log.info("**** Preparing to render ****"); | ||
|
||
Map<String,Object> data = new HashMap<>(); | ||
data.put("columns", readContextV2.getCols()); | ||
data.put("columns", createProjections(readContextV2.getCols())); | ||
data.put("rootTable", readContextV2.getRoot()); | ||
data.put("rootWhere", readContextV2.getRootWhere()); | ||
sqlRenderer.render("read", data); | ||
|
||
Context context = new Context(); | ||
context.setVariables(data); | ||
return templateEngine.process("read", context); | ||
|
||
/* | ||
QueryExpressionDSL<SelectModel> queryExpressionDSL = createProjection(readContextV2); | ||
if(Objects.nonNull(readContextV2.getWhereCondition())) { | ||
queryExpressionDSL.where(readContextV2.getWhereCondition()); | ||
} | ||
addSort(queryExpressionDSL, readContextV2.getSorts()); | ||
for(ReadPostProcessor processor : postProcessors) { | ||
processor.process(queryExpressionDSL, readContextV2); | ||
} | ||
*/ | ||
|
||
//SelectStatementProvider selectStatementProvider = queryExpressionDSL.build().render(RenderingStrategies.SPRING_NAMED_PARAMETER); | ||
} | ||
|
||
//log.info("SQL - {}", selectStatementProvider.getSelectStatement()); | ||
//log.info("Bind Variables - {}", selectStatementProvider.getParameters()); | ||
public String createProjections(List<DbColumn> columns) { | ||
List<String> columList = | ||
columns.stream().map(DbColumn::render).toList(); | ||
|
||
return StringUtils.join(columList, "\n\t,"); | ||
} | ||
|
||
private void addSort(QueryExpressionDSL<SelectModel> queryExpressionDSL, List<String> sorts) { | ||
} | ||
|
||
|
||
protected QueryExpressionDSL<SelectModel> createProjection(ReadContextV2 readContextV2) { | ||
return | ||
select(readContextV2.getColumns()) | ||
.from(readContextV2.getRootTable(), readContextV2.getRootTable().getAlias()); | ||
} | ||
} |
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
26 changes: 0 additions & 26 deletions
26
src/main/java/com/homihq/db2rest/template/SqlRenderer.java
This file was deleted.
Oops, something went wrong.
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