Ejemplo n.º 1
0
        private static ColumnChanges Distinguish(Column oldColumn, Column newColumn)
        {
            //if (newColumn == null) throw new ArgumentNullException("newColumn");
            //if (newColumn.Name.EqualsIgnoreCase(oldColumn.Name)) throw new InvalidOperationException("newColumn.Name.EqualsIgnoreCase(oldColumn.Name) must be false.");
            //if (newColumn.Table.Name.EqualsIgnoreCase(oldColumn.Table.Name)) throw new InvalidOperationException("newColumn.Table.Name.EqualsIgnoreCase(oldColumn.Table.Name) must be false.");

            ColumnChanges columnChanged = null;

            if (!Equals(newColumn, oldColumn))
            {
                columnChanged = new ColumnChanges(oldColumn, newColumn, ChangeType.Modified);

                if (newColumn.IsRequired != oldColumn.IsRequired)
                {
                    columnChanged.IsRequiredChanged = true;
                }

                if (newColumn.IsPrimaryKey != oldColumn.IsPrimaryKey)
                {
                    columnChanged.IsPrimaryKeyChanged = true;
                }

                if (!DbTypeHelper.IsCompatible(newColumn.DataType, oldColumn.DataType))
                {
                    columnChanged.IsDbTypeChanged = true;
                }

                //ForeignRelationChangeType
                columnChanged.ForeignRelationChangeType = ChangeType.UnChanged;
                if (!newColumn.IsForeignKey && oldColumn.IsForeignKey)
                {
                    columnChanged.ForeignRelationChangeType = ChangeType.Removed;
                }
                else if (newColumn.IsForeignKey && !oldColumn.IsForeignKey)
                {
                    columnChanged.ForeignRelationChangeType = ChangeType.Added;
                }
                else if (newColumn.IsForeignKey && oldColumn.IsForeignKey)
                {
                    if (!Equals(newColumn.ForeignConstraint, oldColumn.ForeignConstraint))
                    {
                        columnChanged.ForeignRelationChangeType = ChangeType.Modified;
                    }
                }
            }
            return(columnChanged);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
0
        private static ColumnChanges Distinguish(Column oldColumn, Column newColumn)
        {
            //if (newColumn == null) throw new ArgumentNullException("newColumn");
            //if (newColumn.Name.EqualsIgnoreCase(oldColumn.Name)) throw new InvalidOperationException("newColumn.Name.EqualsIgnoreCase(oldColumn.Name) must be false.");
            //if (newColumn.Table.Name.EqualsIgnoreCase(oldColumn.Table.Name)) throw new InvalidOperationException("newColumn.Table.Name.EqualsIgnoreCase(oldColumn.Table.Name) must be false.");

            ColumnChanges columnChanged = null;
            if (!Equals(newColumn, oldColumn))
            {
                columnChanged = new ColumnChanges(oldColumn, newColumn, ChangeType.Modified);

                if (newColumn.IsRequired != oldColumn.IsRequired) { columnChanged.IsRequiredChanged = true; }

                if (newColumn.IsPrimaryKey != oldColumn.IsPrimaryKey) { columnChanged.IsPrimaryKeyChanged = true; }

                if (newColumn.DataType != oldColumn.DataType) { columnChanged.IsDbTypeChanged = true; }

                //ForeignRelationChangeType
                columnChanged.ForeignRelationChangeType = ChangeType.UnChanged;
                if (!newColumn.IsForeignKey && oldColumn.IsForeignKey)
                {
                    columnChanged.ForeignRelationChangeType = ChangeType.Removed;
                }
                else if (newColumn.IsForeignKey && !oldColumn.IsForeignKey)
                {
                    columnChanged.ForeignRelationChangeType = ChangeType.Added;
                }
                else if (newColumn.IsForeignKey && oldColumn.IsForeignKey)
                {
                    if (!Equals(newColumn.ForeignConstraint, oldColumn.ForeignConstraint))
                    {
                        columnChanged.ForeignRelationChangeType = ChangeType.Modified;
                    }
                }
            }
            return columnChanged;
        }
Ejemplo n.º 4
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;
        }
Ejemplo n.º 5
0
 private void ModifyColumnRequired(ColumnChanges columnChanges)
 {
     if (columnChanges.NewColumn.IsRequired)
     {
         if (columnChanges.OldColumn.IsForeignKey)
         {
             this.AddOperation(new AddNotNullConstraintFK
             {
                 CopyFromColumn = columnChanges.NewColumn
             });
         }
         else
         {
             this.AddOperation(new AddNotNullConstraint
             {
                 CopyFromColumn = columnChanges.NewColumn
             });
         }
     }
     else
     {
         if (columnChanges.OldColumn.IsForeignKey)
         {
             this.AddOperation(new RemoveNotNullConstraintFK
             {
                 CopyFromColumn = columnChanges.NewColumn
             });
         }
         else
         {
             this.AddOperation(new RemoveNotNullConstraint
             {
                 CopyFromColumn = columnChanges.NewColumn
             });
         }
     }
 }
Ejemplo n.º 6
0
 private void ModifyColumnForeignConstraint(ColumnChanges columnChanges)
 {
     var value = columnChanges.ForeignRelationChangeType;
     switch (value)
     {
         case ChangeType.Added:
             this.AddOperation(new AddFKConstraint
             {
                 CopyFromConstraint = columnChanges.NewColumn.ForeignConstraint
             });
             break;
         case ChangeType.Removed:
             this.AddOperation(new RemoveFKConstraint
             {
                 CopyFromConstraint = columnChanges.OldColumn.ForeignConstraint
             });
             break;
         case ChangeType.Modified:
             //throw new NotSupportedException("暂时不支持外键修改。");
             this.AddOperation(new RemoveFKConstraint
             {
                 CopyFromConstraint = columnChanges.OldColumn.ForeignConstraint
             });
             this.AddOperation(new AddFKConstraint
             {
                 CopyFromConstraint = columnChanges.NewColumn.ForeignConstraint
             });
             break;
         default:
             break;
     }
 }
Ejemplo n.º 7
0
        private void ModifyColumn(ColumnChanges columnChanges)
        {
            //数据类型
            if (columnChanges.IsDbTypeChanged)
            {
                this.AddOperation(new AlterColumnType
                {
                    CopyFromColumn = columnChanges.OldColumn,
                    NewType = columnChanges.NewColumn.DataType,
                    IsRequired = columnChanges.OldColumn.IsRequired,
                });
            }

            //是否主键
            if (columnChanges.IsPrimaryKeyChanged)
            {
                var column = columnChanges.NewColumn;
                if (column.IsPrimaryKey)
                {
                    this.AddOperation(new AddPKConstraint
                    {
                        CopyFromColumn = column,
                    });
                }
                else
                {
                    this.AddOperation(new RemovePKConstraint
                    {
                        CopyFromColumn = column,
                    });
                }
            }

            //可空性
            if (columnChanges.IsRequiredChanged)
            {
                this.ModifyColumnRequired(columnChanges);
            }

            //外键
            if (columnChanges.ForeignRelationChangeType != ChangeType.UnChanged)
            {
                this.ModifyColumnForeignConstraint(columnChanges);
            }
        }
Ejemplo n.º 8
0
 private void GenerateOpertions(ColumnChanges columnChanges)
 {
     switch (columnChanges.ChangeType)
     {
         case ChangeType.Added:
             this.AddColumn(columnChanges.NewColumn);
             break;
         case ChangeType.Removed:
             this.RemoveColumn(columnChanges.OldColumn);
             break;
         case ChangeType.Modified:
             this.ModifyColumn(columnChanges);
             break;
         default:
             break;
     }
 }