arnaudroger / SimpleFlatMapper

Fast and Easy mapping from database and csv to POJO. A java micro ORM, lightweight alternative to iBatis and Hibernate. Fast Csv Parser and Csv Mapper
http://simpleflatmapper.org
MIT License
435 stars 76 forks source link

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

Open LukasHaken opened 4 years ago

LukasHaken commented 4 years ago

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?

arnaudroger commented 4 years ago

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

LukasHaken commented 4 years ago

So, could I ask you, how?

arnaudroger commented 4 years ago

sorry been crazy last couple of weeks. will check today

LukasHaken commented 4 years ago

It's okay :) Thanks

arnaudroger commented 4 years ago

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.

LukasHaken commented 4 years ago

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?

arnaudroger commented 4 years ago

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