Пример #1
0
        private SQLScriptList ToSQLChangeColumns()
        {
            Hashtable     fields = new Hashtable();
            SQLScriptList list   = new SQLScriptList();

            if ((this.Status == Enums.ObjectStatusType.AlterStatus) || (this.Status == Enums.ObjectStatusType.RebuildStatus))
            {
                foreach (ObjectDependency dependency in this.Dependencys)
                {
                    ISchemaBase itemDepens = ((Database)this.Parent).Find(dependency.Name);
                    if (dependency.IsCodeType)
                    {
                        list.AddRange(((ICode)itemDepens).Rebuild());
                    }
                    if (dependency.Type == Enums.ObjectType.Table)
                    {
                        Column column = ((Table)itemDepens).Columns[dependency.ColumnName];
                        if ((column.Parent.Status != Enums.ObjectStatusType.DropStatus) && (column.Parent.Status != Enums.ObjectStatusType.CreateStatus) && ((column.Status != Enums.ObjectStatusType.CreateStatus)))
                        {
                            if (!fields.ContainsKey(column.FullName))
                            {
                                if (column.HasToRebuildOnlyConstraint)
                                {
                                    column.Parent.Status = Enums.ObjectStatusType.RebuildDependenciesStatus;
                                }
                                list.AddRange(column.RebuildConstraint(true));
                                list.Add("ALTER TABLE " + column.Parent.FullName + " ALTER COLUMN " + column.ToSQLRedefine(null, 0, "") + "\r\nGO\r\n", 0, Enums.ScripActionType.AlterColumn);
                                /*Si la columna va a ser eliminada o la tabla va a ser reconstruida, no restaura la columna*/
                                if ((column.Status != Enums.ObjectStatusType.DropStatus) && (column.Parent.Status != Enums.ObjectStatusType.RebuildStatus))
                                {
                                    list.AddRange(column.Alter(Enums.ScripActionType.AlterColumnRestore));
                                }
                                fields.Add(column.FullName, column.FullName);
                            }
                        }
                    }
                }
            }
            return(list);
        }
Пример #2
0
        private SQLScriptList ToSQLChangeColumns()
        {
            var fields          = new Hashtable();
            var list            = new SQLScriptList();
            var listDependencys = new SQLScriptList();

            if ((Status == Enums.ObjectStatusType.AlterStatus) || (Status == Enums.ObjectStatusType.RebuildStatus))
            {
                foreach (ObjectDependency dependency in Dependencys)
                {
                    ISchemaBase itemDepens = ((Database)Parent).Find(dependency.Name);
                    /*Si la dependencia es una funcion o una vista, reconstruye el objecto*/
                    if (dependency.IsCodeType)
                    {
                        if (itemDepens != null)
                        {
                            list.AddRange(((ICode)itemDepens).Rebuild());
                        }
                    }
                    /*Si la dependencia es una tabla, reconstruye los indices, constraint y columnas asociadas*/
                    if (dependency.Type == Enums.ObjectType.Table)
                    {
                        Column column = ((Table)itemDepens).Columns[dependency.ColumnName];
                        if ((column.Parent.Status != Enums.ObjectStatusType.DropStatus) &&
                            (column.Parent.Status != Enums.ObjectStatusType.CreateStatus) &&
                            ((column.Status != Enums.ObjectStatusType.CreateStatus) || (column.IsComputed)))
                        {
                            if (!fields.ContainsKey(column.FullName))
                            {
                                listDependencys.AddRange(RebuildDependencys((Table)itemDepens));
                                if (column.HasToRebuildOnlyConstraint)
                                {
                                    //column.Parent.Status = Enums.ObjectStatusType.AlterRebuildDependenciesStatus;
                                    list.AddRange(column.RebuildDependencies());
                                }
                                if (!column.IsComputed)
                                {
                                    list.AddRange(column.RebuildConstraint(true));
                                    list.Add(
                                        "ALTER TABLE " + column.Parent.FullName + " ALTER COLUMN " +
                                        column.ToSQLRedefine(Type, Size, null) + "\r\nGO\r\n", 0,
                                        Enums.ScripActionType.AlterColumn);
                                    /*Si la columna va a ser eliminada o la tabla va a ser reconstruida, no restaura la columna*/
                                    if ((column.Status != Enums.ObjectStatusType.DropStatus) &&
                                        (column.Parent.Status != Enums.ObjectStatusType.RebuildStatus))
                                    {
                                        list.AddRange(column.Alter(Enums.ScripActionType.AlterColumnRestore));
                                    }
                                }
                                else
                                {
                                    if (column.Status != Enums.ObjectStatusType.CreateStatus)
                                    {
                                        if (!column.GetWasInsertInDiffList(Enums.ScripActionType.AlterColumnFormula))
                                        {
                                            column.SetWasInsertInDiffList(Enums.ScripActionType.AlterColumnFormula);
                                            list.Add(column.ToSqlDrop(), 0, Enums.ScripActionType.AlterColumnFormula);
                                            List <ISchemaBase> drops =
                                                ((Database)column.Parent.Parent).Dependencies.Find(column.Parent.Id,
                                                                                                   column.Id, 0);
                                            drops.ForEach(item => {
                                                if (item.Status != Enums.ObjectStatusType.CreateStatus)
                                                {
                                                    list.Add(item.Drop());
                                                }
                                                if (item.Status != Enums.ObjectStatusType.DropStatus)
                                                {
                                                    list.Add(item.Create());
                                                }
                                            });
                                            /*Si la columna va a ser eliminada o la tabla va a ser reconstruida, no restaura la columna*/
                                            if ((column.Status != Enums.ObjectStatusType.DropStatus) &&
                                                (column.Parent.Status != Enums.ObjectStatusType.RebuildStatus))
                                            {
                                                list.Add(column.ToSqlAdd(), 0,
                                                         Enums.ScripActionType.AlterColumnFormulaRestore);
                                            }
                                        }
                                    }
                                }
                                fields.Add(column.FullName, column.FullName);
                            }
                        }
                    }
                }
            }
            list.AddRange(listDependencys);
            return(list);
        }