Search in sources :

Example 16 with ConfigRegistry

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)));
}
Also used : JdbiCollectors(org.jdbi.v3.core.collector.JdbiCollectors) Type(java.lang.reflect.Type) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) ColumnMapper(org.jdbi.v3.core.mapper.ColumnMapper) Optional(java.util.Optional) GenericTypes(org.jdbi.v3.core.generic.GenericTypes) ColumnMapperFactory(org.jdbi.v3.core.mapper.ColumnMapperFactory) ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) JdbiCollectors(org.jdbi.v3.core.collector.JdbiCollectors)

Example 17 with ConfigRegistry

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);
}
Also used : ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) ColumnMappers(org.jdbi.v3.core.mapper.ColumnMappers) Test(org.junit.Test)

Example 18 with ConfigRegistry

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);
    }
}
Also used : ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) ExtensionMethod(org.jdbi.v3.core.extension.ExtensionMethod)

Example 19 with ConfigRegistry

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);
}
Also used : PGInterval(org.postgresql.util.PGInterval) AbstractArgumentFactory(org.jdbi.v3.core.argument.AbstractArgumentFactory) Duration(java.time.Duration) Argument(org.jdbi.v3.core.argument.Argument) ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) Types(java.sql.Types) PGInterval(org.postgresql.util.PGInterval)

Example 20 with ConfigRegistry

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));
}
Also used : RegisterColumnMapperFactories(org.jdbi.v3.sqlobject.config.RegisterColumnMapperFactories) Configurer(org.jdbi.v3.sqlobject.config.Configurer)

Aggregations

Configurer (org.jdbi.v3.sqlobject.config.Configurer)10 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)8 RowMappers (org.jdbi.v3.core.mapper.RowMappers)5 SqlStatements (org.jdbi.v3.core.statement.SqlStatements)5 ColumnMappers (org.jdbi.v3.core.mapper.ColumnMappers)4 Type (java.lang.reflect.Type)3 Optional (java.util.Optional)3 ExtensionMethod (org.jdbi.v3.core.extension.ExtensionMethod)3 TemplateEngine (org.jdbi.v3.core.statement.TemplateEngine)3 Method (java.lang.reflect.Method)2 Types (java.sql.Types)2 Argument (org.jdbi.v3.core.argument.Argument)2 Arguments (org.jdbi.v3.core.argument.Arguments)2 JdbiCollectors (org.jdbi.v3.core.collector.JdbiCollectors)2 GenericTypes (org.jdbi.v3.core.generic.GenericTypes)2 ColumnMapperFactory (org.jdbi.v3.core.mapper.ColumnMapperFactory)2 MapEntryMappers (org.jdbi.v3.core.mapper.MapEntryMappers)2 SqlParser (org.jdbi.v3.core.statement.SqlParser)2 UseSqlParser (org.jdbi.v3.sqlobject.config.UseSqlParser)2 UseTemplateEngine (org.jdbi.v3.sqlobject.config.UseTemplateEngine)2