Search in sources :

Example 6 with Argument

use of org.jdbi.v3.core.argument.Argument in project jdbi by jdbi.

the class Binding method toString.

@Override
public String toString() {
    boolean wrote = false;
    StringBuilder b = new StringBuilder();
    b.append("{ positional:{");
    for (Map.Entry<Integer, Argument> entry : positionals.entrySet()) {
        wrote = true;
        b.append(entry.getKey()).append(":").append(entry.getValue()).append(",");
    }
    if (wrote) {
        wrote = false;
        b.deleteCharAt(b.length() - 1);
    }
    b.append("}");
    b.append(", named:{");
    for (Map.Entry<String, Argument> entry : named.entrySet()) {
        wrote = true;
        b.append(entry.getKey()).append(":").append(entry.getValue()).append(",");
    }
    if (wrote) {
        wrote = false;
        b.deleteCharAt(b.length() - 1);
    }
    b.append("}");
    b.append(", finder:[");
    for (NamedArgumentFinder argument : namedArgumentFinder) {
        wrote = true;
        b.append(argument).append(",");
    }
    if (wrote) {
        b.deleteCharAt(b.length() - 1);
    }
    b.append("]");
    b.append("}");
    return b.toString();
}
Also used : Argument(org.jdbi.v3.core.argument.Argument) NamedArgumentFinder(org.jdbi.v3.core.argument.NamedArgumentFinder) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with Argument

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

use of org.jdbi.v3.core.argument.Argument in project jdbi by jdbi.

the class RegisterArgumentFactoryImpl method configureForType.

@Override
public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) {
    RegisterArgumentFactory raf = (RegisterArgumentFactory) annotation;
    Arguments arguments = registry.get(Arguments.class);
    try {
        arguments.register(raf.value().newInstance());
    } catch (Exception e) {
        throw new IllegalStateException("unable to instantiate specified argument factory", e);
    }
}
Also used : RegisterArgumentFactory(org.jdbi.v3.sqlobject.config.RegisterArgumentFactory) Arguments(org.jdbi.v3.core.argument.Arguments)

Example 9 with Argument

use of org.jdbi.v3.core.argument.Argument in project jdbi by jdbi.

the class BindListFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    final BindList bindList = (BindList) annotation;
    final String name = ParameterUtil.findParameterName(bindList.value(), param).orElseThrow(() -> new UnsupportedOperationException("A @BindList parameter was not given a name, " + "and parameter name data is not present in the class file, for: " + param.getDeclaringExecutable() + "::" + param));
    return (stmt, arg) -> {
        if (arg == null || IterableLike.isEmpty(arg)) {
            switch(bindList.onEmpty()) {
                case VOID:
                    stmt.define(name, "");
                    return;
                case NULL:
                    stmt.define(name, "null");
                    return;
                case THROW:
                    throw new IllegalArgumentException(arg == null ? "argument is null; null was explicitly forbidden on this instance of BindList" : "argument is empty; emptiness was explicitly forbidden on this instance of BindList");
                default:
                    throw new IllegalStateException(valueNotHandledMessage);
            }
        }
        stmt.bindList(name, IterableLike.toList(arg));
    };
}
Also used : ParameterUtil(org.jdbi.v3.sqlobject.internal.ParameterUtil) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) SqlStatementCustomizerFactory(org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory) Annotation(java.lang.annotation.Annotation) SqlStatementParameterCustomizer(org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer) IterableLike(org.jdbi.v3.core.internal.IterableLike) BindList(org.jdbi.v3.sqlobject.customizer.BindList) Method(java.lang.reflect.Method) BindList(org.jdbi.v3.sqlobject.customizer.BindList)

Example 10 with Argument

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

Type (java.lang.reflect.Type)4 Argument (org.jdbi.v3.core.argument.Argument)4 NullArgument (org.jdbi.v3.core.argument.NullArgument)3 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 Parameter (java.lang.reflect.Parameter)2 Types (java.sql.Types)2 Optional (java.util.Optional)2 ConfigRegistry (org.jdbi.v3.core.config.ConfigRegistry)2 IterableLike (org.jdbi.v3.core.internal.IterableLike)2 SqlStatementCustomizerFactory (org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory)2 SqlStatementParameterCustomizer (org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer)2 ParameterUtil (org.jdbi.v3.sqlobject.internal.ParameterUtil)2 Duration (java.time.Duration)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Nonnull (javax.annotation.Nonnull)1