Search in sources :

Example 1 with NullArgument

use of org.jdbi.v3.core.argument.NullArgument 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 NullArgument

use of org.jdbi.v3.core.argument.NullArgument in project providence by morimekta.

the class MessageNamedArgumentFinder method find.

@Nonnull
@Override
@SuppressWarnings("unchecked")
public Optional<Argument> find(String name, StatementContext ctx) {
    if (!prefix.isEmpty()) {
        if (name.startsWith(prefix)) {
            name = name.substring(prefix.length());
        } else {
            return Optional.empty();
        }
    }
    String[] parts = name.split("[.]");
    PMessage leaf = message;
    PMessageDescriptor leafDescriptor = message.descriptor();
    for (int i = 0; i < parts.length - 1; ++i) {
        String part = parts[i];
        PField field = leafDescriptor.findFieldByName(part);
        if (field == null)
            return Optional.empty();
        if (field.getType() != PType.MESSAGE) {
            throw new IllegalArgumentException("");
        }
        leafDescriptor = (PMessageDescriptor) field.getDescriptor();
        if (leaf != null) {
            leaf = (PMessage) leaf.get(field.getId());
        }
    }
    String leafName = parts[parts.length - 1];
    PField field = leafDescriptor.findFieldByName(leafName);
    if (field != null) {
        if (leaf != null) {
            return Optional.of(new MessageFieldArgument(leaf, field, getColumnType(field)));
        }
        return Optional.of(new NullArgument(getColumnType(field)));
    }
    return Optional.empty();
}
Also used : PField(net.morimekta.providence.descriptor.PField) PMessage(net.morimekta.providence.PMessage) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) NullArgument(org.jdbi.v3.core.argument.NullArgument) Nonnull(javax.annotation.Nonnull)

Aggregations

NullArgument (org.jdbi.v3.core.argument.NullArgument)2 Type (java.lang.reflect.Type)1 Types (java.sql.Types)1 Collection (java.util.Collection)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Nonnull (javax.annotation.Nonnull)1 PMessage (net.morimekta.providence.PMessage)1 PField (net.morimekta.providence.descriptor.PField)1 PMessageDescriptor (net.morimekta.providence.descriptor.PMessageDescriptor)1 Argument (org.jdbi.v3.core.argument.Argument)1 ArgumentFactory (org.jdbi.v3.core.argument.ArgumentFactory)1 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)1 GenericTypes (org.jdbi.v3.core.generic.GenericTypes)1