Search in sources :

Example 1 with GenericTypes.getErasedType

use of org.jdbi.v3.core.generic.GenericTypes.getErasedType in project jdbi by jdbi.

the class SqlArrayArgumentFactory method build.

@Override
public Optional<Argument> build(Type type, Object value, ConfigRegistry config) {
    Class<?> erasedType = GenericTypes.getErasedType(type);
    if (!(erasedType.isArray() || Collection.class.isAssignableFrom(erasedType))) {
        return Optional.empty();
    }
    if (value == null) {
        return Optional.of(new NullArgument(Types.ARRAY));
    }
    Function<Type, Optional<SqlArrayType<?>>> lookup = eT -> config.get(SqlArrayTypes.class).findFor(eT);
    if (erasedType.isArray()) {
        Class<?> elementType = erasedType.getComponentType();
        return lookup.apply(elementType).map(arrayType -> new SqlArrayArgument<>(arrayType, value));
    }
    return GenericTypes.findGenericParameter(type, Collection.class).flatMap(lookup).map(arrayType -> new SqlArrayArgument<>(arrayType, value));
}
Also used : Type(java.lang.reflect.Type) Collection(java.util.Collection) Argument(org.jdbi.v3.core.argument.Argument) ArgumentFactory(org.jdbi.v3.core.argument.ArgumentFactory) NullArgument(org.jdbi.v3.core.argument.NullArgument) Optional(java.util.Optional) GenericTypes(org.jdbi.v3.core.generic.GenericTypes) Function(java.util.function.Function) ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) Types(java.sql.Types) Type(java.lang.reflect.Type) Optional(java.util.Optional) Collection(java.util.Collection) NullArgument(org.jdbi.v3.core.argument.NullArgument)

Example 2 with GenericTypes.getErasedType

use of org.jdbi.v3.core.generic.GenericTypes.getErasedType 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)

Aggregations

Type (java.lang.reflect.Type)2 Optional (java.util.Optional)2 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)2 GenericTypes (org.jdbi.v3.core.generic.GenericTypes)2 Types (java.sql.Types)1 Collection (java.util.Collection)1 Function (java.util.function.Function)1 Argument (org.jdbi.v3.core.argument.Argument)1 ArgumentFactory (org.jdbi.v3.core.argument.ArgumentFactory)1 NullArgument (org.jdbi.v3.core.argument.NullArgument)1 JdbiCollectors (org.jdbi.v3.core.collector.JdbiCollectors)1 ColumnMapper (org.jdbi.v3.core.mapper.ColumnMapper)1 ColumnMapperFactory (org.jdbi.v3.core.mapper.ColumnMapperFactory)1 ColumnMappers (org.jdbi.v3.core.mapper.ColumnMappers)1