use of org.simpleflatmapper.map.property.ConverterProperty in project SimpleFlatMapper by arnaudroger.
the class SqlParameterSourceBuilder method build.
private <P> PlaceHolderValueGetter<T> build(JdbcColumnKey key, PropertyMapping<T, P, JdbcColumnKey> pm) {
if (pm != null) {
int parameterType = getParameterType(pm);
ContextualGetter<T, ? extends P> getter = ContextualGetterAdapter.of(pm.getPropertyMeta().getGetter());
// need conversion ?
final DefaultContextFactoryBuilder contextFactoryBuilder = new DefaultContextFactoryBuilder();
Type propertyType = pm.getPropertyMeta().getPropertyType();
Class<?> sqlType = JdbcTypeHelper.toJavaType(parameterType, propertyType);
boolean findConverter = false;
for (ConverterProperty cp : pm.getColumnDefinition().lookForAll(ConverterProperty.class)) {
if (TypeHelper.isAssignable(cp.inType, propertyType)) {
getter = new FieldMapperGetterWithConverter(cp.function, getter);
findConverter = true;
break;
}
}
if (!findConverter && !TypeHelper.isAssignable(sqlType, propertyType)) {
ContextualConverter<? super Object, ?> converter = ConverterService.getInstance().findConverter(propertyType, sqlType, contextFactoryBuilder);
if (converter != null) {
getter = new FieldMapperGetterWithConverter(converter, getter);
}
}
return new PlaceHolderValueGetter<T>(pm.getColumnKey().getOrginalName(), parameterType, null, getter, contextFactoryBuilder.build());
} else {
return new PlaceHolderValueGetter<T>(key.getOrginalName(), key.getSqlType(null), null, NullContextualGetter.<T, P>getter(), EmptyContextFactory.INSTANCE);
}
}
use of org.simpleflatmapper.map.property.ConverterProperty in project SimpleFlatMapper by arnaudroger.
the class ConstantSourceFieldMapperFactoryImpl method getGetterFromSource.
@Override
public <P> ContextualGetter<? super S, ? extends P> getGetterFromSource(K columnKey, Type propertyType, ColumnDefinition<K, ?> columnDefinition, Supplier<ClassMeta<P>> propertyClassMetaSupplier, MappingContextFactoryBuilder<?, ? extends FieldKey<?>> mappingContextFactoryBuilder) {
@SuppressWarnings("unchecked") ContextualGetter<? super S, ? extends P> getter = ContextualGetterAdapter.of((Getter<? super S, ? extends P>) columnDefinition.getCustomGetterFrom(sourceType));
if (getter == null) {
ContextualGetterFactory<? super S, K> customGetterFactory = (ContextualGetterFactory<? super S, K>) columnDefinition.getCustomGetterFactoryFrom(sourceType);
if (customGetterFactory != null) {
getter = (ContextualGetter<? super S, ? extends P>) customGetterFactory.<P>newGetter(propertyType, columnKey, mappingContextFactoryBuilder, columnDefinition.properties());
}
}
ConverterProperty converterProperty = columnDefinition.lookFor(ConverterProperty.class);
if (converterProperty != null) {
Type t = converterProperty.inType;
if (Object.class.equals(t)) {
// lost type info... assume sql type is right
t = columnKey.getType(t);
}
getter = getterFactory.<P>newGetter(t, columnKey, mappingContextFactoryBuilder, columnDefinition.properties());
if (getter == null) {
return null;
}
return new FieldMapperGetterWithConverter(converterProperty.function, getter);
}
if (getter == null) {
getter = getterFactory.newGetter(propertyType, columnKey, mappingContextFactoryBuilder, columnDefinition.properties());
}
// try to identify constructor that we could build from
if (getter == null) {
getter = lookForAlternativeGetter(propertyClassMetaSupplier.get(), columnKey, columnDefinition, new HashSet<Type>(), mappingContextFactoryBuilder);
}
DefaultValueProperty defaultValueProperty = columnDefinition.lookFor(DefaultValueProperty.class);
if (defaultValueProperty != null) {
Object value = defaultValueProperty.getValue();
if (value != null) {
if (TypeHelper.isAssignable(propertyType, value.getClass())) {
getter = new ContextualGetterWithDefaultValue<S, P>(getter, (P) value);
} else {
throw new IllegalArgumentException("Incompatible default value " + value + " type " + value.getClass() + " with property " + columnKey + " of type " + propertyType);
}
}
}
return getter;
}
use of org.simpleflatmapper.map.property.ConverterProperty in project SimpleFlatMapper by arnaudroger.
the class Issue675Test method testSourceFactory.
@Test
public void testSourceFactory() {
String query = "SELECT * FROM table WHERE type = :myEnum and id = :id";
SqlParameterSourceFactory<Issue675> sourceFactory = JdbcTemplateMapperFactory.newInstance().addColumnProperty("myEnum", new ConverterProperty<MyEnum, Long>(new ContextualConverter<MyEnum, Long>() {
@Override
public Long convert(MyEnum in, Context context) throws Exception {
switch(in) {
case ONE:
return 1l;
case TWO:
return 2l;
default:
throw new IllegalArgumentException();
}
}
}, MyEnum.class)).newSqlParameterSourceFactory(Issue675.class, query);
SqlParameterSource sqlParameterSource = sourceFactory.newSqlParameterSource(new Issue675(1, MyEnum.TWO));
assertEquals(1l, sqlParameterSource.getValue("id"));
assertEquals(2l, sqlParameterSource.getValue("myEnum"));
}
Aggregations