@Before public void setUp() { dbRule.getJdbi().registerRowMapper(new SomethingMapper()); handle = dbRule.getSharedHandle(); handle.registerRowMapper(new SomethingMapper()); }
@Before public void setUp() { handle = dbRule.getSharedHandle(); handle.registerRowMapper(new SomethingMapper()); }
@Test public void testMapTuple2UsingRegisteredRowMappersShouldSucceed() { Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(new SomethingMapper()); handle.registerRowMapper(SomethingValues.class, (rs, ctx) -> new SomethingValues(rs.getInt("integerValue"), rs.getInt("intValue"))); Tuple2<Something, SomethingValues> result = handle .createQuery("select * from something where id = 2") .mapTo(new GenericType<Tuple2<Something, SomethingValues>>() {}) .findOnly(); assertThat(result._1).isEqualTo(new Something(2, "brian")); assertThat(result._2).isEqualTo(new SomethingValues(101, 102)); }
@Test public void testMapTuple3WithExtraSpecifiedColumnShouldSucceed() { Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(new SomethingMapper()); handle.configure(TupleMappers.class, c -> c.setColumn(2, "integerValue").setColumn(3, "intValue")); Tuple3<Something, Integer, Integer> result = handle .createQuery("select * from something where id = 1") .mapTo(new GenericType<Tuple3<Something, Integer, Integer>>() {}) .findOnly(); assertThat(result._1).isEqualTo(new Something(1, "eric")); assertThat(result._2).isEqualTo(99); assertThat(result._3).isEqualTo(100); }
@Test public void testMapTuple3WithoutSpecifiedColumnShouldFail() { Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(new SomethingMapper()); assertThatThrownBy(() -> handle .createQuery("select * from something where id = 1") .mapTo(new GenericType<Tuple3<Integer, Something, Integer>>() {}) .findOnly()) .isInstanceOf(NoSuchMapperException.class) .hasMessageContaining("TupleMappers config class"); }
@Test public void testMapTuple2HavingOnlyOneRowMapperShouldFail() { final Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(new SomethingMapper()); assertThatThrownBy(() -> handle .createQuery("select * from something where id = 1") .mapTo(new GenericType<Tuple2<Something, SomethingValues>>() {}) .findOnly() ).isInstanceOf(NoSuchMapperException.class) .hasMessageContaining("SomethingValues"); }
@Test public void testMapTuple1UsingRegisteredRowMapperShouldSucceed() { Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(new SomethingMapper()); Tuple1<Something> result = handle .createQuery("select id, name from something where id = 1") .mapTo(new GenericType<Tuple1<Something>>() {}) .findOnly(); assertThat(result._1).isEqualTo(new Something(1, "eric")); }
@Test public void testMapTuple3WithOnlyOneSpecifiedColumnShouldFail() { Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(new SomethingMapper()); handle.configure(TupleMappers.class, c -> c.setColumn(1, "integerValue")); assertThatThrownBy(() -> handle .createQuery("select * from something where id = 1") .mapTo(new GenericType<Tuple3<Integer, Something, Integer>>() {}) .findOnly()).isInstanceOf(NoSuchMapperException.class) .isInstanceOf(NoSuchMapperException.class) .hasMessageContaining("TupleMappers config class"); }
@Test public void testRegistered() { dbRule.getSharedHandle().registerRowMapper(new SomethingMapper()); Spiffy s = dbRule.getSharedHandle().attach(Spiffy.class); s.insert(1, "Tatu"); Something t = s.byId(1); assertThat(t).isEqualTo(new Something(1, "Tatu")); }
@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))"); }
@Test public void testRegisterInferredOnJdbi() { db.registerRowMapper(new SomethingMapper()); Something sam = db.withHandle(handle1 -> { handle1.execute("insert into something (id, name) values (18, 'Sam')"); return handle1.createQuery("select id, name from something where id = :id") .bind("id", 18) .mapTo(Something.class) .findOnly(); }); assertThat(sam.getName()).isEqualTo("Sam"); }
@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))"); }
@SqlQuery("select * from something") @RegisterRowMapper(SomethingMapper.class) default List<Something> list() { return getHandle().createQuery("select * from something") .map(new SomethingMapper()) .list(); } }
@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() { handle = h2.getJdbi() .registerRowMapper(new SomethingMapper()) .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")); }
@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 setUp() { Handle h = dbRule.getSharedHandle(); h.execute("CREATE TABLE something_location (id int, location varchar)"); h.execute("INSERT INTO something (id, name) VALUES (1, 'tree')"); h.execute("INSERT INTO something (id, name) VALUES (2, 'apple')"); h.execute("INSERT INTO something_location (id, location) VALUES (1, 'outside')"); h.execute("INSERT INTO something_location (id, location) VALUES (2, 'tree')"); h.execute("INSERT INTO something_location (id, location) VALUES (2, 'pie')"); h.registerRowMapper(new SomethingMapper()); }
@Before public void before() { final Jdbi db = dbRule.getJdbi(); db.registerRowMapper(new SomethingMapper()); db.installPlugin(new SqlObjectPlugin()); handle = db.open(); handle.execute("insert into something(id, name) values(1, null)"); handle.execute("insert into something(id, name) values(2, null)"); handle.execute("insert into something(id, name) values(3, null)"); handle.execute("insert into something(id, name) values(4, null)"); handle.execute("insert into something(id, name) values(5, 'bla')"); handle.execute("insert into something(id, name) values(6, 'null')"); handle.execute("insert into something(id, name) values(7, '')"); }