Skip to content

Commit

Permalink
Fixed tip for <order alias="" />
Browse files Browse the repository at this point in the history
Fixed tip for distinct queries that include primary id
  • Loading branch information
MarkMpn committed Feb 28, 2022
1 parent 4a46235 commit f372e9a
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions FetchXmlBuilder/AppCode/Validations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ internal static ControlValidationResult GetWarning(TreeNode node, FetchXmlBuilde
return new ControlValidationResult(ControlValidationLevel.Info, "Alias is not recommended for not Aggregate queries.");
}

if (node.IsFetchDistinct() && !HasSortOnAttribute(node))
if (node.IsFetchDistinct() && !HasSortOnAttribute(node) && !HasPrimaryIdAttribute(node.Parent, fxb))
{
return new ControlValidationResult(ControlValidationLevel.Info, "Distinct queries should be sorted by all attributes for correct paging.", "https://markcarrington.dev/2020/12/08/dataverse-paging-with-distinct/");
}
Expand Down Expand Up @@ -141,7 +141,7 @@ internal static ControlValidationResult GetWarning(TreeNode node, FetchXmlBuilde
{
return new ControlValidationResult(ControlValidationLevel.Info, "Sorting on a link-entity triggers legacy paging.", "https://docs.microsoft.com/en-us/powerapps/developer/data-platform/org-service/paging-behaviors-and-ordering#ordering-and-multiple-table-queries");
}
if (fxb.entities != null)
if (fxb.entities != null && !string.IsNullOrWhiteSpace(attribute))
{
if (fxb.GetAttribute(parententity, attribute) is AttributeMetadata metaatt)
{
Expand Down Expand Up @@ -169,6 +169,32 @@ internal static ControlValidationResult GetWarning(TreeNode node, FetchXmlBuilde
return null;
}

private static bool HasPrimaryIdAttribute(TreeNode parent, FetchXmlBuilder fxb)
{
var entity = parent.Value("name");

if (string.IsNullOrWhiteSpace(entity))
return true;

if (fxb == null || fxb.entities == null)
return true;

if (!fxb.entities.TryGetValue(entity, out var metadata))
return true;

if (string.IsNullOrWhiteSpace(metadata.PrimaryIdAttribute))
return true;

var attr = parent.Nodes.OfType<TreeNode>()
.Where(n => n.Name == "attribute" && n.Value("name") == metadata.PrimaryIdAttribute)
.FirstOrDefault();

if (attr != null)
return true;

return false;
}

private static bool HasSortOnAttribute(TreeNode node)
{
var attrName = node.Value("name");
Expand Down

0 comments on commit f372e9a

Please sign in to comment.