コード例 #1
0
ファイル: Database.cs プロジェクト: daviabdallah/schemazen
        public string Script()
        {
            var text = new StringBuilder();

            //alter database props
            //TODO need to check dependencies for collation change
            //TODO how can collation be set to null at the server level?
            if (PropsChanged.Count > 0)
            {
                text.Append(Database.ScriptPropList(PropsChanged));
                text.AppendLine("GO");
                text.AppendLine();
            }

            //delete foreign keys
            if (ForeignKeysDeleted.Count + ForeignKeysDiff.Count > 0)
            {
                foreach (ForeignKey fk in ForeignKeysDeleted)
                {
                    text.AppendLine(fk.ScriptDrop());
                }
                //delete modified foreign keys
                foreach (ForeignKey fk in ForeignKeysDiff)
                {
                    text.AppendLine(fk.ScriptDrop());
                }
                text.AppendLine("GO");
            }

            //add tables
            if (TablesAdded.Count > 0)
            {
                foreach (Table t in TablesAdded)
                {
                    text.Append(t.ScriptCreate());
                }
                text.AppendLine("GO");
            }

            //modify tables
            if (TablesDiff.Count > 0)
            {
                foreach (TableDiff t in TablesDiff)
                {
                    text.Append(t.Script());
                }
                text.AppendLine("GO");
            }

            //delete tables
            if (TablesDeleted.Count > 0)
            {
                foreach (Table t in TablesDeleted)
                {
                    text.AppendLine(t.ScriptDrop());
                }
                text.AppendLine("GO");
            }

            //add foreign keys
            if (ForeignKeysAdded.Count + ForeignKeysDiff.Count > 0)
            {
                foreach (ForeignKey fk in ForeignKeysAdded)
                {
                    text.AppendLine(fk.ScriptCreate());
                }
                //add modified foreign keys
                foreach (ForeignKey fk in ForeignKeysDiff)
                {
                    text.AppendLine(fk.ScriptCreate());
                }
                text.AppendLine("GO");
            }

            //add & delete procs, functions, & triggers
            foreach (Routine r in RoutinesAdded)
            {
                text.AppendLine(r.ScriptCreate(Db));
                text.AppendLine("GO");
            }
            foreach (Routine r in RoutinesDiff)
            {
                text.AppendLine(r.ScriptDrop());
                text.AppendLine("GO");
                text.AppendLine(r.ScriptCreate(Db));
                text.AppendLine("GO");
            }
            foreach (Routine r in RoutinesDeleted)
            {
                text.AppendLine(r.ScriptDrop());
                text.AppendLine("GO");
            }

            return(text.ToString());
        }