Search in sources :

Example 56 with Handle

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

the class FieldMapperTest method testColumnNameAnnotation.

@Test
public void testColumnNameAnnotation() {
    Handle handle = dbRule.getSharedHandle();
    handle.execute("insert into something (id, name) values (1, 'foo')");
    ColumnNameThing thing = handle.createQuery("select * from something").map(FieldMapper.of(ColumnNameThing.class)).findOnly();
    assertThat(thing.i).isEqualTo(1);
    assertThat(thing.s).isEqualTo("foo");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 57 with Handle

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

the class FieldMapperTest method testNested.

@Test
public void testNested() {
    Handle handle = dbRule.getSharedHandle();
    handle.execute("insert into something (id, name) values (1, 'foo')");
    assertThat(handle.registerRowMapper(FieldMapper.factory(NestedThing.class)).select("SELECT id, name FROM something").mapTo(NestedThing.class).findOnly()).extracting("nested.i", "nested.s").containsExactly(1, "foo");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 58 with Handle

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

the class TestReducing method setUp.

@Before
public void setUp() {
    Handle h = dbRule.getSharedHandle();
    h.execute("CREATE TABLE something_location (id int, location varchar)");
    h.execute("INSERT INTO something (id, name) VALUES (1, 'tree')");
    h.execute("INSERT INTO something (id, name) VALUES (2, 'apple')");
    h.execute("INSERT INTO something_location (id, location) VALUES (1, 'outside')");
    h.execute("INSERT INTO something_location (id, location) VALUES (2, 'tree')");
    h.execute("INSERT INTO something_location (id, location) VALUES (2, 'pie')");
    h.registerRowMapper(new SomethingMapper());
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) Handle(org.jdbi.v3.core.Handle) Before(org.junit.Before)

Example 59 with Handle

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

the class TestResultBearing method setUp.

@Before
public void setUp() throws Exception {
    Handle h = dbRule.getSharedHandle();
    h.execute("CREATE TABLE reduce (u INT)");
    for (int u = 0; u < 5; u++) {
        h.execute("INSERT INTO reduce VALUES (?)", u);
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Before(org.junit.Before)

Example 60 with Handle

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

the class TestBindList method testBindListWithHashPrefixParser.

@Test
public void testBindListWithHashPrefixParser() throws Exception {
    Jdbi jdbi = Jdbi.create(dbRule.getConnectionFactory());
    jdbi.setSqlParser(new HashPrefixSqlParser());
    jdbi.useHandle(handle -> {
        handle.registerRowMapper(FieldMapper.factory(Thing.class));
        handle.createUpdate("insert into thing (<columns>) values (<values>)").defineList("columns", "id", "foo").bindList("values", 3, "abc").execute();
        List<Thing> list = handle.createQuery("select id, foo from thing where id in (<ids>)").bindList("ids", 1, 3).mapTo(Thing.class).list();
        assertThat(list).extracting(Thing::getId, Thing::getFoo, Thing::getBar, Thing::getBaz).containsExactly(tuple(1, "foo1", null, null), tuple(3, "abc", null, null));
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) 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