Search in sources :

Example 1 with Handle

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

the class TestTooManyCursors method testFoo.

@Test
public void testFoo() throws Exception {
    ConnectionFactory cf = dbRule.getConnectionFactory();
    ConnectionFactory errorCf = new ErrorProducingConnectionFactory(cf, 99);
    Jdbi db = Jdbi.create(errorCf);
    db.useHandle(handle -> {
        handle.setStatementBuilder(new DefaultStatementBuilder());
        for (int idx = 0; idx < 100; idx++) {
            handle.createQuery("SELECT " + idx + " FROM something").mapTo(int.class).findFirst();
        }
    });
}
Also used : DefaultStatementBuilder(org.jdbi.v3.core.statement.DefaultStatementBuilder) Test(org.junit.Test)

Example 2 with Handle

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

the class TestArgumentFactory method testOnPreparedBatch.

@Test
public void testOnPreparedBatch() throws Exception {
    Handle h = dbRule.getSharedHandle();
    PreparedBatch batch = h.prepareBatch("insert into something (id, name) values (:id, :name)");
    batch.registerArgument(new NameAF());
    batch.bind("id", 1).bind("name", new Name("Brian", "McCallister")).add();
    batch.bind("id", 2).bind("name", new Name("Henning", "S")).add();
    batch.execute();
    List<String> rs = h.createQuery("select name from something order by id").mapTo(String.class).list();
    assertThat(rs.get(0)).isEqualTo("Brian McCallister");
    assertThat(rs.get(1)).isEqualTo("Henning S");
}
Also used : PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 3 with Handle

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

the class TestTransactional method setUp.

@Before
public void setUp() throws Exception {
    final JdbcDataSource ds = new JdbcDataSource() {

        private static final long serialVersionUID = 1L;

        @Override
        public Connection getConnection() throws SQLException {
            final Connection real = super.getConnection();
            return (Connection) Proxy.newProxyInstance(real.getClass().getClassLoader(), new Class<?>[] { Connection.class }, new TxnIsolationCheckingInvocationHandler(real));
        }
    };
    // in MVCC mode h2 doesn't shut down immediately on all connections closed, so need random db name
    ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID()));
    db = Jdbi.create(ds);
    db.installPlugin(new SqlObjectPlugin());
    db.registerRowMapper(new SomethingMapper());
    handle = db.open();
    handle.execute("create table something (id int primary key, name varchar(100))");
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) Before(org.junit.Before)

Example 4 with Handle

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

the class TestRegisterJoinRowMapper method testSqlObjectJoinRow.

@Test
public void testSqlObjectJoinRow() {
    Handle handle = dbRule.getSharedHandle();
    // tag::joinrowusage[]
    Multimap<User, Article> joined = HashMultimap.create();
    handle.attach(UserArticleDao.class).getAuthorship().forEach(jr -> joined.put(jr.get(User.class), jr.get(Article.class)));
    assertThat(joined).isEqualTo(JoinRowMapperTest.getExpected());
// end::joinrowusage[]
}
Also used : User(org.jdbi.v3.core.mapper.JoinRowMapperTest.User) Article(org.jdbi.v3.core.mapper.JoinRowMapperTest.Article) Handle(org.jdbi.v3.core.Handle) JoinRowMapperTest(org.jdbi.v3.core.mapper.JoinRowMapperTest) Test(org.junit.Test)

Example 5 with Handle

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

the class TestRegisteredMappersWork method testNoRootRegistrations.

@Test(expected = NoSuchMapperException.class)
public void testNoRootRegistrations() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        h.execute("insert into something (id, name) values (1, 'Henning')");
        h.createQuery("select id, name from something where id = 1").mapTo(Something.class).findFirst();
    }
}
Also used : Something(org.jdbi.v3.core.Something) 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