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

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

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

import org.springframework.jdbc.datasource.DataSourceUtils;
import pers.clare.hisql.exception.HiSqlException;
import pers.clare.hisql.function.*;
import pers.clare.hisql.naming.NamingStrategy;
import pers.clare.hisql.page.PaginationMode;
import pers.clare.hisql.support.CommandTypeParser;
import pers.clare.hisql.support.ResultSetConverter;
import pers.clare.hisql.util.ConnectionUtil;

import javax.sql.DataSource;
import java.sql.Connection;

public abstract class SQLBasicService {

    private DataSource dataSource;

    private String xmlRoot;

    private PaginationMode paginationMode;

    private NamingStrategy naming;

    private ResultSetConverter resultSetConverter;

    private CommandTypeParser commandTypeParser;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public String getXmlRoot() {
        return xmlRoot;
    }

    public void setXmlRoot(String xmlRoot) {
        this.xmlRoot = xmlRoot;
    }

    public PaginationMode getPaginationMode() {
        return paginationMode;
    }

    public void setPaginationMode(PaginationMode paginationMode) {
        this.paginationMode = paginationMode;
    }

    public NamingStrategy getNaming() {
        return naming;
    }

    public void setNaming(NamingStrategy naming) {
        this.naming = naming;
    }

    public ResultSetConverter getResultSetConverter() {
        return resultSetConverter;
    }

    public void setResultSetConverter(ResultSetConverter resultSetConverter) {
        this.resultSetConverter = resultSetConverter;
    }

    public CommandTypeParser getCommandTypeParser() {
        return commandTypeParser;
    }

    public void setCommandTypeParser(CommandTypeParser commandTypeParser) {
        this.commandTypeParser = commandTypeParser;
    }

    public Connection getConnection() {
        return DataSourceUtils.getConnection(dataSource);
    }

    public void closeConnection(Connection connection) {
        DataSourceUtils.releaseConnection(connection, dataSource);
    }

    public  R connection(
            ConnectionOnlyCallback callback
    ) {
        Connection connection = null;
        try {
            connection = getConnection();
            return callback.apply(connection);
        } catch (HiSqlException e) {
            throw e;
        } catch (Exception e) {
            throw new HiSqlException(e);
        } finally {
            closeConnection(connection);
        }
    }

    public  R connection(
            String sql
            , Object[] parameters
            , ConnectionCallback callback
    ) {
        Connection connection = null;
        try {
            connection = getConnection();
            return callback.apply(connection, sql, parameters);
        } catch (HiSqlException e) {
            throw e;
        } catch (Exception e) {
            throw new HiSqlException(sql, e);
        } finally {
            closeConnection(connection);
        }
    }

    public  R connection(
            Class keyType
            , String sql
            , Object[] parameters
            , ConnectionKeyTypeCallback callback
    ) {
        Connection connection = null;
        try {
            connection = getConnection();
            return callback.apply(connection, keyType, sql, parameters);
        } catch (HiSqlException e) {
            throw e;
        } catch (Exception e) {
            throw new HiSqlException(sql, e);
        } finally {
            closeConnection(connection);
        }
    }

    public  R prepared(
            String sql
            , PreparedStatementCallback callback
    ) {
        Connection connection = null;
        try {
            connection = getConnection();
            return callback.apply(connection.prepareStatement(sql));
        } catch (HiSqlException e) {
            throw e;
        } catch (Exception e) {
            throw new HiSqlException(sql, e);
        } finally {
            closeConnection(connection);
        }
    }

    public  R query(
            String sql
            , Object[] parameters
            , ResultSetCallback resultSetCallback
    ) {
        Connection connection = null;
        try {
            connection = getConnection();
            return resultSetCallback.apply(ConnectionUtil.query(connection, sql, parameters));
        } catch (HiSqlException e) {
            throw e;
        } catch (Exception e) {
            throw new HiSqlException(sql, e);
        } finally {
            closeConnection(connection);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy