Search in sources :

Example 1 with ConstantValueProperty

use of org.simpleflatmapper.map.property.ConstantValueProperty in project SimpleFlatMapper by arnaudroger.

the class AbstractConstantTargetMapperBuilder method addColumn.

@SuppressWarnings("unchecked")
public B addColumn(K key, FieldMapperColumnDefinition<K> columnDefinition) {
    final FieldMapperColumnDefinition<K> composedDefinition = columnDefinition.compose(mapperConfig.columnDefinitions().getColumnDefinition(key));
    final K mappedColumnKey = composedDefinition.rename(key);
    if (composedDefinition.has(ConstantValueProperty.class)) {
        ConstantValueProperty staticValueProperty = composedDefinition.lookFor(ConstantValueProperty.class);
        PropertyMeta<T, Object> meta = new ObjectPropertyMeta<T, Object>(key.getName(), classMeta.getType(), reflectionService, staticValueProperty.getType(), ScoredGetter.of(new ConstantGetter<T, Object>(staticValueProperty.getValue()), 1), null, null);
        propertyMappingsBuilder.addProperty(key, columnDefinition, meta);
    } else {
        propertyMappingsBuilder.addProperty(mappedColumnKey, composedDefinition);
    }
    return (B) this;
}
Also used : ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) ConstantValueProperty(org.simpleflatmapper.map.property.ConstantValueProperty) ObjectPropertyMeta(org.simpleflatmapper.reflect.meta.ObjectPropertyMeta)

Example 2 with ConstantValueProperty

use of org.simpleflatmapper.map.property.ConstantValueProperty in project SimpleFlatMapper by arnaudroger.

the class QueryPreparerBuilderTest method testCustomGetterOnNonExistantProp.

@Test
public void testCustomGetterOnNonExistantProp() throws Exception {
    PreparedStatementMapperBuilder<Object> mapperBuilder = JdbcMapperFactory.newInstance().addColumnProperty("text", new ConstantValueProperty<String>("value2", String.class)).buildFrom(Object.class);
    mapperBuilder.addColumn("text");
    Mapper<Object, PreparedStatement> mapper = mapperBuilder.mapper();
    PreparedStatement ps = mock(PreparedStatement.class);
    mapper.mapTo(new Object(), ps, null);
    verify(ps).setString(1, "value2");
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) PreparedStatement(java.sql.PreparedStatement) ConstantValueProperty(org.simpleflatmapper.map.property.ConstantValueProperty) Test(org.junit.Test)

Example 3 with ConstantValueProperty

use of org.simpleflatmapper.map.property.ConstantValueProperty in project SimpleFlatMapper by arnaudroger.

the class SqlParameterSourceBuilder method add.

public SqlParameterSourceBuilder<T> add(JdbcColumnKey key, FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition) {
    final FieldMapperColumnDefinition<JdbcColumnKey> composedDefinition = columnDefinition.compose(mapperConfig.columnDefinitions().getColumnDefinition(key));
    final JdbcColumnKey mappedColumnKey = composedDefinition.rename(key);
    if (composedDefinition.has(ConstantValueProperty.class)) {
        ConstantValueProperty staticValueProperty = composedDefinition.lookFor(ConstantValueProperty.class);
        PropertyMeta<T, Object> meta = new ObjectPropertyMeta<T, Object>(key.getName(), builder.getClassMeta().getType(), reflectionService, staticValueProperty.getType(), ScoredGetter.of(new ConstantGetter<T, Object>(staticValueProperty.getValue()), 1), null, null);
        builder.addProperty(key, columnDefinition, meta);
    } else {
        builder.addProperty(mappedColumnKey, composedDefinition);
    }
    return this;
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) ConstantGetter(org.simpleflatmapper.reflect.getter.ConstantGetter) ConstantValueProperty(org.simpleflatmapper.map.property.ConstantValueProperty) ObjectPropertyMeta(org.simpleflatmapper.reflect.meta.ObjectPropertyMeta)

Example 4 with ConstantValueProperty

use of org.simpleflatmapper.map.property.ConstantValueProperty in project SimpleFlatMapper by arnaudroger.

the class SqlParameterSourceTest method testConstantValue.

@Test
public void testConstantValue() {
    SqlParameterSourceFactory<DbObject> parameterSourceFactory = JdbcTemplateMapperFactory.newInstance().addColumnProperty("id", new ConstantValueProperty<Long>(-3l, Long.class)).newSqlParameterSourceFactory(DbObject.class);
    SqlParameterSource parameterSource = parameterSourceFactory.newSqlParameterSource(new DbObject());
    assertEquals(-3l, parameterSource.getValue("id"));
}
Also used : SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) DbObject(org.simpleflatmapper.test.beans.DbObject) ConstantValueProperty(org.simpleflatmapper.map.property.ConstantValueProperty) Test(org.junit.Test)

Aggregations

ConstantValueProperty (org.simpleflatmapper.map.property.ConstantValueProperty)4 Test (org.junit.Test)2 ConstantGetter (org.simpleflatmapper.reflect.getter.ConstantGetter)2 ObjectPropertyMeta (org.simpleflatmapper.reflect.meta.ObjectPropertyMeta)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 PreparedStatement (java.sql.PreparedStatement)1 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)1 SqlParameterSource (org.springframework.jdbc.core.namedparam.SqlParameterSource)1