예제 #1
0
        public static NewExpression VisitNew(NewExpression nex)
        {
            IEnumerable <Expression> arguments = (IEnumerable <Expression>)NewHelpers.VisitExpressionList(nex.Arguments);

            if (arguments == nex.Arguments)
            {
                return(nex);
            }
            if (nex.Members != null)
            {
                return(Expression.New(nex.Constructor, arguments, (IEnumerable <MemberInfo>)nex.Members));
            }
            return(Expression.New(nex.Constructor, arguments));
        }
예제 #2
0
        private static IQueryItem WriteExpression(Expression e)
        {
            BinaryOperator binaryOperator;

            if (binaryOperators.TryGetValue(e.NodeType, out binaryOperator))
            {
                IQueryItem queryItem1 = WriteExpression((e as BinaryExpression).Left);
                IQueryItem queryItem2 = WriteExpression((e as BinaryExpression).Right);
                if ((object)(queryItem1 as VAL) != null && (queryItem1 as VAL).Value == DBNull.Value)
                {
                    if (binaryOperator == BinaryOperator.Equal)
                    {
                        return (IQueryItem) new BEXP()
                               {
                                   Operand1 = queryItem2, Operator = BinaryOperator.IsNull
                               }
                    }
                    ;
                    if (binaryOperator == BinaryOperator.NotEqual)
                    {
                        return (IQueryItem) new BEXP()
                               {
                                   Operand1 = queryItem2, Operator = BinaryOperator.IsNotNull
                               }
                    }
                    ;
                }
                if ((object)(queryItem2 as VAL) != null && (queryItem2 as VAL).Value == DBNull.Value)
                {
                    if (binaryOperator == BinaryOperator.Equal)
                    {
                        return (IQueryItem) new BEXP()
                               {
                                   Operand1 = queryItem1, Operator = BinaryOperator.IsNull
                               }
                    }
                    ;
                    if (binaryOperator == BinaryOperator.NotEqual)
                    {
                        return (IQueryItem) new BEXP()
                               {
                                   Operand1 = queryItem1, Operator = BinaryOperator.IsNotNull
                               }
                    }
                    ;
                }
                return((IQueryItem) new BEXP()
                {
                    Operand1 = queryItem1, Operand2 = queryItem2, Operator = binaryOperator
                });
            }
            if (e.NodeType == ExpressionType.New)
            {
                object obj = Expression.Lambda(e).Compile().DynamicInvoke();
                return((IQueryItem) new VAL()
                {
                    Value = obj
                });
            }
            if (e.NodeType == ExpressionType.Not)
            {
                IQueryItem queryItem = NewHelpers.WriteExpression((e as UnaryExpression).Operand);
                return((IQueryItem) new BEXP()
                {
                    Operand1 = queryItem, Operator = BinaryOperator.Not
                });
            }
            TransformOperator transformOperator;

            if (NewHelpers.transformOperators.TryGetValue(e.NodeType, out transformOperator))
            {
                IQueryItem queryItem1 = NewHelpers.WriteExpression((e as BinaryExpression).Left);
                IQueryItem queryItem2 = NewHelpers.WriteExpression((e as BinaryExpression).Right);
                return((IQueryItem) new TEXP()
                {
                    Operand1 = queryItem1, Operand2 = queryItem2, Operator = transformOperator
                });
            }
            if (e.NodeType == ExpressionType.Negate)
            {
                IQueryItem queryItem = NewHelpers.WriteExpression((e as BinaryExpression).Left);
                return((IQueryItem) new TEXP()
                {
                    Operand1 = queryItem, Operator = TransformOperator.Negate
                });
            }
            if (e is MemberExpression)
            {
                MemberExpression exp = e as MemberExpression;
                KeyValuePair <NewHelpers.FunctionProperty, QueryFunctions>[] array = NewHelpers.functionProperties.Where <KeyValuePair <NewHelpers.FunctionProperty, QueryFunctions> >((Func <KeyValuePair <NewHelpers.FunctionProperty, QueryFunctions>, bool>)(a =>
                {
                    if (a.Key.FunctionName == exp.Member.Name.ToLower())
                    {
                        return(a.Key.ObjectType.IsAssignableFrom(exp.Expression.Type));
                    }
                    return(false);
                })).ToArray <KeyValuePair <NewHelpers.FunctionProperty, QueryFunctions> >();
                if ((uint)array.Length > 0U)
                {
                    IQueryItem queryItem = NewHelpers.WriteExpression(exp.Expression);
                    return((IQueryItem) new FEXP()
                    {
                        Function = array[0].Value, Parameters = new IQueryValue[1] {
                            (IQueryValue)queryItem
                        }
                    });
                }
                if (exp.Expression is ParameterExpression || exp.Expression is MemberExpression && (exp.Expression as MemberExpression).Expression is ParameterExpression)
                {
                    return((IQueryItem) new COL(exp.Member.Name));
                }
                object obj = Expression.Lambda((Expression)exp).Compile().DynamicInvoke();
                if (obj == null)
                {
                    return (IQueryItem) new VAL()
                           {
                               Value = (object)DBNull.Value
                           }
                }
                ;
                if (!obj.GetType().IsArray)
                {
                    return (IQueryItem) new VAL()
                           {
                               Value = obj
                           }
                }
                ;
                return((IQueryItem) new ARR()
                {
                    Values = (IQueryValue[])((Array)obj).Cast <object>().Select <object, VAL>((Func <object, VAL>)(a => new VAL()
                    {
                        Value = a
                    })).ToArray <VAL>()
                });
            }
            if (e is UnaryExpression)
            {
                return(NewHelpers.WriteExpression((e as UnaryExpression).Operand));
            }
            if (e is ConstantExpression)
            {
                return (IQueryItem) new VAL()
                       {
                           Value = Expression.Lambda(e).Compile().DynamicInvoke()
                       }
            }
            ;
            if (!(e is MethodCallExpression))
            {
                return((IQueryItem)null);
            }
            MethodCallExpression methodCallExpression = (MethodCallExpression)e;
            QueryFunctions       queryFunctions;

            if (NewHelpers.aggregteFunctions.TryGetValue(methodCallExpression.Method.Name.ToLower(CultureInfo.InvariantCulture), out queryFunctions) && methodCallExpression.Object is ParameterExpression)
            {
                List <IQueryItem> source = new List <IQueryItem>();
                foreach (Expression expression in methodCallExpression.Arguments)
                {
                    source.Add(NewHelpers.WriteExpression((expression as LambdaExpression).Body));
                }
                if (methodCallExpression.Method.Name.ToLower() == "count")
                {
                    source.Add((IQueryItem)COL.ALL);
                }
                FEXP fexp = new FEXP();
                fexp.Function = queryFunctions;
                IQueryValue[] array = source.Where <IQueryItem>((Func <IQueryItem, bool>)(a => a is IQueryValue)).Cast <IQueryValue>().ToArray <IQueryValue>();
                fexp.Parameters = array;
                return((IQueryItem)fexp);
            }
            if (methodCallExpression.Method.Name.ToLower(CultureInfo.InvariantCulture) == "asc")
            {
                List <IQueryItem> queryItemList = new List <IQueryItem>();
                foreach (Expression expression in methodCallExpression.Arguments)
                {
                    queryItemList.Add(NewHelpers.WriteExpression((expression as LambdaExpression).Body));
                }
                return((IQueryItem) new ASC()
                {
                    Value = (IQueryValue)queryItemList[0]
                });
            }
            if (methodCallExpression.Method.Name.ToLower(CultureInfo.InvariantCulture) == "desc")
            {
                List <IQueryItem> queryItemList = new List <IQueryItem>();
                foreach (Expression expression in methodCallExpression.Arguments)
                {
                    queryItemList.Add(NewHelpers.WriteExpression((expression as LambdaExpression).Body));
                }
                return((IQueryItem) new DESC()
                {
                    Value = (IQueryValue)queryItemList[0]
                });
            }
            if (NewHelpers.staticFunctions.TryGetValue(methodCallExpression.Method.Name.ToLower(CultureInfo.InvariantCulture), out queryFunctions))
            {
                List <IQueryItem> source = new List <IQueryItem>();
                foreach (Expression e1 in methodCallExpression.Arguments)
                {
                    source.Add(NewHelpers.WriteExpression(e1));
                }
                FEXP fexp = new FEXP();
                fexp.Function = queryFunctions;
                IQueryValue[] array = source.Where <IQueryItem>((Func <IQueryItem, bool>)(a => a is IQueryValue)).Cast <IQueryValue>().ToArray <IQueryValue>();
                fexp.Parameters = array;
                return((IQueryItem)fexp);
            }
            if (NewHelpers.instanceFunctions.TryGetValue(methodCallExpression.Method.Name.ToLower(CultureInfo.InvariantCulture), out queryFunctions))
            {
                List <IQueryItem> source = new List <IQueryItem>();
                source.Add(NewHelpers.WriteExpression(methodCallExpression.Object));
                foreach (Expression e1 in methodCallExpression.Arguments)
                {
                    source.Add(NewHelpers.WriteExpression(e1));
                }
                FEXP fexp = new FEXP();
                fexp.Function = queryFunctions;
                IQueryValue[] array = source.Where <IQueryItem>((Func <IQueryItem, bool>)(a => a is IQueryValue)).Cast <IQueryValue>().ToArray <IQueryValue>();
                fexp.Parameters = array;
                return((IQueryItem)fexp);
            }
            if (methodCallExpression.Method.Name.ToLower(CultureInfo.InvariantCulture) == "in")
            {
                IQueryItem queryItem1 = NewHelpers.WriteExpression(methodCallExpression.Arguments[0]);
                IQueryItem queryItem2 = NewHelpers.WriteExpression(methodCallExpression.Arguments[1]);
                return((IQueryItem) new BEXP()
                {
                    Operand1 = queryItem1, Operand2 = queryItem2, Operator = BinaryOperator.In
                });
            }
            if (((IEnumerable <string>) new string[3] {
                "contains", "startswith", "endswith"
            }).Contains <string>(methodCallExpression.Method.Name.ToLower(CultureInfo.InvariantCulture)))
            {
                string            lower         = methodCallExpression.Method.Name.ToLower(CultureInfo.InvariantCulture);
                List <IQueryItem> queryItemList = new List <IQueryItem>();
                queryItemList.Add(NewHelpers.WriteExpression(methodCallExpression.Object));
                foreach (Expression e1 in methodCallExpression.Arguments)
                {
                    queryItemList.Add(NewHelpers.WriteExpression(e1));
                }
                VAL                val            = (VAL)"%";
                IQueryItem         queryItem1     = queryItemList[1];
                List <IQueryValue> queryValueList = new List <IQueryValue>();
                if (lower == "contains" || lower == "endswith")
                {
                    queryValueList.Add((IQueryValue)val);
                }
                queryValueList.Add((IQueryValue)queryItem1);
                if (lower == "contains" || lower == "startswith")
                {
                    queryValueList.Add((IQueryValue)val);
                }
                FEXP fexp = new FEXP();
                fexp.Function = QueryFunctions.Concat;
                IQueryValue[] array = queryValueList.ToArray();
                fexp.Parameters = array;
                IQueryItem queryItem2 = (IQueryItem)fexp;
                BEXP       bexp       = new BEXP();
                bexp.Operator = BinaryOperator.Like;
                IQueryItem queryItem3 = queryItemList[0];
                bexp.Operand1 = queryItem3;
                IQueryItem queryItem4 = queryItem2;
                bexp.Operand2 = queryItem4;
                return((IQueryItem)bexp);
            }
            object obj1 = Expression.Lambda((Expression)methodCallExpression).Compile().DynamicInvoke();

            if (obj1 == null)
            {
                return (IQueryItem) new VAL()
                       {
                           Value = (object)DBNull.Value
                       }
            }
            ;
            if (!obj1.GetType().IsArray)
            {
                return (IQueryItem) new VAL()
                       {
                           Value = obj1
                       }
            }
            ;
            return((IQueryItem) new ARR()
            {
                Values = (IQueryValue[])((Array)obj1).Cast <object>().Select <object, VAL>((Func <object, VAL>)(a => new VAL()
                {
                    Value = a
                })).ToArray <VAL>()
            });
        }