/** * Will create a newInstance of JdbcMapper based on the specified metadata and the target class. * @param target the target class of the jdbcMapper * @param metaData the metadata to create the jdbcMapper from * @param <T> the jdbcMapper target type * @return a jdbcMapper that will map the data represented by the metadata to an newInstance of target * @throws java.sql.SQLException if an error occurs getting the metaData */ public <T> JdbcMapper<T> newMapper(final Class<T> target, final ResultSetMetaData metaData) throws SQLException { JdbcMapperBuilder<T> builder = newBuilder(target); builder.addMapping(metaData); return builder.mapper(); }
@Test public void testJoinTableGSManualMapping() throws Exception { JdbcMapper<ProfessorGS> mapper = JdbcMapperFactoryHelper.asm() .newBuilder(ProfessorGS.class) .addKey("id") .addMapping("name") .addKey("students_id") .addMapping("students_name") .addMapping("students_phones_value") .mapper(); validateMapper(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 testInstantiateBuilderOnType() throws SQLException { JdbcMapperBuilder<DbObject> builder = new JdbcMapperBuilder<DbObject>(DbObject.class); builder.addMapping("id").addMapping("name"); final JdbcMapper<DbObject> mapper = builder.mapper(); List<DbObject> l = mapper.forEach(new MockDbObjectResultSet(1), new ListCollector<DbObject>()).getList(); assertEquals(1, l.size()); assertEquals(1, l.get(0).getId()); assertEquals("name1", l.get(0).getName()); }
/** * add the all the property present in the metaData * * @param metaData the metaDAta * @return the current builder * @throws SQLException when an error occurs getting the metaData */ public JdbcMapperBuilder<T> addMapping(final ResultSetMetaData metaData) throws SQLException { for (int i = 1; i <= metaData.getColumnCount(); i++) { addMapping(metaData.getColumnLabel(i), i, metaData.getColumnType(i)); } return this; }
private <T extends PrimitiveObject> void testIndexedPrimitives(JdbcMapperBuilder<T> builder) throws SQLException, Exception { addIndexedColumn(builder); testPrimitives(builder.mapper()); } private void testIndexedPrimitivesWithNull(JdbcMapperBuilder<DbBoxedPrimitiveObject> builder)
@Test public void testAddFieldMapper() { JdbcMapperBuilder<DbObject> builder = JdbcMapperFactoryHelper.asm().newBuilder(DbObject.class); JdbcMapper<DbObject> mapper = builder.addMapper(new FieldMapper<ResultSet, DbObject>() { @Override public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> mappingContext) throws MappingException { target.setId(33); } }).mapper(); assertEquals(33, mapper.map(null).getId()); }
public <T> JdbcMapperBuilder<T> newBuilder(ClassMeta<T> classMeta) { MapperConfig<JdbcColumnKey, ResultSet> mapperConfig = mapperConfig(); return new JdbcMapperBuilder<T>( classMeta, mapperConfig, getterFactory, new JdbcMappingContextFactoryBuilder(!mapperConfig.unorderedJoin())); }
/** * add the all the property present in the metaData * * @param metaData the metaDAta * @return the current builder * @throws SQLException when an error occurs getting the metaData */ public JdbcMapperBuilder<T> addMapping(final ResultSetMetaData metaData) throws SQLException { for (int i = 1; i <= metaData.getColumnCount(); i++) { addMapping(metaData.getColumnLabel(i), i, metaData.getColumnType(i)); } return this; }
private void testIndexedPrimitivesWithNull(JdbcMapperBuilder<DbBoxedPrimitiveObject> builder) throws SQLException, Exception { addIndexedColumn(builder); testPrimitivesWithNullValues(builder.mapper()); } private <T extends PrimitiveObject> void addIndexedColumn(
public <T> JdbcMapperBuilder<T> newBuilder(ClassMeta<T> classMeta) { MapperConfig<JdbcColumnKey, ResultSet> mapperConfig = mapperConfig(); return new JdbcMapperBuilder<T>( classMeta, mapperConfig, getterFactory, new JdbcMappingContextFactoryBuilder(!mapperConfig.unorderedJoin())); }
/** * Will create a newInstance of JdbcMapper based on the specified metadata and the target class. * @param target the target class of the jdbcMapper * @param metaData the metadata to create the jdbcMapper from * @param <T> the jdbcMapper target type * @return a jdbcMapper that will map the data represented by the metadata to an newInstance of target * @throws java.sql.SQLException if an error occurs getting the metaData */ public <T> JdbcMapper<T> newMapper(final Class<T> target, final ResultSetMetaData metaData) throws SQLException { JdbcMapperBuilder<T> builder = newBuilder(target); builder.addMapping(metaData); return builder.mapper(); }
@Test public void testUser() throws SQLException { JdbcMapper<User> mapper = userJdbcMapperFactory.newBuilder(User.class).addKey("id").addMapping("name").addMapping("roles_name").mapper(); ResultSet rs = mock(ResultSet.class); when(rs.next()).thenReturn(true, true, false); when(rs.getInt(1)).thenReturn(1, 1); when(rs.getString(2)).thenReturn("n1", "n1"); when(rs.getString(3)).thenReturn("r1", "r2"); Iterator<User> iterator = mapper.iterator(rs); User u = iterator.next(); assertEquals(1, u.id); assertEquals("n1", u.name); assertEquals(2, u.roles.size()); }
/** * 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); }
private void testMapperBuilderWithIterator(JdbcMapperBuilder<DbObject> builder) throws Exception { addColumn(builder); final JdbcMapper<DbObject> mapper = builder.mapper(); DbHelper.testDbObjectFromDb(new TestRowHandler<PreparedStatement>() { @Override public void handle(PreparedStatement ps) throws Exception { Iterator<DbObject> objects = mapper.iterator(ps.executeQuery()); assertTrue(objects.hasNext()); DbHelper.assertDbObjectMapping(objects.next()); assertFalse(objects.hasNext()); } }); }
@Test public void jdbcMapperCustomGetter() { JdbcMapperFactory .newInstance() .addColumnProperty("bar", new GetterProperty(new Getter<ResultSet, Bar2>() { @Override public Bar2 get(ResultSet target) throws Exception { return new Bar2(target.getString("bar"), 2); } })) .newBuilder(Foo2.class) .addKey("bar").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(); } }
@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(); } }