private final <T> void setValue(int index, Field<T> field, T value) { Value<T> val = getValue0(index); UniqueKey<?> key = getPrimaryKey(); // Normal fields' changed flag is always set to true if (key == null || !key.getFields().contains(field)) { val.setValue(value); } // The primary key's changed flag might've been set previously else if (val.isChanged()) { val.setValue(value); } // [#2764] Users may override updatability of primary key values else if (updatablePrimaryKeys(settings(this))) { val.setValue(value); } // [#2698] If the primary key has not yet been set else if (val.getOriginal() == null) { val.setValue(value); } // [#979] If the primary key is being changed, all other fields' flags // need to be set to true for in case this record is stored again, an // INSERT statement will thus be issued else { val.setValue(value, true); if (val.isChanged()) { changed(true); } } }
@Override public final boolean changed() { for (Value<?> value : getValues()) { if (value.isChanged()) { return true; } } return false; }
@Override public R operate(R target) throws MappingException { AbstractRecord source = AbstractRecord.this; try { // [#1522] [#2989] If possible the complete state of this record should be copied onto the // other record if (target instanceof AbstractRecord) { AbstractRecord t = (AbstractRecord) target; // Iterate over target fields, to avoid ambiguities when two source fields share the same // name. for (int targetIndex = 0; targetIndex < t.size(); targetIndex++) { Field<?> targetField = t.field(targetIndex); int sourceIndex = fields.indexOf(targetField); if (sourceIndex >= 0) { DataType<?> targetType = targetField.getDataType(); Value<?> sourceValue = values[sourceIndex]; t.values[targetIndex] = new Value<Object>( targetType.convert(sourceValue.getValue()), targetType.convert(sourceValue.getOriginal()), sourceValue.isChanged()); } } } else { for (Field<?> targetField : target.fields()) { Field<?> sourceField = field(targetField); if (sourceField != null) { Utils.setValue(target, targetField, source, sourceField); } } } return target; } // All reflection exceptions are intercepted catch (Exception e) { throw new MappingException("An error ocurred when mapping record to " + target, e); } }