Skip to content

Commit dcb71e3

Browse files
committed
Refactor the Insert Renderers for Consistency
1 parent 47bf61a commit dcb71e3

11 files changed

+216
-223
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.mybatis.dynamic.sql.util.PropertyMapping;
2424
import org.mybatis.dynamic.sql.util.StringConstantMapping;
2525

26-
public abstract class AbstractMultiRowValuePhraseVisitor extends MultiRowInsertMappingVisitor<FieldAndValue> {
26+
public abstract class AbstractMultiRowValuePhraseVisitor extends MultiRowInsertMappingVisitor<FieldAndValueAndParameters> {
2727

2828
protected final RenderingStrategy renderingStrategy;
2929
protected final String prefix;
@@ -34,29 +34,29 @@ protected AbstractMultiRowValuePhraseVisitor(RenderingStrategy renderingStrategy
3434
}
3535

3636
@Override
37-
public FieldAndValue visit(NullMapping mapping) {
38-
return FieldAndValue.withFieldName(mapping.columnName())
37+
public FieldAndValueAndParameters visit(NullMapping mapping) {
38+
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
3939
.withValuePhrase("null") //$NON-NLS-1$
4040
.build();
4141
}
4242

4343
@Override
44-
public FieldAndValue visit(ConstantMapping mapping) {
45-
return FieldAndValue.withFieldName(mapping.columnName())
44+
public FieldAndValueAndParameters visit(ConstantMapping mapping) {
45+
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
4646
.withValuePhrase(mapping.constant())
4747
.build();
4848
}
4949

5050
@Override
51-
public FieldAndValue visit(StringConstantMapping mapping) {
52-
return FieldAndValue.withFieldName(mapping.columnName())
51+
public FieldAndValueAndParameters visit(StringConstantMapping mapping) {
52+
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
5353
.withValuePhrase("'" + mapping.constant() + "'") //$NON-NLS-1$ //$NON-NLS-2$
5454
.build();
5555
}
5656

5757
@Override
58-
public FieldAndValue visit(PropertyMapping mapping) {
59-
return FieldAndValue.withFieldName(mapping.columnName())
58+
public FieldAndValueAndParameters visit(PropertyMapping mapping) {
59+
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
6060
.withValuePhrase(mapping.mapColumn(c -> calculateJdbcPlaceholder(c, mapping.property())))
6161
.build();
6262
}

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

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18-
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19-
20-
import java.util.List;
2118
import java.util.Objects;
22-
import java.util.stream.Collectors;
2319

2420
import org.mybatis.dynamic.sql.insert.BatchInsertModel;
2521
import org.mybatis.dynamic.sql.render.RenderingStrategy;
@@ -36,34 +32,16 @@ private BatchInsertRenderer(Builder<T> builder) {
3632

3733
public BatchInsert<T> render() {
3834
BatchValuePhraseVisitor visitor = new BatchValuePhraseVisitor(renderingStrategy, "row"); //$NON-NLS-1$)
39-
List<FieldAndValue> fieldsAndValues = model
40-
.mapColumnMappings(m -> m.accept(visitor))
41-
.collect(Collectors.toList());
35+
FieldAndValueCollector collector = model.mapColumnMappings(m -> m.accept(visitor))
36+
.collect(FieldAndValueCollector.collect());
37+
38+
String insertStatement = InsertRenderingUtilities.calculateInsertStatement(model.table(), collector);
4239

4340
return BatchInsert.withRecords(model.records())
44-
.withInsertStatement(calculateInsertStatement(fieldsAndValues))
41+
.withInsertStatement(insertStatement)
4542
.build();
4643
}
4744

48-
private String calculateInsertStatement(List<FieldAndValue> fieldsAndValues) {
49-
return "insert into" //$NON-NLS-1$
50-
+ spaceBefore(model.table().tableNameAtRuntime())
51-
+ spaceBefore(calculateColumnsPhrase(fieldsAndValues))
52-
+ spaceBefore(calculateValuesPhrase(fieldsAndValues));
53-
}
54-
55-
private String calculateColumnsPhrase(List<FieldAndValue> fieldsAndValues) {
56-
return fieldsAndValues.stream()
57-
.map(FieldAndValue::fieldName)
58-
.collect(Collectors.joining(", ", "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
59-
}
60-
61-
private String calculateValuesPhrase(List<FieldAndValue> fieldsAndValues) {
62-
return fieldsAndValues.stream()
63-
.map(FieldAndValue::valuePhrase)
64-
.collect(Collectors.joining(", ", "values (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
65-
}
66-
6745
public static <T> Builder<T> withBatchInsertModel(BatchInsertModel<T> model) {
6846
return new Builder<T>().withBatchInsertModel(model);
6947
}

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

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2016-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.insert.render;
17+
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.stream.Collector;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.IntStream;
25+
26+
public class FieldAndValueCollector {
27+
final List<FieldAndValueAndParameters> fieldsAndValues = new ArrayList<>();
28+
29+
public FieldAndValueCollector() {
30+
super();
31+
}
32+
33+
public void add(FieldAndValueAndParameters fieldAndValueAndParameters) {
34+
fieldsAndValues.add(fieldAndValueAndParameters);
35+
}
36+
37+
public FieldAndValueCollector merge(FieldAndValueCollector other) {
38+
fieldsAndValues.addAll(other.fieldsAndValues);
39+
return this;
40+
}
41+
42+
public boolean isEmpty() {
43+
return fieldsAndValues.isEmpty();
44+
}
45+
46+
public String columnsPhrase() {
47+
return fieldsAndValues.stream()
48+
.map(FieldAndValueAndParameters::fieldName)
49+
.collect(Collectors.joining(", ", "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
50+
}
51+
52+
public String valuesPhrase() {
53+
return fieldsAndValues.stream()
54+
.map(FieldAndValueAndParameters::valuePhrase)
55+
.collect(Collectors.joining(", ", "values (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
56+
}
57+
58+
public String multiRowInsertValuesPhrase(int rowCount) {
59+
return IntStream.range(0, rowCount)
60+
.mapToObj(this::toSingleRowOfValues)
61+
.collect(Collectors.joining(", ", "values ", "")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
62+
}
63+
64+
private String toSingleRowOfValues(int row) {
65+
return fieldsAndValues.stream()
66+
.map(FieldAndValueAndParameters::valuePhrase)
67+
.map(s -> String.format(s, row))
68+
.collect(Collectors.joining(", ", "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
69+
}
70+
71+
public Map<String, Object> parameters() {
72+
return fieldsAndValues.stream()
73+
.map(FieldAndValueAndParameters::parameters)
74+
.collect(HashMap::new, HashMap::putAll, HashMap::putAll);
75+
}
76+
77+
public static Collector<FieldAndValueAndParameters, FieldAndValueCollector, FieldAndValueCollector> collect() {
78+
return Collector.of(FieldAndValueCollector::new,
79+
FieldAndValueCollector::add,
80+
FieldAndValueCollector::merge);
81+
}
82+
}

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

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18-
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19-
20-
import java.util.HashMap;
21-
import java.util.List;
22-
import java.util.Map;
2318
import java.util.Objects;
2419
import java.util.Optional;
25-
import java.util.stream.Collectors;
2620

2721
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2822
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
@@ -41,47 +35,20 @@ private GeneralInsertRenderer(Builder builder) {
4135

4236
public GeneralInsertStatementProvider render() {
4337
GeneralInsertValuePhraseVisitor visitor = new GeneralInsertValuePhraseVisitor(renderingStrategy);
44-
List<Optional<FieldAndValueAndParameters>> fieldsAndValues = model.mapColumnMappings(m -> m.accept(visitor))
45-
.collect(Collectors.toList());
38+
FieldAndValueCollector collector = model.mapColumnMappings(m -> m.accept(visitor))
39+
.filter(Optional::isPresent)
40+
.map(Optional::get)
41+
.collect(FieldAndValueCollector.collect());
4642

47-
if (fieldsAndValues.stream().noneMatch(Optional::isPresent)) {
43+
if (collector.isEmpty()) {
4844
throw new InvalidSqlException(Messages.getString("ERROR.9")); //$NON-NLS-1$
4945
}
5046

51-
return DefaultGeneralInsertStatementProvider.withInsertStatement(calculateInsertStatement(fieldsAndValues))
52-
.withParameters(calculateParameters(fieldsAndValues))
53-
.build();
54-
}
55-
56-
private String calculateInsertStatement(List<Optional<FieldAndValueAndParameters>> fieldsAndValues) {
57-
return "insert into" //$NON-NLS-1$
58-
+ spaceBefore(model.table().tableNameAtRuntime())
59-
+ spaceBefore(calculateColumnsPhrase(fieldsAndValues))
60-
+ spaceBefore(calculateValuesPhrase(fieldsAndValues));
61-
}
62-
63-
private String calculateColumnsPhrase(List<Optional<FieldAndValueAndParameters>> fieldsAndValues) {
64-
return fieldsAndValues.stream()
65-
.filter(Optional::isPresent)
66-
.map(Optional::get)
67-
.map(FieldAndValueAndParameters::fieldName)
68-
.collect(Collectors.joining(", ", "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
69-
}
70-
71-
private String calculateValuesPhrase(List<Optional<FieldAndValueAndParameters>> fieldsAndValues) {
72-
return fieldsAndValues.stream()
73-
.filter(Optional::isPresent)
74-
.map(Optional::get)
75-
.map(FieldAndValueAndParameters::valuePhrase)
76-
.collect(Collectors.joining(", ", "values (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
77-
}
47+
String insertStatement = InsertRenderingUtilities.calculateInsertStatement(model.table(), collector);
7848

79-
private Map<String, Object> calculateParameters(List<Optional<FieldAndValueAndParameters>> fieldsAndValues) {
80-
return fieldsAndValues.stream()
81-
.filter(Optional::isPresent)
82-
.map(Optional::get)
83-
.map(FieldAndValueAndParameters::parameters)
84-
.collect(HashMap::new, HashMap::putAll, HashMap::putAll);
49+
return DefaultGeneralInsertStatementProvider.withInsertStatement(insertStatement)
50+
.withParameters(collector.parameters())
51+
.build();
8552
}
8653

8754
public static Builder withInsertModel(GeneralInsertModel model) {

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

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
*/
1616
package org.mybatis.dynamic.sql.insert.render;
1717

18-
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
19-
20-
import java.util.List;
2118
import java.util.Objects;
2219
import java.util.Optional;
23-
import java.util.stream.Collectors;
2420

2521
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2622
import org.mybatis.dynamic.sql.insert.InsertModel;
@@ -40,41 +36,22 @@ private InsertRenderer(Builder<T> builder) {
4036
public InsertStatementProvider<T> render() {
4137
ValuePhraseVisitor visitor = new ValuePhraseVisitor(renderingStrategy);
4238

43-
List<Optional<FieldAndValue>> fieldsAndValues = model.mapColumnMappings(m -> m.accept(visitor))
44-
.collect(Collectors.toList());
39+
FieldAndValueCollector collector = model.mapColumnMappings(m -> m.accept(visitor))
40+
.filter(Optional::isPresent)
41+
.map(Optional::get)
42+
.collect(FieldAndValueCollector.collect());
4543

46-
if (fieldsAndValues.stream().noneMatch(Optional::isPresent)) {
44+
if (collector.isEmpty()) {
4745
throw new InvalidSqlException(Messages.getString("ERROR.10")); //$NON-NLS-1$
4846
}
4947

48+
String insertStatement = InsertRenderingUtilities.calculateInsertStatement(model.table(), collector);
49+
5050
return DefaultInsertStatementProvider.withRow(model.row())
51-
.withInsertStatement(calculateInsertStatement(fieldsAndValues))
51+
.withInsertStatement(insertStatement)
5252
.build();
5353
}
5454

55-
private String calculateInsertStatement(List<Optional<FieldAndValue>> fieldsAndValues) {
56-
return "insert into" //$NON-NLS-1$
57-
+ spaceBefore(model.table().tableNameAtRuntime())
58-
+ spaceBefore(calculateColumnsPhrase(fieldsAndValues))
59-
+ spaceBefore(calculateValuesPhrase(fieldsAndValues));
60-
}
61-
62-
private String calculateColumnsPhrase(List<Optional<FieldAndValue>> fieldsAndValues) {
63-
return fieldsAndValues.stream()
64-
.filter(Optional::isPresent)
65-
.map(Optional::get)
66-
.map(FieldAndValue::fieldName)
67-
.collect(Collectors.joining(", ", "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
68-
}
69-
70-
private String calculateValuesPhrase(List<Optional<FieldAndValue>> fieldsAndValues) {
71-
return fieldsAndValues.stream()
72-
.filter(Optional::isPresent)
73-
.map(Optional::get)
74-
.map(FieldAndValue::valuePhrase)
75-
.collect(Collectors.joining(", ", "values (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
76-
}
77-
7855
public static <T> Builder<T> withInsertModel(InsertModel<T> model) {
7956
return new Builder<T>().withInsertModel(model);
8057
}

0 commit comments

Comments
 (0)