@Override public void close() { closed = true; context.close(); }
@Override public void close() { ctx.close(); } };
/** * Result producer that eagerly executes the statement, returning the update count * * @return update count * @see PreparedStatement#getUpdateCount() */ public static ResultProducer<Integer> returningUpdateCount() { return (statementSupplier, ctx) -> { try { return statementSupplier.get().getUpdateCount(); } finally { ctx.close(); } }; }
@Override public void close() { getContext().close(); }
/** * Reduce the results. Using a {@code ResultSetAccumulator}, repeatedly * combine query results until only a single value remains. * * @param <U> the accumulator type * @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 reduceResultSet(U seed, ResultSetAccumulator<U> accumulator) { return scanResultSet((supplier, ctx) -> { try (ResultSet rs = supplier.get()) { U result = seed; while (rs.next()) { result = accumulator.apply(result, rs, ctx); } return result; } catch (SQLException e) { throw new UnableToProduceResultException(e, ctx); } finally { ctx.close(); } }); }
/** * 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 RowView(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(); } }); }
/** * Returns a ResultIterable backed by the given result set supplier, mapper, and context. * * @param supplier result set supplier * @param mapper row mapper * @param ctx statement context * @param <T> the mapped type * @return the result iterable */ static <T> ResultIterable<T> of(Supplier<ResultSet> supplier, RowMapper<T> mapper, StatementContext ctx) { return () -> { try { return new ResultSetResultIterator<>(supplier.get(), mapper, ctx); } catch (SQLException e) { try { ctx.close(); } catch (Exception e1) { e.addSuppressed(e1); } throw new ResultSetException("Unable to iterator result set", e, ctx); } }; }
/** * 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 RowView(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(); } }); }
/** * 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 RowView(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(); } }); }
@Override public void close() { closed = true; context.close(); }
/** * Result producer that eagerly executes the statement, returning the update count * * @return update count * @see PreparedStatement#getUpdateCount() */ public static ResultProducer<Integer> returningUpdateCount() { return (statementSupplier, ctx) -> { try { return statementSupplier.get().getUpdateCount(); } finally { ctx.close(); } }; }
@Override public void close() { getContext().close(); } }
/** * 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 mapResultSet((supplier, ctx) -> { try (ResultSet rs = supplier.get()) { RowView rv = new RowView(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(); } }); }
/** * Reduce the results. Using a {@code ResultSetAccumulator}, repeatedly * combine query results until only a single value remains. * * @param <U> the accumulator type * @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 reduceResultSet(U seed, ResultSetAccumulator<U> accumulator) { return mapResultSet((supplier, ctx) -> { try (ResultSet rs = supplier.get()) { U result = seed; while (rs.next()) { result = accumulator.apply(result, rs, ctx); } return result; } catch (SQLException e) { throw new UnableToProduceResultException(e, ctx); } finally { ctx.close(); } }); }
/** * Returns a ResultIterable backed by the given result set supplier, mapper, and context. * * @param supplier result set supplier * @param mapper row mapper * @param ctx statement context * @param <T> the mapped type * @return the result iterable */ static <T> ResultIterable<T> of(Supplier<ResultSet> supplier, RowMapper<T> mapper, StatementContext ctx) { return () -> { try { return new ResultSetResultIterator<>(supplier.get(), mapper, ctx); } catch (SQLException e) { try { ctx.close(); } catch (Exception e1) { e.addSuppressed(e1); } throw new ResultSetException("Unable to iterator result set", e, ctx); } }; }