Search in sources :

Example 11 with JdbcMapperFactory

use of org.simpleflatmapper.jdbc.JdbcMapperFactory in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperCustomMappingTest method testCustomMapping.

@Test
public void testCustomMapping() throws SQLException, Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactoryHelper.asm();
    mapperFactory.addCustomFieldMapper("id", new FieldMapper<ResultSet, DbObject>() {

        @Override
        public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> mappingContext) throws MappingException {
            target.setId(1);
        }
    });
    final JdbcMapper<DbObject> mapper = mapperFactory.newMapper(DbObject.class);
    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 : JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) DbObject(org.simpleflatmapper.test.beans.DbObject) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MappingException(org.simpleflatmapper.map.MappingException) SQLException(java.sql.SQLException) MappingException(org.simpleflatmapper.map.MappingException) Test(org.junit.Test)

Example 12 with JdbcMapperFactory

use of org.simpleflatmapper.jdbc.JdbcMapperFactory in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperCustomMappingTest method testCustomReaderOnConstructor.

@Test
public void testCustomReaderOnConstructor() throws SQLException, Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactoryHelper.asm().addCustomGetter("id", new Getter<ResultSet, Long>() {

        @Override
        public Long get(ResultSet target) throws Exception {
            return 1l;
        }
    });
    final JdbcMapper<DbFinalObject> mapper = mapperFactory.newMapper(DbFinalObject.class);
    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 : DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MappingException(org.simpleflatmapper.map.MappingException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 13 with JdbcMapperFactory

use of org.simpleflatmapper.jdbc.JdbcMapperFactory in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperCustomMappingTest method testCustomReaderOnSetter.

@Test
public void testCustomReaderOnSetter() throws SQLException, Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactoryHelper.asm().addCustomGetter("id", new Getter<ResultSet, Long>() {

        @Override
        public Long get(ResultSet target) throws Exception {
            return 1l;
        }
    });
    final JdbcMapper<DbObject> mapper = mapperFactory.newMapper(DbObject.class);
    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 : JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) DbObject(org.simpleflatmapper.test.beans.DbObject) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MappingException(org.simpleflatmapper.map.MappingException) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 14 with JdbcMapperFactory

use of org.simpleflatmapper.jdbc.JdbcMapperFactory in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperCustomMappingTest method testCustomMappingStatic.

@Test
public void testCustomMappingStatic() throws SQLException, Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactoryHelper.asm();
    mapperFactory.addCustomFieldMapper("id", new FieldMapper<ResultSet, DbObject>() {

        @Override
        public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> mappingContext) throws MappingException {
            target.setId(1);
        }
    });
    final JdbcMapper<DbObject> mapper = JdbcMapperDbObjectTest.addColumn(mapperFactory.newBuilder(DbObject.class)).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 : JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) DbObject(org.simpleflatmapper.test.beans.DbObject) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) MappingException(org.simpleflatmapper.map.MappingException) SQLException(java.sql.SQLException) MappingException(org.simpleflatmapper.map.MappingException) Test(org.junit.Test)

Aggregations

JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)14 Test (org.junit.Test)9 SQLException (java.sql.SQLException)8 PreparedStatement (java.sql.PreparedStatement)7 ResultSet (java.sql.ResultSet)7 MappingException (org.simpleflatmapper.map.MappingException)6 DbObject (org.simpleflatmapper.test.beans.DbObject)6 Connection (java.sql.Connection)4 Crud (org.simpleflatmapper.jdbc.Crud)2 DataAccessException (org.springframework.dao.DataAccessException)2 DynamicJdbcMapper (org.simpleflatmapper.jdbc.DynamicJdbcMapper)1 IndexedSetterProperty (org.simpleflatmapper.jdbc.property.IndexedSetterProperty)1 DefaultPropertyNameMatcherFactory (org.simpleflatmapper.map.mapper.DefaultPropertyNameMatcherFactory)1 GetterProperty (org.simpleflatmapper.map.property.GetterProperty)1 Getter (org.simpleflatmapper.reflect.Getter)1 IndexedSetter (org.simpleflatmapper.reflect.IndexedSetter)1 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)1