Search in sources :

Example 11 with GenericType

use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.

the class TestGuavaCollectors method testMultimapCollector.

@SuppressWarnings("unchecked")
private <M extends Multimap<Long, String>> void testMultimapCollector(Class<? extends Multimap> erasedType, GenericType<M> genericType) {
    JdbiCollectors registry = dbRule.getJdbi().getConfig(JdbiCollectors.class);
    assertThat(registry.findElementTypeFor(genericType.getType())).contains(new GenericType<Map.Entry<Long, String>>() {
    }.getType());
    Collector<Map.Entry<Long, String>, ?, M> collector = (Collector<Map.Entry<Long, String>, ?, M>) registry.findFor(genericType.getType()).orElseThrow(() -> new IllegalStateException("Missing collector for " + genericType));
    M map = Stream.of(entry(1L, "foo"), entry(2L, "bar")).collect(collector);
    assertThat(map).isInstanceOf(erasedType).containsAllEntriesOf(ImmutableMultimap.of(1L, "foo", 2L, "bar"));
}
Also used : GenericType(org.jdbi.v3.core.generic.GenericType) JdbiCollectors(org.jdbi.v3.core.collector.JdbiCollectors) Collector(java.util.stream.Collector) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 12 with GenericType

use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.

the class TestGuavaOptional method testDynamicBindOptionalEmpty.

@Test
public void testDynamicBindOptionalEmpty() throws Exception {
    List<Something> result = handle.createQuery(SELECT_BY_NAME).bindByType("name", Optional.absent(), new GenericType<Optional<String>>() {
    }).mapToBean(Something.class).list();
    assertThat(result).containsExactly(new Something(1, "eric"), new Something(2, "brian"));
}
Also used : Optional(com.google.common.base.Optional) Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 13 with GenericType

use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.

the class TestVavrMapCollectorWithDB method testNonUniqueIndex_withMultimap.

@Test
public void testNonUniqueIndex_withMultimap() {
    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"));
}
Also used : Multimap(io.vavr.collection.Multimap) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 14 with GenericType

use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.

the class TestVavrMapCollectorWithDB method uniqueIndex.

/**
 * 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")));
}
Also used : HashMap(io.vavr.collection.HashMap) Map(io.vavr.collection.Map) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 15 with GenericType

use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.

the class TestVavrMapCollectorWithDB method testMapCollectorWithGlobalKeyValue_shouldSucceed.

@Test
public void testMapCollectorWithGlobalKeyValue_shouldSucceed() {
    Jdbi jdbiWithKeyColAndValCol = dbRule.getJdbi().setMapKeyColumn("key_c").setMapValueColumn("val_c");
    Boolean executed = jdbiWithKeyColAndValCol.withHandle(h -> {
        HashMap<String, String> valueMap = h.createQuery("select val_c, key_c from keyval").collectInto(new GenericType<HashMap<String, String>>() {
        });
        assertThat(valueMap).containsOnlyElementsOf(expectedMap);
        return true;
    });
    assertTrue(executed);
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) HashMap(io.vavr.collection.HashMap) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 GenericType (org.jdbi.v3.core.generic.GenericType)11 Handle (org.jdbi.v3.core.Handle)10 Something (org.jdbi.v3.core.Something)6 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)6 Collector (java.util.stream.Collector)5 Type (java.lang.reflect.Type)3 Map (java.util.Map)3 GenericTypes.getErasedType (org.jdbi.v3.core.generic.GenericTypes.getErasedType)3 GenericTypes.resolveMapEntryType (org.jdbi.v3.core.generic.GenericTypes.resolveMapEntryType)3 NoSuchMapperException (org.jdbi.v3.core.mapper.NoSuchMapperException)3 Optional (com.google.common.base.Optional)2 BiMap (com.google.common.collect.BiMap)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Tuple2 (io.vavr.Tuple2)2 HashMap (io.vavr.collection.HashMap)2 JdbiCollectors (org.jdbi.v3.core.collector.JdbiCollectors)2 Tuple3 (io.vavr.Tuple3)1 Map (io.vavr.collection.Map)1 Multimap (io.vavr.collection.Multimap)1