Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into direct-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMpn committed Nov 3, 2024
2 parents 6495afb + a4324e2 commit 1107675
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
23 changes: 23 additions & 0 deletions MarkMpn.Sql4Cds.Engine.FetchXml.Tests/FetchXml2SqlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@ public void JoinAlias()
Assert.AreEqual("SELECT contact.firstname, contact.lastname, a.name FROM contact INNER JOIN account AS a ON contact.parentcustomerid = a.accountid", NormalizeWhitespace(converted));
}

[TestMethod]
public void JoinAliasFilter()
{
var metadata = new AttributeMetadataCache(_service);
var fetch = @"
<fetch>
<entity name='contact'>
<attribute name='firstname' />
<attribute name='lastname' />
<link-entity name='account' from='accountid' to='parentcustomerid' alias='a'>
<attribute name='name' />
</link-entity>
<filter>
<condition attribute='name' operator='eq' value='data8' entityname='a' />
</filter>
</entity>
</fetch>";

var converted = FetchXml2Sql.Convert(_service, metadata, fetch, new FetchXml2SqlOptions(), out _);

Assert.AreEqual("SELECT contact.firstname, contact.lastname, a.name FROM contact INNER JOIN account AS a ON contact.parentcustomerid = a.accountid WHERE a.name = 'data8'", NormalizeWhitespace(converted));
}

[TestMethod]
public void Order()
{
Expand Down
10 changes: 6 additions & 4 deletions MarkMpn.Sql4Cds.Engine/FetchXml2Sql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static string Convert(IOrganizationService org, IAttributeMetadataCache m
var aliasToLogicalName = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

// Link entities can also affect the WHERE clause
var filter = GetFilter(org, metadata, entity.Items, entity.name, aliasToLogicalName, options, ctes, parameterValues, ref requiresTimeZone, ref usesToday);
BooleanExpression filter = null;

if (entity != null)
{
Expand Down Expand Up @@ -117,6 +117,8 @@ public static string Convert(IOrganizationService org, IAttributeMetadataCache m
}

// WHERE
filter = CombineExpressions(filter, BooleanBinaryExpressionType.And, GetFilter(org, metadata, entity.Items, entity.name, aliasToLogicalName, options, ctes, parameterValues, ref requiresTimeZone, ref usesToday));

if (filter != null)
{
query.WhereClause = new WhereClause
Expand Down Expand Up @@ -639,12 +641,12 @@ private static TableReference BuildJoins(IOrganizationService org, IAttributeMet

private static BooleanExpression CombineExpressions(BooleanExpression expr1, BooleanBinaryExpressionType type, BooleanExpression expr2)
{
if (expr1 == null || expr2 == null)
return expr1 ?? expr2;

if (expr2 is BooleanBinaryExpression bbe && bbe.BinaryExpressionType != type)
expr2 = new BooleanParenthesisExpression { Expression = expr2 };

if (expr1 == null)
return expr2;

if (expr1 is BooleanBinaryExpression lhs && lhs.BinaryExpressionType != type)
expr2 = new BooleanParenthesisExpression { Expression = expr1 };

Expand Down

0 comments on commit 1107675

Please sign in to comment.