Пример #1
0
        private void Generate(DropTableOperation op)
        {
            using (var tw = Format.CreateIndentedTextWriter())
            {
                tw.Write("DROP TABLE ");
                tw.Write(Format.ReservedWord(Format.RemoveDbo(op.Name)));

                AddSqlStatement(tw);
            }
        }
Пример #2
0
        private void Generate(RenameTableOperation op)
        {
            using (var tw = Format.CreateIndentedTextWriter())
            {
                tw.Write("ALTER TABLE ");
                tw.Write(Format.ReservedWord(Format.RemoveDbo(op.Name)));
                tw.Write(" RENAME TO ");
                tw.Write(Format.ReservedWord(Format.RemoveDbo(op.NewName)));

                AddSqlStatement(tw);
            }
        }
Пример #3
0
        /// <summary>
        /// Generate CREATE TABLE command
        /// </summary>
        private void GenerateCreateTableCommand(CreateTableOperation op, IndentedTextWriter tw)
        {
            tw.WriteLine("CREATE TABLE " + Format.ReservedWord(Format.RemoveDbo(op.Name)) + " (");
            tw.Indent++;

            bool hasGeneratedIdPk = false;

            for (int i = 0; i < op.Columns.Count; i++)
            {
                ColumnModel lcmDadosColuna = op.Columns.ToList()[i];
                hasGeneratedIdPk |= Generate(lcmDadosColuna, tw, op.PrimaryKey);

                if (i < op.Columns.Count - 1)
                {
                    tw.WriteLine(",");
                }
            }

            if ((op.PrimaryKey != null) && !hasGeneratedIdPk)
            {
                tw.WriteLine(",");
                tw.Write("CONSTRAINT ");
                tw.Write(Format.RemoveDbo(op.PrimaryKey.Name));
                tw.Write(" PRIMARY KEY ");
                tw.Write("(");

                for (int li = 0; li < op.PrimaryKey.Columns.Count; li++)
                {
                    var lstrNomeColuna = op.PrimaryKey.Columns[li];

                    tw.Write(lstrNomeColuna);

                    if (li < op.PrimaryKey.Columns.Count - 1)
                    {
                        tw.WriteLine(",");
                    }
                }

                tw.WriteLine(")");
            }
            else
            {
                tw.WriteLine();
            }

            tw.Indent--;
            tw.Write(")");
        }
Пример #4
0
        private void Generate(AddForeignKeyOperation op)
        {
            if (!_migrationStatements.Any(item => item.Sql.Contains("CREATE TABLE")))
            {
                return;
            }

            var createScript = string.Format(CultureInfo.InvariantCulture,
                                             "CREATE TABLE {0} (", Format.ReservedWord(Format.RemoveDbo(op.DependentTable)));

            var migrationStatement = _migrationStatements
                                     .FirstOrDefault(item => item.Sql
                                                     .Contains(createScript));

            if (migrationStatement == null)
            {
                throw new NotSupportedException("SQL command to create the dependent table not found.");
            }

            using (var tw = Format.CreateIndentedTextWriter())
            {
                tw.Write(migrationStatement.Sql.TrimEnd(')').TrimEnd('\r', '\n'));

                tw.WriteLine(",");

                tw.Indent++;

                var referenceList = op.DependentColumns.Select(dependentColumn => op.PrincipalColumns[op.DependentColumns.IndexOf(dependentColumn)]).ToList();

                var cmd = string.Format(CultureInfo.InvariantCulture, "FOREIGN KEY ({0}) REFERENCES [{1}] ({2}) {3}",
                                        string.Join(", ", op.DependentColumns.Select(Format.ReservedWord)),
                                        Format.ReservedWord(op.PrincipalTable),
                                        string.Join(", ", referenceList.Select(Format.ReservedWord)),
                                        (op.CascadeDelete
                        ? "ON DELETE CASCADE"
                        : "ON DELETE NO ACTION"));

                tw.WriteLine(Format.RemoveDbo(cmd));

                tw.Indent--;
                tw.Write(")");

                migrationStatement.Sql = tw.InnerWriter.ToString();
            }
        }
Пример #5
0
        private void Generate(AddColumnOperation op)
        {
            SetAnnotatedColumn(op.Column, op.Table);

            using (var tw = Format.CreateIndentedTextWriter())
            {
                tw.Write("ALTER TABLE ");
                tw.Write(Format.ReservedWord(Format.RemoveDbo(op.Table)));
                tw.Write(" ADD ");

                var col = op.Column;

                Generate(col, tw, null);

                if ((col.IsNullable != null) &&
                    !col.IsNullable.Value &&
                    (col.DefaultValue == null) &&
                    (string.IsNullOrWhiteSpace(col.DefaultValueSql)) &&
                    !col.IsIdentity &&
                    !col.IsTimestamp &&
                    (
                        col.StoreType == null || (
                            !col.StoreType.Equals("rowversion", StringComparison.OrdinalIgnoreCase) &&
                            !col.StoreType.Equals("timestamp", StringComparison.OrdinalIgnoreCase))))
                {
                    tw.Write(" DEFAULT ");

                    if (col.Type == PrimitiveTypeKind.DateTime)
                    {
                        tw.Write(Generate(DateTime.Parse("1900-01-01 00:00:00", CultureInfo.InvariantCulture)));
                    }
                    else
                    {
                        tw.Write(Generate((dynamic)col.ClrDefaultValue));
                    }
                }

                AddSqlStatement(tw);
            }
        }
Пример #6
0
        private void Generate(CreateIndexOperation op)
        {
            using (var tw = Format.CreateIndentedTextWriter())
            {
                var indexName = op.HasDefaultName
                    ? string.Format(CultureInfo.InvariantCulture, "{0}_{1}", op.Name, Format.RemoveDbo(op.Table))
                    : op.Name;

                tw.Write("CREATE ");

                if (op.IsUnique)
                {
                    tw.Write(" UNIQUE ");
                }

                tw.Write("INDEX ");
                tw.Write(Format.ReservedWord(indexName));
                tw.Write(" ON ");
                tw.Write(Format.ReservedWord(Format.RemoveDbo(op.Table)));
                tw.Write("(");

                for (int i = 0; i < op.Columns.Count; i++)
                {
                    tw.Write(Format.ReservedWord(op.Columns[i]));

                    if (i < op.Columns.Count - 1)
                    {
                        tw.WriteLine(",");
                    }
                }

                tw.Write(")");

                AddSqlStatement(tw);
            }
        }
Пример #7
0
        private void Generate(SqlOperation opeSQL)
        {
            var sql = Format.RemoveDbo(opeSQL.Sql);

            AddSqlStatement(sql, opeSQL.SuppressTransaction);
        }
Пример #8
0
        private void Generate(DropIndexOperation op)
        {
            using (var tw = Format.CreateIndentedTextWriter())
            {
                var indexName = op.HasDefaultName
                    ? string.Format(CultureInfo.InvariantCulture, "{0}_{1}", op.Name, Format.ReservedWord(Format.RemoveDbo(op.Table)))
                    : op.Name;

                tw.Write("DROP INDEX ");
                tw.Write(indexName);

                AddSqlStatement(tw);
            }
        }