private static SnapshotTableDifferences SortTable(SnapshotTableDifferences tableDifferences)
        {
            Func <IEnumerable <RowDifference>, IEnumerable <RowDifference> > sorter = null;

            foreach (var fieldSortSpec in SortFieldOrderer.Order(tableDifferences.TableDefinition.SortColumns))
            {
                var fieldName = fieldSortSpec.Field;
                Func <RowDifference, object> fetch = (r) => r.After.GetField(fieldName) ?? r.Before.GetField(fieldName);

                Func <IEnumerable <RowDifference>, IEnumerable <RowDifference> > nextSorter;
                if (fieldSortSpec.SortOrder == SortOrder.Ascending)
                {
                    nextSorter = r => r.OrderBy(fetch, ValueComparer.Comparer);
                }
                else
                {
                    nextSorter = r => r.OrderByDescending(fetch, ValueComparer.Comparer);
                }

                if (sorter == null)
                {
                    sorter = nextSorter;
                }
                else
                {
                    var currentSorter = sorter;
                    sorter = r => currentSorter(nextSorter(r));
                }
            }

            Debug.Assert(sorter != null);
            var rowDifferences = sorter(tableDifferences.RowDifferences).ToList();

            return(new SnapshotTableDifferences(rowDifferences, tableDifferences.TableDefinition));
        }
        private static IEnumerable <object> ColumnValues(string column, SnapshotTableDifferences tableForValues)
        {
            var items = tableForValues.RowDifferences.SelectMany(rd => rd.Differences.Differences.Where(d => d.Name == column));

            foreach (var item in items)
            {
                var columnValue = item.Before ?? item.After;

                yield return(columnValue);

                if (item.Before != null && item.After != null && !item.Before.Equals(item.After))
                {
                    yield return(item.After);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Add mandatory rows to a difference set as references if they do not currently exist.
        /// </summary>
        /// <param name="collection">The collection that generated the differences.</param>
        /// <param name="tableDiffs">The difference set.</param>
        /// <param name="rows">The rows that are mandatory.</param>
        /// <param name="additionalRows">The rows that are mandatory.</param>
        /// <param name="differenceSide">One of the snapshots - it does not generally matter which because rows that were not extracted should be identical in both snapshots.</param>
        internal static List <SnapshotTableDifferences> RequireRows(SnapshotCollection collection, List <SnapshotTableDifferences> tableDiffs, AdditionalReferencedRows additionalRows, Snapshot differenceSide)
        {
            var result = new List <SnapshotTableDifferences>();

            foreach (var table in collection.TablesInDefinitionOrder)
            {
                var current    = tableDiffs.SingleOrDefault(t => t.TableDefinition.TableName == table.TableName);
                var additional = additionalRows.Tables.SingleOrDefault(t => t.TableDefinition.TableName == table.TableName);
                if (additional == null && current != null)
                {
                    result.Add(current);
                }
                else if (additional != null)
                {
                    var snapRows = differenceSide.Rows(additional.TableDefinition.TableName).ToList();
                    var addRows  = additional.Keys.Select(rowRequest => snapRows.SingleOrDefault(r => r.GetField(rowRequest.ColumnName)?.Equals(rowRequest.RequestedValue) ?? false))
                                   .Where(r => r != null)
                                   .ToList();

                    var requiredSnapRows = snapRows.Select((r, i) => new
                    {
                        Index      = i, SnapRow = r,
                        Difference = current?.RowDifferences.SingleOrDefault(d => ReferenceEquals(d.After, r) || ReferenceEquals(d.Before, r)),
                        Additional = additional.Keys.FirstOrDefault(a => r.GetField(a.ColumnName)?.Equals(a.RequestedValue) ?? false)
                    })
                                           .Where(req => req.Additional != null || req.Difference != null)
                                           .ToList();

                    var allDiffs = requiredSnapRows
                                   .Select(r => r.Difference ?? new RowDifference(new SnapshotRowKey(r.SnapRow, table), DifferenceType.Reference, r.SnapRow, r.SnapRow));

                    if (current != null && current.RowDifferences.Any())
                    {
                        //Add in any rows from the original differences that were not matched in the required rows.
                        allDiffs = allDiffs.Concat(current.RowDifferences.Where(r => !requiredSnapRows.Any(sr => ReferenceEquals(sr.Difference, r))));
                    }

                    var newDiffs = new SnapshotTableDifferences(allDiffs.ToList(), additional.TableDefinition);
                    result.Add(newDiffs);
                }
            }

            return(result);
        }
Beispiel #4
0
        private static void ReportDifferences(ReportParameters <RowDifference> rep, SnapshotTableDifferences tableDifferences, Output output, ChangeReportOptions changeReportOptions)
        {
            var differenceCols
                = tableDifferences.RowDifferences
                  .Where(r => r.Differences?.Differences != null)
                  .SelectMany(r => r.Differences.Differences.Select(d => d.Name)).Distinct();

            var allCols = tableDifferences.TableDefinition.Columns.Where(c => differenceCols.Contains(c.Name)).Select(c => c.Name);

            rep.Title(tableDifferences.TableDefinition.TableName);
            rep.RemoveBufferLimit();

            rep.AddColumn(rd => rd.DifferenceType.ToString(), cc => cc.Heading("Difference"));

            foreach (var col in allCols)
            {
                rep.AddColumn(rep.Lambda(rd => DifferenceDisplay(rd, col)), cc => cc.Heading(col));
            }
        }
Beispiel #5
0
        public static List <SnapshotTableDifferences> RequireColumns(SnapshotCollection collection, List <SnapshotTableDifferences> diffs, TableDefinition table, IEnumerable <string> requiredColumns, Snapshot before)
        {
            var result     = new List <SnapshotTableDifferences>();
            var columnList = requiredColumns.ToList();

            foreach (var differences in diffs)
            {
                if (differences.TableDefinition.TableName == table.TableName)
                {
                    var rowDiffs = differences.RowDifferences.Select(r => RequireColumns(r, columnList)).ToList();
                    var newDiffs = new SnapshotTableDifferences(rowDiffs, differences.TableDefinition);
                    result.Add(newDiffs);
                }
                else
                {
                    result.Add(differences);
                }
            }

            return(result);
        }
        private static SnapshotTableDifferences SubstituteDiff(SnapshotTableDifferences tableDiff, List <SubstituteValues> substituteValues)
        {
            var amendedDiffs = tableDiff.RowDifferences.Select(rd => SubstituteRowDiffs(tableDiff.TableDefinition, rd, substituteValues)).ToList();

            return(new SnapshotTableDifferences(amendedDiffs, tableDiff.TableDefinition));
        }