/// <summary>
        /// Generates SQL for a <see cref="HistoryOperation" />.
        /// Generated SQL should be added using the Statement method.
        /// </summary>
        /// <param name="historyOperation"> The operation to produce SQL for. </param>
        protected virtual void Generate(HistoryOperation historyOperation)
        {
            Check.NotNull(historyOperation, "historyOperation");

            using (var writer = Writer())
            {
                historyOperation.CommandTrees.Each(
                    commandTree =>
                    {
                        List<SqlParameter> _;

                        switch (commandTree.CommandTreeKind)
                        {
                            case DbCommandTreeKind.Insert:

                                writer.Write(
                                    DmlSqlGenerator
                                        .GenerateInsertSql(
                                            (DbInsertCommandTree)commandTree,
                                            _sqlGenerator,
                                            out _,
                                            generateReturningSql: false,
                                            upperCaseKeywords: true,
                                            createParameters: false));
                                break;

                            case DbCommandTreeKind.Delete:
                                writer.Write(
                                    DmlSqlGenerator
                                        .GenerateDeleteSql(
                                            (DbDeleteCommandTree)commandTree,
                                            _sqlGenerator,
                                            out _,
                                            upperCaseKeywords: true,
                                            createParameters: false));
                                break;
                        }
                    });

                Statement(writer);
            }
        }
Exemplo n.º 2
0
        public void Generate_can_output_insert_history_statement()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            using (var historyContext = new HistoryContext())
            {
                historyContext.History.Add(
                    new HistoryRow
                        {
                            MigrationId = "House Lannister",
                            ContextKey = "The pointy end",
                            Model = new byte[0],
                            ProductVersion = "Awesomeness"
                        });

                using (var commandTracer = new CommandTracer(historyContext))
                {
                    historyContext.SaveChanges();

                    var insertHistoryOperation
                        = new HistoryOperation(commandTracer.CommandTrees.OfType<DbModificationCommandTree>().ToList());

                    var sql
                        = migrationSqlGenerator
                            .Generate(new[] { insertHistoryOperation }, "4.0")
                            .Single();

                    Assert.Equal(@"INSERT [__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'House Lannister', N'The pointy end',  0x , N'Awesomeness')", sql.Sql.Trim());
                }
            }
        }
Exemplo n.º 3
0
 private void Convert(HistoryOperation historyOperation)
 {
     foreach (var command in historyOperation.CommandTrees)
     {
         AddStatment(NpgsqlServices.Instance.CreateDbCommand(command).CommandText);
     }
 }
 private void Convert(HistoryOperation historyOperation)
 {
     foreach (var command in historyOperation.CommandTrees)
     {
         AddStatment(NpgsqlServices.Instance.CreateDbCommand(command, (NpgsqlProviderManifest)NpgsqlServices.Instance.GetProviderManifest(serverVersion.ToString())).CommandText);
     }
 }
        public void Can_get_and_set_properties()
        {
            var historyOperation = new HistoryOperation(new[] { new DbInsertCommandTree() });

            Assert.NotEmpty(historyOperation.CommandTrees);
        }
    protected virtual MigrationStatement Generate(HistoryOperation op)
    {
      if (op == null) return null;

      MigrationStatement stmt = new MigrationStatement();

      var cmdStr = "";
      SqlGenerator generator = new SelectGenerator();
      foreach (var commandTree in op.CommandTrees)
      {
        switch (commandTree.CommandTreeKind)
        {
          case DbCommandTreeKind.Insert:
            generator = new InsertGenerator();
            break;
          case DbCommandTreeKind.Delete:
            generator = new DeleteGenerator();
            break;
          case DbCommandTreeKind.Update:
            generator = new UpdateGenerator();
            break;
          case DbCommandTreeKind.Query:
            generator = new SelectGenerator();
            break;
          case DbCommandTreeKind.Function:
            generator = new FunctionGenerator();
            break;
          default:
            throw new NotImplementedException(commandTree.CommandTreeKind.ToString());
        }
        cmdStr = generator.GenerateSQL(commandTree);

        ReplaceParemeters(ref cmdStr, generator.Parameters);
        stmt.Sql += cmdStr.Replace("dbo", "") + ";";
      }
      return stmt;
    }
 protected virtual void Convert(HistoryOperation historyOperation)
 {
     foreach (var command in historyOperation.CommandTrees)
     {
         var npgsqlCommand = new NpgsqlCommand();
         NpgsqlServices.Instance.TranslateCommandTree(serverVersion, command, npgsqlCommand, false);
         AddStatment(npgsqlCommand.CommandText);
     }
 }
        public void Generate_can_output_delete_history_statement()
        {
            var migrationSqlGenerator = new SqlServerMigrationSqlGenerator();

            var sqlCommand = new SqlCommand("delete Foo\r\nwhere Bar = p1");
            sqlCommand.Parameters.Add(new SqlParameter("p1", "Baz"));

            var insertHistoryOperation
                = new HistoryOperation(
                    new[]
                        {
                            sqlCommand
                        });

            var sql =
                migrationSqlGenerator.Generate(
                    new[] { insertHistoryOperation },
                    "2008").Join(s => s.Sql, Environment.NewLine);

            Assert.Contains("DELETE Foo\r\nWHERE Bar = 'Baz'", sql);
        }
        public void Can_get_and_set_properties()
        {
            var historyOperation = new HistoryOperation(new[] { new SqlCommand() });

            Assert.NotEmpty(historyOperation.Commands);
        }
Exemplo n.º 10
0
        public void Can_get_and_set_properties()
        {
            var historyOperation = new HistoryOperation(new[] { new SqlCommand() });

            Assert.NotEmpty(historyOperation.Commands);
        }
        /// <summary>
        /// Gera SQL para uma operação <see cref="HistoryOperation" />.
        /// </summary>
        /// <param name="opeHistorico"> The operation to produce SQL for. </param>
        protected virtual void Generate(HistoryOperation opeHistorico)
        {
            // It was removed because it does not currently use the Migration
            using (var ltextWriter = TextWriter())
            {
                //Console.WriteLine(opeHistorico);
                //if (opeHistorico is HistoryOperation)
                //{
                foreach (var tree in opeHistorico.CommandTrees)
                {
                    if (tree is DbInsertCommandTree)
                    {
                        var insert = (DbInsertCommandTree) tree;
                        var props = new List<string>();
                        var values = new List<string>();
                        foreach (DbSetClause clause in insert.SetClauses)
                        {
                            var prop = (DbPropertyExpression)clause.Property;
                            var val = (DbConstantExpression)clause.Value;

                            props.Add(prop.Property.Name);
                            values.Add(Generate((dynamic)val.Value));
                        }
                        ltextWriter.Write(" INSERT INTO ");
                        ltextWriter.Write("__MigrationHistory");
                        ltextWriter.Write(" ( " + string.Join(", " ,props) + " ) VALUES ");
                        ltextWriter.Write(" ( " + string.Join(", ", values) + " )");
                    }
                    if (tree is DbDeleteCommandTree)
                    {
                        var delete = (DbDeleteCommandTree)tree;

                    }
                }

                //Console.WriteLine(opeHistorico);
                //InsertHistoryOperation lhisOperacaoInsert = opeHistorico as InsertHistoryOperation;
                //ltextWriter.Write(" INSERT INTO ");
                //ltextWriter.Write("__MigrationHistory");
                //ltextWriter.Write(" ( MigrationId, Model, ProductVersion) VALUES ");
                //ltextWriter.Write(string.Format(" ('{0}', {1}, '{2}')",
                //    lhisOperacaoInsert.MigrationId,
                //    Generate(lhisOperacaoInsert.Model), lhisOperacaoInsert.ProductVersion));
                //}
                //else if (opeHistorico is DeleteHistoryOperation)
                //{
                //    DeleteHistoryOperation lhisOperacaoInsert = opeHistorico as DeleteHistoryOperation;
                //    ltextWriter.Write(" DELETE FROM ");
                //    ltextWriter.Write(pstrNomeTabelaMigration);
                //    ltextWriter.Write(string.Format(" WHERE MigrationId = '{0}'",
                //        lhisOperacaoInsert.MigrationId));
                //}

                ComandoSQL(ltextWriter);
            }
        }
Exemplo n.º 12
0
        public void Can_get_and_set_properties()
        {
            var historyOperation = new HistoryOperation(new[] { new DbInsertCommandTree() });

            Assert.NotEmpty(historyOperation.CommandTrees);
        }
        /// <summary>
        ///     Generates SQL for a <see cref="HistoryOperation" />.
        ///     Generated SQL should be added using the Statement method.
        /// </summary>
        /// <param name="historyOperation"> The operation to produce SQL for. </param>
        protected virtual void Generate(HistoryOperation historyOperation)
        {
            Check.NotNull(historyOperation, "historyOperation");

            using (var writer = Writer())
            {
                historyOperation.Commands.Each(
                    c =>
                        {
                            var sql = UpperCaseKeywords(c.CommandText);

                            // inline params
                            c.Parameters
                             .Cast<DbParameter>()
                             .Each(p => sql = sql.Replace(p.ParameterName, Generate((dynamic)p.Value)));

                            writer.Write(sql);
                        });

                Statement(writer);
            }
        }
Exemplo n.º 14
0
        public void Generate_can_output_delete_history_statement()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            using (var historyContext = new HistoryContext())
            {
                var historyRow
                    = new HistoryRow
                          {
                              MigrationId = "House Lannister",
                              ContextKey = "The pointy end"
                          };

                historyContext.History.Attach(historyRow);
                historyContext.History.Remove(historyRow);

                using (var commandTracer = new CommandTracer(historyContext))
                {
                    historyContext.SaveChanges();

                    var deleteHistoryOperation
                        = new HistoryOperation(commandTracer.CommandTrees.OfType<DbModificationCommandTree>().ToList());

                    var sql
                        = migrationSqlGenerator
                            .Generate(new[] { deleteHistoryOperation }, "4.0")
                            .Single();

                    Assert.Equal(@"DELETE [__MigrationHistory]
WHERE (([MigrationId] = N'House Lannister') AND ([ContextKey] = N'The pointy end'))", sql.Sql.Trim());
                }
            }
        }
        /// <summary>
        ///     Generates SQL for a <see cref="HistoryOperation" />.
        ///     Generated SQL should be added using the Statement method.
        /// </summary>
        /// <param name="historyOperation"> The operation to produce SQL for. </param>
        protected virtual void Generate(HistoryOperation historyOperation)
        {
            Check.NotNull(historyOperation, "historyOperation");

            using (var writer = Writer())
            {
                historyOperation.CommandTrees.Each(
                    commandTree =>
                        {
                            List<DbParameter> _;

                            switch (commandTree.CommandTreeKind)
                            {
                                case DbCommandTreeKind.Insert:

                                    writer.Write(
                                        string.Join(
                                            Environment.NewLine,
                                            DmlSqlGenerator.GenerateInsertSql(
                                                (DbInsertCommandTree)commandTree,
                                                out _,
                                                isLocalProvider: true,
                                                upperCaseKeywords: true,
                                                createParameters: false)));
                                    break;

                                case DbCommandTreeKind.Delete:
                                    writer.Write(
                                        string.Join(
                                            Environment.NewLine,
                                            DmlSqlGenerator.GenerateDeleteSql(
                                                (DbDeleteCommandTree)commandTree,
                                                out _,
                                                isLocalProvider: true,
                                                upperCaseKeywords: true,
                                                createParameters: false)));
                                    break;
                            }
                        });

                Statement(writer);
            }
        }
        protected virtual IEnumerable<MigrationStatement> Generate(HistoryOperation operation)
        {
            foreach (var commandTree in operation.CommandTrees)
            {
                List<DbParameter> _;

                switch (commandTree.CommandTreeKind)
                {
                    case DbCommandTreeKind.Insert:
                        using (var writer = SqlWriter())
                        {
                            writer.Write(DmlSqlGenerator.GenerateInsertSql((DbInsertCommandTree)commandTree, out _,
                                generateParameters: false));
                            yield return Statement(writer);
                        }
                        break;
                    case DbCommandTreeKind.Delete:
                        using (var writer = SqlWriter())
                        {
                            writer.Write(DmlSqlGenerator.GenerateDeleteSql((DbDeleteCommandTree)commandTree, out _,
                                generateParameters: false));
                            yield return Statement(writer);
                        }
                        break;
                }
            }
        }
        public void Generate_can_output_insert_history_statement()
        {
            var migrationSqlGenerator = new SqlServerMigrationSqlGenerator();

            var sqlCommand = new SqlCommand("insert Foo (Bar)\r\nvalues (p1)");
            sqlCommand.Parameters.Add(new SqlParameter("p1", "Baz"));

            var insertHistoryOperation
                = new HistoryOperation(
                    new[]
                        {
                            sqlCommand
                        });

            var sql =
                migrationSqlGenerator.Generate(
                    new[] { insertHistoryOperation },
                    "2008").Join(s => s.Sql, Environment.NewLine);

            Assert.Contains("INSERT Foo (Bar)\r\nVALUES ('Baz')", sql);
        }