Skip to content

Commit 012d1bc

Browse files
authored
Merge pull request #545 from jeffgbutler/general-refactoring
General refactoring
2 parents 17ffeb7 + 6f12d9a commit 012d1bc

28 files changed

+653
-694
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.common;
17+
18+
import org.mybatis.dynamic.sql.SqlTable;
19+
import org.mybatis.dynamic.sql.where.WhereModel;
20+
21+
/**
22+
* Builder class shared between the delete and update model builders.
23+
*
24+
* @param <T> type of the implementing builder
25+
*/
26+
public abstract class CommonBuilder<T extends CommonBuilder<T>> {
27+
private SqlTable table;
28+
private String tableAlias;
29+
private WhereModel whereModel;
30+
private Long limit;
31+
private OrderByModel orderByModel;
32+
33+
public SqlTable table() {
34+
return table;
35+
}
36+
37+
public String tableAlias() {
38+
return tableAlias;
39+
}
40+
41+
public WhereModel whereModel() {
42+
return whereModel;
43+
}
44+
45+
public Long limit() {
46+
return limit;
47+
}
48+
49+
public OrderByModel orderByModel() {
50+
return orderByModel;
51+
}
52+
53+
public T withTable(SqlTable table) {
54+
this.table = table;
55+
return getThis();
56+
}
57+
58+
public T withTableAlias(String tableAlias) {
59+
this.tableAlias = tableAlias;
60+
return getThis();
61+
}
62+
63+
public T withWhereModel(WhereModel whereModel) {
64+
this.whereModel = whereModel;
65+
return getThis();
66+
}
67+
68+
public T withLimit(Long limit) {
69+
this.limit = limit;
70+
return getThis();
71+
}
72+
73+
public T withOrderByModel(OrderByModel orderByModel) {
74+
this.orderByModel = orderByModel;
75+
return getThis();
76+
}
77+
78+
protected abstract T getThis();
79+
}

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

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.jetbrains.annotations.NotNull;
2222
import org.mybatis.dynamic.sql.SqlTable;
23+
import org.mybatis.dynamic.sql.common.CommonBuilder;
2324
import org.mybatis.dynamic.sql.common.OrderByModel;
2425
import org.mybatis.dynamic.sql.delete.render.DeleteRenderer;
2526
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
@@ -34,11 +35,11 @@ public class DeleteModel {
3435
private final OrderByModel orderByModel;
3536

3637
private DeleteModel(Builder builder) {
37-
table = Objects.requireNonNull(builder.table);
38-
whereModel = builder.whereModel;
39-
tableAlias = builder.tableAlias;
40-
limit = builder.limit;
41-
orderByModel = builder.orderByModel;
38+
table = Objects.requireNonNull(builder.table());
39+
whereModel = builder.whereModel();
40+
tableAlias = builder.tableAlias();
41+
limit = builder.limit();
42+
orderByModel = builder.orderByModel();
4243
}
4344

4445
public SqlTable table() {
@@ -73,35 +74,9 @@ public static Builder withTable(SqlTable table) {
7374
return new Builder().withTable(table);
7475
}
7576

76-
public static class Builder {
77-
private SqlTable table;
78-
private String tableAlias;
79-
private WhereModel whereModel;
80-
private Long limit;
81-
private OrderByModel orderByModel;
82-
83-
public Builder withTable(SqlTable table) {
84-
this.table = table;
85-
return this;
86-
}
87-
88-
public Builder withTableAlias(String tableAlias) {
89-
this.tableAlias = tableAlias;
90-
return this;
91-
}
92-
93-
public Builder withWhereModel(WhereModel whereModel) {
94-
this.whereModel = whereModel;
95-
return this;
96-
}
97-
98-
public Builder withLimit(Long limit) {
99-
this.limit = limit;
100-
return this;
101-
}
102-
103-
public Builder withOrderByModel(OrderByModel orderByModel) {
104-
this.orderByModel = orderByModel;
77+
public static class Builder extends CommonBuilder<Builder> {
78+
@Override
79+
protected Builder getThis() {
10580
return this;
10681
}
10782

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
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
27+
extends MultiRowInsertMappingVisitor<FieldAndValueAndParameters> {
2728

2829
protected final RenderingStrategy renderingStrategy;
2930
protected final String prefix;
@@ -34,29 +35,29 @@ protected AbstractMultiRowValuePhraseVisitor(RenderingStrategy renderingStrategy
3435
}
3536

3637
@Override
37-
public FieldAndValue visit(NullMapping mapping) {
38-
return FieldAndValue.withFieldName(mapping.columnName())
38+
public FieldAndValueAndParameters visit(NullMapping mapping) {
39+
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
3940
.withValuePhrase("null") //$NON-NLS-1$
4041
.build();
4142
}
4243

4344
@Override
44-
public FieldAndValue visit(ConstantMapping mapping) {
45-
return FieldAndValue.withFieldName(mapping.columnName())
45+
public FieldAndValueAndParameters visit(ConstantMapping mapping) {
46+
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
4647
.withValuePhrase(mapping.constant())
4748
.build();
4849
}
4950

5051
@Override
51-
public FieldAndValue visit(StringConstantMapping mapping) {
52-
return FieldAndValue.withFieldName(mapping.columnName())
52+
public FieldAndValueAndParameters visit(StringConstantMapping mapping) {
53+
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
5354
.withValuePhrase("'" + mapping.constant() + "'") //$NON-NLS-1$ //$NON-NLS-2$
5455
.build();
5556
}
5657

5758
@Override
58-
public FieldAndValue visit(PropertyMapping mapping) {
59-
return FieldAndValue.withFieldName(mapping.columnName())
59+
public FieldAndValueAndParameters visit(PropertyMapping mapping) {
60+
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
6061
.withValuePhrase(mapping.mapColumn(c -> calculateJdbcPlaceholder(c, mapping.property())))
6162
.build();
6263
}

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+
}

0 commit comments

Comments
 (0)