Ejemplo n.º 1
0
        /// <summary>
        /// GROUP和是否编译判断
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        internal CallBackDataReader GetPageReader <TModel>(LambdaQuery <TModel> query) where TModel : IModel, new()
        {
            if (query.__GroupFields.Count > 0)
            {
                return(GetGroupPageReader(query));
            }
            if (_DBAdapter.CanCompileSP && query.__CompileSp)
            {
                return(GetSpPageReader(query));
            }

            CheckTableCreated <TModel>();
            //var fields = query.GetQueryFieldString(b => b.Length > 500 || b.PropertyType == typeof(byte[]));
            var fields  = query.GetQueryFieldString();
            var rowOver = query.__QueryOrderBy;

            if (string.IsNullOrEmpty(rowOver))
            {
                var table = TypeCache.GetTable(typeof(TModel));
                rowOver = string.Format("t1.{0} desc", table.PrimaryKey.Name);
            }
            var orderBy   = System.Text.RegularExpressions.Regex.Replace(rowOver, @"t\d\.", "t.");
            var condition = query.GetQueryConditions();

            condition = _DBAdapter.SqlFormat(condition);
            query.FillParames(this);

            var pageIndex = query.__PageIndex;
            var pageSize  = query.__PageSize;

            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            pageSize  = pageSize == 0 ? 15 : pageSize;
            string countSql = string.Format("select count(*) from {0}", condition);
            int    count    = Convert.ToInt32(dbHelper.ExecScalar(countSql));

            query.ExecuteTime += dbHelper.ExecuteTime;
            query.RowCount     = count;
            if (count == 0)
            {
                return(null);
            }
            int pageCount = (count + pageSize - 1) / pageSize;

            if (pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }

            var    start  = pageSize * (pageIndex - 1) + 1;
            var    end    = start + pageSize - 1;
            string sql    = _DBAdapter.PageSqlFormat(fields, rowOver, condition, start, end, orderBy);
            var    reader = new CallBackDataReader(dbHelper.ExecDataReader(sql), () =>
            {
                return(count);
            });

            query.ExecuteTime += dbHelper.ExecuteTime;
            ClearParame();
            return(reader);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 返回动态类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="query"></param>
        /// <param name="resultSelector"></param>
        /// <param name="compileSp"></param>
        /// <returns></returns>
        public List <TResult> QueryDynamic <T, TResult>(LambdaQuery <T> query, Expression <Func <T, TResult> > resultSelector, bool compileSp = false) where T : IModel, new()
        {
            CheckTableCreated <T>();
            string sql = "";

            query.FillParames(this);
            sql = query.GetQuery();
            sql = _DBAdapter.SqlFormat(sql);
            System.Data.Common.DbDataReader reader;
            List <TResult> list;

            if (!compileSp)
            {
                if (query.QueryTop > 0)
                {
                    helper.AutoFormatWithNolock = false;
                }
                reader = helper.ExecDataReader(sql);
            }
            else//生成储过程
            {
                string sp = CompileSqlToSp(_DBAdapter.TemplateSp, sql);
                reader = helper.RunDataReader(sql);
            }
            list = Dynamic.DynamicObjConvert.DataReaderToDynamic(reader, resultSelector, query.FieldMapping);
            ClearParame();
            return(list);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 返回动态对象的查询
        /// </summary>
        /// <typeparam name="TItem"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        internal System.Data.Common.DbDataReader GetQueryDynamicReader <TItem>(LambdaQuery <TItem> query)
            where TItem : IModel, new()
        {
            CheckTableCreated <TItem>();
            string sql = "";

            query.FillParames(this);
            sql = query.GetQuery();
            sql = _DBAdapter.SqlFormat(sql);
            System.Data.Common.DbDataReader reader;
            var compileSp = query.__CompileSp;

            if (!compileSp)
            {
                if (query.__QueryTop > 0)
                {
                    dbHelper.AutoFormatWithNolock = false;
                }
                reader = dbHelper.ExecDataReader(sql);
            }
            else//生成储过程
            {
                string sp = CompileSqlToSp(_DBAdapter.TemplateSp, sql);
                reader = dbHelper.RunDataReader(sp);
            }
            query.ExecuteTime = dbHelper.ExecuteTime;
            ClearParame();
            return(reader);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 返回动态对象的查询
        /// 已支持join group
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="cacheTime"></param>
        /// <param name="compileSp"></param>
        /// <returns></returns>
        public List <dynamic> QueryDynamic <T>(LambdaQuery <T> query, int cacheTime = 0, bool compileSp = false)
            where T : IModel, new()
        {
            CheckTableCreated <T>();
            string sql = "";

            //foreach (var n in query.QueryParames)
            //{
            //    AddParam(n.Key, n.Value);
            //}
            query.FillParames(this);
            sql = query.GetQuery();
            sql = _DBAdapter.SqlFormat(sql);
            //DataTable dt;
            System.Data.Common.DbDataReader reader;
            List <dynamic> list;

            if (!compileSp)
            {
                if (query.QueryTop > 0)
                {
                    helper.AutoFormatWithNolock = false;
                }
                reader = helper.ExecDataReader(sql);
            }
            else//生成储过程
            {
                string sp = CompileSqlToSp(_DBAdapter.TemplateSp, sql);
                reader = helper.RunDataReader(sql);
            }
            list = Dynamic.DynamicObjConvert.DataReaderToDynamic(reader);

            ClearParame();
            return(list);
        }
Ejemplo n.º 5
0
        TType GetFunction <TType, TModel>(Expression <Func <TModel, bool> > expression, Expression <Func <TModel, TType> > selectField, FunctionType functionType, bool compileSp = false) where TModel : IModel, new()
        {
            LambdaQuery <TModel> query = new LambdaQuery <TModel>(this, false);
            string condition           = query.FormatExpression(expression);

            query.FillParames(this);
            string field = "";

            if (selectField.Body is MemberExpression)
            {
                MemberExpression mExp = (MemberExpression)selectField.Body;
                field = mExp.Member.Name;
                if (!field.Contains(","))
                {
                    field = _DBAdapter.KeyWordFormat(field);
                }
            }
            else
            {
                var constant = (ConstantExpression)selectField.Body;
                field = constant.Value.ToString();
            }
            CheckTableCreated <TModel>();
            string tableName = TypeCache.GetTableName <TModel>();

            string sql = "select " + functionType + "(" + field + ") from " + tableName + ' ' + _DBAdapter.GetWithNolockFormat() + " where " + condition;

            if (compileSp)
            {
                return(AutoExecuteScalar <TType>(sql));
            }
            return(ExecScalar <TType>(sql));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 指定条件删除
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="expression"></param>
        /// <returns></returns>
        public int Delete <TModel>(Expression <Func <TModel, bool> > expression) where TModel : IModel, new()
        {
            LambdaQuery <TModel> query = new LambdaQuery <TModel>(dbContext, false);
            string condition           = query.FormatExpression(expression);

            query.FillParames(this);
            return(Delete <TModel>(condition));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 使用完整的LamadaQuery查询
        /// </summary>
        /// <typeparam name="TItem"></typeparam>
        /// <param name="query"></param>
        /// <param name="cacheTime"></param>
        /// <param name="cacheKey">过期时间,分</param>
        /// <param name="compileSp">是否编译成储过程</param>
        /// <returns></returns>
        public List <TItem> QueryList <TItem>(LambdaQuery <TItem> query, int cacheTime, out string cacheKey, bool compileSp = false) where TItem : IModel, new()
        {
            CheckTableCreated <TItem>();
            if (query.PageSize > 0)//按分页
            {
                cacheKey = "";
                int count;
                return(AutoSpPage(query, out count));
            }
            string sql = "";
            bool   setConstraintObj = true;

            cacheKey = "";
            //foreach (var n in query.QueryParames)
            //{
            //    AddParam(n.Key, n.Value);
            //}
            query.FillParames(this);
            sql = query.GetQuery();
            sql = _DBAdapter.SqlFormat(sql);
            //DataTable dt;
            System.Data.Common.DbDataReader reader;
            List <TItem> list;

            if (cacheTime <= 0)
            {
                if (!compileSp)
                {
                    if (query.QueryTop > 0)
                    {
                        helper.AutoFormatWithNolock = false;
                    }
                    reader = helper.ExecDataReader(sql);
                }
                else//生成储过程
                {
                    string sp = CompileSqlToSp(_DBAdapter.TemplateSp, sql);
                    reader = helper.RunDataReader(sql);
                }
                list = ObjectConvert.DataReaderToList <TItem>(reader, setConstraintObj, query.FieldMapping);
            }
            else
            {
                list = MemoryDataCache.GetCacheList <TItem>(sql, cacheTime, helper, out cacheKey).Values.ToList();
            }
            ClearParame();
            query.RowCount = list.Count;
            SetOriginClone(list);
            return(list);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 使用完整的LamadaQuery查询
        /// </summary>
        /// <typeparam name="TItem"></typeparam>
        /// <param name="query"></param>
        /// <param name="cacheKey">cacheKey</param>
        /// <returns></returns>
        public List <TItem> QueryList <TItem>(LambdaQuery <TItem> query, out string cacheKey) where TItem : IModel, new()
        {
            CheckTableCreated <TItem>();
            if (query.__PageSize > 0)//按分页
            {
                cacheKey = "";
                return(Page <TItem, TItem>(query));
            }
            string sql = "";

            cacheKey = "";
            query.FillParames(this);
            sql = query.GetQuery();
            sql = _DBAdapter.SqlFormat(sql);
            System.Data.Common.DbDataReader reader;
            var          cacheTime = query.__ExpireMinute;
            var          compileSp = query.__CompileSp;
            List <TItem> list;
            double       runTime;

            if (cacheTime <= 0)
            {
                if (!compileSp)
                {
                    if (query.__QueryTop > 0)
                    {
                        dbHelper.AutoFormatWithNolock = false;
                    }
                    reader = dbHelper.ExecDataReader(sql);
                }
                else//生成储过程
                {
                    string sp = CompileSqlToSp(_DBAdapter.TemplateSp, sql);
                    reader = dbHelper.RunDataReader(sql);
                }
                query.ExecuteTime += dbHelper.ExecuteTime;
                list              = ObjectConvert.DataReaderToList <TItem>(reader, out runTime, true);
                query.MapingTime += runTime;
            }
            else
            {
                list = MemoryDataCache.CacheService.GetCacheList <TItem>(sql, cacheTime, dbHelper, out cacheKey).Values.ToList();
            }
            ClearParame();
            query.RowCount = list.Count;
            SetOriginClone(list);
            return(list);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 按编译
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        CallBackDataReader GetSpGroupPageReader <TModel>(LambdaQuery <TModel> query) where TModel : IModel, new()
        {
            CheckTableCreated <TModel>();
            var conditions = query.GetQueryConditions();
            var fields     = query.GetQueryFieldString();

            if (!conditions.Contains("group"))
            {
                throw new Exception("缺少group语法");
            }
            var rowOver = query.__QueryOrderBy;

            if (string.IsNullOrEmpty(rowOver))
            {
                throw new Exception("Group分页需指定Group排序字段");
                //var table = TypeCache.GetTable(typeof(T));
                //rowOver = string.Format("t1.{0} desc", table.PrimaryKey.Name);
            }
            var sort1 = System.Text.RegularExpressions.Regex.Replace(rowOver, @"t\d\.", "");

            conditions = _DBAdapter.SqlFormat(conditions);

            query.FillParames(this);
            var pageIndex = query.__PageIndex;
            var pageSize  = query.__PageSize;

            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            pageSize  = pageSize == 0 ? 15 : pageSize;
            AddParam("pageIndex", pageIndex);
            AddParam("pageSize", pageSize);
            dbHelper.AddOutParam("count", -1);
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("fields", fields);
            dic.Add("rowOver", rowOver);
            //dic.Add("sort", sort1);
            string             sp = CompileSqlToSp(_DBAdapter.TemplateGroupPage, conditions, dic);
            CallBackDataReader reader;

            reader = new CallBackDataReader(dbHelper.RunDataReader(sp), () =>
            {
                return(GetOutParam <int>("count"));
            });
            query.ExecuteTime += dbHelper.ExecuteTime;
            ClearParame();
            return(reader);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 按编译
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        CallBackDataReader GetSpPageReader <TModel>(LambdaQuery <TModel> query) where TModel : IModel, new()
        {
            CheckTableCreated <TModel>();
            //var fields = query.GetQueryFieldString(b => b.Length > 500 || b.PropertyType == typeof(byte[]));
            var fields  = query.GetQueryFieldString();
            var rowOver = query.__QueryOrderBy;

            if (string.IsNullOrEmpty(rowOver))
            {
                var table = TypeCache.GetTable(typeof(TModel));
                rowOver = string.Format("t1.{0} desc", table.PrimaryKey.Name);
            }
            var orderBy   = System.Text.RegularExpressions.Regex.Replace(rowOver, @"t\d\.", "t.");
            var condition = query.GetQueryConditions();

            condition = _DBAdapter.SqlFormat(condition);
            query.FillParames(this);
            var pageIndex = query.__PageIndex;
            var pageSize  = query.__PageSize;

            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            pageSize  = pageSize == 0 ? 15 : pageSize;
            AddParam("pageIndex", pageIndex);
            AddParam("pageSize", pageSize);
            AddOutParam("count", -1);
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("fields", fields);
            dic.Add("sort", orderBy);
            dic.Add("rowOver", rowOver);
            //string sql = string.Format("{0} with(nolock) where {1}", tableName, where);
            string             sp = CompileSqlToSp(_DBAdapter.TemplatePage, condition, dic);
            CallBackDataReader reader;

            reader = new CallBackDataReader(dbHelper.RunDataReader(sp), () =>
            {
                return(GetOutParam <int>("count"));
            });
            ClearParame();
            query.ExecuteTime += dbHelper.ExecuteTime;
            return(reader);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 指定条件和参数进行更新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression">条件</param>
        /// <param name="setValue">值</param>
        /// <returns></returns>
        public int Update <T>(Expression <Func <T, bool> > expression, ParameCollection setValue) where T : IModel, new()
        {
            if (setValue.Count == 0)
            {
                throw new Exception("更新时发生错误,参数值为空 ParameCollection setValue");
            }
            LambdaQuery <T> query     = new LambdaQuery <T>(this, false);
            string          condition = query.FormatExpression(expression);

            //foreach (var n in query.QueryParames)
            //{
            //    AddParam(n.Key, n.Value);
            //}
            query.FillParames(this);
            var count = Update <T>(setValue, condition);

            UpdateCacheItem <T>(expression, setValue);
            //CacheUpdated(typeof(T).Name);
            return(count);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// 按是否能编译
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="query"></param>
        /// <returns></returns>
        CallBackDataReader GetGroupPageReader <TModel>(LambdaQuery <TModel> query) where TModel : IModel, new()
        {
            if (_DBAdapter.CanCompileSP && query.__CompileSp)
            {
                return(GetSpGroupPageReader(query));
            }
            CheckTableCreated <TModel>();
            var condition = query.GetQueryConditions();
            var fields    = query.GetQueryFieldString();

            if (!condition.Contains("group"))
            {
                throw new Exception("缺少group语法");
            }
            var rowOver = query.__QueryOrderBy;

            if (string.IsNullOrEmpty(rowOver))
            {
                throw new Exception("Group分页需指定Group排序字段");
                //var table = TypeCache.GetTable(typeof(T));
                //rowOver = string.Format("t1.{0} desc", table.PrimaryKey.Name);
            }
            var sort1 = System.Text.RegularExpressions.Regex.Replace(rowOver, @"t\d\.", "");

            condition = _DBAdapter.SqlFormat(condition);

            query.FillParames(this);
            var pageIndex = query.__PageIndex;
            var pageSize  = query.__PageSize;

            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            pageSize  = pageSize == 0 ? 15 : pageSize;

            string countSql = string.Format("select count(*)  from (select count(*) as a from {0}) t", condition);
            int    count    = Convert.ToInt32(dbHelper.ExecScalar(countSql));

            query.ExecuteTime += dbHelper.ExecuteTime;
            query.RowCount     = count;
            if (count == 0)
            {
                return(null);
            }
            int pageCount = (count + pageSize - 1) / pageSize;

            if (pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }

            var    start = pageSize * (pageIndex - 1) + 1;
            var    end   = start + pageSize - 1;
            string sql   = _DBAdapter.PageSqlFormat(fields, rowOver, condition, start, end, "");
            //System.Data.Common.DbDataReader reader;
            //reader = dbHelper.ExecDataReader(sql);
            var reader = new CallBackDataReader(dbHelper.ExecDataReader(sql), () =>
            {
                return(count);
            });

            query.ExecuteTime += dbHelper.ExecuteTime;
            ClearParame();
            return(reader);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 对GROUP进行分页
        /// </summary>
        /// <param name="query">包含了group的查询</param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List <dynamic> AutoSpGroupPage <T>(LambdaQuery <T> query, out int count) where T : IModel, new()
        {
            var conditions = query.GetQueryConditions();
            var fields     = query.GetQueryFieldString();

            if (!conditions.Contains("group"))
            {
                throw new Exception("缺少group语法");
            }
            var rowOver = query.QueryOrderBy;

            if (string.IsNullOrEmpty(rowOver))
            {
                rowOver = "t1.id desc";
            }
            var sort1 = System.Text.RegularExpressions.Regex.Replace(rowOver, @"t\d\.", "");

            conditions = _DBAdapter.SqlFormat(conditions);
            count      = 0;
            //var parame = query.QueryParames;
            //foreach (var n in parame)
            //{
            //    AddParam(n.Key, n.Value);
            //}
            query.FillParames(this);
            var pageIndex = query.PageIndex;
            var pageSize  = query.PageSize;

            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            pageSize  = pageSize == 0 ? 15 : pageSize;
            AddParam("pageIndex", pageIndex);
            AddParam("pageSize", pageSize);
            helper.AddOutParam("count", 1);
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("fields", fields);
            dic.Add("rowOver", rowOver);
            //dic.Add("sort", sort1);
            string sp = CompileSqlToSp(_DBAdapter.TemplateGroupPage, conditions, dic);

            System.Data.Common.DbDataReader reader;
            try
            {
                reader = helper.RunDataReader(sp);
                count  = GetOutParam <int>("count");
            }
            catch (Exception ero)
            {
                if (ero.Message.Contains("找不到存储过程"))//删除后自动创建
                {
                    spCahe.Remove(sp);
                    sp     = CompileSqlToSp(_DBAdapter.TemplateGroupPage, conditions, dic);
                    reader = helper.RunDataReader(sp);
                    count  = GetOutParam <int>("count");
                }
                else
                {
                    throw ero;
                }
            }
            ClearParame();
            return(Dynamic.DynamicObjConvert.DataReaderToDynamic(reader));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 对表进行分页并编译成存储过程
        /// </summary>
        /// <typeparam name="TItem"></typeparam>
        /// <param name="query"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List <TItem> AutoSpPage <TItem>(LambdaQuery <TItem> query, out int count) where TItem : IModel, new()
        {
            CheckTableCreated <TItem>();
            var fields  = query.GetQueryFieldString(b => b.Length > 500 || b.PropertyType == typeof(byte[]));
            var rowOver = query.QueryOrderBy;

            if (string.IsNullOrEmpty(rowOver))
            {
                rowOver = "t1.id desc";
            }
            var orderBy   = System.Text.RegularExpressions.Regex.Replace(rowOver, @"t\d\.", "t.");
            var condition = query.GetQueryConditions();

            //var parame = query.QueryParames;
            condition = _DBAdapter.SqlFormat(condition);
            count     = 0;
            query.FillParames(this);
            //foreach (var n in parame)
            //{
            //    AddParam(n.Key, n.Value);
            //}
            var pageIndex = query.PageIndex;
            var pageSize  = query.PageSize;

            pageIndex = pageIndex == 0 ? 1 : pageIndex;
            pageSize  = pageSize == 0 ? 15 : pageSize;
            AddParam("pageIndex", pageIndex);
            AddParam("pageSize", pageSize);
            AddOutParam("count", 1);
            Dictionary <string, string> dic = new Dictionary <string, string>();

            dic.Add("fields", fields);
            dic.Add("sort", orderBy);
            dic.Add("rowOver", rowOver);
            //string sql = string.Format("{0} with(nolock) where {1}", tableName, where);
            string             sp = CompileSqlToSp(_DBAdapter.TemplatePage, condition, dic);
            CallBackDataReader reader;

            try
            {
                reader = new CallBackDataReader(helper.RunDataReader(sp), () =>
                {
                    return(GetOutParam <int>("count"));
                });
                //count = GetOutParam<int>("count");
            }
            catch (Exception ero)
            {
                if (ero.Message.Contains("找不到存储过程"))//删除后自动创建
                {
                    spCahe.Remove(sp);
                    sp     = CompileSqlToSp(_DBAdapter.TemplatePage, condition, dic);
                    reader = new CallBackDataReader(helper.RunDataReader(sp), () =>
                    {
                        return(GetOutParam <int>("count"));
                    });
                }
                else
                {
                    throw ero;
                }
            }
            var list = reader.GetData <TItem>(out count, query.FieldMapping);

            query.RowCount = count;
            ClearParame();
            //return ObjectConvert.DataReaderToList<TItem>(reader);
            return(list);
        }