예제 #1
0
        /// <summary>
        /// 计算出两个数据库元数据的所有表差别
        /// </summary>
        /// <param name="oldDatabase">旧数据库</param>
        /// <param name="newDatabase">新数据库</param>
        /// <returns></returns>
        public static DatabaseChanges Distinguish(Database oldDatabase, DestinationDatabase newDatabase)
        {
            if (!oldDatabase.Removed)
            {
                oldDatabase.OrderByRelations();
            }
            if (!newDatabase.Removed)
            {
                newDatabase.OrderByRelations();
            }

            List <TableChanges> result = new List <TableChanges>();

            if (!oldDatabase.Removed && !newDatabase.Removed)
            {
                //先找出所有被删除的表
                foreach (var oldTable in oldDatabase.Tables.Reverse())
                {
                    if (newDatabase.FindTable(oldTable.Name) == null && !newDatabase.IsIgnored(oldTable.Name))
                    {
                        result.Add(new TableChanges(oldTable, null, ChangeType.Removed));
                    }
                }

                foreach (var newTable in newDatabase.Tables)
                {
                    if (!newDatabase.IsIgnored(newTable.Name))
                    {
                        var oldTable = oldDatabase.FindTable(newTable.Name);
                        //如果没有找到旧表,说明这个表是新加的。
                        if (oldTable == null)
                        {
                            result.Add(new TableChanges(null, newTable, ChangeType.Added));
                        }
                        else
                        {
                            //即不是新表,也不是旧表,计算两个表的区别
                            TableChanges record = Distinguish(oldTable, newTable);

                            //如果有区别,则记录下来
                            if (record != null)
                            {
                                result.Add(record);
                            }
                        }
                    }
                }
            }

            return(new DatabaseChanges(oldDatabase, newDatabase, result));
        }
예제 #2
0
        /// <summary>
        /// 计算出新旧表之间的数据列差别
        /// </summary>
        /// <param name="oldTable">旧表</param>
        /// <param name="newTable">新表</param>
        /// <returns>
        /// 返回表之间区别,如果没有区别,则返回null
        /// </returns>
        private static TableChanges Distinguish(Table oldTable, Table newTable)
        {
            //if (newTable == null) throw new ArgumentNullException("newTable");
            //if (oldTable == null) throw new ArgumentNullException("oldTable");
            //if (newTable.Name != oldTable.Name) throw new InvalidOperationException("newTable.Name != oldTable.Name must be false.");

            var record = new TableChanges(oldTable, newTable, ChangeType.Modified);

            //先找到已经删除的列
            foreach (var oldColumn in oldTable.Columns)
            {
                if (newTable.FindColumn(oldColumn.Name) == null)
                {
                    record.ColumnsChanged.Add(new ColumnChanges(oldColumn, null, ChangeType.Removed));
                }
            }

            //记录新增的和更改过的列
            foreach (var column in newTable.Columns)
            {
                Column oldColumn = oldTable.FindColumn(column.Name);

                if (oldColumn == null)
                {
                    var columnChanged = new ColumnChanges(null, column, ChangeType.Added);
                    record.ColumnsChanged.Add(columnChanged);
                }
                else
                {
                    var columnChanged = Distinguish(oldColumn, column);
                    //新增的 或者 修改的 列
                    if (columnChanged != null)
                    {
                        record.ColumnsChanged.Add(columnChanged);
                    }
                }
            }

            //如果被修改了,则返回record;否则返回null
            if (record.ColumnsChanged.Count > 0)
            {
                return(record);
            }

            return(null);
        }
예제 #3
0
        /// <summary>
        /// 计算出新旧表之间的数据列差别
        /// </summary>
        /// <param name="oldTable">旧表</param>
        /// <param name="newTable">新表</param>
        /// <returns>
        /// 返回表之间区别,如果没有区别,则返回null
        /// </returns>
        private static TableChanges Distinguish(Table oldTable, Table newTable)
        {
            //if (newTable == null) throw new ArgumentNullException("newTable");
            //if (oldTable == null) throw new ArgumentNullException("oldTable");
            //if (newTable.Name != oldTable.Name) throw new InvalidOperationException("newTable.Name != oldTable.Name must be false.");

            var record = new TableChanges(oldTable, newTable, ChangeType.Modified);

            //先找到已经删除的列
            foreach (var oldColumn in oldTable.Columns)
            {
                if (newTable.FindColumn(oldColumn.Name) == null)
                {
                    record.ColumnsChanged.Add(new ColumnChanges(oldColumn, null, ChangeType.Removed));
                }
            }

            //记录新增的和更改过的列
            foreach (var column in newTable.Columns)
            {
                Column oldColumn = oldTable.FindColumn(column.Name);

                if (oldColumn == null)
                {
                    var columnChanged = new ColumnChanges(null, column, ChangeType.Added);
                    record.ColumnsChanged.Add(columnChanged);
                }
                else
                {
                    var columnChanged = Distinguish(oldColumn, column);
                    //新增的 或者 修改的 列
                    if (columnChanged != null) { record.ColumnsChanged.Add(columnChanged); }
                }
            }

            //如果被修改了,则返回record;否则返回null
            if (record.ColumnsChanged.Count > 0) { return record; }

            return null;
        }
예제 #4
0
 private void GenerateOpertions(TableChanges tableChanges)
 {
     switch (tableChanges.ChangeType)
     {
         case ChangeType.Added:
             this.AddTable(tableChanges.NewTable);
             break;
         case ChangeType.Removed:
             this.RemoveTable(tableChanges.OldTable);
             break;
         case ChangeType.Modified:
             foreach (var column in tableChanges.ColumnsChanged)
             {
                 this.GenerateOpertions(column);
             }
             break;
         default:
             break;
     }
 }