Example #1
0
 public virtual IExpression Clone(ConcatenationOperator op)
 {
     return(new ConcatenationOperator()
     {
         Left = CloneAndReturn(op.Left),
         Right = CloneValue(op.Left, op.Right)
     }.Normalize());
 }
Example #2
0
        private IExpression ReplaceParameters(string value)
        {
            const char invalid = '\uFFFF';

            var parameters = new List <ParameterReference>();
            var str        = Regex.Replace(value, @"(\$\$)|(\$\w+)", match =>
            {
                if (match.Value == "$$")
                {
                    return("$");
                }

                var param = _parameters.Find(p => p.Name == match.Value.Substring(1));
                if (param == null)
                {
                    throw new ArgumentException($"Invalid parameter name `{match.Value}`");
                }

                parameters.Add(param);
                return(invalid.ToString());
            });

            if (str == invalid.ToString() && parameters.Count == 1)
            {
                return(parameters[0]);
            }
            if (parameters.Count == 0)
            {
                return(new StringLiteral(str));
            }

            var parts       = str.Split(invalid);
            var expressions = new List <IExpression>();

            for (var i = 0; i < parts.Length; i++)
            {
                if (i > 0)
                {
                    expressions.Add(parameters[i - 1]);
                }
                if (!string.IsNullOrEmpty(parts[i]))
                {
                    expressions.Add(new StringLiteral(parts[i]));
                }
            }

            var result = expressions[0];

            for (var i = 1; i < expressions.Count; i++)
            {
                result = new ConcatenationOperator()
                {
                    Left  = result,
                    Right = expressions[i]
                }.Normalize();
            }
            return(result);
        }
Example #3
0
 public virtual void Visit(ConcatenationOperator op)
 {
     AddParenthesesIfNeeded(op, () =>
     {
         op.Left.Visit(this);
         Writer.Write(" + ");
         op.Right.Visit(this);
     });
 }
Example #4
0
            public IExpression Normalize()
            {
                switch (Name.ToLowerInvariant())
                {
                case "concat":
                    if (_ptr < 1)
                    {
                        throw new NotSupportedException();
                    }
                    if (_ptr == 1)
                    {
                        return(_args[0]);
                    }
                    var concat = (IExpression) new ConcatenationOperator()
                    {
                        Left  = _args[0],
                        Right = _args[1]
                    }.Normalize();
                    for (var i = 2; i < _ptr; i++)
                    {
                        concat = new ConcatenationOperator()
                        {
                            Left  = concat,
                            Right = _args[i]
                        }.Normalize();
                    }
                    return(concat);

                case "contains":
                case "endswith":
                case "startswith":
                    if (_ptr == 2)
                    {
                        return(LikeOperator.FromMethod(Name, _args[0], _args[1]));
                    }
                    break;

                case "substringof":
                    if (_ptr == 2)
                    {
                        return(LikeOperator.FromMethod("contains", _args[1], _args[0]));
                    }
                    break;

                case "indexof":
                    if (_ptr == 2)
                    {
                        return(new Functions.IndexOf_Zero()
                        {
                            String = _args[0], Target = _args[1]
                        });
                    }
                    break;

                case "length":
                    if (_ptr == 1)
                    {
                        return new Functions.Length()
                               {
                                   String = _args[0]
                               }
                    }
                    ;
                    break;

                case "substring":
                    if (_ptr == 2)
                    {
                        return(new Functions.Substring_Zero()
                        {
                            String = _args[0],
                            Start = _args[1],
                            Length = new SubtractionOperator()
                            {
                                Left = new Functions.Length()
                                {
                                    String = _args[2]
                                },
                                Right = _args[1]
                            }.Normalize()
                        });
                    }
                    else if (_ptr == 3)
                    {
                        return(new Functions.Substring_Zero()
                        {
                            String = _args[0],
                            Start = _args[1],
                            Length = _args[2]
                        });
                    }
                    break;

                case "tolower":
                    if (_ptr == 1)
                    {
                        return new Functions.ToLower()
                               {
                                   String = _args[0]
                               }
                    }
                    ;
                    break;

                case "toupper":
                    if (_ptr == 1)
                    {
                        return new Functions.ToUpper()
                               {
                                   String = _args[0]
                               }
                    }
                    ;
                    break;

                case "trim":
                    if (_ptr == 1)
                    {
                        return new Functions.Trim()
                               {
                                   String = _args[0]
                               }
                    }
                    ;
                    break;

                case "date":
                    if (_ptr == 1)
                    {
                        return new Functions.TruncateTime()
                               {
                                   Expression = _args[0]
                               }
                    }
                    ;
                    break;

                case "day":
                    if (_ptr == 1)
                    {
                        return new Functions.Day()
                               {
                                   Expression = _args[0]
                               }
                    }
                    ;
                    break;

                case "fractionalseconds":
                    if (_ptr == 1)
                    {
                        return(new DivisionOperator()
                        {
                            Left = new Functions.Millisecond()
                            {
                                Expression = _args[0]
                            },
                            Right = new FloatLiteral(1000)
                        }.Normalize());
                    }
                    break;

                case "hour":
                    if (_ptr == 1)
                    {
                        return new Functions.Hour()
                               {
                                   Expression = _args[0]
                               }
                    }
                    ;
                    break;

                case "minute":
                    if (_ptr == 1)
                    {
                        return new Functions.Minute()
                               {
                                   Expression = _args[0]
                               }
                    }
                    ;
                    break;

                case "month":
                    if (_ptr == 1)
                    {
                        return new Functions.Month()
                               {
                                   Expression = _args[0]
                               }
                    }
                    ;
                    break;

                case "now":
                    if (_ptr == 0)
                    {
                        return(new Functions.CurrentDateTime());
                    }
                    break;

                case "second":
                    if (_ptr == 1)
                    {
                        return new Functions.Second()
                               {
                                   Expression = _args[0]
                               }
                    }
                    ;
                    break;

                case "year":
                    if (_ptr == 1)
                    {
                        return new Functions.Year()
                               {
                                   Expression = _args[0]
                               }
                    }
                    ;
                    break;

                case "ceiling":
                    if (_ptr == 1)
                    {
                        return new Functions.Ceiling()
                               {
                                   Value = _args[0]
                               }
                    }
                    ;
                    break;

                case "floor":
                    if (_ptr == 1)
                    {
                        return new Functions.Floor()
                               {
                                   Value = _args[0]
                               }
                    }
                    ;
                    break;

                case "round":
                    if (_ptr == 1)
                    {
                        return new Functions.Round()
                               {
                                   Value = _args[0], Digits = new IntegerLiteral(0)
                               }
                    }
                    ;
                    break;
                }

                throw new NotSupportedException();
            }
        }
Example #5
0
 public virtual void Visit(ConcatenationOperator op)
 {
     op.Left.Visit(this);
     op.Right.Visit(this);
 }
 void IExpressionVisitor.Visit(ConcatenationOperator op)
 {
     throw new NotSupportedException();
 }
Example #7
0
 void IExpressionVisitor.Visit(ConcatenationOperator op)
 {
     _clone = Clone(op);
 }