Skip to content

Commit 60cb080

Browse files
authored
Merge pull request #763 from jeffgbutler/context-refactoring
Refactoring Slice - Add Statement Configuration to Rendering Context
2 parents f450687 + 3b2d7b0 commit 60cb080

File tree

57 files changed

+641
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+641
-197
lines changed

src/main/java/org/mybatis/dynamic/sql/StringConstant.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.mybatis.dynamic.sql.render.RenderingContext;
2222
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
23+
import org.mybatis.dynamic.sql.util.StringUtilities;
2324

2425
public class StringConstant implements BindableColumn<String> {
2526

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

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

4949
@Override

src/main/java/org/mybatis/dynamic/sql/common/AbstractBooleanExpressionModel.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525

2626
public abstract class AbstractBooleanExpressionModel {
2727
private final SqlCriterion initialCriterion;
28-
private final List<AndOrCriteriaGroup> subCriteria = new ArrayList<>();
28+
private final List<AndOrCriteriaGroup> subCriteria ;
2929

30-
protected AbstractBooleanExpressionModel(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriteria) {
31-
this.initialCriterion = initialCriterion;
32-
this.subCriteria.addAll(subCriteria);
30+
protected AbstractBooleanExpressionModel(AbstractBuilder<?> builder) {
31+
initialCriterion = builder.initialCriterion;
32+
subCriteria = builder.subCriteria;
3333
}
3434

3535
public Optional<SqlCriterion> initialCriterion() {
@@ -39,4 +39,21 @@ public Optional<SqlCriterion> initialCriterion() {
3939
public List<AndOrCriteriaGroup> subCriteria() {
4040
return Collections.unmodifiableList(subCriteria);
4141
}
42+
43+
public abstract static class AbstractBuilder<T extends AbstractBuilder<T>> {
44+
private SqlCriterion initialCriterion;
45+
private final List<AndOrCriteriaGroup> subCriteria = new ArrayList<>();
46+
47+
public T withInitialCriterion(SqlCriterion initialCriterion) {
48+
this.initialCriterion = initialCriterion;
49+
return getThis();
50+
}
51+
52+
public T withSubCriteria(List<AndOrCriteriaGroup> subCriteria) {
53+
this.subCriteria.addAll(subCriteria);
54+
return getThis();
55+
}
56+
57+
protected abstract T getThis();
58+
}
4259
}

src/main/java/org/mybatis/dynamic/sql/common/AbstractBooleanExpressionRenderer.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@
2828
import org.mybatis.dynamic.sql.where.render.CriterionRenderer;
2929
import org.mybatis.dynamic.sql.where.render.RenderedCriterion;
3030

31-
public abstract class AbstractBooleanExpressionRenderer<M extends AbstractBooleanExpressionModel> {
32-
protected final M model;
31+
public abstract class AbstractBooleanExpressionRenderer {
32+
protected final AbstractBooleanExpressionModel model;
3333
private final String prefix;
3434
private final CriterionRenderer criterionRenderer;
35+
protected final RenderingContext renderingContext;
3536

36-
protected AbstractBooleanExpressionRenderer(String prefix, AbstractBuilder<M, ?> builder) {
37+
protected AbstractBooleanExpressionRenderer(String prefix, AbstractBuilder<?> builder) {
3738
model = Objects.requireNonNull(builder.model);
3839
this.prefix = Objects.requireNonNull(prefix);
39-
criterionRenderer = new CriterionRenderer(builder.renderingContext);
40+
renderingContext = Objects.requireNonNull(builder.renderingContext);
41+
criterionRenderer = new CriterionRenderer(renderingContext);
4042
}
4143

4244
public Optional<FragmentAndParameters> render() {
@@ -80,11 +82,11 @@ private String addPrefix(String fragment) {
8082
return spaceAfter(prefix) + fragment;
8183
}
8284

83-
public abstract static class AbstractBuilder<M, B extends AbstractBuilder<M, B>> {
84-
private final M model;
85+
public abstract static class AbstractBuilder<B extends AbstractBuilder<B>> {
86+
private final AbstractBooleanExpressionModel model;
8587
private RenderingContext renderingContext;
8688

87-
protected AbstractBuilder(M model) {
89+
protected AbstractBuilder(AbstractBooleanExpressionModel model) {
8890
this.model = model;
8991
}
9092

src/main/java/org/mybatis/dynamic/sql/common/CommonBuilder.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
package org.mybatis.dynamic.sql.common;
1717

1818
import org.mybatis.dynamic.sql.SqlTable;
19-
import org.mybatis.dynamic.sql.where.WhereModel;
19+
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
20+
import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;
2021

2122
/**
2223
* Builder class shared between the delete and update model builders.
@@ -26,9 +27,10 @@
2627
public abstract class CommonBuilder<T extends CommonBuilder<T>> {
2728
private SqlTable table;
2829
private String tableAlias;
29-
private WhereModel whereModel;
30+
private EmbeddedWhereModel whereModel;
3031
private Long limit;
3132
private OrderByModel orderByModel;
33+
private StatementConfiguration statementConfiguration;
3234

3335
public SqlTable table() {
3436
return table;
@@ -38,7 +40,7 @@ public String tableAlias() {
3840
return tableAlias;
3941
}
4042

41-
public WhereModel whereModel() {
43+
public EmbeddedWhereModel whereModel() {
4244
return whereModel;
4345
}
4446

@@ -50,6 +52,10 @@ public OrderByModel orderByModel() {
5052
return orderByModel;
5153
}
5254

55+
public StatementConfiguration statementConfiguration() {
56+
return statementConfiguration;
57+
}
58+
5359
public T withTable(SqlTable table) {
5460
this.table = table;
5561
return getThis();
@@ -60,7 +66,7 @@ public T withTableAlias(String tableAlias) {
6066
return getThis();
6167
}
6268

63-
public T withWhereModel(WhereModel whereModel) {
69+
public T withWhereModel(EmbeddedWhereModel whereModel) {
6470
this.whereModel = whereModel;
6571
return getThis();
6672
}
@@ -75,5 +81,10 @@ public T withOrderByModel(OrderByModel orderByModel) {
7581
return getThis();
7682
}
7783

84+
public T withStatementConfiguration(StatementConfiguration statementConfiguration) {
85+
this.statementConfiguration = statementConfiguration;
86+
return getThis();
87+
}
88+
7889
protected abstract T getThis();
7990
}

src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.mybatis.dynamic.sql.util.Utilities;
3131
import org.mybatis.dynamic.sql.where.AbstractWhereFinisher;
3232
import org.mybatis.dynamic.sql.where.AbstractWhereStarter;
33-
import org.mybatis.dynamic.sql.where.WhereModel;
33+
import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;
3434

3535
public class DeleteDSL<R> extends AbstractWhereStarter<DeleteDSL<R>.DeleteWhereBuilder, DeleteDSL<R>>
3636
implements Buildable<R> {
@@ -83,6 +83,7 @@ public R build() {
8383
.withLimit(limit)
8484
.withOrderByModel(orderByModel)
8585
.withWhereModel(whereBuilder == null ? null : whereBuilder.buildWhereModel())
86+
.withStatementConfiguration(statementConfiguration)
8687
.build();
8788

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

112113
private DeleteWhereBuilder() {
113-
super(statementConfiguration);
114+
super(DeleteDSL.this);
114115
}
115116

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

140-
protected WhereModel buildWhereModel() {
141+
protected EmbeddedWhereModel buildWhereModel() {
141142
return buildModel();
142143
}
143144
}

src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,27 @@
2222
import org.mybatis.dynamic.sql.SqlTable;
2323
import org.mybatis.dynamic.sql.common.CommonBuilder;
2424
import org.mybatis.dynamic.sql.common.OrderByModel;
25+
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
2526
import org.mybatis.dynamic.sql.delete.render.DeleteRenderer;
2627
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
2728
import org.mybatis.dynamic.sql.render.RenderingStrategy;
28-
import org.mybatis.dynamic.sql.where.WhereModel;
29+
import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;
2930

3031
public class DeleteModel {
3132
private final SqlTable table;
3233
private final String tableAlias;
33-
private final WhereModel whereModel;
34+
private final EmbeddedWhereModel whereModel;
3435
private final Long limit;
3536
private final OrderByModel orderByModel;
37+
private final StatementConfiguration statementConfiguration;
3638

3739
private DeleteModel(Builder builder) {
3840
table = Objects.requireNonNull(builder.table());
3941
whereModel = builder.whereModel();
4042
tableAlias = builder.tableAlias();
4143
limit = builder.limit();
4244
orderByModel = builder.orderByModel();
45+
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration());
4346
}
4447

4548
public SqlTable table() {
@@ -50,7 +53,7 @@ public Optional<String> tableAlias() {
5053
return Optional.ofNullable(tableAlias);
5154
}
5255

53-
public Optional<WhereModel> whereModel() {
56+
public Optional<EmbeddedWhereModel> whereModel() {
5457
return Optional.ofNullable(whereModel);
5558
}
5659

@@ -66,6 +69,7 @@ public Optional<OrderByModel> orderByModel() {
6669
public DeleteStatementProvider render(RenderingStrategy renderingStrategy) {
6770
return DeleteRenderer.withDeleteModel(this)
6871
.withRenderingStrategy(renderingStrategy)
72+
.withStatementConfiguration(statementConfiguration)
6973
.build()
7074
.render();
7175
}

src/main/java/org/mybatis/dynamic/sql/delete/render/DeleteRenderer.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.mybatis.dynamic.sql.common.OrderByModel;
2323
import org.mybatis.dynamic.sql.common.OrderByRenderer;
24+
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
2425
import org.mybatis.dynamic.sql.delete.DeleteModel;
2526
import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator;
2627
import org.mybatis.dynamic.sql.render.RenderedParameterInfo;
@@ -29,8 +30,7 @@
2930
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
3031
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
3132
import org.mybatis.dynamic.sql.util.FragmentCollector;
32-
import org.mybatis.dynamic.sql.where.WhereModel;
33-
import org.mybatis.dynamic.sql.where.render.WhereRenderer;
33+
import org.mybatis.dynamic.sql.where.EmbeddedWhereModel;
3434

3535
public class DeleteRenderer {
3636
private final DeleteModel deleteModel;
@@ -44,6 +44,7 @@ private DeleteRenderer(Builder builder) {
4444
renderingContext = RenderingContext
4545
.withRenderingStrategy(Objects.requireNonNull(builder.renderingStrategy))
4646
.withTableAliasCalculator(tableAliasCalculator)
47+
.withStatementConfiguration(builder.statementConfiguration)
4748
.build();
4849
}
4950

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

77-
private Optional<FragmentAndParameters> renderWhereClause(WhereModel whereModel) {
78-
return WhereRenderer.withWhereModel(whereModel)
79-
.withRenderingContext(renderingContext)
80-
.build()
81-
.render();
78+
private Optional<FragmentAndParameters> renderWhereClause(EmbeddedWhereModel whereModel) {
79+
return whereModel.render(renderingContext);
8280
}
8381

8482
private Optional<FragmentAndParameters> calculateLimitClause() {
@@ -108,6 +106,7 @@ public static Builder withDeleteModel(DeleteModel deleteModel) {
108106
public static class Builder {
109107
private DeleteModel deleteModel;
110108
private RenderingStrategy renderingStrategy;
109+
private StatementConfiguration statementConfiguration;
111110

112111
public Builder withDeleteModel(DeleteModel deleteModel) {
113112
this.deleteModel = deleteModel;
@@ -119,6 +118,11 @@ public Builder withRenderingStrategy(RenderingStrategy renderingStrategy) {
119118
return this;
120119
}
121120

121+
public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
122+
this.statementConfiguration = statementConfiguration;
123+
return this;
124+
}
125+
122126
public DeleteRenderer build() {
123127
return new DeleteRenderer(this);
124128
}

src/main/java/org/mybatis/dynamic/sql/insert/GeneralInsertDSL.java

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.jetbrains.annotations.NotNull;
2525
import org.mybatis.dynamic.sql.SqlColumn;
2626
import org.mybatis.dynamic.sql.SqlTable;
27+
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
2728
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
2829
import org.mybatis.dynamic.sql.util.Buildable;
2930
import org.mybatis.dynamic.sql.util.ConstantMapping;
@@ -52,6 +53,7 @@ public GeneralInsertModel build() {
5253
return new GeneralInsertModel.Builder()
5354
.withTable(table)
5455
.withInsertMappings(columnMappings)
56+
.withStatementConfiguration(new StatementConfiguration()) // nothing configurable in this statement yet
5557
.build();
5658
}
5759

src/main/java/org/mybatis/dynamic/sql/insert/GeneralInsertModel.java

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.jetbrains.annotations.NotNull;
2525
import org.mybatis.dynamic.sql.SqlTable;
26+
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
2627
import org.mybatis.dynamic.sql.insert.render.GeneralInsertRenderer;
2728
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
2829
import org.mybatis.dynamic.sql.render.RenderingStrategy;
@@ -33,11 +34,13 @@ public class GeneralInsertModel {
3334

3435
private final SqlTable table;
3536
private final List<AbstractColumnMapping> insertMappings;
37+
private final StatementConfiguration statementConfiguration;
3638

3739
private GeneralInsertModel(Builder builder) {
3840
table = Objects.requireNonNull(builder.table);
3941
Validator.assertNotEmpty(builder.insertMappings, "ERROR.6"); //$NON-NLS-1$
4042
insertMappings = builder.insertMappings;
43+
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
4144
}
4245

4346
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
@@ -52,13 +55,15 @@ public SqlTable table() {
5255
public GeneralInsertStatementProvider render(RenderingStrategy renderingStrategy) {
5356
return GeneralInsertRenderer.withInsertModel(this)
5457
.withRenderingStrategy(renderingStrategy)
58+
.withStatementConfiguration(statementConfiguration)
5559
.build()
5660
.render();
5761
}
5862

5963
public static class Builder {
6064
private SqlTable table;
6165
private final List<AbstractColumnMapping> insertMappings = new ArrayList<>();
66+
private StatementConfiguration statementConfiguration;
6267

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

78+
public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
79+
this.statementConfiguration = statementConfiguration;
80+
return this;
81+
}
82+
7383
public GeneralInsertModel build() {
7484
return new GeneralInsertModel(this);
7585
}

0 commit comments

Comments
 (0)