Search in sources :

Example 96 with Something

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

the class TestNamedParams method testCascadedLazyArgs.

@Test
public void testCascadedLazyArgs() 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);
    s.bindMap(args);
    s.bindBean(new Object() {

        @SuppressWarnings("unused")
        public String getName() {
            return "Keith";
        }
    });
    int insert_count = s.execute();
    assertThat(insert_count).isEqualTo(1);
    Something something = h.createQuery("select id, name from something").mapToBean(Something.class).findOnly();
    assertThat(something).isEqualTo(new Something(0, "Keith"));
}
Also used : 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 97 with Something

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

the class TestArgumentFactory method testRegisterOnJdbi.

@Test
public void testRegisterOnJdbi() throws Exception {
    final Jdbi db = dbRule.getJdbi();
    db.registerArgument(new NameAF());
    try (Handle h = db.open()) {
        h.createUpdate("insert into something (id, name) values (:id, :name)").bind("id", 7).bind("name", new Name("Brian", "McCallister")).execute();
        String full_name = h.createQuery("select name from something where id = 7").mapTo(String.class).findOnly();
        assertThat(full_name).isEqualTo("Brian McCallister");
    }
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 98 with Something

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

the class TestArgumentFactory method testRegisterOnHandle.

@Test
public void testRegisterOnHandle() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        h.registerArgument(new NameAF());
        h.createUpdate("insert into something (id, name) values (:id, :name)").bind("id", 7).bind("name", new Name("Brian", "McCallister")).execute();
        String full_name = h.createQuery("select name from something where id = 7").mapTo(String.class).findOnly();
        assertThat(full_name).isEqualTo("Brian McCallister");
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 99 with Something

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

the class TestNamedParams method testBeanPropertyNestedBinding.

@Test
public void testBeanPropertyNestedBinding() throws Exception {
    Handle h = dbRule.openHandle();
    Something thing = new Something(0, "Keith");
    assertThat(h.createUpdate("insert into something (id, name) values (:my.nested.id, :my.nested.name)").bindBean("my", new Object() {

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

Example 100 with Something

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

the class TestNamedParams method testInsert.

@Test
public void testInsert() throws Exception {
    Handle h = dbRule.openHandle();
    Update insert = h.createUpdate("insert into something (id, name) values (:id, :name)");
    insert.bind("id", 1);
    insert.bind("name", "Brian");
    int count = insert.execute();
    assertThat(count).isEqualTo(1);
}
Also used : Update(org.jdbi.v3.core.statement.Update) 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