Skip to content

Refactoring/Code Polishing #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/org/mybatis/dynamic/sql/BasicColumn.java
Original file line number Diff line number Diff line change
@@ -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 Down Expand Up @@ -66,4 +66,15 @@ default String renderWithTableAndColumnAlias(TableAliasCalculator tableAliasCalc
return alias().map(a -> nameAndTableAlias + " as " + a) //$NON-NLS-1$
.orElse(nameAndTableAlias);
}

/**
* Utility method to make it easier to build column lists for methods that require an
* array rather than the varargs method.
*
* @param columns list of BasicColumn
* @return an array of BasicColumn
*/
static BasicColumn[] columnList(BasicColumn...columns) {
return columns;
}
}
3 changes: 1 addition & 2 deletions src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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;

Expand Down Expand Up @@ -81,7 +80,7 @@ public static DeleteDSL<DeleteModel> deleteFrom(SqlTable table) {
/**
* Delete record(s) by executing a MyBatis3 mapper method.
*
* @deprecated in favor of {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, MyBatis3DeleteCompleter)}.
* @deprecated in favor of {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, DeleteDSLCompleter)}.
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util.mybatis3;
package org.mybatis.dynamic.sql.delete;

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

import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.delete.DeleteDSL;
import org.mybatis.dynamic.sql.delete.DeleteModel;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

/**
* Represents a function that can be used to create a general delete method in the style
* of MyBatis Generator. When using this function, you can create a method that does not require a user to
* call the build() and render() methods - making client code look a bit cleaner.
*
* <p>This function is intended to be used in conjunction with the utility function
* {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, MyBatis3DeleteCompleter)}
* <p>This function is intended to be used in conjunction with a utility method like
* {@link MyBatis3Utils#deleteFrom(ToIntFunction, SqlTable, DeleteDSLCompleter)}
*
* <p>For example, you can create mapper interface methods like this:
*
* <pre>
* &#64;DeleteProvider(type=SqlProviderAdapter.class, method="delete")
* int delete(DeleteStatementProvider deleteStatement);
*
* default int delete(MyBatis3DeleteCompleter completer) {
* default int delete(DeleteDSLCompleter completer) {
* return MyBatis3Utils.deleteFrom(this::delete, person, completer);
* }
* </pre>
Expand All @@ -52,27 +51,27 @@
* <p>You can implement a "delete all" with the following code:
*
* <pre>
* int rows = mapper.delete(q -&gt; q);
* int rows = mapper.delete(c -&gt; c);
* </pre>
*
* <p>Or
*
* <pre>
* long rows = mapper.delete(MyBatis3DeleteCompleter.allRows());
* long rows = mapper.delete(DeleteDSLCompleter.allRows());
* </pre>

* @author Jeff Butler
*/
@FunctionalInterface
public interface MyBatis3DeleteCompleter extends
public interface DeleteDSLCompleter extends
Function<DeleteDSL<DeleteModel>, Buildable<DeleteModel>> {

/**
* Returns a helper that can be used to delete every row in a table.
* Returns a completer that can be used to delete every row in a table.
*
* @return the helper that will delete every row in a table
* @return the completer that will delete every row in a table
*/
static MyBatis3DeleteCompleter allRows() {
return h -> h;
static DeleteDSLCompleter allRows() {
return c -> c;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@

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
* @deprecated in favor of {@link DeleteDSLCompleter}. This class will be removed without replacement in a
* future version
*
* @author Jeff Butler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
import org.mybatis.dynamic.sql.SqlCriterion;
import org.mybatis.dynamic.sql.VisitableCondition;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectCompleter;

/**
* This interface describes operations allowed for a select statement after the from and join clauses. This is
* primarily to support {@link MyBatis3SelectCompleter}.
* primarily to support {@link SelectDSLCompleter}.
*
* @author Jeff Butler
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@

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
* @deprecated in favor is {@link SelectDSLCompleter}. This class will be removed without direct replacement
* in a future version
*
* @author Jeff Butler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util.mybatis3;
package org.mybatis.dynamic.sql.select;

import java.util.function.Function;

import org.mybatis.dynamic.sql.SortSpecification;
import org.mybatis.dynamic.sql.select.CompletableQuery;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

/**
* Represents a function that can be used to create a general count method in the style
* of MyBatis Generator. When using this function, you can create a method that does not require a user to
* call the build() and render() methods - making client code look a bit cleaner.
*
* <p>This function is intended to by used in conjunction with several utility methods in
* {@link MyBatis3Utils}
* <p>This function is intended to by used in conjunction with utility methods like the select and count
* methods in {@link MyBatis3Utils}
*
* <p>For example, you can create mapper interface methods like this:
*
* <pre>
* &#64;SelectProvider(type=SqlProviderAdapter.class, method="select")
* long count(SelectStatementProvider selectStatement);
*
* default long count(MyBatis3SelectCompleter completer) {
* default long count(SelectDSLCompleter completer) {
return MyBatis3Utils.count(this::count, person, completer);
* }
* </pre>
Expand All @@ -57,25 +56,31 @@
* <p>Or
*
* <pre>
* long rows = mapper.count(MyBatis3CountHelper.allRows());
* long rows = mapper.count(SelectDSLCompleter.allRows());
* </pre>
*
* @author Jeff Butler
*/
@FunctionalInterface
public interface MyBatis3SelectCompleter extends
public interface SelectDSLCompleter extends
Function<CompletableQuery<SelectModel>, Buildable<SelectModel>> {

/**
* Returns a helper that can be used to count every row in a table.
* Returns a completer that can be used to select every row in a table.
*
* @return the helper that will count every row in a table
* @return the completer that will select every row in a table
*/
static MyBatis3SelectCompleter allRows() {
return h -> h;
static SelectDSLCompleter allRows() {
return c -> c;
}

static MyBatis3SelectCompleter allRowsOrderedBy(SortSpecification...columns) {
return h -> h.orderBy(columns);
/**
* Returns a completer that can be used to select every row in a table with specified order.
*
* @param columns list of sort specifications for an order by clause
* @return the completer that will select every row in a table with specified order
*/
static SelectDSLCompleter allRowsOrderedBy(SortSpecification...columns) {
return c -> c.orderBy(columns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@

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
* @deprecated in favor of {@link UpdateDSLCompleter}. This class will be removed without direct
* replacement in a future version.
* @author Jeff Butler
*
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
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;

Expand Down Expand Up @@ -100,7 +99,7 @@ public static UpdateDSL<UpdateModel> update(SqlTable table) {
/**
* Executes an update using a MyBatis3 mapper method.
*
* @deprecated in favor of {@link MyBatis3Utils#update(ToIntFunction, SqlTable, MyBatis3UpdateCompleter)}. This
* @deprecated in favor of {@link MyBatis3Utils#update(ToIntFunction, SqlTable, UpdateDSLCompleter)}. 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.util.mybatis3;
package org.mybatis.dynamic.sql.update;

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

import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

/**
* Represents a function that can be used to create a general update method in the style
* of MyBatis Generator. When using this function, you can create a method that does not require a user to
* call the build() and render() methods - making client code look a bit cleaner.
*
* <p>This function is intended to be used in conjunction with the utility function
* {@link MyBatis3Utils#update(ToIntFunction, SqlTable, MyBatis3UpdateCompleter)}
* <p>This function is intended to be used in conjunction in the utility method like
* {@link MyBatis3Utils#update(ToIntFunction, SqlTable, UpdateDSLCompleter)}
*
* <p>For example, you can create mapper interface methods like this:
*
* <pre>
* &#64;UpdateProvider(type=SqlProviderAdapter.class, method="update")
* int update(UpdateStatementProvider updateStatement);
*
* default int update(MyBatis3UpdateCompleter completer) {
* default int update(UpdateDSLCompleter completer) {
return MyBatis3Utils.update(this::update, person, completer);
* }
* </pre>
Expand Down Expand Up @@ -88,6 +87,6 @@
* @author Jeff Butler
*/
@FunctionalInterface
public interface MyBatis3UpdateCompleter extends
public interface UpdateDSLCompleter extends
Function<UpdateDSL<UpdateModel>, Buildable<UpdateModel>> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.delete.DeleteDSL;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
Expand All @@ -35,9 +36,11 @@
import org.mybatis.dynamic.sql.select.CompletableQuery;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
import org.mybatis.dynamic.sql.select.SelectDSL;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;

/**
Expand All @@ -50,17 +53,17 @@ public class MyBatis3Utils {
private MyBatis3Utils() {}

public static long count(ToLongFunction<SelectStatementProvider> mapper,
SqlTable table, MyBatis3SelectCompleter completer) {
SqlTable table, SelectDSLCompleter completer) {
return count(mapper, SelectDSL.select(SqlBuilder.count()).from(table), completer);
}

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

public static int deleteFrom(ToIntFunction<DeleteStatementProvider> mapper,
SqlTable table, MyBatis3DeleteCompleter completer) {
SqlTable table, DeleteDSLCompleter completer) {
return mapper.applyAsInt(
completer.apply(DeleteDSL.deleteFrom(table))
.build()
Expand All @@ -80,38 +83,38 @@ public static <R> int insertMultiple(ToIntFunction<MultiRowInsertStatementProvid
}

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

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

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

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

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

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

public static int update(ToIntFunction<UpdateStatementProvider> mapper,
SqlTable table, MyBatis3UpdateCompleter completer) {
SqlTable table, UpdateDSLCompleter completer) {
return mapper.applyAsInt(
completer.apply(UpdateDSL.update(table))
.build()
Expand Down
Loading