Search in sources :

Example 91 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestNamedParams method testFunctionsNestedBinding.

@Test
public void testFunctionsNestedBinding() throws Exception {
    Handle h = dbRule.openHandle();
    assertThat(h.createUpdate("insert into something (id, name) values (:my.nested.id, :my.nested.name)").bindMethods("my", new Object() {

        @SuppressWarnings("unused")
        public NoArgFunctions nested() {
            return new NoArgFunctions(0, "Keith");
        }
    }).execute()).isEqualTo(1);
    assertThat(h.select("select * from something where id = ?", 0).mapToBean(Something.class).findOnly()).isEqualTo(new Something(0, "Keith"));
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 92 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestNamedParams method testFunctionsPrefixBinding.

@Test
public void testFunctionsPrefixBinding() throws Exception {
    Handle h = dbRule.openHandle();
    assertThat(h.createUpdate("insert into something (id, name) values (:my.id, :my.name)").bindMethods("my", new NoArgFunctions(0, "Keith")).execute()).isEqualTo(1);
    assertThat(h.select("select * from something where id = ?", 0).mapToBean(Something.class).findOnly()).isEqualTo(new Something(0, "Keith"));
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 93 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestNamedParams method testDemo.

@Test
public void testDemo() throws Exception {
    Handle h = dbRule.getSharedHandle();
    h.createUpdate("insert into something (id, name) values (:id, :name)").bind("id", 1).bind("name", "Brian").execute();
    h.execute("insert into something (id, name) values (?, ?)", 2, "Eric");
    h.execute("insert into something (id, name) values (?, ?)", 3, "Erin");
    List<Something> r = h.createQuery("select id, name from something " + "where name like :name " + "order by id").bind("name", "Eri%").mapToBean(Something.class).list();
    assertThat(r).extracting(Something::getId).containsExactly(2, 3);
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 94 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestNamedParams method testMapKeyBinding.

@Test
public void testMapKeyBinding() throws Exception {
    Handle h = dbRule.openHandle();
    Update s = h.createUpdate("insert into something (id, name) values (:id, :name)");
    Map<String, Object> args = new HashMap<>();
    args.put("id", 0);
    args.put("name", "Keith");
    s.bindMap(args);
    int insert_count = s.execute();
    Query q = h.createQuery("select * from something where id = :id").bind("id", 0);
    final Something fromDb = q.mapToBean(Something.class).findOnly();
    assertThat(insert_count).isEqualTo(1);
    assertThat(fromDb).extracting(Something::getId, Something::getName).containsExactly(0, "Keith");
}
Also used : Query(org.jdbi.v3.core.statement.Query) HashMap(java.util.HashMap) Update(org.jdbi.v3.core.statement.Update) Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 95 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestNamedParams method testFieldsBinding.

@Test
public void testFieldsBinding() throws Exception {
    Handle h = dbRule.openHandle();
    assertThat(h.createUpdate("insert into something (id, name) values (:id, :name)").bindFields(new PublicFields(0, "Keith")).execute()).isEqualTo(1);
    assertThat(h.select("select * from something where id = ?", 0).mapToBean(Something.class).findOnly()).isEqualTo(new Something(0, "Keith"));
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)170 Something (org.jdbi.v3.core.Something)123 Handle (org.jdbi.v3.core.Handle)80 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 Before (org.junit.Before)11 ArrayList (java.util.ArrayList)6 Jdbi (org.jdbi.v3.core.Jdbi)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 GenericType (org.jdbi.v3.core.generic.GenericType)5 List (java.util.List)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 H2DatabaseRule (org.jdbi.v3.core.rule.H2DatabaseRule)4 Rule (org.junit.Rule)4 Connection (java.sql.Connection)3 LinkedHashMap (java.util.LinkedHashMap)3 Collector (java.util.stream.Collector)3 Collectors.toList (java.util.stream.Collectors.toList)3 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)3 NoSuchMapperException (org.jdbi.v3.core.mapper.NoSuchMapperException)3