Search in sources :

Example 66 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperCustomMappingTest method testColumnAliasStatic.

@Test
public void testColumnAliasStatic() throws Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactoryHelper.asm();
    mapperFactory.addAlias("not_id_column", "id");
    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,", "id as not_id_column,"));
}
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 67 with DbObject

use of org.simpleflatmapper.test.beans.DbObject 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 68 with DbObject

use of org.simpleflatmapper.test.beans.DbObject 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 69 with DbObject

use of org.simpleflatmapper.test.beans.DbObject 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)

Example 70 with DbObject

use of org.simpleflatmapper.test.beans.DbObject 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

DbObject (org.simpleflatmapper.test.beans.DbObject)83 Test (org.junit.Test)75 Connection (java.sql.Connection)27 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)10 CheckedConsumer (org.simpleflatmapper.util.CheckedConsumer)10 ArrayList (java.util.ArrayList)9 MappingException (org.simpleflatmapper.map.MappingException)6 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)6 Type (java.lang.reflect.Type)5 ParseException (java.text.ParseException)5 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)5 StringReader (java.io.StringReader)4 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)4 DbPartialFinalObject (org.simpleflatmapper.test.beans.DbPartialFinalObject)4 Statement (java.sql.Statement)3 MapperBuilderErrorHandler (org.simpleflatmapper.map.MapperBuilderErrorHandler)3 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)3 ListCollector (org.simpleflatmapper.util.ListCollector)3