Search in sources :

Example 1 with ConverterProperty

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);
    }
}
Also used : Type(java.lang.reflect.Type) ConverterProperty(org.simpleflatmapper.map.property.ConverterProperty) FieldMapperGetterWithConverter(org.simpleflatmapper.map.fieldmapper.FieldMapperGetterWithConverter) DefaultContextFactoryBuilder(org.simpleflatmapper.converter.DefaultContextFactoryBuilder)

Example 2 with ConverterProperty

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;
}
Also used : DefaultValueProperty(org.simpleflatmapper.map.property.DefaultValueProperty) Type(java.lang.reflect.Type) ConverterProperty(org.simpleflatmapper.map.property.ConverterProperty) ContextualGetterFactory(org.simpleflatmapper.map.getter.ContextualGetterFactory) HashSet(java.util.HashSet)

Example 3 with ConverterProperty

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"));
}
Also used : Context(org.simpleflatmapper.converter.Context) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) ConverterProperty(org.simpleflatmapper.map.property.ConverterProperty) Test(org.junit.Test)

Aggregations

ConverterProperty (org.simpleflatmapper.map.property.ConverterProperty)3 Type (java.lang.reflect.Type)2 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 Context (org.simpleflatmapper.converter.Context)1 DefaultContextFactoryBuilder (org.simpleflatmapper.converter.DefaultContextFactoryBuilder)1 FieldMapperGetterWithConverter (org.simpleflatmapper.map.fieldmapper.FieldMapperGetterWithConverter)1 ContextualGetterFactory (org.simpleflatmapper.map.getter.ContextualGetterFactory)1 DefaultValueProperty (org.simpleflatmapper.map.property.DefaultValueProperty)1 SqlParameterSource (org.springframework.jdbc.core.namedparam.SqlParameterSource)1