Skip to content

Commit bdec2d7

Browse files
committed
Deprecations
- Deprecate the constants in RenderingStrategy as they are a code smell and mildly dangerous (SonarQube S2390) - Deprecate the DeleteWithMapper, SelectWithMapper, and UpdateWithMapper methods in favor of the new style of MyBatis3 specific support (#128)
1 parent 61fd0cd commit bdec2d7

File tree

56 files changed

+784
-682
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+784
-682
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ One capability is that very expressive dynamic queries can be generated. Here's
6767
.and(bodyWeight, isBetween(1.0).and(3.0))
6868
.orderBy(id.descending(), bodyWeight)
6969
.build()
70-
.render(RenderingStrategy.MYBATIS3);
70+
.render(RenderingStrategies.MYBATIS3);
7171

7272
List<AnimalData> animals = mapper.selectMany(selectStatement);
7373
assertThat(animals.size()).isEqualTo(4);
@@ -192,7 +192,7 @@ For example, a very simple select statement can be defined like this:
192192
.from(simpleTable)
193193
.where(id, isEqualTo(3))
194194
.build()
195-
.render(RenderingStrategy.MYBATIS3);
195+
.render(RenderingStrategies.MYBATIS3);
196196
```
197197

198198
Or this (also note that you can give a table an alias):
@@ -202,15 +202,15 @@ Or this (also note that you can give a table an alias):
202202
.from(simpleTable, "a")
203203
.where(id, isNull())
204204
.build()
205-
.render(RenderingStrategy.MYBATIS3);
205+
.render(RenderingStrategies.MYBATIS3);
206206
```
207207
A delete statement looks like this:
208208

209209
```java
210210
DeleteStatementProvider deleteStatement = deleteFrom(simpleTable)
211211
.where(occupation, isNull())
212212
.build()
213-
.render(RenderingStrategy.MYBATIS3);
213+
.render(RenderingStrategies.MYBATIS3);
214214
```
215215

216216
The "between" condition is also expressive:
@@ -220,7 +220,7 @@ The "between" condition is also expressive:
220220
.from(simpleTable)
221221
.where(id, isBetween(1).and(4))
222222
.build()
223-
.render(RenderingStrategy.MYBATIS3);
223+
.render(RenderingStrategies.MYBATIS3);
224224
```
225225

226226
More complex expressions can be built using the "and" and "or" conditions as follows:
@@ -231,7 +231,7 @@ More complex expressions can be built using the "and" and "or" conditions as fol
231231
.where(id, isGreaterThan(2))
232232
.or(occupation, isNull(), and(id, isLessThan(6)))
233233
.build()
234-
.render(RenderingStrategy.MYBATIS3);
234+
.render(RenderingStrategies.MYBATIS3);
235235
```
236236

237237
All of these statements rely on a set of expressive static methods. It is typical to import the following:
@@ -259,7 +259,7 @@ an example from `examples.simple.SimpleTableAnnotatedMapperTest`:
259259
.where(id, isEqualTo(1))
260260
.or(occupation, isNull())
261261
.build()
262-
.render(RenderingStrategy.MYBATIS3);
262+
.render(RenderingStrategies.MYBATIS3);
263263

264264
List<SimpleTableRecord> rows = mapper.selectMany(selectStatement);
265265

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

+14
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717

1818
import java.util.Objects;
1919
import java.util.function.Function;
20+
import java.util.function.ToIntFunction;
2021

2122
import org.mybatis.dynamic.sql.BindableColumn;
2223
import org.mybatis.dynamic.sql.SqlCriterion;
2324
import org.mybatis.dynamic.sql.SqlTable;
2425
import org.mybatis.dynamic.sql.VisitableCondition;
2526
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
2627
import org.mybatis.dynamic.sql.util.Buildable;
28+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteCompleter;
29+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
2730
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
2831

2932
public class DeleteDSL<R> implements Buildable<R> {
@@ -75,6 +78,17 @@ public static DeleteDSL<DeleteModel> deleteFrom(SqlTable table) {
7578
return deleteFrom(Function.identity(), table);
7679
}
7780

81+
/**
82+
* Delete record(s) by executing a MyBatis3 mapper method.
83+
*
84+
* @deprecated in favor of {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, MyBatis3DeleteCompleter)}.
85+
* This method will be removed without direct replacement in a future version
86+
* @param <T> return value from a delete method - typically Integer
87+
* @param mapperMethod MyBatis3 mapper method that performs the delete
88+
* @param table table to delete from
89+
* @return number of records deleted - typically as Integer
90+
*/
91+
@Deprecated
7892
public static <T> DeleteDSL<MyBatis3DeleteModelAdapter<T>> deleteFromWithMapper(
7993
Function<DeleteStatementProvider, T> mapperMethod, SqlTable table) {
8094
return deleteFrom(deleteModel -> MyBatis3DeleteModelAdapter.of(deleteModel, mapperMethod), table);

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -20,13 +20,18 @@
2020

2121
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
2222
import org.mybatis.dynamic.sql.render.RenderingStrategy;
23+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteCompleter;
2324

2425
/**
2526
* This adapter will render the underlying delete model for MyBatis3, and then call a MyBatis mapper method.
27+
*
28+
* @deprecated in favor of {@link MyBatis3DeleteCompleter}. This class will be removed without replacement in a
29+
* future version
2630
*
2731
* @author Jeff Butler
2832
*
2933
*/
34+
@Deprecated
3035
public class MyBatis3DeleteModelAdapter<R> {
3136

3237
private DeleteModel deleteModel;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright 2016-2019 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.render;
17+
18+
public class RenderingStrategies {
19+
private RenderingStrategies() {}
20+
21+
public static final RenderingStrategy MYBATIS3 = new MyBatis3RenderingStrategy();
22+
23+
public static final RenderingStrategy SPRING_NAMED_PARAMETER = new SpringNamedParameterRenderingStrategy();
24+
}

src/main/java/org/mybatis/dynamic/sql/render/RenderingStrategy.java

+14
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,24 @@
1818
import org.mybatis.dynamic.sql.BindableColumn;
1919

2020
public abstract class RenderingStrategy {
21+
/**
22+
* Rendering strategy for MyBatis3.
23+
*
24+
* @deprecated use {@link RenderingStrategies#MYBATIS3} instead
25+
*/
26+
@Deprecated
2127
@SuppressWarnings("squid:S2390")
2228
public static final RenderingStrategy MYBATIS3 = new MyBatis3RenderingStrategy();
29+
30+
/**
31+
* Rendering strategy for Spring JDBC Template Named Parameters.
32+
*
33+
* @deprecated use {@link RenderingStrategies#SPRING_NAMED_PARAMETER} instead
34+
*/
35+
@Deprecated
2336
@SuppressWarnings("squid:S2390")
2437
public static final RenderingStrategy SPRING_NAMED_PARAMETER = new SpringNamedParameterRenderingStrategy();
38+
2539
public static final String DEFAULT_PARAMETER_PREFIX = "parameters"; //$NON-NLS-1$
2640

2741
public abstract String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName);

src/main/java/org/mybatis/dynamic/sql/select/MyBatis3SelectModelAdapter.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -20,13 +20,18 @@
2020

2121
import org.mybatis.dynamic.sql.render.RenderingStrategy;
2222
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
23+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectCompleter;
2324

2425
/**
2526
* This adapter will render the underlying select model for MyBatis3, and then call a MyBatis mapper method.
26-
*
27+
*
28+
* @deprecated in favor is {@link MyBatis3SelectCompleter}. This class will be removed without direct replacement
29+
* in a future version
30+
*
2731
* @author Jeff Butler
2832
*
2933
*/
34+
@Deprecated
3035
public class MyBatis3SelectModelAdapter<R> {
3136

3237
private SelectModel selectModel;

src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java

+23
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
2727
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2828
import org.mybatis.dynamic.sql.util.Buildable;
29+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
2930

3031
/**
3132
* Implements a standard SQL dialect for building model classes.
@@ -72,11 +73,33 @@ public static <R> QueryExpressionDSL.FromGatherer<R> selectDistinct(Function<Sel
7273
.build();
7374
}
7475

76+
/**
77+
* Select records by executing a MyBatis3 Mapper.
78+
*
79+
* @deprecated in favor of various select methods in {@link MyBatis3Utils}.
80+
* This method will be removed without direct replacement in a future version
81+
* @param <T> the return type from a MyBatis mapper - typically a List or a single record
82+
* @param mapperMethod MyBatis3 Mapper Method to perfomr the select
83+
* @param selectList the column list to select
84+
* @return the partially created query
85+
*/
86+
@Deprecated
7587
public static <T> QueryExpressionDSL.FromGatherer<MyBatis3SelectModelAdapter<T>> selectWithMapper(
7688
Function<SelectStatementProvider, T> mapperMethod, BasicColumn...selectList) {
7789
return select(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod), selectList);
7890
}
7991

92+
/**
93+
* Select records by executing a MyBatis3 Mapper.
94+
*
95+
* @deprecated in favor of various select methods in {@link MyBatis3Utils}.
96+
* This method will be removed without direct replacement in a future version
97+
* @param <T> the return type from a MyBatis mapper - typically a List or a single record
98+
* @param mapperMethod MyBatis3 Mapper Method to perfomr the select
99+
* @param selectList the column list to select
100+
* @return the partially created query
101+
*/
102+
@Deprecated
80103
public static <T> QueryExpressionDSL.FromGatherer<MyBatis3SelectModelAdapter<T>> selectDistinctWithMapper(
81104
Function<SelectStatementProvider, T> mapperMethod, BasicColumn...selectList) {
82105
return selectDistinct(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod),

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2019 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.
@@ -20,13 +20,17 @@
2020

2121
import org.mybatis.dynamic.sql.render.RenderingStrategy;
2222
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
23+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateCompleter;
2324

2425
/**
2526
* This adapter will render the underlying update model for MyBatis3, and then call a MyBatis mapper method.
2627
*
28+
* @deprecated in favor of {@link MyBatis3UpdateCompleter}. This class will be removed without direct
29+
* replacement in a future version.
2730
* @author Jeff Butler
2831
*
2932
*/
33+
@Deprecated
3034
public class MyBatis3UpdateModelAdapter<R> {
3135

3236
private UpdateModel updateModel;

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

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Objects;
2121
import java.util.function.Function;
2222
import java.util.function.Supplier;
23+
import java.util.function.ToIntFunction;
2324

2425
import org.mybatis.dynamic.sql.BasicColumn;
2526
import org.mybatis.dynamic.sql.BindableColumn;
@@ -37,6 +38,8 @@
3738
import org.mybatis.dynamic.sql.util.StringConstantMapping;
3839
import org.mybatis.dynamic.sql.util.UpdateMapping;
3940
import org.mybatis.dynamic.sql.util.ValueMapping;
41+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateCompleter;
42+
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
4043
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
4144

4245
public class UpdateDSL<R> implements Buildable<R> {
@@ -94,6 +97,17 @@ public static UpdateDSL<UpdateModel> update(SqlTable table) {
9497
return update(Function.identity(), table);
9598
}
9699

100+
/**
101+
* Executes an update using a MyBatis3 mapper method.
102+
*
103+
* @deprecated in favor of {@link MyBatis3Utils#update(ToIntFunction, SqlTable, MyBatis3UpdateCompleter)}. This
104+
* method will be removed without direct replacement in a future version.
105+
* @param <T> return value from an update method - typically Integer
106+
* @param mapperMethod MyBatis3 mapper method that performs the update
107+
* @param table table to update
108+
* @return number of records updated - typically as Integer
109+
*/
110+
@Deprecated
97111
public static <T> UpdateDSL<MyBatis3UpdateModelAdapter<T>> updateWithMapper(
98112
Function<UpdateStatementProvider, T> mapperMethod, SqlTable table) {
99113
return update(updateModel -> MyBatis3UpdateModelAdapter.of(updateModel, mapperMethod), table);

src/main/java/org/mybatis/dynamic/sql/util/mybatis3/MyBatis3Utils.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.Collection;
1919
import java.util.List;
20-
import java.util.Optional;
2120
import java.util.function.Function;
2221
import java.util.function.ToIntFunction;
2322
import java.util.function.ToLongFunction;
@@ -32,7 +31,7 @@
3231
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
3332
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
3433
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
35-
import org.mybatis.dynamic.sql.render.RenderingStrategy;
34+
import org.mybatis.dynamic.sql.render.RenderingStrategies;
3635
import org.mybatis.dynamic.sql.select.CompletableQuery;
3736
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
3837
import org.mybatis.dynamic.sql.select.SelectDSL;
@@ -57,27 +56,27 @@ public static long count(ToLongFunction<SelectStatementProvider> mapper,
5756

5857
public static long count(ToLongFunction<SelectStatementProvider> mapper,
5958
QueryExpressionDSL<SelectModel> start, MyBatis3SelectCompleter completer) {
60-
return mapper.applyAsLong(completer.apply(start).build().render(RenderingStrategy.MYBATIS3));
59+
return mapper.applyAsLong(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
6160
}
6261

6362
public static int deleteFrom(ToIntFunction<DeleteStatementProvider> mapper,
6463
SqlTable table, MyBatis3DeleteCompleter completer) {
6564
return mapper.applyAsInt(
6665
completer.apply(DeleteDSL.deleteFrom(table))
6766
.build()
68-
.render(RenderingStrategy.MYBATIS3));
67+
.render(RenderingStrategies.MYBATIS3));
6968
}
7069

7170
public static <R> int insert(ToIntFunction<InsertStatementProvider<R>> mapper, R record,
7271
SqlTable table, UnaryOperator<InsertDSL<R>> completer) {
7372
return mapper.applyAsInt(completer.apply(
74-
InsertDSL.insert(record).into(table)).build().render(RenderingStrategy.MYBATIS3));
73+
InsertDSL.insert(record).into(table)).build().render(RenderingStrategies.MYBATIS3));
7574
}
7675

7776
public static <R> int insertMultiple(ToIntFunction<MultiRowInsertStatementProvider<R>> mapper,
7877
Collection<R> records, SqlTable table, UnaryOperator<MultiRowInsertDSL<R>> completer) {
7978
return mapper.applyAsInt(completer.apply(
80-
MultiRowInsertDSL.insert(records).into(table)).build().render(RenderingStrategy.MYBATIS3));
79+
MultiRowInsertDSL.insert(records).into(table)).build().render(RenderingStrategies.MYBATIS3));
8180
}
8281

8382
public static <R> List<R> selectDistinct(Function<SelectStatementProvider, List<R>> mapper,
@@ -87,7 +86,7 @@ public static <R> List<R> selectDistinct(Function<SelectStatementProvider, List<
8786

8887
public static <R> List<R> selectDistinct(Function<SelectStatementProvider, List<R>> mapper,
8988
CompletableQuery<SelectModel> start, MyBatis3SelectCompleter completer) {
90-
return mapper.apply(completer.apply(start).build().render(RenderingStrategy.MYBATIS3));
89+
return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
9190
}
9291

9392
public static <R> List<R> selectList(Function<SelectStatementProvider, List<R>> mapper,
@@ -97,25 +96,25 @@ public static <R> List<R> selectList(Function<SelectStatementProvider, List<R>>
9796

9897
public static <R> List<R> selectList(Function<SelectStatementProvider, List<R>> mapper,
9998
CompletableQuery<SelectModel> start, MyBatis3SelectCompleter completer) {
100-
return mapper.apply(completer.apply(start).build().render(RenderingStrategy.MYBATIS3));
99+
return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
101100
}
102101

103-
public static <R> Optional<R> selectOne(Function<SelectStatementProvider, Optional<R>> mapper,
102+
public static <R> R selectOne(Function<SelectStatementProvider, R> mapper,
104103
BasicColumn[] selectList, SqlTable table, MyBatis3SelectCompleter completer) {
105104
return selectOne(mapper, SelectDSL.select(selectList).from(table), completer);
106105
}
107106

108-
public static <R> Optional<R> selectOne(Function<SelectStatementProvider, Optional<R>> mapper,
107+
public static <R> R selectOne(Function<SelectStatementProvider, R> mapper,
109108
CompletableQuery<SelectModel> start,
110109
MyBatis3SelectCompleter completer) {
111-
return mapper.apply(completer.apply(start).build().render(RenderingStrategy.MYBATIS3));
110+
return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
112111
}
113112

114113
public static int update(ToIntFunction<UpdateStatementProvider> mapper,
115114
SqlTable table, MyBatis3UpdateCompleter completer) {
116115
return mapper.applyAsInt(
117116
completer.apply(UpdateDSL.update(table))
118117
.build()
119-
.render(RenderingStrategy.MYBATIS3));
118+
.render(RenderingStrategies.MYBATIS3));
120119
}
121120
}

0 commit comments

Comments
 (0)