public void TestObjectsTracking() { var tracker = new ObjectsChangeTracker(); var a = new A(); tracker.RegisterObject(a); a.f2 = 3; var changes = tracker.GetChanges(a); Assert.IsTrue( changes[0].name == "f2"); tracker.RegisterObject(a); changes = tracker.GetChanges(a); Assert.IsTrue(changes.Length == 0); a.f1 = "new"; a.f2 = 13; a.f3 = false; for (int i = 0; i < 10; ++i) { tracker.GetChanges(a); } changes = tracker.GetChanges(a); Assert.IsTrue(TestUtils.AreEqualArraysUnordered(new[] { "f1", "f2", "f3" }, changes.Select(c => c.name).ToArray())); changes = tracker.GetChanges(new A()); Assert.IsNull(changes); }
public void TestObjectsTracking() { var tracker = new ObjectsChangeTracker(); var a = new A(); tracker.RegisterObject(a); a.f2 = 3; var changes = tracker.GetChanges(a); Assert.IsTrue(changes[0].name == "f2"); tracker.RegisterObject(a); changes = tracker.GetChanges(a); Assert.IsTrue(changes.Length == 0); a.f1 = "new"; a.f2 = 13; a.f3 = false; for (int i = 0; i < 10; ++i) { tracker.GetChanges(a); } changes = tracker.GetChanges(a); Assert.IsTrue(TestUtils.AreEqualArraysUnordered(new[] { "f1", "f2", "f3" }, changes.Select(c => c.name).ToArray())); changes = tracker.GetChanges(new A()); Assert.IsNull(changes); }
public void TestTwoVersionOfObjects() { var tracker = new ObjectsChangeTracker(); var original = new A() { f1 = "f1", f2 = 2, f3 = true }; var current = new A() { f1 = "f0", f2 = 2, f3 = false }; var changes = tracker.GetChanges(original, current); Assert.AreEqual(2, changes.Count()); Assert.AreEqual("f1", changes[0].name); Assert.AreEqual("f0", changes[0].CurrentValue); Assert.AreEqual("f3", changes[1].name); Assert.AreEqual(false, changes[1].CurrentValue); changes = tracker.GetChanges(original, null); Assert.IsNull(changes); changes = tracker.GetChanges(null, current); Assert.IsNull(changes); }
public static bool BuildUpdateCommand( this DbCommand cmd, object obj, string tableName, IEnumerable<string> idFieldNames, IEnumerable<string> includeFields, IEnumerable<string> excludeFields, ObjectsChangeTracker changeTracker, DbSettings dbSettings ) { if(idFieldNames == null) { idFieldNames = new string[0]; } idFieldNames = idFieldNames.Select (n => n.ToUpper()).ToArray(); if (changeTracker != null) { var changedFields = changeTracker.GetChanges(obj); if (changedFields != null) { if (includeFields == null) { includeFields = changedFields.Select(c => c.name).ToArray(); } else { includeFields = includeFields.Intersect(changedFields.Select(c => c.name)).ToArray(); } } } if (includeFields != null) { includeFields = includeFields.Concat(idFieldNames); } IMappingConfigurator config = new AddDbCommandsMappingConfig( dbSettings, includeFields, excludeFields, "updateop_inc_" + includeFields.ToCSV("_") + "_exc_" + excludeFields.ToCSV("_") ); var mapper = ObjectMapperManager.DefaultInstance.GetMapperImpl( obj.GetType(), typeof(DbCommand), config ); string[] fields = mapper .StroredObjects .OfType<SrcReadOperation>() .Select( m => m.Source.MemberInfo.Name ) .Where( f => !idFieldNames.Contains(f) ) .ToArray(); if (fields.Length == 0) { return false; } var cmdStr = "UPDATE " + tableName + " SET " + fields .Select( f => dbSettings.GetEscapedName(f) + "=" + dbSettings.GetParamName(f) ) .ToCSV(",") + " WHERE " + idFieldNames.Select(fn => dbSettings.GetEscapedName(fn) + "=" + dbSettings.GetParamName(fn)).ToCSV(" AND ") ; cmd.CommandText = cmdStr; cmd.CommandType = System.Data.CommandType.Text; mapper.Map(obj, cmd, null); return true; }
/// <summary> /// Builds the update command. /// </summary> /// <param name="cmd">The CMD.</param> /// <param name="obj">The obj.</param> /// <param name="tableName">Name of the table.</param> /// <param name="idFieldNames">The id field names.</param> /// <param name="includeFields">The include fields.</param> /// <param name="excludeFields">The exclude fields.</param> /// <param name="changeTracker">The change tracker.</param> /// <param name="dbSettings">The db settings.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public static bool BuildUpdateCommand( this DbCommand cmd, object obj, string tableName, IEnumerable <string> idFieldNames, IEnumerable <string> includeFields, IEnumerable <string> excludeFields, ObjectsChangeTracker changeTracker, DbSettings dbSettings ) { if (idFieldNames == null) { idFieldNames = new string[0]; } idFieldNames = idFieldNames.Select(n => n.ToUpper()).ToArray(); if (changeTracker != null) { var changedFields = changeTracker.GetChanges(obj); if (changedFields != null) { if (includeFields == null) { includeFields = changedFields.Select(c => c.name).ToArray(); } else { includeFields = includeFields.Intersect(changedFields.Select(c => c.name)).ToArray(); } } } if (includeFields != null) { includeFields = includeFields.Concat(idFieldNames); } IMappingConfigurator config = new AddDbCommandsMappingConfig( dbSettings, includeFields, excludeFields, "updateop_inc_" + includeFields.ToCSV("_") + "_exc_" + excludeFields.ToCSV("_") ); var mapper = ObjectMapperManager.DefaultInstance.GetMapperImpl( obj.GetType(), typeof(DbCommand), config ); string[] fields = mapper .StroredObjects .OfType <SrcReadOperation>() .Select(m => m.Source.MemberInfo.Name) .Where(f => !idFieldNames.Contains(f)) .ToArray(); if (fields.Length == 0) { return(false); } var cmdStr = "UPDATE " + tableName + " SET " + fields .Select( f => dbSettings.GetEscapedName(f.ToUpper()) + "=" + dbSettings.GetParamName(f.ToUpper()) ) .ToCSV(",") + " WHERE " + idFieldNames.Select(fn => dbSettings.GetEscapedName(fn) + "=" + dbSettings.GetParamName(fn)).ToCSV(" AND ") ; cmd.CommandText = cmdStr; cmd.CommandType = System.Data.CommandType.Text; mapper.Map(obj, cmd, null); return(true); }