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

io.github.jinghui70.rainbow.dbaccess.GeneralSql Maven / Gradle / Ivy

There is a newer version: 5.2.11
Show newest version
package io.github.jinghui70.rainbow.dbaccess;

import io.github.jinghui70.rainbow.dbaccess.cnd.Cnd;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.*;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

import static io.github.jinghui70.rainbow.dbaccess.DbaUtil.enumCheck;

/**
 * 封装了一个Sql的内容对象
 *
 * @author lijinghui
 */
@SuppressWarnings("unchecked")
public abstract class GeneralSql> extends SqlWrapper {

    private final List params = new ArrayList<>();

    protected GeneralSql() {
        super();
    }

    protected GeneralSql(String str) {
        super(str);
    }

    protected GeneralSql(Dba dba) {
        super(dba);
    }

    protected JdbcTemplate getJdbcTemplate() {
        return dba.getJdbcTemplate();
    }

    public S from(GeneralSql sql) {
        append(" FROM (");
        append(sql.getSql());
        this.params.addAll(sql.getParams());
        append(")");
        return (S) this;
    }

    public S append(GeneralSql sql) {
        append(sql.getSql());
        this.params.addAll(sql.getParams());
        return (S) this;
    }

    @Override
    public S set(String field, Object value) {
        set();
        append(field).append("=?").addParam(enumCheck(value));
        return (S) this;
    }

    @Override
    public S append(Cnd cnd) {
        cnd.toSql(this);
        return (S) this;
    }

    public List getParams() {
        return params;
    }


    public S addParam(Object... params) {
        Collections.addAll(this.params, params);
        return (S) this;
    }

    public S addParams(Collection params) {
        this.params.addAll(params);
        return (S) this;
    }

    public S setParam(Object... params) {
        this.params.clear();
        return addParam(params);
    }

    public S setParams(Collection params) {
        this.params.clear();
        return addParams(params);
    }

    public boolean noParams() {
        return params.isEmpty();
    }

    /**
     * 执行当前sql
     *
     * @return 执行影响的行数
     */
    @Override
    public int execute() {
        if (noParams())
            return getJdbcTemplate().update(getSql());
        else
            return getJdbcTemplate().update(getSql(), new ArgumentSetter(params));
    }

    public int[] batchUpdate(List batchArgs) {
        Map nullTypeCache = new HashMap<>();
        return getJdbcTemplate().batchUpdate(
                getSql(),
                new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement ps, int i) throws SQLException {
                        Object[] values = batchArgs.get(i);
                        int colIndex = 1;
                        for (Object value : values) {
                            DbaUtil.setParameterValue(ps, colIndex++, value, nullTypeCache);
                        }
                    }

                    @Override
                    public int getBatchSize() {
                        return batchArgs.size();
                    }
                }
        );
    }

    public int[][] batchUpdate(List batchArgs, int batchSize) {
        Map nullTypeCache = new HashMap<>();
        return getJdbcTemplate().batchUpdate(getSql(), batchArgs, batchSize,
                (ps, argument) -> {
                    int colIndex = 1;
                    for (Object value : argument) {
                        DbaUtil.setParameterValue(ps, colIndex++, value, nullTypeCache);
                    }
                });
    }

    @Override
    public void query(RowCallbackHandler rch) {
        if (noParams())
            getJdbcTemplate().query(getSql(), rch);
        else
            getJdbcTemplate().query(getSql(), new ArgumentSetter(params), rch);
    }

    protected  T query(String sql, ResultSetExtractor rse) throws DataAccessException {
        return noParams() ? getJdbcTemplate().query(sql, rse) :
                getJdbcTemplate().query(sql, new ArgumentSetter(params), rse);
    }

    @Override
    public  T queryForObject(String sql, RowMapper mapper) throws DataAccessException {
        ResultSetExtractor> extractor = new RowMapperResultSetExtractor<>(mapper, 1);
        try {
            List results = query(sql, extractor);
            return DataAccessUtils.nullableSingleResult(results);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public  List queryForList(String sql, RowMapper rowMapper) throws DataAccessException {
        return query(sql, new RowMapperResultSetExtractor<>(rowMapper));
    }

}