Search in sources :

Example 1 with DefineList

use of org.jdbi.v3.sqlobject.customizer.DefineList 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 2 with DefineList

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

the class TestBindList method testBindListWithHashPrefixParser.

@Test
public void testBindListWithHashPrefixParser() throws Exception {
    Jdbi jdbi = Jdbi.create(dbRule.getConnectionFactory());
    jdbi.setSqlParser(new HashPrefixSqlParser());
    jdbi.useHandle(handle -> {
        handle.registerRowMapper(FieldMapper.factory(Thing.class));
        handle.createUpdate("insert into thing (<columns>) values (<values>)").defineList("columns", "id", "foo").bindList("values", 3, "abc").execute();
        List<Thing> list = handle.createQuery("select id, foo from thing where id in (<ids>)").bindList("ids", 1, 3).mapTo(Thing.class).list();
        assertThat(list).extracting(Thing::getId, Thing::getFoo, Thing::getBar, Thing::getBaz).containsExactly(tuple(1, "foo1", null, null), tuple(3, "abc", null, null));
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

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 List (java.util.List)1 Jdbi (org.jdbi.v3.core.Jdbi)1 DefineList (org.jdbi.v3.sqlobject.customizer.DefineList)1 SqlStatementCustomizerFactory (org.jdbi.v3.sqlobject.customizer.SqlStatementCustomizerFactory)1 SqlStatementParameterCustomizer (org.jdbi.v3.sqlobject.customizer.SqlStatementParameterCustomizer)1 ParameterUtil (org.jdbi.v3.sqlobject.internal.ParameterUtil)1 Test (org.junit.Test)1