Example #1
0
        /// <summary>
        /// Builds the SQL statement.
        /// </summary>
        /// <returns></returns>
        public string BuildSqlStatement(MigrationDirection direction)
        {
            if (Provider == null)
            {
                Provider = DataService.Provider;
            }

            ISqlGenerator generator = DataService.GetGenerator(Provider);
            StringBuilder sql       = new StringBuilder();

            // based on direction build migration steps only when dealing with a
            // decendent class, there is no overridden Up or Down if doing a
            // migrate on dispose style so the step.Clear() would destroy all
            // our hard work.
            if (!migrateOnDispose)
            {
                steps.Clear();

                switch (direction)
                {
                case MigrationDirection.Up:
                    Up();
                    break;

                case MigrationDirection.Down:
                    Down();
                    break;
                }
            }

            //build sql
            foreach (MigrationStep step in steps)
            {
                switch (step.StepType)
                {
                case MigrationStepType.CreateTable:
                    // need to make sure this table has a pk defined
                    // if not, add one
                    // we'll do yer job for ya :)
                    if (step.Table.PrimaryKey == null)
                    {
                        step.Table.AddPrimaryKeyColumn();
                    }
                    sql.Append(generator.BuildCreateTableStatement(step.Table));
                    break;

                case MigrationStepType.DropTable:
                    sql.Append(generator.BuildDropTableStatement(step.Table));
                    break;

                case MigrationStepType.AddForeignKey:
                    sql.Append(generator.BuildForeignKeyStatement(step.Column, step.Column2));
                    break;

                case MigrationStepType.DropForeignKey:
                    sql.Append(generator.BuildForeignKeyDropStatement(step.Column, step.Column2));
                    break;

                case MigrationStepType.AddColumn:
                    sql.Append(generator.BuildAddColumnStatement(step.Table, step.Column));
                    break;

                case MigrationStepType.AlterColumn:
                    sql.Append(generator.BuildAlterColumnStatement(step.Column));
                    break;

                case MigrationStepType.DropColumn:
                    sql.Append(generator.BuildDropColumnStatement(step.Table, step.Column));
                    break;

                case MigrationStepType.ExecuteSql:
                    sql.Append(step.Sql);
                    break;

                default:
                    break;
                }

                sql.Append(";\r\n");
            }

            return(sql.ToString());
        }