Search in sources :

Example 1 with FieldMapperGetterWithConverter

use of org.simpleflatmapper.map.fieldmapper.FieldMapperGetterWithConverter 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)

Aggregations

Type (java.lang.reflect.Type)1 DefaultContextFactoryBuilder (org.simpleflatmapper.converter.DefaultContextFactoryBuilder)1 FieldMapperGetterWithConverter (org.simpleflatmapper.map.fieldmapper.FieldMapperGetterWithConverter)1 ConverterProperty (org.simpleflatmapper.map.property.ConverterProperty)1