Search in sources :

Example 1 with GetterProperty

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

the class CrudTest method testDbObjectCrudWithCustomGetter.

@Test
public void testDbObjectCrudWithCustomGetter() throws Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactory.newInstance().addColumnProperty("name", new GetterProperty(new Getter<ResultSet, String>() {

        @Override
        public String get(ResultSet target) throws Exception {
            return "customname";
        }
    }));
    Connection connection = DbHelper.getDbConnection(targetDB);
    if (connection == null) {
        System.err.println("Db " + targetDB + " not available");
        return;
    }
    try {
        Crud<TestDbObject, Long> objectCrud = mapperFactory.<TestDbObject, Long>crud(TestDbObject.class, Long.class).to(connection);
        TestDbObject testDbObject = DbObject.newInstance(new TestDbObject());
        objectCrud.create(connection, testDbObject);
        TestDbObject read = objectCrud.read(connection, testDbObject.getId());
        assertEquals("customname", read.getName());
        assertEquals(testDbObject.getEmail(), read.getEmail());
    } finally {
        connection.close();
    }
}
Also used : JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) Getter(org.simpleflatmapper.reflect.Getter) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) Test(org.junit.Test)

Example 2 with GetterProperty

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

the class QueryPreparerBuilderTest method testCustomGetter.

@Test
public void testCustomGetter() throws Exception {
    PreparedStatementMapperBuilder<DMClass> mapperBuilder = JdbcMapperFactory.newInstance().addColumnProperty("value", new GetterProperty(new Getter<Object, String>() {

        @Override
        public String get(Object target) throws Exception {
            return "value2";
        }
    })).buildFrom(DMClass.class);
    mapperBuilder.addColumn("value");
    Mapper<DMClass, PreparedStatement> mapper = mapperBuilder.mapper();
    DMClass dmClass = new DMClass();
    PreparedStatement ps = mock(PreparedStatement.class);
    mapper.mapTo(dmClass, ps, null);
    verify(ps).setString(1, "value2");
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) PreparedStatement(java.sql.PreparedStatement) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) Test(org.junit.Test)

Example 3 with GetterProperty

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

the class Issue365Test method mapperConfig.

private MapperConfig<SampleFieldKey, FieldMapperColumnDefinition<SampleFieldKey>> mapperConfig() {
    FieldMapperColumnDefinitionProviderImpl<SampleFieldKey> provider = new FieldMapperColumnDefinitionProviderImpl<SampleFieldKey>();
    provider.addColumnDefinition("benchmark", FieldMapperColumnDefinition.<SampleFieldKey>identity().add(new SetterProperty(SETTER)).add(new GetterProperty(GETTER)));
    return MapperConfig.<SampleFieldKey>fieldMapperConfig().columnDefinitions(provider);
}
Also used : SetterProperty(org.simpleflatmapper.map.property.SetterProperty) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) FieldMapperColumnDefinitionProviderImpl(org.simpleflatmapper.map.mapper.FieldMapperColumnDefinitionProviderImpl) GetterProperty(org.simpleflatmapper.map.property.GetterProperty)

Example 4 with GetterProperty

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

the class AbstractMapperBuilderTest method testCustomization.

@Test
public void testCustomization() throws Exception {
    ClassMeta<DbObject> classMeta = ReflectionService.newInstance().getClassMeta(DbObject.class);
    Mapper<Object[], DbObject> mapper = new SampleMapperBuilder<DbObject>(classMeta).addKey("id").addMapper(new FieldMapper<Object[], DbObject>() {

        @Override
        public void mapTo(Object[] source, DbObject target, MappingContext<? super Object[]> context) throws Exception {
            target.setName("fieldMapper");
        }
    }).addMapping("email", new GetterProperty(new Getter<Object, String>() {

        @Override
        public String get(Object target) throws Exception {
            return "getterEmail";
        }
    })).mapper();
    DbObject dbObject = mapper.map(new Object[] { 1l });
    assertEquals(1, dbObject.getId());
    assertEquals("fieldMapper", dbObject.getName());
    assertEquals("getterEmail", dbObject.getEmail());
    mapper = new SampleMapperBuilder<DbObject>(classMeta).addKey("id").addMapping("email", (Object) FieldMapperColumnDefinition.<SampleFieldKey>identity().add(new GetterProperty(new Getter<Object, String>() {

        @Override
        public String get(Object target) throws Exception {
            return "getterEmail";
        }
    }))).addMapping("name", FieldMapperColumnDefinition.<SampleFieldKey>identity().add(new GetterProperty(new Getter<Object, String>() {

        @Override
        public String get(Object target) throws Exception {
            return "getterName";
        }
    }))).mapper();
    dbObject = mapper.map(new Object[] { 1l });
    assertEquals(1, dbObject.getId());
    assertEquals("getterEmail", dbObject.getEmail());
    assertEquals("getterName", dbObject.getName());
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) MapperBuildingException(org.simpleflatmapper.map.MapperBuildingException) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) DbListObject(org.simpleflatmapper.test.beans.DbListObject) DbObject(org.simpleflatmapper.test.beans.DbObject) DbFinal1DeepObject(org.simpleflatmapper.test.beans.DbFinal1DeepObject) DbPartialFinalObject(org.simpleflatmapper.test.beans.DbPartialFinalObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) Test(org.junit.Test)

Example 5 with GetterProperty

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

the class JdbcMapperCustomMappingTest method testCustomReaderOnSetterStatic.

@SuppressWarnings("unchecked")
@Test
public void testCustomReaderOnSetterStatic() throws Exception {
    final JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().newBuilder(DbObject.class).addMapping("id", new GetterProperty(new Getter<ResultSet, Long>() {

        @Override
        public Long get(ResultSet target) throws Exception {
            return 1l;
        }
    })).addMapping(// email, creation_time, type_ordinal, type_name
    "name").addMapping("email").addMapping("creation_time").addMapping("type_ordinal").addMapping("type_name").mapper();
    DbHelper.testQuery(new TestRowHandler<PreparedStatement>() {

        @Override
        public void handle(PreparedStatement t) throws Exception {
            ResultSet r = t.executeQuery();
            r.next();
            DbHelper.assertDbObjectMapping(mapper.map(r));
        }
    }, DbHelper.TEST_DB_OBJECT_QUERY.replace("id,", "33 as id,"));
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) Getter(org.simpleflatmapper.reflect.Getter) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MappingException(org.simpleflatmapper.map.MappingException) SQLException(java.sql.SQLException) GetterProperty(org.simpleflatmapper.map.property.GetterProperty) Test(org.junit.Test)

Aggregations

GetterProperty (org.simpleflatmapper.map.property.GetterProperty)6 Test (org.junit.Test)4 DbObject (org.simpleflatmapper.test.beans.DbObject)3 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 Getter (org.simpleflatmapper.reflect.Getter)2 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)1 FieldMapper (org.simpleflatmapper.map.FieldMapper)1 MapperBuildingException (org.simpleflatmapper.map.MapperBuildingException)1 MappingException (org.simpleflatmapper.map.MappingException)1 MapperAsmFactory (org.simpleflatmapper.map.asm.MapperAsmFactory)1 MapperFieldMapper (org.simpleflatmapper.map.fieldmapper.MapperFieldMapper)1 FieldMapperColumnDefinitionProviderImpl (org.simpleflatmapper.map.mapper.FieldMapperColumnDefinitionProviderImpl)1 DefaultValueProperty (org.simpleflatmapper.map.property.DefaultValueProperty)1 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)1 SetterProperty (org.simpleflatmapper.map.property.SetterProperty)1 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)1