Exemplo n.º 1
0
        public CRLExpression.CRLExpression DealParame(CRLExpression.CRLExpression par1, string typeStr, out string typeStr2)
        {
            var par = par1.Data + "";

            typeStr2 = typeStr;
            //todo 非关系型数据库不参数化
            if (dbContext.DataBaseArchitecture == DataBaseArchitecture.NotRelation)
            {
                return(par1);
            }
            if (par1.Data is CRLExpression.MethodCallObj)
            {
                var method     = par1.Data as CRLExpression.MethodCallObj;
                var _DBAdapter = DBAdapter.DBAdapterBase.GetDBAdapterBase(dbContext);
                var dic        = MethodAnalyze.GetMethos(_DBAdapter);
                if (!dic.ContainsKey(method.MethodName))
                {
                    throw new CRLException("LambdaQuery不支持方法" + method.MethodName);
                }
                int newParIndex = parIndex;
                par      = dic[method.MethodName](method, ref newParIndex, AddParame);
                parIndex = newParIndex;
            }

            //字段会返回替换符
            bool needPar = par1.Type == CRLExpression.CRLExpressionType.Value;//是否需要参数化处理

            if (!needPar)
            {
                par1.Data = par;
                return(par1);
            }
            if (needPar)
            {
                if (par.ToUpper() == "NULL")
                {
                    typeStr2 = "";
                    if (typeStr == "=")
                    {
                        par = " IS NULL ";
                    }
                    else if (typeStr == "<>")
                    {
                        par = " IS NOT NULL ";
                    }
                }
                else
                {
                    QueryParames.Add("par" + parIndex, par);
                    par       = "@par" + parIndex;
                    parIndex += 1;
                }
            }
            par1.Data = par;
            return(par1);
        }
Exemplo n.º 2
0
        CRLExpression.CRLExpression DealParame(CRLExpression.CRLExpression par1, string typeStr)
        {
            var par = par1.Data + "";

            //typeStr2 = typeStr;
            //todo 非关系型数据库不参数化
            if (dbContext.DataBaseArchitecture == DataBaseArchitecture.NotRelation)
            {
                return(par1);
            }
            if (parIndex > 1000)
            {
                throw new CRLException("参数计数超过了1000,请确认数据访问对象没有被静态化");
            }
            switch (par1.Type)
            {
            case CRLExpression.CRLExpressionType.Value:
                #region value
                if (par1.Data == null)
                {
                    par = __DBAdapter.IsNotFormat(typeStr != "=") + "null";
                }
                else
                {
                    QueryParames.Add("par" + parIndex, par);
                    par       = "@par" + parIndex;
                    parIndex += 1;
                }
                #endregion
                break;

            case CRLExpression.CRLExpressionType.MethodCall:
                #region method
                var method = par1.Data as CRLExpression.MethodCallObj;
                var dic    = MethodAnalyze.GetMethos(__DBAdapter);
                if (!dic.ContainsKey(method.MethodName))
                {
                    throw new CRLException("LambdaQuery不支持扩展方法" + method.MemberQueryName + "." + method.MethodName);
                }
                int newParIndex = parIndex;
                par      = dic[method.MethodName](method, ref newParIndex, AddParame);
                parIndex = newParIndex;
                #endregion
                break;
            }
            par1.DataParamed = par;
            return(par1);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 返回方法调用拼接
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="memberName"></param>
        /// <param name="argsIndex"></param>
        /// <param name="remArgsIndex"></param>
        /// <returns></returns>
        string getSelectMethodCall(Expression expression, out string memberName, int argsIndex, bool remArgsIndex = true)
        {
            var cRLExpression = __Visitor.RouteExpressionHandler(expression);

            memberName = "";
            if (cRLExpression.Type == CRLExpression.CRLExpressionType.Value)
            {
                //值类型返回参数值
                var parName = __DBAdapter.GetParamName("cons", __DbContext.parIndex);
                __DbContext.parIndex += 1;
                __Visitor.AddParame(parName, cRLExpression.Data);
                //if (remArgsIndex)
                //{
                //    newExpressionParame.Add(parName, argsIndex);
                //}
                return(parName);
            }
            var methodCallObj = cRLExpression.Data as CRLExpression.MethodCallObj;

            memberName = methodCallObj.MethodName;

            var dic = MethodAnalyze.GetMethos(__DBAdapter);

            if (!dic.ContainsKey(methodCallObj.MethodName))
            {
                throw new CRLException("LambdaQuery不支持扩展方法" + methodCallObj.MemberQueryName + "." + methodCallObj.MethodName);
            }
            int newParIndex = __DbContext.parIndex;

            var par = dic[methodCallObj.MethodName](methodCallObj, ref newParIndex, __Visitor.AddParame);

            __DbContext.parIndex = newParIndex;
            //if (remArgsIndex)
            //{
            //    newExpressionParame[par] = argsIndex;
            //}
            return(par);
            //return string.Format("{0}({1})", methodName, methodField);
        }
Exemplo n.º 4
0
        CRLExpression.CRLExpression DealParame(CRLExpression.CRLExpression par1, string typeStr)
        {
            var par = par1.Data + "";

            //typeStr2 = typeStr;
            //todo 非关系型数据库不参数化
            if (dbContext.DataBaseArchitecture == DataBaseArchitecture.NotRelation)
            {
                return(par1);
            }
            var a = parameDicList.TryGetValue(__DBAdapter.DBType, out string[] parameDic);

            if (!a)
            {
                parameDic = new string[5000];
                for (int i = 0; i < 5000; i++)
                {
                    parameDic[i] = __DBAdapter.GetParamName("p", i);
                }
                parameDicList.TryAdd(__DBAdapter.DBType, parameDic);
            }
            if (parIndex >= 5000)
            {
                //MSSQL 参数最多2800
                throw new Exception("参数计数超过了5000,请确认数据访问对象没有被静态化" + parIndex);
            }
            switch (par1.Type)
            {
            case CRLExpression.CRLExpressionType.Value:
                #region value
                if (par1.Data == null)
                {
                    par = __DBAdapter.IsNotFormat(typeStr != "=") + "null";
                }
                else
                {
                    if (SettingConfig.FieldParameName)
                    {
                        par1.DataParamed = par;
                        return(par1);   //参数名按字段名
                    }
                    var _par = parameDic[parIndex];
                    AddParame(_par, par);
                    par       = _par;
                    parIndex += 1;
                }
                #endregion
                break;

            case CRLExpression.CRLExpressionType.MethodCall:
                #region method
                var method = par1.Data as CRLExpression.MethodCallObj;

                #region in优化 MSSQL内部已自动优化
                var nodeType = method.ExpressionType;

                /**
                 * if (dbContext.DataBaseArchitecture == DataBaseArchitecture.Relation && method.MethodName == "In" && lambdaQueryBase.__AutoInJoin > 5 && nodeType == ExpressionType.Equal)
                 * {
                 *  var inArgs = method.Args[0] as System.Collections.IEnumerable;
                 *  int _i = 0;
                 *  Type innerType = null;
                 *  var pars = new List<InJoin>();
                 *  var batchNo = System.Guid.NewGuid().ToString().Replace("-", "");
                 *  foreach (var a in inArgs)
                 *  {
                 *      _i += 1;
                 *      if (innerType == null)
                 *      {
                 *          innerType = a.GetType();
                 *      }
                 *      var obj2 = new InJoin() { BatchNo = batchNo, V_String = "" };
                 *      obj2.SetValue(innerType, a);
                 *      pars.Add(obj2);
                 *  }
                 *  if (_i > lambdaQueryBase.__AutoInJoin)
                 *  {
                 *      QueryParames.Add("__batchNO", batchNo);
                 *      var typeJoin = typeof(InJoin);
                 *      var inJoinName = lambdaQueryBase.GetPrefix(typeJoin);
                 *      var joinFormat = string.Format("{1}BatchNo=@__batchNO and {0}={1}V_{2}", method.MemberQueryName, inJoinName, innerType.Name);
                 *      lambdaQueryBase.AddInnerRelation(new TypeQuery(typeJoin), JoinType.Inner, joinFormat);
                 *      par1.DataParamed = "";
                 *      var dbEx = DBExtendFactory.CreateDBExtend(dbContext);
                 *      dbEx.CheckTableCreated(typeJoin);
                 *      __DBAdapter.BatchInsert(pars);
                 *      lambdaQueryBase.__RemoveInJionBatchNo = batchNo;
                 *      return par1;
                 *  }
                 * }
                 */
                #endregion

                var dic = MethodAnalyze.GetMethos(__DBAdapter);
                if (!dic.ContainsKey(method.MethodName))
                {
                    throw new Exception("LambdaQuery不支持扩展方法" + method.MemberQueryName + "." + method.MethodName);
                }
                int newParIndex = parIndex;
                par      = dic[method.MethodName](method, ref newParIndex, AddParame);
                parIndex = newParIndex;
                #endregion
                break;
            }
            par1.DataParamed = par;
            return(par1);
        }
Exemplo n.º 5
0
        public string RouteExpressionHandler(Expression exp, ExpressionType nodeType = ExpressionType.Equal)
        {
            if (exp is BinaryExpression)
            {
                BinaryExpression be = (BinaryExpression)exp;
                return(BinaryExpressionHandler(be.Left, be.Right, be.NodeType));
            }
            else if (exp is MemberExpression)
            {
                //区分 属性表达带替换符{0} 变量值不带
                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;
                    var filed     = TypeCache.GetProperties(type, true)[fieldName];
                    if (filed == null)
                    {
                        throw new Exception("类型 " + type.Name + "." + fieldName + " 不是数据库字段,请检查查询条件");
                    }
                    if (!string.IsNullOrEmpty(filed.VirtualField))//按虚拟字段
                    {
                        //如果没有使用$前辍
                        if (!filed.VirtualField.Contains("$"))
                        {
                            filed.VirtualField = "{VirtualField}" + filed.VirtualField;
                        }
                        return(filed.VirtualField);
                    }
                    var field = "{" + type.FullName + "}" + fieldName; //格式化为别名
                    if (nodeType == ExpressionType.Not)                //like b=!b.IsNew
                    {
                        field += ExpressionTypeCast(nodeType) + 1;
                    }
                    return(field);
                }
                var obj = LambdaCompileCache.GetExpressionCacheValue(mExp);
                if (obj is Enum)
                {
                    obj = (int)obj;
                }
                return(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));
                }
                return(sb.Length == 0 ? "" : sb.Remove(0, 1).ToString());

                #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 = LambdaCompileCache.GetExpressionCacheValue(exp);
                        return(obj + "");
                    }
                }
                else if (mcExp.Object is ConstantExpression)
                {
                    //var cExp = mcExp.Object as ConstantExpression;
                    //like b.BarCode == aa()
                    var obj = LambdaCompileCache.GetExpressionCacheValue(exp);
                    return(obj + "");
                }
                var _DBAdapter = DBAdapter.DBAdapterBase.GetDBAdapterBase(dbContext);
                //var methodAnalyze = new CRL.LambdaQuery.MethodAnalyze(_DBAdapter);
                var dic = MethodAnalyze.GetMethos(_DBAdapter);
                #region 方法
                //请扩展ExtensionMethod的方法
                string methodName = mcExp.Method.Name;
                parIndex += 1;
                if (!dic.ContainsKey(methodName))
                {
                    //return Expression.Lambda(exp).Compile().DynamicInvoke() + "";
                    throw new Exception("LambdaQuery不支持方法" + mcExp.Method.Name);
                }
                string field = "";
                #region par
                List <object> args = new List <object>();
                if (mcExp.Object == null)
                {
                    field = RouteExpressionHandler(mcExp.Arguments[0]);
                }
                else
                {
                    field = mcExp.Object.ToString().Split('.')[1];
                    var mExpression = mcExp.Object as MemberExpression;
                    var type        = mExpression.Expression.Type;
                    field = "{" + type.FullName + "}" + field;
                    var obj = LambdaCompileCache.GetExpressionCacheValue(mcExp.Arguments[0]);
                    args.Add(obj);
                    //args.Add(Expression.Lambda(mcExp.Arguments[0]).Compile().DynamicInvoke());
                }

                if (mcExp.Arguments.Count > 1)
                {
                    var obj = LambdaCompileCache.GetExpressionCacheValue(mcExp.Arguments[1]);
                    args.Add(obj);
                    //args.Add(Expression.Lambda(mcExp.Arguments[1]).Compile().DynamicInvoke());
                }
                if (mcExp.Arguments.Count > 2)
                {
                    var obj = LambdaCompileCache.GetExpressionCacheValue(mcExp.Arguments[2]);
                    args.Add(obj);
                    //args.Add(Expression.Lambda(mcExp.Arguments[2]).Compile().DynamicInvoke());
                }
                if (mcExp.Arguments.Count > 3)
                {
                    var obj = LambdaCompileCache.GetExpressionCacheValue(mcExp.Arguments[3]);
                    args.Add(obj);
                    //args.Add(Expression.Lambda(mcExp.Arguments[3]).Compile().DynamicInvoke());
                }
                #endregion
                int newParIndex = parIndex;
                var result      = dic[methodName](field, nodeType, ref newParIndex, AddParame, args.ToArray());
                parIndex = newParIndex;
                return(result);

                #endregion
            }
            else if (exp is ConstantExpression)
            {
                #region 常量
                ConstantExpression cExp = (ConstantExpression)exp;
                if (cExp.Value == null)
                {
                    return("null");
                }
                else
                {
                    if (cExp.Value is Boolean)
                    {
                        return(Convert.ToInt32(cExp.Value).ToString());
                    }
                    else if (cExp.Value is Enum)
                    {
                        return(Convert.ToInt32(cExp.Value).ToString());
                    }
                    return(cExp.Value.ToString());
                }
                #endregion
            }
            else if (exp is UnaryExpression)
            {
                UnaryExpression ue = ((UnaryExpression)exp);
                return(RouteExpressionHandler(ue.Operand, ue.NodeType));
            }
            else
            {
                throw new Exception("不支持此语法解析:" + exp);
            }
        }