@Override public void configureForType(ConfigRegistry registry, Annotation annotation, Class<?> sqlObjectType) { RegisterFieldMapper registerFieldMapper = (RegisterFieldMapper) annotation; Class<?> type = registerFieldMapper.value(); String prefix = registerFieldMapper.prefix(); RowMappers mappers = registry.get(RowMappers.class); if (prefix.isEmpty()) { mappers.register(FieldMapper.factory(type)); } else { mappers.register(FieldMapper.factory(type, prefix)); } }
@Test public void testNoRecognizedColumns() { Handle handle = dbRule.getSharedHandle(); assertThatThrownBy(() -> handle .registerRowMapper(FieldMapper.factory(NullableNestedThing.class)) .select("SELECT 'foo' bar") .mapTo(NullableNestedThing.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class); }
@Before public void setUp() { Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(FieldMapper.factory(TestBindBeanList.Thing.class)); handle.execute("create table thing (id identity primary key, foo varchar(50), bar varchar(50), baz varchar(50))"); }
@Test public void testNestedHalfPresent() { Handle handle = dbRule.getSharedHandle(); assertThat(handle .registerRowMapper(FieldMapper.factory(NullableNestedThing.class)) .select("SELECT 42 as testValue, '3' as s") .mapTo(NullableNestedThing.class) .findOnly()) .extracting("testValue", "nested.i", "nested.s") .containsExactly(42, null, "3"); }
@Test public void testNullableColumnAbsentButNestedPresent() { Handle handle = dbRule.getSharedHandle(); assertThat(handle .registerRowMapper(FieldMapper.factory(NullableNestedThing.class)) .select("SELECT 's' s, 1 i") .mapTo(NullableNestedThing.class) .findOnly()) .extracting("testValue", "nested.s", "nested.i") .containsExactly(null, "s", 1); }
@Before public void setUp() { handle = dbRule.getSharedHandle(); handle.execute("create table thing (id identity primary key, foo varchar(50), bar varchar(50), baz varchar(50))"); handle.execute("insert into thing (id, foo, bar, baz) values (?, ?, ?, ?)", 1, "foo1", "bar1", "baz1"); handle.execute("insert into thing (id, foo, bar, baz) values (?, ?, ?, ?)", 2, "foo2", "bar2", "baz2"); handle.registerRowMapper(FieldMapper.factory(Thing.class)); }
@Test public void testNestedPresent() { Handle handle = dbRule.getSharedHandle(); assertThat(handle .registerRowMapper(FieldMapper.factory(NullableNestedThing.class)) .select("SELECT 42 as testValue, 2 as i, '3' as s") .mapTo(NullableNestedThing.class) .findOnly()) .extracting("testValue", "nested.i", "nested.s") .containsExactly(42, 2, "3"); }
@Test public void testNestedAbsent() { Handle handle = dbRule.getSharedHandle(); assertThat(handle .registerRowMapper(FieldMapper.factory(NullableNestedThing.class)) .select("SELECT 42 as testValue") .mapTo(NullableNestedThing.class) .findOnly()) .extracting("testValue", "nested") .containsExactly(42, null); }
@Before public void setUp() { handle = dbRule.getSharedHandle(); handle.registerRowMapper(FieldMapper.factory(Thing.class)); handle.execute("create table thing (id identity primary key, foo varchar(50), bar varchar(50), baz varchar(50))"); handle.execute("insert into thing (id, foo, bar, baz) values (?, ?, ?, ?)", 1, "foo1", "bar1", "baz1"); handle.execute("insert into thing (id, foo, bar, baz) values (?, ?, ?, ?)", 2, "foo2", "bar2", "baz2"); }
@Test public void testNested() { Handle handle = dbRule.getSharedHandle(); handle.execute("insert into something (id, name) values (1, 'foo')"); assertThat(handle .registerRowMapper(FieldMapper.factory(NestedThing.class)) .select("SELECT id, name FROM something") .mapTo(NestedThing.class) .findOnly()) .extracting("nested.i", "nested.s") .containsExactly(1, "foo"); }
@Test public void testNestedPrefix() { Handle handle = dbRule.getSharedHandle(); handle.execute("insert into something (id, name) values (1, 'foo')"); assertThat(handle .registerRowMapper(FieldMapper.factory(NestedPrefixThing.class)) .select("select id nested_id, name nested_name from something") .mapTo(NestedPrefixThing.class) .findOnly()) .extracting("nested.i", "nested.s") .containsExactly(1, "foo"); }
@Test public void mapFieldsQualified() { dbRule.getJdbi() .registerColumnMapper(new ReversedStringMapper()) .registerRowMapper(FieldMapper.factory(QualifiedFieldThing.class)) .useHandle(handle -> { handle.execute("INSERT INTO something (id, name) VALUES (1, 'abc')"); assertThat(handle.select("SELECT * FROM something") .mapTo(QualifiedFieldThing.class) .findOnly()) .isEqualTo(new QualifiedFieldThing(1, "cba")); }); }
@Test public void testNestedStrict() { Handle handle = dbRule.getSharedHandle(); handle.getConfig(ReflectionMappers.class).setStrictMatching(true); handle.registerRowMapper(FieldMapper.factory(NestedThing.class)); handle.execute("insert into something (id, name) values (1, 'foo')"); assertThat(handle .registerRowMapper(FieldMapper.factory(NestedThing.class)) .select("select id, name from something") .mapTo(NestedThing.class) .findOnly()) .extracting("nested.i", "nested.s") .containsExactly(1, "foo"); assertThatThrownBy(() -> handle .createQuery("select id, name, 1 as other from something") .mapTo(NestedThing.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match fields for columns: [other]"); }
@Before public void setUp() { handle = dbRule.getSharedHandle(); handle.registerRowMapper(FieldMapper.factory(Thing.class)); handle.execute("create table thing (id identity primary key, foo varchar(50), bar varchar(50), baz varchar(50))"); handle.execute("insert into thing (id, foo, bar, baz) values (?, ?, ?, ?)", 1, "foo1", "bar1", "baz1"); handle.execute("insert into thing (id, foo, bar, baz) values (?, ?, ?, ?)", 2, "foo2", "bar2", "baz2"); handle.execute("insert into thing (id, foo, bar, baz) values (?, ?, ?, ?)", 3, "foo3", "bar3", "baz3"); }
@Test public void testNestedPrefixStrict() { Handle handle = dbRule.getSharedHandle(); handle.getConfig(ReflectionMappers.class).setStrictMatching(true); handle.registerRowMapper(FieldMapper.factory(NestedPrefixThing.class)); handle.execute("insert into something (id, name, integerValue) values (1, 'foo', 5)"); // three, sir! assertThat(handle .createQuery("select id nested_id, name nested_name, integerValue from something") .mapTo(NestedPrefixThing.class) .findOnly()) .extracting("nested.i", "nested.s", "integerValue") .containsExactly(1, "foo", 5); assertThatThrownBy(() -> handle .createQuery("select id nested_id, name nested_name, 1 as other from something") .mapTo(NestedPrefixThing.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match fields for columns: [other]"); assertThatThrownBy(() -> handle .createQuery("select id nested_id, name nested_name, 1 as nested_other from something") .mapTo(NestedPrefixThing.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match fields for columns: [nested_other]"); }
@Test public void testBindListWithHashPrefixParser() { Jdbi jdbi = Jdbi.create(dbRule.getConnectionFactory()); jdbi.setSqlParser(new HashPrefixSqlParser()); jdbi.useHandle(handle -> { handle.registerRowMapper(FieldMapper.factory(Thing.class)); handle.createUpdate("insert into thing (<columns>) values (<values>)") .defineList("columns", "id", "foo") .bindList("values", 3, "abc") .execute(); List<Thing> list = handle.createQuery("select id, foo from thing where id in (<ids>)") .bindList("ids", 1, 3) .mapTo(Thing.class) .list(); assertThat(list) .extracting(Thing::getId, Thing::getFoo, Thing::getBar, Thing::getBaz) .containsExactly( tuple(1, "foo1", null, null), tuple(3, "abc", null, null)); }); }
@Before public void setUp() { Handle handle = dbRule.getSharedHandle(); handle.registerRowMapper(FieldMapper.factory(TestBindBeanList.Thing.class)); handle.execute("create table thing (id identity primary key, foo varchar(50), bar varchar(50), baz varchar(50))"); }