diff --git a/api-rest/src/main/java/com/homihq/db2rest/jdbc/config/JdbcConfiguration.java b/api-rest/src/main/java/com/homihq/db2rest/jdbc/config/JdbcConfiguration.java index 5b8f7c5f..1b6cbb54 100644 --- a/api-rest/src/main/java/com/homihq/db2rest/jdbc/config/JdbcConfiguration.java +++ b/api-rest/src/main/java/com/homihq/db2rest/jdbc/config/JdbcConfiguration.java @@ -28,14 +28,16 @@ import com.homihq.db2rest.multidb.DatabaseProperties; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; +import gg.jte.CodeResolver; +import gg.jte.ContentType; +import gg.jte.TemplateEngine; +import gg.jte.resolve.ResourceCodeResolver; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.DependsOn; -import org.thymeleaf.spring6.SpringTemplateEngine; import javax.sql.DataSource; import java.util.HashMap; @@ -116,12 +118,16 @@ public JdbcOperationService operationService() { @Bean - @DependsOn("textTemplateResolver") - public SqlCreatorTemplate sqlCreatorTemplate(SpringTemplateEngine templateEngine, JdbcManager jdbcManager - ) { + public SqlCreatorTemplate sqlCreatorTemplate(TemplateEngine templateEngine, JdbcManager jdbcManager) { return new SqlCreatorTemplate(templateEngine, jdbcManager); } + @Bean + public TemplateEngine templateEngine() { + CodeResolver codeResolver = + new ResourceCodeResolver("sql-templates", this.getClass().getClassLoader()); + return TemplateEngine.create(codeResolver, ContentType.Plain); + } //START ::: Processors @Bean diff --git a/api-rest/src/main/resources/application.yml b/api-rest/src/main/resources/application.yml index 409961ee..3f83f2fe 100644 --- a/api-rest/src/main/resources/application.yml +++ b/api-rest/src/main/resources/application.yml @@ -29,10 +29,6 @@ spring: banner: location: classpath:banner.txt - thymeleaf: - check-template-location: false - cache: true - db2rest: diff --git a/api-rest/src/main/resources/sql-templates/count.jte b/api-rest/src/main/resources/sql-templates/count.jte new file mode 100644 index 00000000..20cabf04 --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/count.jte @@ -0,0 +1,10 @@ +@param String rootTable +@param String rootWhere + +SELECT + COUNT(*) +FROM + ${rootTable} +@if(rootWhere != null) + WHERE ${rootWhere} +@endif diff --git a/api-rest/src/main/resources/sql-templates/count.sql b/api-rest/src/main/resources/sql-templates/count.sql deleted file mode 100644 index bf348db7..00000000 --- a/api-rest/src/main/resources/sql-templates/count.sql +++ /dev/null @@ -1,6 +0,0 @@ -SELECT - count(*) -FROM - [(${rootTable})] -[# th:if="${rootWhere}"]WHERE - [(${rootWhere})][/] diff --git a/api-rest/src/main/resources/sql-templates/delete-mssql.jte b/api-rest/src/main/resources/sql-templates/delete-mssql.jte new file mode 100644 index 00000000..09ba25e1 --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/delete-mssql.jte @@ -0,0 +1,10 @@ +@param String rootTable +@param String rootWhere +@param String rootTableAlias + +DELETE FROM ${rootTableAlias} +FROM ${rootTable} + +@if(rootWhere != null) + WHERE ${rootWhere} +@endif diff --git a/api-rest/src/main/resources/sql-templates/delete-mssql.sql b/api-rest/src/main/resources/sql-templates/delete-mssql.sql deleted file mode 100644 index 513c6111..00000000 --- a/api-rest/src/main/resources/sql-templates/delete-mssql.sql +++ /dev/null @@ -1,5 +0,0 @@ -DELETE [(${rootTableAlias})] -FROM [(${rootTable})] -[# th:if="${rootWhere}"] - WHERE -[(${rootWhere})][/] diff --git a/api-rest/src/main/resources/sql-templates/delete.jte b/api-rest/src/main/resources/sql-templates/delete.jte new file mode 100644 index 00000000..634dd1c4 --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/delete.jte @@ -0,0 +1,8 @@ +@param String rootTable +@param String rootWhere + +DELETE FROM + ${rootTable} +@if(rootWhere != null) + WHERE ${rootWhere} +@endif diff --git a/api-rest/src/main/resources/sql-templates/delete.sql b/api-rest/src/main/resources/sql-templates/delete.sql deleted file mode 100644 index af2b159e..00000000 --- a/api-rest/src/main/resources/sql-templates/delete.sql +++ /dev/null @@ -1,4 +0,0 @@ -DELETE FROM [(${rootTable})] -[# th:if="${rootWhere}"]WHERE -[(${rootWhere})] -[/] diff --git a/api-rest/src/main/resources/sql-templates/exists-mssql.jte b/api-rest/src/main/resources/sql-templates/exists-mssql.jte new file mode 100644 index 00000000..d14f5100 --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/exists-mssql.jte @@ -0,0 +1,17 @@ +@import com.homihq.db2rest.jdbc.config.model.DbJoin +@import java.util.List + +@param String rootTable +@param String rootWhere +@param List joins + +SELECT TOP 1 1 +FROM ${rootTable} +@if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor +@endif +@if(rootWhere != null) + WHERE ${rootWhere} +@endif diff --git a/api-rest/src/main/resources/sql-templates/exists-mssql.sql b/api-rest/src/main/resources/sql-templates/exists-mssql.sql deleted file mode 100644 index 07d5a0a2..00000000 --- a/api-rest/src/main/resources/sql-templates/exists-mssql.sql +++ /dev/null @@ -1,9 +0,0 @@ -SELECT TOP 1 1 -FROM -[(${rootTable})] -[# th:if="${joins}"] - [# th:each="join : ${joins}"][(${join.render()})][/] -[/] -[# th:if="${rootWhere}"] - WHERE [(${rootWhere})] -[/] diff --git a/api-rest/src/main/resources/sql-templates/exists.jte b/api-rest/src/main/resources/sql-templates/exists.jte new file mode 100644 index 00000000..9c2818f2 --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/exists.jte @@ -0,0 +1,19 @@ +@import com.homihq.db2rest.jdbc.config.model.DbJoin +@import java.util.List + +@param String rootTable +@param String rootWhere +@param List joins + +SELECT + 1 +FROM ${rootTable} +@if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor +@endif +@if(rootWhere != null) + WHERE ${rootWhere} +@endif +LIMIT 1 diff --git a/api-rest/src/main/resources/sql-templates/exists.sql b/api-rest/src/main/resources/sql-templates/exists.sql deleted file mode 100644 index 741f5821..00000000 --- a/api-rest/src/main/resources/sql-templates/exists.sql +++ /dev/null @@ -1,8 +0,0 @@ -SELECT - 1 -FROM - [(${rootTable})] -[# th:if="${joins}"][# th:each="join : ${joins}"][(${join.render()})][/][/] -[# th:if="${rootWhere}"] WHERE - [(${rootWhere})][/] -LIMIT 1 \ No newline at end of file diff --git a/api-rest/src/main/resources/sql-templates/find-one.jte b/api-rest/src/main/resources/sql-templates/find-one.jte new file mode 100644 index 00000000..267621d8 --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/find-one.jte @@ -0,0 +1,11 @@ +@param String rootTable +@param String rootWhere +@param String columns + +SELECT + ${columns} +FROM + ${rootTable} +@if(rootWhere != null) + WHERE ${rootWhere} +@endif diff --git a/api-rest/src/main/resources/sql-templates/find-one.sql b/api-rest/src/main/resources/sql-templates/find-one.sql deleted file mode 100644 index 93c1ccad..00000000 --- a/api-rest/src/main/resources/sql-templates/find-one.sql +++ /dev/null @@ -1,6 +0,0 @@ -SELECT - [(${columns})] -FROM - [(${rootTable})] -[# th:if="${rootWhere}"]WHERE - [(${rootWhere})][/] diff --git a/api-rest/src/main/resources/sql-templates/insert.jte b/api-rest/src/main/resources/sql-templates/insert.jte new file mode 100644 index 00000000..5e68e947 --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/insert.jte @@ -0,0 +1,8 @@ +@param String columns +@param String table +@param String parameters + +INSERT INTO ${table} + (${columns}) +VALUES + (${parameters}) diff --git a/api-rest/src/main/resources/sql-templates/insert.sql b/api-rest/src/main/resources/sql-templates/insert.sql deleted file mode 100644 index 1c7d7966..00000000 --- a/api-rest/src/main/resources/sql-templates/insert.sql +++ /dev/null @@ -1,4 +0,0 @@ -INSERT INTO [(${table})] -([(${columns})]) -VALUES -([(${parameters})]) diff --git a/api-rest/src/main/resources/sql-templates/read-mssql.jte b/api-rest/src/main/resources/sql-templates/read-mssql.jte new file mode 100644 index 00000000..41a242de --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/read-mssql.jte @@ -0,0 +1,70 @@ +@import com.homihq.db2rest.jdbc.config.model.DbJoin +@import java.util.List + +@param List joins +@param String rootTable +@param String columns +@param String rootWhere +@param String sorts +@param Integer limit +@param Long offset + +@if(sorts != null) + SELECT + ${columns} + FROM ${rootTable} + @if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor + @endif + @if(rootWhere != null) + WHERE ${rootWhere} + @endif + ORDER BY ${sorts} + @if(limit != null) + OFFSET ${(offset == null ? 0 : offset)} ROWS FETCH NEXT ${limit} ROWS ONLY + @endif +@elseif(limit != null) + @if(offset != null) + SELECT T.* FROM + ( + SELECT + ${columns}, + ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS rowIndex + FROM ${rootTable} + @if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor + @endif + @if(rootWhere != null) + WHERE ${rootWhere} + @endif + ) AS T + WHERE rowIndex > ${offset} AND rowIndex <= ${offset + limit} + @else + SELECT TOP ${limit} ${columns} + FROM ${rootTable} + @if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor + @endif + @if(rootWhere != null) + WHERE ${rootWhere} + @endif + @endif +@else + SELECT + ${columns} + FROM ${rootTable} + @if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor + @endif + @if(rootWhere != null) + WHERE ${rootWhere} + @endif +@endif diff --git a/api-rest/src/main/resources/sql-templates/read-mssql.sql b/api-rest/src/main/resources/sql-templates/read-mssql.sql deleted file mode 100644 index f813391e..00000000 --- a/api-rest/src/main/resources/sql-templates/read-mssql.sql +++ /dev/null @@ -1,46 +0,0 @@ -[# th:if="${sorts}"] - SELECT - [(${columns})] - FROM - [(${rootTable})] - [# th:if="${joins}"] - [# th:each ="join:${joins}"][(${join.render()})][/] - [/] - [# th:if="${rootWhere}"] - WHERE [(${rootWhere})] - [/] - ORDER BY [(${sorts})] - [# th:if="${limit}"] - OFFSET [(${offset ?: 0})] ROWS FETCH NEXT [(${limit})] ROWS ONLY - [/] -[/] -[# th:unless="${sorts}"] - [# th:if="${limit}"] - [# th:if="${offset}"] - SELECT T.* FROM - ( - SELECT - [(${columns})], - ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS rowIndex - FROM [(${rootTable})] - [# th:if="${joins}"] - [# th:each="join : ${joins}"][(${join.render()})][/] - [/] - [# th:if="${rootWhere}"] - WHERE [(${rootWhere})] - [/] - ) AS T - WHERE rowIndex > [(${offset})] AND rowIndex <= [(${offset + limit})] - [/] - [# th:unless="${offset}"] - SELECT TOP [(${limit})] [(${columns})] - FROM [(${rootTable})] - [# th:if="${joins}"] - [# th:each="join : ${joins}"][(${join.render()})][/] - [/] - [# th:if="${rootWhere}"] - WHERE [(${rootWhere})] - [/] - [/] - [/] -[/] diff --git a/api-rest/src/main/resources/sql-templates/read-ora-12.jte b/api-rest/src/main/resources/sql-templates/read-ora-12.jte new file mode 100644 index 00000000..45edfbbf --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/read-ora-12.jte @@ -0,0 +1,34 @@ +@import com.homihq.db2rest.jdbc.config.model.DbJoin +@import java.util.List + +@param List joins +@param String rootTable +@param String columns +@param String rootWhere +@param String sorts +@param Integer limit +@param Long offset + +SELECT + ${columns} +FROM + ${rootTable} +@if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor +@endif +@if(rootWhere != null) + WHERE ${rootWhere} +@endif +@if(sorts != null) + ORDER BY ${sorts} +@endif +@if(limit != null) + @if(offset != null) + OFFSET ${offset} ROWS + FETCH NEXT ${limit} ROWS ONLY + @else + FETCH FIRST ${limit} ROWS ONLY + @endif +@endif diff --git a/api-rest/src/main/resources/sql-templates/read-ora-12.sql b/api-rest/src/main/resources/sql-templates/read-ora-12.sql deleted file mode 100644 index b0c5e480..00000000 --- a/api-rest/src/main/resources/sql-templates/read-ora-12.sql +++ /dev/null @@ -1,18 +0,0 @@ -SELECT - [(${columns})] -FROM - [(${rootTable})] -[# th:if="${joins}"][# th:each="join : ${joins}"][(${join.render()})][/][/] -[# th:if="${rootWhere}"]WHERE - [(${rootWhere})][/] -[# th:if="${sorts}"]ORDER BY [(${sorts})] [/] -[# th:if="${limit}"] - [# th:if="${offset}"] -OFFSET [(${offset})] ROWS -FETCH NEXT [(${limit})] ROWS ONLY - [/] - [# th:unless="${offset}"] -FETCH FIRST [(${limit})] ROWS ONLY - [/] -[/] - diff --git a/api-rest/src/main/resources/sql-templates/read-ora-9.jte b/api-rest/src/main/resources/sql-templates/read-ora-9.jte new file mode 100644 index 00000000..f3fe8ebe --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/read-ora-9.jte @@ -0,0 +1,72 @@ +@import com.homihq.db2rest.jdbc.config.model.DbJoin +@import java.util.List + +@param List joins +@param String rootTable +@param String columns +@param String rootWhere +@param String sorts +@param Integer limit +@param Long offset + +@if(limit != null) + @if(offset != null) + SELECT T.* FROM + ( + SELECT T.*, rownum as rowIndex FROM + ( + SELECT + ${columns} + FROM + ${rootTable} + @if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor + @endif + @if(rootWhere != null) + WHERE ${rootWhere} + @endif + @if(sorts != null) + ORDER BY ${sorts} + @endif + ) T + ) T + WHERE rowIndex > ${offset} AND rowIndex <= ${offset + limit} + @else + SELECT * FROM ( + SELECT + ${columns} + FROM + ${rootTable} + @if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor + @endif + @if(rootWhere != null) + WHERE ${rootWhere} + @endif + @if(sorts != null) + ORDER BY ${sorts} + @endif + ) + WHERE ROWNUM <= ${limit} + @endif +@else + SELECT + ${columns} + FROM + ${rootTable} + @if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor + @endif + @if(rootWhere != null) + WHERE ${rootWhere} + @endif + @if(sorts != null) + ORDER BY ${sorts} + @endif +@endif diff --git a/api-rest/src/main/resources/sql-templates/read-ora-9.sql b/api-rest/src/main/resources/sql-templates/read-ora-9.sql deleted file mode 100644 index 41ad004a..00000000 --- a/api-rest/src/main/resources/sql-templates/read-ora-9.sql +++ /dev/null @@ -1,46 +0,0 @@ -[# th:if="${limit}"] - - [# th:if="${offset}"] -SELECT T.* FROM ( - SELECT T.*, rownum as rowIndex FROM - ( - SELECT - [(${columns})] - FROM - [(${rootTable})] - [# th:if="${joins}"][# th:each="join : ${joins}"][(${join.render()})][/][/] - [# th:if="${rootWhere}"]WHERE - [(${rootWhere})][/] - [# th:if="${sorts}"]ORDER BY [(${sorts})] [/] - ) T) T -WHERE rowIndex > [(${offset})] AND rowIndex <= [(${offset + limit})] - [/] - - [# th:unless="${offset}"] - -SELECT * FROM ( - SELECT - [(${columns})] - FROM - [(${rootTable})] - [# th:if="${joins}"][# th:each="join : ${joins}"][(${join.render()})][/][/] - [# th:if="${rootWhere}"]WHERE - [(${rootWhere})][/] - [# th:if="${sorts}"]ORDER BY [(${sorts})] [/] -) -WHERE ROWNUM <= [(${limit})] - [/] - -[/] - -[# th:unless="${limit}"] -SELECT - [(${columns})] -FROM - [(${rootTable})] -[# th:if="${joins}"][# th:each="join : ${joins}"][(${join.render()})][/][/] -[# th:if="${rootWhere}"]WHERE - [(${rootWhere})][/] -[# th:if="${sorts}"]ORDER BY [(${sorts})] [/] - -[/] diff --git a/api-rest/src/main/resources/sql-templates/read.jte b/api-rest/src/main/resources/sql-templates/read.jte new file mode 100644 index 00000000..e27a8e6c --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/read.jte @@ -0,0 +1,32 @@ +@import com.homihq.db2rest.jdbc.config.model.DbJoin +@import java.util.List + +@param List joins +@param String rootTable +@param String columns +@param String rootWhere +@param String sorts +@param Integer limit +@param Long offset + +SELECT + ${columns} +FROM + ${rootTable} +@if(joins != null) + @for(DbJoin join : joins) + ${join.render()} + @endfor +@endif +@if(rootWhere != null) + WHERE ${rootWhere} +@endif +@if(sorts != null) + ORDER BY ${sorts} +@endif +@if(limit != null) + LIMIT ${limit} + @if(offset != null) + OFFSET ${offset} + @endif +@endif diff --git a/api-rest/src/main/resources/sql-templates/read.sql b/api-rest/src/main/resources/sql-templates/read.sql deleted file mode 100644 index 02dbb63c..00000000 --- a/api-rest/src/main/resources/sql-templates/read.sql +++ /dev/null @@ -1,9 +0,0 @@ -SELECT - [(${columns})] -FROM - [(${rootTable})] -[# th:if="${joins}"][# th:each="join : ${joins}"][(${join.render()})][/][/] -[# th:if="${rootWhere}"]WHERE - [(${rootWhere})][/] -[# th:if="${sorts}"]ORDER BY [(${sorts})] [/] -[# th:if="${limit}"]LIMIT [(${limit})] [# th:if="${offset}"]OFFSET [(${offset})][/] [/] diff --git a/api-rest/src/main/resources/sql-templates/update-mssql.jte b/api-rest/src/main/resources/sql-templates/update-mssql.jte new file mode 100644 index 00000000..fbe98781 --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/update-mssql.jte @@ -0,0 +1,11 @@ +@param String rootTable +@param String rootWhere +@param String columnSets +@param String rootTableAlias + +UPDATE ${rootTableAlias} +SET ${columnSets} +FROM ${rootTable} +@if(rootWhere != null) + WHERE ${rootWhere} +@endif diff --git a/api-rest/src/main/resources/sql-templates/update-mssql.sql b/api-rest/src/main/resources/sql-templates/update-mssql.sql deleted file mode 100644 index f23b0f35..00000000 --- a/api-rest/src/main/resources/sql-templates/update-mssql.sql +++ /dev/null @@ -1,6 +0,0 @@ -UPDATE [(${rootTableAlias})] -SET [(${columnSets})] -FROM [(${rootTable})] -[# th:if="${rootWhere}"] - WHERE [(${rootWhere})] -[/] diff --git a/api-rest/src/main/resources/sql-templates/update.jte b/api-rest/src/main/resources/sql-templates/update.jte new file mode 100644 index 00000000..e8b16046 --- /dev/null +++ b/api-rest/src/main/resources/sql-templates/update.jte @@ -0,0 +1,9 @@ +@param String rootTable +@param String rootWhere +@param String columnSets + +UPDATE ${rootTable} +SET ${columnSets} +@if(rootWhere != null) + WHERE ${rootWhere} +@endif diff --git a/api-rest/src/main/resources/sql-templates/update.sql b/api-rest/src/main/resources/sql-templates/update.sql deleted file mode 100644 index 480fb39d..00000000 --- a/api-rest/src/main/resources/sql-templates/update.sql +++ /dev/null @@ -1,5 +0,0 @@ -UPDATE [(${rootTable})] - SET [(${columnSets})] -[# th:if="${rootWhere}"] WHERE - [(${rootWhere})] -[/] diff --git a/rdbms-support/pom.xml b/rdbms-support/pom.xml index dfb56cae..b43fad3e 100644 --- a/rdbms-support/pom.xml +++ b/rdbms-support/pom.xml @@ -14,8 +14,19 @@ rdbms-support rdbms-support rdbms-support + + + 3.1.12 + + + + gg.jte + jte + ${jte.version} + + com.github db2rest-common @@ -33,11 +44,6 @@ spring-boot-starter-jdbc - - org.springframework.boot - spring-boot-starter-thymeleaf - - io.hypersistence tsid diff --git a/rdbms-support/src/main/java/com/homihq/db2rest/jdbc/config/ThymeleafConfiguration.java b/rdbms-support/src/main/java/com/homihq/db2rest/jdbc/config/ThymeleafConfiguration.java deleted file mode 100644 index f8826089..00000000 --- a/rdbms-support/src/main/java/com/homihq/db2rest/jdbc/config/ThymeleafConfiguration.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.homihq.db2rest.jdbc.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver; -import org.thymeleaf.templatemode.TemplateMode; - - -@Configuration -public class ThymeleafConfiguration { - - @Bean("textTemplateResolver") - public SpringResourceTemplateResolver textTemplateResolver() { - SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); - templateResolver.setPrefix("classpath:/sql-templates/"); - templateResolver.setSuffix(".sql"); - templateResolver.setTemplateMode(TemplateMode.TEXT); - templateResolver.setCharacterEncoding("UTF-8"); - templateResolver.setOrder(3); - templateResolver.setCheckExistence(true); - templateResolver.setCacheable(false); - - return templateResolver; - } -} diff --git a/rdbms-support/src/main/java/com/homihq/db2rest/jdbc/sql/SqlCreatorTemplate.java b/rdbms-support/src/main/java/com/homihq/db2rest/jdbc/sql/SqlCreatorTemplate.java index 6db191a7..ede8a7e0 100644 --- a/rdbms-support/src/main/java/com/homihq/db2rest/jdbc/sql/SqlCreatorTemplate.java +++ b/rdbms-support/src/main/java/com/homihq/db2rest/jdbc/sql/SqlCreatorTemplate.java @@ -9,11 +9,12 @@ import com.homihq.db2rest.jdbc.dto.DeleteContext; import com.homihq.db2rest.jdbc.dto.ReadContext; import com.homihq.db2rest.jdbc.dto.UpdateContext; +import gg.jte.TemplateEngine; +import gg.jte.TemplateOutput; +import gg.jte.output.StringOutput; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; -import org.thymeleaf.context.Context; -import org.thymeleaf.spring6.SpringTemplateEngine; import java.util.HashMap; import java.util.List; @@ -25,32 +26,27 @@ @RequiredArgsConstructor public class SqlCreatorTemplate { - - private final SpringTemplateEngine templateEngine; + private final TemplateEngine templateEngine; private final JdbcManager jdbcManager; - public String updateQuery(UpdateContext updateContext) { - Map data = new HashMap<>(); + Map params = new HashMap<>(); DbTable table = updateContext.getTable(); Dialect dialect = jdbcManager.getDialect(updateContext.getDbId()); if(dialect.supportAlias()) { - data.put("rootTable", table.render()); + params.put("rootTable", table.render()); } else{ - data.put("rootTable", table.name()); + params.put("rootTable", table.name()); } - data.put("rootWhere", updateContext.getWhere()); - data.put("columnSets", updateContext.renderSetColumns()); - data.put("rootTableAlias", table.alias()); - - Context context = new Context(); - context.setVariables(data); + params.put("rootWhere", updateContext.getWhere()); + params.put("columnSets", updateContext.renderSetColumns()); + params.put("rootTableAlias", table.alias()); - return templateEngine.process(dialect.getUpdateSqlTemplate(), context); + return renderSqlTemplate(dialect.getUpdateSqlTemplate(), params); } public String deleteQuery(DeleteContext deleteContext) { @@ -64,84 +60,73 @@ public String deleteQuery(DeleteContext deleteContext) { log.info("rendererTableName - {}", rendererTableName); - Map data = new HashMap<>(); - data.put("rootTable", rendererTableName); - data.put("rootWhere", deleteContext.getWhere()); - data.put("rootTableAlias", deleteContext.getTable().alias()); + Map params = new HashMap<>(); + params.put("rootTable", rendererTableName); + params.put("rootWhere", deleteContext.getWhere()); + params.put("rootTableAlias", deleteContext.getTable().alias()); - Context context = new Context(); - context.setVariables(data); - - return templateEngine.process(dialect.getDeleteSqlTemplate(), context); + return this.renderSqlTemplate(dialect.getDeleteSqlTemplate(), params); } public String create(CreateContext createContext) { - Map data = new HashMap<>(); + Map params = new HashMap<>(); - data.put("table", createContext.table().fullName()); - data.put("columns", createContext.renderColumns()); - data.put("parameters", createContext.renderParams()); + params.put("table", createContext.table().fullName()); + params.put("columns", createContext.renderColumns()); + params.put("parameters", createContext.renderParams()); - Context context = new Context(); - context.setVariables(data); Dialect dialect = jdbcManager.getDialect(createContext.dbId()); - return templateEngine.process(dialect.getInsertSqlTemplate(), context); + return this.renderSqlTemplate(dialect.getInsertSqlTemplate(), params); } public String findOne(ReadContext readContext) { - Map data = new HashMap<>(); - data.put("columns", projections(readContext.getCols())); - data.put("rootTable", readContext.getRoot().render()); - data.put("rootWhere", readContext.getRootWhere()); + Map params = new HashMap<>(); + params.put("columns", projections(readContext.getCols())); + params.put("rootTable", readContext.getRoot().render()); + params.put("rootWhere", readContext.getRootWhere()); - Context context = new Context(); - context.setVariables(data); Dialect dialect = jdbcManager.getDialect(readContext.getDbId()); - return templateEngine.process(dialect.getFindOneSqlTemplate(), context); + return this.renderSqlTemplate(dialect.getFindOneSqlTemplate(), params); } public String count(ReadContext readContext) { - Map data = new HashMap<>(); + Map params = new HashMap<>(); - data.put("rootTable", readContext.getRoot().render()); - data.put("rootWhere", readContext.getRootWhere()); + params.put("rootTable", readContext.getRoot().render()); + params.put("rootWhere", readContext.getRootWhere()); - Context context = new Context(); - context.setVariables(data); Dialect dialect = jdbcManager.getDialect(readContext.getDbId()); - return templateEngine.process(dialect.getCountSqlTemplate(), context); + return this.renderSqlTemplate(dialect.getCountSqlTemplate(), params); } public String exists(ReadContext readContext) { - Map data = new HashMap<>(); + Map params = new HashMap<>(); - data.put("rootTable", readContext.getRoot().render()); - data.put("rootWhere", readContext.getRootWhere()); - data.put("joins", readContext.getDbJoins()); + params.put("rootTable", readContext.getRoot().render()); + params.put("rootWhere", readContext.getRootWhere()); + params.put("joins", readContext.getDbJoins()); - Context context = new Context(); - context.setVariables(data); Dialect dialect = jdbcManager.getDialect(readContext.getDbId()); - return templateEngine.process(dialect.getExistSqlTemplate(), context); + return this.renderSqlTemplate(dialect.getExistSqlTemplate(), params); } public String query(ReadContext readContext) { log.debug("**** Preparing to render ****"); - Map data = new HashMap<>(); - data.put("columns", projections(readContext.getCols())); - data.put("rootTable", readContext.getRoot().render()); - data.put("rootWhere", readContext.getRootWhere()); - data.put("joins", readContext.getDbJoins()); + Map params = new HashMap<>(); + params.put("columns", projections(readContext.getCols())); + params.put("rootTable", readContext.getRoot().render()); + params.put("rootWhere", readContext.getRootWhere()); + params.put("joins", readContext.getDbJoins()); if(Objects.nonNull(readContext.getDbSortList()) && !readContext.getDbSortList().isEmpty()) { - data.put("sorts", orderBy(readContext.getDbSortList())); + params.put("sorts", orderBy(readContext.getDbSortList())); } @@ -149,27 +134,34 @@ public String query(ReadContext readContext) { log.debug("offset - {}", readContext.getOffset()); - if(readContext.getLimit() > -1) data.put("limit", readContext.getLimit()); - if(readContext.getLimit() == -1) data.put("limit", readContext.getDefaultFetchLimit()); + if(readContext.getLimit() > -1) params.put("limit", readContext.getLimit()); + if(readContext.getLimit() == -1) params.put("limit", readContext.getDefaultFetchLimit()); + + if(readContext.getOffset() > -1) params.put("offset", readContext.getOffset()); - if(readContext.getOffset() > -1) data.put("offset", readContext.getOffset()); + log.debug("data - {}", params); - log.debug("data - {}", data); - Context context = new Context(); - context.setVariables(data); Dialect dialect = jdbcManager.getDialect(readContext.getDbId()); - return templateEngine.process(dialect.getReadSqlTemplate(), context); + return this.renderSqlTemplate(dialect.getReadSqlTemplate(), params); + } + + private String renderSqlTemplate(String template, Map params) { + TemplateOutput output = new StringOutput(); + + this.templateEngine.render(template + ".jte", params, output); + + return output.toString(); } - public String projections(List columns) { + private String projections(List columns) { List columList = columns.stream().map(DbColumn::renderWithAlias).toList(); return StringUtils.join(columList, "\n\t,"); } - public String orderBy(List sorts) { + private String orderBy(List sorts) { List sortList = sorts.stream().map(DbSort::render).toList();