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

com.gitee.qdbp.jdbc.biz.BaseQueryerImpl Maven / Gradle / Ivy


package com.gitee.qdbp.jdbc.biz;

import java.util.List;
import java.util.Map;
import com.gitee.qdbp.able.beans.KeyValue;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.condition.DbWhere;
import com.gitee.qdbp.able.jdbc.fields.Fields;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.ordering.Orderings;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.jdbc.paging.PartList;
import com.gitee.qdbp.jdbc.api.SqlBufferJdbcOperations;
import com.gitee.qdbp.jdbc.exception.DbErrorCode;
import com.gitee.qdbp.jdbc.model.FieldScene;
import com.gitee.qdbp.jdbc.model.SimpleFieldColumn;
import com.gitee.qdbp.jdbc.plugins.DbPluginHelper;
import com.gitee.qdbp.jdbc.plugins.EntityFieldFillExecutor;
import com.gitee.qdbp.jdbc.plugins.SqlDialect;
import com.gitee.qdbp.jdbc.result.KeyIntegerMapper;
import com.gitee.qdbp.jdbc.result.RowToBeanMapper;
import com.gitee.qdbp.jdbc.result.SingleColumnMapper;
import com.gitee.qdbp.jdbc.sql.SqlBuffer;
import com.gitee.qdbp.jdbc.sql.build.QuerySqlHelper;
import com.gitee.qdbp.jdbc.utils.PagingQuery;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 基础表查询操作
 *
 * @author 赵卉华
 * @version 190608
 */
public abstract class BaseQueryerImpl {

    protected RowToBeanMapper rowToBeanMapper;
    protected QuerySqlHelper sqlHelper;
    protected EntityFieldFillExecutor entityFieldFillExecutor;
    protected SqlBufferJdbcOperations jdbc;
    protected SqlDialect dialect;

    /**
     * 构造函数
     * 
     * @param sqlHelper SQL生成工具
     * @param entityFieldFillExecutor 实体业务处理接口
     * @param jdbcOperations SqlBuffer数据库操作类
     * @param rowToBeanMapper 结果转换接口
     */
    public BaseQueryerImpl(QuerySqlHelper sqlHelper, EntityFieldFillExecutor entityFieldFillExecutor,
            SqlBufferJdbcOperations jdbcOperations, RowToBeanMapper rowToBeanMapper) {
        this.rowToBeanMapper = rowToBeanMapper;
        this.sqlHelper = sqlHelper;
        this.entityFieldFillExecutor = entityFieldFillExecutor;
        this.jdbc = jdbcOperations;
        this.dialect = jdbcOperations.sqlDialect();
    }

    /**
     * 获取SQL方言处理类
     * 
     * @return SQL方言处理类
     */
    public SqlDialect sqlDialect() {
        return this.dialect;
    }

    /**
     * 获取插件容器
     * 
     * @return 插件容器
     */
    public DbPluginHelper plugins() {
        return this.jdbc.plugins();
    }

    /**
     * 获取主表别名(用于表关联查询)
     * 
     * @return 主表别名
     */
    protected String getMajorTableAlias() {
        return null;
    }

    public T find(DbWhere where) {
        return find(Fields.ALL, where);
    }

    public T find(Fields fields, DbWhere where) {
        VerifyTools.requireNotBlank(where, "where");
        entityFieldFillExecutor.fillQueryWhereDataState(where, getMajorTableAlias());
        entityFieldFillExecutor.fillQueryWhereParams(where, getMajorTableAlias());
        SqlBuffer buffer = sqlHelper.buildFindSql(fields, where);
        return jdbc.queryForObject(buffer, rowToBeanMapper);
    }

    public List listAll() {
        return listAll(Fields.ALL, Orderings.NONE);
    }

    public List listAll(Fields fields) {
        return listAll(Fields.ALL, Orderings.NONE);
    }

    public List listAll(Orderings orderings) {
        return this.listAll(Fields.ALL, orderings);
    }

    public List listAll(Fields fields, Orderings orderings) {
        DbWhere where = new DbWhere();
        entityFieldFillExecutor.fillQueryWhereDataState(where, getMajorTableAlias());
        entityFieldFillExecutor.fillQueryWhereParams(where, getMajorTableAlias());
        return this.doList(fields, where, orderings);
    }

    public PageList list(DbWhere where, OrderPaging odpg) {
        return list(Fields.ALL, where, odpg);
    }

    public PageList list(Fields fields, DbWhere where, OrderPaging odpg) {
        // DbWhere readyWhere = checkWhere(where); // 带分页查询列表, 允许条件为空, 因此不检查
        DbWhere readyWhere = where;
        if (where == null || where == DbWhere.NONE) {
            readyWhere = new DbWhere();
        }
        entityFieldFillExecutor.fillQueryWhereDataState(readyWhere, getMajorTableAlias());
        entityFieldFillExecutor.fillQueryWhereParams(readyWhere, getMajorTableAlias());
        return this.doList(fields, readyWhere, odpg);
    }

    public List list(DbWhere where, Orderings orderings) throws ServiceException {
        return list(Fields.ALL, where, orderings);
    }

    public List list(Fields fields, DbWhere where, Orderings orderings) throws ServiceException {
        DbWhere readyWhere = checkWhere(where);
        entityFieldFillExecutor.fillQueryWhereDataState(readyWhere, getMajorTableAlias());
        entityFieldFillExecutor.fillQueryWhereParams(readyWhere, getMajorTableAlias());
        return this.doList(fields, readyWhere, orderings);
    }

    protected List doList(Fields fields, DbWhere where, Orderings orderings) {
        SqlBuffer buffer = sqlHelper.buildListSql(fields, where, orderings);
        return jdbc.query(buffer, rowToBeanMapper);
    }

    protected PageList doList(Fields fields, DbWhere where, OrderPaging odpg) {
        SqlBuffer wsb = sqlHelper.fragment().buildWhereSql(where, true);
        SqlBuffer qsb = sqlHelper.buildListSql(fields, wsb, odpg.getOrderings());
        SqlBuffer csb = null;
        if (odpg.isPaging() && odpg.isNeedCount()) {
            csb = sqlHelper.buildCountSql(wsb);
        }

        PartList list = PagingQuery.queryForList(jdbc, qsb, csb, odpg, rowToBeanMapper);
        return list == null ? null : new PageList<>(list, list.getTotal());
    }

    public  V findFieldValue(String fieldName, DbWhere where, Class valueClazz) throws ServiceException {
        DbWhere readyWhere = checkWhere(where);
        entityFieldFillExecutor.fillQueryWhereDataState(readyWhere, getMajorTableAlias());
        entityFieldFillExecutor.fillQueryWhereParams(readyWhere, getMajorTableAlias());
        PageList list = doListFieldValues(fieldName, false, readyWhere, OrderPaging.NONE, valueClazz);
        return VerifyTools.isBlank(list) ? null : list.get(0);
    }

    public  List listFieldValues(String fieldName, boolean distinct, DbWhere where, Orderings orderings,
            Class valueClazz) throws ServiceException {
        DbWhere readyWhere = checkWhere(where);
        entityFieldFillExecutor.fillQueryWhereDataState(readyWhere, getMajorTableAlias());
        entityFieldFillExecutor.fillQueryWhereParams(readyWhere, getMajorTableAlias());
        PageList result = doListFieldValues(fieldName, distinct, readyWhere, OrderPaging.of(orderings), valueClazz);
        return result == null ? null : result.toList();
    }

    public  PageList listFieldValues(String fieldName, boolean distinct, DbWhere where, OrderPaging odpg,
            Class valueClazz) throws ServiceException {
        DbWhere readyWhere = checkWhere(where);
        entityFieldFillExecutor.fillQueryWhereDataState(readyWhere, getMajorTableAlias());
        entityFieldFillExecutor.fillQueryWhereParams(readyWhere, getMajorTableAlias());
        return doListFieldValues(fieldName, distinct, readyWhere, odpg, valueClazz);
    }

    protected  PageList doListFieldValues(String fieldName, boolean distinct, DbWhere where, OrderPaging odpg,
            Class valueClazz) throws ServiceException {
        SqlBuffer wsb = sqlHelper.fragment().buildWhereSql(where, true);
        SqlBuffer qsb = sqlHelper.buildListFieldValuesSql(fieldName, distinct, wsb, odpg.getOrderings());
        SqlBuffer csb = null;
        if (odpg.isPaging() && odpg.isNeedCount()) {
            csb = sqlHelper.buildCountSql(wsb);
        }
        // 这里不能用getColumnName(), 否则JoinQueryer调用时会返回u.USER_NAME这种带表别名的列名, 导致取不到数据
        SimpleFieldColumn columnInfo = sqlHelper.fragment().getColumnInfo(FieldScene.CONDITION, fieldName);
        String columnName = columnInfo.getColumnName();
        SingleColumnMapper rowMapper = new SingleColumnMapper<>(columnName, valueClazz);
        rowMapper.setPlugins(jdbc.plugins());
        PartList list = PagingQuery.queryForList(jdbc, qsb, csb, odpg, rowMapper);
        return list == null ? null : new PageList<>(list, list.getTotal());
    }

    public int count(DbWhere where) throws ServiceException {
        DbWhere readyWhere = checkWhere(where);
        entityFieldFillExecutor.fillQueryWhereDataState(readyWhere, getMajorTableAlias());
        entityFieldFillExecutor.fillQueryWhereParams(readyWhere, getMajorTableAlias());
        return doCount(readyWhere);
    }

    protected int doCount(DbWhere readyWhere) throws ServiceException {
        SqlBuffer buffer = sqlHelper.buildCountSql(readyWhere);
        return jdbc.queryForObject(buffer, Integer.class);
    }

    public Map groupCount(String groupBy, DbWhere where) throws ServiceException {
        VerifyTools.requireNotBlank(groupBy, "groupBy");
        DbWhere readyWhere = checkWhere(where);
        entityFieldFillExecutor.fillQueryWhereDataState(readyWhere, getMajorTableAlias());
        entityFieldFillExecutor.fillQueryWhereParams(readyWhere, getMajorTableAlias());
        return this.doGroupCount(groupBy, readyWhere);
    }

    protected Map doGroupCount(String groupBy, DbWhere readyWhere) throws ServiceException {
        SqlBuffer buffer = sqlHelper.buildGroupCountSql(groupBy, readyWhere);
        List> list = jdbc.query(buffer, KEY_INTEGER_MAPPER);
        return KeyValue.toMap(list);
    }

    protected static KeyIntegerMapper KEY_INTEGER_MAPPER = new KeyIntegerMapper();

    protected DbWhere checkWhere(DbWhere where) {
        if (where == null || where == DbWhere.NONE) {
            return new DbWhere();
        } else if (where.isEmpty() && !where.isEmptiable()) {
            String details = "If you want to find all records, please use DbWhere.NONE";
            throw new ServiceException(DbErrorCode.DB_WHERE_MUST_NOT_BE_EMPTY).setDetails(details);
        } else {
            return where;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy