Skip to content

Commit

Permalink
Merge pull request #865 from jeffgbutler/renderer-factory
Browse files Browse the repository at this point in the history
Implement Renderer Factory
  • Loading branch information
jeffgbutler authored Oct 28, 2024
2 parents c96a12f + 3ddf8bd commit 1ea86ea
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 51 deletions.
9 changes: 3 additions & 6 deletions src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -67,11 +67,8 @@ public Optional<OrderByModel> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,10 +33,8 @@ private BatchInsertModel(Builder<T> builder) {

@NotNull
public BatchInsert<T> render(RenderingStrategy renderingStrategy) {
return BatchInsertRenderer.withBatchInsertModel(this)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
return RendererFactory.createBatchInsertRenderer(this)
.render(renderingStrategy);
}

public static <T> Builder<T> withRecords(Collection<T> records) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/mybatis/dynamic/sql/insert/InsertModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,10 +54,8 @@ public SqlTable table() {

@NotNull
public InsertStatementProvider<T> render(RenderingStrategy renderingStrategy) {
return InsertRenderer.withInsertModel(this)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
return RendererFactory.createInsertRenderer(this)
.render(renderingStrategy);
}

public static <T> Builder<T> withRow(T row) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -53,11 +53,8 @@ public Optional<InsertColumnListModel> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,10 +33,8 @@ private MultiRowInsertModel(Builder<T> builder) {

@NotNull
public MultiRowInsertStatementProvider<T> render(RenderingStrategy renderingStrategy) {
return MultiRowInsertRenderer.withMultiRowInsertModel(this)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
return RendererFactory.createMultiRowInsertRenderer(this)
.render(renderingStrategy);
}

public static <T> Builder<T> withRecords(Collection<T> records) {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/render/Renderer.java
Original file line number Diff line number Diff line change
@@ -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<T, R> {
R render(T t);
}
124 changes: 124 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/render/RendererFactory.java
Original file line number Diff line number Diff line change
@@ -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<RenderingStrategy, DeleteStatementProvider> createDeleteRenderer(DeleteModel deleteModel,
StatementConfiguration statementConfiguration) {
return renderingStrategy -> DeleteRenderer.withDeleteModel(deleteModel)
.withStatementConfiguration(statementConfiguration)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
}

static <T> Renderer<RenderingStrategy, BatchInsert<T>> createBatchInsertRenderer(
BatchInsertModel<T> batchInsertModel) {
return renderingStrategy -> BatchInsertRenderer.withBatchInsertModel(batchInsertModel)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
}

static Renderer<RenderingStrategy, GeneralInsertStatementProvider> createGeneralInsertRenderer(
GeneralInsertModel generalInsertModel, StatementConfiguration statementConfiguration) {
return renderingStrategy -> GeneralInsertRenderer.withInsertModel(generalInsertModel)
.withStatementConfiguration(statementConfiguration)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
}

static <T> Renderer<RenderingStrategy, InsertStatementProvider<T>> createInsertRenderer(
InsertModel<T> insertModel) {
return renderingStrategy -> InsertRenderer.withInsertModel(insertModel)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
}

static Renderer<RenderingStrategy, InsertSelectStatementProvider> createInsertSelectRenderer(
InsertSelectModel insertSelectModel, StatementConfiguration statementConfiguration) {
return renderingStrategy -> InsertSelectRenderer.withInsertSelectModel(insertSelectModel)
.withStatementConfiguration(statementConfiguration)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
}

static <T> Renderer<RenderingStrategy, MultiRowInsertStatementProvider<T>> createMultiRowInsertRenderer(
MultiRowInsertModel<T> multiRowInsertModel) {
return renderingStrategy -> MultiRowInsertRenderer.withMultiRowInsertModel(multiRowInsertModel)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
}

static Renderer<RenderingStrategy, SelectStatementProvider> createMultiSelectRenderer(
MultiSelectModel multiSelectModel, StatementConfiguration statementConfiguration) {
return renderingStrategy -> new MultiSelectRenderer.Builder()
.withMultiSelectModel(multiSelectModel)
.withStatementConfiguration(statementConfiguration)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
}

static Renderer<RenderingContext, SelectStatementProvider> createSelectRenderer(
SelectModel selectModel) {
return renderingContext -> SelectRenderer.withSelectModel(selectModel)
.withRenderingContext(renderingContext)
.build()
.render();
}

static Renderer<RenderingStrategy, UpdateStatementProvider> createUpdateRenderer(
UpdateModel updateModel, StatementConfiguration statementConfiguration) {
return renderingStrategy -> UpdateRenderer.withUpdateModel(updateModel)
.withStatementConfiguration(statementConfiguration)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -47,12 +47,8 @@ public Stream<UnionQuery> 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<Builder> {
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/mybatis/dynamic/sql/select/SelectModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<QueryExpressionModel> queryExpressions) {
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/org/mybatis/dynamic/sql/update/UpdateModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,11 +79,8 @@ public Optional<OrderByModel> 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) {
Expand Down

0 comments on commit 1ea86ea

Please sign in to comment.