use of org.simpleflatmapper.jdbc.JdbcColumnKey in project SimpleFlatMapper by arnaudroger.
the class CrudMeta method getPrimaryKeys.
private static List<String> getPrimaryKeys(Connection connection, ResultSetMetaData resultSetMetaData, ColumnDefinitionProvider<FieldMapperColumnDefinition<JdbcColumnKey>, JdbcColumnKey> columnDefinitionProvider) throws SQLException {
List<String> primaryKeys = new ArrayList<String>();
for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
JdbcColumnKey key = JdbcColumnKey.of(resultSetMetaData, i);
if (columnDefinitionProvider.getColumnDefinition(key).has(KeyProperty.class)) {
primaryKeys.add(key.getName());
}
}
if (!primaryKeys.isEmpty()) {
return primaryKeys;
}
ResultSet set = connection.getMetaData().getPrimaryKeys(resultSetMetaData.getCatalogName(1), resultSetMetaData.getSchemaName(1), resultSetMetaData.getTableName(1));
try {
while (set.next()) {
primaryKeys.add(set.getString("COLUMN_NAME"));
}
} finally {
set.close();
}
return primaryKeys;
}
use of org.simpleflatmapper.jdbc.JdbcColumnKey in project SimpleFlatMapper by arnaudroger.
the class JdbcMapperFactoryTest method testCustomGetterFactory.
@Test
public void testCustomGetterFactory() throws SQLException {
JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().getterFactory(new GetterFactory<ResultSet, JdbcColumnKey>() {
@SuppressWarnings("unchecked")
@Override
public <P> Getter<ResultSet, P> newGetter(Type target, JdbcColumnKey key, Object... properties) {
return new Getter() {
@Override
public Object get(Object target) throws Exception {
return "Hello!";
}
};
}
}).newBuilder(DbObject.class).addMapping("name").mapper();
ResultSet rs = mock(ResultSet.class);
when(rs.next()).thenReturn(true, false);
DbObject object = mapper.iterator(rs).next();
assertEquals("Hello!", object.getName());
}
use of org.simpleflatmapper.jdbc.JdbcColumnKey in project SimpleFlatMapper by arnaudroger.
the class JdbcMapperFactoryTest method testFieldErrorHandling.
@Test
public void testFieldErrorHandling() throws SQLException, Exception, ParseException {
@SuppressWarnings("unchecked") FieldMapperErrorHandler<JdbcColumnKey> fieldMapperErrorHandler = mock(FieldMapperErrorHandler.class);
final Exception exception = new Exception("Error!");
JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().fieldMapperErrorHandler(fieldMapperErrorHandler).addCustomFieldMapper("id", new FieldMapper<ResultSet, DbObject>() {
@Override
public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> mappingContext) throws Exception {
throw exception;
}
}).newBuilder(DbObject.class).addMapping("id").mapper();
List<DbObject> list = mapper.forEach(new MockDbObjectResultSet(1), new ListCollector<DbObject>()).getList();
assertNotNull(list.get(0));
verify(fieldMapperErrorHandler).errorMappingField(eq(new JdbcColumnKey("id", 1)), any(), same(list.get(0)), same(exception));
}
use of org.simpleflatmapper.jdbc.JdbcColumnKey in project SimpleFlatMapper by arnaudroger.
the class Utils method generateKeys.
public static List<MapperKey<JdbcColumnKey>> generateKeys(int size, int maxColumns) {
Random random = new Random(13265656556l);
ArrayList<MapperKey<JdbcColumnKey>> list = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
int nbColumn = random.nextInt(maxColumns) + 2;
JdbcColumnKey[] columnKeys = new JdbcColumnKey[nbColumn];
for (int j = 0; j < columnKeys.length; j++) {
JdbcColumnKey key = new JdbcColumnKey(newName(random), j + 1);
columnKeys[j] = key;
}
list.add(new MapperKey<>(columnKeys));
}
return list;
}
Aggregations