Example #1
0
        /// <summary>
        /// 获取实体的列特性
        /// </summary>
        /// <returns></returns>
        public static List <EntityPropColumnAttributes> GetEntityColumnAtrributes <TEntity>() where TEntity : new()
        {
            List <EntityPropColumnAttributes> list = new List <EntityPropColumnAttributes>();
            TEntity model = new TEntity();

            foreach (PropertyInfo prop in model.GetType().GetProperties())
            {
                EntityPropColumnAttributes entity = new EntityPropColumnAttributes();
                entity.propName      = prop.Name;
                entity.fieldName     = prop.Name;
                entity.isPrimaryKey  = false;
                entity.isDbGenerated = false;
                Type type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                entity.typeName = type.FullName;
                object[] CustomAttributesArr = prop.GetCustomAttributes(typeof(ColumnAttribute), false);
                if (CustomAttributesArr.Count() > 0)
                {
                    foreach (var obj in CustomAttributesArr)
                    {
                        ColumnAttribute attr = obj as ColumnAttribute;
                        entity.fieldName     = attr.Name == null ? prop.Name : attr.Name;
                        entity.isPrimaryKey  = attr.IsPrimaryKey;
                        entity.isDbGenerated = attr.IsDbGenerated;
                    }
                }
                list.Add(entity);
            }
            return(list);
        }
Example #2
0
        /// <summary>
        /// 二元运算符表达式
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        string BinarExpressionProvider(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right, System.Linq.Expressions.ExpressionType type)
        {
            string where = "(";
            //先处理左边
            string leftStr = ExpressionRouter(left);
            //获取实体列的特性
            List <EntityPropColumnAttributes> columnAttrList = AttributeHelper.GetEntityColumnAtrributes <TEntity>();
            var list = columnAttrList.Where(w => w.propName == leftStr);

            if (list.Count() > 0)
            {
                EntityPropColumnAttributes columnAttribute = list.First();
                leftStr = columnAttribute.fieldName;
            }
            //节点类型
            string typeStr = ExpressionTypeCast(type);

            //再处理右边
            string rightStr = RightExpressionRouter(right).ToStr();

            where += leftStr;
            where += typeStr;

            if (rightStr == "null")
            {
                if (where.EndsWith(" ="))
                {
                    where = where.Substring(0, where.Length - 2) + " is null";
                }
                else if (where.EndsWith("<>"))
                {
                    where = where.Substring(0, where.Length - 2) + " is not null";
                }
            }
            else
            {
                //如果左侧包含(则代表左侧非字段
                if (leftStr.Contains("("))
                {
                    where += rightStr;
                }
                else
                {
                    int num = random.Next(100, 999);
                    where += "@" + leftStr + num;
                    listPara.Add(new SqlParameter("@" + leftStr + num, rightStr));
                }
            }
            return(where += ")");
        }
Example #3
0
        string MethodExpression(System.Linq.Expressions.Expression exp)
        {
            System.Linq.Expressions.MethodCallExpression mce = (System.Linq.Expressions.MethodCallExpression)exp;

            string key = ExpressionRouter(mce.Arguments[0]);
            object obj = RightExpressionRouter(mce.Arguments[1]);

            //对象属性
            EntityPropColumnAttributes columnAttribute = columnAttrList.Where(w => w.propName == key).First();

            key = columnAttribute.fieldName;

            //参数名称
            string paramName = "@" + key + random.Next(1000, 9999);

            if (mce.Method.Name == "Like" || mce.Method.Name == "NotLike")
            {
                listPara.Add(new SqlParameter(paramName, "%" + obj.ToStr() + "%"));
            }
            string values = "";

            #region 拼接参数值
            if (mce.Method.Name == "In" || mce.Method.Name == "NotIn")
            {
                string        json = JsonHelper.Serialize(obj);
                List <object> list = JsonHelper.Deserialize <List <object> >(json);

                if (list.Count == 0)
                {
                    return(" (1=2) ");
                }

                int index = 0;
                foreach (var value in list)
                {
                    index++;
                    listPara.Add(new SqlParameter(paramName + index, value.ToStr()));
                    values += paramName + index + ",";
                }
            }
            values = values.TrimEnd(',');
            #endregion

            if (mce.Method.Name == "Like")
            {
                return(string.Format("({0} like {1})", key, paramName));
            }
            else if (mce.Method.Name == "NotLike")
            {
                return(string.Format("({0} Not like {1})", key, paramName));
            }
            else if (mce.Method.Name == "In")
            {
                return(string.Format("{0} In ({1})", key, values));
            }
            else if (mce.Method.Name == "NotIn")
            {
                return(string.Format("{0} Not In ({1})", key, values));
            }

            return(" (1=2) ");
        }