Search in sources :

Example 6 with SqlStatementParameterCustomizer

use of org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer in project jdbi by jdbi.

the class BindFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    Bind b = (Bind) annotation;
    String nameFromAnnotation = b == null ? Bind.NO_VALUE : b.value();
    Optional<String> name = ParameterUtil.findParameterName(nameFromAnnotation, param);
    return (stmt, arg) -> {
        stmt.bindByType(index, arg, type);
        name.ifPresent(n -> stmt.bindByType(n, arg, type));
    };
}
Also used : Bind(org.jdbi.v3.sqlobject.customizer.Bind) 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) Optional(java.util.Optional) Method(java.lang.reflect.Method) Bind(org.jdbi.v3.sqlobject.customizer.Bind)

Example 7 with SqlStatementParameterCustomizer

use of org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer 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)

Aggregations

Annotation (java.lang.annotation.Annotation)7 Method (java.lang.reflect.Method)7 Parameter (java.lang.reflect.Parameter)7 Type (java.lang.reflect.Type)7 SqlStatementCustomizerFactory (org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory)7 SqlStatementParameterCustomizer (org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer)7 ParameterUtil (org.jdbi.v3.sqlobject.internal.ParameterUtil)5 Arrays (java.util.Arrays)3 List (java.util.List)2 IterableLike (org.jdbi.v3.core.internal.IterableLike)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 BindJpa (org.jdbi.v3.jpa.BindJpa)1 EntityMemberAccessException (org.jdbi.v3.jpa.EntityMemberAccessException)1 Bind (org.jdbi.v3.sqlobject.customizer.Bind)1 BindBeanList (org.jdbi.v3.sqlobject.customizer.BindBeanList)1 BindList (org.jdbi.v3.sqlobject.customizer.BindList)1 BindMap (org.jdbi.v3.sqlobject.customizer.BindMap)1