Beispiel #1
0
        protected override Expression VisitNew(NewExpression expression)
        {
            var     expressionArguments   = expression.Arguments;
            int     expressionMemberCount = expression.Members.Count;
            dynamic expressionArgument    = null;

            TranslatorMapperItem mapperItem = null;
            string fieldName = null;

            for (int index = 0; index < expressionMemberCount; index++)
            {
                expressionArgument = expressionArguments[index];

                mapperItem = this.typeDict[expressionArgument.Expression.Name];
                fieldName  = TQueryReflectionHelper.GetFieldName(mapperItem.TableType, expressionArgument.Member.Name);
                if (mapperItem != null && fieldName != null)
                {
                    this.stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.TABLE_FIELD, mapperItem.TableName, fieldName));
                    if (index < expressionMemberCount - 1)
                    {
                        this.stringBuilder.Append(",");
                    }
                }
            }

            return(expression);
        }
        protected override Expression VisitMethodCall(MethodCallExpression expression)
        {
            dynamic expressionArguments = expression.Arguments;

            if (expression.Method.Name == "Contains" || expression.Method.Name == "StartsWith" || expression.Method.Name == "EndsWith")
            {
                dynamic expressionData = expression.Object;
                if (expressionData != null && expressionData.Expression != null && expressionData.Member != null && expressionArguments.Count > 0)
                {
                    if (expressionData.NodeType != ExpressionType.MemberAccess)
                    {
                        TranslatorMapperItem mapperItem = this.typeDict[expressionData.Expression.Name];
                        string fieldName = TQueryReflectionHelper.GetFieldName(mapperItem.TableType, expressionData.Member.Name);
                        this.stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.TABLE_FIELD, mapperItem.TableName, fieldName));

                        string expressionParam = null;
                        if (expressionArguments[0].NodeType == ExpressionType.MemberAccess)
                        {
                            expressionParam = Expression.Lambda <Func <string> >(Expression.Convert(expressionArguments[0], typeof(string))).Compile().Invoke();
                        }
                        else
                        {
                            expressionParam = expressionArguments[0].Value.ToString().Trim();
                        }
                        if (expressionParam.IndexOf("@") >= 0)
                        {
                            this.stringBuilder.Append(" = ");
                            this.stringBuilder.Append(expressionParam);
                            this.stringBuilder.Append(" ");
                        }
                        else
                        {
                            this.stringBuilder.Append(" like '");
                            if (expression.Method.Name != "StartsWith")
                            {
                                this.stringBuilder.Append("%");
                            }
                            this.stringBuilder.Append(expressionParam);
                            if (expression.Method.Name != "EndsWith")
                            {
                                this.stringBuilder.Append("%");
                            }
                            this.stringBuilder.Append("' ");
                        }
                    }
                    else
                    {
                        if (expression.Method.Name == "Contains")
                        {
                            this.stringBuilder.Append(this.VisitMethodCallExpression(expression));
                        }
                    }
                }
            }
            return(expression);
        }
 protected override Expression VisitMember(MemberExpression expression)
 {
     if (expression.Expression != null && expression.Expression.NodeType == ExpressionType.Parameter)
     {
         dynamic expressionMember        = expression.Expression;
         TranslatorMapperItem mapperItem = this.typeDict[expressionMember.Name];
         string fieldName = TQueryReflectionHelper.GetFieldName(mapperItem.TableType, expression.Member.Name);
         this.stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.TABLE_FIELD_BORDERBY, mapperItem.TableName, fieldName, this.orderBy));
     }
     return(expression);
 }
        protected override Expression VisitMember(MemberExpression expression)
        {
            if (expression.Expression == null && expression.Type == typeof(DateTime))
            {
                if (expression.Member is PropertyInfo)
                {
                    var value = (expression.Member as PropertyInfo).GetValue(expression);
                    this.ConstantProcess(value, expression.Type);
                }
            }
            else
            {
                if (expression.Expression != null)
                {
                    dynamic expressionMember = expression.Expression;
                    if (expression.Expression.NodeType == ExpressionType.Parameter)
                    {
                        TranslatorMapperItem mapperItem = this.typeDict[expressionMember.Name];
                        string fieldName = TQueryReflectionHelper.GetFieldName(mapperItem.TableType, expression.Member.Name);

                        if (!this.stringBuilder.ToString().EndsWith("@"))
                        {
                            this.stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.TABLE_FIELD, mapperItem.TableName, fieldName));
                        }
                        else
                        {
                            this.stringBuilder.Append(expression.Member.Name);
                        }
                    }
                    else if (expression.Expression.NodeType == ExpressionType.Constant)
                    {
                        if (expression.Member is FieldInfo)
                        {
                            var value = (expression.Member as FieldInfo).GetValue(expressionMember.Value);
                            this.ConstantProcess(value, expression.Type);
                        }
                        if (expression.Member is PropertyInfo)
                        {
                            var value = (expression.Member as PropertyInfo).GetValue(expressionMember.Value);
                            this.ConstantProcess(value, expression.Type);
                        }
                    }
                }
            }
            return(expression);
        }
        protected override Expression VisitNew(NewExpression expression)
        {
            var     expressionArguments   = expression.Arguments;
            int     expressionMemberCount = expression.Members.Count;
            dynamic expressionArgument    = null;

            string fieldName  = null;
            string memberName = null;
            TranslatorMapperItem mapperItem = null;

            if (this.mapperDict == null)
            {
                this.mapperDict = new Dictionary <string, string>();
            }

            for (int index = 0; index < expressionMemberCount; index++)
            {
                expressionArgument = expressionArguments[index];

                mapperItem = this.typeDict[expressionArgument.Expression.Name];
                memberName = expressionArgument.Member.Name;
                fieldName  = TQueryReflectionHelper.GetFieldName(mapperItem.TableType, memberName);

                if (mapperItem.TableName != null && !string.IsNullOrEmpty(memberName) && !string.IsNullOrEmpty(fieldName))
                {
                    if (memberName != expression.Members[index].Name)
                    {
                        this.mapperDict.Add(fieldName, expression.Members[index].Name);
                    }
                    else
                    {
                        this.mapperDict.Add(fieldName, memberName);
                    }
                }
            }

            return(expression);
        }
        private string VisitMethodCallExpression(MethodCallExpression func)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (func.Method.Name.Contains("Contains"))
            {
                //获得调用者的内容元素
                var originalFunc = Expression.Lambda <Func <object> >(func.Object).Compile();
                var dataList     = originalFunc() as IEnumerable;
                //获得字段
                dynamic funcType = func.Arguments[0];

                TranslatorMapperItem mapperItem = this.typeDict[funcType.Expression.Name];
                string fieldName = TQueryReflectionHelper.GetFieldName(mapperItem.TableType, funcType.Member.Name);

                stringBuilder.Append(" ");
                stringBuilder.Append(fieldName);
                stringBuilder.Append(" in (");

                StringBuilder dataStringBuilder = new StringBuilder();
                foreach (var data in dataList)
                {
                    dataStringBuilder.Append(string.Format("'{0}',", data.ToString()));
                }

                string inCondition = dataStringBuilder.ToString().TrimEnd(new char[] { ',' });
                if (funcType.Type == typeof(int))
                {
                    inCondition = inCondition.Replace("'", "");
                }
                stringBuilder.Append(inCondition);

                stringBuilder.Append(") ");
            }
            return(stringBuilder.ToString());
        }