/** * add a new mapping to the specified property with the specified index, the specified type. * * @param column the property name * @param index the property index * @param sqlType the property type, @see java.sql.Types * @param properties the property properties * @return the current builder */ public JdbcMapperBuilder<T> addMapping(final String column, final int index, final int sqlType, Object... properties) { return addMapping(new JdbcColumnKey(column, index, sqlType), properties); }
/** * add a new mapping to the specified property with the specified index, the specified type. * * @param column the property name * @param index the property index * @param sqlType the property type, @see java.sql.Types * @param columnDefinition the property definition * @return the current builder */ public JdbcMapperBuilder<T> addMapping(final String column, final int index, final int sqlType, FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition) { return addMapping(new JdbcColumnKey(column, index, sqlType), columnDefinition); }
/** * add a new mapping to the specified property with the specified index, the specified type. * * @param column the property name * @param index the property index * @param sqlType the property type, @see java.sql.Types * @param properties the property properties * @return the current builder */ public JdbcMapperBuilder<T> addMapping(final String column, final int index, final int sqlType, Object... properties) { return addMapping(new JdbcColumnKey(column, index, sqlType), properties); }
public static <T> JdbcMapperBuilder<T> addColumn(JdbcMapperBuilder<T> builder) { builder.addMapping("id"); builder.addMapping("name"); builder.addMapping("email"); builder.addMapping("creation_time"); builder.addMapping("type_ordinal"); builder.addMapping("type_name"); return builder; }
/** * add a new mapping to the specified property with the specified index, the specified type. * * @param column the property name * @param index the property index * @param sqlType the property type, @see java.sql.Types * @param columnDefinition the property definition * @return the current builder */ public JdbcMapperBuilder<T> addMapping(final String column, final int index, final int sqlType, FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition) { return addMapping(new JdbcColumnKey(column, index, sqlType), columnDefinition); }
/** * add a new mapping to the specified property with the specified index and the specified type. * * @param column the property name * @param index the property index * @param sqlType the property type, @see java.sql.Types * @return the current builder */ public JdbcMapperBuilder<T> addMapping(final String column, final int index, final int sqlType) { addMapping(column, index, sqlType, FieldMapperColumnDefinition.<JdbcColumnKey>identity()); return this; }
@Override public SetRowMapper<ResultSet,ResultSet,T,SQLException> newInstance(MapperKey<JdbcColumnKey> jdbcColumnKeyMapperKey) { final JdbcMapperBuilder<T> builder = newBuilder(classMeta); for(JdbcColumnKey key : jdbcColumnKeyMapperKey.getColumns()) { builder.addMapping(key); } return builder.mapper(); } }
@Override public ContextualSourceFieldMapper<ResultSet, T> newInstance(MapperKey<JdbcColumnKey> jdbcColumnKeyMapperKey) { final JdbcMapperBuilder<T> builder = newBuilder(classMeta); for(JdbcColumnKey key : jdbcColumnKeyMapperKey.getColumns()) { builder.addMapping(key); } return builder.newSourceFieldMapper(); } }
@Test public void testConstructorProp1Prop2Prop3() throws Exception { JdbcMapperBuilder<DbConstructorObject> builder = JdbcMapperFactoryHelper.asm().newBuilder(DbConstructorObject.class); builder.addMapping("prop1"); builder.addMapping("prop2"); try { builder.addMapping("prop3"); fail("Expect exception"); } catch(Exception e) { // expected } }
private static <T, K> JdbcMapper<T> buildSelectMapper(ClassMeta<T> target, CrudMeta crudMeta, JdbcMapperFactory jdbcMapperFactory) throws SQLException { JdbcMapperBuilder<T> mapperBuilder = jdbcMapperFactory.<T>newBuilder(target); int i = 1; for(ColumnMeta cm : crudMeta.getColumnMetas()) { mapperBuilder.addMapping(cm.toJdbcColumnKey(i)); i++; } return mapperBuilder.mapper(); }
private void testMatchConstructor(JdbcMapperBuilder<MyObject> builder) throws SQLException { builder.addMapping("prop"); JdbcMapper<MyObject> mapper = builder.mapper(); ResultSet rs = mock(ResultSet.class); when(rs.getString(1)).thenReturn("val"); assertEquals("val", mapper.map(rs).prop.value); } }
@Test public void testCanCreateTypeFromAmbiguousWithType() throws Exception { JdbcMapperBuilder<MyObjectAmbiguity> builder = JdbcMapperFactoryHelper.asm().newBuilder(MyObjectAmbiguity.class); builder.addMapping("prop", 1, Types.VARCHAR); JdbcMapper<MyObjectAmbiguity> mapper = builder.mapper(); ResultSet rs = mock(ResultSet.class); when(rs.getString(1)).thenReturn("val"); assertEquals("val", mapper.map(rs).prop.value); } private void testMatchConstructor(JdbcMapperBuilder<MyObject> builder)
@Test public void testIndexedEnumOrdinal() throws Exception { JdbcMapperBuilder<DbEnumObject> builder = JdbcMapperFactoryHelper.asm().newBuilder(DbEnumObject.class); builder.addMapping("val",1, Types.INTEGER); JdbcMapper<DbEnumObject> mapper = builder.mapper(); ResultSet rs = mock(ResultSet.class); when(rs.getInt(1)).thenReturn(2); assertEquals(DbObject.Type.type3, mapper.map(rs).getVal()); }
@Test public void jdbcMapperExtrapolateGetterFromConstructor() { JdbcMapperFactory.newInstance().newBuilder(FooC.class).addMapping("bar").mapper(); JdbcMapperFactory.newInstance().newBuilder(Crux.class).addMapping("foo").mapper(); }
@Test public void testWithWrongColumn() throws MappingException, SQLException { JdbcMapperBuilder<DbObject> builder = JdbcMapperFactoryHelper.asm().mapperBuilderErrorHandler(MapperBuilderErrorHandler.NULL).newBuilder(DbObject.class); builder.addMapping("no_id").addMapping("no_name").addMapping("email"); JdbcMapper<DbObject> mapper = builder.mapper(); List<DbObject> l = mapper.forEach(new MockDbObjectResultSet(1), new ListCollector<DbObject>()).getList(); assertEquals(1, l.size()); assertEquals(0, l.get(0).getId()); assertNull(l.get(0).getName()); assertEquals("email1", l.get(0).getEmail()); }
@Override public void handle(PreparedStatement t) throws Exception { ResultSet rs = t.executeQuery(); final JdbcMapper<DbListOfString> mapper = JdbcMapperFactoryHelper.asm().useAsm(asm).newBuilder( DbListOfString.class).addKey("id").addKey("objects_0") .addMapping(rs.getMetaData()).mapper(); List<DbListOfString> strings = mapper.forEach(rs, new ListCollector<DbListOfString>()).getList(); assertEquals(1, strings.size()); assertEquals(12, strings.get(0).getId()); assertEquals(1, strings.get(0).getObjects().size()); } }
@Test public void setChangeFieldMapperErrorHandler() throws NoSuchMethodException, SecurityException, IOException { JdbcMapperBuilder<DbObject> builder = JdbcMapperFactoryHelper .asm() .fieldMapperErrorHandler(new LogFieldMapperErrorHandler<JdbcColumnKey>()) .newBuilder(DbObject.class); builder.addMapping("id"); }
@Override public void handle(PreparedStatement t) throws Exception { ResultSet rs = t.executeQuery(); JdbcMapper<Db2DeepObject> mapper = builder.addMapping(rs.getMetaData()).mapper(); rs.next(); Db2DeepObject object = mapper.map(rs); assertEquals(33, object.getId()); assertEquals(32, object.getDb1Object().getId()); assertEquals("value12", object.getDb1Object().getValue()); DbHelper.assertDbObjectMapping(object.getDb1Object().getDbObject()); } }, "select 33 as id, "
@Test public void test544() { final String VALUES_VAL = "values_val"; final String VALUES = "values"; JdbcColumnKey valuesKeys = new JdbcColumnKey(VALUES, 1, Types.VARCHAR); JdbcMapperFactory .newInstance() .addAlias(VALUES, VALUES_VAL) .newBuilder(C544.class) .addMapping(valuesKeys) .mapper(); }
private void validateBuilderInverted(JdbcMapperBuilder<Tuple2<List<Person>, Person>> builder) throws Exception { for(int i = 0; i < columns.length; i++) { builder.addMapping(columns[i]); } final JdbcMapper<Tuple2<List<Person>, Person>> mapper = builder.mapper(); List<Tuple2<List<Person>, Person>> professors = mapper.forEach(setUpResultSetMock(columns, rowsInverted), new ListCollector<Tuple2<List<Person>, Person>>()).getList(); validateProfessorsInverted(professors); }