SimpleFlatMapper icon indicating copy to clipboard operation
SimpleFlatMapper copied to clipboard

Sfm-jdbc - Read columns from ResultSet by column's name instead of column's index

Open LukasHaken opened this issue 4 years ago • 7 comments

Hi, thanks for great library and also thanks for your fast and great support.

This is question or request about new feature.

I'd like use custom GetterFactory, which will read column from ResultSet by column's name instead of read by column's index. Because creating huge mapper(most column, most association with discriminator) is expensive therefore I use cache of mappers. I'd like to create mapper, which knows to map subset of column and it doesn't matter to order columns in ResultSet. Now I don't know how can i do it, because JdbcMapperBuilder use JdbcKeySourceGetter, which read column by column's index. Does some solution exist?

LukasHaken avatar Mar 17 '20 15:03 LukasHaken

So I believe there is a way, let me confirm how,

arnaudroger avatar Mar 19 '20 12:03 arnaudroger

So, could I ask you, how?

LukasHaken avatar Apr 01 '20 14:04 LukasHaken

sorry been crazy last couple of weeks. will check today

arnaudroger avatar Apr 01 '20 14:04 arnaudroger

It's okay :) Thanks

LukasHaken avatar Apr 01 '20 14:04 LukasHaken

so looking into it it won;t be that straight forward.

You can provide a GetterFactoryProperty

                .addColumnProperty(ConstantPredicate.truePredicate(), new GetterFactoryProperty(new GetterFactory<ResultSet, JdbcColumnKey>() {
                    @Override
                    public <P> Getter<ResultSet, P> newGetter(Type targetType, JdbcColumnKey key, Object... properties) {
                        return new Getter<ResultSet, P>() {
                            @Override
                            public P get(ResultSet target) throws Exception {
                                return target.getObject(key.getOrginalName(), TypeHelper.toClass(targetType));
                            }
                        };
                    }
                }), ResultSet.class)

but then you can't leverage all the convertion logic in the ResultSetGetterFactory if there getter from ResultSetGetterFactory where to provide a indexed access then you could first resolve the column index and then delegate to the IndexedGetter. but does not look possible right now.

arnaudroger avatar Apr 03 '20 13:04 arnaudroger

Thanks. This is exactly my problem. I have many associations with many keys.

I have one question. Are you planning to make support for getter by column's name?

LukasHaken avatar Apr 29 '20 18:04 LukasHaken

It's possible will see if I can do that. It should not be too bad

arnaudroger avatar May 04 '20 08:05 arnaudroger