public void ModificationCommand_initialized_correctly_for_added_entities_with_non_temp_generated_key()
        {
            var entry = CreateEntry(EntityState.Added, generateKeyValues: true);
            entry.MarkAsTemporary(entry.EntityType.FindPrimaryKey().Properties[0], isTemporary: false);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            Assert.Equal("T1", command.TableName);
            Assert.Null(command.Schema);
            Assert.Equal(EntityState.Added, command.EntityState);
            Assert.Equal(2, command.ColumnModifications.Count);

            var columnMod = command.ColumnModifications[0];

            Assert.Equal("Col1", columnMod.ColumnName);
            Assert.Same(entry, columnMod.Entry);
            Assert.Equal("Id", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.True(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);

            columnMod = command.ColumnModifications[1];

            Assert.Equal("Col2", columnMod.ColumnName);
            Assert.Same(entry, columnMod.Entry);
            Assert.Equal("Name", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.False(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);
        }
        public override bool AddCommand(ModificationCommand modificationCommand)
        {
            Check.NotNull(modificationCommand, nameof(modificationCommand));

            if (ModificationCommands.Count == 0)
            {
                ResetCommandText();
            }

            if (!CanAddCommand(modificationCommand))
            {
                return false;
            }

            _modificationCommands.Add(modificationCommand);
            CommandResultSet.Add(ResultSetMapping.LastInResultSet);

            if (!IsCommandTextValid())
            {
                ResetCommandText();
                _modificationCommands.RemoveAt(_modificationCommands.Count - 1);
                CommandResultSet.RemoveAt(CommandResultSet.Count - 1);
                return false;
            }

            return true;
        }
        public virtual ResultSetMapping AppendDeleteOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
        {
            Check.NotNull(commandStringBuilder, nameof(commandStringBuilder));
            Check.NotNull(command, nameof(command));

            var name = command.TableName;
            var schema = command.Schema;
            var conditionOperations = command.ColumnModifications.Where(o => o.IsCondition).ToArray();

            AppendDeleteCommand(commandStringBuilder, name, schema, conditionOperations);

            return AppendSelectAffectedCountCommand(commandStringBuilder, name, schema, commandPosition);
        }
        public void AddCommand_does_not_add_command_if_resulting_sql_is_invalid()
        {
            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            var batch = new ModificationCommandBatchFake();
            batch.AddCommand(command);
            batch.ShouldAddCommand = true;
            batch.ShouldValidateSql = false;

            batch.AddCommand(command);

            Assert.Equal(1, batch.ModificationCommands.Count);
            Assert.Equal(".", batch.CommandText);
        }
        public void AddCommand_adds_command_if_possible()
        {
            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            var batch = new ModificationCommandBatchFake();
            batch.AddCommand(command);
            batch.ShouldAddCommand = true;
            batch.ShouldValidateSql = true;

            batch.AddCommand(command);

            Assert.Equal(2, batch.ModificationCommands.Count);
            Assert.Same(command, batch.ModificationCommands[0]);
            Assert.Equal("..", batch.CommandText);
        }
        public void UpdateCommandText_compiles_inserts()
        {
            var entry = CreateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            var sqlGeneratorMock = new Mock<IUpdateSqlGenerator>();
            var batch = new ModificationCommandBatchFake(sqlGeneratorMock.Object);
            batch.AddCommand(command);

            batch.UpdateCachedCommandTextBase(0);

            sqlGeneratorMock.Verify(g => g.AppendBatchHeader(It.IsAny<StringBuilder>()));
            sqlGeneratorMock.Verify(g => g.AppendInsertOperation(It.IsAny<StringBuilder>(), command, 0));
        }
        public virtual ResultSetMapping AppendUpdateOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
        {
            Check.NotNull(commandStringBuilder, nameof(commandStringBuilder));
            Check.NotNull(command, nameof(command));

            var name = command.TableName;
            var schema = command.Schema;
            var operations = command.ColumnModifications;

            var writeOperations = operations.Where(o => o.IsWrite).ToArray();
            var conditionOperations = operations.Where(o => o.IsCondition).ToArray();
            var readOperations = operations.Where(o => o.IsRead).ToArray();

            AppendUpdateCommand(commandStringBuilder, name, schema, writeOperations, conditionOperations);

            if (readOperations.Length > 0)
            {
                var keyOperations = operations.Where(o => o.IsKey).ToArray();

                return AppendSelectAffectedCommand(commandStringBuilder, name, schema, readOperations, keyOperations, commandPosition);
            }
            return AppendSelectAffectedCountCommand(commandStringBuilder, name, schema, commandPosition);
        }
        public async Task Exception_thrown_if_no_rows_returned_for_command_with_store_generated_values()
        {
            var entry = CreateEntry(EntityState.Added, generateKeyValues: true);
            entry.MarkAsTemporary(entry.EntityType.FindPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            var connection = CreateConnection(
                CreateFakeDataReader(new[] { "Col1" }, new List<object[]>()));

            var batch = new ModificationCommandBatchFake();
            batch.AddCommand(command);

            Assert.Equal(RelationalStrings.UpdateConcurrencyException(1, 0),
                (await Assert.ThrowsAsync<DbUpdateConcurrencyException>(
                    async () => await batch.ExecuteAsync(connection))).Message);
        }
        public void RequiresResultPropagation_false_for_Delete_operation()
        {
            var entry = CreateEntry(
                EntityState.Deleted, generateKeyValues: true, computeNonKeyValue: true);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            Assert.False(command.RequiresResultPropagation);
        }
        public async Task Exception_thrown_if_rows_returned_for_command_without_store_generated_values_is_not_1()
        {
            var entry = CreateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            var connection = CreateConnection(
                CreateFakeDataReader(new[] { "Col1" }, new List<object[]> { new object[] { 42 } }));

            var batch = new ModificationCommandBatchFake();
            batch.AddCommand(command);

            Assert.Equal(RelationalStrings.UpdateConcurrencyException(1, 42),
                (await Assert.ThrowsAsync<DbUpdateConcurrencyException>(
                    async () => await batch.ExecuteAsync(connection))).Message);
        }
        public async Task Exception_not_thrown_for_more_than_one_row_returned_for_single_command()
        {
            var entry = CreateEntry(EntityState.Added, generateKeyValues: true);
            entry.MarkAsTemporary(entry.EntityType.FindPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            var connection = CreateConnection(
                CreateFakeDataReader(
                    new[] { "Col1" },
                    new List<object[]>
                    {
                        new object[] { 42 },
                        new object[] { 43 }
                    }));

            var batch = new ModificationCommandBatchFake();
            batch.AddCommand(command);

            await batch.ExecuteAsync(connection);

            Assert.Equal(42, entry[entry.EntityType.FindProperty("Id")]);
        }
        public async Task ExecuteAsync_saves_store_generated_values_when_updating()
        {
            var entry = CreateEntry(
                EntityState.Modified, generateKeyValues: true, computeNonKeyValue: true);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            var connection = CreateConnection(
                CreateFakeDataReader(new[] { "Col2" }, new List<object[]> { new object[] { "FortyTwo" } }));

            var batch = new ModificationCommandBatchFake();
            batch.AddCommand(command);

            await batch.ExecuteAsync(connection);

            Assert.Equal(1, entry[entry.EntityType.FindProperty("Id")]);
            Assert.Equal("FortyTwo", entry[entry.EntityType.FindProperty("Name")]);
        }
        public void Compare_returns_0_only_for_commands_that_are_equal()
        {
            var modelBuilder = new ModelBuilder(TestRelationalConventionSetBuilder.Build());
            var model        = modelBuilder.Model;
            var entityType   = model.AddEntityType(typeof(object));
            var key          = entityType.AddProperty("Id", typeof(int));

            entityType.SetPrimaryKey(key);

            var optionsBuilder = new DbContextOptionsBuilder()
                                 .UseModel(RelationalTestHelpers.Instance.Finalize(modelBuilder))
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString())
                                 .UseInternalServiceProvider(InMemoryFixture.DefaultServiceProvider);

            var stateManager = new DbContext(optionsBuilder.Options).GetService <IStateManager>();

            var entry1 = stateManager.GetOrCreateEntry(new object());

            entry1[(IProperty)key] = 1;
            entry1.SetEntityState(EntityState.Added);
            var modificationCommandAdded = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null);

            modificationCommandAdded.AddEntry(entry1, true);

            var entry2 = stateManager.GetOrCreateEntry(new object());

            entry2[(IProperty)key] = 2;
            entry2.SetEntityState(EntityState.Modified);
            var modificationCommandModified = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null);

            modificationCommandModified.AddEntry(entry2, true);

            var entry3 = stateManager.GetOrCreateEntry(new object());

            entry3[(IProperty)key] = 3;
            entry3.SetEntityState(EntityState.Deleted);
            var modificationCommandDeleted = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null);

            modificationCommandDeleted.AddEntry(entry3, true);

            var mCC = new ModificationCommandComparer();

            Assert.True(0 == mCC.Compare(modificationCommandAdded, modificationCommandAdded));
            Assert.True(0 == mCC.Compare(null, null));
            Assert.True(
                0
                == mCC.Compare(
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null)));

            Assert.True(0 > mCC.Compare(null, new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.True(0 < mCC.Compare(new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null), null));

            Assert.True(
                0
                > mCC.Compare(
                    new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.True(
                0
                < mCC.Compare(
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null)));

            Assert.True(
                0
                > mCC.Compare(
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", "foo", new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.True(
                0
                < mCC.Compare(
                    new ModificationCommand("A", "foo", new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null)));

            Assert.True(
                0
                > mCC.Compare(
                    new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("B", null, new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.True(
                0
                < mCC.Compare(
                    new ModificationCommand("B", null, new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null)));

            Assert.True(0 > mCC.Compare(modificationCommandModified, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandModified));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandDeleted));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandModified));
            Assert.True(0 < mCC.Compare(modificationCommandModified, modificationCommandDeleted));
        }
Example #14
0
        public void Compare_returns_0_only_for_commands_that_are_equal()
        {
            var model      = new Model();
            var entityType = model.AddEntityType(typeof(object));

            var optionsBuilder = new DbContextOptionsBuilder()
                                 .UseModel(model);

            optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString());

            var stateManager = new DbContext(optionsBuilder.Options).GetService <IStateManager>();

            var key = entityType.AddProperty("Id", typeof(int));

            entityType.GetOrSetPrimaryKey(key);

            var entry1 = stateManager.GetOrCreateEntry(new object());

            entry1[key] = 1;
            entry1.SetEntityState(EntityState.Added);
            var modificationCommandAdded = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null);

            modificationCommandAdded.AddEntry(entry1);

            var entry2 = stateManager.GetOrCreateEntry(new object());

            entry2[key] = 2;
            entry2.SetEntityState(EntityState.Modified);
            var modificationCommandModified = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null);

            modificationCommandModified.AddEntry(entry2);

            var entry3 = stateManager.GetOrCreateEntry(new object());

            entry3[key] = 3;
            entry3.SetEntityState(EntityState.Deleted);
            var modificationCommandDeleted = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null);

            modificationCommandDeleted.AddEntry(entry3);

            var mCC = new ModificationCommandComparer();

            Assert.True(0 == mCC.Compare(modificationCommandAdded, modificationCommandAdded));
            Assert.True(0 == mCC.Compare(null, null));
            Assert.True(
                0 == mCC.Compare(
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null)));

            Assert.True(0 > mCC.Compare(null, new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.True(0 < mCC.Compare(new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null), null));

            Assert.True(
                0 > mCC.Compare(
                    new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.True(
                0 < mCC.Compare(
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null)));

            Assert.True(
                0 > mCC.Compare(
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", "foo", new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.True(
                0 < mCC.Compare(
                    new ModificationCommand("A", "foo", new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, false, null)));

            Assert.True(
                0 > mCC.Compare(
                    new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("B", null, new ParameterNameGenerator().GenerateNext, false, null)));
            Assert.True(
                0 < mCC.Compare(
                    new ModificationCommand("B", null, new ParameterNameGenerator().GenerateNext, false, null),
                    new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, false, null)));

            Assert.True(0 > mCC.Compare(modificationCommandModified, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandModified));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandDeleted));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandModified));
            Assert.True(0 < mCC.Compare(modificationCommandModified, modificationCommandDeleted));
        }
        /// <summary>
        ///     Appends a SQL command for updating a row to the commands being built.
        /// </summary>
        /// <param name="commandStringBuilder"> The builder to which the SQL should be appended. </param>
        /// <param name="command"> The command that represents the delete operation. </param>
        /// <param name="commandPosition"> The ordinal of this command in the batch. </param>
        /// <returns> The <see cref="ResultSetMapping" /> for the command. </returns>
        public virtual ResultSetMapping AppendUpdateOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
        {
            Check.NotNull(commandStringBuilder, nameof(commandStringBuilder));
            Check.NotNull(command, nameof(command));

            var name       = command.TableName;
            var schema     = command.Schema;
            var operations = command.ColumnModifications;

            var writeOperations     = operations.Where(o => o.IsWrite).ToList();
            var conditionOperations = operations.Where(o => o.IsCondition).ToList();
            var readOperations      = operations.Where(o => o.IsRead).ToList();

            AppendUpdateCommand(commandStringBuilder, name, schema, writeOperations, conditionOperations);

            if (readOperations.Count > 0)
            {
                var keyOperations = operations.Where(o => o.IsKey).ToList();

                return(AppendSelectAffectedCommand(commandStringBuilder, name, schema, readOperations, keyOperations, commandPosition));
            }

            return(AppendSelectAffectedCountCommand(commandStringBuilder, name, schema, commandPosition));
        }
        public void ModificationCommand_throws_for_unknown_entities()
        {
            var entry = CreateEntry(EntityState.Detached);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());

            Assert.Equal(
                RelationalStrings.ModificationFunctionInvalidEntityState(EntityState.Detached),
                Assert.Throws<ArgumentException>(() => command.AddEntry(entry)).Message);
        }
 protected override bool CanAddCommand(ModificationCommand modificationCommand) => ShouldAddCommand;
        public void ModificationCommand_initialized_correctly_for_added_entities_with_explicitly_specified_key_value()
        {
            var entry = CreateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            Assert.Equal("T1", command.TableName);
            Assert.Null(command.Schema);
            Assert.Equal(EntityState.Added, command.EntityState);
            Assert.Equal(2, command.ColumnModifications.Count);

            var columnMod = command.ColumnModifications[0];

            Assert.Equal("Col1", columnMod.ColumnName);
            Assert.Same(entry, columnMod.Entry);
            Assert.Equal("Id", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.True(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);

            columnMod = command.ColumnModifications[1];

            Assert.Equal("Col2", columnMod.ColumnName);
            Assert.Same(entry, columnMod.Entry);
            Assert.Equal("Name", columnMod.Property.Name);
            Assert.False(columnMod.IsCondition);
            Assert.False(columnMod.IsKey);
            Assert.False(columnMod.IsRead);
            Assert.True(columnMod.IsWrite);
        }
 protected override bool CanAddCommand(ModificationCommand modificationCommand)
     => ModificationCommands.Count == 0;
        public void ColumnModifications_throw_on_temporary_values_with_no_store_generation_configured()
        {
            var entry = CreateEntry(EntityState.Added);
            entry.MarkAsTemporary(entry.EntityType.FindPrimaryKey().Properties[0]);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            Assert.Equal(
                CoreStrings.TempValue("Id", "T1"),
                Assert.Throws<InvalidOperationException>(() => command.ColumnModifications).Message);
        }
        public void RequiresResultPropagation_false_for_Update_operation_if_no_non_key_store_generated_columns_exist()
        {
            var entry = CreateEntry(EntityState.Modified, generateKeyValues: true);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            Assert.False(command.RequiresResultPropagation);
        }
        public void RequiresResultPropagation_true_for_Insert_operation_if_store_generated_columns_exist()
        {
            var entry = CreateEntry(
                EntityState.Added, generateKeyValues: true, computeNonKeyValue: true);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            Assert.True(command.RequiresResultPropagation);
        }
 /// <summary>
 ///     Checks whether or not a new command can be added to the batch.
 /// </summary>
 /// <param name="modificationCommand"> The command to potentially add. </param>
 /// <returns> <c>True</c> if the command can be added; <c>false</c> otherwise. </returns>
 protected abstract bool CanAddCommand([NotNull] ModificationCommand modificationCommand);
        public void Compare_returns_0_only_for_commands_that_are_equal()
        {
            var model = new Model();
            var entityType = model.AddEntityType(typeof(object));

            var optionsBuilder = new DbContextOptionsBuilder()
                .UseModel(model);
            optionsBuilder.UseInMemoryDatabase();

            var contextServices = new DbContext(optionsBuilder.Options).GetInfrastructure();
            var stateManager = contextServices.GetRequiredService<IStateManager>();

            var key = entityType.AddProperty("Id", typeof(int));
            entityType.GetOrSetPrimaryKey(key);

            var entry1 = stateManager.GetOrCreateEntry(new object());
            entry1[key] = 1;
            entry1.SetEntityState(EntityState.Added);
            var modificationCommandAdded = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            modificationCommandAdded.AddEntry(entry1);

            var entry2 = stateManager.GetOrCreateEntry(new object());
            entry2[key] = 2;
            entry2.SetEntityState(EntityState.Modified);
            var modificationCommandModified = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            modificationCommandModified.AddEntry(entry2);

            var entry3 = stateManager.GetOrCreateEntry(new object());
            entry3[key] = 3;
            entry3.SetEntityState(EntityState.Deleted);
            var modificationCommandDeleted = new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            modificationCommandDeleted.AddEntry(entry3);

            var mCC = new ModificationCommandComparer();

            Assert.True(0 == mCC.Compare(modificationCommandAdded, modificationCommandAdded));
            Assert.True(0 == mCC.Compare(null, null));
            Assert.True(0 == mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(null, new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider()), null));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                new ModificationCommand("A", "foo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("A", "foo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                new ModificationCommand("A", "dbo", new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(
                new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                new ModificationCommand("B", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));
            Assert.True(0 < mCC.Compare(
                new ModificationCommand("B", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider()),
                new ModificationCommand("A", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider())));

            Assert.True(0 > mCC.Compare(modificationCommandModified, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandModified));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandAdded));
            Assert.True(0 < mCC.Compare(modificationCommandAdded, modificationCommandDeleted));

            Assert.True(0 > mCC.Compare(modificationCommandDeleted, modificationCommandModified));
            Assert.True(0 < mCC.Compare(modificationCommandModified, modificationCommandDeleted));
        }
 public abstract bool AddCommand([NotNull] ModificationCommand modificationCommand);
Example #26
0
 /// <summary>
 ///     Only returns <c>true</c> if the no command has already been added.
 /// </summary>
 /// <param name="modificationCommand"> The command to potentially add. </param>
 /// <returns> <c>True</c> if no command has already been added. </returns>
 protected override bool CanAddCommand(ModificationCommand modificationCommand)
 => ModificationCommands.Count == 0;
        /// <summary>
        ///     Appends a SQL command for deleting a row to the commands being built.
        /// </summary>
        /// <param name="commandStringBuilder"> The builder to which the SQL should be appended. </param>
        /// <param name="command"> The command that represents the delete operation. </param>
        /// <param name="commandPosition"> The ordinal of this command in the batch. </param>
        /// <returns> The <see cref="ResultSetMapping" /> for the command. </returns>
        public virtual ResultSetMapping AppendDeleteOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
        {
            Check.NotNull(commandStringBuilder, nameof(commandStringBuilder));
            Check.NotNull(command, nameof(command));

            var name   = command.TableName;
            var schema = command.Schema;
            var conditionOperations = command.ColumnModifications.Where(o => o.IsCondition).ToList();

            AppendDeleteCommand(commandStringBuilder, name, schema, conditionOperations);

            return(AppendSelectAffectedCountCommand(commandStringBuilder, name, schema, commandPosition));
        }
        public void UpdateCommandText_compiles_multiple_commands()
        {
            var entry = CreateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            var fakeSqlGenerator = new FakeSqlGenerator();
            var batch = new ModificationCommandBatchFake(fakeSqlGenerator);
            batch.AddCommand(command);
            batch.AddCommand(command);

            Assert.Equal("..", batch.CommandText);

            Assert.Equal(1, fakeSqlGenerator.AppendBatchHeaderCalls);
        }
Example #29
0
 protected override bool CanAddCommand(ModificationCommand modificationCommand) => ShouldAddCommand;
            public override ResultSetMapping AppendInsertOperation(StringBuilder commandStringBuilder, ModificationCommand command, int commandPosition)
            {
                if (!string.IsNullOrEmpty(command.Schema))
                {
                    commandStringBuilder.Append(command.Schema + ".");
                }
                commandStringBuilder.Append(command.TableName);

                return ResultSetMapping.NotLastInResultSet;
            }
        public async Task ExecuteAsync_executes_batch_commands_and_consumes_reader()
        {
            var entry = CreateEntry(EntityState.Added);

            var command = new ModificationCommand("T1", null, new ParameterNameGenerator().GenerateNext, p => p.TestProvider());
            command.AddEntry(entry);

            var dbDataReader = CreateFakeDataReader();

            var connection = CreateConnection(dbDataReader);

            var batch = new ModificationCommandBatchFake();
            batch.AddCommand(command);

            await batch.ExecuteAsync(connection);

            Assert.Equal(1, dbDataReader.ReadAsyncCount);
            Assert.Equal(1, dbDataReader.GetInt32Count);
        }
Example #32
0
 /// <summary>
 ///     Adds the given insert/update/delete <see cref="ModificationCommands" /> to the batch.
 /// </summary>
 /// <param name="modificationCommand"> The command to add. </param>
 /// <returns>
 ///     <see langword="true" /> if the command was successfully added; <see langword="false" /> if there was no
 ///     room in the current batch to add the command and it must instead be added to a new batch.
 /// </returns>
 public abstract bool AddCommand(ModificationCommand modificationCommand);