Search in sources :

Example 1 with HandlerFactory

use of org.jdbi.v3.sqlobject.HandlerFactory 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)

Aggregations

Method (java.lang.reflect.Method)1 Optional (java.util.Optional)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Handle (org.jdbi.v3.core.Handle)1 Jdbi (org.jdbi.v3.core.Jdbi)1 Something (org.jdbi.v3.core.Something)1 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)1 H2DatabaseRule (org.jdbi.v3.core.rule.H2DatabaseRule)1 Handler (org.jdbi.v3.sqlobject.Handler)1 HandlerFactory (org.jdbi.v3.sqlobject.HandlerFactory)1 Handlers (org.jdbi.v3.sqlobject.Handlers)1 SqlObjectPlugin (org.jdbi.v3.sqlobject.SqlObjectPlugin)1 Bind (org.jdbi.v3.sqlobject.customizer.Bind)1 BindBean (org.jdbi.v3.sqlobject.customizer.BindBean)1 SqlQuery (org.jdbi.v3.sqlobject.statement.SqlQuery)1 SqlUpdate (org.jdbi.v3.sqlobject.statement.SqlUpdate)1 Transaction (org.jdbi.v3.sqlobject.transaction.Transaction)1 Before (org.junit.Before)1 Rule (org.junit.Rule)1