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();
}
}
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,"));
}
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);
}
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();
}
}
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);
}
Aggregations