Skip to content

Commit 2d7bebb

Browse files
committed
Start of support for Value or Null Mapping
1 parent 4090198 commit 2d7bebb

File tree

7 files changed

+106
-9
lines changed

7 files changed

+106
-9
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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,6 +26,7 @@
2626
import org.mybatis.dynamic.sql.util.NullMapping;
2727
import org.mybatis.dynamic.sql.util.StringConstantMapping;
2828
import org.mybatis.dynamic.sql.util.ValueMapping;
29+
import org.mybatis.dynamic.sql.util.ValueOrNullMapping;
2930
import org.mybatis.dynamic.sql.util.ValueWhenPresentMapping;
3031

3132
public class GeneralInsertValuePhraseVisitor extends GeneralInsertMappingVisitor<Optional<FieldAndValueAndParameters>> {
@@ -39,9 +40,7 @@ public GeneralInsertValuePhraseVisitor(RenderingStrategy renderingStrategy) {
3940

4041
@Override
4142
public Optional<FieldAndValueAndParameters> visit(NullMapping mapping) {
42-
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
43-
.withValuePhrase("null") //$NON-NLS-1$
44-
.buildOptional();
43+
return buildNullFragment(mapping);
4544
}
4645

4746
@Override
@@ -63,6 +62,12 @@ public <T> Optional<FieldAndValueAndParameters> visit(ValueMapping<T> mapping) {
6362
return buildValueFragment(mapping, mapping.value());
6463
}
6564

65+
@Override
66+
public <T> Optional<FieldAndValueAndParameters> visit(ValueOrNullMapping<T> mapping) {
67+
return mapping.value().map(v -> buildValueFragment(mapping, v))
68+
.orElseGet(() -> buildNullFragment(mapping));
69+
}
70+
6671
@Override
6772
public <T> Optional<FieldAndValueAndParameters> visit(ValueWhenPresentMapping<T> mapping) {
6873
return mapping.value().flatMap(v -> buildValueFragment(mapping, v));
@@ -73,6 +78,12 @@ private Optional<FieldAndValueAndParameters> buildValueFragment(AbstractColumnMa
7378
return buildFragment(mapping, value);
7479
}
7580

81+
private Optional<FieldAndValueAndParameters> buildNullFragment(AbstractColumnMapping mapping) {
82+
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
83+
.withValuePhrase("null") //$NON-NLS-1$
84+
.buildOptional();
85+
}
86+
7687
private Optional<FieldAndValueAndParameters> buildFragment(AbstractColumnMapping mapping, Object value) {
7788
String mapKey = RenderingStrategy.formatParameterMapKey(sequence);
7889

src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -34,6 +34,7 @@
3434
import org.mybatis.dynamic.sql.util.SelectMapping;
3535
import org.mybatis.dynamic.sql.util.StringConstantMapping;
3636
import org.mybatis.dynamic.sql.util.ValueMapping;
37+
import org.mybatis.dynamic.sql.util.ValueOrNullMapping;
3738
import org.mybatis.dynamic.sql.util.ValueWhenPresentMapping;
3839
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
3940
import org.mybatis.dynamic.sql.where.AbstractWhereSupport;
@@ -126,6 +127,15 @@ public UpdateDSL<R> equalTo(BasicColumn rightColumn) {
126127
return UpdateDSL.this;
127128
}
128129

130+
public UpdateDSL<R> equalToOrNull(T value) {
131+
return equalToOrNull(() -> value);
132+
}
133+
134+
public UpdateDSL<R> equalToOrNull(Supplier<T> valueSupplier) {
135+
columnMappings.add(ValueOrNullMapping.of(column, valueSupplier));
136+
return UpdateDSL.this;
137+
}
138+
129139
public UpdateDSL<R> equalToWhenPresent(T value) {
130140
return equalToWhenPresent(() -> value);
131141
}

src/main/java/org/mybatis/dynamic/sql/update/render/SetPhraseVisitor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -33,6 +33,7 @@
3333
import org.mybatis.dynamic.sql.util.StringConstantMapping;
3434
import org.mybatis.dynamic.sql.util.UpdateMappingVisitor;
3535
import org.mybatis.dynamic.sql.util.ValueMapping;
36+
import org.mybatis.dynamic.sql.util.ValueOrNullMapping;
3637
import org.mybatis.dynamic.sql.util.ValueWhenPresentMapping;
3738

3839
public class SetPhraseVisitor extends UpdateMappingVisitor<Optional<FragmentAndParameters>> {
@@ -74,6 +75,16 @@ public <T> Optional<FragmentAndParameters> visit(ValueMapping<T> mapping) {
7475
return buildFragment(mapping, mapping.value());
7576
}
7677

78+
@Override
79+
public <T> Optional<FragmentAndParameters> visit(ValueOrNullMapping<T> mapping) {
80+
return mapping.value()
81+
.map(v -> buildFragment(mapping, v))
82+
.orElseGet(() -> FragmentAndParameters
83+
.withFragment(mapping.columnName() + " = null") //$NON-NLS-1$
84+
.buildOptional()
85+
);
86+
}
87+
7788
@Override
7889
public <T> Optional<FragmentAndParameters> visit(ValueWhenPresentMapping<T> mapping) {
7990
return mapping.value().flatMap(v -> buildFragment(mapping, v));

src/main/java/org/mybatis/dynamic/sql/util/ColumnMappingVisitor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -38,6 +38,8 @@ public interface ColumnMappingVisitor<R> {
3838

3939
<T> R visit(ValueMapping<T> mapping);
4040

41+
<T> R visit(ValueOrNullMapping<T> mapping);
42+
4143
<T> R visit(ValueWhenPresentMapping<T> mapping);
4244

4345
R visit(SelectMapping mapping);

src/main/java/org/mybatis/dynamic/sql/util/InsertMappingVisitor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -21,6 +21,11 @@ public final <T> R visit(ValueMapping<T> mapping) {
2121
throw new UnsupportedOperationException();
2222
}
2323

24+
@Override
25+
public final <T> R visit(ValueOrNullMapping<T> mapping) {
26+
throw new UnsupportedOperationException();
27+
}
28+
2429
@Override
2530
public final <T> R visit(ValueWhenPresentMapping<T> mapping) {
2631
throw new UnsupportedOperationException();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016-2021 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+
* http://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.util;
17+
18+
import org.mybatis.dynamic.sql.SqlColumn;
19+
20+
import java.util.Objects;
21+
import java.util.Optional;
22+
import java.util.function.Supplier;
23+
24+
public class ValueOrNullMapping<T> extends AbstractColumnMapping {
25+
26+
private final Supplier<T> valueSupplier;
27+
// keep a reference to the column so we don't lose the type
28+
private final SqlColumn<T> localColumn;
29+
30+
private ValueOrNullMapping(SqlColumn<T> column, Supplier<T> valueSupplier) {
31+
super(column);
32+
this.valueSupplier = Objects.requireNonNull(valueSupplier);
33+
localColumn = Objects.requireNonNull(column);
34+
}
35+
36+
public Optional<Object> value() {
37+
return Optional.ofNullable(localColumn.convertParameterType(valueSupplier.get()));
38+
}
39+
40+
@Override
41+
public <R> R accept(ColumnMappingVisitor<R> visitor) {
42+
return visitor.visit(this);
43+
}
44+
45+
public static <T> ValueOrNullMapping<T> of(SqlColumn<T> column, Supplier<T> valueSupplier) {
46+
return new ValueOrNullMapping<>(column, valueSupplier);
47+
}
48+
}

src/test/java/org/mybatis/dynamic/sql/util/ColumnMappingVisitorTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2021 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.
@@ -192,6 +192,11 @@ public <R> String visit(ValueMapping<R> mapping) {
192192
return "Value Mapping";
193193
}
194194

195+
@Override
196+
public <R> String visit(ValueOrNullMapping<R> mapping) {
197+
return "Value or Null Mapping";
198+
}
199+
195200
@Override
196201
public <R> String visit(ValueWhenPresentMapping<R> mapping) {
197202
return "Value When Present Mapping";
@@ -246,6 +251,11 @@ public <R> String visit(ValueMapping<R> mapping) {
246251
return "Value Mapping";
247252
}
248253

254+
@Override
255+
public <R> String visit(ValueOrNullMapping<R> mapping) {
256+
return "Value or Null Mapping";
257+
}
258+
249259
@Override
250260
public <R> String visit(ValueWhenPresentMapping<R> mapping) {
251261
return "Value When Present Mapping";

0 commit comments

Comments
 (0)