use of org.simpleflatmapper.jdbc.spring.JdbcTemplateMapperFactory in project SimpleFlatMapper by arnaudroger.
the class JdbcTemplateMapperFactoryTest method testIssue618OverrideEnum.
@Test
public void testIssue618OverrideEnum() throws SQLException {
JdbcTemplateMapperFactory config = JdbcTemplateMapperFactory.newInstance().addGetterForType(new Predicate<Type>() {
@Override
public boolean test(Type type) {
return TypeHelper.isAssignable(LookupValue.class, type);
}
}, new ContextualGetterFactory<ResultSet, JdbcColumnKey>() {
@Override
public <P> ContextualGetter<ResultSet, P> newGetter(Type target, JdbcColumnKey key, MappingContextFactoryBuilder<?, ? extends FieldKey<?>> mappingContextFactoryBuilder, Object... properties) {
Class enumClass = TypeHelper.toClass(target);
final Enum[] values = EnumHelper.getValues(enumClass);
final int index = key.getIndex();
return new ContextualGetter<ResultSet, P>() {
@Override
public P get(ResultSet resultSet, Context context) throws Exception {
int i = resultSet.getInt(index);
for (Enum e : values) {
if (((LookupValue) e).lookupValue() == i) {
return (P) e;
}
}
return null;
}
};
}
});
ResultSetExtractorImpl<Issue618> rse = JdbcTemplateMapperFactory.newInstance(config).newResultSetExtractor(Issue618.class);
Connection conn = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
Statement st = null;
ResultSet rs = null;
if (conn == null)
return;
try {
st = conn.createStatement();
rs = st.executeQuery("SELECT * FROM (VALUES (1, 1), (2, 2)) AS t (id, enum618) ");
List<Issue618> issue618s = rse.extractData(rs);
assertEquals(Arrays.asList(new Issue618(1, Enum618.A), new Issue618(2, Enum618.B)), issue618s);
} finally {
if (rs != null)
rs.close();
if (st != null)
st.close();
conn.close();
}
}
Aggregations