Search in sources :

Example 1 with UnableToProduceResultException

use of org.jdbi.v3.core.result.UnableToProduceResultException in project jdbi by jdbi.

the class JsonColumnMapperFactory method build.

@Override
public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) {
    ColumnMappers cm = config.get(ColumnMappers.class);
    // look for specialized json support first, revert to simple String mapping if absent
    ColumnMapper<String> jsonStringMapper = JdbiOptionals.findFirstPresent(() -> cm.findFor(QualifiedType.of(String.class).with(EncodedJson.class)), () -> cm.findFor(String.class)).orElseThrow(() -> new UnableToProduceResultException(JSON_NOT_RETRIEVABLE));
    final JsonMapper mapper = config.get(JsonConfig.class).getJsonMapper();
    return Optional.of((rs, i, ctx) -> mapper.fromJson(type, Optional.ofNullable(jsonStringMapper.map(rs, i, ctx)).orElse(// sql null -> json null
    "null"), config));
}
Also used : JsonMapper(org.jdbi.v3.json.JsonMapper) JsonConfig(org.jdbi.v3.json.JsonConfig) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) UnableToProduceResultException(org.jdbi.v3.core.result.UnableToProduceResultException) EncodedJson(org.jdbi.v3.json.EncodedJson)

Example 2 with UnableToProduceResultException

use of org.jdbi.v3.core.result.UnableToProduceResultException 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 3 with UnableToProduceResultException

use of org.jdbi.v3.core.result.UnableToProduceResultException 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 4 with UnableToProduceResultException

use of org.jdbi.v3.core.result.UnableToProduceResultException 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)

Example 5 with UnableToProduceResultException

use of org.jdbi.v3.core.result.UnableToProduceResultException in project jdbi by jdbi.

the class JacksonJsonMapper method toJson.

@Override
public String toJson(Type type, Object value, ConfigRegistry config) {
    try {
        Jackson2Config cfg = config.get(Jackson2Config.class);
        ObjectWriter writer = cfg.getMapper().writerFor(cfg.getMapper().constructType(type));
        Class<?> view = cfg.getSerializationView();
        if (view != null) {
            writer = writer.withView(view);
        }
        return writer.writeValueAsString(value);
    } catch (JsonProcessingException e) {
        throw new UnableToProduceResultException(e);
    }
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) UnableToProduceResultException(org.jdbi.v3.core.result.UnableToProduceResultException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 UnableToProduceResultException (org.jdbi.v3.core.result.UnableToProduceResultException)3 RowViewImpl (org.jdbi.v3.core.result.internal.RowViewImpl)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 IOException (java.io.IOException)1 ColumnMappers (org.jdbi.v3.core.mapper.ColumnMappers)1 EncodedJson (org.jdbi.v3.json.EncodedJson)1 JsonConfig (org.jdbi.v3.json.JsonConfig)1 JsonMapper (org.jdbi.v3.json.JsonMapper)1