@Inject public MysqlDaoProvider(DbResourceGroupConfig config) { requireNonNull(config, "DbResourceGroupConfig is null"); MysqlDataSource dataSource = new MysqlDataSource(); dataSource.setURL(requireNonNull(config.getConfigDbUrl(), "resource-groups.config-db-url is null")); this.dao = Jdbi.create(dataSource) .installPlugin(new SqlObjectPlugin()) .onDemand(ResourceGroupsDao.class); }
.installPlugin(new SqlObjectPlugin()) .onDemand(VerifierDao.class);
/** * Overridable function to allow extra customization of the created {@link Jdbi} * instance. * * <p> * If this is overridden it is strongly recommend that * {@code super.configure(jdbi, configuration)} is invoked before any other * changes are made if you intend to use the default as a base so that the * customized settings will supersede the defaults * </p> * * @param jdbi */ protected void configure(final Jdbi jdbi) { jdbi.installPlugin(new SqlObjectPlugin()); jdbi.installPlugin(new JodaTimePlugin()); jdbi.installPlugin(new GuavaPlugin()); } }
@Inject public H2DaoProvider(DbResourceGroupConfig config) { JdbcDataSource ds = new JdbcDataSource(); ds.setURL(requireNonNull(config.getConfigDbUrl(), "resource-groups.config-db-url is null")); // TODO: this should use onDemand() this.dao = Jdbi.create(ds) .installPlugin(new SqlObjectPlugin()) .open() .attach(H2ResourceGroupsDao.class); }
@Test public void testCorrectExceptionIfUnableToConnectOnDemand() { assertThatThrownBy(() -> Jdbi.create("jdbc:mysql://invalid.invalid/test", "john", "scott") .installPlugin(new SqlObjectPlugin()) .onDemand(Spiffy.class) .findNameById(1)).isInstanceOf(ConnectionException.class); }
@Test public void testCorrectExceptionIfUnableToConnectOnAttach() { assertThatThrownBy(() -> Jdbi.create("jdbc:mysql://invalid.invalid/test", "john", "scott") .installPlugin(new SqlObjectPlugin()) .open() .attach(Spiffy.class)).isInstanceOf(ConnectionException.class); }
@Test public void testCorrectExceptionIfUnableToConnectOnOpen() { assertThatThrownBy(() -> Jdbi.create("jdbc:mysql://invalid.invalid/test", "john", "scott") .installPlugin(new SqlObjectPlugin()) .open() .attach(Spiffy.class)).isInstanceOf(ConnectionException.class); }
@Before public void setUp() { db = Jdbi.create("jdbc:hsqldb:mem:" + UUID.randomUUID(), "username", "password") .installPlugin(new SqlObjectPlugin()); db.useHandle(handle -> handle.execute("create table something (id identity primary key, name varchar(32))")); }
public static JdbiRule rule() { return JdbiRule.embeddedPostgres().withPlugin(new SqlObjectPlugin()).withPlugin(new PostgresPlugin()); } }
@Before public void setUp() { JdbcDataSource ds = new JdbcDataSource(); ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID())); db = Jdbi.create(ds); db.installPlugin(new SqlObjectPlugin()); handle = db.open(); handle.execute("create table something (id int primary key, name varchar(100))"); }
@Before public void setUp() { final JdbcDataSource ds = new JdbcDataSource() { private static final long serialVersionUID = 1L; @Override public Connection getConnection() { throw new UnsupportedOperationException(); } }; db = Jdbi.create(ds); db.installPlugin(new SqlObjectPlugin()); dao = db.onDemand(UselessDao.class); }
@Before public void setUp() { ds = new JdbcDataSource(); // in MVCC mode h2 doesn't shut down immediately on all connections closed, so need random db name ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID())); db = Jdbi.create(ds); db.installPlugin(new SqlObjectPlugin()); handle = db.open(); handle.execute("create table something (id int primary key, name varchar(100))"); db.installPlugin(tracker); }
@Before public void setUp() { JdbcDataSource ds = new JdbcDataSource(); // in MVCC mode h2 doesn't shut down immediately on all connections closed, so need random db name ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID())); db = Jdbi.create(ds); db.installPlugin(new SqlObjectPlugin()); db.registerRowMapper(new SomethingMapper()); handle = db.open(); handle.execute("create table something (id int primary key, name varchar(100))"); }
@Before public void setUp() { db = Jdbi.create("jdbc:h2:mem:" + UUID.randomUUID()); db.installPlugin(new SqlObjectPlugin()); handle = db.open(); handle.createUpdate( "create table foo (id int, bar varchar(100) default null);") .execute(); dao = db.onDemand(MyDAO.class); }
@Before public void setUp() { final JdbcDataSource ds = new JdbcDataSource() { private static final long serialVersionUID = 1L; @Override public Connection getConnection() throws SQLException { final Connection real = super.getConnection(); return (Connection) Proxy.newProxyInstance(real.getClass().getClassLoader(), new Class<?>[] {Connection.class}, new TxnIsolationCheckingInvocationHandler(real)); } }; // in MVCC mode h2 doesn't shut down immediately on all connections closed, so need random db name ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID())); db = Jdbi.create(ds); db.installPlugin(new SqlObjectPlugin()); db.registerRowMapper(new SomethingMapper()); handle = db.open(); handle.execute("create table something (id int primary key, name varchar(100))"); }
@Before public void setUp() { JdbcDataSource ds = new JdbcDataSource(); ds.setURL("jdbc:h2:mem:" + UUID.randomUUID()); db = Jdbi.create(ds); db.installPlugin(new SqlObjectPlugin()); db.registerRowMapper(new SomethingMapper()); handle = db.open(); handle.execute("create table something (id int primary key, name varchar(100))"); }
@Before public void before() { final Jdbi db = dbRule.getJdbi(); db.installPlugin(new SqlObjectPlugin()); db.registerRowMapper(new SomethingMapper()); handle = db.open(); handle.execute("insert into something(id, name) values(1, '1')"); handle.execute("insert into something(id, name) values(2, '2')"); // "control group" element that should *not* be returned by the queries handle.execute("insert into something(id, name) values(3, '3')"); expectedSomethings = Arrays.asList(new Something(1, "1"), new Something(2, "2")); }
@Before public void before() { final Jdbi db = dbRule.getJdbi(); db.installPlugin(new SqlObjectPlugin()); db.registerRowMapper(new SomethingMapper()); handle = db.open(); handle.execute("insert into something(id, name) values(1, '1')"); handle.execute("insert into something(id, name) values(2, '2')"); // "control group" element that should *not* be returned by the queries handle.execute("insert into something(id, name) values(3, '3')"); expectedSomethings = Arrays.asList(new Something(1, "1"), new Something(2, "2")); }
@Before public void before() { final Jdbi db = dbRule.getJdbi(); db.installPlugin(new SqlObjectPlugin()); db.registerRowMapper(new SomethingMapper()); handle = db.open(); handle.execute("insert into something(id, name) values(1, '1')"); handle.execute("insert into something(id, name) values(2, '2')"); // "control group" element that should *not* be returned by the queries handle.execute("insert into something(id, name) values(3, '3')"); expectedSomethings = Arrays.asList(new Something(1, "1"), new Something(2, "2")); }
@Test public void sqlObject() { // tag::sqlobject-usage[] Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test"); jdbi.installPlugin(new SqlObjectPlugin()); // Jdbi implements your interface based on annotations List<User> userNames = jdbi.withExtension(UserDao.class, dao -> { dao.createTable(); dao.insertPositional(0, "Alice"); dao.insertPositional(1, "Bob"); dao.insertNamed(2, "Clarice"); dao.insertBean(new User(3, "David")); return dao.listUsers(); }); assertThat(userNames).containsExactly( new User(0, "Alice"), new User(1, "Bob"), new User(2, "Clarice"), new User(3, "David")); // end::sqlobject-usage[] }