Search in sources :

Example 1 with BindList

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

Example 2 with BindList

use of org.jdbi.v3.sqlobject.customizer.BindList 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)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Type (java.lang.reflect.Type)1 Jdbi (org.jdbi.v3.core.Jdbi)1 IterableLike (org.jdbi.v3.core.internal.IterableLike)1 BindList (org.jdbi.v3.sqlobject.customizer.BindList)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