use of org.jdbi.v3.core.internal.exceptions.Unchecked in project jdbi by jdbi.
the class BuiltInArgumentFactory method build.
@Override
@SuppressWarnings("unchecked")
public Optional<Argument> build(Type expectedType, Object value, ConfigRegistry config) {
Class<?> expectedClass = getErasedType(expectedType);
if (value != null && expectedClass == Object.class) {
expectedClass = value.getClass();
}
@SuppressWarnings("rawtypes") ArgBuilder v = BUILDERS.get(expectedClass);
if (v != null) {
return Optional.of(v.build(value));
}
// Enums must be bound as VARCHAR.
if (value instanceof Enum) {
Enum<?> enumValue = (Enum<?>) value;
return Optional.of(STR_BUILDER.build(enumValue.name()));
}
if (value instanceof Optional) {
Object nestedValue = ((Optional<?>) value).orElse(null);
Type nestedType = findOptionalType(expectedType, nestedValue);
return config.get(Arguments.class).findFor(nestedType, nestedValue);
}
return value == null ? Optional.of(config.get(Arguments.class).getUntypedNullArgument()) : Optional.empty();
}
use of org.jdbi.v3.core.internal.exceptions.Unchecked in project jdbi by jdbi.
the class TestGuavaCollectors method testMapCollector.
@SuppressWarnings("unchecked")
private <M extends Map<Long, String>> void testMapCollector(Class<? extends Map> erasedType, GenericType<M> genericType) {
JdbiCollectors registry = h2Extension.getJdbi().getConfig(JdbiCollectors.class);
assertThat(registry.findElementTypeFor(genericType.getType())).contains(new GenericType<Map.Entry<Long, String>>() {
}.getType());
Collector<Map.Entry<Long, String>, ?, M> collector = (Collector<Map.Entry<Long, String>, ?, M>) registry.findFor(genericType.getType()).orElseThrow(() -> new IllegalStateException("Missing collector for " + genericType));
M map = Stream.of(entry(1L, "foo"), entry(2L, "bar")).collect(collector);
assertThat(map).isInstanceOf(erasedType).containsExactly(entry(1L, "foo"), entry(2L, "bar"));
}
use of org.jdbi.v3.core.internal.exceptions.Unchecked in project jdbi by jdbi.
the class TestRegisteredMappers method registerByGenericType.
@Test
public void registerByGenericType() {
Jdbi db = h2Extension.getJdbi();
@SuppressWarnings("unchecked") RowMapper<Iterable<Calendar>> mapper = mock(RowMapper.class);
GenericType<Iterable<Calendar>> iterableOfCalendarType = new GenericType<Iterable<Calendar>>() {
};
db.registerRowMapper(iterableOfCalendarType, mapper);
assertThat(db.getConfig(RowMappers.class).findFor(iterableOfCalendarType)).contains(mapper);
}
use of org.jdbi.v3.core.internal.exceptions.Unchecked in project jdbi by jdbi.
the class PojoMapper method specialize0.
private Optional<RowMapper<T>> specialize0(StatementContext ctx, List<String> columnNames, List<ColumnNameMatcher> columnNameMatchers, List<String> unmatchedColumns) {
final List<PropertyData<T>> propList = new ArrayList<>();
for (PojoProperty<T> property : getProperties(ctx.getConfig()).getProperties().values()) {
Nested anno = property.getAnnotation(Nested.class).orElse(null);
if (property.getAnnotation(Unmappable.class).map(Unmappable::value).orElse(false)) {
continue;
}
if (anno == null) {
String paramName = prefix + getName(property);
findColumnIndex(paramName, columnNames, columnNameMatchers, () -> debugName(property)).ifPresent(index -> {
@SuppressWarnings({ "unchecked", "rawtypes" }) ColumnMapper<?> mapper = ctx.findColumnMapperFor(property.getQualifiedType().mapType(GenericTypes::box)).orElseGet(() -> (ColumnMapper) defaultColumnMapper(property));
propList.add(new PropertyData<>(property, new SingleColumnMapper<>(mapper, index + 1)));
unmatchedColumns.remove(columnNames.get(index));
});
} else {
String nestedPrefix = prefix + anno.value();
if (anyColumnsStartWithPrefix(columnNames, nestedPrefix, columnNameMatchers)) {
nestedMappers.computeIfAbsent(property, d -> createNestedMapper(ctx, d, nestedPrefix)).specialize0(ctx, columnNames, columnNameMatchers, unmatchedColumns).ifPresent(nestedMapper -> propList.add(new PropertyData<>(property, nestedMapper)));
}
}
}
if (propList.isEmpty() && !columnNames.isEmpty()) {
return Optional.empty();
}
propList.sort(Comparator.comparing(p -> p.propagateNull ? 1 : 0));
final Optional<String> nullMarkerColumn = Optional.ofNullable(GenericTypes.getErasedType(type).getAnnotation(PropagateNull.class)).map(PropagateNull::value);
return Optional.of((r, c) -> {
if (propagateNull(r, nullMarkerColumn)) {
return null;
}
final PojoBuilder<T> pojo = getProperties(c.getConfig()).create();
for (PropertyData<T> p : propList) {
Object value = p.mapper.map(r, ctx);
if (p.propagateNull && (value == null || (p.isPrimitive && r.wasNull()))) {
return null;
}
if (value != null) {
pojo.set(p.property, value);
}
}
return pojo.build();
});
}
use of org.jdbi.v3.core.internal.exceptions.Unchecked in project jdbi by jdbi.
the class ResultBearing method collectInto.
/**
* Collect the results into a container of the given type. A collector
* must be registered for the container type, which knows the element type
* for the container. A mapper must be registered for the element type.
* <p>
* This method is equivalent to {@code ResultBearing.mapTo(elementType).collect(containerCollector)}.
* </p>
*
* @param containerType the container type into which results will be collected
* @return a container into which result rows have been collected
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
default Object collectInto(Type containerType) {
return scanResultSet((rs, ctx) -> {
Collector collector = ctx.findCollectorFor(containerType).orElseThrow(() -> new NoSuchCollectorException("No collector registered for container type " + containerType));
Type elementType = ctx.findElementTypeFor(containerType).orElseThrow(() -> new ElementTypeNotFoundException("Unknown element type for container type " + containerType));
RowMapper<?> mapper = ctx.findMapperFor(elementType).orElseThrow(() -> new NoSuchMapperException("No mapper registered for element type " + elementType));
return ResultIterable.of(rs, mapper, ctx).collect(collector);
});
}
Aggregations