Esempio n. 1
0
        /// <summary>
        /// Finds the page.
        /// </summary>
        /// <param name="pageNo">The page no.</param>
        /// <returns>The result.</returns>
        public EntityType[] FindPage(int pageNo)
        {
            Check.Require(pageNo > 0, "pageNo must > 0.");

            EntityConfiguration ec = new EntityType().GetEntityConfiguration();
            string cacheKey        = null;

            if (gateway.IsCacheTurnedOn && ec.IsAutoPreLoad)
            {
                cacheKey = gateway.ComputeCacheKey(typeof(EntityType).ToString() + "|PreLoad", new WhereClip(string.Empty));
                if (Gateway.cache.Get(cacheKey) == null)
                {
                    gateway.PreLoadEntities <EntityType>(ec);
                }
                DataTable dt = (DataTable)gateway.GetCache(cacheKey);
                if (dt == null)
                {
                    gateway.PreLoadEntities <EntityType>(ec);
                    dt = (DataTable)gateway.GetCache(cacheKey);
                }
                if (dt != null)
                {
                    DataRow[] rows;
                    if (string.IsNullOrEmpty(ps.Where) && string.IsNullOrEmpty(ps.OrderBy))
                    {
                        rows = dt.Select();
                    }
                    else if (string.IsNullOrEmpty(ps.Where))
                    {
                        rows = dt.Select(null, ps.OrderBy);
                    }
                    else if (string.IsNullOrEmpty(ps.OrderBy))
                    {
                        rows = dt.Select(gateway.ToFlatWhereClip(new WhereClip(ps.Where, ps.ParamValues), ec).ToString());
                    }
                    else
                    {
                        rows = dt.Select(gateway.ToFlatWhereClip(new WhereClip(ps.Where, ps.ParamValues), ec).ToString(), ps.OrderBy);
                    }

                    if (rows != null && rows.Length > 0)
                    {
                        List <DataRow> pageList = new List <DataRow>();
                        for (int i = (pageNo - 1) * PageSize; i < rows.Length && i < pageNo * PageSize; i++)
                        {
                            pageList.Add(rows[i]);
                        }
                        rows = pageList.ToArray();

                        List <EntityType> list = new List <EntityType>();
                        foreach (DataRow row in rows)
                        {
                            EntityType retObj = gateway.CreateEntity <EntityType>();
                            retObj.SetPropertyValues(row);
                            list.Add(retObj);
                        }
                        return(list.ToArray());
                    }
                }
                return(new EntityType[0]);
            }

            if (gateway.IsCacheTurnedOn && gateway.GetTableCacheExpireSeconds(ec.ViewName) > 0)
            {
                PageSplit pageSplit = (PageSplit)ps;
                cacheKey = gateway.ComputeCacheKey(typeof(EntityType).ToString() + "|FindPage_" + pageNo.ToString(), new WhereClip(pageNo == 1 ? pageSplit._PageSplitableSelectStatementForFirstPage : pageSplit._PageSplitableSelectStatement, pageSplit.paramValues));
                object cachedObj = gateway.GetCache(cacheKey);
                if (cachedObj != null)
                {
                    return((EntityType[])cachedObj);
                }
            }

            IDataReader       reader = ps.GetPageReadOnly(pageNo);
            List <EntityType> objs   = new List <EntityType>();

            while (reader.Read())
            {
                EntityType obj = gateway.CreateEntity <EntityType>();
                obj.SetPropertyValues(reader);
                objs.Add(obj);
            }
            reader.Close();
            reader.Dispose();

            if (gateway.IsCacheTurnedOn && gateway.GetTableCacheExpireSeconds(ec.ViewName) > 0 && cacheKey != null)
            {
                gateway.AddCache(cacheKey, objs.ToArray(), gateway.GetTableCacheExpireSeconds(ec.ViewName));
            }

            return(objs.ToArray());
        }