Search in sources :

Example 1 with JdbcColumnKey

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

the class PreparedStatementFieldMapperFactoryTest method newPropertyMapping.

@SuppressWarnings("unchecked")
private <T, P> PropertyMapping<T, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> newPropertyMapping(Getter getter, Class<P> clazz, int sqltype, Object... properties) {
    PropertyMeta<T, P> propertyMeta = mock(PropertyMeta.class);
    when(propertyMeta.getGetter()).thenReturn(getter);
    when(propertyMeta.getPropertyType()).thenReturn(clazz);
    return new PropertyMapping<T, P, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(propertyMeta, new JdbcColumnKey("col", index++, sqltype), FieldMapperColumnDefinition.<JdbcColumnKey>of(properties));
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping)

Example 2 with JdbcColumnKey

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

the class ResultSetFieldMapperFactoryTest method testPrimitiveField.

@Test
public void testPrimitiveField() {
    ClassMeta<DbObject> classMeta = ReflectionService.newInstance(false).getClassMeta(DbObject.class);
    PropertyMeta<DbObject, Long> id = classMeta.newPropertyFinder(ConstantPredicate.<PropertyMeta<?, ?>>truePredicate()).<Long>findProperty(new DefaultPropertyNameMatcher("id", 0, false, false), new Object[0]);
    FieldMapperColumnDefinition<JdbcColumnKey> identity = FieldMapperColumnDefinition.identity();
    PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 1), identity);
    FieldMapper<ResultSet, DbObject> fieldMapper = factory.newFieldMapper(propertyMapping, null, RethrowMapperBuilderErrorHandler.INSTANCE);
    assertTrue(fieldMapper instanceof LongFieldMapper);
    PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>> propertyMapping1 = new PropertyMapping<DbObject, Long, JdbcColumnKey, FieldMapperColumnDefinition<JdbcColumnKey>>(id, new JdbcColumnKey("id", 0), identity);
    fieldMapper = factory.newFieldMapper(propertyMapping1, null, RethrowMapperBuilderErrorHandler.INSTANCE);
    assertTrue(fieldMapper instanceof LongFieldMapper);
}
Also used : FieldMapperColumnDefinition(org.simpleflatmapper.map.property.FieldMapperColumnDefinition) LongFieldMapper(org.simpleflatmapper.map.fieldmapper.LongFieldMapper) DbObject(org.simpleflatmapper.test.beans.DbObject) DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) PropertyMapping(org.simpleflatmapper.map.mapper.PropertyMapping) ResultSet(java.sql.ResultSet) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Test(org.junit.Test)

Example 3 with JdbcColumnKey

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

the class JoinJdbcMapperCustomKeyTest method test.

@Test
public void test() throws SQLException {
    JdbcMapper<Person> mapper = JdbcMapperFactory.newInstance().addColumnDefinition(new Predicate<JdbcColumnKey>() {

        @Override
        public boolean test(JdbcColumnKey jdbcColumnKey) {
            return jdbcColumnKey.getIndex() <= 2;
        }
    }, FieldMapperColumnDefinition.<JdbcColumnKey>key(new Predicate<PropertyMeta<?, ?>>() {

        @Override
        public boolean test(PropertyMeta<?, ?> propertyMeta) {
            return propertyMeta.getPath().startsWith("key.");
        }
    })).newBuilder(Person.class).addMapping("key_tag").addMapping("key_n").addMapping("name").addMapping("phones_value").mapper();
    ResultSet rs = JoinJdbcMapperOnTupleTest.setUpResultSetMock(new String[] { "key_tag", "key_n", "name", "phones_value" }, new Object[][] { { "t1", 1, "t11", "p111" }, { "t1", 1, "t11", "p112" }, { "t1", 2, "t12", "p121" } });
    final List<Person> list = mapper.forEach(rs, new ListCollector<Person>()).getList();
    assertEquals(2, list.size());
    assertEquals("t1", list.get(0).key.tag);
    assertEquals(1, list.get(0).key.n);
    assertEquals("t11", list.get(0).name);
    assertEquals(Arrays.asList("p111", "p112"), list.get(0).phones);
    assertEquals("t1", list.get(1).key.tag);
    assertEquals(2, list.get(1).key.n);
    assertEquals("t12", list.get(1).name);
    assertEquals(Arrays.asList("p121"), list.get(1).phones);
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) ListCollector(org.simpleflatmapper.util.ListCollector) ResultSet(java.sql.ResultSet) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Predicate(org.simpleflatmapper.util.Predicate) Test(org.junit.Test)

Example 4 with JdbcColumnKey

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

the class JdbcMapperFactoryTest method testFieldErrorHandlingOnResultSet.

@Test
public void testFieldErrorHandlingOnResultSet() throws SQLException, Exception, ParseException {
    @SuppressWarnings("unchecked") FieldMapperErrorHandler<JdbcColumnKey> fieldMapperErrorHandler = mock(FieldMapperErrorHandler.class);
    ResultSet rs = mock(ResultSet.class);
    final Exception exception = new SQLException("Error!");
    JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().fieldMapperErrorHandler(fieldMapperErrorHandler).newBuilder(DbObject.class).addMapping("id").mapper();
    when(rs.next()).thenReturn(true, false);
    when(rs.getLong(1)).thenThrow(exception);
    List<DbObject> list = mapper.forEach(rs, 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) ListCollector(org.simpleflatmapper.util.ListCollector) DbObject(org.simpleflatmapper.test.beans.DbObject) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 5 with JdbcColumnKey

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

the class JdbcMapperSubObjectTest method testMapInnerObjectWithColumnDefinition.

@Test
public void testMapInnerObjectWithColumnDefinition() throws Exception {
    JdbcMapperBuilder<Db1DeepObject> builder = JdbcMapperFactoryHelper.asm().newBuilder(Db1DeepObject.class);
    FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition = FieldMapperColumnDefinition.customGetter(new Getter<ResultSet, String>() {

        @Override
        public String get(ResultSet target) throws Exception {
            return "ov1";
        }
    });
    builder.addMapping("db_object_name", columnDefinition);
    final JdbcMapper<Db1DeepObject> mapper = builder.mapper();
    ResultSet rs = mock(ResultSet.class);
    when(rs.getString(1)).thenReturn("name1");
    when(rs.next()).thenReturn(true, false);
    Db1DeepObject next = mapper.iterator(rs).next();
    assertEquals("ov1", next.getDbObject().getName());
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) Db1DeepObject(org.simpleflatmapper.test.beans.Db1DeepObject) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

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