Skip to content

Commit

Permalink
Deprecations
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
jeffgbutler committed Aug 18, 2019
1 parent 61fd0cd commit bdec2d7
Show file tree
Hide file tree
Showing 56 changed files with 784 additions and 682 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -67,7 +67,7 @@ One capability is that very expressive dynamic queries can be generated. Here's
.and(bodyWeight, isBetween(1.0).and(3.0))
.orderBy(id.descending(), bodyWeight)
.build()
.render(RenderingStrategy.MYBATIS3);
.render(RenderingStrategies.MYBATIS3);

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

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

```java
DeleteStatementProvider deleteStatement = deleteFrom(simpleTable)
.where(occupation, isNull())
.build()
.render(RenderingStrategy.MYBATIS3);
.render(RenderingStrategies.MYBATIS3);
```

The "between" condition is also expressive:
Expand All @@ -220,7 +220,7 @@ The "between" condition is also expressive:
.from(simpleTable)
.where(id, isBetween(1).and(4))
.build()
.render(RenderingStrategy.MYBATIS3);
.render(RenderingStrategies.MYBATIS3);
```

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

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

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

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java
Expand Up @@ -17,13 +17,16 @@

import java.util.Objects;
import java.util.function.Function;
import java.util.function.ToIntFunction;

import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.SqlCriterion;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.VisitableCondition;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteCompleter;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;

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

/**
* Delete record(s) by executing a MyBatis3 mapper method.
*
* @deprecated in favor of {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, MyBatis3DeleteCompleter)}.
* This method will be removed without direct replacement in a future version
* @param <T> return value from a delete method - typically Integer
* @param mapperMethod MyBatis3 mapper method that performs the delete
* @param table table to delete from
* @return number of records deleted - typically as Integer
*/
@Deprecated
public static <T> DeleteDSL<MyBatis3DeleteModelAdapter<T>> deleteFromWithMapper(
Function<DeleteStatementProvider, T> mapperMethod, SqlTable table) {
return deleteFrom(deleteModel -> MyBatis3DeleteModelAdapter.of(deleteModel, mapperMethod), table);
Expand Down
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,13 +20,18 @@

import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3DeleteCompleter;

/**
* This adapter will render the underlying delete model for MyBatis3, and then call a MyBatis mapper method.
*
* @deprecated in favor of {@link MyBatis3DeleteCompleter}. This class will be removed without replacement in a
* future version
*
* @author Jeff Butler
*
*/
@Deprecated
public class MyBatis3DeleteModelAdapter<R> {

private DeleteModel deleteModel;
Expand Down
@@ -0,0 +1,24 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.render;

public class RenderingStrategies {
private RenderingStrategies() {}

public static final RenderingStrategy MYBATIS3 = new MyBatis3RenderingStrategy();

public static final RenderingStrategy SPRING_NAMED_PARAMETER = new SpringNamedParameterRenderingStrategy();
}
Expand Up @@ -18,10 +18,24 @@
import org.mybatis.dynamic.sql.BindableColumn;

public abstract class RenderingStrategy {
/**
* Rendering strategy for MyBatis3.
*
* @deprecated use {@link RenderingStrategies#MYBATIS3} instead
*/
@Deprecated
@SuppressWarnings("squid:S2390")
public static final RenderingStrategy MYBATIS3 = new MyBatis3RenderingStrategy();

/**
* Rendering strategy for Spring JDBC Template Named Parameters.
*
* @deprecated use {@link RenderingStrategies#SPRING_NAMED_PARAMETER} instead
*/
@Deprecated
@SuppressWarnings("squid:S2390")
public static final RenderingStrategy SPRING_NAMED_PARAMETER = new SpringNamedParameterRenderingStrategy();

public static final String DEFAULT_PARAMETER_PREFIX = "parameters"; //$NON-NLS-1$

public abstract String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName);
Expand Down
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,13 +20,18 @@

import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectCompleter;

/**
* This adapter will render the underlying select model for MyBatis3, and then call a MyBatis mapper method.
*
*
* @deprecated in favor is {@link MyBatis3SelectCompleter}. This class will be removed without direct replacement
* in a future version
*
* @author Jeff Butler
*
*/
@Deprecated
public class MyBatis3SelectModelAdapter<R> {

private SelectModel selectModel;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java
Expand Up @@ -26,6 +26,7 @@
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

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

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

/**
* Select records by executing a MyBatis3 Mapper.
*
* @deprecated in favor of various select methods in {@link MyBatis3Utils}.
* This method will be removed without direct replacement in a future version
* @param <T> the return type from a MyBatis mapper - typically a List or a single record
* @param mapperMethod MyBatis3 Mapper Method to perfomr the select
* @param selectList the column list to select
* @return the partially created query
*/
@Deprecated
public static <T> QueryExpressionDSL.FromGatherer<MyBatis3SelectModelAdapter<T>> selectDistinctWithMapper(
Function<SelectStatementProvider, T> mapperMethod, BasicColumn...selectList) {
return selectDistinct(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod),
Expand Down
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,13 +20,17 @@

import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateCompleter;

/**
* This adapter will render the underlying update model for MyBatis3, and then call a MyBatis mapper method.
*
* @deprecated in favor of {@link MyBatis3UpdateCompleter}. This class will be removed without direct
* replacement in a future version.
* @author Jeff Butler
*
*/
@Deprecated
public class MyBatis3UpdateModelAdapter<R> {

private UpdateModel updateModel;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java
Expand Up @@ -20,6 +20,7 @@
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;
Expand All @@ -37,6 +38,8 @@
import org.mybatis.dynamic.sql.util.StringConstantMapping;
import org.mybatis.dynamic.sql.util.UpdateMapping;
import org.mybatis.dynamic.sql.util.ValueMapping;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3UpdateCompleter;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;

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

/**
* Executes an update using a MyBatis3 mapper method.
*
* @deprecated in favor of {@link MyBatis3Utils#update(ToIntFunction, SqlTable, MyBatis3UpdateCompleter)}. This
* method will be removed without direct replacement in a future version.
* @param <T> return value from an update method - typically Integer
* @param mapperMethod MyBatis3 mapper method that performs the update
* @param table table to update
* @return number of records updated - typically as Integer
*/
@Deprecated
public static <T> UpdateDSL<MyBatis3UpdateModelAdapter<T>> updateWithMapper(
Function<UpdateStatementProvider, T> mapperMethod, SqlTable table) {
return update(updateModel -> MyBatis3UpdateModelAdapter.of(updateModel, mapperMethod), table);
Expand Down
Expand Up @@ -17,7 +17,6 @@

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
Expand All @@ -32,7 +31,7 @@
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.CompletableQuery;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectDSL;
Expand All @@ -57,27 +56,27 @@ public static long count(ToLongFunction<SelectStatementProvider> mapper,

public static long count(ToLongFunction<SelectStatementProvider> mapper,
QueryExpressionDSL<SelectModel> start, MyBatis3SelectCompleter completer) {
return mapper.applyAsLong(completer.apply(start).build().render(RenderingStrategy.MYBATIS3));
return mapper.applyAsLong(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
}

public static int deleteFrom(ToIntFunction<DeleteStatementProvider> mapper,
SqlTable table, MyBatis3DeleteCompleter completer) {
return mapper.applyAsInt(
completer.apply(DeleteDSL.deleteFrom(table))
.build()
.render(RenderingStrategy.MYBATIS3));
.render(RenderingStrategies.MYBATIS3));
}

public static <R> int insert(ToIntFunction<InsertStatementProvider<R>> mapper, R record,
SqlTable table, UnaryOperator<InsertDSL<R>> completer) {
return mapper.applyAsInt(completer.apply(
InsertDSL.insert(record).into(table)).build().render(RenderingStrategy.MYBATIS3));
InsertDSL.insert(record).into(table)).build().render(RenderingStrategies.MYBATIS3));
}

public static <R> int insertMultiple(ToIntFunction<MultiRowInsertStatementProvider<R>> mapper,
Collection<R> records, SqlTable table, UnaryOperator<MultiRowInsertDSL<R>> completer) {
return mapper.applyAsInt(completer.apply(
MultiRowInsertDSL.insert(records).into(table)).build().render(RenderingStrategy.MYBATIS3));
MultiRowInsertDSL.insert(records).into(table)).build().render(RenderingStrategies.MYBATIS3));
}

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

public static <R> List<R> selectDistinct(Function<SelectStatementProvider, List<R>> mapper,
CompletableQuery<SelectModel> start, MyBatis3SelectCompleter completer) {
return mapper.apply(completer.apply(start).build().render(RenderingStrategy.MYBATIS3));
return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
}

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

public static <R> List<R> selectList(Function<SelectStatementProvider, List<R>> mapper,
CompletableQuery<SelectModel> start, MyBatis3SelectCompleter completer) {
return mapper.apply(completer.apply(start).build().render(RenderingStrategy.MYBATIS3));
return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
}

public static <R> Optional<R> selectOne(Function<SelectStatementProvider, Optional<R>> mapper,
public static <R> R selectOne(Function<SelectStatementProvider, R> mapper,
BasicColumn[] selectList, SqlTable table, MyBatis3SelectCompleter completer) {
return selectOne(mapper, SelectDSL.select(selectList).from(table), completer);
}

public static <R> Optional<R> selectOne(Function<SelectStatementProvider, Optional<R>> mapper,
public static <R> R selectOne(Function<SelectStatementProvider, R> mapper,
CompletableQuery<SelectModel> start,
MyBatis3SelectCompleter completer) {
return mapper.apply(completer.apply(start).build().render(RenderingStrategy.MYBATIS3));
return mapper.apply(completer.apply(start).build().render(RenderingStrategies.MYBATIS3));
}

public static int update(ToIntFunction<UpdateStatementProvider> mapper,
SqlTable table, MyBatis3UpdateCompleter completer) {
return mapper.applyAsInt(
completer.apply(UpdateDSL.update(table))
.build()
.render(RenderingStrategy.MYBATIS3));
.render(RenderingStrategies.MYBATIS3));
}
}

0 comments on commit bdec2d7

Please sign in to comment.