Skip to content

Commit 129b3f0

Browse files
authored
Merge pull request mybatis#81 from jeffgbutler/master
Refactor the renderers
2 parents 25580b2 + 2b3f8e2 commit 129b3f0

19 files changed

+216
-502
lines changed

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,23 +15,16 @@
1515
*/
1616
package org.mybatis.dynamic.sql.delete.render;
1717

18-
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19-
2018
import java.util.HashMap;
2119
import java.util.Map;
2220
import java.util.Objects;
23-
import java.util.Optional;
24-
25-
import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;
2621

2722
public class DefaultDeleteStatementProvider implements DeleteStatementProvider {
28-
private String tableName;
29-
private Optional<String> whereClause;
23+
private String deleteStatement;
3024
private Map<String, Object> parameters;
3125

3226
private DefaultDeleteStatementProvider(Builder builder) {
33-
tableName = Objects.requireNonNull(builder.tableName);
34-
whereClause = Optional.ofNullable(builder.whereClause);
27+
deleteStatement = Objects.requireNonNull(builder.deleteStatement);
3528
parameters = Objects.requireNonNull(builder.parameters);
3629
}
3730

@@ -42,30 +35,24 @@ public Map<String, Object> getParameters() {
4235

4336
@Override
4437
public String getDeleteStatement() {
45-
return "delete from" //$NON-NLS-1$
46-
+ spaceBefore(tableName)
47-
+ spaceBefore(whereClause);
38+
return deleteStatement;
4839
}
4940

50-
public static Builder withTableName(String tableName) {
51-
return new Builder().withTableName(tableName);
41+
public static Builder withDeleteStatement(String deleteStatement) {
42+
return new Builder().withDeleteStatement(deleteStatement);
5243
}
5344

5445
public static class Builder {
55-
private String tableName;
56-
private String whereClause;
46+
private String deleteStatement;
5747
private Map<String, Object> parameters = new HashMap<>();
5848

59-
public Builder withTableName(String tableName) {
60-
this.tableName = tableName;
49+
public Builder withDeleteStatement(String deleteStatement) {
50+
this.deleteStatement = deleteStatement;
6151
return this;
6252
}
6353

64-
public Builder withWhereClause(Optional<WhereClauseProvider> whereClauseProvider) {
65-
whereClauseProvider.ifPresent(wcp -> {
66-
whereClause = wcp.getWhereClause();
67-
parameters.putAll(wcp.getParameters());
68-
});
54+
public Builder withParameters(Map<String, Object> parameters) {
55+
this.parameters.putAll(parameters);
6956
return this;
7057
}
7158

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,10 @@
1515
*/
1616
package org.mybatis.dynamic.sql.delete.render;
1717

18+
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19+
20+
import java.util.Collections;
21+
import java.util.Map;
1822
import java.util.Objects;
1923
import java.util.Optional;
2024
import java.util.concurrent.atomic.AtomicInteger;
@@ -36,11 +40,13 @@ private DeleteRenderer(Builder builder) {
3640
}
3741

3842
public DeleteStatementProvider render() {
39-
return DefaultDeleteStatementProvider.withTableName(deleteModel.table().name())
40-
.withWhereClause(deleteModel.whereModel().flatMap(this::renderWhereClause))
43+
Optional<WhereClauseProvider> whereClause = deleteModel.whereModel().flatMap(this::renderWhereClause);
44+
45+
return DefaultDeleteStatementProvider.withDeleteStatement(calculateDeleteStatement(whereClause))
46+
.withParameters(calculateParameters(whereClause))
4147
.build();
4248
}
43-
49+
4450
private Optional<WhereClauseProvider> renderWhereClause(WhereModel whereModel) {
4551
return WhereRenderer.withWhereModel(whereModel)
4652
.withRenderingStrategy(renderingStrategy)
@@ -50,6 +56,18 @@ private Optional<WhereClauseProvider> renderWhereClause(WhereModel whereModel) {
5056
.render();
5157
}
5258

59+
private String calculateDeleteStatement(Optional<WhereClauseProvider> whereClause) {
60+
return "delete from" //$NON-NLS-1$
61+
+ spaceBefore(deleteModel.table().name())
62+
+ spaceBefore(whereClause.map(WhereClauseProvider::getWhereClause));
63+
}
64+
65+
private Map<String, Object> calculateParameters(Optional<WhereClauseProvider> whereClause) {
66+
return whereClause
67+
.map(WhereClauseProvider::getParameters)
68+
.orElse(Collections.emptyMap());
69+
}
70+
5371
public static Builder withDeleteModel(DeleteModel deleteModel) {
5472
return new Builder().withDeleteModel(deleteModel);
5573
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818
import java.util.Arrays;
1919
import java.util.List;
2020
import java.util.Objects;
21-
import java.util.Optional;
2221

2322
import org.mybatis.dynamic.sql.SqlColumn;
2423
import org.mybatis.dynamic.sql.SqlTable;
@@ -43,7 +42,7 @@ private InsertSelectDSL(SqlTable table, SelectModel selectModel) {
4342

4443
public InsertSelectModel build() {
4544
return InsertSelectModel.withTable(table)
46-
.withColumnList(Optional.ofNullable(columnList))
45+
.withColumnList(columnList)
4746
.withSelectModel(selectModel)
4847
.build();
4948
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,12 +26,12 @@
2626

2727
public class InsertSelectModel {
2828
private SqlTable table;
29-
private Optional<InsertColumnListModel> columnList;
29+
private InsertColumnListModel columnList;
3030
private SelectModel selectModel;
3131

3232
private InsertSelectModel(Builder builder) {
3333
table = Objects.requireNonNull(builder.table);
34-
columnList = Objects.requireNonNull(builder.columnList);
34+
columnList = builder.columnList;
3535
selectModel = Objects.requireNonNull(builder.selectModel);
3636
}
3737

@@ -44,7 +44,7 @@ public SelectModel selectModel() {
4444
}
4545

4646
public Optional<InsertColumnListModel> columnList() {
47-
return columnList;
47+
return Optional.ofNullable(columnList);
4848
}
4949

5050
public InsertSelectStatementProvider render(RenderingStrategy renderingStrategy) {
@@ -60,15 +60,15 @@ public static Builder withTable(SqlTable table) {
6060

6161
public static class Builder {
6262
private SqlTable table;
63-
private Optional<InsertColumnListModel> columnList = Optional.empty();
63+
private InsertColumnListModel columnList;
6464
private SelectModel selectModel;
6565

6666
public Builder withTable(SqlTable table) {
6767
this.table = table;
6868
return this;
6969
}
7070

71-
public Builder withColumnList(Optional<InsertColumnListModel> columnList) {
71+
public Builder withColumnList(InsertColumnListModel columnList) {
7272
this.columnList = columnList;
7373
return this;
7474
}

src/main/java/org/mybatis/dynamic/sql/insert/render/BatchInsert.java

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,24 +15,18 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18-
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19-
2018
import java.util.ArrayList;
2119
import java.util.Collections;
2220
import java.util.List;
2321
import java.util.Objects;
2422
import java.util.stream.Collectors;
2523

2624
public class BatchInsert<T> {
27-
private String tableName;
28-
private String columnsPhrase;
29-
private String valuesPhrase;
25+
private String insertStatement;
3026
private List<T> records;
3127

3228
private BatchInsert(Builder<T> builder) {
33-
tableName = Objects.requireNonNull(builder.tableName);
34-
columnsPhrase = Objects.requireNonNull(builder.columnsPhrase);
35-
valuesPhrase = Objects.requireNonNull(builder.valuesPhrase);
29+
insertStatement = Objects.requireNonNull(builder.insertStatement);
3630
records = Collections.unmodifiableList(Objects.requireNonNull(builder.records));
3731
}
3832

@@ -49,9 +43,7 @@ public List<InsertStatementProvider<T>> insertStatements() {
4943

5044
private InsertStatementProvider<T> toInsertStatement(T record) {
5145
return DefaultInsertStatementProvider.withRecord(record)
52-
.withTableName(tableName)
53-
.withColumnsPhrase(columnsPhrase)
54-
.withValuesPhrase(valuesPhrase)
46+
.withInsertStatement(insertStatement)
5547
.build();
5648
}
5749

@@ -61,37 +53,22 @@ private InsertStatementProvider<T> toInsertStatement(T record) {
6153
* @return the generated INSERT statement
6254
*/
6355
public String getInsertStatementSQL() {
64-
return "insert into" //$NON-NLS-1$
65-
+ spaceBefore(tableName)
66-
+ spaceBefore(columnsPhrase)
67-
+ spaceBefore(valuesPhrase);
56+
return insertStatement;
6857
}
6958

7059
public static <T> Builder<T> withRecords(List<T> records) {
7160
return new Builder<T>().withRecords(records);
7261
}
7362

7463
public static class Builder<T> {
75-
private String tableName;
76-
private String columnsPhrase;
77-
private String valuesPhrase;
64+
private String insertStatement;
7865
private List<T> records = new ArrayList<>();
7966

80-
public Builder<T> withTableName(String tableName) {
81-
this.tableName = tableName;
67+
public Builder<T> withInsertStatement(String insertStatement) {
68+
this.insertStatement = insertStatement;
8269
return this;
8370
}
8471

85-
public Builder<T> withColumnsPhrase(String columnsPhrase) {
86-
this.columnsPhrase = columnsPhrase;
87-
return this;
88-
}
89-
90-
public Builder<T> withValuesPhrase(String valuesPhrase) {
91-
this.valuesPhrase = valuesPhrase;
92-
return this;
93-
}
94-
9572
public Builder<T> withRecords(List<T> records) {
9673
this.records.addAll(records);
9774
return this;

src/main/java/org/mybatis/dynamic/sql/insert/render/BatchInsertRenderer.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18+
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19+
1820
import java.util.Objects;
1921
import java.util.function.Function;
2022

@@ -36,10 +38,9 @@ public BatchInsert<T> render() {
3638
ValuePhraseVisitor visitor = new ValuePhraseVisitor(renderingStrategy);
3739
FieldAndValueCollector<T> collector = model.mapColumnMappings(toFieldAndValue(visitor))
3840
.collect(FieldAndValueCollector.collect());
41+
3942
return BatchInsert.withRecords(model.records())
40-
.withTableName(model.table().name())
41-
.withColumnsPhrase(collector.columnsPhrase())
42-
.withValuesPhrase(collector.valuesPhrase())
43+
.withInsertStatement(calculateInsertStatement(collector))
4344
.build();
4445
}
4546

@@ -50,6 +51,13 @@ private Function<InsertMapping, FieldAndValue> toFieldAndValue(ValuePhraseVisito
5051
private FieldAndValue toFieldAndValue(ValuePhraseVisitor visitor, InsertMapping insertMapping) {
5152
return insertMapping.accept(visitor);
5253
}
54+
55+
private String calculateInsertStatement(FieldAndValueCollector<T> collector) {
56+
return "insert into" //$NON-NLS-1$
57+
+ spaceBefore(model.table().name())
58+
+ spaceBefore(collector.columnsPhrase())
59+
+ spaceBefore(collector.valuesPhrase());
60+
}
5361

5462
public static <T> Builder<T> withBatchInsertModel(BatchInsertModel<T> model) {
5563
return new Builder<T>().withBatchInsertModel(model);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy