Search in sources :

Example 1 with GenericType

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

the class TestVavrMapCollectorWithDB method testSingleInstanceAssignmentWithSelectedKeyValue_shouldSucceed.

@Test
public void testSingleInstanceAssignmentWithSelectedKeyValue_shouldSucceed() {
    Handle handle = dbRule.getSharedHandle().configure(MapEntryMappers.class, c -> c.setKeyColumn("key_c").setValueColumn("val_c"));
    Optional<Tuple2<String, String>> valueMap = handle.createQuery("select val_c, key_c from keyval").mapTo(new GenericType<Tuple2<String, String>>() {
    }).findFirst();
    assertThat(valueMap).isNotEmpty();
    assertThat(valueMap.get()).isEqualTo(Tuple.of(KEY_PREFIX + 0, VAL_PREFIX + 1));
}
Also used : GenericType(org.jdbi.v3.core.generic.GenericType) Tuple2(io.vavr.Tuple2) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 2 with GenericType

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

the class TestVavrTupleRowMapperFactoryWithDB method testMapTuple3WithExtraSpecifiedColumn_shouldSucceed.

@Test
public void testMapTuple3WithExtraSpecifiedColumn_shouldSucceed() throws SQLException {
    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);
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) GenericType(org.jdbi.v3.core.generic.GenericType) Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 3 with GenericType

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

the class TestVavrTupleRowMapperFactoryWithDB method testMapTuple2UsingRegisteredRowMappers_shouldSucceed.

@Test
public void testMapTuple2UsingRegisteredRowMappers_shouldSucceed() throws SQLException {
    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));
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) GenericType(org.jdbi.v3.core.generic.GenericType) Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 4 with GenericType

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

the class TestVavrTupleRowMapperFactoryWithDB method testMapTuple2HavingOnlyOneRowMapper_shouldFail.

@Test
public void testMapTuple2HavingOnlyOneRowMapper_shouldFail() throws SQLException {
    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");
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) Tuple2(io.vavr.Tuple2) NoSuchMapperException(org.jdbi.v3.core.mapper.NoSuchMapperException) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 5 with GenericType

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

the class TestVavrTupleRowMapperFactoryWithDB method testMapTuple1UsingRegisteredRowMapper_shouldSucceed.

@Test
public void testMapTuple1UsingRegisteredRowMapper_shouldSucceed() throws SQLException {
    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"));
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) GenericType(org.jdbi.v3.core.generic.GenericType) Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) 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