Ejemplo n.º 1
0
        protected virtual Expression VisitMethodCall(MethodCallExpression m)
        {
            if (this.ConditionPars == null)
            {
                Expression expression = this.Visit(m.Object);
                IEnumerable <Expression> enumerable = this.VisitExpressionList(m.Arguments);
                if (expression != m.Object || enumerable != m.Arguments)
                {
                    return(Expression.Call(expression, m.Method, enumerable));
                }
            }
            else
            {
                if (m == null)
                {
                    return(m);
                }
                string arg_4F_0 = string.Empty;
                string value    = string.Empty;
                string name;
                if ((name = m.Method.Name) != null)
                {
                    if (_sqlKeywords == null)
                    {
                        _sqlKeywords = new Dictionary <string, int>(12)
                        {
                            {
                                "Contains",
                                0
                            },

                            {
                                "FirstOrDefault",
                                1
                            },

                            {
                                "First",
                                2
                            },

                            {
                                "Select",
                                3
                            },

                            {
                                "Count",
                                4
                            },

                            {
                                "Skip",
                                5
                            },

                            {
                                "Take",
                                6
                            },

                            {
                                "OrderBy",
                                7
                            },

                            {
                                "OrderByDescending",
                                8
                            },

                            {
                                "Where",
                                9
                            },

                            {
                                "WhereAnd",
                                10
                            },

                            {
                                "WhereOr",
                                11
                            }
                        };
                    }
                    int num;
                    if (_sqlKeywords.TryGetValue(name, out num))
                    {
                        switch (num)
                        {
                        case 0:
                        {
                            string name2;
                            if ((name2 = m.Arguments[0].Type.Name) != null && (name2 == "String[]" || name2 == "Int64[]" || name2 == "Int32[]" || name2 == "List`1"))
                            {
                                this.Visit(m.Arguments[1]);
                                this.ConditionPars["where"].Append(" in (");
                                this.Visit(m.Arguments[0]);
                                this.ConditionPars["where"].Append(") ");
                                return(m);
                            }
                            this.Visit(m.Object);
                            this.ConditionPars["where"].Append(" like ");
                            this.Visit(m.Arguments[0]);
                            return(m);
                        }

                        case 1:
                        case 2:
                            foreach (Expression current in m.Arguments)
                            {
                                if (current.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current);
                                }
                            }
                            this.ConditionPars["top"] = new StringBuilder("1");
                            return(m);

                        case 3:
                        {
                            foreach (Expression current2 in m.Arguments)
                            {
                                if (current2.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current2);
                                }
                            }
                            UnaryExpression  unaryExpression  = (UnaryExpression)m.Arguments[1];
                            LambdaExpression lambdaExpression = (LambdaExpression)unaryExpression.Operand;
                            StringBuilder    stringBuilder    = new StringBuilder();
                            if (lambdaExpression.Body.Type.Equals(ExpressionType.New))
                            {
                                NewExpression newExpression = (NewExpression)lambdaExpression.Body;
                                using (IEnumerator <MemberInfo> enumerator3 = newExpression.Members.GetEnumerator())
                                {
                                    while (enumerator3.MoveNext())
                                    {
                                        MemberInfo current3 = enumerator3.Current;
                                        stringBuilder.Append(current3.Name + ",");
                                    }
                                    goto IL_3FF;
                                }
                            }
                            PropertyInfo[] propertys = ConvertHelper.GetPropertys(lambdaExpression.Body.Type);
                            PropertyInfo[] array     = propertys;
                            for (int i = 0; i < array.Length; i++)
                            {
                                PropertyInfo propertyInfo = array[i];
                                stringBuilder.Append(propertyInfo.Name + ",");
                            }
IL_3FF:
                            stringBuilder.Remove(stringBuilder.Length - 1, 1);
                            this.ConditionPars["select"] = stringBuilder;
                            this.ConditionPars["from"].Append(lambdaExpression.Type.GetGenericArguments()[0].Name);
                            return(m);
                        }

                        case 4:
                            foreach (Expression current4 in m.Arguments)
                            {
                                if (current4.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current4);
                                }
                            }
                            this.ConditionPars["select"]  = new StringBuilder("count(*)");
                            this.ConditionPars["orderby"] = new StringBuilder();
                            return(m);

                        case 5:
                            foreach (Expression current5 in m.Arguments)
                            {
                                if (current5.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current5);
                                }
                            }
                            this.ConditionPars["top"].Append(m.Arguments[1].ToString());
                            return(m);

                        case 6:
                            foreach (Expression current6 in m.Arguments)
                            {
                                if (current6.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current6);
                                }
                            }
                            this.ConditionPars["top"].Append(m.Arguments[1].ToString());
                            return(m);

                        case 7:
                            foreach (Expression current7 in m.Arguments)
                            {
                                if (current7.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current7);
                                }
                            }
                            value = this.GetMemName(m) + ",";
                            this.ConditionPars["orderby"].Append(value);
                            return(m);

                        case 8:
                            foreach (Expression current8 in m.Arguments)
                            {
                                if (current8.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current8);
                                }
                            }
                            value = this.GetMemName(m) + " desc ,";
                            this.ConditionPars["orderby"].Append(value);
                            return(m);

                        case 9:
                            using (IEnumerator <Expression> enumerator9 = m.Arguments.GetEnumerator())
                            {
                                while (enumerator9.MoveNext())
                                {
                                    Expression current9 = enumerator9.Current;
                                    if (current9.NodeType.Equals(ExpressionType.Quote) || current9.NodeType.Equals(ExpressionType.Call))
                                    {
                                        this.Visit(current9);
                                    }
                                }
                                return(m);
                            }
                            break;

                        case 10:
                            break;

                        case 11:
                            goto IL_7D8;

                        default:
                            goto IL_867;
                        }
                        using (IEnumerator <Expression> enumerator10 = m.Arguments.GetEnumerator())
                        {
                            while (enumerator10.MoveNext())
                            {
                                Expression current10 = enumerator10.Current;
                                if (current10.NodeType.Equals(ExpressionType.Quote))
                                {
                                    this.Visit(current10);
                                }
                                if (current10.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current10);
                                    this.ConditionPars["where"].Append(" and ");
                                }
                            }
                            return(m);
                        }
IL_7D8:
                        using (IEnumerator <Expression> enumerator11 = m.Arguments.GetEnumerator())
                        {
                            while (enumerator11.MoveNext())
                            {
                                Expression current11 = enumerator11.Current;
                                if (current11.NodeType.Equals(ExpressionType.Quote))
                                {
                                    this.Visit(current11);
                                }
                                if (current11.NodeType.Equals(ExpressionType.Call))
                                {
                                    this.Visit(current11);
                                    this.ConditionPars["where"].Append(" or ");
                                }
                            }
                            return(m);
                        }
                    }
                }
IL_867:
                throw new NotSupportedException(m.NodeType + " is not supported!");
            }
            return(m);
        }
Ejemplo n.º 2
0
        private object ProviderCallBack(Expression expression, Type returnType)
        {
            MethodCallExpression methodCallExpression = (MethodCallExpression)expression;
            ConditionBuilder     conditionBuilder     = new ConditionBuilder();

            conditionBuilder.Build(expression, this.type, true);
            object obj = null;
            string name;

            if ((name = methodCallExpression.Method.Name) != null)
            {
                if (_sqlKeywords == null)
                {
                    _sqlKeywords = new Dictionary <string, int>(8)
                    {
                        {
                            "FirstOrDefault",
                            0
                        },

                        {
                            "First",
                            1
                        },

                        {
                            "Select",
                            2
                        },

                        {
                            "Skip",
                            3
                        },

                        {
                            "Take",
                            4
                        },

                        {
                            "OrderByDescending",
                            5
                        },

                        {
                            "OrderBy",
                            6
                        },

                        {
                            "Count",
                            7
                        }
                    };
                }
                int num;
                if (_sqlKeywords.TryGetValue(name, out num))
                {
                    DataTable dataTable;
                    switch (num)
                    {
                    case 0:
                    case 1:
                    {
                        dataTable = this.DBbase.GetDataTable(conditionBuilder.Condition, conditionBuilder.Arguments.ToArray());
                        object obj2;
                        if (AnonymouseHelper.IsAnonyousType(returnType))
                        {
                            obj2 = ConvertHelper.GetAnonymous(dataTable, returnType);
                        }
                        else
                        {
                            obj2 = ConvertHelper.GetList(dataTable, returnType);
                        }
                        IEnumerable enumerable = (IEnumerable)obj2;
                        IEnumerator enumerator = enumerable.GetEnumerator();
                        try
                        {
                            if (enumerator.MoveNext())
                            {
                                object current = enumerator.Current;
                                obj = current;
                            }
                            return(obj);
                        }
                        finally
                        {
                            IDisposable disposable = enumerator as IDisposable;
                            if (disposable != null)
                            {
                                disposable.Dispose();
                            }
                        }
                        break;
                    }

                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                        break;

                    case 7:
                        obj = this.DBbase.GetSingle(conditionBuilder.Condition, conditionBuilder.Arguments.ToArray());
                        obj = Convert.ToInt32(obj);
                        return(obj);

                    default:
                        return(obj);
                    }
                    Type[] genericArguments = returnType.GetGenericArguments();
                    returnType = ((genericArguments.Length == 0) ? this.type : genericArguments[0]);
                    dataTable  = this.DBbase.GetDataTable(conditionBuilder.Condition, conditionBuilder.Arguments.ToArray());
                    if (AnonymouseHelper.IsAnonyousType(returnType))
                    {
                        obj = ConvertHelper.GetAnonymous(dataTable, returnType);
                    }
                    else
                    {
                        obj = ConvertHelper.GetList(dataTable, returnType);
                    }
                    obj = (IEnumerable)obj;
                }
            }
            return(obj);
        }