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

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

There is a newer version: 6.0.2
Show newest version
package net.dongliu.dbutils;

import net.dongliu.commons.annotation.Nullable;
import net.dongliu.dbutils.exception.TooManyResultException;
import net.dongliu.dbutils.mapper.BeanRowMapper;
import net.dongliu.dbutils.mapper.RecordRowMapper;
import net.dongliu.dbutils.mapper.RowMapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Stream;

import static java.util.Objects.requireNonNull;

/**
 * Class for holding sql execute contexts which returns a resultSet.
 *
 * @author Liu Dong
 */
public abstract class QueryContext extends AbstractQueryContext {

    QueryContext() {
    }

    /**
     * Set RowMapper to convert the ResultSet
     */
    public  TypedQueryContext map(RowMapper rowMapper) {
        return new TypedQueryContext(requireNonNull(rowMapper)) {
            @Override
            protected PreparedStatement prepare(int fetchSize, String[] keyColumns, Connection connection)
                    throws SQLException {
                return QueryContext.this.prepare(fetchSize, keyColumns, connection);
            }

            @Override
            protected ResultSet execute(int fetchSize, PreparedStatement statement) throws SQLException {
                return QueryContext.this.execute(fetchSize, statement);
            }

            @Override
            protected MyConnection retrieveConnection() throws SQLException {
                return QueryContext.this.retrieveConnection();
            }
        };
    }

    /**
     * Set a bean lass to convert the ResultSet to.
     */
    public  TypedQueryContext map(Class beanClass) {
        return map(beanClass, true);
    }

    /**
     * Set a bean lass to convert the ResultSet to.
     *
     * @param abortWhenMissingProperty if true: when bean do not have property to hold column, throw exception
     */
    public  TypedQueryContext map(Class beanClass, boolean abortWhenMissingProperty) {
        return map(BeanRowMapper.getInstance(beanClass, abortWhenMissingProperty));
    }

    /**
     * Return query result as List of Record
     */
    public List getList() {
        return convertToList(RecordRowMapper.getInstance());
    }

    /**
     * Return query result as one Record
     *
     * @return null if no record returned.
     * @throws TooManyResultException if has more than one result
     * @deprecated using {@link #get()}
     */
    @Deprecated
    @Nullable
    public Record getOne() throws TooManyResultException {
        return get();
    }

    /**
     * Return query result as one Record
     *
     * @return null if no record returned.
     * @throws TooManyResultException if has more than one result
     */
    @Nullable
    public Record get() throws TooManyResultException {
        return convertTo(RecordRowMapper.getInstance());
    }

    /**
     * Get value of column as int.
     *
     * @param index start from 0
     * @throws TooManyResultException if has more than one result
     */
    @Nullable
    public Integer getInt(int index) {
        return map((row, rs) -> rs.getInt(index)).get();
    }

    /**
     * Get value of column as int.
     *
     * @param name the column name
     * @return null if value is null or name not exists
     * @throws TooManyResultException if has more than one result
     */
    @Nullable
    public Integer getInt(String name) {
        return map((row, rs) -> rs.getInt(name)).get();
    }

    /**
     * Get values of column as int list.
     *
     * @param index start from 0
     * @throws TooManyResultException if has more than one result
     */
    public List<@Nullable Integer> getIntList(int index) {
        return map((row, rs) -> rs.getInt(index)).getList();
    }

    /**
     * Get values of column as int list.
     *
     * @param name the column name
     * @return null if value is null or name not exists
     * @throws TooManyResultException if has more than one result
     */
    public List<@Nullable Integer> getIntList(String name) {
        return map((row, rs) -> rs.getInt(name)).getList();
    }

    /**
     * Get value of column as long.
     *
     * @param index start from 0
     * @throws TooManyResultException if has more than one result
     */
    @Nullable
    public Long getLong(int index) {
        return map((row, rs) -> rs.getLong(index)).get();
    }

    /**
     * Get value of column as long.
     *
     * @param name the column name
     * @return null if value is null or name not exists
     * @throws TooManyResultException if has more than one result
     */
    @Nullable
    public Long getLong(String name) {
        return map((row, rs) -> rs.getLong(name)).get();
    }


    /**
     * Get values of column as long list.
     *
     * @param index start from 0
     * @throws TooManyResultException if has more than one result
     */
    public List<@Nullable Long> getLongList(int index) {
        return map((row, rs) -> rs.getLong(index)).getList();
    }

    /**
     * Get values of column as int list.
     *
     * @param name the column name
     * @return null if value is null or name not exists
     * @throws TooManyResultException if has more than one result
     */
    public List<@Nullable Long> getLongList(String name) {
        return map((row, rs) -> rs.getLong(name)).getList();
    }

    /**
     * Get value of column as String.
     *
     * @param index start from 0
     * @throws TooManyResultException if has more than one result
     */
    @Nullable
    public String getString(int index) {
        return map((row, rs) -> rs.getString(index)).get();
    }

    /**
     * Get value of column as String.
     *
     * @param name the column name
     * @throws TooManyResultException if has more than one result
     */
    @Nullable
    public String getString(String name) {
        return map((row, rs) -> rs.getString(name)).get();
    }

    /**
     * Get values of column as String list.
     *
     * @param index start from 0
     * @throws TooManyResultException if has more than one result
     */
    public List<@Nullable String> getStringList(int index) {
        return map((row, rs) -> rs.getString(index)).getList();
    }

    /**
     * Get values of column as String list.
     *
     * @param name the column name
     * @return null if value is null or name not exists
     * @throws TooManyResultException if has more than one result
     */
    public List<@Nullable String> getStringList(String name) {
        return map((row, rs) -> rs.getString(name)).getList();
    }

    /**
     * Wrap ResultSet as Stream.
     * Need to close this stream if not consumed.
     */
    public Stream asStream() {
        return asStream(RecordRowMapper.getInstance());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy