Search in sources :

Example 26 with Bind

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

the class TestOracleReturning method testReturningDmlPositionalParams.

@Test
public void testReturningDmlPositionalParams() {
    Handle h = dbRule.getSharedHandle();
    List<Integer> ids = h.createUpdate("insert into something(id, name) values (?, ?) returning id into ?").bind(0, 17).bind(1, "Brian").addCustomizer(returnParameters().register(2, OracleTypes.INTEGER)).execute(returningDml()).mapTo(int.class).list();
    assertThat(ids).containsExactly(17);
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 27 with Bind

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

the class TestUpdateGeneratedKeys method testInsert.

@Test
public void testInsert() throws Exception {
    Handle h = dbRule.openHandle();
    Update insert1 = h.createUpdate("insert into something_else (name) values (:name)");
    insert1.bind("name", "Brian");
    Long id1 = insert1.executeAndReturnGeneratedKeys().mapTo(long.class).findOnly();
    assertThat(id1).isNotNull();
    Update insert2 = h.createUpdate("insert into something_else (name) values (:name)");
    insert2.bind("name", "Tom");
    Long id2 = insert2.executeAndReturnGeneratedKeys().mapTo(long.class).findOnly();
    assertThat(id2).isNotNull();
    assertThat(id2).isGreaterThan(id1);
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 28 with Bind

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

the class TestGuavaOptional method testBindOptionalEmpty.

@Test
public void testBindOptionalEmpty() throws Exception {
    List<Something> result = handle.createQuery(SELECT_BY_NAME).bind("name", Optional.absent()).mapToBean(Something.class).list();
    assertThat(result).containsExactly(new Something(1, "eric"), new Something(2, "brian"));
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 29 with Bind

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

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

the class TestSqlCall method testFoo.

@Test
public void testFoo() throws Exception {
    Dao dao = handle.attach(Dao.class);
    // OutParameters out = handle.createCall(":num = call stored_insert(:id, :name)")
    // .bind("id", 1)
    // .bind("name", "Jeff")
    // .registerOutParameter("num", Types.INTEGER)
    // .invoke();
    dao.insert(1, "Jeff");
    assertThat(handle.attach(Dao.class).findById(1)).isEqualTo(new Something(1, "Jeff"));
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)34 Handle (org.jdbi.v3.core.Handle)18 Something (org.jdbi.v3.core.Something)15 Jdbi (org.jdbi.v3.core.Jdbi)4 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 Parameter (java.lang.reflect.Parameter)2 Type (java.lang.reflect.Type)2 OptionalFields (net.morimekta.test.providence.storage.jdbc.OptionalFields)2 PreparedBatch (org.jdbi.v3.core.statement.PreparedBatch)2 Query (org.jdbi.v3.core.statement.Query)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Types (java.sql.Types)1 Clock (java.time.Clock)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 ProvidenceJdbi.columnsFromAllFields (net.morimekta.providence.jdbi.v3.ProvidenceJdbi.columnsFromAllFields)1 ProvidenceJdbi.forMessage (net.morimekta.providence.jdbi.v3.ProvidenceJdbi.forMessage)1 ProvidenceJdbi.toField (net.morimekta.providence.jdbi.v3.ProvidenceJdbi.toField)1 ProvidenceJdbi.toMessage (net.morimekta.providence.jdbi.v3.ProvidenceJdbi.toMessage)1