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

org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils Maven / Gradle / Ivy

The newest version!
/*
 *    Copyright 2016-2024 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
 *
 *       https://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.util.mybatis3;

import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.ToIntBiFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.UnaryOperator;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.select.CountDSL;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
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.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;

/**
 * Utility functions for building MyBatis3 mappers.
 *
 * @author Jeff Butler
 */
public class MyBatis3Utils {
    private MyBatis3Utils() {}

    public static long count(ToLongFunction mapper, BasicColumn column, SqlTable table,
            CountDSLCompleter completer) {
        return mapper.applyAsLong(count(column, table, completer));
    }

    public static SelectStatementProvider count(BasicColumn column, SqlTable table, CountDSLCompleter completer) {
        return countFrom(SqlBuilder.countColumn(column).from(table), completer);
    }

    public static long countDistinct(ToLongFunction mapper, BasicColumn column, SqlTable table,
            CountDSLCompleter completer) {
        return mapper.applyAsLong(countDistinct(column, table, completer));
    }

    public static SelectStatementProvider countDistinct(BasicColumn column, SqlTable table,
            CountDSLCompleter completer) {
        return countFrom(SqlBuilder.countDistinctColumn(column).from(table), completer);
    }

    public static SelectStatementProvider countFrom(SqlTable table, CountDSLCompleter completer) {
        return countFrom(SqlBuilder.countFrom(table), completer);
    }

    public static long countFrom(ToLongFunction mapper,
            SqlTable table, CountDSLCompleter completer) {
        return mapper.applyAsLong(countFrom(table, completer));
    }

    public static SelectStatementProvider countFrom(CountDSL start, CountDSLCompleter completer) {
        return completer.apply(start)
                .build()
                .render(RenderingStrategies.MYBATIS3);
    }

    public static long countFrom(ToLongFunction mapper,
            CountDSL start, CountDSLCompleter completer) {
        return mapper.applyAsLong(countFrom(start, completer));
    }

    public static DeleteStatementProvider deleteFrom(SqlTable table, DeleteDSLCompleter completer) {
        return completer.apply(SqlBuilder.deleteFrom(table))
                .build()
                .render(RenderingStrategies.MYBATIS3);
    }

    public static int deleteFrom(ToIntFunction mapper,
            SqlTable table, DeleteDSLCompleter completer) {
        return mapper.applyAsInt(deleteFrom(table, completer));
    }

    public static  InsertStatementProvider insert(R row, SqlTable table,
            UnaryOperator> completer) {
        return completer.apply(SqlBuilder.insert(row).into(table))
                .build()
                .render(RenderingStrategies.MYBATIS3);
    }

    public static  int insert(ToIntFunction> mapper, R row,
            SqlTable table, UnaryOperator> completer) {
        return mapper.applyAsInt(insert(row, table, completer));
    }

    public static GeneralInsertStatementProvider generalInsert(SqlTable table,
            UnaryOperator completer) {
        return completer.apply(GeneralInsertDSL.insertInto(table))
                .build()
                .render(RenderingStrategies.MYBATIS3);
    }

    public static int generalInsert(ToIntFunction mapper,
            SqlTable table, UnaryOperator completer) {
        return mapper.applyAsInt(generalInsert(table, completer));
    }

    public static  MultiRowInsertStatementProvider insertMultiple(Collection records, SqlTable table,
            UnaryOperator> completer) {
        return completer.apply(SqlBuilder.insertMultiple(records).into(table))
                .build()
                .render(RenderingStrategies.MYBATIS3);
    }

    public static  int insertMultiple(ToIntFunction> mapper,
            Collection records, SqlTable table, UnaryOperator> completer) {
        return mapper.applyAsInt(insertMultiple(records, table, completer));
    }

    public static  int insertMultipleWithGeneratedKeys(ToIntBiFunction> mapper,
            Collection records, SqlTable table, UnaryOperator> completer) {
        MultiRowInsertStatementProvider provider = insertMultiple(records, table, completer);
        return mapper.applyAsInt(provider.getInsertStatement(), provider.getRecords());
    }

    public static SelectStatementProvider select(BasicColumn[] selectList, SqlTable table,
            SelectDSLCompleter completer) {
        return select(SqlBuilder.select(selectList).from(table), completer);
    }

    public static SelectStatementProvider select(QueryExpressionDSL start,
            SelectDSLCompleter completer) {
        return completer.apply(start)
                .build()
                .render(RenderingStrategies.MYBATIS3);
    }

    public static SelectStatementProvider selectDistinct(BasicColumn[] selectList, SqlTable table,
            SelectDSLCompleter completer) {
        return select(SqlBuilder.selectDistinct(selectList).from(table), completer);
    }

    public static  List selectDistinct(Function> mapper,
            BasicColumn[] selectList, SqlTable table, SelectDSLCompleter completer) {
        return mapper.apply(selectDistinct(selectList, table, completer));
    }

    public static  List selectList(Function> mapper,
            BasicColumn[] selectList, SqlTable table, SelectDSLCompleter completer) {
        return mapper.apply(select(selectList, table, completer));
    }

    public static  List selectList(Function> mapper,
            QueryExpressionDSL start, SelectDSLCompleter completer) {
        return mapper.apply(select(start, completer));
    }

    public static  R selectOne(Function mapper,
            BasicColumn[] selectList, SqlTable table, SelectDSLCompleter completer) {
        return mapper.apply(select(selectList, table, completer));
    }

    public static  R selectOne(Function mapper,
            QueryExpressionDSL start, SelectDSLCompleter completer) {
        return mapper.apply(select(start, completer));
    }

    public static UpdateStatementProvider update(SqlTable table, UpdateDSLCompleter completer) {
        return completer.apply(SqlBuilder.update(table))
                .build()
                .render(RenderingStrategies.MYBATIS3);
    }

    public static int update(ToIntFunction mapper,
            SqlTable table, UpdateDSLCompleter completer) {
        return mapper.applyAsInt(update(table, completer));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy