Search in sources :

Example 6 with Bind

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

the class TestPositionalParameterBinding method testSetPositionalInteger.

@Test
public void testSetPositionalInteger() throws Exception {
    h.execute("insert into something (id, name) values (1, 'eric')");
    h.execute("insert into something (id, name) values (2, 'brian')");
    Something eric = h.createQuery("select * from something where id = ?").bind(0, 1).mapToBean(Something.class).list().get(0);
    assertThat(eric.getId()).isEqualTo(1);
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 7 with Bind

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

the class CallTest method testCall.

@Test
public void testCall() {
    Handle handle = db.getHandle();
    handle.execute(findSqlOnClasspath("create_stored_proc_add"));
    // tag::invokeProcedure[]
    OutParameters result = handle.createCall(// <1>
    "{:sum = call add(:a, :b)}").bind("a", // <2>
    13).bind("b", // <2>
    9).registerOutParameter("sum", // <3> <4>
    Types.INTEGER).invoke();
    // end::invokeProcedure[]
    // tag::getOutParameters[]
    int sum = result.getInt("sum");
    // end::getOutParameters[]
    assertThat(sum).isEqualTo(22);
}
Also used : OutParameters(org.jdbi.v3.core.statement.OutParameters) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 8 with Bind

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

the class TestGuavaOptional method testBindOptionalOfCustomType.

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

Example 9 with Bind

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

the class TestGuavaOptional method testBindOptionalPresent.

@Test
public void testBindOptionalPresent() throws Exception {
    Something result = handle.createQuery(SELECT_BY_NAME).bind("name", Optional.of("brian")).mapToBean(Something.class).findOnly();
    assertThat(result).isEqualTo(new Something(2, "brian"));
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 10 with Bind

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

the class StatementsTest method testBatch.

@Test
public void testBatch() throws Exception {
    // tag::batch[]
    PreparedBatch batch = handle.prepareBatch("INSERT INTO user(id, name) VALUES(:id, :name)");
    for (int i = 100; i < 5000; i++) {
        batch.bind("id", i).bind("name", "User:" + i).add();
    }
    int[] counts = batch.execute();
    // end::batch[]
    int[] expected = new int[4900];
    Arrays.fill(expected, 1);
    assertThat(counts).isEqualTo(expected);
}
Also used : PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) 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