Search in sources :

Example 46 with Handle

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

the class BeanMapperTest method testNestedPrefixStrict.

@Test
public void testNestedPrefixStrict() {
    Handle handle = dbRule.getSharedHandle();
    handle.getConfig(ReflectionMappers.class).setStrictMatching(true);
    handle.registerRowMapper(BeanMapper.factory(NestedPrefixBean.class));
    // three, sir!
    handle.execute("insert into something (id, name, integerValue) values (1, 'foo', 5)");
    assertThat(handle.createQuery("select id nested_id, name nested_name, integerValue from something").mapTo(NestedPrefixBean.class).findOnly()).extracting("nested.id", "nested.name", "integerValue").containsExactly(1, "foo", 5);
    assertThatThrownBy(() -> handle.createQuery("select id nested_id, name nested_name, 1 as other from something").mapTo(NestedPrefixBean.class).findOnly()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("could not match properties for columns: [other]");
    assertThatThrownBy(() -> handle.createQuery("select id nested_id, name nested_name, 1 as nested_other from something").mapTo(NestedPrefixBean.class).findOnly()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("could not match properties for columns: [nested_other]");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 47 with Handle

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

the class BeanMapperTest method testNestedStrict.

@Test
public void testNestedStrict() {
    Handle handle = dbRule.getSharedHandle();
    handle.getConfig(ReflectionMappers.class).setStrictMatching(true);
    handle.registerRowMapper(BeanMapper.factory(NestedBean.class));
    handle.execute("insert into something (id, name) values (1, 'foo')");
    assertThat(handle.createQuery("select id, name from something").mapTo(NestedBean.class).findOnly()).extracting("nested.id", "nested.name").containsExactly(1, "foo");
    assertThatThrownBy(() -> handle.createQuery("select id, name, 1 as other from something").mapTo(NestedBean.class).findOnly()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("could not match properties for columns: [other]");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 48 with Handle

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

the class ConstructorMapperTest method nestedPrefixParametersStrict.

@Test
public void nestedPrefixParametersStrict() {
    Handle handle = dbRule.getSharedHandle();
    handle.getConfig(ReflectionMappers.class).setStrictMatching(true);
    handle.registerRowMapper(ConstructorMapper.factory(NestedPrefixBean.class));
    assertThat(handle.createQuery("select i nested_i, s nested_s from bean").mapTo(NestedPrefixBean.class).findOnly()).extracting("nested.s", "nested.i").containsExactly("3", 2);
    assertThatThrownBy(() -> handle.createQuery("select i nested_i, s nested_s, 1 as other from bean").mapTo(NestedPrefixBean.class).findOnly()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("could not match parameters for columns: [other]");
    assertThatThrownBy(() -> handle.createQuery("select i nested_i, s nested_s, 1 as nested_other from bean").mapTo(NestedPrefixBean.class).findOnly()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("could not match parameters for columns: [nested_other]");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 49 with Handle

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

the class TestUpdateGeneratedKeys method testUpdate.

@Test
public void testUpdate() throws Exception {
    Handle h = dbRule.openHandle();
    Update insert = h.createUpdate("insert into something_else (name) values (:name)");
    insert.bind("name", "Brian");
    Long id1 = insert.executeAndReturnGeneratedKeys().mapTo(long.class).findOnly();
    assertThat(id1).isNotNull();
    Update update = h.createUpdate("update something_else set name = :name where id = :id");
    update.bind("id", id1);
    update.bind("name", "Tom");
    Optional<Long> id2 = update.executeAndReturnGeneratedKeys().mapTo(long.class).findFirst();
    assertThat(id2.isPresent()).isFalse();
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 50 with Handle

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

the class TestUpdateGeneratedKeys method testDelete.

@Test
public void testDelete() throws Exception {
    Handle h = dbRule.openHandle();
    Update insert = h.createUpdate("insert into something_else (name) values (:name)");
    insert.bind("name", "Brian");
    Long id1 = insert.executeAndReturnGeneratedKeys().mapTo(long.class).findOnly();
    assertThat(id1).isNotNull();
    Update delete = h.createUpdate("delete from something_else where id = :id");
    delete.bind("id", id1);
    Optional<Long> id2 = delete.executeAndReturnGeneratedKeys().mapTo(long.class).findFirst();
    assertThat(id2.isPresent()).isFalse();
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)123 Handle (org.jdbi.v3.core.Handle)119 Jdbi (org.jdbi.v3.core.Jdbi)31 Something (org.jdbi.v3.core.Something)29 Before (org.junit.Before)20 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 SQLException (java.sql.SQLException)8 Update (org.jdbi.v3.core.statement.Update)7 GenericType (org.jdbi.v3.core.generic.GenericType)6 Query (org.jdbi.v3.core.statement.Query)6 Rule (org.junit.Rule)5 Map (java.util.Map)4 BrokenDao (org.jdbi.v3.sqlobject.subpackage.BrokenDao)4 SomethingDao (org.jdbi.v3.sqlobject.subpackage.SomethingDao)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Method (java.lang.reflect.Method)3 Connection (java.sql.Connection)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3