Skip to content

Commit ba7dc5d

Browse files
authored
Merge pull request #218 from jeffgbutler/delay-when-present-calculations
Delay "when present" Calculations on Inserts and Updates
2 parents e04c0a9 + b7ea53e commit ba7dc5d

31 files changed

+565
-407
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.mybatis.dynamic.sql.util.NullMapping;
2828
import org.mybatis.dynamic.sql.util.StringConstantMapping;
2929
import org.mybatis.dynamic.sql.util.ValueMapping;
30+
import org.mybatis.dynamic.sql.util.ValueWhenPresentMapping;
3031

3132
public class GeneralInsertDSL {
3233
private List<AbstractColumnMapping> insertMappings = new ArrayList<>();
@@ -88,9 +89,7 @@ public GeneralInsertDSL toValueWhenPresent(T value) {
8889
}
8990

9091
public GeneralInsertDSL toValueWhenPresent(Supplier<T> valueSupplier) {
91-
if (valueSupplier.get() != null) {
92-
insertMappings.add(ValueMapping.of(column, valueSupplier));
93-
}
92+
insertMappings.add(ValueWhenPresentMapping.of(column, valueSupplier));
9493
return GeneralInsertDSL.this;
9594
}
9695
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -25,6 +25,7 @@
2525
import org.mybatis.dynamic.sql.util.ConstantMapping;
2626
import org.mybatis.dynamic.sql.util.NullMapping;
2727
import org.mybatis.dynamic.sql.util.PropertyMapping;
28+
import org.mybatis.dynamic.sql.util.PropertyWhenPresentMapping;
2829
import org.mybatis.dynamic.sql.util.StringConstantMapping;
2930

3031
public class InsertDSL<T> {
@@ -78,9 +79,7 @@ public InsertDSL<T> toProperty(String property) {
7879
}
7980

8081
public InsertDSL<T> toPropertyWhenPresent(String property, Supplier<?> valueSupplier) {
81-
if (valueSupplier.get() != null) {
82-
toProperty(property);
83-
}
82+
columnMappings.add(PropertyWhenPresentMapping.of(column, property, valueSupplier));
8483
return InsertDSL.this;
8584
}
8685

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -17,7 +17,9 @@
1717

1818
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
1919

20+
import java.util.List;
2021
import java.util.Objects;
22+
import java.util.stream.Collectors;
2123

2224
import org.mybatis.dynamic.sql.insert.BatchInsertModel;
2325
import org.mybatis.dynamic.sql.render.RenderingStrategy;
@@ -33,22 +35,28 @@ private BatchInsertRenderer(Builder<T> builder) {
3335
}
3436

3537
public BatchInsert<T> render() {
36-
ValuePhraseVisitor visitor = new ValuePhraseVisitor(renderingStrategy);
37-
FieldAndValueCollector collector = model.mapColumnMappings(MultiRowRenderingUtilities.toFieldAndValue(visitor))
38-
.collect(FieldAndValueCollector.collect());
38+
MultiRowValuePhraseVisitor visitor = new MultiRowValuePhraseVisitor(renderingStrategy, "record"); //$NON-NLS-1$)
39+
List<FieldAndValue> fieldsAndValues = model.mapColumnMappings(MultiRowRenderingUtilities.toFieldAndValue(visitor))
40+
.collect(Collectors.toList());
3941

4042
return BatchInsert.withRecords(model.records())
41-
.withInsertStatement(calculateInsertStatement(collector))
43+
.withInsertStatement(calculateInsertStatement(fieldsAndValues))
4244
.build();
4345
}
4446

45-
private String calculateInsertStatement(FieldAndValueCollector collector) {
47+
private String calculateInsertStatement(List<FieldAndValue> fieldsAndValues) {
4648
return "insert into" //$NON-NLS-1$
4749
+ spaceBefore(model.table().tableNameAtRuntime())
48-
+ spaceBefore(collector.columnsPhrase())
49-
+ spaceBefore(collector.valuesPhrase());
50+
+ spaceBefore(MultiRowRenderingUtilities.calculateColumnsPhrase(fieldsAndValues))
51+
+ spaceBefore(calculateVluesPhrase(fieldsAndValues));
5052
}
5153

54+
private String calculateVluesPhrase(List<FieldAndValue> fieldsAndValues) {
55+
return fieldsAndValues.stream()
56+
.map(FieldAndValue::valuePhrase)
57+
.collect(Collectors.joining(", ", "values (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
58+
}
59+
5260
public static <T> Builder<T> withBatchInsertModel(BatchInsertModel<T> model) {
5361
return new Builder<T>().withBatchInsertModel(model);
5462
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -16,6 +16,7 @@
1616
package org.mybatis.dynamic.sql.insert.render;
1717

1818
import java.util.Objects;
19+
import java.util.Optional;
1920

2021
public class FieldAndValue {
2122
private String fieldName;
@@ -59,5 +60,9 @@ public Builder withValuePhrase(String valuePhrase) {
5960
public FieldAndValue build() {
6061
return new FieldAndValue(this);
6162
}
63+
64+
public Optional<FieldAndValue> buildOptional() {
65+
return Optional.of(build());
66+
}
6267
}
6368
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.HashMap;
1919
import java.util.Map;
2020
import java.util.Objects;
21+
import java.util.Optional;
2122

2223
public class FieldAndValueAndParameters {
2324
private String fieldName;
@@ -69,5 +70,9 @@ public Builder withParameter(String key, Object value) {
6970
public FieldAndValueAndParameters build() {
7071
return new FieldAndValueAndParameters(this);
7172
}
73+
74+
public Optional<FieldAndValueAndParameters> buildOptional() {
75+
return Optional.of(build());
76+
}
7277
}
7378
}

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

Lines changed: 0 additions & 66 deletions
This file was deleted.

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

Lines changed: 0 additions & 70 deletions
This file was deleted.

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

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717

1818
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
1919

20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
2023
import java.util.Objects;
24+
import java.util.Optional;
2125
import java.util.function.Function;
26+
import java.util.stream.Collectors;
2227

2328
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
2429
import org.mybatis.dynamic.sql.render.RenderingStrategy;
@@ -36,31 +41,55 @@ private GeneralInsertRenderer(Builder builder) {
3641

3742
public GeneralInsertStatementProvider render() {
3843
GeneralInsertValuePhraseVisitor visitor = new GeneralInsertValuePhraseVisitor(renderingStrategy);
39-
FieldAndValueAndParametersCollector collector = model.mapColumnMappings(toFieldAndValue(visitor))
40-
.collect(FieldAndValueAndParametersCollector.collect());
44+
List<Optional<FieldAndValueAndParameters>> fieldsAndValues = model.mapColumnMappings(toFieldAndValue(visitor))
45+
.collect(Collectors.toList());
4146

42-
return DefaultGeneralInsertStatementProvider.withInsertStatement(calculateInsertStatement(collector))
43-
.withParameters(collector.parameters())
47+
return DefaultGeneralInsertStatementProvider.withInsertStatement(calculateInsertStatement(fieldsAndValues))
48+
.withParameters(calculateParameters(fieldsAndValues))
4449
.build();
4550
}
4651

47-
private String calculateInsertStatement(FieldAndValueAndParametersCollector collector) {
52+
private String calculateInsertStatement(List<Optional<FieldAndValueAndParameters>> fieldsAndValues) {
4853
return "insert into" //$NON-NLS-1$
4954
+ spaceBefore(model.table().tableNameAtRuntime())
50-
+ spaceBefore(collector.columnsPhrase())
51-
+ spaceBefore(collector.valuesPhrase());
55+
+ spaceBefore(calculateColumnsPhrase(fieldsAndValues))
56+
+ spaceBefore(calculateValuesPhrase(fieldsAndValues));
5257
}
5358

54-
private Function<AbstractColumnMapping, FieldAndValueAndParameters> toFieldAndValue(
59+
private Function<AbstractColumnMapping, Optional<FieldAndValueAndParameters>> toFieldAndValue(
5560
GeneralInsertValuePhraseVisitor visitor) {
5661
return insertMapping -> toFieldAndValue(visitor, insertMapping);
5762
}
5863

59-
private FieldAndValueAndParameters toFieldAndValue(GeneralInsertValuePhraseVisitor visitor,
64+
private Optional<FieldAndValueAndParameters> toFieldAndValue(GeneralInsertValuePhraseVisitor visitor,
6065
AbstractColumnMapping insertMapping) {
6166
return insertMapping.accept(visitor);
6267
}
6368

69+
private String calculateColumnsPhrase(List<Optional<FieldAndValueAndParameters>> fieldsAndValues) {
70+
return fieldsAndValues.stream()
71+
.filter(Optional::isPresent)
72+
.map(Optional::get)
73+
.map(FieldAndValueAndParameters::fieldName)
74+
.collect(Collectors.joining(", ", "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
75+
}
76+
77+
private String calculateValuesPhrase(List<Optional<FieldAndValueAndParameters>> fieldsAndValues) {
78+
return fieldsAndValues.stream()
79+
.filter(Optional::isPresent)
80+
.map(Optional::get)
81+
.map(FieldAndValueAndParameters::valuePhrase)
82+
.collect(Collectors.joining(", ", "values (", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
83+
}
84+
85+
private Map<String, Object> calculateParameters(List<Optional<FieldAndValueAndParameters>> fieldsAndValues) {
86+
return fieldsAndValues.stream()
87+
.filter(Optional::isPresent)
88+
.map(Optional::get)
89+
.map(FieldAndValueAndParameters::parameters)
90+
.collect(HashMap::new, HashMap::putAll, HashMap::putAll);
91+
}
92+
6493
public static Builder withInsertModel(GeneralInsertModel model) {
6594
return new Builder().withInsertModel(model);
6695
}

0 commit comments

Comments
 (0)