Search in sources :

Example 21 with Bind

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

the class TestClasspathSqlLocator method testNamedParamsInExternal.

@Test
public void testNamedParamsInExternal() throws Exception {
    Handle h = dbRule.openHandle();
    h.createUpdate(findSqlOnClasspath("insert-id-name")).bind("id", 1).bind("name", "Tip").execute();
    assertThat(h.select("select name from something").mapTo(String.class).list()).hasSize(1);
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 22 with Bind

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

the class TestRegisteredMappers method testRegisterInferredOnJdbi.

@Test
public void testRegisterInferredOnJdbi() throws Exception {
    db.registerRowMapper(new SomethingMapper());
    Something sam = db.withHandle(handle1 -> {
        handle1.execute("insert into something (id, name) values (18, 'Sam')");
        return handle1.createQuery("select id, name from something where id = :id").bind("id", 18).mapTo(Something.class).findOnly();
    });
    assertThat(sam.getName()).isEqualTo("Sam");
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 23 with Bind

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

the class TestPositionalParameterBinding method testSetPositionalString.

@Test
public void testSetPositionalString() 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 name = ?").bind(0, "eric").mapToBean(Something.class).list().get(0);
    assertThat(eric.getId()).isEqualTo(1);
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 24 with Bind

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

the class TestStatementContext method testFoo.

@Test
public void testFoo() throws Exception {
    Handle h = dbRule.openHandle();
    final int inserted = h.createUpdate("insert into <table> (id, name) values (:id, :name)").bind("id", 7).bind("name", "Martin").define("table", "something").execute();
    assertThat(inserted).isEqualTo(1);
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 25 with Bind

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

the class TestTimingCollector method testUpdate.

@Test
public void testUpdate() throws Exception {
    String stmt1 = "insert into something (id, name) values (1, 'eric')";
    String stmt2 = "update something set name = :name where id = :id";
    String stmt3 = "select * from something where id = :id";
    h.execute(stmt1);
    h.createUpdate(stmt2).bind("id", 1).bind("name", "ERIC").execute();
    Something eric = h.createQuery(stmt3).bind("id", 1).mapToBean(Something.class).list().get(0);
    assertThat(eric.getName()).isEqualTo("ERIC");
    assertThat(tc.getRawStatements()).containsExactly(stmt1, stmt2, stmt3);
    assertThat(tc.getRenderedStatements()).containsExactly(stmt1, stmt2, stmt3);
    assertThat(tc.getParsedStatements()).extracting("sql").containsExactly(stmt1, "update something set name = ? where id = ?", "select * from something where id = ?");
}
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