Search in sources :

Example 11 with ConfigRegistry

use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.

the class UseTemplateEngineImpl method configureForType.

@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
    UseTemplateEngine anno = (UseTemplateEngine) annotation;
    try {
        final TemplateEngine templateEngine = instantiate(anno.value(), sqlObjectType, null);
        registry.get(SqlStatements.class).setTemplateEngine(templateEngine);
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : UseTemplateEngine(org.jdbi.v3.sqlobject.config.UseTemplateEngine) TemplateEngine(org.jdbi.v3.core.statement.TemplateEngine) SqlStatements(org.jdbi.v3.core.statement.SqlStatements) UseTemplateEngine(org.jdbi.v3.sqlobject.config.UseTemplateEngine)

Example 12 with ConfigRegistry

use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.

the class ValueColumnImpl method configureForMethod.

@Override
public void configureForMethod(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType, Method method) {
    ValueColumn valueColumn = (ValueColumn) annotation;
    String name = valueColumn.value();
    registry.get(MapEntryMappers.class).setValueColumn(name.isEmpty() ? null : name);
}
Also used : MapEntryMappers(org.jdbi.v3.core.mapper.MapEntryMappers) ValueColumn(org.jdbi.v3.sqlobject.config.ValueColumn)

Example 13 with ConfigRegistry

use of org.jdbi.v3.core.config.ConfigRegistry in project jdbi by jdbi.

the class SqlObjectFactory method attach.

/**
 * Create a sql object of the specified type bound to this handle. Any state changes to the handle, or the sql
 * object, such as transaction status, closing it, etc, will apply to both the object and the handle.
 *
 * @param extensionType the type of sql object to create
 * @param handle the Handle instance to attach ths sql object to
 * @return the new sql object bound to this handle
 */
@Override
public <E> E attach(Class<E> extensionType, HandleSupplier handle) {
    Map<Method, Handler> handlers = methodHandlersFor(extensionType, handle.getConfig(Handlers.class), handle.getConfig(HandlerDecorators.class));
    ConfigRegistry instanceConfig = handle.getConfig().createCopy();
    for (Class<?> iface : extensionType.getInterfaces()) {
        forEachConfigurer(iface, (configurer, annotation) -> configurer.configureForType(instanceConfig, annotation, extensionType));
    }
    forEachConfigurer(extensionType, (configurer, annotation) -> configurer.configureForType(instanceConfig, annotation, extensionType));
    InvocationHandler invocationHandler = createInvocationHandler(extensionType, instanceConfig, handlers, handle);
    return extensionType.cast(Proxy.newProxyInstance(extensionType.getClassLoader(), new Class[] { extensionType }, invocationHandler));
}
Also used : ConfigRegistry(org.jdbi.v3.core.config.ConfigRegistry) InvocationHandler(java.lang.reflect.InvocationHandler) ExtensionMethod(org.jdbi.v3.core.extension.ExtensionMethod) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 14 with ConfigRegistry

use of org.jdbi.v3.core.config.ConfigRegistry 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();
}
Also used : GenericTypes.getErasedType(org.jdbi.v3.core.generic.GenericTypes.getErasedType) Type(java.lang.reflect.Type) Optional(java.util.Optional)

Example 15 with ConfigRegistry

use of org.jdbi.v3.core.config.ConfigRegistry 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)

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