Search in sources :

Example 56 with Something

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

the class TestReentrancy method testTxnReentrant.

@Test
public void testTxnReentrant() throws Exception {
    final TheBasics dao = db.onDemand(TheBasics.class);
    dao.withHandle(handle1 -> {
        handle1.useTransaction(h -> {
            dao.insert(new Something(1, "x"));
            List<String> rs = h.createQuery("select name from something where id = 1").mapTo(String.class).list();
            assertThat(rs).hasSize(1);
            h.createQuery("SELECT 1").mapTo(int.class).list();
        });
        return null;
    });
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 57 with Something

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

the class TestDefineParameter method testDefineParameter.

@Test
public void testDefineParameter() throws Exception {
    handle.execute("create table stuff (id identity primary key, name varchar(50))");
    handle.execute("create table junk  (id identity primary key, name varchar(50))");
    HoneyBadger badass = handle.attach(HoneyBadger.class);
    Something ted = new Something(1, "Ted");
    Something fred = new Something(2, "Fred");
    badass.insert("stuff", ted);
    badass.insert("junk", fred);
    assertThat(badass.findById("stuff", 1)).isEqualTo(ted);
    assertThat(badass.findById("junk", 1)).isNull();
    assertThat(badass.findById("stuff", 2)).isNull();
    assertThat(badass.findById("junk", 2)).isEqualTo(fred);
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 58 with Something

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

the class TestDocumentation method testMappingExampleChainedIterator2.

@Test
public void testMappingExampleChainedIterator2() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        h.execute("insert into something (id, name) values (1, 'Brian')");
        h.execute("insert into something (id, name) values (2, 'Keith')");
        Iterator<String> rs = h.createQuery("select name from something order by id").mapTo(String.class).iterator();
        assertThat(rs.next()).isEqualTo("Brian");
        assertThat(rs.next()).isEqualTo("Keith");
        assertThat(rs.hasNext()).isFalse();
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 59 with Something

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

the class TestDocumentation method testFiveMinuteFluentApi.

@Test
public void testFiveMinuteFluentApi() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        h.execute("insert into something (id, name) values (?, ?)", 1, "Brian");
        String name = h.createQuery("select name from something where id = :id").bind("id", 1).mapTo(String.class).findOnly();
        assertThat(name).isEqualTo("Brian");
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 60 with Something

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

the class TestDocumentation method testFoo.

@Test
public void testFoo() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        h.attach(BatchInserter.class).insert(new Something(1, "Brian"), new Something(3, "Patrick"), new Something(2, "Robert"));
        QueryReturningResultIterable qrri = h.attach(QueryReturningResultIterable.class);
        ResultIterable<String> iterable = qrri.findById(1);
        assertThat(iterable.findOnly()).isEqualTo("Brian");
    }
}
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