Skip to content

Commit

Permalink
Counting pages, records, etc. #153
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Aug 6, 2023
1 parent 24e9b6c commit 9030b63
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions FetchXmlBuilder/AppCode/QueryInfo.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,72 @@
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Rappen.XRM.Helpers.FetchXML;
using System;
using System.Xml;

namespace Rappen.XTB.FetchXmlBuilder.AppCode
{
internal class QueryInfo
{
public QueryBase Query;
private QueryBase query;
private EntityCollection result;
internal int PageSize = 0;
internal int PageNo = 1;
internal int Pages = -1;
internal int RecordFrom = -1;
internal int RecordTo = -1;

public string QuerySignature;
public string AttributesSignature;
public EntityCollection Results;

public QueryBase Query
{
get { return query; }
set
{
query = value;
if (query is FetchExpression fetch)
{
var fetchdoc = new XmlDocument();
fetchdoc.LoadXml(fetch.Query);
if (fetchdoc.SelectSingleNode("fetch") is XmlElement fetchnode)
{
if (fetchnode.AttributeInt("count") is int count)
{
PageSize = count;
}
if (fetchnode.AttributeInt("page") is int page)
{
PageNo = page;
}
}
}
}
}

public EntityCollection Results
{
get { return result; }
set
{
result = value;
if (!string.IsNullOrEmpty(result.PagingCookie))
{
var cookdoc = new XmlDocument();
cookdoc.LoadXml(result.PagingCookie);
if (cookdoc.SelectSingleNode("cookie") is XmlElement cookie &&
cookie.AttributeInt("page") is int page)
{
PageNo = page;
}
}
if (result.TotalRecordCount > -1 && PageSize > 0)
{
Pages = (int)Math.Ceiling((decimal)result.TotalRecordCount / PageSize);
}
RecordFrom = 1 + (PageNo - 1) * PageSize;
RecordTo = RecordFrom - 1 + result.Entities.Count;
}
}
}
}

0 comments on commit 9030b63

Please sign in to comment.