Search in sources :

Example 6 with Handle

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

the class TestSqlObject method testSqlUpdateWithTransaction.

@Test
public void testSqlUpdateWithTransaction() {
    Handle handle = Mockito.spy(dbRule.getSharedHandle());
    Dao dao = handle.attach(Dao.class);
    dao.insert(1, "foo");
    verify(handle, never()).begin();
    assertThat(dao.findById(1)).isEqualTo(new Something(1, "foo"));
    assertThat(dao.insertTransactional(2, "bar")).isEqualTo(1);
    verify(handle, times(1)).begin();
    assertThat(dao.findById(2)).isEqualTo(new Something(2, "bar"));
}
Also used : SomethingDao(org.jdbi.v3.sqlobject.subpackage.SomethingDao) BrokenDao(org.jdbi.v3.sqlobject.subpackage.BrokenDao) Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 7 with Handle

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

the class TestSqlObject method testNestedTransactionWithSameIsolation.

@Test
public void testNestedTransactionWithSameIsolation() {
    Handle handle = Mockito.spy(dbRule.getSharedHandle());
    Dao dao = handle.attach(Dao.class);
    dao.nestedTransactionWithSameIsolation();
    verify(handle, times(1)).begin();
    verify(handle, times(1)).commit();
}
Also used : SomethingDao(org.jdbi.v3.sqlobject.subpackage.SomethingDao) BrokenDao(org.jdbi.v3.sqlobject.subpackage.BrokenDao) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 8 with Handle

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

the class TestUseCustomHandlerFactory method setUp.

@Before
public void setUp() throws Exception {
    Jdbi db = dbRule.getJdbi();
    HandlerFactory defaultHandlerFactory = new HandlerFactory() {

        @Override
        public Optional<Handler> buildHandler(Class<?> sqlObjectType, Method method) {
            return getImplementation(sqlObjectType, method).map(m -> (Handler) (target, args, handle) -> m.invoke(null, Stream.concat(Stream.of(target), Stream.of(args)).toArray()));
        }

        private Optional<Method> getImplementation(Class<?> type, Method method) {
            return Stream.of(type.getClasses()).filter(c -> c.getSimpleName().equals("DefaultImpls")).flatMap(c -> Stream.of(c.getMethods()).filter(m -> m.getName().equals(method.getName()))).findAny();
        }
    };
    db.configure(Handlers.class, c -> c.register(defaultHandlerFactory));
    handle = db.open();
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Handlers(org.jdbi.v3.sqlobject.Handlers) BindBean(org.jdbi.v3.sqlobject.customizer.BindBean) SqlObjectPlugin(org.jdbi.v3.sqlobject.SqlObjectPlugin) Bind(org.jdbi.v3.sqlobject.customizer.Bind) Transaction(org.jdbi.v3.sqlobject.transaction.Transaction) Something(org.jdbi.v3.core.Something) SqlUpdate(org.jdbi.v3.sqlobject.statement.SqlUpdate) H2DatabaseRule(org.jdbi.v3.core.rule.H2DatabaseRule) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Handler(org.jdbi.v3.sqlobject.Handler) Rule(org.junit.Rule) Stream(java.util.stream.Stream) Handle(org.jdbi.v3.core.Handle) HandlerFactory(org.jdbi.v3.sqlobject.HandlerFactory) Optional(java.util.Optional) SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) Method(java.lang.reflect.Method) SqlQuery(org.jdbi.v3.sqlobject.statement.SqlQuery) Before(org.junit.Before) Jdbi(org.jdbi.v3.core.Jdbi) HandlerFactory(org.jdbi.v3.sqlobject.HandlerFactory) Handler(org.jdbi.v3.sqlobject.Handler) Method(java.lang.reflect.Method) Before(org.junit.Before)

Example 9 with Handle

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

the class TestUseSqlParser method setUp.

@Before
public void setUp() throws Exception {
    Jdbi db = dbRule.getJdbi();
    // this is the default, but be explicit for sake of clarity in test
    db.setSqlParser(new ColonPrefixSqlParser());
    handle = db.open();
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) ColonPrefixSqlParser(org.jdbi.v3.core.statement.ColonPrefixSqlParser) Before(org.junit.Before)

Example 10 with Handle

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

the class TestVavrMapCollectorWithDB method testSingleInstanceAssignmentWithSelectedKeyValue_shouldSucceed.

@Test
public void testSingleInstanceAssignmentWithSelectedKeyValue_shouldSucceed() {
    Handle handle = dbRule.getSharedHandle().configure(MapEntryMappers.class, c -> c.setKeyColumn("key_c").setValueColumn("val_c"));
    Optional<Tuple2<String, String>> valueMap = handle.createQuery("select val_c, key_c from keyval").mapTo(new GenericType<Tuple2<String, String>>() {
    }).findFirst();
    assertThat(valueMap).isNotEmpty();
    assertThat(valueMap.get()).isEqualTo(Tuple.of(KEY_PREFIX + 0, VAL_PREFIX + 1));
}
Also used : GenericType(org.jdbi.v3.core.generic.GenericType) Tuple2(io.vavr.Tuple2) 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