use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class Jdbi method open.
/**
* Obtain a Handle to the data source wrapped by this Jdbi instance.
* You own this expensive resource and are required to close it or
* risk leaks. Using a {@code try-with-resources} block is recommended.
*
* @return an open Handle instance
* @see #useHandle(HandleConsumer)
* @see #withHandle(HandleCallback)
*/
public Handle open() {
try {
final long start = System.nanoTime();
Connection conn = connectionFactory.openConnection();
final long stop = System.nanoTime();
for (JdbiPlugin p : plugins) {
conn = p.customizeConnection(conn);
}
StatementBuilder cache = statementBuilderFactory.get().createStatementBuilder(conn);
Handle h = new Handle(config.createCopy(), transactionhandler.get(), cache, conn);
for (JdbiPlugin p : plugins) {
h = p.customizeHandle(h);
}
LOG.trace("Jdbi [{}] obtain handle [{}] in {}ms", this, h, (stop - start) / 1000000L);
return h;
} catch (SQLException e) {
throw new ConnectionException(e);
}
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestNamedParams method testFunctionsPrefixBinding.
@Test
public void testFunctionsPrefixBinding() throws Exception {
Handle h = dbRule.openHandle();
assertThat(h.createUpdate("insert into something (id, name) values (:my.id, :my.name)").bindMethods("my", new NoArgFunctions(0, "Keith")).execute()).isEqualTo(1);
assertThat(h.select("select * from something where id = ?", 0).mapToBean(Something.class).findOnly()).isEqualTo(new Something(0, "Keith"));
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestNamedParams method testDemo.
@Test
public void testDemo() throws Exception {
Handle h = dbRule.getSharedHandle();
h.createUpdate("insert into something (id, name) values (:id, :name)").bind("id", 1).bind("name", "Brian").execute();
h.execute("insert into something (id, name) values (?, ?)", 2, "Eric");
h.execute("insert into something (id, name) values (?, ?)", 3, "Erin");
List<Something> r = h.createQuery("select id, name from something " + "where name like :name " + "order by id").bind("name", "Eri%").mapToBean(Something.class).list();
assertThat(r).extracting(Something::getId).containsExactly(2, 3);
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestNamedParams method testMapKeyBinding.
@Test
public void testMapKeyBinding() throws Exception {
Handle h = dbRule.openHandle();
Update s = h.createUpdate("insert into something (id, name) values (:id, :name)");
Map<String, Object> args = new HashMap<>();
args.put("id", 0);
args.put("name", "Keith");
s.bindMap(args);
int insert_count = s.execute();
Query q = h.createQuery("select * from something where id = :id").bind("id", 0);
final Something fromDb = q.mapToBean(Something.class).findOnly();
assertThat(insert_count).isEqualTo(1);
assertThat(fromDb).extracting(Something::getId, Something::getName).containsExactly(0, "Keith");
}
use of org.jdbi.v3.core.Handle in project jdbi by jdbi.
the class TestNamedParams method testFieldsBinding.
@Test
public void testFieldsBinding() throws Exception {
Handle h = dbRule.openHandle();
assertThat(h.createUpdate("insert into something (id, name) values (:id, :name)").bindFields(new PublicFields(0, "Keith")).execute()).isEqualTo(1);
assertThat(h.select("select * from something where id = ?", 0).mapToBean(Something.class).findOnly()).isEqualTo(new Something(0, "Keith"));
}
Aggregations