Search in sources :

Example 11 with JdbcColumnKey

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;
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet)

Example 12 with JdbcColumnKey

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());
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) GetterFactory(org.simpleflatmapper.reflect.getter.GetterFactory) Type(java.lang.reflect.Type) DbObject(org.simpleflatmapper.test.beans.DbObject) Getter(org.simpleflatmapper.reflect.Getter) DbObject(org.simpleflatmapper.test.beans.DbObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) Test(org.junit.Test)

Example 13 with JdbcColumnKey

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));
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) MappingContext(org.simpleflatmapper.map.MappingContext) ListCollector(org.simpleflatmapper.util.ListCollector) DbObject(org.simpleflatmapper.test.beans.DbObject) FieldMapper(org.simpleflatmapper.map.FieldMapper) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 14 with JdbcColumnKey

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;
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) Random(java.util.Random) ArrayList(java.util.ArrayList) MapperKey(org.simpleflatmapper.map.mapper.MapperKey)

Aggregations

JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)14 Test (org.junit.Test)8 ResultSet (java.sql.ResultSet)7 DbObject (org.simpleflatmapper.test.beans.DbObject)4 ListCollector (org.simpleflatmapper.util.ListCollector)4 Type (java.lang.reflect.Type)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 PropertyMapping (org.simpleflatmapper.map.mapper.PropertyMapping)2 Getter (org.simpleflatmapper.reflect.Getter)2 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)2 Array (java.sql.Array)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 Time (java.sql.Time)1 LocalTime (java.time.LocalTime)1 OffsetTime (java.time.OffsetTime)1 ZoneOffset (java.time.ZoneOffset)1 List (java.util.List)1