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

net.dongliu.dbutils.SQLInfo Maven / Gradle / Ivy

package net.dongliu.dbutils;

import net.dongliu.commons.exception.Exceptions;
import net.dongliu.dbutils.handlers.ArrayRowProcessor;
import net.dongliu.dbutils.handlers.BeanRowProcessor;
import net.dongliu.dbutils.handlers.MapRowProcessor;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.stream.Stream;

/**
 * For hold sql execute infos
 *
 * @author Liu Dong
 */
public class SQLInfo {

    private static final SqlExecutor sqlExecutor = new SqlExecutor();

    private Connection connection;
    private boolean closeConn;
    private String clause;
    private Object[] params;
    private int fetchSize;

    SQLInfo() {
    }

    /**
     * Executes the SQL query and return result as stream.
     * You need consume all data in stream, or close stream manually
     */
    public  Stream queryAsStream(RowProcessor processor) {
        try {
            return sqlExecutor.queryAsStream(connection, closeConn, fetchSize, clause, processor, params);
        } catch (SQLException e) {
            throw Exceptions.sneakyThrow(e);
        }
    }

    /**
     * Query result as Object array stream, with column index as array index
     */
    public Stream queryAsArrayStream() {
        return queryAsStream(ArrayRowProcessor.getInstance());
    }

    /**
     * Query result as map stream, with column name as key
     */
    public Stream> queryAsMapStream() {
        return queryAsStream(new MapRowProcessor());
    }

    /**
     * Query result as bean stream
     */
    public  Stream queryAsBeanStream(Class cls) {
        return queryAsStream(new BeanRowProcessor<>(cls));
    }

    /**
     * Query result as bean stream
     */
    public  Stream queryAsBeanStream(Class cls, Map nameMap) {
        return queryAsStream(new BeanRowProcessor<>(cls, nameMap));
    }

    /**
     * Executes the SQL query and returns result.
     */
    public ResultSetHolder query() {
        return rsh -> sqlExecutor.query(connection, closeConn, fetchSize, clause, rsh, params);
    }

    /**
     * Executes the given INSERT, UPDATE, or DELETE SQL statement.
     *
     * @return The number of rows updated.
     */
    public int update() {
        try {
            return sqlExecutor.update(connection, closeConn, clause, params);
        } catch (SQLException e) {
            throw Exceptions.sneakyThrow(e);
        }
    }

    /**
     * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.
     *
     * @return The number of rows updated per statement.
     */
    public int[] updateBatch() {
        try {
            return sqlExecutor.updateBatch(connection, closeConn, clause, (Object[][]) params);
        } catch (SQLException e) {
            throw Exceptions.sneakyThrow(e);
        }
    }

    /**
     * Executes the given INSERT, UPDATE, or DELETE SQL statement.
     *
     * @param keyColumns the column to retrieve after insert, new String[]{"id"} for example.
     *                   will try retrieve auto generated columns if not specified
     * @return The key column result set of inserted row
     */
    public ResultSetHolder insert(String... keyColumns) {
        return rsh -> sqlExecutor.insert(keyColumns, connection, closeConn, clause, rsh, params);
    }

    /**
     * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.
     *
     * @param keyColumns the column to retrieve after insert, new String[]{"id"} for example.
     * @return The key column result set of inserted rows
     */
    public ResultSetHolder insertBatch(String... keyColumns) {
        return rsh -> sqlExecutor.insertBatch(keyColumns, connection, closeConn, clause, rsh,
                (Object[][]) params);
    }

    /**
     * Executes the given INSERT, UPDATE, or DELETE SQL statement.
     *
     * @return The key column result set of inserted row. will try retrieve auto generated columns
     */
    public ResultSetHolder insert() {
        return rsh -> sqlExecutor.insert(null, connection, closeConn, clause, rsh, params);
    }

    /**
     * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.
     *
     * @return The key column result set of inserted row. will try retrieve auto generated columns
     */
    public ResultSetHolder insertBatch() {
        return rsh -> sqlExecutor.insertBatch(null, connection, closeConn, clause, rsh,
                (Object[][]) params);
    }


    SQLInfo connection(Connection connection) {
        this.connection = connection;
        return this;
    }

    SQLInfo closeConn(boolean closeConn) {
        this.closeConn = closeConn;
        return this;
    }

    SQLInfo clause(String clause) {
        this.clause = clause;
        return this;
    }

    /**
     * Set params
     */
    public SQLInfo params(Object[] params) {
        this.params = params;
        return this;
    }

    /**
     * This param works for query method.
     * 

* Gives the JDBC driver a hint as to the number of rows that should * be fetched from the database when more rows are needed for * ResultSet objects generated by this Statement. * If the value specified is zero, then the hint is ignored. * The default value is zero. *

*

* if setFetchSize(10) is being called and the driver is ignoring it, there are probably only two options: * 1. Try a different JDBC driver that will honor the fetch-size hint. * 2. Look at driver-specific properties on the Connection (URL and/or property map when creating the Connection instance). *

*/ public SQLInfo fetchSize(int fetchSize) { this.fetchSize = fetchSize; return this; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy