public void AddOperation(UpdateOperation operation) { var name = TableName; if (null != name && operation.TableName != name) { throw new ArgumentException(string.Format( "Table name mismatch for UPDATE command. Expected: {0}. Actual: {1}.", name, operation.TableName), "operation"); } var pk = PrimaryKeyAsObject; if (null != pk && !PrimaryKeyComparer.SuppliedPrimaryKeyValuesMatch( operation.OwnerMetadata, pk, operation.OwnerPrimaryKeyAsObject))//operation.OwnerPrimaryKey != pk.Value) { throw new ArgumentException(string.Format( "Primary key mismatch for UPDATE command on table {0}. Expected: {1}. Actual: {2}.", name, pk, operation.OwnerPrimaryKeyAsObject), "operation"); } name = PrimaryKeyColumn; if (null != name && operation.OwnerPrimaryKeyColumn != name) { throw new ArgumentException(string.Format( "Primary key column mismatch for UPDATE command on table {0}. Expected: {1}. Actual: {2}.", TableName, name, operation.OwnerPrimaryKeyColumn), "operation"); } TableName = operation.TableName; PrimaryKeyColumn = operation.OwnerPrimaryKeyColumn; _operations.Add(operation); }
private void Diff( object oldOwner, object newOwner, DtoMetadata ownerMetadata, PropertyMetadata property, object oldObject, object newObject, Type handleAsType, IList <Difference> target, bool softDelete, IList <Ancestor> ancestors) { ancestors = new List <Ancestor>(ancestors); var metadata = _dtoMetadataCache.GetValidatedMetadataFor(handleAsType); var doReferenceShortcut = false; if (oldObject == null) { if (newObject == null) { return; } doReferenceShortcut = true; } else if (newObject == null) { if (softDelete) { var propertyMetaData = SoftDeleteValidator.GetValidatedSoftDeleteProperty(metadata); var trueIndicatesDeleted = propertyMetaData.Prop.Attribute <SoftDeleteColumnAttribute>().TrueIndicatesDeleted;//GetAttribute<SoftDeleteColumnAttribute>().TrueIndicatesDeleted; target.Add(new Difference() { DifferenceType = DifferenceType.Update, OldValue = !trueIndicatesDeleted, NewValue = trueIndicatesDeleted, ValueMetadata = null, OwnerMetadata = metadata, OwnerPropertyMetadata = propertyMetaData, OldOwner = oldObject, NewOwner = oldObject, Path = ancestors }); return; } doReferenceShortcut = true; } if (doReferenceShortcut) { DiffReferenceType(oldObject, newObject, null, target, metadata, ancestors); } else { if (!PrimaryKeyComparer.HaveSamePrimaryKeyValue(metadata, oldObject, newObject))//objKey != metadata.GetPrimaryKeyValue(newObject)) { if (property == null) { throw new ArgumentException(string.Format( "Cannot diff two objects that do not represent the same row. " + "{0}: primary key does not match - for {1} does not match {2}", handleAsType, metadata.GetPrimaryKeyValueAsObject(oldObject), metadata.GetPrimaryKeyValueAsObject(newObject))); } target.Add(new Difference { OldOwner = oldOwner, NewOwner = newOwner, DifferenceType = DifferenceType.Update, OldValue = oldObject, NewValue = newObject, ValueMetadata = metadata, OwnerPropertyMetadata = property, OwnerMetadata = ownerMetadata, Path = ancestors }); // TODO: Bart - Possibly this may be needed? Hard to see how we wouldn't need to do this //DiffReferenceType(oldObject, null, null, target, metadata); //DiffReferenceType(null, newObject, null, target, metadata); // TODO: Bart - or more likely this, below. DiffProperties(metadata, oldObject, null, target, ancestors, property); DiffProperties(metadata, null, newObject, target, ancestors, property); } else { DiffProperties(metadata, oldObject, newObject, target, ancestors, property); } } }
public IEnumerable <BaseCommand> Coalesce(IEnumerable <BaseOperation> operations) { var results = new List <BaseCommand>(); var updates = new List <UpdateOperation>(); var updateColumns = CreateUpdateColumnSet(); DtoMetadata updateMetadata = null; string updateTableName = null; object updatePk = null; foreach (var operation in operations) { if (operation is UpdateOperation) { var update = operation as UpdateOperation; var newMetadata = GetNewUpdateMetadata(update); var newTableName = GetNewUpdateTableName(update); var newPrimaryKeyValue = GetNewUpdatePrimaryKeyValue(update); // If the table name, or row PK changes, we need to apply any UpdateOperations we already have as a new command // then start tracking UpdateOperations afresh, starting with this one. if (updateTableName != newTableName || updateMetadata != newMetadata || !PrimaryKeyComparer.SuppliedPrimaryKeyValuesMatch( newMetadata, updatePk, newPrimaryKeyValue) || updateColumns.Contains(update.ColumnName)) { ValidateUpdateOperation(update); if (null != updateTableName) { ApplyUpdatesSoFarAsNewCommand(results, updates, ref updateTableName, ref updatePk); } updateColumns = CreateUpdateColumnSet(); updateMetadata = newMetadata; updateTableName = newTableName; updatePk = newPrimaryKeyValue; } // TODO: replace UPDATE on same column updateColumns.Add(update.ColumnName); updates.Add(update); } else { ApplyUpdatesSoFarAsNewCommand(results, updates, ref updateTableName, ref updatePk); updateColumns = CreateUpdateColumnSet(); updateMetadata = null; updateTableName = null; updatePk = null; ValidateInsertOrDeleteOperation((BaseInsertDeleteOperation)operation); if (operation is InsertOperation) { results.Add(new InsertCommand(operation as InsertOperation)); } else if (operation is DeleteOperation) { results.Add(new DeleteCommand(operation as DeleteOperation)); } } } if (updates.Count > 0) { ApplyUpdatesSoFarAsNewCommand(results, updates, ref updateTableName, ref updatePk); } return(results); }