Griffin.Data.Mapper.CommandBuilders.CommandBuilder.UpdateCommand C# (CSharp) Method

UpdateCommand() public method

Create an update query from the entity.
/// command /// or /// entity /// /// At least one property (other than primary keys) must be specified. /// or ///
public UpdateCommand ( IDbCommand command, object entity ) : void
command IDbCommand Command to modify
entity object Entity to update
return void
        public void UpdateCommand(IDbCommand command, object entity)
        {
            if (command == null) throw new ArgumentNullException("command");
            if (entity == null) throw new ArgumentNullException("entity");

            var updates = "";
            var where = "";
            foreach (var property in _values)
            {
                if (!property.CanRead)
                    continue;

                var value = property.GetValue(entity);
                updates += string.Format("{0}=@{1}, ", property.ColumnName, property.PropertyName);
                command.AddParameter(property.PropertyName, value);
            }
            if (command.Parameters.Count == 0)
                throw new DataException("At least one property (other than primary keys) must be specified.");

            foreach (var property in _keys)
            {
                var value = property.GetValue(entity);
                if (value == null || value == DBNull.Value)
                    throw new DataException(
                        string.Format("Entity {0}' do not contain a value for the key property '{1}'", entity,
                            property.PropertyName));
                where += property.ColumnName + "=" + "@" + property.PropertyName + " AND ";
                command.AddParameter(property.PropertyName, value);
            }

            if (updates.Length < 2 || where.Length < 5)
                throw new DataException(string.Format("Could not construct a proper UPDATE command. Is your mapping for '{0}' correct?\r\n UPDATE clause '{1}'\r\n WHERE clause '{2}'", entity.GetType().FullName, updates, @where));

            command.CommandText = string.Format("UPDATE {0} SET {1} WHERE {2}",
                TableName,
                updates.Remove(updates.Length - 2, 2),
                @where.Remove(@where.Length - 5, 5));
        }

Usage Example

        public void field_without_value_is_added_with_DbNull_in_the_update_query()
        {
            var mapper = Substitute.For<ICrudEntityMapper>();
            mapper.TableName.Returns("Users");
            mapper.Properties.Returns(new Dictionary<string, IPropertyMapping>
            {
                {"Name", new FakePropertyMapping("Name", "real_name") {CanRead = true} },
                {"Id", new FakePropertyMapping("Id", "id"){Value = "Hello", IsPrimaryKey = true}}
            });
            var command = new AdoNetFakes.FakeCommand();
            var entity = new { Id = "Hello" };


            var sut = new CommandBuilder(mapper);
            sut.UpdateCommand(command, entity);

            command.CommandText.Should().Be("UPDATE Users SET real_name=@Name WHERE id=@Id");
            command.Parameters[0].ParameterName.Should().Be("Name");
            command.Parameters[0].Value.Should().Be(DBNull.Value);
            command.Parameters[1].ParameterName.Should().Be("Id");
            command.Parameters[1].Value.Should().Be("Hello");
        }
All Usage Examples Of Griffin.Data.Mapper.CommandBuilders.CommandBuilder::UpdateCommand