diff --git a/src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java b/src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java index 54770d5d4..015f9823c 100644 --- a/src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java +++ b/src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java @@ -23,8 +23,8 @@ import org.mybatis.dynamic.sql.common.CommonBuilder; import org.mybatis.dynamic.sql.common.OrderByModel; import org.mybatis.dynamic.sql.configuration.StatementConfiguration; -import org.mybatis.dynamic.sql.delete.render.DeleteRenderer; import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider; +import org.mybatis.dynamic.sql.render.RendererFactory; import org.mybatis.dynamic.sql.render.RenderingStrategy; import org.mybatis.dynamic.sql.where.EmbeddedWhereModel; @@ -67,11 +67,8 @@ public Optional orderByModel() { @NotNull public DeleteStatementProvider render(RenderingStrategy renderingStrategy) { - return DeleteRenderer.withDeleteModel(this) - .withRenderingStrategy(renderingStrategy) - .withStatementConfiguration(statementConfiguration) - .build() - .render(); + return RendererFactory.createDeleteRenderer(this, statementConfiguration) + .render(renderingStrategy); } public static Builder withTable(SqlTable table) { diff --git a/src/main/java/org/mybatis/dynamic/sql/insert/BatchInsertModel.java b/src/main/java/org/mybatis/dynamic/sql/insert/BatchInsertModel.java index b591753a2..5e48e9035 100644 --- a/src/main/java/org/mybatis/dynamic/sql/insert/BatchInsertModel.java +++ b/src/main/java/org/mybatis/dynamic/sql/insert/BatchInsertModel.java @@ -19,7 +19,7 @@ import org.jetbrains.annotations.NotNull; import org.mybatis.dynamic.sql.insert.render.BatchInsert; -import org.mybatis.dynamic.sql.insert.render.BatchInsertRenderer; +import org.mybatis.dynamic.sql.render.RendererFactory; import org.mybatis.dynamic.sql.render.RenderingStrategy; import org.mybatis.dynamic.sql.util.Validator; @@ -33,10 +33,8 @@ private BatchInsertModel(Builder builder) { @NotNull public BatchInsert render(RenderingStrategy renderingStrategy) { - return BatchInsertRenderer.withBatchInsertModel(this) - .withRenderingStrategy(renderingStrategy) - .build() - .render(); + return RendererFactory.createBatchInsertRenderer(this) + .render(renderingStrategy); } public static Builder withRecords(Collection records) { diff --git a/src/main/java/org/mybatis/dynamic/sql/insert/GeneralInsertModel.java b/src/main/java/org/mybatis/dynamic/sql/insert/GeneralInsertModel.java index 7f219027d..f3fb146ee 100644 --- a/src/main/java/org/mybatis/dynamic/sql/insert/GeneralInsertModel.java +++ b/src/main/java/org/mybatis/dynamic/sql/insert/GeneralInsertModel.java @@ -23,8 +23,8 @@ import org.jetbrains.annotations.NotNull; import org.mybatis.dynamic.sql.SqlTable; import org.mybatis.dynamic.sql.configuration.StatementConfiguration; -import org.mybatis.dynamic.sql.insert.render.GeneralInsertRenderer; import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider; +import org.mybatis.dynamic.sql.render.RendererFactory; import org.mybatis.dynamic.sql.render.RenderingStrategy; import org.mybatis.dynamic.sql.util.AbstractColumnMapping; import org.mybatis.dynamic.sql.util.Validator; @@ -52,11 +52,8 @@ public SqlTable table() { @NotNull public GeneralInsertStatementProvider render(RenderingStrategy renderingStrategy) { - return GeneralInsertRenderer.withInsertModel(this) - .withRenderingStrategy(renderingStrategy) - .withStatementConfiguration(statementConfiguration) - .build() - .render(); + return RendererFactory.createGeneralInsertRenderer(this, statementConfiguration) + .render(renderingStrategy); } public static class Builder { diff --git a/src/main/java/org/mybatis/dynamic/sql/insert/InsertModel.java b/src/main/java/org/mybatis/dynamic/sql/insert/InsertModel.java index 7f740051e..07fac79ed 100644 --- a/src/main/java/org/mybatis/dynamic/sql/insert/InsertModel.java +++ b/src/main/java/org/mybatis/dynamic/sql/insert/InsertModel.java @@ -22,8 +22,8 @@ import org.jetbrains.annotations.NotNull; import org.mybatis.dynamic.sql.SqlTable; -import org.mybatis.dynamic.sql.insert.render.InsertRenderer; import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider; +import org.mybatis.dynamic.sql.render.RendererFactory; import org.mybatis.dynamic.sql.render.RenderingStrategy; import org.mybatis.dynamic.sql.util.AbstractColumnMapping; import org.mybatis.dynamic.sql.util.Validator; @@ -54,10 +54,8 @@ public SqlTable table() { @NotNull public InsertStatementProvider render(RenderingStrategy renderingStrategy) { - return InsertRenderer.withInsertModel(this) - .withRenderingStrategy(renderingStrategy) - .build() - .render(); + return RendererFactory.createInsertRenderer(this) + .render(renderingStrategy); } public static Builder withRow(T row) { diff --git a/src/main/java/org/mybatis/dynamic/sql/insert/InsertSelectModel.java b/src/main/java/org/mybatis/dynamic/sql/insert/InsertSelectModel.java index 4da2cef1c..5b22eaeb7 100644 --- a/src/main/java/org/mybatis/dynamic/sql/insert/InsertSelectModel.java +++ b/src/main/java/org/mybatis/dynamic/sql/insert/InsertSelectModel.java @@ -21,8 +21,8 @@ import org.jetbrains.annotations.NotNull; import org.mybatis.dynamic.sql.SqlTable; import org.mybatis.dynamic.sql.configuration.StatementConfiguration; -import org.mybatis.dynamic.sql.insert.render.InsertSelectRenderer; import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider; +import org.mybatis.dynamic.sql.render.RendererFactory; import org.mybatis.dynamic.sql.render.RenderingStrategy; import org.mybatis.dynamic.sql.select.SelectModel; @@ -53,11 +53,8 @@ public Optional columnList() { @NotNull public InsertSelectStatementProvider render(RenderingStrategy renderingStrategy) { - return InsertSelectRenderer.withInsertSelectModel(this) - .withRenderingStrategy(renderingStrategy) - .withStatementConfiguration(statementConfiguration) - .build() - .render(); + return RendererFactory.createInsertSelectRenderer(this, statementConfiguration) + .render(renderingStrategy); } public static Builder withTable(SqlTable table) { diff --git a/src/main/java/org/mybatis/dynamic/sql/insert/MultiRowInsertModel.java b/src/main/java/org/mybatis/dynamic/sql/insert/MultiRowInsertModel.java index 406c02b7a..d5d7f478d 100644 --- a/src/main/java/org/mybatis/dynamic/sql/insert/MultiRowInsertModel.java +++ b/src/main/java/org/mybatis/dynamic/sql/insert/MultiRowInsertModel.java @@ -18,8 +18,8 @@ import java.util.Collection; import org.jetbrains.annotations.NotNull; -import org.mybatis.dynamic.sql.insert.render.MultiRowInsertRenderer; import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider; +import org.mybatis.dynamic.sql.render.RendererFactory; import org.mybatis.dynamic.sql.render.RenderingStrategy; import org.mybatis.dynamic.sql.util.Validator; @@ -33,10 +33,8 @@ private MultiRowInsertModel(Builder builder) { @NotNull public MultiRowInsertStatementProvider render(RenderingStrategy renderingStrategy) { - return MultiRowInsertRenderer.withMultiRowInsertModel(this) - .withRenderingStrategy(renderingStrategy) - .build() - .render(); + return RendererFactory.createMultiRowInsertRenderer(this) + .render(renderingStrategy); } public static Builder withRecords(Collection records) { diff --git a/src/main/java/org/mybatis/dynamic/sql/render/Renderer.java b/src/main/java/org/mybatis/dynamic/sql/render/Renderer.java new file mode 100644 index 000000000..acb84a98f --- /dev/null +++ b/src/main/java/org/mybatis/dynamic/sql/render/Renderer.java @@ -0,0 +1,21 @@ +/* + * Copyright 2016-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.dynamic.sql.render; + +@FunctionalInterface +public interface Renderer { + R render(T t); +} diff --git a/src/main/java/org/mybatis/dynamic/sql/render/RendererFactory.java b/src/main/java/org/mybatis/dynamic/sql/render/RendererFactory.java new file mode 100644 index 000000000..c47ca1f78 --- /dev/null +++ b/src/main/java/org/mybatis/dynamic/sql/render/RendererFactory.java @@ -0,0 +1,124 @@ +/* + * Copyright 2016-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.dynamic.sql.render; + +import org.mybatis.dynamic.sql.configuration.StatementConfiguration; +import org.mybatis.dynamic.sql.delete.DeleteModel; +import org.mybatis.dynamic.sql.delete.render.DeleteRenderer; +import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider; +import org.mybatis.dynamic.sql.insert.BatchInsertModel; +import org.mybatis.dynamic.sql.insert.GeneralInsertModel; +import org.mybatis.dynamic.sql.insert.InsertModel; +import org.mybatis.dynamic.sql.insert.InsertSelectModel; +import org.mybatis.dynamic.sql.insert.MultiRowInsertModel; +import org.mybatis.dynamic.sql.insert.render.BatchInsert; +import org.mybatis.dynamic.sql.insert.render.BatchInsertRenderer; +import org.mybatis.dynamic.sql.insert.render.GeneralInsertRenderer; +import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider; +import org.mybatis.dynamic.sql.insert.render.InsertRenderer; +import org.mybatis.dynamic.sql.insert.render.InsertSelectRenderer; +import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider; +import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider; +import org.mybatis.dynamic.sql.insert.render.MultiRowInsertRenderer; +import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider; +import org.mybatis.dynamic.sql.select.MultiSelectModel; +import org.mybatis.dynamic.sql.select.SelectModel; +import org.mybatis.dynamic.sql.select.render.MultiSelectRenderer; +import org.mybatis.dynamic.sql.select.render.SelectRenderer; +import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; +import org.mybatis.dynamic.sql.update.UpdateModel; +import org.mybatis.dynamic.sql.update.render.UpdateRenderer; +import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; + +public interface RendererFactory { + static Renderer createDeleteRenderer(DeleteModel deleteModel, + StatementConfiguration statementConfiguration) { + return renderingStrategy -> DeleteRenderer.withDeleteModel(deleteModel) + .withStatementConfiguration(statementConfiguration) + .withRenderingStrategy(renderingStrategy) + .build() + .render(); + } + + static Renderer> createBatchInsertRenderer( + BatchInsertModel batchInsertModel) { + return renderingStrategy -> BatchInsertRenderer.withBatchInsertModel(batchInsertModel) + .withRenderingStrategy(renderingStrategy) + .build() + .render(); + } + + static Renderer createGeneralInsertRenderer( + GeneralInsertModel generalInsertModel, StatementConfiguration statementConfiguration) { + return renderingStrategy -> GeneralInsertRenderer.withInsertModel(generalInsertModel) + .withStatementConfiguration(statementConfiguration) + .withRenderingStrategy(renderingStrategy) + .build() + .render(); + } + + static Renderer> createInsertRenderer( + InsertModel insertModel) { + return renderingStrategy -> InsertRenderer.withInsertModel(insertModel) + .withRenderingStrategy(renderingStrategy) + .build() + .render(); + } + + static Renderer createInsertSelectRenderer( + InsertSelectModel insertSelectModel, StatementConfiguration statementConfiguration) { + return renderingStrategy -> InsertSelectRenderer.withInsertSelectModel(insertSelectModel) + .withStatementConfiguration(statementConfiguration) + .withRenderingStrategy(renderingStrategy) + .build() + .render(); + } + + static Renderer> createMultiRowInsertRenderer( + MultiRowInsertModel multiRowInsertModel) { + return renderingStrategy -> MultiRowInsertRenderer.withMultiRowInsertModel(multiRowInsertModel) + .withRenderingStrategy(renderingStrategy) + .build() + .render(); + } + + static Renderer createMultiSelectRenderer( + MultiSelectModel multiSelectModel, StatementConfiguration statementConfiguration) { + return renderingStrategy -> new MultiSelectRenderer.Builder() + .withMultiSelectModel(multiSelectModel) + .withStatementConfiguration(statementConfiguration) + .withRenderingStrategy(renderingStrategy) + .build() + .render(); + } + + static Renderer createSelectRenderer( + SelectModel selectModel) { + return renderingContext -> SelectRenderer.withSelectModel(selectModel) + .withRenderingContext(renderingContext) + .build() + .render(); + } + + static Renderer createUpdateRenderer( + UpdateModel updateModel, StatementConfiguration statementConfiguration) { + return renderingStrategy -> UpdateRenderer.withUpdateModel(updateModel) + .withStatementConfiguration(statementConfiguration) + .withRenderingStrategy(renderingStrategy) + .build() + .render(); + } +} diff --git a/src/main/java/org/mybatis/dynamic/sql/select/MultiSelectModel.java b/src/main/java/org/mybatis/dynamic/sql/select/MultiSelectModel.java index f9a1a388a..607378c20 100644 --- a/src/main/java/org/mybatis/dynamic/sql/select/MultiSelectModel.java +++ b/src/main/java/org/mybatis/dynamic/sql/select/MultiSelectModel.java @@ -21,8 +21,8 @@ import java.util.stream.Stream; import org.jetbrains.annotations.NotNull; +import org.mybatis.dynamic.sql.render.RendererFactory; import org.mybatis.dynamic.sql.render.RenderingStrategy; -import org.mybatis.dynamic.sql.select.render.MultiSelectRenderer; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.util.Validator; @@ -47,12 +47,8 @@ public Stream unionQueries() { @NotNull public SelectStatementProvider render(RenderingStrategy renderingStrategy) { - return new MultiSelectRenderer.Builder() - .withMultiSelectModel(this) - .withRenderingStrategy(renderingStrategy) - .withStatementConfiguration(statementConfiguration) - .build() - .render(); + return RendererFactory.createMultiSelectRenderer(this, statementConfiguration) + .render(renderingStrategy); } public static class Builder extends AbstractBuilder { diff --git a/src/main/java/org/mybatis/dynamic/sql/select/SelectModel.java b/src/main/java/org/mybatis/dynamic/sql/select/SelectModel.java index 57fd06a43..85196be7b 100644 --- a/src/main/java/org/mybatis/dynamic/sql/select/SelectModel.java +++ b/src/main/java/org/mybatis/dynamic/sql/select/SelectModel.java @@ -21,9 +21,9 @@ import java.util.stream.Stream; import org.jetbrains.annotations.NotNull; +import org.mybatis.dynamic.sql.render.RendererFactory; import org.mybatis.dynamic.sql.render.RenderingContext; import org.mybatis.dynamic.sql.render.RenderingStrategy; -import org.mybatis.dynamic.sql.select.render.SelectRenderer; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.util.Validator; @@ -56,10 +56,7 @@ public SelectStatementProvider render(RenderingStrategy renderingStrategy) { */ @NotNull public SelectStatementProvider render(RenderingContext renderingContext) { - return SelectRenderer.withSelectModel(this) - .withRenderingContext(renderingContext) - .build() - .render(); + return RendererFactory.createSelectRenderer(this).render(renderingContext); } public static Builder withQueryExpressions(List queryExpressions) { diff --git a/src/main/java/org/mybatis/dynamic/sql/update/UpdateModel.java b/src/main/java/org/mybatis/dynamic/sql/update/UpdateModel.java index 7ff82df26..672b08c44 100644 --- a/src/main/java/org/mybatis/dynamic/sql/update/UpdateModel.java +++ b/src/main/java/org/mybatis/dynamic/sql/update/UpdateModel.java @@ -26,8 +26,8 @@ import org.mybatis.dynamic.sql.common.CommonBuilder; import org.mybatis.dynamic.sql.common.OrderByModel; import org.mybatis.dynamic.sql.configuration.StatementConfiguration; +import org.mybatis.dynamic.sql.render.RendererFactory; import org.mybatis.dynamic.sql.render.RenderingStrategy; -import org.mybatis.dynamic.sql.update.render.UpdateRenderer; import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider; import org.mybatis.dynamic.sql.util.AbstractColumnMapping; import org.mybatis.dynamic.sql.util.Validator; @@ -79,11 +79,8 @@ public Optional orderByModel() { @NotNull public UpdateStatementProvider render(RenderingStrategy renderingStrategy) { - return UpdateRenderer.withUpdateModel(this) - .withRenderingStrategy(renderingStrategy) - .withStatementConfiguration(statementConfiguration) - .build() - .render(); + return RendererFactory.createUpdateRenderer(this, statementConfiguration) + .render(renderingStrategy); } public static Builder withTable(SqlTable table) {