Example #1
0
        internal static Filter Convert(ScriptNode Conditions, Variables Variables, string Name)
        {
            if (Conditions is null)
            {
                return(null);
            }

            if (Conditions is BinaryOperator Bin)
            {
                ScriptNode LO = Reduce(Bin.LeftOperand, Name);
                ScriptNode RO = Reduce(Bin.RightOperand, Name);

                Operators.Logical.And And  = Conditions as Operators.Logical.And;
                Operators.Dual.And    And2 = And is null ? Conditions as Operators.Dual.And : null;

                if (And != null || And2 != null)
                {
                    Filter L = Convert(LO, Variables, Name);
                    Filter R = Convert(RO, Variables, Name);

                    if (L is null && R is null)
                    {
                        return(null);
                    }
                    else if (L is null)
                    {
                        return(R);
                    }
                    else if (R is null)
                    {
                        return(L);
                    }
                    else
                    {
                        List <Filter> Filters = new List <Filter>();

                        if (L is FilterAnd L2)
                        {
                            Filters.AddRange(L2.ChildFilters);
                        }
                        else
                        {
                            Filters.Add(L);
                        }

                        if (R is FilterAnd R2)
                        {
                            Filters.AddRange(R2.ChildFilters);
                        }
                        else
                        {
                            Filters.Add(R);
                        }

                        return(new FilterAnd(Filters.ToArray()));
                    }
                }

                Operators.Logical.Or Or  = Conditions as Operators.Logical.Or;
                Operators.Dual.Or    Or2 = Or is null ? Conditions as Operators.Dual.Or : null;

                if (Or != null || Or2 != null)
                {
                    Filter L = Convert(LO, Variables, Name);
                    Filter R = Convert(RO, Variables, Name);

                    if (L is null || R is null)
                    {
                        return(null);
                    }
                    else
                    {
                        List <Filter> Filters = new List <Filter>();

                        if (L is FilterOr L2)
                        {
                            Filters.AddRange(L2.ChildFilters);
                        }
                        else
                        {
                            Filters.Add(L);
                        }

                        if (R is FilterOr R2)
                        {
                            Filters.AddRange(R2.ChildFilters);
                        }
                        else
                        {
                            Filters.Add(R);
                        }

                        return(new FilterOr(Filters.ToArray()));
                    }
                }
Example #2
0
        internal static Filter Convert(ScriptNode Conditions, Variables Variables)
        {
            if (Conditions == null)
            {
                return(null);
            }

            Operators.Logical.And And  = Conditions as Operators.Logical.And;
            Operators.Dual.And    And2 = And == null ? Conditions as Operators.Dual.And : null;

            if (And != null || And2 != null)
            {
                Filter L = Convert(And != null ? And.LeftOperand : And2.LeftOperand, Variables);
                Filter R = Convert(And != null ? And.RightOperand : And2.RightOperand, Variables);

                if (L == null && R == null)
                {
                    return(null);
                }
                else if (L == null)
                {
                    return(R);
                }
                else if (R == null)
                {
                    return(L);
                }
                else
                {
                    List <Filter> Filters = new List <Filter>();

                    if (L is FilterAnd L2)
                    {
                        Filters.AddRange(L2.ChildFilters);
                    }
                    else
                    {
                        Filters.Add(L);
                    }

                    if (R is FilterAnd R2)
                    {
                        Filters.AddRange(R2.ChildFilters);
                    }
                    else
                    {
                        Filters.Add(R);
                    }

                    return(new FilterAnd(Filters.ToArray()));
                }
            }

            Operators.Logical.Or Or  = Conditions as Operators.Logical.Or;
            Operators.Dual.Or    Or2 = Or == null ? Conditions as Operators.Dual.Or : null;

            if (Or != null || Or2 != null)
            {
                Filter L = Convert(Or != null ? Or.LeftOperand : Or2.LeftOperand, Variables);
                Filter R = Convert(Or != null ? Or.RightOperand : Or2.RightOperand, Variables);

                if (L == null || R == null)
                {
                    return(null);
                }
                else
                {
                    List <Filter> Filters = new List <Filter>();

                    if (L is FilterOr L2)
                    {
                        Filters.AddRange(L2.ChildFilters);
                    }
                    else
                    {
                        Filters.Add(L);
                    }

                    if (R is FilterOr R2)
                    {
                        Filters.AddRange(R2.ChildFilters);
                    }
                    else
                    {
                        Filters.Add(R);
                    }

                    return(new FilterOr(Filters.ToArray()));
                }
            }

            if (Conditions is Operators.Logical.Not Not)
            {
                Filter F = Convert(Not.Operand, Variables);
                if (F == null)
                {
                    return(null);
                }
                else
                {
                    return(new FilterNot(F));
                }
            }
            else if (Conditions is BinaryOperator Bin && Bin.LeftOperand is VariableReference Var)
            {
                string FieldName = Var.VariableName;
                object Value     = Bin.RightOperand.Evaluate(Variables)?.AssociatedObjectValue ?? null;

                if (Conditions is Operators.Comparisons.EqualTo ||
                    Conditions is Operators.Comparisons.EqualToElementWise ||
                    Conditions is Operators.Comparisons.IdenticalTo ||
                    Conditions is Operators.Comparisons.IdenticalToElementWise)
                {
                    return(new FilterFieldEqualTo(FieldName, Value));
                }
                else if (Conditions is Operators.Comparisons.NotEqualTo ||
                         Conditions is Operators.Comparisons.NotEqualToElementWise)
                {
                    return(new FilterFieldNotEqualTo(FieldName, Value));
                }
                else if (Conditions is Operators.Comparisons.GreaterThan)
                {
                    return(new FilterFieldGreaterThan(FieldName, Value));
                }
                else if (Conditions is Operators.Comparisons.GreaterThanOrEqualTo)
                {
                    return(new FilterFieldGreaterOrEqualTo(FieldName, Value));
                }
                else if (Conditions is Operators.Comparisons.LesserThan)
                {
                    return(new FilterFieldLesserThan(FieldName, Value));
                }
                else if (Conditions is Operators.Comparisons.LesserThanOrEqualTo)
                {
                    return(new FilterFieldLesserOrEqualTo(FieldName, Value));
                }
                else if (Conditions is Operators.Comparisons.Like Like)
                {
                    string RegEx = WildcardToRegex(Value is string s ? s : Expression.ToString(Value), "%");
                    Like.TransformExpression += (Expression) => WildcardToRegex(Expression, "%");
                    return(new FilterFieldLikeRegEx(FieldName, RegEx));
                }
                else if (Conditions is Operators.Comparisons.NotLike NotLike)
                {
                    string RegEx = WildcardToRegex(Value is string s ? s : Expression.ToString(Value), "%");
                    NotLike.TransformExpression += (Expression) => WildcardToRegex(Expression, "%");
                    return(new FilterNot(new FilterFieldLikeRegEx(FieldName, RegEx)));
                }
            }

            return(null);
        }