Skip to content

Commit fb8a122

Browse files
committed
Initial work on parameter type converter
1 parent bed8e84 commit fb8a122

35 files changed

+302
-126
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright 2016-2020 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;
17+
18+
/**
19+
* A parameter type converter is used to change a parameter value from one type to another
20+
* during statement rendering before the parameter is placed into the parameter map. This can be used
21+
* to somewhat mimic the function of a MyBatis type handler for runtimes such as Spring that don't have
22+
* a corresponding concept.
23+
*
24+
* <p>A parameter type converter is associated with a SqlColumn
25+
*
26+
* <p>The converter is only used for parameters - it is not used for result set processing. The converter will be
27+
* called in the following circumstances:
28+
*
29+
* <ul>
30+
* <li>Parameters in a general insert statement</li>
31+
* <li>Parameters in an update statement</li>
32+
* <li>Parameters in a where clause in any statement</li>
33+
* </ul>
34+
*
35+
* @param <S> Source Type
36+
*
37+
* @see SqlColumn
38+
* @author Jeff Butler
39+
*/
40+
@FunctionalInterface
41+
public interface ParameterTypeConverter<S> {
42+
Object convert(S source);
43+
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,25 @@ public class SqlColumn<T> implements BindableColumn<T>, SortSpecification {
3131
protected String alias;
3232
protected String typeHandler;
3333
protected RenderingStrategy renderingStrategy;
34-
34+
protected ParameterTypeConverter<T> parameterTypeConverter;
35+
3536
private SqlColumn(Builder builder) {
3637
name = Objects.requireNonNull(builder.name);
3738
jdbcType = builder.jdbcType;
3839
table = Objects.requireNonNull(builder.table);
3940
typeHandler = builder.typeHandler;
4041
}
41-
42-
protected SqlColumn(SqlColumn<?> sqlColumn) {
42+
43+
@SuppressWarnings("unchecked")
44+
protected <S> SqlColumn(SqlColumn<S> sqlColumn) {
4345
name = sqlColumn.name;
4446
table = sqlColumn.table;
4547
jdbcType = sqlColumn.jdbcType;
4648
isDescending = sqlColumn.isDescending;
4749
alias = sqlColumn.alias;
4850
typeHandler = sqlColumn.typeHandler;
4951
renderingStrategy = sqlColumn.renderingStrategy;
52+
parameterTypeConverter = (ParameterTypeConverter<T>) sqlColumn.parameterTypeConverter;
5053
}
5154

5255
public String name() {
@@ -72,6 +75,10 @@ public Optional<String> typeHandler() {
7275
return Optional.ofNullable(typeHandler);
7376
}
7477

78+
public Optional<ParameterTypeConverter<T>> parameterTypeConverter() {
79+
return Optional.ofNullable(parameterTypeConverter);
80+
}
81+
7582
@Override
7683
public SortSpecification descending() {
7784
SqlColumn<T> column = new SqlColumn<>(this);
@@ -108,8 +115,8 @@ public Optional<RenderingStrategy> renderingStrategy() {
108115
return Optional.ofNullable(renderingStrategy);
109116
}
110117

111-
public SqlColumn<T> withTypeHandler(String typeHandler) {
112-
SqlColumn<T> column = new SqlColumn<>(this);
118+
public <S> SqlColumn<S> withTypeHandler(String typeHandler) {
119+
SqlColumn<S> column = new SqlColumn<>(this);
113120
column.typeHandler = typeHandler;
114121
return column;
115122
}
@@ -120,6 +127,12 @@ public <S> SqlColumn<S> withRenderingStrategy(RenderingStrategy renderingStrateg
120127
return column;
121128
}
122129

130+
public <S> SqlColumn<S> withParameterTypeConverter(ParameterTypeConverter<S> parameterTypeConverter) {
131+
SqlColumn<S> column = new SqlColumn<>(this);
132+
column.parameterTypeConverter = parameterTypeConverter;
133+
return column;
134+
}
135+
123136
private String applyTableAlias(String tableAlias) {
124137
return tableAlias + "." + name(); //$NON-NLS-1$
125138
}

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

Lines changed: 5 additions & 5 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.
@@ -29,15 +29,15 @@
2929
public abstract class AbstractMultiRowInsertModel<T> {
3030
private SqlTable table;
3131
private List<T> records;
32-
private List<AbstractColumnMapping> columnMappings;
32+
private List<AbstractColumnMapping<?>> columnMappings;
3333

3434
protected AbstractMultiRowInsertModel(AbstractBuilder<T, ?> builder) {
3535
table = Objects.requireNonNull(builder.table);
3636
records = Collections.unmodifiableList(Objects.requireNonNull(builder.records));
3737
columnMappings = Objects.requireNonNull(builder.columnMappings);
3838
}
3939

40-
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
40+
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping<?>, R> mapper) {
4141
return columnMappings.stream().map(mapper);
4242
}
4343

@@ -56,7 +56,7 @@ public int recordCount() {
5656
public abstract static class AbstractBuilder<T, S extends AbstractBuilder<T, S>> {
5757
private SqlTable table;
5858
private List<T> records = new ArrayList<>();
59-
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();
59+
private List<AbstractColumnMapping<?>> columnMappings = new ArrayList<>();
6060

6161
public S withTable(SqlTable table) {
6262
this.table = table;
@@ -68,7 +68,7 @@ public S withRecords(Collection<T> records) {
6868
return getThis();
6969
}
7070

71-
public S withColumnMappings(List<AbstractColumnMapping> columnMappings) {
71+
public S withColumnMappings(List<AbstractColumnMapping<?>> columnMappings) {
7272
this.columnMappings.addAll(columnMappings);
7373
return getThis();
7474
}

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

Lines changed: 2 additions & 2 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.
@@ -32,7 +32,7 @@ public class BatchInsertDSL<T> {
3232

3333
private Collection<T> records;
3434
private SqlTable table;
35-
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();
35+
private List<AbstractColumnMapping<?>> columnMappings = new ArrayList<>();
3636

3737
private BatchInsertDSL(Collection<T> records, SqlTable table) {
3838
this.records = records;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.mybatis.dynamic.sql.util.ValueWhenPresentMapping;
3232

3333
public class GeneralInsertDSL implements Buildable<GeneralInsertModel> {
34-
private List<AbstractColumnMapping> insertMappings = new ArrayList<>();
34+
private List<AbstractColumnMapping<?>> insertMappings = new ArrayList<>();
3535
private SqlTable table;
3636

3737
private GeneralInsertDSL(SqlTable table) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
public class GeneralInsertModel {
3232

3333
private SqlTable table;
34-
private List<AbstractColumnMapping> insertMappings;
34+
private List<AbstractColumnMapping<?>> insertMappings;
3535

3636
private GeneralInsertModel(Builder builder) {
3737
table = Objects.requireNonNull(builder.table);
3838
insertMappings = builder.insertMappings;
3939
}
4040

41-
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
41+
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping<?>, R> mapper) {
4242
return insertMappings.stream().map(mapper);
4343
}
4444

@@ -56,14 +56,14 @@ public GeneralInsertStatementProvider render(RenderingStrategy renderingStrategy
5656

5757
public static class Builder {
5858
private SqlTable table;
59-
private List<AbstractColumnMapping> insertMappings = new ArrayList<>();
59+
private List<AbstractColumnMapping<?>> insertMappings = new ArrayList<>();
6060

6161
public Builder withTable(SqlTable table) {
6262
this.table = table;
6363
return this;
6464
}
6565

66-
public Builder withInsertMappings(List<AbstractColumnMapping> insertMappings) {
66+
public Builder withInsertMappings(List<AbstractColumnMapping<?>> insertMappings) {
6767
this.insertMappings.addAll(insertMappings);
6868
return this;
6969
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class InsertDSL<T> implements Buildable<InsertModel<T>> {
3333

3434
private T record;
3535
private SqlTable table;
36-
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();
36+
private List<AbstractColumnMapping<?>> columnMappings = new ArrayList<>();
3737

3838
private InsertDSL(T record, SqlTable table) {
3939
this.record = record;
@@ -80,7 +80,7 @@ public InsertDSL<T> toProperty(String property) {
8080
return InsertDSL.this;
8181
}
8282

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@
3131
public class InsertModel<T> {
3232
private SqlTable table;
3333
private T record;
34-
private List<AbstractColumnMapping> columnMappings;
34+
private List<AbstractColumnMapping<?>> columnMappings;
3535

3636
private InsertModel(Builder<T> builder) {
3737
table = Objects.requireNonNull(builder.table);
3838
record = Objects.requireNonNull(builder.record);
3939
columnMappings = Objects.requireNonNull(builder.columnMappings);
4040
}
4141

42-
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
42+
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping<?>, R> mapper) {
4343
return columnMappings.stream().map(mapper);
4444
}
4545

@@ -66,7 +66,7 @@ public static <T> Builder<T> withRecord(T record) {
6666
public static class Builder<T> {
6767
private SqlTable table;
6868
private T record;
69-
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();
69+
private List<AbstractColumnMapping<?>> columnMappings = new ArrayList<>();
7070

7171
public Builder<T> withTable(SqlTable table) {
7272
this.table = table;
@@ -78,7 +78,7 @@ public Builder<T> withRecord(T record) {
7878
return this;
7979
}
8080

81-
public Builder<T> withColumnMappings(List<AbstractColumnMapping> columnMappings) {
81+
public Builder<T> withColumnMappings(List<AbstractColumnMapping<?>> columnMappings) {
8282
this.columnMappings.addAll(columnMappings);
8383
return this;
8484
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class MultiRowInsertDSL<T> implements Buildable<MultiRowInsertModel<T>> {
3333

3434
private Collection<T> records;
3535
private SqlTable table;
36-
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();
36+
private List<AbstractColumnMapping<?>> columnMappings = new ArrayList<>();
3737

3838
private MultiRowInsertDSL(Collection<T> records, SqlTable table) {
3939
this.records = records;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ private String calculateInsertStatement(List<Optional<FieldAndValueAndParameters
5656
+ spaceBefore(calculateValuesPhrase(fieldsAndValues));
5757
}
5858

59-
private Function<AbstractColumnMapping, Optional<FieldAndValueAndParameters>> toFieldAndValue(
59+
private Function<AbstractColumnMapping<?>, Optional<FieldAndValueAndParameters>> toFieldAndValue(
6060
GeneralInsertValuePhraseVisitor visitor) {
6161
return insertMapping -> toFieldAndValue(visitor, insertMapping);
6262
}
6363

64-
private Optional<FieldAndValueAndParameters> toFieldAndValue(GeneralInsertValuePhraseVisitor visitor,
65-
AbstractColumnMapping insertMapping) {
64+
private <T> Optional<FieldAndValueAndParameters> toFieldAndValue(GeneralInsertValuePhraseVisitor visitor,
65+
AbstractColumnMapping<T> insertMapping) {
6666
return insertMapping.accept(visitor);
6767
}
6868

0 commit comments

Comments
 (0)