Skip to content

Commit 055b3ed

Browse files
committed
Refactoring usage of parameter value converter
1 parent fb8a122 commit 055b3ed

12 files changed

+50
-38
lines changed

src/main/java/org/mybatis/dynamic/sql/BindableColumn.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
*
2727
* @author Jeff Butler
2828
*
29-
* @param <T> - even though the type is not directly used in this class,
30-
* it is used by the compiler to match columns with conditions so it should
31-
* not be removed.
29+
* @param <T> - the Java type that corresponds to this column
3230
*/
3331
public interface BindableColumn<T> extends BasicColumn {
3432

@@ -49,4 +47,8 @@ default Optional<String> typeHandler() {
4947
default Optional<RenderingStrategy> renderingStrategy() {
5048
return Optional.empty();
5149
}
50+
51+
default Optional<ParameterTypeConverter<T>> parameterTypeConverter() {
52+
return Optional.empty();
53+
}
5254
}

src/main/java/org/mybatis/dynamic/sql/ParameterTypeConverter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
* to somewhat mimic the function of a MyBatis type handler for runtimes such as Spring that don't have
2222
* a corresponding concept.
2323
*
24-
* <p>A parameter type converter is associated with a SqlColumn
24+
* <p>A parameter type converter is associated with a SqlColumn.
25+
*
26+
* <p>A parameter type converter is compatible with Spring's general Converter interface so existing converters can be reused
27+
* here if they are marked with this additional interface.
2528
*
2629
* <p>The converter is only used for parameters - it is not used for result set processing. The converter will be
2730
* called in the following circumstances:
@@ -31,7 +34,7 @@
3134
* <li>Parameters in an update statement</li>
3235
* <li>Parameters in a where clause in any statement</li>
3336
* </ul>
34-
*
37+
*
3538
* @param <S> Source Type
3639
*
3740
* @see SqlColumn

src/main/java/org/mybatis/dynamic/sql/SqlColumn.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private SqlColumn(Builder builder) {
4141
}
4242

4343
@SuppressWarnings("unchecked")
44-
protected <S> SqlColumn(SqlColumn<S> sqlColumn) {
44+
protected SqlColumn(SqlColumn<?> sqlColumn) {
4545
name = sqlColumn.name;
4646
table = sqlColumn.table;
4747
jdbcType = sqlColumn.jdbcType;
@@ -75,6 +75,7 @@ public Optional<String> typeHandler() {
7575
return Optional.ofNullable(typeHandler);
7676
}
7777

78+
@Override
7879
public Optional<ParameterTypeConverter<T>> parameterTypeConverter() {
7980
return Optional.ofNullable(parameterTypeConverter);
8081
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public InsertDSL<T> toProperty(String property) {
8080
return InsertDSL.this;
8181
}
8282

83-
public InsertDSL<T> toPropertyWhenPresent(String property, Supplier<F> valueSupplier) {
83+
public InsertDSL<T> toPropertyWhenPresent(String property, Supplier<Object> valueSupplier) {
8484
columnMappings.add(PropertyWhenPresentMapping.of(column, property, valueSupplier));
8585
return InsertDSL.this;
8686
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.concurrent.atomic.AtomicInteger;
2020
import java.util.function.Function;
2121

22-
import org.mybatis.dynamic.sql.ParameterTypeConverter;
2322
import org.mybatis.dynamic.sql.SqlColumn;
2423
import org.mybatis.dynamic.sql.render.RenderingStrategy;
2524
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
@@ -70,14 +69,10 @@ public <T> Optional<FieldAndValueAndParameters> visit(ValueWhenPresentMapping<T>
7069
return mapping.value().flatMap(v -> buildValueFragment(mapping, v));
7170
}
7271

73-
private <T> Optional<FieldAndValueAndParameters> buildValueFragment(AbstractColumnMapping<T> mapping, T value) {
74-
Optional<ParameterTypeConverter<T>> typeConverter = mapping.parameterTypeConverter();
75-
76-
return buildFragment(mapping, typeConverter.map(tc -> tc.convert(value))
77-
.orElse(value));
78-
72+
private <T> Optional<FieldAndValueAndParameters> buildValueFragment(AbstractColumnMapping<T> mapping, Object value) {
73+
return buildFragment(mapping, value);
7974
}
80-
75+
8176
private <T> Optional<FieldAndValueAndParameters> buildFragment(AbstractColumnMapping<T> mapping, Object value) {
8277
String mapKey = RenderingStrategy.formatParameterMapKey(sequence);
8378

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public <T> Optional<FragmentAndParameters> visit(ColumnToColumnMapping<T> mappin
108108
.buildOptional();
109109
}
110110

111-
private <T> Optional<FragmentAndParameters> buildFragment(AbstractColumnMapping<T> mapping, T value) {
111+
private <T> Optional<FragmentAndParameters> buildFragment(AbstractColumnMapping<T> mapping, Object value) {
112112
String mapKey = RenderingStrategy.formatParameterMapKey(sequence);
113113

114114
String jdbcPlaceholder = mapping.mapColumn(toJdbcPlaceholder(mapKey));

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
package org.mybatis.dynamic.sql.util;
1717

1818
import java.util.Objects;
19-
import java.util.Optional;
2019
import java.util.function.Function;
2120

22-
import org.mybatis.dynamic.sql.ParameterTypeConverter;
2321
import org.mybatis.dynamic.sql.SqlColumn;
2422

2523
public abstract class AbstractColumnMapping<T> {
@@ -33,9 +31,5 @@ public <R> R mapColumn(Function<SqlColumn<?>, R> mapper) {
3331
return mapper.apply(column);
3432
}
3533

36-
public Optional<ParameterTypeConverter<T>> parameterTypeConverter() {
37-
return column.parameterTypeConverter();
38-
}
39-
4034
public abstract <R> R accept(ColumnMappingVisitor<R> visitor);
4135
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
import org.mybatis.dynamic.sql.SqlColumn;
2222

2323
public class PropertyWhenPresentMapping<T> extends PropertyMapping<T> {
24-
private Supplier<?> valueSupplier;
24+
/**
25+
* The Supplier is of type Object because we need to allow for data type changes
26+
* for Spring where there are no type handlers.
27+
*/
28+
private Supplier<Object> valueSupplier;
2529

26-
private PropertyWhenPresentMapping(SqlColumn<T> column, String property, Supplier<T> valueSupplier) {
30+
private PropertyWhenPresentMapping(SqlColumn<T> column, String property, Supplier<Object> valueSupplier) {
2731
super(column, property);
2832
this.valueSupplier = Objects.requireNonNull(valueSupplier);
2933
}
@@ -38,7 +42,7 @@ public <R> R accept(ColumnMappingVisitor<R> visitor) {
3842
}
3943

4044
public static <T> PropertyWhenPresentMapping<T> of(SqlColumn<T> column, String property,
41-
Supplier<T> valueSupplier) {
45+
Supplier<Object> valueSupplier) {
4246
return new PropertyWhenPresentMapping<>(column, property, valueSupplier);
4347
}
4448
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ private ValueMapping(SqlColumn<T> column, Supplier<T> valueSupplier) {
2929
this.valueSupplier = Objects.requireNonNull(valueSupplier);
3030
}
3131

32-
public T value() {
33-
return valueSupplier.get();
32+
public Object value() {
33+
return column.parameterTypeConverter().map(tc -> tc.convert(valueSupplier.get()))
34+
.orElseGet(valueSupplier);
3435
}
3536

3637
@Override

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ private ValueWhenPresentMapping(SqlColumn<T> column, Supplier<T> valueSupplier)
3030
this.valueSupplier = Objects.requireNonNull(valueSupplier);
3131
}
3232

33-
public Optional<T> value() {
34-
return Optional.ofNullable(valueSupplier.get());
33+
public Optional<Object> value() {
34+
return Optional.ofNullable(valueSupplier.get()).map(this::convert);
35+
}
36+
37+
private Object convert(T value) {
38+
return column.parameterTypeConverter().map(tc -> tc.convert(value)).orElse(value);
3539
}
3640

3741
@Override

0 commit comments

Comments
 (0)