Ejemplo n.º 1
0
        public static string Enum_GetString(this Enum_Opertion Enum_Model)
        {
            string result = string.Empty;

            switch (Enum_Model)
            {
            case Enum_Opertion.Delete:
                result = "delete";
                break;

            case Enum_Opertion.Insert:
                result = "insert";
                break;

            case Enum_Opertion.Update:
                result = "update";
                break;

            case Enum_Opertion.Where:
                result = "Where";
                break;

            case Enum_Opertion.Set:
                result = "Set";
                break;

            default:
                result = "";
                break;
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 文章点赞
        /// </summary>
        /// <returns></returns>
        public Article ArticleLoveCount(int ArticleId, Enum_Opertion opertion)
        {
            var     ListModel = GetArticleList();
            Article model;

            if (ListModel == null)
            {
                model    = new Article();
                model.id = ArticleId;
                model    = ArticleOper.Instance.Select(model).FirstOrDefault();
            }
            else
            {
                model = ListModel.Where(p => p.id == ArticleId).FirstOrDefault();
            }
            if (model != null)
            {
                if (opertion == Enum_Opertion.Delete)
                {
                    model.loveCount--;
                }
                else if (opertion == Enum_Opertion.Insert)
                {
                    model.loveCount++;
                }
                if (ArticleOper.Instance.Update(model))
                {
                    MemCacheHelper.Instance.writer.Remove("List_Article");
                    GetArticleList();
                    return(model);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 拆分表达式树
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public string BinaryExpressionHandler(Expression left, Expression right, ExpressionType type, SqlOpertionHelper helper, Enum_Opertion opertion, ref List <IDbDataParameter> List_SqlParameter)
        {
            StringBuilder sb = new StringBuilder();

            if (opertion == Enum_Opertion.Where)
            {
                sb.Append("(");
            }
            string needParKey = "=,>,<,>=,<=,<>";
            string leftPar    = RouteExpressionHandler(left, helper, opertion, ref List_SqlParameter);
            string typeStr    = null;

            if (opertion == Enum_Opertion.Where)
            {
                typeStr = ExpressionTypeCast(type);
            }
            else if (opertion == Enum_Opertion.Set)
            {
                typeStr = ExpressionTypeCastSet(type);
            }
            var    isRight  = needParKey.IndexOf(typeStr) > -1;
            string rightPar = RouteExpressionHandler(right, helper, opertion, ref List_SqlParameter, isRight);

            if (!ContainsKey(leftPar) && !ContainsKey(rightPar))
            {
                var Count = List_SqlParameter.Where(p => p.ParameterName.Contains("@" + leftPar)).Count();
                IDbDataParameter parameter = helper.GetParameter();
                parameter.ParameterName = "@" + leftPar + Count;
                if (rightPar.ToLower() == "true")
                {
                    parameter.Value = true;
                }
                else if (rightPar.ToLower() == "false")
                {
                    parameter.Value = false;
                }
                else
                {
                    parameter.Value = rightPar;
                }
                List_SqlParameter.Add(parameter);
                rightPar = "@" + leftPar + Count;
            }
            string appendLeft = leftPar;

            sb.Append(appendLeft);//字段名称
            if (rightPar.ToUpper() == "NULL")
            {
                if (typeStr == "=")
                {
                    rightPar = " IS NULL ";
                }
                else if (typeStr == "<>")
                {
                    rightPar = " IS NOT NULL ";
                }
            }
            else
            {
                sb.Append(typeStr);
            }
            sb.Append(rightPar);
            if (opertion == Enum_Opertion.Where)
            {
                sb.Append(")");
            }
            return(sb.ToString());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 解析表达式
        /// </summary>
        /// <param name="exp"></param>
        /// <param name="isRight"></param>
        /// <returns></returns>
        public string RouteExpressionHandler(Expression exp, SqlOpertionHelper helper, Enum_Opertion opertion, ref List <IDbDataParameter> List_SqlParameter, bool isRight = false)
        {
            if (exp is BinaryExpression)
            {
                BinaryExpression be = (BinaryExpression)exp;
                //重新拆分树,形成递归
                return(BinaryExpressionHandler(be.Left, be.Right, be.NodeType, helper, opertion, ref List_SqlParameter));
            }
            else if (exp is MemberExpression)
            {
                MemberExpression mExp = (MemberExpression)exp;
                if (isRight)//按表达式右边值
                {
                    var obj = Expression.Lambda(mExp).Compile().DynamicInvoke();
                    if (obj is Enum)
                    {
                        obj = (int)obj;
                    }
                    return(obj + "");
                }
                return(mExp.Member.Name);
            }
            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, helper, opertion, ref List_SqlParameter));
                }
                return(sb.Length == 0 ? "" : sb.Remove(0, 1).ToString());

                #endregion
            }
            else if (exp is MethodCallExpression)
            {
                MethodCallExpression cExp = (MethodCallExpression)exp;
                if (isRight)
                {
                    return(Expression.Lambda(exp).Compile().DynamicInvoke() + "");
                }
                #region 数据库Like方法
                if (cExp.Method.Name == "Contains" || cExp.Method.Name.ToLower() == "like")
                {
                    if (cExp.Arguments.Count == 1)
                    {
                        var              ParmName     = cExp.Object.ToString();
                        var              ParmLastName = ParmName.Split('.').Last();
                        string           Search_Value = RouteExpressionHandler(cExp.Arguments[0], helper, opertion, ref List_SqlParameter, true);
                        var              Count        = List_SqlParameter.Where(p => p.ParameterName.Contains("@" + ParmLastName)).Count();
                        IDbDataParameter parameter    = helper.GetParameter();
                        parameter.ParameterName = "@" + ParmLastName + Count;
                        parameter.Value         = "%" + Search_Value + "%";
                        List_SqlParameter.Add(parameter);
                        return(ParmLastName + " like @" + ParmLastName + Count);
                    }
                    else if (cExp.Arguments.Count == 2)
                    {
                        MemberExpression ConstantExp  = cExp.Arguments[0] as MemberExpression;
                        string           ParmLastName = "";
                        if (ConstantExp != null)
                        {
                            ParmLastName = ConstantExp.Member.Name;
                        }
                        else
                        {
                            UnaryExpression  NewNameExp = cExp.Arguments[0] as UnaryExpression;
                            MemberExpression proexp     = NewNameExp.Operand as MemberExpression;
                            ParmLastName = proexp.Member.Name;
                        }
                        string           Search_Value = RouteExpressionHandler(cExp.Arguments[1], helper, opertion, ref List_SqlParameter, true);
                        var              Count        = List_SqlParameter.Where(p => p.ParameterName.Contains("@" + ParmLastName)).Count();
                        IDbDataParameter parameter    = helper.GetParameter();
                        parameter.ParameterName = "@" + ParmLastName + Count;
                        parameter.Value         = "%" + Search_Value + "%";
                        List_SqlParameter.Add(parameter);
                        return(ParmLastName + " like @" + ParmLastName + Count);
                    }
                }
                #endregion

                #region 数据库In函数
                else if (cExp.Method.Name == "ContainsIn" || cExp.Method.Name.ToLower() == "in")
                {
                    if (cExp.Arguments.Count == 1)
                    {
                        var ParmName                 = cExp.Object.ToString();
                        var ParmLastName             = ParmName.Split('.').Last();
                        MemberExpression ConstantExp = cExp.Arguments[0] as MemberExpression;
                        var           obj            = (List <string>)Expression.Lambda(ConstantExp).Compile().DynamicInvoke();
                        StringBuilder builder        = new StringBuilder();
                        if (obj != null && obj.Count > 0)
                        {
                            builder.Append(ParmLastName).Append(" in (");
                            foreach (var item in obj)
                            {
                                var Count = List_SqlParameter.Where(p => p.ParameterName.Contains("@" + ParmLastName)).Count();
                                IDbDataParameter parameter = helper.GetParameter();
                                parameter.ParameterName = "@" + ParmLastName + Count;
                                parameter.Value         = item;
                                List_SqlParameter.Add(parameter);
                                builder.Append("@").Append(ParmLastName).Append(Count).Append(",");
                            }
                            builder.Remove(builder.Length - 1, 1).Append(") ");
                        }
                        else
                        {
                            builder.Append(ParmLastName).Append(" in (-1)");
                        }

                        return(builder.ToString());
                    }
                    else if (cExp.Arguments.Count == 2)
                    {
                        MemberExpression NameExp      = cExp.Arguments[0] as MemberExpression;
                        string           ParmLastName = "";
                        if (NameExp != null)
                        {
                            ParmLastName = NameExp.Member.Name;
                        }
                        else
                        {
                            UnaryExpression  NewNameExp = cExp.Arguments[0] as UnaryExpression;
                            MemberExpression proexp     = NewNameExp.Operand as MemberExpression;
                            ParmLastName = proexp.Member.Name;
                        }
                        MemberExpression ConstantExp = cExp.Arguments[1] as MemberExpression;
                        var           obj            = (List <string>)Expression.Lambda(ConstantExp).Compile().DynamicInvoke();
                        StringBuilder builder        = new StringBuilder();
                        if (obj != null && obj.Count > 0)
                        {
                            builder.Append(ParmLastName).Append(" in (");
                            foreach (var item in obj)
                            {
                                var Count = List_SqlParameter.Where(p => p.ParameterName.Contains("@" + ParmLastName)).Count();
                                IDbDataParameter parameter = helper.GetParameter();
                                parameter.ParameterName = "@" + ParmLastName + Count;
                                parameter.Value         = item;
                                List_SqlParameter.Add(parameter);
                                builder.Append("@").Append(ParmLastName).Append(Count).Append(",");
                            }
                            builder.Remove(builder.Length - 1, 1).Append(") ");
                        }
                        else
                        {
                            builder.Append(ParmLastName).Append(" in (-1)");
                        }
                        return(builder.ToString());
                    }
                }
                #endregion

                #region 数据库NotIn函数
                else if (cExp.Method.Name == "ContainsNotIn" || cExp.Method.Name.ToLower() == "notin")
                {
                    if (cExp.Arguments.Count == 1)
                    {
                        var ParmName                 = cExp.Object.ToString();
                        var ParmLastName             = ParmName.Split('.').Last();
                        MemberExpression ConstantExp = cExp.Arguments[0] as MemberExpression;
                        var           obj            = (List <string>)Expression.Lambda(ConstantExp).Compile().DynamicInvoke();
                        StringBuilder builder        = new StringBuilder();
                        if (obj != null && obj.Count > 0)
                        {
                            builder.Append(ParmLastName).Append(" not in (");
                            foreach (var item in obj)
                            {
                                var Count = List_SqlParameter.Where(p => p.ParameterName.Contains("@" + ParmLastName)).Count();
                                IDbDataParameter parameter = helper.GetParameter();
                                parameter.ParameterName = "@" + ParmLastName + Count;
                                parameter.Value         = item;
                                List_SqlParameter.Add(parameter);
                                builder.Append("@").Append(ParmLastName).Append(Count).Append(",");
                            }
                            builder.Remove(builder.Length - 1, 1).Append(") ");
                        }
                        else
                        {
                            builder.Append(ParmLastName).Append(" not in (-1)");
                        }
                        return(builder.ToString());
                    }
                    else if (cExp.Arguments.Count == 2)
                    {
                        MemberExpression NameExp      = cExp.Arguments[0] as MemberExpression;
                        string           ParmLastName = "";
                        if (NameExp != null)
                        {
                            ParmLastName = NameExp.Member.Name;
                        }
                        else
                        {
                            UnaryExpression  NewNameExp = cExp.Arguments[0] as UnaryExpression;
                            MemberExpression proexp     = NewNameExp.Operand as MemberExpression;
                            ParmLastName = proexp.Member.Name;
                        }
                        MemberExpression ConstantExp = cExp.Arguments[1] as MemberExpression;
                        var           obj            = (List <object>)Expression.Lambda(ConstantExp).Compile().DynamicInvoke();
                        StringBuilder builder        = new StringBuilder();
                        if (obj != null && obj.Count > 0)
                        {
                            builder.Append(ParmLastName).Append(" not in (");
                            foreach (var item in obj)
                            {
                                var Count = List_SqlParameter.Where(p => p.ParameterName.Contains("@" + ParmLastName)).Count();
                                IDbDataParameter parameter = helper.GetParameter();
                                parameter.ParameterName = "@" + ParmLastName + Count;
                                parameter.Value         = item;
                                List_SqlParameter.Add(parameter);
                                builder.Append("@").Append(ParmLastName).Append(Count).Append(",");
                            }
                            builder.Remove(builder.Length - 1, 1).Append(") ");
                        }
                        else
                        {
                            builder.Append(ParmLastName).Append(" not in (-1)");
                        }
                        return(builder.ToString());
                    }
                }
                #endregion
                //在这里解析方法
                throw new Exception("暂不支持");
            }
            else if (exp is ConstantExpression)
            {
                #region 常量
                ConstantExpression cExp = (ConstantExpression)exp;
                if (cExp.Value == null)
                {
                    return("null");
                }
                else
                {
                    SqlParameter parameter = new SqlParameter("cExp", cExp.Value);
                    return(cExp.Value.ToString());
                }
                #endregion
            }
            else if (exp is UnaryExpression)
            {
                UnaryExpression ue = ((UnaryExpression)exp);
                return(RouteExpressionHandler(ue.Operand, helper, opertion, ref List_SqlParameter, isRight));
            }
            return(null);
        }