Search in sources :

Example 1 with SqlStatementParameterCustomizer

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

the class BindBeanListFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    final BindBeanList bindBeanList = (BindBeanList) annotation;
    final String name = ParameterUtil.findParameterName(bindBeanList.value(), param).orElseThrow(() -> new UnsupportedOperationException("A @BindBeanList 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) {
            throw new IllegalArgumentException("argument is null; null was explicitly forbidden on BindBeanList");
        }
        stmt.bindBeanList(name, IterableLike.toList(arg), Arrays.asList(bindBeanList.propertyNames()));
    };
}
Also used : Arrays(java.util.Arrays) 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) BindBeanList(org.jdbi.v3.sqlobject.customizer.BindBeanList) SqlStatementParameterCustomizer(org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer) IterableLike(org.jdbi.v3.core.internal.IterableLike) Method(java.lang.reflect.Method) BindBeanList(org.jdbi.v3.sqlobject.customizer.BindBeanList)

Example 2 with SqlStatementParameterCustomizer

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

the class BindMapFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation a, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    BindMap annotation = (BindMap) a;
    List<String> keys = Arrays.asList(annotation.keys());
    String prefix = annotation.value().isEmpty() ? "" : annotation.value() + ".";
    return (stmt, arg) -> {
        Map<?, ?> map = (Map<?, ?>) arg;
        Map<String, Object> toBind = new HashMap<>();
        map.forEach((k, v) -> {
            if (annotation.convertKeys() || k instanceof String) {
                String key = k.toString();
                if (keys.isEmpty() || keys.contains(key)) {
                    toBind.put(prefix + key, v);
                }
            } else {
                throw new IllegalArgumentException("Key " + k + " (of " + k.getClass() + ") must be a String");
            }
        });
        keys.forEach(key -> toBind.putIfAbsent(prefix + key, null));
        stmt.bindMap(toBind);
    };
}
Also used : BindMap(org.jdbi.v3.sqlobject.customizer.BindMap) Arrays(java.util.Arrays) List(java.util.List) Type(java.lang.reflect.Type) Parameter(java.lang.reflect.Parameter) SqlStatementCustomizerFactory(org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory) Map(java.util.Map) Annotation(java.lang.annotation.Annotation) SqlStatementParameterCustomizer(org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) BindMap(org.jdbi.v3.sqlobject.customizer.BindMap) BindMap(org.jdbi.v3.sqlobject.customizer.BindMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with SqlStatementParameterCustomizer

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

the class DefineFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    Define define = (Define) annotation;
    final String name = ParameterUtil.findParameterName(define.value(), param).orElseThrow(() -> new UnsupportedOperationException("A @Define parameter was not given a name, " + "and parameter name data is not present in the class file, for: " + param.getDeclaringExecutable() + "::" + param));
    return (stmt, arg) -> stmt.define(name, arg);
}
Also used : Define(org.jdbi.v3.sqlobject.customizer.Define) 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) Method(java.lang.reflect.Method) Define(org.jdbi.v3.sqlobject.customizer.Define)

Example 4 with SqlStatementParameterCustomizer

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

the class DefineListFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    final DefineList d = (DefineList) annotation;
    final String name = ParameterUtil.findParameterName(d.value(), param).orElseThrow(() -> new UnsupportedOperationException("A @DefineList parameter was not given a name, " + "and parameter name data is not present in the class file, for: " + param.getDeclaringExecutable() + "::" + param));
    return (stmt, arg) -> {
        List<?> argsList;
        if (arg instanceof List) {
            argsList = (List<?>) arg;
        } else if (arg instanceof Object[]) {
            argsList = Arrays.asList((Object[]) arg);
        } else if (arg == null) {
            throw new IllegalArgumentException("A null object was passed as a @DefineList parameter. " + "@DefineList is only supported on List and array arguments");
        } else {
            throw new IllegalArgumentException("A " + arg.getClass() + " object was passed as a @DefineList " + "parameter. @DefineList is only supported on List and array arguments");
        }
        if (argsList.isEmpty()) {
            throw new IllegalArgumentException("An empty list was passed as a @DefineList parameter. Can't define " + "an empty attribute.");
        }
        if (argsList.contains(null)) {
            throw new IllegalArgumentException("A @DefineList parameter was passed a list with null values in it.");
        }
        stmt.defineList(name, argsList);
    };
}
Also used : Arrays(java.util.Arrays) List(java.util.List) 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) DefineList(org.jdbi.v3.sqlobject.customizer.DefineList) Method(java.lang.reflect.Method) DefineList(org.jdbi.v3.sqlobject.customizer.DefineList) List(java.util.List) DefineList(org.jdbi.v3.sqlobject.customizer.DefineList)

Example 5 with SqlStatementParameterCustomizer

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

the class BindJpaFactory method createForParameter.

@Override
public SqlStatementParameterCustomizer createForParameter(Annotation annotation, Class<?> sqlObjectType, Method method, Parameter param, int index, Type type) {
    BindJpa bind = (BindJpa) annotation;
    final String prefix;
    if (bind.value().isEmpty()) {
        prefix = "";
    } else {
        prefix = bind.value() + ".";
    }
    return (stmt, arg) -> {
        JpaClass<?> jpaClass = JpaClass.get(arg.getClass());
        for (JpaMember member : jpaClass.members()) {
            stmt.bindByType(prefix + member.getColumnName(), readMember(arg, member), member.getType());
        }
    };
}
Also used : EntityMemberAccessException(org.jdbi.v3.jpa.EntityMemberAccessException) BindJpa(org.jdbi.v3.jpa.BindJpa) 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) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) BindJpa(org.jdbi.v3.jpa.BindJpa)

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