org.sfm.jooq.SfmRecordMapperProvider Maven / Gradle / Ivy
Show all versions of simpleFlatMapper Show documentation
package org.sfm.jooq;
import org.jooq.*;
import org.sfm.map.Mapper;
import org.sfm.map.impl.MapperCache;
import org.sfm.map.impl.TargetColumnsMapperKey;
/**
* Integration point with jooq.
* Provide a JooqRecordMapper backed by an Sfm {@link org.jooq.Record} {@link org.sfm.map.Mapper}
*/
public class SfmRecordMapperProvider implements RecordMapperProvider {
private final MapperCache> mapperCache = new MapperCache>();
@Override
public RecordMapper provide(RecordType recordType, Class extends E> type) {
TargetColumnsMapperKey key = getMapperKey(recordType, type);
@SuppressWarnings("unchecked")
Mapper mapper = (Mapper) mapperCache.get(key);
if (mapper == null) {
JooqMapperBuilder mapperBuilder = new JooqMapperBuilder(type);
int i = 0;
for(Field> field : recordType.fields()) {
mapperBuilder.addField(new JooqFieldKey(field, i++));
}
mapper = mapperBuilder.mapper();
mapperCache.add(key, mapper);
}
return new JooqRecordMapperWrapper(mapper);
}
private TargetColumnsMapperKey getMapperKey(RecordType recordType, Class> type) {
String[] columns = new String[recordType.size()];
int i = 0;
for(Field> field : recordType.fields()) {
columns[i++] = field.getName();
}
return new TargetColumnsMapperKey(type, columns);
}
}