Search in sources :

Example 1 with BindMap

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

Aggregations

Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Type (java.lang.reflect.Type)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BindMap (org.jdbi.v3.sqlobject.customizer.BindMap)1 SqlStatementCustomizerFactory (org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory)1 SqlStatementParameterCustomizer (org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer)1