@Test public void inlineRowMapper() { // tag::inlineRowMapper[] List<User> users = handle.createQuery("SELECT id, name FROM user ORDER BY id ASC") .map((rs, ctx) -> new User(rs.getInt("id"), rs.getString("name"))) .list(); // end::inlineRowMapper[] assertThat(users).hasSize(4); assertThat(users.get(3).name).isEqualTo("Data"); }
@Test public void testUnusedBinding() { assertThatThrownBy(() -> h.createQuery("select * from something") .bind("id", 1) .collectRows(Collectors.counting()) ).isInstanceOf(UnableToCreateStatementException.class); }
@Test public void testBindOptionalInt() { assertThat(handle.createQuery("SELECT :value") .bind("value", OptionalInt.empty()) .collectInto(OptionalInt.class)) .isEmpty(); assertThat(handle.createQuery("SELECT :value") .bind("value", OptionalInt.of(123)) .collectInto(OptionalInt.class)) .hasValue(123); }
@Override public void evaluate() { assertThat( rule.getHandle() .select("select value from custom_migration_location") .mapTo(String.class) .findOnly()) .isEqualTo("inserted in migration script in a custom location"); } };
@Test public void testExceptionAbortsTransaction() { assertThatThrownBy(() -> h.inTransaction(handle -> { handle.execute("insert into something (id, name) values (?, ?)", 0, "Keith"); throw new IOException(); })) .isInstanceOf(IOException.class); List<Something> r = h.createQuery("select * from something").mapToBean(Something.class).list(); assertThat(r).isEmpty(); }
@Test public void testDynamicBindOptionalEmpty() { List<Something> result = handle.createQuery(SELECT_BY_NAME) .bindByType("name", Optional.empty(), new GenericType<Optional<String>>() {}) .mapToBean(Something.class) .list(); assertThat(result).containsExactly(new Something(1, "eric"), new Something(2, "brian")); }
default void check() throws Exception { Class<StatementContextExtensionMethodDao> extensionMethodDaoClass = StatementContextExtensionMethodDao.class; Method checkMethod = extensionMethodDaoClass.getMethod("check"); ExtensionMethod extensionMethod = getHandle().getExtensionMethod(); assertThat(extensionMethod.getType()).isEqualTo(extensionMethodDaoClass); assertThat(extensionMethod.getMethod()).isEqualTo(checkMethod); extensionMethod = getHandle().createQuery("select * from something").getContext().getExtensionMethod(); assertThat(extensionMethod.getType()).isEqualTo(extensionMethodDaoClass); assertThat(extensionMethod.getMethod()).isEqualTo(checkMethod); } }
@Test public void zoneId() { final ZoneId zone = ZoneId.systemDefault(); h.execute("insert into stuff(z) values (?)", zone); assertThat(h.createQuery("select z from stuff").mapTo(ZoneId.class).findOnly()).isEqualTo(zone); } }
@Test public void testBindMaps() { PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)"); b.add(ImmutableMap.of("id", 0, "name", "Keith")); b.add(ImmutableMap.of("id", 1, "name", "Eric")); b.add(ImmutableMap.of("id", 2, "name", "Brian")); b.execute(); List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list(); assertThat(r).hasSize(3); assertThat(r.get(2).getName()).isEqualTo("Brian"); }
@Test public void testDynamicBindOptionalOfUnregisteredCustomType() { exception.expect(UnsupportedOperationException.class); handle.createQuery(SELECT_BY_NAME) .bindByType("name", Optional.of(new Name("eric")), new GenericType<Optional<Name>>() {}) .mapToBean(Something.class) .list(); }
@Test public void testZipping() { UsesBatching b = handle.attach(UsesBatching.class); List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5); List<String> names = Arrays.asList("David", "Tim", "Mike"); b.zipArgumentsTogether(ids, names); assertThat(b.size()).isEqualTo(3); List<String> insNames = handle.createQuery("select distinct name from something order by name") .mapTo(String.class) .list(); assertThat(insNames).containsExactly("David", "Mike", "Tim"); }
@Test public void testSimpleUpdate() { h.execute("insert into something (id, name) values (1, 'eric')"); h.execute("update something set name = 'cire' where id = 1"); Something eric = h.createQuery("select * from something where id = 1").mapToBean(Something.class).list().get(0); assertThat(eric.getName()).isEqualTo("cire"); }
@Test public void rowMapper() { // tag::rowMapper[] List<User> users = handle.createQuery("SELECT id, name FROM user ORDER BY id ASC") .map(new UserMapper()) .list(); // end::rowMapper[] assertThat(users).hasSize(4); assertThat(users.get(3).name).isEqualTo("Data"); }
@Test public void testDynamicBindOptionalOfUnregisteredCustomType() { exception.expect(UnsupportedOperationException.class); handle.createQuery(SELECT_BY_NAME) .bindByType("name", Optional.of(new Name("eric")), new GenericType<Optional<Name>>() {}) .mapToBean(Something.class) .list(); }
@Test public void testNegativePeriod() { handle.execute("insert into intervals(id, foo) values(?, interval '-3 years -1 month 2 days')", 7); final Period p = handle.createQuery("select foo from intervals where id=?") .bind(0, 7) .mapTo(Period.class) .findOnly(); assertThat(p).isEqualTo(Period.of(-3, -1, 2)); } }