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();
}
});
}
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();
}
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();
}
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"));
}
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;
}
Aggregations