public async Task <IActionResult> Update(Guid expressionId, [FromBody] UpdateExpression toUpdate)
        {
            toUpdate.UserId = UserId;
            await mediator.Send(toUpdate);

            return(new OkResult());
        }
        public void UpdateExpression_WithDefaultConstructor_ShouldInitializeUpdateCommandsProperty()
        {
            // Arrange
            var updateExpression = new UpdateExpression <ITweet>();

            // Assert
            Assert.IsNotNull(updateExpression.UpdateCommands, "UpdateCommands property should be correctly initialized.");
        }
        public void UpdateExpression_WithDefaultConstructor_ShouldReturnValidObject()
        {
            // Arrange
            var updateExpression = new UpdateExpression <ITweet>();

            // Assert
            Assert.IsNotNull(updateExpression, "UpdateExpression<ITweet> should not be null.");
        }
Beispiel #4
0
    protected internal override Expression VisitUpdate(UpdateExpression update)
    {
        if (update.ReturnRowCount == false)
        {
            return(update);
        }

        return(new UpdateExpression(update.Table, update.UseHistoryTable, update.Source, update.Where, update.Assigments, returnRowCount: false));
    }
Beispiel #5
0
        public async Task <Unit> Handle(UpdateExpression message, CancellationToken cancellationToken)
        {
            var expression = await repository.Find(message.Id);

            expression.ExpressionString = message.ExpressionString;
            expression.Name             = message.Name;
            await repository.Save();

            return(Unit.Value);
        }
        public void UpdateTest2()
        {
            var expression = new UpdateExpression(new[]
            {
                Change.Add("list", new[] { "A", "B", "C" }),
                Change.Remove("deleted")
            }, new JsonObject(), new AttributeCollection());

            Assert.Equal(@"REMOVE deleted
ADD list :v0", expression.ToString());
        }
        public void UpdateExpression_SetNullField_ShouldThrowArgumentNullExceptionWithFieldParamName()
        {
            // Arrange
            var updateExpression = new UpdateExpression <ITweet>();
            var value            = "Some value";

            // Act + Assert
            var exception = Assert.Throws <ArgumentNullException>(() => updateExpression.Set <string>(null, value), @"ArgumentNullException should be thrown.");

            Assert.AreEqual("field", exception.ParamName, @"ParamName should be ""field"".");
        }
        public void UpdateExpression_SetNullOrWhiteSpaceFieldName_ShouldThrowArgumentNullExceptionWithFieldNameParamName(string fieldName)
        {
            // Arrange
            var updateExpression = new UpdateExpression <ITweet>();
            var value            = "Some value";

            // Act + Assert
            var exception = Assert.Throws <ArgumentNullException>(() => updateExpression.Set(fieldName, value), @"ArgumentNullException should be thrown.");

            Assert.AreEqual(nameof(fieldName), exception.ParamName, @"ParamName should be ""fieldName"".");
        }
        public void UpdateTest3()
        {
            var expression = new UpdateExpression(new[]
            {
                Change.Replace("deleted", new DateTime(2015, 01, 01)),
                Change.Replace("colors", new [] { "red", "yellow", "blue" }),
                Change.Replace("version", 1)
            }, new JsonObject(), new AttributeCollection());

            Assert.Equal(@"SET deleted = :v0, colors = :v1, version = :v2", expression.ToString());
        }
    protected internal override Expression VisitUpdate(UpdateExpression update)
    {
        var where = Visit(update.Where);
        var assigments = Visit(update.Assigments, VisitColumnAssigment);
        var source     = Visit(update.Source);

        if (source != update.Source || where != update.Where || assigments != update.Assigments)
        {
            return(new UpdateExpression(update.Table, update.UseHistoryTable, (SourceWithAliasExpression)source, where, assigments, update.ReturnRowCount));
        }
        return(update);
    }
Beispiel #11
0
 public static ExpressionExecuter Build(Expression expression)
 {
     return(expression switch
     {
         AssignmentExpression assign => new AssignmentExpressionExecuter(assign),
         BinaryExpression binary => new BinaryExpressionExecuter(binary),
         UpdateExpression update => new UpdateExpressionExecuter(update),
         UnaryExpression unary => new UnaryExpressionExecuter(unary),
         Identifier identifier => new IdentifierExecuter(identifier),
         CallExpression call => new CallExpressionExecuter(call),
         Literal literal => new LiteralExecuter(literal),
         _ => ErrorHelper.ThrowUnknownNode <ExpressionExecuter>(expression),
     });
        public void UpdateExpression_WithDefaultConstructor_ShouldInitializeUpdateCommandsField()
        {
            // Arrange
            const string UpdateCommandsFieldName = "updateCommands";

            var updateExpression = new UpdateExpression <ITweet>();

            // Act
            var privateObject       = new Microsoft.VisualStudio.TestTools.UnitTesting.PrivateObject(updateExpression);
            var updateCommandsField = privateObject.GetField(UpdateCommandsFieldName);

            // Assert
            Assert.IsNotNull(updateCommandsField, "UpdateCommands field should be correctly initialized.");
        }
        public void UpdateExpression()
        {
            var expression = ParseExpression <UpdateExpression>("x++");

            var visitor = new AstVisitorEventSource();
            UpdateExpression updateExpression = null;
            Identifier       argument         = null;

            visitor.VisitingUpdateExpression += (_, arg) => updateExpression = arg;
            visitor.VisitedIdentifier        += (_, arg) => argument = arg;
            visitor.Visit(expression);

            Assert.Same(expression, updateExpression);
            Assert.Same(expression.Argument, argument);
        }
        public void UpdateTest4()
        {
            var expression = new UpdateExpression(new[]
            {
                Change.Replace("deleted", new DateTime(2015, 01, 01)),
                Change.Replace("colors", new [] { "red", "yellow", "blue" }),
                Change.Remove("deleted"),
                Change.Add("version", 1),
                Change.Replace("modified", DateTime.UtcNow)
            }, new JsonObject(), new AttributeCollection());

            Assert.Equal(@"SET deleted = :v0, colors = :v1, modified = :v3
REMOVE deleted
ADD version :v2", expression.ToString());
        }
Beispiel #15
0
        public void TestUpldateExpression()
        {
            float b = 5;
            ExecutionEnvironment e   = new ExecutionEnvironment();
            UpdateExpression     add = new UpdateExpression()
            {
                IsPrefix = false, Operator = UpdateOperator.Add, Expression = new Identifier()
                {
                    Variable = "a"
                }
            };
            UpdateExpression minus = new UpdateExpression()
            {
                IsPrefix = false, Operator = UpdateOperator.Minus, Expression = new Identifier()
                {
                    Variable = "a"
                }
            };
            //test int
            int a = 5;

            e.RegisterValue("a", a);
            Assert.AreEqual(add.Execute(e).ReturnValue, a++);
            Assert.AreEqual(e.GetValue("a"), a);
            Assert.AreEqual(minus.Execute(e).ReturnValue, a--);
            Assert.AreEqual(e.GetValue("a"), a);
            //test float
            b = 5;
            e.SetValue("a", b);
            Assert.AreEqual(add.Execute(e).ReturnValue, b++);
            Assert.AreEqual(e.GetValue("a"), b);
            Assert.AreEqual(minus.Execute(e).ReturnValue, b--);
            Assert.AreEqual(e.GetValue("a"), b);
            //test double
            double c = 7.1;

            e.SetValue("a", c);
            Assert.AreEqual(add.Execute(e).ReturnValue, c++);
            Assert.AreEqual(e.GetValue("a"), c);
            Assert.AreEqual(minus.Execute(e).ReturnValue, c--);
            Assert.AreEqual(e.GetValue("a"), c);
            //test string
            string s = "5";

            e.SetValue("a", s);
            Assert.AreEqual(add.Execute(e).Type, CompletionType.Exception);
            Assert.AreEqual(minus.Execute(e).Type, CompletionType.Exception);
        }
        public void UpdateTest1()
        {
            var locked = DateTimeOffset.FromUnixTimeSeconds(1497212690).UtcDateTime;

            var attributeNames = new JsonObject();

            var values = new AttributeCollection();

            var expression = new UpdateExpression(new[] {
                Change.Replace("locked", locked)
            }, attributeNames, values);

            Assert.Equal(@"SET locked = :v0", expression.ToString());

            Assert.Equal(1497212690L, values.Get(":v0").Value);
        }
        public void UpdateExpression_SetValidStringFieldNameAndAnyValueObject_ShouldRegisterSingleSetCommandInUpdateCommands(string fieldName, object value)
        {
            // Arrange
            var updateExpression = new UpdateExpression <ITweet>();

            // Act
            updateExpression.Set(fieldName, value);

            // Assert
            Assert.AreEqual(1, updateExpression.UpdateCommands.Count(), "Number of UpdateCommands should be 1.");

            var command = updateExpression.UpdateCommands.Single();

            Assert.IsNotNull(command, "IUpdateCommand object should not be null.");

            Assert.AreEqual("Set", command.UpdateVerb, @"UpdateVerb of the IUpdateCommand should be ""Set"".");
            Assert.AreEqual(fieldName, command.FieldName, @"FieldName of the IUpdateCommand should be """ + fieldName + @""".");
            Assert.AreSame(value, command.Value, @"Value of the IUpdateCommand should be """ + value + @""".");
        }
Beispiel #18
0
        protected virtual void Update(IEntityProxy entity, TableAttribute table, IEnumerable <ColumnInfo> columns)
        {
            //TODO: do not let an identity field to be dirtied
            var update = new UpdateExpression(new TableExpression(table));

            foreach (var column in columns.Where(c => c.IsIdentity))
            {
                update.AddWhere(column.ToEqualExpression(entity));
            }
            var type = entity.GetType();

            foreach (var property in entity.DirtyProperties)
            {
                var pi     = type.GetProperty(property);
                var column = columns.First(c => c.DescribesProperty(pi));
                update.AddSet(new SetExpression(column.ToColumnExpression(), Expression.Constant(pi.GetValue(entity))));
            }
            ExecuteNonQuery(update);
        }
Beispiel #19
0
        public JsValue EvaluateUpdateExpression(UpdateExpression updateExpression)
        {
            var       value = _engine.EvaluateExpression(updateExpression.Argument);
            Reference r;

            switch (updateExpression.Operator)
            {
            case UnaryOperator.Increment:
                r = value as Reference;
                if (r != null &&
                    r.IsStrict() &&
                    (r.GetBase().TryCast <EnvironmentRecord>() != null) &&
                    (Array.IndexOf(new[] { "eval", "arguments" }, r.GetReferencedName()) != -1))
                {
                    throw new JavaScriptException(_engine.SyntaxError);
                }

                var oldValue = TypeConverter.ToNumber(_engine.GetValue(value));
                var newValue = oldValue + 1;
                _engine.PutValue(r, newValue);

                return(updateExpression.Prefix ? newValue : oldValue);

            case UnaryOperator.Decrement:
                r = value as Reference;
                if (r != null &&
                    r.IsStrict() &&
                    (r.GetBase().TryCast <EnvironmentRecord>() != null) &&
                    (Array.IndexOf(new[] { "eval", "arguments" }, r.GetReferencedName()) != -1))
                {
                    throw new JavaScriptException(_engine.SyntaxError);
                }

                oldValue = TypeConverter.ToNumber(_engine.GetValue(value));
                newValue = oldValue - 1;
                _engine.PutValue(r, newValue);

                return(updateExpression.Prefix ? newValue : oldValue);

            default:
                throw new ArgumentException();
            }
        }
Beispiel #20
0
        internal override string GetUpdateCommand(UpdateExpression expression)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append($"UPDATE {expression.TableName} SET ");
            foreach (var setting in expression.SettingList)
            {
                stringBuilder.Append($"{setting.ColumnName}={setting.Value}");
                if (setting != expression.SettingList.Last())
                {
                    stringBuilder.Append(", ");
                }
            }
            if (expression.ConditionBracket != null)
            {
                stringBuilder.Append($" {GetConditionCommand(expression.ConditionBracket)}");
            }
            stringBuilder.Append(";");
            return(stringBuilder.ToString());
        }
        public void UpdateExpression_SetValidExpressionForFavesFieldAndValidValue_ShouldRegisterSingleSetCommandInUpdateCommands()
        {
            // Arrange
            var updateExpression = new UpdateExpression <ITweet>();
            var value            = 123;

            // Act
            updateExpression.Set(t => t.Faves, value);

            // Assert
            Assert.AreEqual(1, updateExpression.UpdateCommands.Count(), "Number of UpdateCommands should be 1.");

            var command = updateExpression.UpdateCommands.Single();

            Assert.IsNotNull(command, "IUpdateCommand object should not be null.");

            Assert.AreEqual("Set", command.UpdateVerb, @"UpdateVerb of the IUpdateCommand should be ""Set"".");
            Assert.AreEqual("Faves", command.FieldName, @"FieldName of the IUpdateCommand should be ""Faves"".");
            Assert.AreEqual(value, command.Value, @"Value of the IUpdateCommand should be """ + value + @""".");
        }
Beispiel #22
0
    protected internal override Expression VisitUpdate(UpdateExpression update)
    {
        var coll = GetColumnCollector(update.Source.KnownAliases);

        coll.Visit(update.Where);
        foreach (var ca in update.Assigments)
        {
            coll.Visit(ca.Expression);
        }

        var source = Visit(update.Source);

        var where = Visit(update.Where);
        var assigments = Visit(update.Assigments, VisitColumnAssigment);

        if (source != update.Source || where != update.Where || assigments != update.Assigments)
        {
            return(new UpdateExpression(update.Table, update.UseHistoryTable, (SourceWithAliasExpression)source, where, assigments, update.ReturnRowCount));
        }

        return(update);
    }
Beispiel #23
0
        protected override Expression VisitUpdate(UpdateExpression update)
        {
            sb.Append("UPDATE ");
            sb.Append(update.Table.Name.ToString());
            sb.Append(" SET");
            this.AppendNewLine(Indentation.Inner);
           
            for (int i = 0, n = update.Assigments.Count; i < n; i++)
            {
                ColumnAssignment assignment= update.Assigments[i];
                if (i > 0)
                {
                    sb.Append(",");
                    this.AppendNewLine(Indentation.Same);
                }
                sb.Append(assignment.Column.SqlEscape());
                sb.Append(" = ");
                this.Visit(assignment.Expression);
            }
            this.AppendNewLine(Indentation.Outer);
            sb.Append("FROM ");
            VisitSource(update.Source);
            if (update.Where != null)
            {
                this.AppendNewLine(Indentation.Same);
                sb.Append("WHERE ");
                Visit(update.Where);
            }
            return update; 

        }
Beispiel #24
0
        protected override Expression VisitUpdate(UpdateExpression update)
        {
            Visit(update.Where);
            update.Assigments.NewIfChange(VisitColumnAssigment);

            var source = Visit(update.Source);
            var where = Visit(update.Where);
            var assigments = update.Assigments.NewIfChange(VisitColumnAssigment);
            if (source != update.Source || where != update.Where || assigments != update.Assigments)
                return new UpdateExpression(update.Table, (SourceWithAliasExpression)source, where, assigments);

            return update;
        }
Beispiel #25
0
 protected override Expression VisitUpdate(UpdateExpression update)
 {
     var source = Visit(update.Source);
     var where = Visit(update.Where);
     var assigments = update.Assigments.NewIfChange(c =>
     {
         var exp = MakeSqlValue(Visit(c.Expression));
         if (exp != c.Expression)
             return new ColumnAssignment(c.Column, exp);
         return c;
     });
     if (source != update.Source || where != update.Where || assigments != update.Assigments)
         return new UpdateExpression(update.Table, (SelectExpression)source, where, assigments);
     return update;
 }
Beispiel #26
0
 public virtual void VisitUpdateExpression(UpdateExpression updateExpression)
 {
 }
Beispiel #27
0
        public JsValue EvaluateUpdateExpression(UpdateExpression updateExpression)
        {
            var value = _engine.EvaluateExpression(updateExpression.Argument);
            Reference r;

            switch (updateExpression.Operator)
            {
                case UnaryOperator.Increment:
                    r = value as Reference;
                    if (r != null
                        && r.IsStrict()
                        && (r.GetBase().TryCast<EnvironmentRecord>() != null)
                        && (Array.IndexOf(new[] { "eval", "arguments" }, r.GetReferencedName()) != -1))
                    {
                        throw new JavaScriptException(_engine.SyntaxError);
                    }

                    var oldValue = TypeConverter.ToNumber(_engine.GetValue(value));
                    var newValue = oldValue + 1;
                    _engine.PutValue(r, newValue);

                    return updateExpression.Prefix ? newValue : oldValue;

                case UnaryOperator.Decrement:
                    r = value as Reference;
                    if (r != null
                        && r.IsStrict()
                        && (r.GetBase().TryCast<EnvironmentRecord>() != null)
                        && (Array.IndexOf(new[] { "eval", "arguments" }, r.GetReferencedName()) != -1))
                    {
                        throw new JavaScriptException(_engine.SyntaxError);
                    }

                    oldValue = TypeConverter.ToNumber(_engine.GetValue(value));
                    newValue = oldValue - 1;
                    _engine.PutValue(r, newValue);

                    return updateExpression.Prefix ? newValue : oldValue;
                default:
                    throw new ArgumentException();
            }

        }
Beispiel #28
0
 protected override void Visit(UpdateExpression node)
 {
     Encode(node, Member("operator", node.Operator), Member("argument", node.Argument),
            Member("prefix", node.Prefix));
 }
Beispiel #29
0
 protected abstract void Visit(UpdateExpression node);
Beispiel #30
0
 protected virtual void VisitUpdateExpression(UpdateExpression updateExpression)
 {
     VisitExpression(updateExpression.Argument);
 }
Beispiel #31
0
 public JintUpdateExpression(Engine engine, UpdateExpression expression) : base(engine, expression)
 {
     _initialized = false;
 }
 protected override void VisitUpdateExpression(UpdateExpression updateExpression)
 {
 }
Beispiel #33
0
 private void EmitUpdateExpression(UpdateExpression e)
 {
     using (var ec = new ExpressionContext(this, e))
     {
         if (e.Prefix)
         {
             Write(ec.Token);
             Emit(e.Argument);
         }
         else
         {
             Emit(e.Argument);
             Write(ec.Token);
         }
     }
 }
Beispiel #34
0
 protected override void VisitUpdateExpression(UpdateExpression updateExpression) =>
 VisitUnaryExpression(updateExpression);
Beispiel #35
0
 protected internal override void VisitUpdateExpression(UpdateExpression updateExpression)
 {
     VisitingUpdateExpression?.Invoke(this, updateExpression);
     base.VisitUpdateExpression(updateExpression);
     VisitedUpdateExpression?.Invoke(this, updateExpression);
 }