Search in sources :

Example 1 with SfmResultSetMapperFactory

use of org.simpleflatmapper.jdbi.SfmResultSetMapperFactory in project SimpleFlatMapper by arnaudroger.

the class ResultSetMapperFactoryTest method testMapToDbObjectStatic.

@Test
public void testMapToDbObjectStatic() throws Exception {
    DBI dbi = new DBI(DbHelper.getHsqlDataSource());
    dbi.registerMapper(new SfmResultSetMapperFactory(new UnaryFactory<Class<?>, Mapper<ResultSet, ?>>() {

        @Override
        public Mapper<ResultSet, ?> newInstance(Class<?> aClass) {
            return new Mapper<ResultSet, DbObject>() {

                @Override
                public DbObject map(ResultSet source) throws MappingException {
                    return map(source, null);
                }

                @Override
                public DbObject map(ResultSet source, MappingContext<? super ResultSet> context) throws MappingException {
                    DbObject dbObject = new DbObject();
                    try {
                        mapTo(source, dbObject, context);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return dbObject;
                }

                @Override
                public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> context) throws Exception {
                    target.setId(source.getInt("id"));
                    target.setCreationTime(source.getTimestamp("creation_time"));
                    target.setEmail(source.getString("email"));
                    target.setName(source.getString("name"));
                    String type_name = source.getString("type_name");
                    if (type_name != null) {
                        target.setTypeName(DbObject.Type.valueOf(type_name));
                    }
                    target.setTypeOrdinal(DbObject.Type.values()[source.getInt("type_ordinal")]);
                }
            };
        }
    }));
    Handle handle = dbi.open();
    try {
        DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).first();
        DbHelper.assertDbObjectMapping(dbObject);
        SfmBindTest.SfmBindExample attach = handle.attach(SfmBindTest.SfmBindExample.class);
        attach.insert(DbObject.newInstance());
        assertTrue(handle.createQuery("select * from TEST_DB_OBJECT").mapTo(DbObject.class).list().size() > 1);
    } finally {
        handle.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) SfmResultSetMapperFactory(org.simpleflatmapper.jdbi.SfmResultSetMapperFactory) DBI(org.skife.jdbi.v2.DBI) MappingException(org.simpleflatmapper.map.MappingException) Handle(org.skife.jdbi.v2.Handle) MappingContext(org.simpleflatmapper.map.MappingContext) Mapper(org.simpleflatmapper.map.Mapper) ResultSet(java.sql.ResultSet) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) Test(org.junit.Test)

Example 2 with SfmResultSetMapperFactory

use of org.simpleflatmapper.jdbi.SfmResultSetMapperFactory in project SimpleFlatMapper by arnaudroger.

the class ResultSetMapperFactoryTest method testMapToDbObject.

@Test
public void testMapToDbObject() throws Exception {
    DBI dbi = new DBI(DbHelper.getHsqlDataSource());
    dbi.registerMapper(new SfmResultSetMapperFactory());
    Handle handle = dbi.open();
    try {
        DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).first();
        DbHelper.assertDbObjectMapping(dbObject);
        SfmBindTest.SfmBindExample attach = handle.attach(SfmBindTest.SfmBindExample.class);
        attach.insert(DbObject.newInstance());
        assertTrue(handle.createQuery("select * from TEST_DB_OBJECT").mapTo(DbObject.class).list().size() > 1);
    } finally {
        handle.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) SfmResultSetMapperFactory(org.simpleflatmapper.jdbi.SfmResultSetMapperFactory) DBI(org.skife.jdbi.v2.DBI) Handle(org.skife.jdbi.v2.Handle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 SfmResultSetMapperFactory (org.simpleflatmapper.jdbi.SfmResultSetMapperFactory)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 DBI (org.skife.jdbi.v2.DBI)2 Handle (org.skife.jdbi.v2.Handle)2 ResultSet (java.sql.ResultSet)1 Mapper (org.simpleflatmapper.map.Mapper)1 MappingContext (org.simpleflatmapper.map.MappingContext)1 MappingException (org.simpleflatmapper.map.MappingException)1 UnaryFactory (org.simpleflatmapper.util.UnaryFactory)1