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");
}
}
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");
}
}
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"));
}
}
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")));
}
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);
}
Aggregations