Search in sources :

Example 81 with Handle

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);
    }
}
Also used : SQLException(java.sql.SQLException) JdbiPlugin(org.jdbi.v3.core.spi.JdbiPlugin) DefaultStatementBuilder(org.jdbi.v3.core.statement.DefaultStatementBuilder) StatementBuilder(org.jdbi.v3.core.statement.StatementBuilder) Connection(java.sql.Connection)

Example 82 with Handle

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"));
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 83 with Handle

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);
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 84 with Handle

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");
}
Also used : Query(org.jdbi.v3.core.statement.Query) HashMap(java.util.HashMap) Update(org.jdbi.v3.core.statement.Update) Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 85 with Handle

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"));
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)123 Handle (org.jdbi.v3.core.Handle)119 Jdbi (org.jdbi.v3.core.Jdbi)31 Something (org.jdbi.v3.core.Something)29 Before (org.junit.Before)20 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 SQLException (java.sql.SQLException)8 Update (org.jdbi.v3.core.statement.Update)7 GenericType (org.jdbi.v3.core.generic.GenericType)6 Query (org.jdbi.v3.core.statement.Query)6 Rule (org.junit.Rule)5 Map (java.util.Map)4 BrokenDao (org.jdbi.v3.sqlobject.subpackage.BrokenDao)4 SomethingDao (org.jdbi.v3.sqlobject.subpackage.SomethingDao)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Method (java.lang.reflect.Method)3 Connection (java.sql.Connection)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3