Ejemplo n.º 1
0
        public string StringContains(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var    field    = methodInfo.MemberQueryName;
            var    nodeType = methodInfo.ExpressionType;
            var    args     = methodInfo.Args;
            string parName  = string.Format("@contrains{0}", parIndex);
            var    args1    = args[0];

            if (args1 is ExpressionValueObj)
            {
                parName = args1.ToString();
            }
            else
            {
                addParame(parName, args1);
            }
            parIndex += 1;
            if (nodeType == ExpressionType.Equal)
            {
                return(dBAdapter.StringContainsFormat(field, parName));
            }
            else
            {
                return(dBAdapter.StringNotContainsFormat(field, parName));
            }
            //return string.Format("CHARINDEX({1},{0})>0", field, parName);
        }
Ejemplo n.º 2
0
        public string Replace(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var    field   = methodInfo.MemberQueryName;
            var    args    = methodInfo.Args;
            string parName = string.Format("@repf{0}", parIndex);
            var    args1   = args[0];

            if (args1 is ExpressionValueObj)
            {
                parName = args1.ToString();
            }
            else
            {
                addParame(parName, args1);
            }
            var args2 = args[1];

            parIndex += 1;
            string parName2 = string.Format("@repd{0}", parIndex);

            addParame(parName2, args2);
            parIndex += 1;
            //DateDiff(2015/2/5 17:59:44,t1.AddTime,@DateDiff1)>1
            return(dBAdapter.Replace(field, parName, parName2));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 字符串包含
        /// </summary>
        /// <param name="methodInfo"></param>
        /// <param name="parIndex"></param>
        /// <param name="addParame"></param>
        /// <returns></returns>
        public string Contains(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var field     = methodInfo.MemberQueryName;
            var nodeType  = methodInfo.ExpressionType;
            var args      = methodInfo.Args;
            var firstArgs = args.First();

            if (firstArgs is IEnumerable && firstArgs.GetType() != typeof(string))//如果是集合,按in
            {
                return(In(methodInfo, ref parIndex, addParame));
            }
            string parName = GetParamName("contains", parIndex);
            var    args1   = args[0];

            if (args1 is ExpressionValueObj)
            {
                parName = args1.ToString();
            }
            else
            {
                addParame(parName, args1);
            }
            parIndex += 1;
            if (nodeType == ExpressionType.Equal)
            {
                return(dBAdapter.StringContainsFormat(field, parName));
            }
            else
            {
                return(dBAdapter.StringNotContainsFormat(field, parName));
            }
            //return string.Format("CHARINDEX({1},{0})>0", field, parName);
        }
Ejemplo n.º 4
0
        public string Equals(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var    field    = methodInfo.MemberQueryName;
            var    nodeType = methodInfo.ExpressionType;
            var    args     = methodInfo.Args;
            string parName  = string.Format("@equalEnum{0}", parIndex);

            parIndex += 1;
            var args1 = args[0];

            if (args1 is ExpressionValueObj)
            {
                parName = args1.ToString();
            }
            else
            {
                args1 = ObjectConvert.ConvertObject(args1.GetType(), args1);
                addParame(parName, args1);
            }
            var operate = ExpressionVisitor.ExpressionTypeCast(nodeType);

            return(string.Format("{0}{2}{1}", field, parName, operate));
            //if (nodeType == ExpressionType.Equal)
            //{
            //    return string.Format("{0}={1}", field, parName);
            //}
            //else
            //{
            //    return string.Format("{0}!={1}", field, parName);
            //}
        }
Ejemplo n.º 5
0
        string StringLikeFull(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame, string likeFormat)
        {
            var    field    = methodInfo.MemberQueryName;
            var    nodeType = methodInfo.ExpressionType;
            var    args     = methodInfo.Args[0];
            string parName  = string.Format("@like{0}", parIndex);

            parIndex += 1;

            if (args is ExpressionValueObj)
            {
                parName = args.ToString();
                //if (!parName.ToString().Contains("%"))
                //{
                //    parName = string.Format(likeFormat, parName);
                //}
            }
            else
            {
                if (!args.ToString().Contains("%"))
                {
                    args = string.Format(likeFormat, args);
                }
                addParame(parName, args);
            }
            if (nodeType == ExpressionType.Equal)
            {
                return(dBAdapter.StringLikeFormat(field, parName));
            }
            else
            {
                return(dBAdapter.StringNotLikeFormat(field, parName));
            }
            //return string.Format("{0} LIKE {1}", field, parName);
        }
Ejemplo n.º 6
0
        public string StartsWith(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var    field    = methodInfo.MemberQueryName;
            var    nodeType = methodInfo.ExpressionType;
            var    args     = methodInfo.Args;
            var    par      = args[0].ToString();
            string parName  = string.Format("@StartsWith{0}", parIndex);

            parIndex += 1;
            var args1 = args[0];

            if (args1 is ExpressionValueObj)
            {
                parName = args1.ToString();
            }
            else
            {
                addParame(parName, par);
            }
            var str = dBAdapter.SubstringFormat(field, 0, par.Length);

            var operate = ExpressionVisitor.ExpressionTypeCast(nodeType);

            return(string.Format("{0}{2}{1}", str, parName, operate));
            //if (nodeType == ExpressionType.Equal)
            //{
            //    return string.Format("{0}={1}", str, parName);
            //}
            //else
            //{
            //    return string.Format("{0}!={1}", str, parName);
            //}
        }
Ejemplo n.º 7
0
        public string StringLike(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var field    = methodInfo.MemberQueryName;
            var nodeType = methodInfo.ExpressionType;
            var args     = methodInfo.Args;

            return(StringLikeFull(methodInfo, ref parIndex, addParame, "%{0}%"));
        }
Ejemplo n.º 8
0
        public string IsNullOrEmpty(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var field  = methodInfo.MemberQueryName;
            var isNot  = methodInfo.ExpressionType == ExpressionType.Not;
            var notStr = dBAdapter.IsNotFormat(isNot);
            var result = string.Format("({0} {1} null {3} {0}{2}'')", field, notStr, isNot ? "!=" : "=", isNot ? "and" : "or");

            return(result);
        }
Ejemplo n.º 9
0
        public string IsNull(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var    field   = methodInfo.MemberQueryName;
            var    args    = methodInfo.Args.First();
            string parName = string.Format("@isnull{0}", parIndex);

            parIndex += 1;
            addParame(parName, args);
            return(dBAdapter.IsNull(field, parName));
        }
Ejemplo n.º 10
0
        public string Substring(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var field    = methodInfo.MemberQueryName;
            var nodeType = methodInfo.ExpressionType;
            var args     = methodInfo.Args;

            if (args.Count < 2)
            {
                throw new CRLException("Substring扩展方法需要两个参数,index,length");
            }
            return(dBAdapter.SubstringFormat(field, (int)args[0], (int)args[1]));
            //return string.Format(" SUBSTRING({0},{1},{2})", field, args[0], args[1]);
        }
Ejemplo n.º 11
0
        public string CaseToType(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var field = methodInfo.MemberQueryName;

            if (string.IsNullOrEmpty(field))//按转换常量算
            {
                string parName = string.Format("@case{0}", parIndex);
                parIndex += 1;
                addParame(parName, methodInfo.Args.First());
                field = parName;
            }
            return(dBAdapter.CastField(field, methodInfo.ReturnType));
        }
Ejemplo n.º 12
0
        public string In(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var    field    = methodInfo.MemberQueryName;
            var    nodeType = methodInfo.ExpressionType;
            var    args     = methodInfo.Args;
            string str      = InFormat(args[0], ref parIndex, addParame);

            if (nodeType == ExpressionType.Equal)
            {
                return(dBAdapter.InFormat(field, str));
            }
            else
            {
                return(dBAdapter.NotInFormat(field, str));
            }
            //return string.Format("{0} IN ({1})", field, str);
        }
Ejemplo n.º 13
0
        public string DateTimeDateDiff(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var    field    = methodInfo.MemberQueryName;
            var    nodeType = methodInfo.ExpressionType;
            var    args     = methodInfo.Args;
            string parName  = string.Format("@DateDiff{0}", parIndex);
            var    args1    = args[0];

            if (args1 is ExpressionValueObj)
            {
                parName = args1.ToString();
            }
            else
            {
                addParame(parName, args[1]);
            }
            parIndex += 1;
            //DateDiff(2015/2/5 17:59:44,t1.AddTime,@DateDiff1)>1
            return(dBAdapter.DateDiffFormat(field, args[0].ToString(), parName));
            //return string.Format("DateDiff({0},{1},{2}){3}", args[0], field, parName, args[2]);
        }
Ejemplo n.º 14
0
        public string Between(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var    field    = methodInfo.MemberQueryName;
            var    nodeType = methodInfo.ExpressionType;
            var    args     = methodInfo.Args;
            string parName  = string.Format("@between{0}", parIndex);
            var    args1    = args[0];
            var    args2    = args[1];

            if (args1 is ExpressionValueObj)
            {
                parName = args1.ToString();
            }
            else
            {
                addParame(parName, args1);
            }
            parIndex += 1;
            string parName2 = string.Format("@between{0}", parIndex);

            if (args2 is ExpressionValueObj)
            {
                parName2 = args2.ToString();
            }
            else
            {
                addParame(parName2, args2);
            }
            parIndex += 1;
            if (nodeType == ExpressionType.Equal)
            {
                return(dBAdapter.BetweenFormat(field, parName, parName2));
            }
            else
            {
                return(dBAdapter.NotBetweenFormat(field, parName, parName2));
            }
            //return string.Format("{0} between {1} and {2}", field, parName, parName2);
        }
Ejemplo n.º 15
0
        string StringLikeFull(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame, string likeFormat)
        {
            var    field    = methodInfo.MemberQueryName;
            var    nodeType = methodInfo.ExpressionType;
            var    args     = methodInfo.Args[0];
            string parName  = GetParamName("like", parIndex);

            parIndex += 1;

            if (args is ExpressionValueObj)//like 字段名 '%'+t1.[ProductName1]
            {
                parName = args.ToString();

                likeFormat = likeFormat.Replace("%", "+'%'+");
                parName    = string.Format(likeFormat, parName);
                parName    = System.Text.RegularExpressions.Regex.Replace(parName, @"^\+", "");
                parName    = System.Text.RegularExpressions.Regex.Replace(parName, @"\+$", "");
            }
            else
            {
                if (!args.ToString().Contains("%"))
                {
                    args = string.Format(likeFormat, args);
                }
                addParame(parName, args);
            }
            if (nodeType == ExpressionType.Equal)
            {
                return(dBAdapter.StringLikeFormat(field, parName));
            }
            else
            {
                return(dBAdapter.StringNotLikeFormat(field, parName));
            }
            //return string.Format("{0} LIKE {1}", field, parName);
        }
Ejemplo n.º 16
0
 public string StringLikeRight(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
 {
     //string str = args[0].ToString();
     //str = str.Replace("%","");
     return(StringLikeFull(methodInfo, ref parIndex, addParame, "{0}%"));
 }
Ejemplo n.º 17
0
        public string AVG(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var field = methodInfo.MemberQueryName;

            return(string.Format("avg({0})", field));
        }
Ejemplo n.º 18
0
 public string StartsWith(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
 {
     return(StringLikeRight(methodInfo, ref parIndex, addParame));
 }
Ejemplo n.º 19
0
        CRLExpression.CRLExpression MethodCallExpressionHandler(Expression exp, ExpressionType?nodeType = null, bool firstLevel = false)
        {
            #region methodCall
            MethodCallExpression mcExp = (MethodCallExpression)exp;
            var        arguments       = new List <object>();
            var        allArguments    = new List <Expression>(mcExp.Arguments);
            int        argsIndex       = 0;
            Expression firstArgs;
            bool       isLambdaQueryJoinExt = false;
            if (mcExp.Object == null)        //区分静态方法还是实例方法
            {
                firstArgs = allArguments[0]; //like b.Name.IsNull("22")
                argsIndex = 1;
                if (firstArgs.Type.Name.Contains("LambdaQueryJoin"))
                {
                    isLambdaQueryJoinExt = true;
                }
            }
            else
            {
                firstArgs = mcExp.Object;                                            //like b.Id.ToString()
                if (allArguments.Count > 0 && (allArguments[0] is MemberExpression)) //like ids.Contains(b.Id)
                {
                    var mexp2      = allArguments[0] as MemberExpression;
                    var firstArgsM = firstArgs as MemberExpression;
                    //var par2 = (ParameterExpression)mexp2.Expression;
                    if (mexp2.Expression is ParameterExpression && firstArgsM.Expression is ConstantExpression)
                    {
                        firstArgs = allArguments[0];
                        argsIndex = 1;
                        allArguments.Add(mcExp.Object);
                        if (firstArgs.Type.Name.Contains("LambdaQueryJoin"))
                        {
                            isLambdaQueryJoinExt = true;
                        }
                    }
                }
            }
            #region MethodCallExpression
            //bool isConstantMethod = false;

            MemberExpression memberExpression;

            string methodField = "";
            string memberName  = "";
            string methodName  = mcExp.Method.Name;

            if (firstArgs is ParameterExpression || isLambdaQueryJoinExt)
            {
                var exp2 = mcExp.Arguments[1] as UnaryExpression;
                var type = exp2.Operand.GetType();
                var p    = type.GetProperty("Body");
                var exp3 = p.GetValue(exp2.Operand, null) as Expression;
                methodField = RouteExpressionHandler(exp3).SqlOut;
                memberName  = "";
            }
            else if (firstArgs is UnaryExpression)//like a.Code.Count()
            {
                memberExpression = (firstArgs as UnaryExpression).Operand as MemberExpression;
                memberName       = memberExpression.Member.Name;
                var field = TypeCache.GetProperties(memberExpression.Expression.Type, true)[memberName];
                memberName  = __DBAdapter.FieldNameFormat(field);
                methodField = FormatFieldPrefix(memberExpression.Expression.Type, memberName);
            }
            else if (firstArgs is BinaryExpression)
            {
                var be = firstArgs as BinaryExpression;
                methodField = BinaryExpressionHandler(be.Left, be.Right, be.NodeType).Data.ToString();
            }
            else if (firstArgs is MemberExpression)
            {
                //like a.Code
                memberExpression = firstArgs as MemberExpression;
                memberName       = memberExpression.Member.Name;
                var type = memberExpression.Expression.Type;
                if (type.IsSubclassOf(typeof(IModel)))
                {
                    var field = TypeCache.GetProperties(type, true)[memberExpression.Member.Name];
                    memberName = __DBAdapter.FieldNameFormat(field);
                }
                if (memberExpression.Expression.NodeType == ExpressionType.Parameter)
                {
                    methodField = FormatFieldPrefix(memberExpression.Expression.Type, memberName);
                    //var allConstant = true;
                    for (int i = argsIndex; i < allArguments.Count; i++)
                    {
                        bool isConstant2;
                        var  obj = GetParameExpressionValue(allArguments[i], out isConstant2);
                        arguments.Add(obj);
                    }
                }
                else
                {
                    //like Convert.ToDateTime(times)
                    var obj = ConstantValueVisitor.GetParameExpressionValue(firstArgs);
                    arguments.Add(obj);
                    for (int i = argsIndex; i < allArguments.Count; i++)
                    {
                        bool isConstant2;
                        var  obj2 = GetParameExpressionValue(allArguments[i], out isConstant2);
                        arguments.Add(obj2);
                    }
                }
            }
            else if (firstArgs is ConstantExpression)//按常量
            {
                //like DateTime.Parse("2016-02-11 12:56"),
                //isConstantMethod = true;
                var obj = ConstantValueVisitor.GetParameExpressionValue(firstArgs);
                arguments.Add(obj);
            }
            //else
            //{
            //    throw new CRLException("不支持此语法解析:" + args);
            //}

            if (nodeType == null)
            {
                nodeType = ExpressionType.Equal;
            }
            if (string.IsNullOrEmpty(methodField))
            {
                //当是常量转换方法

                var    method = mcExp.Method;
                object obj    = null;
                if (method.IsStatic)            //like DateTime.Parse("2016-02-11")
                {
                    if (method.IsGenericMethod) //扩展方法,like public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value)
                    {
                        var valueObj = (ExpressionValueObj)arguments[1];
                        if (valueObj == null)
                        {
                            throw new Exception("不支持此语法:" + mcExp);
                        }
                        memberName = valueObj.member.Name;
                        arguments  = new List <object>()
                        {
                            arguments[0]
                        };
                        methodField = valueObj.Value.ToString();
                        goto lable1;
                    }
                    else
                    {
                        obj = method.Invoke(null, arguments.ToArray());
                    }
                }
                else//like time.AddDays(1)
                {
                    if (arguments.Count == 0)
                    {
                        throw new Exception("未能解析" + exp);
                    }
                    var args1 = arguments.First();
                    arguments.RemoveAt(0);
                    if (arguments.Count > 0)
                    {
                        if (arguments[0] is ExpressionValueObj)
                        {
                            throw new Exception("不支持这样的语法:" + exp);
                        }
                    }
                    obj = method.Invoke(args1, arguments.ToArray());
                }
                var exp2 = new CRLExpression.CRLExpression()
                {
                    Type = CRLExpression.CRLExpressionType.Value, Data = obj
                };

                return(exp2);
            }

lable1:
            var methodInfo = new CRLExpression.MethodCallObj()
            {
                Args = arguments, ExpressionType = nodeType.Value, MemberName = memberName, MethodName = methodName, MemberQueryName = methodField
            };
            methodInfo.ReturnType = mcExp.Type;

            #endregion
            var exp4 = new CRLExpression.CRLExpression()
            {
                Type = CRLExpression.CRLExpressionType.MethodCall, Data = methodInfo
            };

            return(exp4);

            #endregion
        }
Ejemplo n.º 20
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="exp"></param>
        /// <param name="nodeType"></param>
        /// <param name="firstLevel">是否首次调用,来用修正bool一元运算</param>
        /// <returns></returns>
        public CRLExpression.CRLExpression RouteExpressionHandler(Expression exp, ExpressionType?nodeType = null, bool firstLevel = false)
        {
            if (exp is BinaryExpression)
            {
                BinaryExpression be = (BinaryExpression)exp;
                return(BinaryExpressionHandler(be.Left, be.Right, be.NodeType));
            }
            else if (exp is MemberExpression)
            {
                //区分 属性表达带替换符{0} 变量值不带
                #region Member
                MemberExpression mExp = (MemberExpression)exp;
                if (mExp.Expression != null && mExp.Expression.NodeType == ExpressionType.Parameter) //like b.Name==b.Name1 或b.Name
                {
                    var fieldName = mExp.Member.Name;
                    var type      = mExp.Expression.Type;
                    if (type.BaseType == typeof(object))//按匿名类
                    {
                        var queryField = FormatFieldPrefix(type, fieldName);
                        return(new CRLExpression.CRLExpression()
                        {
                            Type = CRLExpression.CRLExpressionType.Name, Data = queryField
                        });
                    }

                    CRL.Attribute.FieldAttribute field;
                    var a = TypeCache.GetProperties(type, true).TryGetValue(fieldName, out field);
                    if (!a)
                    {
                        throw new CRLException("类型 " + type.Name + "." + fieldName + " 不是数据库字段,请检查查询条件");
                    }
                    if (!string.IsNullOrEmpty(field.VirtualField))//按虚拟字段
                    {
                        //return filed.VirtualField;
                        var queryField = field.VirtualField.Replace("{" + type.FullName + "}", Prefixs[type]);//替换前辍
                        return(new CRLExpression.CRLExpression()
                        {
                            Type = CRLExpression.CRLExpressionType.Name, Data = queryField
                        });
                    }
                    var fieldStr = FormatFieldPrefix(type, field.MapingName);//格式化为别名
                    //return field;
                    if (firstLevel)
                    {
                        //修正bool值一元运算 t1.isTop=1
                        fieldStr += "=1";
                    }
                    return(new CRLExpression.CRLExpression()
                    {
                        Type = CRLExpression.CRLExpressionType.Name, Data = fieldStr
                    });
                }
                #endregion
                var obj = GetParameExpressionValue(mExp);
                if (obj is Enum)
                {
                    obj = (int)obj;
                }
                else if (obj is Boolean)//sql2000需要转换
                {
                    obj = Convert.ToInt32(obj);
                }
                //return obj + "";
                return(new CRLExpression.CRLExpression()
                {
                    Type = CRLExpression.CRLExpressionType.Value, Data = obj
                });
            }
            else if (exp is NewArrayExpression)
            {
                #region 数组
                NewArrayExpression naExp = (NewArrayExpression)exp;
                StringBuilder      sb    = new StringBuilder();
                foreach (Expression expression in naExp.Expressions)
                {
                    sb.AppendFormat(",{0}", RouteExpressionHandler(expression));
                }
                var str = sb.Length == 0 ? "" : sb.Remove(0, 1).ToString();
                return(new CRLExpression.CRLExpression()
                {
                    Type = CRLExpression.CRLExpressionType.Value, Data = str
                });

                #endregion
            }
            else if (exp is MethodCallExpression)
            {
                MethodCallExpression mcExp = (MethodCallExpression)exp;

                if (mcExp.Object is MemberExpression)
                {
                    var mExp = mcExp.Object as MemberExpression;
                    if (mExp.Expression.NodeType != ExpressionType.Parameter)
                    {
                        //not like b.BarCode.Contains("abc")
                        //按变量或常量编译值
                        var obj = GetParameExpressionValue(exp);
                        //return obj + "";
                        return(new CRLExpression.CRLExpression()
                        {
                            Type = CRLExpression.CRLExpressionType.Value, Data = obj
                        });
                    }
                }
                else if (mcExp.Object is ConstantExpression)
                {
                    //var cExp = mcExp.Object as ConstantExpression;
                    //like b.BarCode == aa()
                    var obj = GetParameExpressionValue(exp);
                    //return obj + "";
                    return(new CRLExpression.CRLExpression()
                    {
                        Type = CRLExpression.CRLExpressionType.Value, Data = obj
                    });
                }
                //var _DBAdapter = DBAdapter.DBAdapterBase.GetDBAdapterBase(dbContext);
                //var methodAnalyze = new CRL.LambdaQuery.MethodAnalyze(_DBAdapter);

                #region 方法
                //请扩展ExtensionMethod的方法
                string methodName = mcExp.Method.Name;
                parIndex += 1;
                //var dic = MethodAnalyze.GetMethos(_DBAdapter);
                //if (!dic.ContainsKey(methodName))
                //{
                //    //return Expression.Lambda(exp).Compile().DynamicInvoke() + "";
                //    throw new CRLException("LambdaQuery不支持方法" + mcExp.Method.Name);
                //}
                string field = "";
                #region par
                List <object> args = new List <object>();
                if (mcExp.Object == null)
                {
                    field = RouteExpressionHandler(mcExp.Arguments[0]).Data.ToString();
                }
                else
                {
                    field = mcExp.Object.ToString().Split('.')[1];

                    var mExpression = mcExp.Object as MemberExpression;
                    var type        = mExpression.Expression.Type;
                    var filed2      = TypeCache.GetProperties(type, true)[field];
                    field = FormatFieldPrefix(type, filed2.MapingName);
                    if (mcExp.Arguments.Count > 0)
                    {
                        var obj = GetParameExpressionValue(mcExp.Arguments[0]);
                        args.Add(obj);
                    }
                }
                if (mcExp.Arguments.Count > 1)
                {
                    for (int i = 1; i < mcExp.Arguments.Count; i++)
                    {
                        var obj = GetParameExpressionValue(mcExp.Arguments[i]);
                        args.Add(obj);
                    }
                }
                #endregion
                //int newParIndex = parIndex;
                if (nodeType == null)
                {
                    nodeType = ExpressionType.Equal;
                }
                //var result = dic[methodName](field, nodeType.Value, ref newParIndex, AddParame, args.ToArray());
                //parIndex = newParIndex;
                //return result;
                var methodInfo = new CRLExpression.MethodCallObj()
                {
                    Args = args, ExpressionType = nodeType.Value, MemberName = field.Substring(field.LastIndexOf("}") + 1), MethodName = methodName, MemberQueryName = field
                };
                methodInfo.ReturnType = mcExp.Type;
                return(new CRLExpression.CRLExpression()
                {
                    Type = CRLExpression.CRLExpressionType.MethodCall, Data = methodInfo
                });

                #endregion
            }
            else if (exp is ConstantExpression)
            {
                #region 常量
                ConstantExpression cExp = (ConstantExpression)exp;
                object             returnValue;
                if (cExp.Value == null)
                {
                    returnValue = "null";
                }
                else
                {
                    if (cExp.Value is Boolean)
                    {
                        returnValue = Convert.ToInt32(cExp.Value).ToString();
                    }
                    else if (cExp.Value is Enum)
                    {
                        returnValue = Convert.ToInt32(cExp.Value).ToString();
                    }
                    else
                    {
                        returnValue = cExp.Value;
                    }
                }
                return(new CRLExpression.CRLExpression()
                {
                    Type = CRLExpression.CRLExpressionType.Value, Data = returnValue
                });

                #endregion
            }
            else if (exp is UnaryExpression)
            {
                UnaryExpression ue = ((UnaryExpression)exp);
                if (ue.Operand is MethodCallExpression)
                {
                    //方法直接下一步解析
                    return(RouteExpressionHandler(ue.Operand, ue.NodeType));
                }
                else if (ue.Operand is MemberExpression)
                {
                    MemberExpression mExp = (MemberExpression)ue.Operand;
                    if (mExp.Expression.NodeType != ExpressionType.Parameter)
                    {
                        return(RouteExpressionHandler(ue.Operand));
                    }
                    var parameter = Expression.Parameter(mExp.Expression.Type, "b");
                    if (ue.NodeType == ExpressionType.Not)
                    {
                        var ex2 = parameter.Property(mExp.Member.Name).Equal(1);
                        return(RouteExpressionHandler(ex2));
                    }
                    else if (ue.NodeType == ExpressionType.Convert)
                    {
                        //like Convert(b.Id);
                        var ex2 = parameter.Property(mExp.Member.Name);
                        return(RouteExpressionHandler(ex2));
                    }
                }
                else if (ue.Operand is ConstantExpression)
                {
                    return(RouteExpressionHandler(ue.Operand));
                }
                throw new CRLException("未处理的一元运算" + ue.NodeType);
            }
            else
            {
                throw new CRLException("不支持此语法解析:" + exp);
            }
        }
Ejemplo n.º 21
0
        CRLExpression.CRLExpression MethodCallExpressionHandler(Expression exp, ExpressionType?nodeType = null, bool firstLevel = false)
        {
            #region methodCall
            MethodCallExpression mcExp = (MethodCallExpression)exp;
            var        arguments       = new List <object>();
            var        allArguments    = mcExp.Arguments;
            int        argsIndex       = 0;
            Expression firstArgs;
            bool       isLambdaQueryJoinExt = false;
            if (mcExp.Method.IsStatic)       //区分静态方法还是实例方法
            {
                firstArgs = allArguments[0]; //like b.Name.IsNull("22")
                argsIndex = 1;
                if (firstArgs.Type.Name.Contains("LambdaQueryJoin"))
                {
                    isLambdaQueryJoinExt = true;
                }
            }
            else
            {
                firstArgs = mcExp.Object;//like b.Id.ToString()
            }

            #region 缓存处理 效率并不高
            //MethodCallExpressionCacheItem methodCache;
            //string key = "";
            //if (SettingConfig.UseLambdaCache&& false)
            //{
            //    key = __PrefixsAllKey + mcExp + nodeType;
            //    var exists = MethodCallExpressionCache.TryGetValue(key, out methodCache);
            //    if (exists)
            //    {
            //        if (!methodCache.isConstantMethod)
            //        {
            //            var methodCall = methodCache.CRLExpression.Data as CRLExpression.MethodCallObj;
            //            if (methodCall.Args.Count > 0)
            //            {
            //                for (int i = argsIndex; i < allArguments.Count; i++)
            //                {
            //                    bool isConstant1;
            //                    var obj = GetParameExpressionValue(allArguments[i], out isConstant1);
            //                    arguments.Add(obj);
            //                }
            //                methodCall.Args = arguments;
            //            }
            //        }
            //        methodCache.CRLExpression.SqlOut = "";
            //        return methodCache.CRLExpression;
            //    }
            //}
            #endregion
            #region MethodCallExpression
            //bool isConstantMethod = false;

            MemberExpression memberExpression;

            string methodField = "";
            string memberName  = "";
            string methodName  = mcExp.Method.Name;

            if (firstArgs is ParameterExpression || isLambdaQueryJoinExt)
            {
                var exp2 = mcExp.Arguments[1] as UnaryExpression;
                var type = exp2.Operand.GetType();
                var p    = type.GetProperty("Body");
                var exp3 = p.GetValue(exp2.Operand, null) as Expression;
                methodField = RouteExpressionHandler(exp3).SqlOut;
                memberName  = "";
            }
            else if (firstArgs is UnaryExpression)//like a.Code.Count()
            {
                memberExpression = (firstArgs as UnaryExpression).Operand as MemberExpression;
                memberName       = memberExpression.Member.Name;
                var field = TypeCache.GetProperties(memberExpression.Expression.Type, true)[memberName];
                memberName  = __DBAdapter.FieldNameFormat(field);
                methodField = FormatFieldPrefix(memberExpression.Expression.Type, memberName);
            }
            else if (firstArgs is BinaryExpression)
            {
                var be = firstArgs as BinaryExpression;
                methodField = BinaryExpressionHandler(be.Left, be.Right, be.NodeType).Data.ToString();
            }
            else if (firstArgs is MemberExpression)
            {
                //like a.Code
                memberExpression = firstArgs as MemberExpression;
                memberName       = memberExpression.Member.Name;
                var type = memberExpression.Expression.Type;
                if (type.IsSubclassOf(typeof(IModel)))
                {
                    var field = TypeCache.GetProperties(type, true)[memberExpression.Member.Name];
                    memberName = __DBAdapter.FieldNameFormat(field);
                }
                if (memberExpression.Expression.NodeType == ExpressionType.Parameter)
                {
                    methodField = FormatFieldPrefix(memberExpression.Expression.Type, memberName);
                    //var allConstant = true;
                    for (int i = argsIndex; i < allArguments.Count; i++)
                    {
                        bool isConstant2;
                        var  obj = GetParameExpressionValue(allArguments[i], out isConstant2);
                        arguments.Add(obj);
                    }
                }
                else
                {
                    //like Convert.ToDateTime(times)
                    var obj = ConstantValueVisitor.GetParameExpressionValue(firstArgs);
                    arguments.Add(obj);
                    for (int i = argsIndex; i < allArguments.Count; i++)
                    {
                        bool isConstant2;
                        var  obj2 = GetParameExpressionValue(allArguments[i], out isConstant2);
                        arguments.Add(obj2);
                    }
                }
            }
            else if (firstArgs is ConstantExpression)//按常量
            {
                //like DateTime.Parse("2016-02-11 12:56"),
                //isConstantMethod = true;
                var obj = ConstantValueVisitor.GetParameExpressionValue(firstArgs);
                arguments.Add(obj);
            }
            //else
            //{
            //    throw new CRLException("不支持此语法解析:" + args);
            //}

            if (nodeType == null)
            {
                nodeType = ExpressionType.Equal;
            }
            if (string.IsNullOrEmpty(methodField))
            {
                //当是常量转换方法

                var    method = mcExp.Method;
                object obj;
                if (mcExp.Method.IsStatic)//like DateTime.Parse("2016-02-11")
                {
                    obj = method.Invoke(null, arguments.ToArray());
                }
                else//like time.AddDays(1)
                {
                    if (arguments.Count == 0)
                    {
                        throw new Exception("未能解析" + exp);
                    }
                    var args1 = arguments.First();
                    arguments.RemoveAt(0);
                    obj = method.Invoke(args1, arguments.ToArray());
                }
                var exp2 = new CRLExpression.CRLExpression()
                {
                    Type = CRLExpression.CRLExpressionType.Value, Data = obj
                };
                //var cache = new MethodCallExpressionCacheItem() { CRLExpression = exp2, argsIndex = argsIndex, isConstantMethod = isConstantMethod, isStatic = mcExp.Method.IsStatic };
                //MethodCallExpressionCache[key] = cache;
                return(exp2);
            }
            var methodInfo = new CRLExpression.MethodCallObj()
            {
                Args = arguments, ExpressionType = nodeType.Value, MemberName = memberName, MethodName = methodName, MemberQueryName = methodField
            };
            methodInfo.ReturnType = mcExp.Type;
            #endregion
            var exp4 = new CRLExpression.CRLExpression()
            {
                Type = CRLExpression.CRLExpressionType.MethodCall, Data = methodInfo
            };
            //if (SettingConfig.UseLambdaCache && false)
            //{
            //    var cache2 = new MethodCallExpressionCacheItem() { CRLExpression = exp4, argsIndex = argsIndex, isConstantMethod = isConstantMethod };
            //    MethodCallExpressionCache[key] = cache2;
            //}
            return(exp4);

            #endregion
        }
Ejemplo n.º 22
0
        public string ToUpper(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var field = methodInfo.MemberQueryName;

            return(dBAdapter.ToUpperFormat(field));
        }
Ejemplo n.º 23
0
        public string CaseToType(CRLExpression.MethodCallObj methodInfo, ref int parIndex, AddParameHandler addParame)
        {
            var field = methodInfo.MemberQueryName;

            return(dBAdapter.CastField(field, methodInfo.ReturnType));
        }