Exemplo n.º 1
0
        public override TType GetFunction <TType, TModel>(Expression <Func <TModel, bool> > expression, Expression <Func <TModel, TType> > selectField, FunctionType functionType, bool compileSp = false)
        {
            var query     = new MongoDBLambdaQuery <TModel>(dbContext);
            var m         = selectField.Body as MemberExpression;
            var fieldName = m?.Member?.Name;
            //query.Select(selectField.Body);
            //query.Where(expression);
            var    collection = _MongoDB.GetCollection <TModel>(query.QueryTableName);
            object result     = null;


            switch (functionType)
            {
            case FunctionType.COUNT:
                result = collection.Find(expression).CountDocuments();
                break;

            case FunctionType.SUM:
                result = getAggregateResult(collection, expression, "sum", fieldName);
                break;

            case FunctionType.MAX:
                result = getAggregateResult(collection, expression, "max", fieldName);
                break;

            case FunctionType.MIN:
                result = getAggregateResult(collection, expression, "min", fieldName);
                break;

            default:
                throw new NotSupportedException("MongoDB不支持的函数:" + functionType);
            }
            return(ObjectConvert.ConvertObject <TType>(result));
        }
Exemplo n.º 2
0
        Expression <Func <T, bool> > CreateLambdaBinary(CRLExpression expression)
        {
            var left    = expression.Left;
            var right   = expression.Right;
            var creater = new LambdaCreater <T>();
            var type    = expression.ExpressionType;

            var dic = new Dictionary <string, ExpressionHandler>();

            dic.Add("Equal", creater.Equal);
            dic.Add("NotEqual", creater.NotEqual);
            dic.Add("Greater", creater.Greater);
            dic.Add("Less", creater.Less);
            dic.Add("GreaterThan", creater.GreaterThan);
            dic.Add("LessThan", creater.LessThan);
            //todo 更多方法解析
            if (!dic.ContainsKey(type))
            {
                throw new CRLException("没有对应的运算方法 " + type);
            }

            if (left.Type == CRLExpressionType.MethodCall)//按方法运算 如 b.NameSubstring,Indexof(0,1)=="123"
            {
                //按属性的子方法
                var left2 = CreateLambdaMethodCall2(left);
                var value = ObjectConvert.ConvertObject(left2.Type, right.Data);
                return(dic[type](left2, value));
            }
            else//按属性运算 如b.Id==1
            {
                var member = creater.CreatePropertyExpression(left.Data.ToString());
                return(dic[type](member, right.Data));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 指定替换对象返回单个结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public override T ExecScalar <T>(string sql, params Type[] types)
        {
            sql = _DBAdapter.SqlFormat(sql);
            var obj = ExecScalar(sql, types);

            return(ObjectConvert.ConvertObject <T>(obj));
        }
Exemplo 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);
            //}
        }
Exemplo n.º 5
0
        /// <summary>
        /// 设置对象属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="value"></param>
        internal void SetValue(object obj, object value)
        {
            if (value == null)
            {
                return;
            }
            if (value is DBNull)
            {
                return;
            }
            Type type = value.GetType();

            if (propertyInfo.PropertyType != type)
            {
                if (value is Int32 && propertyInfo.PropertyType == typeof(string))
                {
                    value = value.ToString();
                }
            }
            try
            {
                //oracle会出现类型转换问题
                value = ObjectConvert.ConvertObject(propertyInfo.PropertyType, value);
                propertyInfo.SetValue(obj, value, null);
            }
            catch (Exception ero)
            {
                throw new Exception(ero.Message + " 在属性" + propertyInfo.Name + " " + propertyInfo.PropertyType);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 单个插入
        /// </summary>
        /// <param name="obj"></param>
        public override void InsertFromObj <TModel>(TModel obj)
        {
            var Reflection = ReflectionHelper.GetInfo <TModel>();

            CheckTableCreated <TModel>();
            var primaryKey = TypeCache.GetTable(obj.GetType()).PrimaryKey;

            CheckData(obj);
            var index = _DBAdapter.InsertObject(obj);

            if (!primaryKey.KeepIdentity)
            {
                //Reflection.GetAccessor(primaryKey.Name).Set((TModel)obj, index);
                index = ObjectConvert.ConvertObject(primaryKey.PropertyType, index);
                primaryKey.SetValue(obj, index);
            }
            ClearParame();
            var clone = obj.Clone();

            obj.OriginClone = clone as TModel;
            //var type = typeof(TModel);
            //if (TypeCache.ModelKeyCache.ContainsKey(type))
            //{
            //    string key = TypeCache.ModelKeyCache[type];
            //    MemoryDataCache.UpdateCacheItem(key, obj);
            //}
            UpdateCacheItem <TModel>(obj, null, true);
            //return index;
        }
Exemplo n.º 7
0
        Expression CreateLambdaBinary(CRLExpression expression)
        {
            var        left  = expression.Left;
            var        right = expression.Right;
            Expression exp;
            object     value;
            var        type = expression.ExpType;

            if (left.Type == CRLExpressionType.MethodCall)//按方法运算 如 b.NameSubstring,Indexof(0,1)=="123"
            {
                //按属性的子方法
                exp   = CreateLambdaMethodCall2(left);
                value = ObjectConvert.ConvertObject(exp.Type, right.Data);
            }
            else if (left.Type == CRLExpressionType.Tree)
            {
                exp   = CreateLambdaBinary(left);//todo 暂时在左边
                value = right.Data;
                //throw new CRLException("不支持的运算 "+ left.ExpressionType);
            }
            else//按属性运算 如b.Id==1
            {
                exp   = creater.CreatePropertyExpression(left.Data.ToString());
                value = right.Data;
            }

            switch (type)
            {
            case ExpressionType.Equal:
                return(creater.Equal(exp, value));

            case ExpressionType.NotEqual:
                return(creater.NotEqual(exp, value));

            case ExpressionType.GreaterThan:
                return(creater.Greater(exp, value));

            case ExpressionType.LessThan:
                return(creater.Less(exp, value));

            case ExpressionType.GreaterThanOrEqual:
                return(creater.GreaterThan(exp, value));

            case ExpressionType.LessThanOrEqual:
                return(creater.LessThan(exp, value));

            case ExpressionType.And:
                return(creater.And(exp, value));

            case ExpressionType.Or:
                return(creater.Or(exp, value));

            default:
                throw new CRLException("没有对应的运算方法 " + type);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// 获取常量表达式,自动转换值的类型
        /// </summary>
        /// <param name="expression">表达式</param>
        /// <param name="value">值</param>
        public static ConstantExpression GetConstant(this Expression expression, object value)
        {
            var memberExpression = expression as MemberExpression;

            if (memberExpression == null)
            {
                return(Expression.Constant(value));
            }
            value = ObjectConvert.ConvertObject(memberExpression.Type, value);
            return(Expression.Constant(value, memberExpression.Type));
        }
Exemplo n.º 9
0
        public string Equals(string field, ExpressionType nodeType, ref int parIndex, AddParameHandler addParame, object[] args)
        {
            string parName = string.Format("@equalEnum{0}", parIndex);

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

            obj = ObjectConvert.ConvertObject(obj.GetType(), obj);
            addParame(parName, obj);
            if (nodeType == ExpressionType.Equal)
            {
                return(string.Format("{0}={1}", field, parName));
            }
            else
            {
                return(string.Format("{0}!={1}", field, parName));
            }
        }
Exemplo n.º 10
0
        internal override TType GetFunction <TType, TModel>(Expression <Func <TModel, bool> > expression, Expression <Func <TModel, TType> > selectField, FunctionType functionType, bool compileSp = false)
        {
            var query = new MongoDBLambdaQuery <TModel>(dbContext);

            query.Select(selectField.Body);
            query.Where(expression);
            var    collection = _MongoDB.GetCollection <TModel>(query.QueryTableName);
            object result     = null;

            switch (functionType)
            {
            case FunctionType.COUNT:
                result = collection.Count(query.__MongoDBFilter);
                break;

            default:
                throw new NotSupportedException("MongoDB不支持的函数:" + functionType);
            }
            return(ObjectConvert.ConvertObject <TType>(result));
        }
Exemplo n.º 11
0
        /// <summary>
        /// 单个插入
        /// </summary>
        /// <param name="obj"></param>
        public override void InsertFromObj <TModel>(TModel obj)
        {
            //var Reflection = ReflectionHelper.GetInfo<TModel>();
            CheckTableCreated <TModel>();
            var primaryKey = TypeCache.GetTable(obj.GetType()).PrimaryKey;

            CheckData(obj);
            var index = _DBAdapter.InsertObject(dbContext, obj);

            if (!primaryKey.KeepIdentity)
            {
                //Reflection.GetAccessor(primaryKey.Name).Set((TModel)obj, index);
                index = ObjectConvert.ConvertObject(primaryKey.PropertyType, index);
                primaryKey.SetValue(obj, index);
            }
            ClearParame();
            obj.SetOriginClone();
            UpdateCacheItem <TModel>(obj, null, true);
            //return index;
        }
Exemplo n.º 12
0
        Expression <Func <T, bool> > CreateLambdaBinary(CRLExpression expression)
        {
            var left    = expression.Left;
            var right   = expression.Right;
            var creater = new LambdaCreater <T>();
            var type    = expression.ExpressionType;

            if (left.Type == CRLExpressionType.MethodCall)//按方法 如 Substring,Indexof
            {
                var dic = new Dictionary <string, BinaryMethodHandler>();
                dic.Add("Equal", creater.Equal);
                dic.Add("NotEqual", creater.NotEqual);
                dic.Add("Greater", creater.Greater);
                dic.Add("Less", creater.Less);
                dic.Add("GreaterThan", creater.GreaterThan);
                dic.Add("LessThan", creater.LessThan);
                if (!dic.ContainsKey(type))
                {
                    throw new Exception("没有对应的运算方法 " + type);
                }
                var left2 = CreateLambdaMethodCall2(left);
                var value = ObjectConvert.ConvertObject(left2.Type, right.Data);
                return(dic[type](left2, value));
            }
            else
            {
                var dic = new Dictionary <string, BinaryHandler>();
                dic.Add("Equal", creater.Equal);
                dic.Add("NotEqual", creater.NotEqual);
                dic.Add("Greater", creater.Greater);
                dic.Add("Less", creater.Less);
                dic.Add("GreaterThan", creater.GreaterThan);
                dic.Add("LessThan", creater.LessThan);
                if (!dic.ContainsKey(type))
                {
                    throw new Exception("没有对应的运算方法 " + type);
                }
                return(dic[type](left.Data.ToString(), right.Data));
            }
        }
Exemplo n.º 13
0
        public override TType GetFunction <TType, TModel>(Expression <Func <TModel, bool> > expression, Expression <Func <TModel, TType> > selectField, FunctionType functionType, bool compileSp = false)
        {
            var query = new MongoDBLambdaQuery <TModel>(dbContext);

            query.Select(selectField.Body);
            query.Where(expression);
            var    collection = _MongoDB.GetCollection <TModel>(query.QueryTableName);
            object result     = null;

            //https://blog.csdn.net/shiyaru1314/article/details/52370478
            //https://www.jb51.net/article/113820.htm
            //https://blog.csdn.net/u013476435/article/details/81560089
            switch (functionType)
            {
            case FunctionType.COUNT:
                result = collection.Count(query.__MongoDBFilter);
                break;

            default:
                throw new NotSupportedException("MongoDB不支持的函数:" + functionType);
            }
            return(ObjectConvert.ConvertObject <TType>(result));
        }
Exemplo n.º 14
0
        /// <summary>
        /// 获取OUT值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public override T GetOutParam <T>(string name)
        {
            var obj = dbHelper.GetOutParam(name);

            return(ObjectConvert.ConvertObject <T>(obj));
        }
Exemplo n.º 15
0
        /// <summary>
        /// 指定替换对象返回单个结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="types"></param>
        /// <returns></returns>
        public override T ExecScalar <T>(string sql, params Type[] types)
        {
            var obj = ExecScalar(sql, types);

            return(ObjectConvert.ConvertObject <T>(obj));
        }
Exemplo n.º 16
0
        /// <summary>
        /// 指定替换对象返回单个结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <returns></returns>
        public override T ExecScalar <T>(string sql)
        {
            var obj = ExecScalar(sql);

            return(ObjectConvert.ConvertObject <T>(obj));
        }