All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mybatis.dynamic.sql.select.CountDSLCompleter Maven / Gradle / Ivy

/**
 *    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.select;

import java.util.function.Function;
import java.util.function.ToLongFunction;

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. 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.
 *
 * 

This function is intended to by used in conjunction with a utility method like * {@link MyBatis3Utils#countFrom(ToLongFunction, CountDSL, CountDSLCompleter)} * *

For example, you can create mapper interface methods like this: * *

 * @SelectProvider(type=SqlProviderAdapter.class, method="select")
 * long count(SelectStatementProvider selectStatement);
 *
 * default long count(CountDSLCompleter completer) {
 *     return MyBatis3Utils.count(this::count, person, completer);
 * } 
 * 
* *

And then call the simplified default method like this: * *

 * long rows = mapper.count(c ->
 *         c.where(occupation, isNull()));
 * 
* *

You can implement a "count all" with the following code: * *

 * long rows = mapper.count(c -> c);
 * 
* *

Or * *

 * long rows = mapper.count(CountDSLCompleter.allRows());
 * 
* * @author Jeff Butler */ @FunctionalInterface public interface CountDSLCompleter extends Function, Buildable> { /** * Returns a completer that can be used to count every row in a table. * * @return the completer that will count every row in a table */ static CountDSLCompleter allRows() { return c -> c; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy