Skip to content

Commit

Permalink
Merge pull request #128 from jeffgbutler/master
Browse files Browse the repository at this point in the history
Improved Utilities for use by MyBatis Generator
  • Loading branch information
jeffgbutler committed Aug 18, 2019
2 parents 88eaaff + 1e6b73e commit 61fd0cd
Show file tree
Hide file tree
Showing 14 changed files with 378 additions and 563 deletions.
Expand Up @@ -21,20 +21,16 @@
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.MyBatis3SelectListHelper;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3SelectOneHelper;
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 the {@link MyBatis3SelectListHelper} and {@link MyBatis3SelectOneHelper} classes.
* primarily to support {@link MyBatis3SelectCompleter}.
*
* @author Jeff Butler
*
* @param <R> the model type created by these operations
*
* @see MyBatis3SelectListHelper
* @see MyBatis3SelectOneHelper
*
*/
public interface CompletableQuery<R> extends Buildable<R> {
QueryExpressionDSL<R>.QueryExpressionWhereBuilder where();
Expand Down
Expand Up @@ -16,33 +16,37 @@
package org.mybatis.dynamic.sql.util.mybatis3;

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;

/**
* 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().execute() methods - making client code look a bit cleaner.
* 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>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(MyBatis3DeleteHelper helper) {
* return helper.apply(DeleteDSL.deleteFromWithMapper(this::delete, simpleTable))
* .build()
* .execute();
* default int delete(MyBatis3DeleteCompleter completer) {
* return MyBatis3Utils.deleteFrom(this::delete, person, completer);
* }
* </pre>
*
* <p>And then call the simplified default method like this:
*
* <pre>
* int rows = mapper.delete(q -&gt;
* q.where(occupation, isNull()));
* int rows = mapper.delete(c -&gt;
* c.where(occupation, isNull()));
* </pre>
*
* <p>You can implement a "delete all" with the following code:
Expand All @@ -54,21 +58,21 @@
* <p>Or
*
* <pre>
* long rows = mapper.delete(MyBatis3DeleteHelper.allRows());
* long rows = mapper.delete(MyBatis3DeleteCompleter.allRows());
* </pre>
* @author Jeff Butler
*/
@FunctionalInterface
public interface MyBatis3DeleteHelper extends
Function<DeleteDSL<MyBatis3DeleteModelToIntAdapter>, Buildable<MyBatis3DeleteModelToIntAdapter>> {
public interface MyBatis3DeleteCompleter extends
Function<DeleteDSL<DeleteModel>, Buildable<DeleteModel>> {

/**
* Returns a helper that can be used to delete every row in a table.
*
* @return the helper that will delete every row in a table
*/
static MyBatis3DeleteHelper allRows() {
static MyBatis3DeleteCompleter allRows() {
return h -> h;
}
}

This file was deleted.

Expand Up @@ -17,40 +17,41 @@

import java.util.function.Function;

import org.mybatis.dynamic.sql.select.MyBatis3SelectModelAdapter;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
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;

/**
* 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().execute() methods - making client code look a bit cleaner.
* 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>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(MyBatis3CountHelper helper) {
* return helper.apply(SelectDSL.selectWithMapper(this::count, SqlBuilder.count())
* .from(simpleTable))
* .build()
* .execute();
* default long count(MyBatis3SelectCompleter completer) {
return MyBatis3Utils.count(this::count, person, completer);
* }
* </pre>
*
* <p>And then call the simplified default method like this:
*
* <pre>
* long rows = mapper.count(q -&gt;
* q.where(occupation, isNull()));
* long rows = mapper.count(c -&gt;
* c.where(occupation, isNull()));
* </pre>
*
* <p>You can implement a "count all" with the following code:
*
* <pre>
* long rows = mapper.count(q -&gt; q);
* long rows = mapper.count(c -&gt; c);
* </pre>
*
* <p>Or
Expand All @@ -62,15 +63,19 @@
* @author Jeff Butler
*/
@FunctionalInterface
public interface MyBatis3CountHelper extends
Function<QueryExpressionDSL<MyBatis3SelectModelAdapter<Long>>, Buildable<MyBatis3SelectModelAdapter<Long>>> {
public interface MyBatis3SelectCompleter extends
Function<CompletableQuery<SelectModel>, Buildable<SelectModel>> {

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

static MyBatis3SelectCompleter allRowsOrderedBy(SortSpecification...columns) {
return h -> h.orderBy(columns);
}
}

This file was deleted.

This file was deleted.

0 comments on commit 61fd0cd

Please sign in to comment.