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

pers.clare.hisql.service.SQLStoreQueryService Maven / Gradle / Ivy

The newest version!
package pers.clare.hisql.service;

import pers.clare.hisql.exception.HiSqlException;
import pers.clare.hisql.function.StoreResultSetHandler;
import pers.clare.hisql.page.Sort;
import pers.clare.hisql.store.SQLCrudStore;
import pers.clare.hisql.store.SQLStore;
import pers.clare.hisql.util.ConnectionUtil;
import pers.clare.hisql.util.ResultSetUtil;
import pers.clare.hisql.util.SQLQueryUtil;

import java.sql.Connection;
import java.util.List;
import java.util.Set;


public abstract class SQLStoreQueryService extends SQLService {

    private  R queryHandler(
            SQLStore sqlStore
            , String sql
            , Sort sort
            , Object[] parameters
            , StoreResultSetHandler storeResultSetHandler
    ) {
        sql = buildSortSQL(sort, sql);
        Connection connection = null;
        try {
            connection = getConnection();
            return storeResultSetHandler.apply(ConnectionUtil.query(connection, sql, parameters), sqlStore);
        } catch (HiSqlException e) {
            throw e;
        } catch (Exception e) {
            throw new HiSqlException(sql, e);
        } finally {
            closeConnection(connection);
        }
    }

    public  T find(
            SQLCrudStore sqlStore
            , T entity
    ) {
        String sql = SQLQueryUtil.setValue(sqlStore.getSelectById(), sqlStore.getKeyFields(), entity);
        return queryHandler(sqlStore, sql, null, null, ResultSetUtil::toInstance);
    }

    public  T find(
            SQLStore sqlStore
            , String sql
            , Object... parameters
    ) {
        return queryHandler(sqlStore, sql, null, parameters, ResultSetUtil::toInstance);
    }

    public  T find(
            SQLStore sqlStore
            , String sql
            , Sort sort
            , Object... parameters
    ) {
        return queryHandler(sqlStore, sql, sort, parameters, ResultSetUtil::toInstance);
    }

    public  Set findSet(
            SQLStore sqlStore
            , String sql
            , Object... parameters
    ) {
        return queryHandler(sqlStore, sql, null, parameters, ResultSetUtil::toSetInstance);
    }

    public  Set findSet(
            SQLStore sqlStore
            , String sql
            , Sort sort
            , Object... parameters
    ) {
        return queryHandler(sqlStore, sql, sort, parameters, ResultSetUtil::toSetInstance);
    }

    public  List findAll(
            SQLStore sqlStore
            , String sql
            , Object... parameters
    ) {
        return queryHandler(sqlStore, sql, null, parameters, ResultSetUtil::toInstances);
    }

    public  List findAll(
            SQLStore sqlStore
            , String sql
            , Sort sort
            , Object... parameters
    ) {
        return queryHandler(sqlStore, sql, sort, parameters, ResultSetUtil::toInstances);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy