Skip to content

Commit

Permalink
Merge pull request #763 from jeffgbutler/context-refactoring
Browse files Browse the repository at this point in the history
Refactoring Slice - Add Statement Configuration to Rendering Context
  • Loading branch information
jeffgbutler authored Mar 26, 2024
2 parents f450687 + 3b2d7b0 commit 60cb080
Show file tree
Hide file tree
Showing 57 changed files with 641 additions and 197 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/mybatis/dynamic/sql/StringConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
import org.mybatis.dynamic.sql.util.StringUtilities;

public class StringConstant implements BindableColumn<String> {

Expand All @@ -42,8 +43,7 @@ public Optional<String> alias() {

@Override
public FragmentAndParameters render(RenderingContext renderingContext) {
String escaped = value.replace("'", "''"); //$NON-NLS-1$ //$NON-NLS-2$
return FragmentAndParameters.fromFragment("'" + escaped + "'"); //$NON-NLS-1$ //$NON-NLS-2$
return FragmentAndParameters.fromFragment(StringUtilities.formatConstantForSQL(value));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

public abstract class AbstractBooleanExpressionModel {
private final SqlCriterion initialCriterion;
private final List<AndOrCriteriaGroup> subCriteria = new ArrayList<>();
private final List<AndOrCriteriaGroup> subCriteria ;

protected AbstractBooleanExpressionModel(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriteria) {
this.initialCriterion = initialCriterion;
this.subCriteria.addAll(subCriteria);
protected AbstractBooleanExpressionModel(AbstractBuilder<?> builder) {
initialCriterion = builder.initialCriterion;
subCriteria = builder.subCriteria;
}

public Optional<SqlCriterion> initialCriterion() {
Expand All @@ -39,4 +39,21 @@ public Optional<SqlCriterion> initialCriterion() {
public List<AndOrCriteriaGroup> subCriteria() {
return Collections.unmodifiableList(subCriteria);
}

public abstract static class AbstractBuilder<T extends AbstractBuilder<T>> {
private SqlCriterion initialCriterion;
private final List<AndOrCriteriaGroup> subCriteria = new ArrayList<>();

public T withInitialCriterion(SqlCriterion initialCriterion) {
this.initialCriterion = initialCriterion;
return getThis();
}

public T withSubCriteria(List<AndOrCriteriaGroup> subCriteria) {
this.subCriteria.addAll(subCriteria);
return getThis();
}

protected abstract T getThis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@
import org.mybatis.dynamic.sql.where.render.CriterionRenderer;
import org.mybatis.dynamic.sql.where.render.RenderedCriterion;

public abstract class AbstractBooleanExpressionRenderer<M extends AbstractBooleanExpressionModel> {
protected final M model;
public abstract class AbstractBooleanExpressionRenderer {
protected final AbstractBooleanExpressionModel model;
private final String prefix;
private final CriterionRenderer criterionRenderer;
protected final RenderingContext renderingContext;

protected AbstractBooleanExpressionRenderer(String prefix, AbstractBuilder<M, ?> builder) {
protected AbstractBooleanExpressionRenderer(String prefix, AbstractBuilder<?> builder) {
model = Objects.requireNonNull(builder.model);
this.prefix = Objects.requireNonNull(prefix);
criterionRenderer = new CriterionRenderer(builder.renderingContext);
renderingContext = Objects.requireNonNull(builder.renderingContext);
criterionRenderer = new CriterionRenderer(renderingContext);
}

public Optional<FragmentAndParameters> render() {
Expand Down Expand Up @@ -80,11 +82,11 @@ private String addPrefix(String fragment) {
return spaceAfter(prefix) + fragment;
}

public abstract static class AbstractBuilder<M, B extends AbstractBuilder<M, B>> {
private final M model;
public abstract static class AbstractBuilder<B extends AbstractBuilder<B>> {
private final AbstractBooleanExpressionModel model;
private RenderingContext renderingContext;

protected AbstractBuilder(M model) {
protected AbstractBuilder(AbstractBooleanExpressionModel model) {
this.model = model;
}

Expand Down
19 changes: 15 additions & 4 deletions src/main/java/org/mybatis/dynamic/sql/common/CommonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package org.mybatis.dynamic.sql.common;

import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.where.WhereModel;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;

/**
* Builder class shared between the delete and update model builders.
Expand All @@ -26,9 +27,10 @@
public abstract class CommonBuilder<T extends CommonBuilder<T>> {
private SqlTable table;
private String tableAlias;
private WhereModel whereModel;
private EmbeddedWhereModel whereModel;
private Long limit;
private OrderByModel orderByModel;
private StatementConfiguration statementConfiguration;

public SqlTable table() {
return table;
Expand All @@ -38,7 +40,7 @@ public String tableAlias() {
return tableAlias;
}

public WhereModel whereModel() {
public EmbeddedWhereModel whereModel() {
return whereModel;
}

Expand All @@ -50,6 +52,10 @@ public OrderByModel orderByModel() {
return orderByModel;
}

public StatementConfiguration statementConfiguration() {
return statementConfiguration;
}

public T withTable(SqlTable table) {
this.table = table;
return getThis();
Expand All @@ -60,7 +66,7 @@ public T withTableAlias(String tableAlias) {
return getThis();
}

public T withWhereModel(WhereModel whereModel) {
public T withWhereModel(EmbeddedWhereModel whereModel) {
this.whereModel = whereModel;
return getThis();
}
Expand All @@ -75,5 +81,10 @@ public T withOrderByModel(OrderByModel orderByModel) {
return getThis();
}

public T withStatementConfiguration(StatementConfiguration statementConfiguration) {
this.statementConfiguration = statementConfiguration;
return getThis();
}

protected abstract T getThis();
}
7 changes: 4 additions & 3 deletions src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.mybatis.dynamic.sql.util.Utilities;
import org.mybatis.dynamic.sql.where.AbstractWhereFinisher;
import org.mybatis.dynamic.sql.where.AbstractWhereStarter;
import org.mybatis.dynamic.sql.where.WhereModel;
import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;

public class DeleteDSL<R> extends AbstractWhereStarter<DeleteDSL<R>.DeleteWhereBuilder, DeleteDSL<R>>
implements Buildable<R> {
Expand Down Expand Up @@ -83,6 +83,7 @@ public R build() {
.withLimit(limit)
.withOrderByModel(orderByModel)
.withWhereModel(whereBuilder == null ? null : whereBuilder.buildWhereModel())
.withStatementConfiguration(statementConfiguration)
.build();

return adapterFunction.apply(deleteModel);
Expand Down Expand Up @@ -110,7 +111,7 @@ public static DeleteDSL<DeleteModel> deleteFrom(SqlTable table, String tableAlia
public class DeleteWhereBuilder extends AbstractWhereFinisher<DeleteWhereBuilder> implements Buildable<R> {

private DeleteWhereBuilder() {
super(statementConfiguration);
super(DeleteDSL.this);
}

public DeleteDSL<R> limit(long limit) {
Expand All @@ -137,7 +138,7 @@ protected DeleteWhereBuilder getThis() {
return this;
}

protected WhereModel buildWhereModel() {
protected EmbeddedWhereModel buildWhereModel() {
return buildModel();
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,27 @@
import org.mybatis.dynamic.sql.SqlTable;
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.RenderingStrategy;
import org.mybatis.dynamic.sql.where.WhereModel;
import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;

public class DeleteModel {
private final SqlTable table;
private final String tableAlias;
private final WhereModel whereModel;
private final EmbeddedWhereModel whereModel;
private final Long limit;
private final OrderByModel orderByModel;
private final StatementConfiguration statementConfiguration;

private DeleteModel(Builder builder) {
table = Objects.requireNonNull(builder.table());
whereModel = builder.whereModel();
tableAlias = builder.tableAlias();
limit = builder.limit();
orderByModel = builder.orderByModel();
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration());
}

public SqlTable table() {
Expand All @@ -50,7 +53,7 @@ public Optional<String> tableAlias() {
return Optional.ofNullable(tableAlias);
}

public Optional<WhereModel> whereModel() {
public Optional<EmbeddedWhereModel> whereModel() {
return Optional.ofNullable(whereModel);
}

Expand All @@ -66,6 +69,7 @@ public Optional<OrderByModel> orderByModel() {
public DeleteStatementProvider render(RenderingStrategy renderingStrategy) {
return DeleteRenderer.withDeleteModel(this)
.withRenderingStrategy(renderingStrategy)
.withStatementConfiguration(statementConfiguration)
.build()
.render();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.mybatis.dynamic.sql.common.OrderByModel;
import org.mybatis.dynamic.sql.common.OrderByRenderer;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
import org.mybatis.dynamic.sql.delete.DeleteModel;
import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator;
import org.mybatis.dynamic.sql.render.RenderedParameterInfo;
Expand All @@ -29,8 +30,7 @@
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
import org.mybatis.dynamic.sql.util.FragmentCollector;
import org.mybatis.dynamic.sql.where.WhereModel;
import org.mybatis.dynamic.sql.where.render.WhereRenderer;
import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;

public class DeleteRenderer {
private final DeleteModel deleteModel;
Expand All @@ -44,6 +44,7 @@ private DeleteRenderer(Builder builder) {
renderingContext = RenderingContext
.withRenderingStrategy(Objects.requireNonNull(builder.renderingStrategy))
.withTableAliasCalculator(tableAliasCalculator)
.withStatementConfiguration(builder.statementConfiguration)
.build();
}

Expand Down Expand Up @@ -74,11 +75,8 @@ private Optional<FragmentAndParameters> calculateWhereClause() {
return deleteModel.whereModel().flatMap(this::renderWhereClause);
}

private Optional<FragmentAndParameters> renderWhereClause(WhereModel whereModel) {
return WhereRenderer.withWhereModel(whereModel)
.withRenderingContext(renderingContext)
.build()
.render();
private Optional<FragmentAndParameters> renderWhereClause(EmbeddedWhereModel whereModel) {
return whereModel.render(renderingContext);
}

private Optional<FragmentAndParameters> calculateLimitClause() {
Expand Down Expand Up @@ -108,6 +106,7 @@ public static Builder withDeleteModel(DeleteModel deleteModel) {
public static class Builder {
private DeleteModel deleteModel;
private RenderingStrategy renderingStrategy;
private StatementConfiguration statementConfiguration;

public Builder withDeleteModel(DeleteModel deleteModel) {
this.deleteModel = deleteModel;
Expand All @@ -119,6 +118,11 @@ public Builder withRenderingStrategy(RenderingStrategy renderingStrategy) {
return this;
}

public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
this.statementConfiguration = statementConfiguration;
return this;
}

public DeleteRenderer build() {
return new DeleteRenderer(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.ConstantMapping;
Expand Down Expand Up @@ -52,6 +53,7 @@ public GeneralInsertModel build() {
return new GeneralInsertModel.Builder()
.withTable(table)
.withInsertMappings(columnMappings)
.withStatementConfiguration(new StatementConfiguration()) // nothing configurable in this statement yet
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

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.RenderingStrategy;
Expand All @@ -33,11 +34,13 @@ public class GeneralInsertModel {

private final SqlTable table;
private final List<AbstractColumnMapping> insertMappings;
private final StatementConfiguration statementConfiguration;

private GeneralInsertModel(Builder builder) {
table = Objects.requireNonNull(builder.table);
Validator.assertNotEmpty(builder.insertMappings, "ERROR.6"); //$NON-NLS-1$
insertMappings = builder.insertMappings;
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
}

public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
Expand All @@ -52,13 +55,15 @@ public SqlTable table() {
public GeneralInsertStatementProvider render(RenderingStrategy renderingStrategy) {
return GeneralInsertRenderer.withInsertModel(this)
.withRenderingStrategy(renderingStrategy)
.withStatementConfiguration(statementConfiguration)
.build()
.render();
}

public static class Builder {
private SqlTable table;
private final List<AbstractColumnMapping> insertMappings = new ArrayList<>();
private StatementConfiguration statementConfiguration;

public Builder withTable(SqlTable table) {
this.table = table;
Expand All @@ -70,6 +75,11 @@ public Builder withInsertMappings(List<? extends AbstractColumnMapping> insertMa
return this;
}

public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
this.statementConfiguration = statementConfiguration;
return this;
}

public GeneralInsertModel build() {
return new GeneralInsertModel(this);
}
Expand Down
Loading

0 comments on commit 60cb080

Please sign in to comment.