@Test public void keyValueColumns() { h.execute("create table config (key varchar, value varchar)"); h.prepareBatch("insert into config (key, value) values (?, ?)") .add("foo", "123") .add("bar", "xyz") .execute(); // tag::keyValue[] Map<String, String> map = h.createQuery("select key, value from config") .setMapKeyColumn("key") .setMapValueColumn("value") .collectInto(new GenericType<Map<String, String>>() {}); // end::keyValue[] assertThat(map).containsOnly( entry("foo", "123"), entry("bar", "xyz")); }
@Test public void testNonUniqueIndexWithMultimap() { Handle h = dbRule.getSharedHandle(); h.execute("create table user (id int, name varchar)"); h.prepareBatch("insert into user (id, name) values (?, ?)") .add(1, "alice") .add(2, "bob") .add(3, "alice") .execute(); Multimap<String, User> usersByName = h.createQuery("select * from user") .setMapKeyColumn("name") .registerRowMapper(ConstructorMapper.factory(User.class)) .collectInto(new GenericType<Multimap<String, User>>() {}); assertThat(usersByName.apply("alice")).hasSize(2).containsExactly( new User(1, "alice"), new User(3, "alice") ); assertThat(usersByName.apply("bob")).hasSize(1).containsExactly( new User(2, "bob") ); }
@Test public void uniqueIndex() { h.execute("create table user (id int, name varchar)"); h.prepareBatch("insert into user (id, name) values (?, ?)") .add(1, "alice") .add(2, "bob") .add(3, "cathy") .add(4, "dilbert") .execute(); // tag::uniqueIndex[] Map<Integer, User> map = h.createQuery("select * from user") .setMapKeyColumn("id") .registerRowMapper(ConstructorMapper.factory(User.class)) .collectInto(new GenericType<Map<Integer, User>>() {}); // end::uniqueIndex[] assertThat(map).containsOnly( entry(1, new User(1, "alice")), entry(2, new User(2, "bob")), entry(3, new User(3, "cathy")), entry(4, new User(4, "dilbert"))); }
@Test public void keyValueColumns() { h.execute("create table config (key varchar, value varchar)"); h.prepareBatch("insert into config (key, value) values (?, ?)") .add("foo", "123") .add("foo", "456") .add("bar", "xyz") .execute(); // tag::keyValue[] Multimap<String, String> map = h.createQuery("select key, value from config") .setMapKeyColumn("key") .setMapValueColumn("value") .collectInto(new GenericType<Multimap<String, String>>() {}); // end::keyValue[] Multimap<String, String> expected = ImmutableListMultimap.<String, String>builder() .putAll("foo", "123", "456") .put("bar", "xyz") .build(); assertThat(map).isEqualTo(expected); }
/** * from {@link org.jdbi.v3.core.mapper.MapEntryMapperTest} */ @Test public void uniqueIndex() { Handle h = dbRule.getSharedHandle(); h.execute("create table user (id int, name varchar)"); h.prepareBatch("insert into user (id, name) values (?, ?)") .add(1, "alice") .add(2, "bob") .add(3, "cathy") .add(4, "dilbert") .execute(); Map<Integer, User> map = h.createQuery("select * from user") .setMapKeyColumn("id") .registerRowMapper(ConstructorMapper.factory(User.class)) .collectInto(new GenericType<Map<Integer, User>>() {}); assertThat(map).containsOnly( Tuple.of(1, new User(1, "alice")), Tuple.of(2, new User(2, "bob")), Tuple.of(3, new User(3, "cathy")), Tuple.of(4, new User(4, "dilbert"))); }
@Test public void index() { h.execute("create table user (id int, manager_id int, name varchar)"); h.prepareBatch("insert into user (id, manager_id, name) values (?, ?, ?)") .add(1, 0, "alice") .add(2, 1, "bob") .add(3, 1, "cathy") .add(4, 3, "dilbert") .execute(); // tag::index[] Multimap<Integer, User> map = h.createQuery("select id, manager_id, name from user") .setMapKeyColumn("manager_id") .registerRowMapper(ConstructorMapper.factory(User.class)) .collectInto(new GenericType<Multimap<Integer, User>>() {}); // end::index[] Multimap<Integer, User> expected = ImmutableListMultimap.<Integer, User>builder() .put(0, new User(1, "alice")) .putAll(1, new User(2, "bob"), new User(3, "cathy")) .put(3, new User(4, "dilbert")) .build(); assertThat(map).isEqualTo(expected); }
.useHandle(handle -> { Map<User, Phone> map = handle.createQuery(sql) .setMapKeyColumn(null) .setMapValueColumn(null) .registerRowMapper(ConstructorMapper.factory(User.class, "u"))
@Test public void keyValueColumns() { h.execute("create table config (key varchar, value varchar)"); h.prepareBatch("insert into config (key, value) values (?, ?)") .add("foo", "123") .add("foo", "456") .add("bar", "xyz") .execute(); // tag::keyValue[] Multimap<String, String> map = h.createQuery("select key, value from config") .setMapKeyColumn("key") .setMapValueColumn("value") .collectInto(new GenericType<Multimap<String, String>>() {}); // end::keyValue[] Multimap<String, String> expected = ImmutableListMultimap.<String, String>builder() .putAll("foo", "123", "456") .put("bar", "xyz") .build(); assertThat(map).isEqualTo(expected); }
@Test public void index() { h.execute("create table user (id int, manager_id int, name varchar)"); h.prepareBatch("insert into user (id, manager_id, name) values (?, ?, ?)") .add(1, 0, "alice") .add(2, 1, "bob") .add(3, 1, "cathy") .add(4, 3, "dilbert") .execute(); // tag::index[] Multimap<Integer, User> map = h.createQuery("select id, manager_id, name from user") .setMapKeyColumn("manager_id") .registerRowMapper(ConstructorMapper.factory(User.class)) .collectInto(new GenericType<Multimap<Integer, User>>() {}); // end::index[] Multimap<Integer, User> expected = ImmutableListMultimap.<Integer, User>builder() .put(0, new User(1, "alice")) .putAll(1, new User(2, "bob"), new User(3, "cathy")) .put(3, new User(4, "dilbert")) .build(); assertThat(map).isEqualTo(expected); }