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));
}
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();
}
});
}
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();
}
});
}
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();
}
});
}
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);
}
}
Aggregations