Search in sources :

Example 6 with Setter

use of org.simpleflatmapper.reflect.Setter in project SimpleFlatMapper by arnaudroger.

the class PreparedStatementFieldMapperFactoryTest method testCustomSetterFactory.

@Test
public void testCustomSetterFactory() throws Exception {
    Object o = new Object();
    newFieldMapperAndMapToPS(new ConstantGetter<Object, Object>(o), Object.class, new SetterFactoryProperty(new SetterFactory<PreparedStatement, PropertyMapping<?, ?, ?, ?>>() {

        @Override
        public <P> Setter<PreparedStatement, P> getSetter(PropertyMapping<?, ?, ?, ?> arg) {
            return new Setter<PreparedStatement, P>() {

                @Override
                public void set(PreparedStatement target, P value) throws Exception {
                    ps.setString(36, "bb");
                }
            };
        }
    }));
    verify(ps).setString(36, "bb");
}
Also used : SetterFactory(org.simpleflatmapper.reflect.SetterFactory) PreparedStatementSetterFactory(org.simpleflatmapper.jdbc.impl.PreparedStatementSetterFactory) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) Setter(org.simpleflatmapper.reflect.Setter) DbObject(org.simpleflatmapper.test.beans.DbObject) SetterFactoryProperty(org.simpleflatmapper.map.property.SetterFactoryProperty) Test(org.junit.Test) ResultSetGetterFactoryTest(org.simpleflatmapper.jdbc.test.impl.getter.ResultSetGetterFactoryTest)

Example 7 with Setter

use of org.simpleflatmapper.reflect.Setter in project SimpleFlatMapper by arnaudroger.

the class PreparedStatementSetterFactory method getSetter.

@SuppressWarnings("unchecked")
@Override
public <P> Setter<PreparedStatement, P> getSetter(PropertyMapping<?, ?, JdbcColumnKey, ? extends ColumnDefinition<JdbcColumnKey, ?>> pm) {
    int columnIndex = pm.getColumnKey().getIndex();
    Type type = pm.getPropertyMeta().getPropertyType();
    Class<?> clazz = TypeHelper.toBoxedClass(type);
    if (Boolean.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new BooleanPreparedStatementSetter(columnIndex);
    } else if (Byte.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new BytePreparedStatementSetter(columnIndex);
    } else if (Character.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new CharacterPreparedStatementSetter(columnIndex);
    } else if (Short.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new ShortPreparedStatementSetter(columnIndex);
    } else if (Integer.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new IntegerPreparedStatementSetter(columnIndex);
    } else if (Long.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new LongPreparedStatementSetter(columnIndex);
    } else if (Double.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new DoublePreparedStatementSetter(columnIndex);
    } else if (Float.class.equals(clazz)) {
        return (Setter<PreparedStatement, P>) new FloatPreparedStatementSetter(columnIndex);
    }
    IndexedSetter<PreparedStatement, P> setter = preparedStatementIndexedSetterFactory.getIndexedSetter(pm);
    if (setter != null) {
        return new PreparedStatementSetterImpl<P>(columnIndex, setter);
    } else
        return null;
}
Also used : BooleanPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.BooleanPreparedStatementSetter) FloatPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.FloatPreparedStatementSetter) Type(java.lang.reflect.Type) DoublePreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.DoublePreparedStatementSetter) BytePreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.BytePreparedStatementSetter) ShortPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.ShortPreparedStatementSetter) PreparedStatementSetterImpl(org.simpleflatmapper.jdbc.impl.setter.PreparedStatementSetterImpl) ShortPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.ShortPreparedStatementSetter) DoublePreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.DoublePreparedStatementSetter) IndexedSetter(org.simpleflatmapper.reflect.IndexedSetter) BooleanPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.BooleanPreparedStatementSetter) Setter(org.simpleflatmapper.reflect.Setter) CharacterPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.CharacterPreparedStatementSetter) BytePreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.BytePreparedStatementSetter) IntegerPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.IntegerPreparedStatementSetter) FloatPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.FloatPreparedStatementSetter) LongPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.LongPreparedStatementSetter) IntegerPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.IntegerPreparedStatementSetter) LongPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.LongPreparedStatementSetter) CharacterPreparedStatementSetter(org.simpleflatmapper.jdbc.impl.setter.CharacterPreparedStatementSetter)

Aggregations

Setter (org.simpleflatmapper.reflect.Setter)7 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)4 Test (org.junit.Test)3 Type (java.lang.reflect.Type)2 ResultSetGetterFactoryTest (org.simpleflatmapper.jdbc.test.impl.getter.ResultSetGetterFactoryTest)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 SetterFactoryProperty (org.simpleflatmapper.map.property.SetterFactoryProperty)2 SetterProperty (org.simpleflatmapper.map.property.SetterProperty)2 SetterFactory (org.simpleflatmapper.reflect.SetterFactory)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 HashMap (java.util.HashMap)1 Converter (org.simpleflatmapper.converter.Converter)1 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)1 CellSetter (org.simpleflatmapper.csv.mapper.CellSetter)1 DelayedCellSetterFactory (org.simpleflatmapper.csv.mapper.DelayedCellSetterFactory)1 BigDecimalSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.BigDecimalSettableDataSetter)1 BigIntegerSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.BigIntegerSettableDataSetter)1 ByteSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.ByteSettableDataSetter)1 DateSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.DateSettableDataSetter)1 DoubleSettableDataSetter (org.simpleflatmapper.datastax.impl.setter.DoubleSettableDataSetter)1