Search in sources :

Example 1 with JdbcMapperFactory

use of org.simpleflatmapper.jdbc.JdbcMapperFactory 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 JdbcMapperFactory

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

the class JdbcMapperCustomMappingTest method testColumnAlias.

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

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

the class JdbcTemplateCrudDSL method to.

public JdbcTemplateCrud<T, K> to(JdbcOperations jdbcOperations) {
    final JdbcMapperFactory factory = JdbcMapperFactory.newInstance(jdbcTemplateMapperFactory);
    Crud<T, K> crud = jdbcOperations.execute(new ConnectionCallback<Crud<T, K>>() {

        @Override
        public Crud<T, K> doInConnection(Connection connection) throws SQLException, DataAccessException {
            return factory.<T, K>crud(target, keyTarget).to(connection);
        }
    });
    return new JdbcTemplateCrud<T, K>(jdbcOperations, crud);
}
Also used : JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Crud(org.simpleflatmapper.jdbc.Crud) DataAccessException(org.springframework.dao.DataAccessException)

Example 4 with JdbcMapperFactory

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

the class CrudTest method testDbObjectCrudWithCustomSetter.

@Test
public void testDbObjectCrudWithCustomSetter() throws Exception {
    JdbcMapperFactory mapperFactory = JdbcMapperFactory.newInstance().addColumnProperty("name", new IndexedSetterProperty(new IndexedSetter<PreparedStatement, Object>() {

        @Override
        public void set(PreparedStatement target, Object value, int index) throws Exception {
            target.setString(index, "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());
        assertNotEquals("customname", 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 : IndexedSetter(org.simpleflatmapper.reflect.IndexedSetter) JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DbObject(org.simpleflatmapper.test.beans.DbObject) IndexedSetterProperty(org.simpleflatmapper.jdbc.property.IndexedSetterProperty) Test(org.junit.Test)

Example 5 with JdbcMapperFactory

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

the class SfmResultSetHandlerFactoryBuilder method newFactory.

@Override
public <E> ResultSetHandlerFactory<E> newFactory(Class<E> aClass) {
    boolean exactMatch = !isAutoDeriveColumnNames();
    DefaultPropertyNameMatcherFactory propertyNameMatcherFactory = DefaultPropertyNameMatcherFactory.DEFAULT.exactMatch(exactMatch).caseSensitive(isCaseSensitive());
    Map<String, String> columnMappings = getColumnMappings();
    JdbcMapperFactory jdbcMapperFactory = JdbcMapperFactory.newInstance().propertyNameMatcherFactory(propertyNameMatcherFactory);
    if (columnMappings != null) {
        jdbcMapperFactory.addAliases(columnMappings);
    }
    DynamicJdbcMapper<E> dynamicJdbcMapper = (DynamicJdbcMapper<E>) jdbcMapperFactory.newMapper(aClass);
    return new SfmResultSetHandlerFactory<E>(dynamicJdbcMapper);
}
Also used : JdbcMapperFactory(org.simpleflatmapper.jdbc.JdbcMapperFactory) DynamicJdbcMapper(org.simpleflatmapper.jdbc.DynamicJdbcMapper) DefaultPropertyNameMatcherFactory(org.simpleflatmapper.map.mapper.DefaultPropertyNameMatcherFactory)

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