Exemplo n.º 1
0
        /// <summary>
        /// Returns a new record containing the ad-hoc schema and values that describes what are
        /// the changes detected AT the original record compared against the target one, or null
        /// if no changes can be detected.
        /// <para>
        /// The new record returned contains its own ad-hoc schema, cloned from the entries of
        /// the original one, as well as the affected values that as they appeared in the source
        /// record.
        /// </para>
        /// </summary>
        /// <param name="source">The source record.</param>
        /// <param name="target">The target record.</param>
        /// <returns>A new record, with its ad-hoc schema, containing the changes, or null if no
        /// changes can be detected.</returns>
        public static IRecord Changes(this IRecord source, IRecord target)
        {
            if (source == null)
            {
                throw new NullReferenceException("Source cannot be null.");
            }
            if (source.IsDisposed)
            {
                throw new ObjectDisposedException(source.ToString());
            }
            if (source.Schema == null)
            {
                throw new InvalidOperationException("Source '{0}' carries no schema.".FormatWith(source));
            }

            if (target == null)
            {
                throw new ArgumentNullException("target", "Target cannot be null.");
            }
            if (target.IsDisposed)
            {
                throw new ObjectDisposedException(target.ToString());
            }
            if (target.Schema == null)
            {
                throw new InvalidOperationException("Target '{0}' carries no schema.".FormatWith(target));
            }

            var values  = new List <object>();
            var entries = new List <ISchemaEntry>();

            for (int i = 0; i < source.Count; i++)
            {
                var sourceEntry = source.Schema[i];
                var targetEntry = target.Schema.FindEntry(sourceEntry.TableName, sourceEntry.ColumnName);

                if (targetEntry == null)
                {
                    targetEntry = target.Schema.FindEntry(sourceEntry.ColumnName, raise: false);
                }
                if (targetEntry == null)
                {
                    values.Add(source[i].TryClone());
                    entries.Add(sourceEntry.Clone());
                }
                else
                {
                    var index = target.Schema.IndexOf(targetEntry);
                    var value = target[index];
                    var temp  = source[i];

                    if (!temp.IsEquivalentTo(value))
                    {
                        values.Add(temp.TryClone());
                        entries.Add(sourceEntry.Clone());
                    }
                }
            }

            if (entries.Count == 0)
            {
                return(null);
            }

            var schema = new Concrete.Schema(source.Schema.CaseSensitiveNames);

            foreach (var entry in entries)
            {
                schema.Add(entry);
            }
            entries.Clear();

            var record = new Concrete.Record(schema);

            for (int i = 0; i < values.Count; i++)
            {
                record[i] = values[i];
            }
            values.Clear();

            return(record);
        }
Exemplo n.º 2
0
		/// <summary>
		/// Returns a new instance that is a copy of the original one.
		/// </summary>
		/// <returns>A new instance.</returns>
		public Schema Clone()
		{
			var cloned = new Schema(CaseSensitiveNames);
			OnClone(cloned); return cloned;
		}