[#1986] Add Record.fromMap() as the inverse operation of
Record.intoMap()
This commit is contained in:
parent
fb0127bc6f
commit
e8b0133cdf
@ -184,6 +184,10 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
|
||||
// ----------
|
||||
B book = create().selectFrom(TBook()).where(TBook_ID().equal(1)).fetchOne();
|
||||
Map<String, Object> map4 = create().selectFrom(TBook()).where(TBook_ID().equal(1)).fetchOneMap();
|
||||
B book2 = create().newRecord(TBook());
|
||||
book2.fromMap(map4);
|
||||
|
||||
assertEquals(book, book2);
|
||||
|
||||
for (Field<?> field : books.getFields()) {
|
||||
assertEquals(book.getValue(field), map4.get(field.getName()));
|
||||
|
||||
@ -398,8 +398,11 @@ public interface Record extends FieldProvider, Attachable {
|
||||
|
||||
/**
|
||||
* Return this record as a name/value map.
|
||||
* <p>
|
||||
* This is the inverse operation to {@link #fromMap(Map)}
|
||||
*
|
||||
* @return This record as a map
|
||||
* @see #fromMap(Map)
|
||||
*/
|
||||
Map<String, Object> intoMap();
|
||||
|
||||
@ -564,8 +567,8 @@ public interface Record extends FieldProvider, Attachable {
|
||||
* or {@link ResultSet#previous()}, etc.</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* You may use {@link Executor#fetch(ResultSet)} to unwind this
|
||||
* wrapper again.
|
||||
* You may use {@link Executor#fetch(ResultSet)} to unwind this wrapper
|
||||
* again.
|
||||
* <p>
|
||||
* This is the same as creating a new {@link Result} with this
|
||||
* <code>Record</code> only, and then calling {@link Result#intoResultSet()}
|
||||
@ -637,4 +640,18 @@ public interface Record extends FieldProvider, Attachable {
|
||||
*/
|
||||
void from(Object source) throws MappingException;
|
||||
|
||||
/**
|
||||
* Load data from a map into this record
|
||||
* <p>
|
||||
* The argument map is expected to hold field-name / value pairs where
|
||||
* field-names correspond to actual field names as provided by
|
||||
* {@link #getField(String)}. Missing fields will be left untouched. Excess
|
||||
* fields will be ignored.
|
||||
* <p>
|
||||
* This is the inverse operation to {@link #intoMap()}
|
||||
*
|
||||
* @see #intoMap()
|
||||
*/
|
||||
void fromMap(Map<String, ?> map);
|
||||
|
||||
}
|
||||
|
||||
@ -723,6 +723,22 @@ abstract class AbstractRecord extends AbstractStore implements Record {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void fromMap(Map<String, ?> map) {
|
||||
List<Field<?>> f = getFields();
|
||||
int size = f.size();
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
Field<?> field = f.get(i);
|
||||
String name = field.getName();
|
||||
|
||||
// Set only those values contained in the map
|
||||
if (map.containsKey(name)) {
|
||||
Utils.setValue(this, field, map.get(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method was implemented with [#799]. It may be useful to make it
|
||||
* public for broader use...?
|
||||
|
||||
Loading…
Reference in New Issue
Block a user