Search in sources :

Example 61 with Something

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

the class TestPositionalParameterBinding method testSetPositionalInteger.

@Test
public void testSetPositionalInteger() throws Exception {
    h.execute("insert into something (id, name) values (1, 'eric')");
    h.execute("insert into something (id, name) values (2, 'brian')");
    Something eric = h.createQuery("select * from something where id = ?").bind(0, 1).mapToBean(Something.class).list().get(0);
    assertThat(eric.getId()).isEqualTo(1);
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 62 with Something

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

the class TestPreparedBatchGenerateKeys method testBatchInsertWithKeyGeneration.

@Test
public void testBatchInsertWithKeyGeneration() throws Exception {
    Jdbi db = Jdbi.create("jdbc:hsqldb:mem:jdbi-batch-keys-test", "sa", "");
    try (Handle h = db.open()) {
        h.execute("create table something (id integer not null generated by default as identity (start with 10000), name varchar(50) )");
        PreparedBatch batch = h.prepareBatch("insert into something (name) values (?)");
        batch.add("Brian");
        batch.add("Thom");
        List<Integer> ids = batch.executeAndReturnGeneratedKeys().mapTo(int.class).list();
        assertThat(ids).containsExactly(10000, 10001);
        List<Something> somethings = h.createQuery("select id, name from something").mapToBean(Something.class).list();
        assertThat(somethings).containsExactly(new Something(10000, "Brian"), new Something(10001, "Thom"));
    }
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 63 with Something

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

the class TestPreparedBatchGenerateKeysPostgres method testBatchInsertWithKeyGenerationAndExplicitColumnNames.

@Test
public void testBatchInsertWithKeyGenerationAndExplicitColumnNames() {
    PreparedBatch batch = h.prepareBatch("insert into something (name) values (?) ");
    batch.add("Brian");
    batch.add("Thom");
    List<Integer> ids = batch.executeAndReturnGeneratedKeys("id").mapTo(Integer.class).list();
    assertThat(ids).containsExactly(1, 2);
    List<Something> somethings = h.createQuery("select id, name from something").mapToBean(Something.class).list();
    assertThat(somethings).containsExactly(new Something(1, "Brian"), new Something(2, "Thom"));
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 64 with Something

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

the class TestQueries method testIteratedResult.

@Test
public void testIteratedResult() throws Exception {
    h.execute("insert into something (id, name) values (1, 'eric')");
    h.execute("insert into something (id, name) values (2, 'brian')");
    try (ResultIterator<Something> i = h.createQuery("select * from something order by id").mapToBean(Something.class).iterator()) {
        assertThat(i.hasNext()).isTrue();
        Something first = i.next();
        assertThat(first.getName()).isEqualTo("eric");
        assertThat(i.hasNext()).isTrue();
        Something second = i.next();
        assertThat(second.getId()).isEqualTo(2);
        assertThat(i.hasNext()).isFalse();
    }
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 65 with Something

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

the class TestQueries method testMappedQueryObjectWithNulls.

@Test
public void testMappedQueryObjectWithNulls() throws Exception {
    h.execute("insert into something (id, name, integerValue) values (1, 'eric', null)");
    ResultIterable<Something> query = h.createQuery("select * from something order by id").mapToBean(Something.class);
    List<Something> r = query.list();
    Something eric = r.get(0);
    assertThat(eric).isEqualTo(new Something(1, "eric"));
    assertThat(eric.getIntegerValue()).isNull();
}
Also used : Something(org.jdbi.v3.core.Something) 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