public static bool AreEqualOfUnknownType(object sourceValue, object targetValue) { if (sourceValue is bool && targetValue is bool) { return(AreEqualBooleans(sourceValue, targetValue)); } else if (sourceValue is DateTime && targetValue is DateTime) { return(AreEqualDateTimes(sourceValue, targetValue)); } else if ((sourceValue is byte || sourceValue is int) && (targetValue is byte || targetValue is int)) { return(AreEqualIntegers(sourceValue, targetValue)); } else if ((sourceValue is decimal || sourceValue is double || sourceValue is float) && (targetValue is decimal || targetValue is double || targetValue is float)) { return(AreEqualNumerics(sourceValue, targetValue)); } else { return(FieldComparer.AreEqualStrings(sourceValue, targetValue)); } }
private static RecordToUpdate GetUpdateRecord(OneToMany_OneWayDataMap map, EntityBatch batch, Dictionary <string, DataOnlyField> dataOnlyFields, DataRow oneSideRow, IEnumerable <DataRow> manySideFilteredRows, TransposeResult transposeResult, Dictionary <string, string> transposeDataOnlyValues, IEnumerable <CustomSetField> customSetFieldsForUpdate) { var transposeRecordToUpdate = (TransposeResult_UpdateRecord)transposeResult; var recordToUpdate = new RecordToUpdate(batch, transposeRecordToUpdate.PrimaryKeyValues); foreach (var fieldValuePair in transposeRecordToUpdate.FieldValuePairs) { string oldValueAsString; var oldValue = transposeRecordToUpdate.AssociatedManySideDataRow[fieldValuePair.Key]; if (oldValue == null || oldValue == DBNull.Value) { oldValueAsString = null; } else { oldValueAsString = oldValue.ToString(); } if (!FieldComparer.AreEqualOfUnknownType(oldValueAsString, fieldValuePair.Value)) { var oldAndNewValues = new UpdateValueSet(oldValueAsString, fieldValuePair.Value); recordToUpdate.AddFieldValuePair(fieldValuePair.Key, oldAndNewValues); } } foreach (var transposeDataOnlyValue in transposeDataOnlyValues) { recordToUpdate.DataOnlyValues.Add(transposeDataOnlyValue.Key, transposeDataOnlyValue.Value); } foreach (var dataOnlyField in dataOnlyFields) { if (!recordToUpdate.DataOnlyValues.ContainsKey(dataOnlyField.Key)) { if (dataOnlyField.Value.MethodToPopulateValue == null && transposeRecordToUpdate.FieldValuePairs.ContainsKey(dataOnlyField.Key)) { recordToUpdate.DataOnlyValues.Add(dataOnlyField.Key, transposeRecordToUpdate.FieldValuePairs[dataOnlyField.Key]); } else if (dataOnlyField.Value.MethodToPopulateValue != null) { recordToUpdate.DataOnlyValues.Add(dataOnlyField.Key, (dataOnlyField.Value.MethodToPopulateValue(oneSideRow) ?? "").ToString()); } else { if (batch.EntityDefinition.SyncSide == SyncSide.Source) { throw new Exception(string.Format("Source-side data only field '{0}' is not mapped.", dataOnlyField.Key)); } else if (batch.EntityDefinition.SyncSide == SyncSide.Target) { throw new Exception(string.Format("Target-side data only field '{0}' is not mapped.", dataOnlyField.Key)); } else { throw new EnumValueNotImplementedException <SyncSide>(batch.EntityDefinition.SyncSide); } } } } foreach (var customSetField in customSetFieldsForUpdate) { if (customSetField.OnlyApplyWithOtherChanges == false || recordToUpdate.FieldValuePairs.Count > 0) { string oldValueAsString; var oldValue = transposeRecordToUpdate.AssociatedManySideDataRow[customSetField.FieldNameToCompare]; if (oldValue == null || oldValue == DBNull.Value) { oldValueAsString = null; } else { oldValueAsString = oldValue.ToString(); } var customSetValue = customSetField.CustomSetMethod(oneSideRow); var oldAndNewValues = new UpdateValueSet(oldValueAsString, customSetValue == null ? null : customSetValue.ToString()); if (!FieldComparer.AreEqualOfUnknownType(oldValueAsString, customSetValue)) { recordToUpdate.AddFieldValuePair(customSetField.FieldNameToUpdate, oldAndNewValues); } // add as data only field, if applicable if (dataOnlyFields.ContainsKey(customSetField.FieldNameToUpdate) && dataOnlyFields[customSetField.FieldNameToUpdate].MethodToPopulateValue == null) { recordToUpdate.DataOnlyValues.Add(customSetField.FieldNameToUpdate, oldAndNewValues.NewValue); } } } // add secondary keys, if applicable AddSecondaryKeyValues(map.EntityToUpdateDefinition, ref recordToUpdate); return(recordToUpdate); }