Search in sources :

Example 1 with RowViewImpl

use of org.jdbi.v3.core.result.internal.RowViewImpl in project jdbi by jdbi.

the class ResultBearing method collectRows.

/**
 * Collect the results using the given collector. Do not attempt to accumulate the
 * {@link RowView} objects into the result--they are only valid within the
 * {@link Collector#accumulator()} function. Instead, extract mapped types from the
 * RowView by calling {@code RowView.getRow()} or {@code RowView.getColumn()}.
 *
 * @param collector the collector to collect the result rows.
 * @param <A> the mutable accumulator type used by the collector.
 * @param <R> the result type returned by the collector.
 * @return the result of the collection
 */
default <A, R> R collectRows(Collector<RowView, A, R> collector) {
    return scanResultSet((supplier, ctx) -> {
        try (ResultSet rs = supplier.get()) {
            RowView rv = new RowViewImpl(rs, ctx);
            A acc = collector.supplier().get();
            BiConsumer<A, RowView> consumer = collector.accumulator();
            while (rs.next()) {
                consumer.accept(acc, rv);
            }
            return collector.finisher().apply(acc);
        } catch (SQLException e) {
            throw new UnableToProduceResultException(e, ctx);
        } finally {
            ctx.close();
        }
    });
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) RowViewImpl(org.jdbi.v3.core.result.internal.RowViewImpl)

Example 2 with RowViewImpl

use of org.jdbi.v3.core.result.internal.RowViewImpl in project jdbi by jdbi.

the class ResultBearing method reduceRows.

/**
 * Reduce the result rows using the given row reducer.
 *
 * @param reducer the row reducer.
 * @param <C> Mutable result container type
 * @param <R> Result element type
 * @return the stream of result elements
 * @see RowReducer
 */
default <C, R> Stream<R> reduceRows(RowReducer<C, R> reducer) {
    return scanResultSet((supplier, ctx) -> {
        try (ResultSet rs = supplier.get()) {
            RowView rowView = new RowViewImpl(rs, ctx);
            C container = reducer.container();
            while (rs.next()) {
                reducer.accumulate(container, rowView);
            }
            return reducer.stream(container);
        } catch (SQLException e) {
            throw new UnableToProduceResultException(e, ctx);
        } finally {
            ctx.close();
        }
    });
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) RowViewImpl(org.jdbi.v3.core.result.internal.RowViewImpl)

Example 3 with RowViewImpl

use of org.jdbi.v3.core.result.internal.RowViewImpl in project jdbi by jdbi.

the class ResultBearing method reduceRows.

/**
 * Reduce the results.  Using a {@code BiFunction<U, RowView, U>}, repeatedly
 * combine query results until only a single value remains.
 *
 * @param <U>         the type of the accumulator
 * @param seed        the {@code U} to combine with the first result
 * @param accumulator the function to apply repeatedly
 * @return the final {@code U}
 */
default <U> U reduceRows(U seed, BiFunction<U, RowView, U> accumulator) {
    return scanResultSet((supplier, ctx) -> {
        try (ResultSet rs = supplier.get()) {
            RowView rv = new RowViewImpl(rs, ctx);
            U result = seed;
            while (rs.next()) {
                result = accumulator.apply(result, rv);
            }
            return result;
        } catch (SQLException e) {
            throw new UnableToProduceResultException(e, ctx);
        } finally {
            ctx.close();
        }
    });
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) RowViewImpl(org.jdbi.v3.core.result.internal.RowViewImpl)

Aggregations

ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 RowViewImpl (org.jdbi.v3.core.result.internal.RowViewImpl)3