Search in sources :

Example 36 with Handle

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

the class TestDocumentation method testBatchExample.

@Test
public void testBatchExample() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        BatchExample b = h.attach(BatchExample.class);
        List<Integer> ids = asList(1, 2, 3, 4, 5);
        Iterator<String> first_names = asList("Tip", "Jane", "Brian", "Keith", "Eric").iterator();
        b.insertFamily(ids, first_names, "McCallister");
        assertThat(b.findNameById(1)).isEqualTo("Tip McCallister");
        assertThat(b.findNameById(2)).isEqualTo("Jane McCallister");
        assertThat(b.findNameById(3)).isEqualTo("Brian McCallister");
        assertThat(b.findNameById(4)).isEqualTo("Keith McCallister");
        assertThat(b.findNameById(5)).isEqualTo("Eric McCallister");
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 37 with Handle

use of org.jdbi.v3.core.Handle 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)

Example 38 with Handle

use of org.jdbi.v3.core.Handle 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 39 with Handle

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

the class TestScript method testFuzzyScript.

@Test
public void testFuzzyScript() throws Exception {
    Handle h = dbRule.openHandle();
    Script script = h.createScript(getResourceOnClasspath("script/fuzzy-script.sql"));
    script.executeAsSeparateStatements();
    List<Map<String, Object>> rows = h.select("select id, name from something order by id").mapToMap().list();
    assertThat(rows).isEqualTo(ImmutableList.of(ImmutableMap.of("id", 1L, "name", "eric"), ImmutableMap.of("id", 2L, "name", "sally;ann"), ImmutableMap.of("id", 3L, "name", "bob"), ImmutableMap.of("id", 12L, "name", "sally;ann;junior")));
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 40 with Handle

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

the class TestClasspathSqlLocator method testPositionalParamsInPrepared.

@Test
public void testPositionalParamsInPrepared() throws Exception {
    Handle h = dbRule.openHandle();
    h.execute(findSqlOnClasspath("insert-id-name-positional"), 3, "Tip");
    assertThat(h.select("select name from something").mapTo(String.class).list()).hasSize(1);
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Handle (org.jdbi.v3.core.Handle)134 Test (org.junit.Test)124 Jdbi (org.jdbi.v3.core.Jdbi)36 Something (org.jdbi.v3.core.Something)29 Before (org.junit.Before)21 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 Namespace (net.sourceforge.argparse4j.inf.Namespace)11 Test (org.junit.jupiter.api.Test)11 SQLException (java.sql.SQLException)10 Query (org.jdbi.v3.core.statement.Query)8 Map (java.util.Map)7 Update (org.jdbi.v3.core.statement.Update)7 GenericType (org.jdbi.v3.core.generic.GenericType)6 Rule (org.junit.Rule)5 List (java.util.List)4 BrokenDao (org.jdbi.v3.sqlobject.subpackage.BrokenDao)4 SomethingDao (org.jdbi.v3.sqlobject.subpackage.SomethingDao)4 ArrayList (java.util.ArrayList)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 JdbiPlugin (org.jdbi.v3.core.spi.JdbiPlugin)3