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

org.jfaster.badger.sql.select.JdbcSelectHelper Maven / Gradle / Ivy

The newest version!
package org.jfaster.badger.sql.select;

import java.util.List;

import org.jfaster.badger.Badger;
import org.jfaster.badger.dialect.Dialect;
import org.jfaster.badger.jdbc.extractor.support.ListResultSetExtractor;
import org.jfaster.badger.jdbc.extractor.support.ObjectResultSetExtractor;
import org.jfaster.badger.jdbc.mapper.RowMapplerRegistry;
import org.jfaster.badger.query.shard.ShardResult;
import org.jfaster.badger.spi.ExtensionLoader;
import org.jfaster.badger.sql.JdbcHelper;
import org.jfaster.badger.util.CheckConditions;
import org.jfaster.badger.util.ManualShardUtils;
import org.jfaster.badger.util.ShardUtils;

/**
 * 查询辅助类
 * @author yanpengfang
 * create 2019-01-27 1:54 PM
 */
public class JdbcSelectHelper {
    /**
     * 查询所有列列表
     * @param clazz
     * @param condition
     * @param paramList
     * @param badger
     * @param useMaster
     * @param 
     * @return
     */
    public static  List find(Class clazz, String condition, List paramList, Badger badger, boolean useMaster) {
        return find(clazz, "*", condition, paramList, badger, useMaster);
    }

    public static  List find(Class clazz, String condition, List paramList,
            Class returnType, Badger badger, boolean useMaster) {
        return find(clazz, "*", condition, paramList, returnType, badger, useMaster);
    }

    /**
     * 查询指定列列表
     * @param clazz
     * @param columns
     * @param condition
     * @param paramList
     * @param badger
     * @param useMaster
     * @param 
     * @return
     */
    public static  List find(Class clazz, String columns, String condition, List paramList, Badger badger, boolean useMaster) {
        ShardResult shardResult = ShardUtils.shard(clazz, condition, paramList, badger);
        return execFind(clazz, columns, condition, paramList, shardResult, badger, useMaster);
    }

    public static  List find(Class clazz, String columns, String condition,
            List paramList, Class returnType, Badger badger, boolean useMaster) {
        ShardResult shardResult = ShardUtils.shard(clazz, condition, paramList, badger);
        return execFind(clazz, columns, condition, paramList, shardResult, returnType, badger, useMaster);
    }

    /**
     * 查询所有列列表
     * @param clazz
     * @param condition
     * @param paramList
     * @param badger
     * @param useMaster
     * @param 
     * @return
     */
    public static  List find(Class clazz, String condition, List paramList,
            Object shardValue, Badger badger, boolean useMaster) {
        return find(clazz, "*", condition, paramList, shardValue, badger, useMaster);
    }

    public static  List find(Class clazz, String condition, List paramList,
            Object shardValue, Class returnType, Badger badger, boolean useMaster) {
        return find(clazz, "*", condition, paramList, shardValue, returnType, badger, useMaster);
    }

    /**
     * 查询指定列列表
     * @param clazz
     * @param columns
     * @param condition
     * @param paramList
     * @param shardValue
     * @param badger
     * @param useMaster
     * @param 
     * @return
     */
    public static  List find(Class clazz, String columns, String condition,
            List paramList, Object shardValue, Badger badger, boolean useMaster) {
        ShardResult shardResult = ManualShardUtils.shard(clazz, condition, paramList, shardValue, badger);
        return execFind(clazz, columns, condition, paramList, shardResult, badger, useMaster);
    }

    public static  List find(Class clazz, String columns, String condition,
            List paramList, Object shardValue, Class returnType, Badger badger, boolean useMaster) {
        ShardResult shardResult = ManualShardUtils.shard(clazz, condition, paramList, shardValue, badger);
        return execFind(clazz, columns, condition, paramList, shardResult, returnType, badger, useMaster);
    }

    private static  List execFind(Class clazz, String columns, String condition,
            List paramList, ShardResult shardResult, Badger badger, boolean useMaster) {
        String sql = getSql(clazz, columns, condition, shardResult.getTableName(), badger);
        List dynamicFields = shardResult.getDynamicFields();
        String dbName = shardResult.getDataSourceName();
        return JdbcHelper.executeQuery(badger, clazz, dynamicFields, dbName,
                paramList, sql,
                new ListResultSetExtractor<>(RowMapplerRegistry.getRowMapper(clazz)), useMaster, true);
    }

    private static  List execFind(Class clazz, String columns, String condition,
            List paramList, ShardResult shardResult, Class returnType, Badger badger, boolean useMaster) {
        String sql = getSql(clazz, columns, condition, shardResult.getTableName(), badger);
        List dynamicFields = shardResult.getDynamicFields();
        String dbName = shardResult.getDataSourceName();
        return JdbcHelper.executeQuery(badger, clazz, dynamicFields, dbName,
                paramList, sql,
                new ListResultSetExtractor<>(RowMapplerRegistry.getRowMapper(returnType)), useMaster, true);
    }

    private static  String getSql(Class clazz, String columns, String condition,
            String tableName, Badger badger) {
        CheckConditions.checkNotNull(columns, "查询列不能为空");
        CheckConditions.checkNotNull(condition, "查询条件不能为空");
        StringBuilder sql = new StringBuilder();
        Dialect dialect = ExtensionLoader.get(Dialect.class).getExtension(badger.getDialect());
        if (tableName == null) {
            sql.append(("*".equals(columns.trim())) ? (dialect.selectAllSql(clazz)) : (dialect.selectSql(clazz, columns)));
        } else {
            sql.append(("*".equals(columns.trim())) ? (dialect.selectAllSql(clazz, tableName)) : (dialect.selectSql(clazz, columns, tableName)));
        }
        sql.append(" WHERE ").append(condition);
        return sql.toString();
    }


    /**
     * 分页查询指定列
     * @param clazz
     * @param condition
     * @param paramList
     * @param pageIndex
     * @param pageSize
     * @param badger
     * @param useMaster
     * @param 
     * @return
     */
    public static  List findByPage(Class clazz, String condition,
            List paramList, int pageIndex, int pageSize, Badger badger, boolean useMaster) {
        return findByPage(clazz, "*", condition, paramList, pageIndex, pageSize, badger, useMaster);
    }

    public static  List findByPage(Class clazz, String condition,
            List paramList, int pageIndex, int pageSize, Class returnType,
            Badger badger, boolean useMaster) {
        return findByPage(clazz, "*", condition, paramList, pageIndex, pageSize, returnType, badger, useMaster);
    }

    /**
     * 分页查询指定列
     * @param clazz
     * @param columns
     * @param condition
     * @param paramList
     * @param pageIndex
     * @param pageSize
     * @param badger
     * @param useMaster
     * @param 
     * @return
     */
    public static  List findByPage(Class clazz, String columns, String condition,
            List paramList, int pageIndex, int pageSize, Badger badger, boolean useMaster) {
        ShardResult shardResult = ShardUtils.shard(clazz, condition, paramList, badger);
        return execFindByPage(clazz, columns, condition, paramList, pageIndex, pageSize, shardResult, badger, useMaster);
    }

    public static  List findByPage(Class clazz, String columns, String condition,
            List paramList, int pageIndex, int pageSize, Class returnType,
            Badger badger, boolean useMaster) {
        ShardResult shardResult = ShardUtils.shard(clazz, condition, paramList, badger);
        return execFindByPage(clazz, columns, condition, paramList, pageIndex, pageSize,
                shardResult, returnType, badger, useMaster);
    }

    /**
     * 分页查询指定列
     * @param clazz
     * @param condition
     * @param paramList
     * @param pageIndex
     * @param pageSize
     * @param badger
     * @param useMaster
     * @param 
     * @return
     */
    public static  List findByPage(Class clazz, String condition,
            List paramList, int pageIndex, int pageSize,
            Object shardValue, Badger badger, boolean useMaster) {
        return findByPage(clazz, "*", condition, paramList, pageIndex, pageSize, shardValue, badger, useMaster);
    }

    public static  List findByPage(Class clazz, String condition,
            List paramList, int pageIndex, int pageSize,
            Object shardValue, Class returnType, Badger badger, boolean useMaster) {
        return findByPage(clazz, "*", condition, paramList, pageIndex, pageSize, shardValue, returnType, badger, useMaster);
    }


    /**
     * 分页查询指定列
     * @param clazz
     * @param columns
     * @param condition
     * @param paramList
     * @param pageIndex
     * @param pageSize
     * @param shardValue
     * @param badger
     * @param useMaster
     * @param 
     * @return
     */
    public static  List findByPage(Class clazz, String columns, String condition,
            List paramList, int pageIndex, int pageSize,
            Object shardValue, Badger badger, boolean useMaster) {
        ShardResult shardResult = ManualShardUtils.shard(clazz, condition, paramList, shardValue, badger);
        return execFindByPage(clazz, columns, condition, paramList, pageIndex, pageSize, shardResult, badger, useMaster);
    }

    public static  List findByPage(Class clazz, String columns, String condition,
            List paramList, int pageIndex, int pageSize,
            Object shardValue, Class returnType, Badger badger, boolean useMaster) {
        ShardResult shardResult = ManualShardUtils.shard(clazz, condition, paramList, shardValue, badger);
        return execFindByPage(clazz, columns, condition, paramList, pageIndex, pageSize,
                shardResult, returnType, badger, useMaster);
    }


    private static  List execFindByPage(Class clazz, String columns, String condition,
            List paramList, int pageIndex, int pageSize,
            ShardResult shardResult, Badger badger, boolean useMaster) {
        String sql = getSql(clazz, columns, condition, shardResult.getTableName(), pageIndex, pageSize, badger);
        List dynamicFields = shardResult.getDynamicFields();
        String dbName = shardResult.getDataSourceName();
        return JdbcHelper.executeQuery(badger, clazz, dynamicFields, dbName,
                paramList, sql,
                new ListResultSetExtractor<>(RowMapplerRegistry.getRowMapper(clazz)), useMaster, true);
    }

    private static  List execFindByPage(Class clazz, String columns, String condition,
            List paramList, int pageIndex, int pageSize,
            ShardResult shardResult, Class returnType, Badger badger, boolean useMaster) {
        String sql = getSql(clazz, columns, condition, shardResult.getTableName(), pageIndex, pageSize, badger);
        List dynamicFields = shardResult.getDynamicFields();
        String dbName = shardResult.getDataSourceName();
        return JdbcHelper.executeQuery(badger, clazz, dynamicFields, dbName,
                paramList, sql,
                new ListResultSetExtractor<>(RowMapplerRegistry.getRowMapper(returnType)), useMaster, true);
    }

    private static  String getSql(Class clazz, String columns, String condition,
            String tableName, int pageIndex, int pageSize, Badger badger) {
        CheckConditions.checkNotNull(columns, "查询列不能为空");
        CheckConditions.checkNotNull(condition, "查询条件不能为空");
        CheckConditions.checkPageSize(pageSize, badger.getPageSizeLimit());
        StringBuilder sql = new StringBuilder();
        Dialect dialect = ExtensionLoader.get(Dialect.class).getExtension(badger.getDialect());
        int start = (pageIndex - 1) * pageSize;
        if (start < 0) {
            start = 0;
        }
        if (tableName == null) {
            sql.append(("*".equals(columns.trim())) ? (dialect.selectAllSql(clazz)) : (dialect.selectSql(clazz, columns)));
        } else {
            sql.append(("*".equals(columns.trim())) ? (dialect.selectAllSql(clazz, tableName)) : (dialect.selectSql(clazz, columns, tableName)));
        }
        sql.append(" WHERE ").append(condition);
        return dialect.getPageSql(sql.toString(), start, pageSize);
    }

    /**
     * 根据条件count
     * @param clazz
     * @param condition
     * @param paramList
     * @param badger
     * @param useMaster
     * @param 
     * @return
     */
    public static  long count(Class clazz, String condition, List paramList, Badger badger, boolean useMaster) {
        ShardResult shardResult = ShardUtils.shard(clazz, condition, paramList, badger);
        return execCount(clazz, condition, paramList, shardResult, badger, useMaster);
    }

    /**
     * 根据条件count
     * @param clazz
     * @param condition
     * @param paramList
     * @param shardValue
     * @param badger
     * @param useMaster
     * @param 
     * @return
     */
    public static  long count(Class clazz, String condition, List paramList, Object shardValue, Badger badger, boolean useMaster) {
        ShardResult shardResult = ManualShardUtils.shard(clazz, condition, paramList, shardValue, badger);
        return execCount(clazz, condition, paramList, shardResult, badger, useMaster);
    }

    private static  long execCount(Class clazz, String condition, List paramList,
            ShardResult shardResult, Badger badger, boolean useMaster) {
        CheckConditions.checkNotNull(condition, "查询条件不能为空");
        Dialect dialect = ExtensionLoader.get(Dialect.class).getExtension(badger.getDialect());
        List dynamicFields = shardResult.getDynamicFields();
        String tableName = shardResult.getTableName();
        String dbName = shardResult.getDataSourceName();
        String sql = tableName == null ? dialect.countSql(clazz, condition) : dialect.countSql(clazz, tableName, condition);
        return JdbcHelper.executeQuery(badger, clazz, dynamicFields, dbName,
                paramList, sql,
                new ObjectResultSetExtractor<>(RowMapplerRegistry.getRowMapper(long.class)), useMaster, true);
    }
}