public static void UpdateObject(
     DbConnection conn,
     object obj,
     string tableName,
     string[] idFieldNames,
     string[] includeFields,
     string[] excludeFields,
     ObjectsChangeTracker changeTracker,
     DbSettings dbSettings
     )
 {
     using (var cmd = conn.CreateCommand())
     {
         if (
             cmd.BuildUpdateCommand(
                 obj,
                 tableName,
                 idFieldNames,
                 includeFields,
                 excludeFields,
                 changeTracker,
                 dbSettings
                 )
             )
         {
             cmd.ExecuteNonQuery();
         }
     }
 }
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        /// <summary>
        /// Reads the single.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="changeTracker">The change tracker.</param>
        /// <returns>`0.</returns>
        public TEntity ReadSingle(IDataReader reader, ObjectsChangeTracker changeTracker)
        {
            TEntity result = MapUsingState(reader, reader);

            changeTracker?.RegisterObject(result);
            return(result);
        }
예제 #5
0
        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);
        }
예제 #6
0
 /// <summary>
 /// Updates the object.
 /// </summary>
 /// <param name="conn">The conn.</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>
 public static Task <int> UpdateObject(
     DbConnection conn,
     object obj,
     string tableName,
     string[] idFieldNames,
     string[] includeFields,
     string[] excludeFields,
     ObjectsChangeTracker changeTracker,
     DbSettings dbSettings
     )
 {
     using (var cmd = conn.CreateCommand())
     {
         if (
             cmd.BuildUpdateCommand(
                 obj,
                 tableName,
                 idFieldNames,
                 includeFields,
                 excludeFields,
                 changeTracker,
                 dbSettings
                 )
             )
         {
             return(cmd.ExecuteNonQueryAsync());
         }
         return(Task.FromResult(0));
     }
 }
예제 #7
0
		public void UpdateCustomer()
		{
			ObjectMapperManager objMan = new ObjectMapperManager();

			Guid guid = Guid.NewGuid();

			using (var ts = new TransactionScope())
			using (var connection = CreateConnection())
			{
				var customer = DBTools.ExecuteReader(
					connection,
					"select top 1 * from [dbo].[Customers]",
					null,
					r => r.ToObject<Customer>()
				);

				var tracker = new ObjectsChangeTracker();
				tracker.RegisterObject(customer);
				customer.Address = guid.ToString();

				DBTools.UpdateObject(
					connection,
					customer,
					"Customers",
					new[] { "CustomerID" },
					tracker,
					DbSettings.MSSQL
				);
			}
		}
예제 #8
0
        public T ReadSingle(DbDataReader reader, ObjectsChangeTracker changeTracker)
        {
            T result = reader.Read() ? MapUsingState(reader, reader) : default(T);

            if (changeTracker != null)
            {
                changeTracker.RegisterObject(result);
            }
            return(result);
        }
 public static void UpdateObject(
     DbConnection conn,
     object obj,
     string tableName,
     string[] idFieldNames,
     ObjectsChangeTracker changeTracker,
     DbSettings dbSettings
     )
 {
     UpdateObject(conn, obj, tableName, idFieldNames, null, null, changeTracker, dbSettings);
 }
예제 #10
0
 /// <summary>
 /// Updates the object.
 /// </summary>
 /// <param name="conn">The conn.</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="changeTracker">The change tracker.</param>
 /// <param name="dbSettings">The db settings.</param>
 public static Task <int> UpdateObject(
     DbConnection conn,
     object obj,
     string tableName,
     string[] idFieldNames,
     ObjectsChangeTracker changeTracker,
     DbSettings dbSettings
     )
 {
     return(UpdateObject(conn, obj, tableName, idFieldNames, null, null, changeTracker, dbSettings));
 }
예제 #11
0
 /// <summary>
 /// Reads the collection.
 /// </summary>
 /// <param name="reader">The reader.</param>
 /// <param name="changeTracker">The change tracker.</param>
 /// <returns>IEnumerable{`0}.</returns>
 public IEnumerable <TEntity> ReadCollection(IDataReader reader, ObjectsChangeTracker changeTracker)
 {
     while (reader.Read())
     {
         TEntity result = MapUsingState(reader, reader);
         if (changeTracker != null)
         {
             changeTracker.RegisterObject(result);
         }
         yield return(result);
     }
 }
예제 #12
0
 /// <summary>
 /// Reads the collection.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="conn">The conn.</param>
 /// <param name="commandText">The command text.</param>
 /// <param name="cmdParams">The CMD params.</param>
 /// <param name="excludeFields">The exclude fields.</param>
 /// <param name="changeTracker">The change tracker.</param>
 /// <returns>IEnumerable{``0}.</returns>
 public static IEnumerable <T> ReadCollection <T>(
     DbConnection conn,
     string commandText,
     CmdParams cmdParams,
     string[] excludeFields,
     ObjectsChangeTracker changeTracker)
 {
     using (var cmd = CreateCommand(conn, commandText, cmdParams))
         using (var reader = cmd.ExecuteReader())
         {
             while (reader.Read())
             {
                 yield return(reader.ToObject <T>(null, excludeFields, changeTracker));
             }
         }
 }
예제 #13
0
 public static IEnumerable <T> ToObjects <T>(this DbDataReader reader, string readerName, string[] excludeFields, ObjectsChangeTracker changeTracker)
 {
     if (string.IsNullOrEmpty(readerName))
     {
         var mappingKeyBuilder = new StringBuilder();
         for (int i = 0; i < reader.FieldCount; ++i)
         {
             mappingKeyBuilder.Append(reader.GetName(i));
             mappingKeyBuilder.Append(' ');
         }
         readerName = mappingKeyBuilder.ToString();
     }
     return(new DataReaderToObjectMapper <T>(readerName, null, excludeFields).ReadCollection(reader, changeTracker));
 }
예제 #14
0
        public static T ToObject <T>(this DbDataReader reader, string readerName, string[] excludeFields, ObjectsChangeTracker changeTracker)
        {
            T result = new DataReaderToObjectMapper <T>(readerName, null, excludeFields).MapUsingState(reader, reader);

            if (changeTracker != null)
            {
                changeTracker.RegisterObject(result);
            }
            return(result);
        }
        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;
        }
예제 #16
0
        /// <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);
        }