use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.
the class SqlArrayMapperFactory method build.
@Override
@SuppressWarnings("unchecked")
public Optional<ColumnMapper<?>> build(Type type, ConfigRegistry config) {
final Class<?> erasedType = GenericTypes.getErasedType(type);
if (erasedType.isArray()) {
Class<?> elementType = erasedType.getComponentType();
return elementTypeMapper(elementType, config).map(elementMapper -> new ArrayColumnMapper(elementMapper, elementType));
}
JdbiCollectors collectorRegistry = config.get(JdbiCollectors.class);
return collectorRegistry.findFor(type).flatMap(collector -> collectorRegistry.findElementTypeFor(type).flatMap(elementType -> elementTypeMapper(elementType, config)).map(elementMapper -> new CollectorColumnMapper(elementMapper, collector)));
}
use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.
the class StatementContextTest method testMapperForDelegatesToRegistry.
@Test
public void testMapperForDelegatesToRegistry() {
ColumnMapper<Foo> mapper = new FooMapper();
ConfigRegistry config = new ConfigRegistry();
config.get(ColumnMappers.class).register(mapper);
final StatementContext context = StatementContextAccess.createContext(config);
assertThat(context.findColumnMapperFor(Foo.class)).contains(mapper);
}
use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.
the class ConstantHandleSupplier method invokeInContext.
@Override
public <V> V invokeInContext(ExtensionMethod extensionMethod, ConfigRegistry config, Callable<V> task) throws Exception {
ExtensionMethod oldExtensionMethod = handle.getExtensionMethod();
try {
handle.setExtensionMethod(extensionMethod);
ConfigRegistry oldConfig = handle.getConfig();
try {
handle.setConfig(config);
return task.call();
} finally {
handle.setConfig(oldConfig);
}
} finally {
handle.setExtensionMethod(oldExtensionMethod);
}
}
use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.
the class DurationArgumentFactory method build.
@Override
public Argument build(Duration duration, ConfigRegistry config) {
final boolean isNegative = duration.isNegative();
if (isNegative) {
duration = duration.negated();
}
final long days = duration.toDays();
if (days > Integer.MAX_VALUE) {
throw new IllegalArgumentException(String.format("duration %s too large to be represented unambiguously as postgres interval", duration));
}
duration = duration.minusDays(days);
final int hours = (int) duration.toHours();
duration = duration.minusHours(hours);
final int minutes = (int) duration.toMinutes();
duration = duration.minusMinutes(minutes);
if (duration.getNano() % 1000 != 0) {
throw new IllegalArgumentException(String.format("duration %s too precise to represented as postgres interval", duration));
}
double seconds = duration.getSeconds() + duration.getNano() / 1e9;
final PGInterval interval = new PGInterval(0, 0, (int) days, hours, minutes, seconds);
if (isNegative) {
interval.scale(-1);
}
return (i, p, cx) -> p.setObject(i, interval, Types.OTHER);
}
use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.
the class RegisterColumnMapperFactoriesImpl method configureForType.
@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
Configurer delegate = new RegisterColumnMapperFactoryImpl();
RegisterColumnMapperFactories registerColumnMapperFactories = (RegisterColumnMapperFactories) annotation;
Stream.of(registerColumnMapperFactories.value()).forEach(anno -> delegate.configureForType(registry, anno, sqlObjectType));
}
Aggregations