public DatabaseMergeResult MergeDatabases(IDatabase db1, IDatabase db2)
        {
            DatabaseMergeResult mergeResult = new DatabaseMergeResult();

            #region Tables
            IEnumerable <ITable> tables1 = db1.Tables;
            IEnumerable <ITable> tables2 = db2.Tables;

            List <ITable> newTables     = tables1.Compliment(tables2).ToList();
            List <ITable> removedTables = tables2.Compliment(tables1).ToList();

            // Check for changed tables.
            Func <ITable, string> sorter        = n => n.Name;
            Table.TableComparer   comparer      = new Table.TableComparer();
            IEnumerable <ITable>  intersection1 = tables1.Intersect(tables2, comparer).OrderBy(sorter);
            var commonItems = intersection1.Zip(tables2.Intersect(tables1, comparer).OrderBy(sorter));

            foreach (var memberPair in commonItems)
            {
                if (memberPair.Key.HasChanges(memberPair.Value))
                {
                    mergeResult.AddTableOperation(new TableChangeOperation(memberPair.Key, memberPair.Value));
                }
            }

            foreach (var t in newTables)
            {
                mergeResult.AddTableOperation(new TableAdditionOperation(db1, t));
            }

            foreach (var t in removedTables)
            {
                mergeResult.AddTableOperation(new TableRemovalOperation(t));
            }

            TableProcessor processor    = new TableProcessor();
            var            tableResults = processor.ProcessTables(tables1, tables2);
            mergeResult.CopyFrom(tableResults);
            #endregion

            #region Views
            IEnumerable <ITable> views1 = db1.Views;
            IEnumerable <ITable> views2 = db2.Views;

            List <ITable> newViews     = views1.Compliment(views2).ToList();
            List <ITable> removedViews = views2.Compliment(views1).ToList();

            // Check for changed tables.
            IEnumerable <ITable> viewsIntersection1 = views1.Intersect(views2, comparer).OrderBy(sorter);
            var commonViewItems = intersection1.Zip(views2.Intersect(views1, comparer).OrderBy(sorter));

            foreach (var memberPair in commonViewItems)
            {
                if (memberPair.Key.HasChanges(memberPair.Value))
                {
                    mergeResult.AddViewOperation(new TableChangeOperation(memberPair.Key, memberPair.Value));
                }
            }

            foreach (var t in newViews)
            {
                mergeResult.AddViewOperation(new TableAdditionOperation(db1, t));
            }

            foreach (var t in removedViews)
            {
                mergeResult.AddViewOperation(new TableRemovalOperation(t));
            }

            TableProcessor viewProcessor = new TableProcessor();
            var            viewResults   = processor.ProcessTables(views1, views2);
            mergeResult.CopyFrom(viewResults);
            #endregion

            return(mergeResult);
        }
Пример #2
0
        public DatabaseMergeResult MergeDatabases(IDatabase db1, IDatabase db2)
        {
            DatabaseMergeResult mergeResult = new DatabaseMergeResult();

            #region Tables
            IEnumerable<ITable> tables1 = db1.Tables;
            IEnumerable<ITable> tables2 = db2.Tables;

            List<ITable> newTables = tables1.Compliment(tables2).ToList();
            List<ITable> removedTables = tables2.Compliment(tables1).ToList();

            // Check for changed tables.
            Func<ITable, string> sorter = n => n.Name;
            Table.TableComparer comparer = new Table.TableComparer();
            IEnumerable<ITable> intersection1 = tables1.Intersect(tables2, comparer).OrderBy(sorter);
            var commonItems = intersection1.Zip(tables2.Intersect(tables1, comparer).OrderBy(sorter));

            foreach (var memberPair in commonItems)
                if (memberPair.Key.HasChanges(memberPair.Value))
                    mergeResult.AddTableOperation(new TableChangeOperation(memberPair.Key, memberPair.Value));

            foreach (var t in newTables)
                mergeResult.AddTableOperation(new TableAdditionOperation(db1, t));

            foreach (var t in removedTables)
                mergeResult.AddTableOperation(new TableRemovalOperation(t));

            TableProcessor processor = new TableProcessor();
            var tableResults = processor.ProcessTables(tables1, tables2);
            mergeResult.CopyFrom(tableResults);
            #endregion

            #region Views
            IEnumerable<ITable> views1 = db1.Views;
            IEnumerable<ITable> views2 = db2.Views;

            List<ITable> newViews = views1.Compliment(views2).ToList();
            List<ITable> removedViews = views2.Compliment(views1).ToList();

            // Check for changed tables.
            IEnumerable<ITable> viewsIntersection1 = views1.Intersect(views2, comparer).OrderBy(sorter);
            var commonViewItems = intersection1.Zip(views2.Intersect(views1, comparer).OrderBy(sorter));

            foreach (var memberPair in commonViewItems)
                if (memberPair.Key.HasChanges(memberPair.Value))
                    mergeResult.AddViewOperation(new TableChangeOperation(memberPair.Key, memberPair.Value));

            foreach (var t in newViews)
                mergeResult.AddViewOperation(new TableAdditionOperation(db1, t));

            foreach (var t in removedViews)
                mergeResult.AddViewOperation(new TableRemovalOperation(t));

            TableProcessor viewProcessor = new TableProcessor();
            var viewResults = processor.ProcessTables(views1, views2);
            mergeResult.CopyFrom(viewResults);
            #endregion

            return mergeResult;
        }