示例#1
0
        /// <summary>
        /// 使用 sql 语句来查询实体。
        /// </summary>
        /// <param name="args">The arguments.</param>
        /// <returns></returns>
        /// <exception cref="System.NotSupportedException">使用内存过滤器的同时,不支持提供分页参数。</exception>
        protected EntityList QueryList(SqlQueryArgs args)
        {
            this.PrepareArgs(args);

            var entityList = args.EntityList;
            var oldCount   = entityList.Count;

            bool autoIndexEnabled = entityList.AutoTreeIndexEnabled;

            try
            {
                //在加载数据时,自动索引功能都不可用。
                entityList.AutoTreeIndexEnabled = false;

                var dataProvider = Repo.RdbDataProvider;
                using (var dba = dataProvider.CreateDbAccesser())
                {
                    //访问数据库
                    if (args.Filter != null)
                    {
                        #region 内存过滤式加载

                        if (!PagingInfo.IsNullOrEmpty(args.PagingInfo))
                        {
                            throw new NotSupportedException("使用内存过滤器的同时,不支持提供分页参数。");
                        }

                        args.EntityType = Repo.EntityType;
                        args.MemoryList = new List <Entity>();
                        dataProvider.DbTable.QueryList(dba, args);
                        this.LoadByFilter(args);

                        #endregion
                    }
                    else
                    {
                        if (args.FetchType == FetchType.Count)
                        {
                            #region 查询 Count

                            var value = dba.QueryValue(args.FormattedSql, args.Parameters);
                            var count = DbTable.ConvertCount(value);
                            entityList.SetTotalCount(count);

                            #endregion
                        }
                        else
                        {
                            //是否需要为 PagingInfo 设置统计值。
                            var pagingInfoCount = !PagingInfo.IsNullOrEmpty(args.PagingInfo) && args.PagingInfo.IsNeedCount;

                            //如果 pagingInfoCount 为真,则在访问数据库时,会设置好 PagingInfo 的总行数。
                            args.EntityType = Repo.EntityType;
                            dataProvider.DbTable.QueryList(dba, args);

                            //最后,还需要设置列表的 TotalCount。
                            if (pagingInfoCount)
                            {
                                entityList.SetTotalCount(args.PagingInfo.TotalCount);
                            }
                        }
                    }
                }
            }
            finally
            {
                entityList.AutoTreeIndexEnabled = autoIndexEnabled;
            }

            this.EagerLoadOnCompleted(args, entityList, oldCount);

            return(entityList);
        }