Search in sources :

Example 1 with Jdbi

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

use of org.jdbi.v3.core.Jdbi 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 3 with Jdbi

use of org.jdbi.v3.core.Jdbi 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 4 with Jdbi

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

the class TestSqlLocator method testLocateConfigDriven.

@Test
public void testLocateConfigDriven() {
    Jdbi jdbi = dbRule.getJdbi();
    jdbi.useHandle(h -> {
        h.execute("create table something (id int, name text)");
        h.execute("insert into something (id, name) values (?, ?)", 2, "Alice");
        h.execute("insert into something (id, name) values (?, ?)", 1, "Bob");
    });
    jdbi.getConfig(SqlObjects.class).setSqlLocator((type, method, config) -> config.get(TestConfig.class).sql);
    jdbi.getConfig(TestConfig.class).sql = "select * from something order by id";
    assertThat(jdbi.withExtension(TestDao.class, TestDao::list)).containsExactly(new Something(1, "Bob"), new Something(2, "Alice"));
    jdbi.getConfig(TestConfig.class).sql = "select * from something order by name";
    assertThat(jdbi.withExtension(TestDao.class, TestDao::list)).containsExactly(new Something(2, "Alice"), new Something(1, "Bob"));
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) SqlObjects(org.jdbi.v3.sqlobject.SqlObjects) Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 5 with Jdbi

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

the class JdbiFactoryBean method getObject.

/**
 * See {@link org.springframework.beans.factory.FactoryBean#getObject}
 */
@Override
public Jdbi getObject() throws Exception {
    final Jdbi jdbi = Jdbi.create(() -> DataSourceUtils.getConnection(dataSource));
    if (autoInstallPlugins) {
        jdbi.installPlugins();
    }
    plugins.forEach(jdbi::installPlugin);
    globalDefines.forEach(jdbi::define);
    return jdbi;
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi)

Aggregations

Jdbi (org.jdbi.v3.core.Jdbi)47 Test (org.junit.Test)32 Handle (org.jdbi.v3.core.Handle)12 Something (org.jdbi.v3.core.Something)6 Query (org.jdbi.v3.core.statement.Query)6 SqlObjectPlugin (org.jdbi.v3.sqlobject.SqlObjectPlugin)6 Before (org.junit.Before)6 SQLException (java.sql.SQLException)5 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)5 Update (org.jdbi.v3.core.statement.Update)3 BeforeClass (org.junit.BeforeClass)3 TypeLiteral (com.google.inject.TypeLiteral)2 JdbiFactory (io.dropwizard.jdbi3.JdbiFactory)2 Method (java.lang.reflect.Method)2 Stream (java.util.stream.Stream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 SqlStatements (org.jdbi.v3.core.statement.SqlStatements)2 Test (org.junit.jupiter.api.Test)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 HealthCheckRegistry (com.codahale.metrics.health.HealthCheckRegistry)1