Skip to content
This repository has been archived by the owner on Sep 13, 2021. It is now read-only.

Commit

Permalink
Add support for JDBI 3, drop JDBI 2
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-shpak committed Nov 7, 2018
1 parent 26e2c5a commit c91fcad
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 30 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ repositories {
dependencies {
compile "io.dropwizard:dropwizard-core:$dropwizardVersion"
compile "org.glassfish.hk2:hk2-extras:2.5.0-b32"
compileOnly "io.dropwizard:dropwizard-jdbi:$dropwizardVersion"
compileOnly "io.dropwizard:dropwizard-jdbi3:$dropwizardVersion"

testCompile "junit:junit:$junitVersion"
testCompile "com.h2database:h2:1.4.197"
testCompile "io.dropwizard:dropwizard-jdbi:$dropwizardVersion"
testCompile "io.dropwizard:dropwizard-jdbi3:$dropwizardVersion"
}

compileJava {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import org.glassfish.hk2.api.Factory;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.skife.jdbi.v2.DBI;
import org.skife.jdbi.v2.Handle;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;

import javax.inject.Inject;
import javax.inject.Provider;
Expand All @@ -15,17 +15,17 @@
@Singleton
public class HandleFactory implements Factory<Handle> {

private final Provider<DBI> dbi;
private final Provider<Jdbi> jdbi;

@Inject
public HandleFactory(Provider<DBI> dbi) {
this.dbi = dbi;
public HandleFactory(Provider<Jdbi> jdbi) {
this.jdbi = jdbi;
}

@Override
@RequestScoped
public Handle provide() {
return dbi.get().open();
return jdbi.get().open();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.dropwizard.db.DatabaseConfiguration;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.skife.jdbi.v2.DBI;
import org.jdbi.v3.core.Jdbi;

import javax.inject.Singleton;
import java.util.Collections;
Expand All @@ -19,7 +19,7 @@
*/
public class JDBIBinder<T extends Configuration> extends AbstractBinder {

private Class<? extends Factory<DBI>> dbiFactory = JDBIFactory.class;
private Class<? extends Factory<Jdbi>> dbiFactory = JDBIFactory.class;
private Class<? extends Factory<Object>> sqlObjectFactory = SqlObjectFactory.class;

private DatabaseConfiguration databaseConfiguration;
Expand All @@ -39,7 +39,7 @@ public JDBIBinder(DatabaseConfiguration<T> databaseConfiguration) {
* @return self
* @see JDBIFactory
*/
public JDBIBinder<T> setDBIFactory(Class<? extends Factory<DBI>> dbiFactory) {
public JDBIBinder<T> setDBIFactory(Class<? extends Factory<Jdbi>> dbiFactory) {
this.dbiFactory = requireNonNull(dbiFactory);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import io.dropwizard.Configuration;
import io.dropwizard.db.DatabaseConfiguration;
import io.dropwizard.db.PooledDataSourceFactory;
import io.dropwizard.jdbi.DBIFactory;
import io.dropwizard.jdbi3.JdbiFactory;
import io.dropwizard.setup.Environment;
import org.glassfish.hk2.api.Factory;
import org.skife.jdbi.v2.DBI;
import org.jdbi.v3.core.Jdbi;

import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class JDBIFactory implements Factory<DBI> {
public class JDBIFactory implements Factory<Jdbi> {

private final DBIFactory factory = new DBIFactory();
private final JdbiFactory factory = new JdbiFactory();
private final String name = getClass().getSimpleName();

private final Environment environment;
Expand All @@ -31,13 +31,13 @@ public JDBIFactory(Environment environment, Configuration configuration,

@Override
@Singleton
public DBI provide() {
public Jdbi provide() {
PooledDataSourceFactory dataSourceFactory = databaseConfiguration.getDataSourceFactory(configuration);
return factory.build(environment, dataSourceFactory, name);
}

@Override
public void dispose(DBI instance) {
public void dispose(Jdbi instance) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.api.Self;
import org.skife.jdbi.v2.DBI;
import org.jdbi.v3.core.Jdbi;

import javax.inject.Inject;
import javax.inject.Provider;
Expand All @@ -20,24 +20,24 @@
@Singleton
public class SqlObjectFactory implements Factory<Object> {

private final Provider<DBI> dbi;
private final Provider<Jdbi> jdbi;
private final ActiveDescriptor<Factory> activeDescriptor;

@Inject
public SqlObjectFactory(Provider<DBI> dbi, @Self ActiveDescriptor<Factory> activeDescriptor) {
public SqlObjectFactory(Provider<Jdbi> dbi, @Self ActiveDescriptor<Factory> activeDescriptor) {
this.activeDescriptor = activeDescriptor;
this.dbi = dbi;
this.jdbi = dbi;
}

@Override
public Object provide() {
Class<?> daoInterface = extractDaoType(activeDescriptor);
return dbi.get().onDemand(daoInterface);
return jdbi.get().onDemand(daoInterface);
}

@Override
public void dispose(Object instance) {
dbi.get().close(instance);

}

private Class<?> extractDaoType(ActiveDescriptor<Factory> activeDescriptor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import net.winterly.dropwizard.hk2bundle.health.ExampleHealthCheck;
import net.winterly.dropwizard.hk2bundle.metric.ExampleGauge;
import net.winterly.dropwizard.hk2bundle.spi.DropwizardBinder;
import net.winterly.dropwizard.hk2bundle.validation.InjectValidatorBundle;

public class ExampleAppBinder extends DropwizardBinder {

@Override
protected void configure() {
bundle(InjectValidatorBundle.class);

metric(ExampleGauge.class);
healthCheck(ExampleHealthCheck.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package net.winterly.dropwizard.hk2bundle.jdbi;

import org.glassfish.hk2.api.UseProxy;
import org.skife.jdbi.v2.sqlobject.Bind;
import org.skife.jdbi.v2.sqlobject.SqlQuery;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.statement.SqlQuery;

@UseProxy
public interface ExampleDAO {

@SqlQuery("SELECT :i")
Expand Down

0 comments on commit c91fcad

Please sign in to comment.