Search in sources :

Example 1 with Something

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

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

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

the class TestTransactionAnnotation method testTx.

@Test
public void testTx() throws Exception {
    Dao dao = handle.attach(Dao.class);
    Something s = dao.insertAndFetch(1, "Ian");
    assertThat(s).isEqualTo(new Something(1, "Ian"));
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 4 with Something

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

the class TestTransactional method testDoublyTransactional.

@Test
public void testDoublyTransactional() throws Exception {
    final TheBasics dao = db.onDemand(TheBasics.class);
    dao.inTransaction(TransactionIsolationLevel.SERIALIZABLE, transactional -> {
        transactional.insert(new Something(1, "2"));
        inTransaction.set(true);
        transactional.insert(new Something(2, "3"));
        inTransaction.set(false);
        return null;
    });
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 5 with Something

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

Aggregations

Test (org.junit.Test)170 Something (org.jdbi.v3.core.Something)123 Handle (org.jdbi.v3.core.Handle)80 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 Before (org.junit.Before)11 ArrayList (java.util.ArrayList)6 Jdbi (org.jdbi.v3.core.Jdbi)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 GenericType (org.jdbi.v3.core.generic.GenericType)5 List (java.util.List)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 H2DatabaseRule (org.jdbi.v3.core.rule.H2DatabaseRule)4 Rule (org.junit.Rule)4 Connection (java.sql.Connection)3 LinkedHashMap (java.util.LinkedHashMap)3 Collector (java.util.stream.Collector)3 Collectors.toList (java.util.stream.Collectors.toList)3 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)3 NoSuchMapperException (org.jdbi.v3.core.mapper.NoSuchMapperException)3