Skip to content

GH-1879 - Allow Coalesce and Conditional logic on entity properties #1880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Cfg;
using NHibernate.Hql.Ast;
using NHibernate.Linq.Functions;
using NHibernate.Linq.Visitors;
using NHibernate.Util;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1879
{
using System.Threading.Tasks;
[TestFixture]
public class CoalesceChildThenAccessExtensionMethodAsync : GH1879BaseFixtureAsync<Issue>
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var clientA = new Client { Name = "Albert" };
var clientB = new Client { Name = "Bob" };
var corpA = new CorporateClient { Name = "Alpha", CorporateId = "1234" };
var corpB = new CorporateClient { Name = "Beta", CorporateId = "5647" };
var clientZ = new Client { Name = null }; // A null value should propagate if the entity is non-null
session.Save(clientA);
session.Save(clientB);
session.Save(corpA);
session.Save(corpB);
session.Save(clientZ);

var projectA = new Project { Name = "A", BillingClient = null, Client = clientA };
var projectB = new Project { Name = "B", BillingClient = corpB, Client = clientA };
var projectC = new Project { Name = "C", BillingClient = null, Client = clientB };
var projectD = new Project { Name = "D", BillingClient = corpA, Client = clientB };
var projectE = new Project { Name = "E", BillingClient = clientZ, Client = clientA };
var projectZ = new Project { Name = "Z", BillingClient = null, Client = null };
session.Save(projectA);
session.Save(projectB);
session.Save(projectC);
session.Save(projectD);
session.Save(projectE);
session.Save(projectZ);

session.Save(new Issue { Name = "01", Project = null, Client = null });
session.Save(new Issue { Name = "02", Project = null, Client = clientA });
session.Save(new Issue { Name = "03", Project = null, Client = clientB });
session.Save(new Issue { Name = "04", Project = projectC, Client = clientA });
session.Save(new Issue { Name = "05", Project = projectA, Client = clientB });
session.Save(new Issue { Name = "06", Project = projectB, Client = clientA });
session.Save(new Issue { Name = "07", Project = projectD, Client = clientB });
session.Save(new Issue { Name = "08", Project = projectZ, Client = corpA });
session.Save(new Issue { Name = "09", Project = projectZ, Client = corpB });
session.Save(new Issue { Name = "10", Project = projectE, Client = clientA });

session.Flush();
transaction.Commit();
}
}

protected override void Configure(Configuration configuration)
{
configuration.LinqToHqlGeneratorsRegistry<TestLinqToHqlGeneratorsRegistry>();
}

private class TestLinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
{
public TestLinqToHqlGeneratorsRegistry()
{
this.Merge(new TestHqlGeneratorForMethod());
}
}

private class TestHqlGeneratorForMethod : IHqlGeneratorForMethod
{
/// <inheritdoc />
public IEnumerable<MethodInfo> SupportedMethods => new []
{
ReflectHelper.GetMethodDefinition<Client>(x => x.NameByExtension()),
};

/// <inheritdoc />
public HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.Dot(visitor.Visit(arguments[0]).AsExpression(), treeBuilder.Ident("Name").AsExpression());
}
}

[Test]
public async Task WhereClauseAsync()
{
await (AreEqualAsync(
// Actual
q => q.Where(i => (i.Project.BillingClient ?? i.Project.Client ?? i.Client).NameByExtension().StartsWith("A")),
// Expected
q => q.Where(i => (i.Project.BillingClient != null ? i.Project.BillingClient.NameByExtension() : i.Project.Client != null ? i.Project.Client.NameByExtension() : i.Client.NameByExtension()).StartsWith("A"))
));
}

[Test]
public async Task SelectClauseAsync()
{
await (AreEqualAsync(
// Actual
q => q.OrderBy(i =>i.Name)
.Select(i => (i.Project.BillingClient ?? i.Project.Client ?? i.Client).NameByExtension()),
// Expected
q => q.OrderBy(i =>i.Name)
.Select(i => i.Project.BillingClient != null ? i.Project.BillingClient.NameByExtension() : i.Project.Client != null ? i.Project.Client.NameByExtension() : i.Client.NameByExtension())
));
}

[Test]
public async Task SelectClauseToAnonAsync()
{
await (AreEqualAsync(
// Actual
q => q.OrderBy(i =>i.Name)
.Select(i => new { Key =i.Name, Client = (i.Project.BillingClient ?? i.Project.Client ?? i.Client).NameByExtension() }),
// Expected
q => q.OrderBy(i =>i.Name)
.Select(i => new { Key =i.Name, Client = i.Project.BillingClient != null ? i.Project.BillingClient.NameByExtension() : i.Project.Client != null ? i.Project.Client.NameByExtension() : i.Client.NameByExtension() })
));
}

[Test]
public async Task OrderByClauseAsync()
{
await (AreEqualAsync(
// Actual
q => q.OrderBy(i => (i.Project.BillingClient ?? i.Project.Client ?? i.Client).NameByExtension() ?? "ZZZ")
.ThenBy(i =>i.Name)
.Select(i =>i.Name),
// Expected
q => q.OrderBy(i => (i.Project.BillingClient != null ? i.Project.BillingClient.NameByExtension() : i.Project.Client != null ? i.Project.Client.NameByExtension() : i.Client.NameByExtension()) ?? "ZZZ")
.ThenBy(i =>i.Name)
.Select(i =>i.Name)
));
}

[Test]
public async Task GroupByClauseAsync()
{
await (AreEqualAsync(
// Actual
q => q.GroupBy(i => (i.Project.BillingClient ?? i.Project.Client ?? i.Client).NameByExtension())
.OrderBy(x => x.Key ?? "ZZZ")
.Select(grp => new { grp.Key, Count = grp.Count() }),
// Expected
q => q.GroupBy(i => i.Project.BillingClient != null ? i.Project.BillingClient.NameByExtension() : i.Project.Client != null ? i.Project.Client.NameByExtension() : i.Client.NameByExtension())
.OrderBy(x => x.Key ?? "ZZZ")
.Select(grp => new { grp.Key, Count = grp.Count() })
));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1879
{
using System.Threading.Tasks;
[TestFixture]
public class CoalesceChildThenAccessMemberAsync : GH1879BaseFixtureAsync<Issue>
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var clientA = new Client { Name = "Albert" };
var clientB = new Client { Name = "Bob" };
var corpA = new CorporateClient { Name = "Alpha", CorporateId = "1234" };
var corpB = new CorporateClient { Name = "Beta", CorporateId = "5647" };
var clientZ = new Client { Name = null }; // A null value should propagate if the entity is non-null
session.Save(clientA);
session.Save(clientB);
session.Save(corpA);
session.Save(corpB);
session.Save(clientZ);

var projectA = new Project { Name = "A", BillingClient = null, Client = clientA };
var projectB = new Project { Name = "B", BillingClient = corpB, Client = clientA };
var projectC = new Project { Name = "C", BillingClient = null, Client = clientB };
var projectD = new Project { Name = "D", BillingClient = corpA, Client = clientB };
var projectE = new Project { Name = "E", BillingClient = clientZ, Client = clientA };
var projectZ = new Project { Name = "Z", BillingClient = null, Client = null };
session.Save(projectA);
session.Save(projectB);
session.Save(projectC);
session.Save(projectD);
session.Save(projectE);
session.Save(projectZ);

session.Save(new Issue { Name = "01", Project = null, Client = null });
session.Save(new Issue { Name = "02", Project = null, Client = clientA });
session.Save(new Issue { Name = "03", Project = null, Client = clientB });
session.Save(new Issue { Name = "04", Project = projectC, Client = clientA });
session.Save(new Issue { Name = "05", Project = projectA, Client = clientB });
session.Save(new Issue { Name = "06", Project = projectB, Client = clientA });
session.Save(new Issue { Name = "07", Project = projectD, Client = clientB });
session.Save(new Issue { Name = "08", Project = projectZ, Client = corpA });
session.Save(new Issue { Name = "09", Project = projectZ, Client = corpB });
session.Save(new Issue { Name = "10", Project = projectE, Client = clientA });

session.Flush();
transaction.Commit();
}
}

[Test]
public async Task WhereClauseAsync()
{
await (AreEqualAsync(
// Actual
q => q.Where(i => (i.Project.BillingClient ?? i.Project.Client ?? i.Client).Name.StartsWith("A")),
// Expected
q => q.Where(i => (i.Project.BillingClient != null ? i.Project.BillingClient.Name : i.Project.Client != null ? i.Project.Client.Name : i.Client.Name).StartsWith("A"))
));
}

[Test]
public async Task SelectClauseAsync()
{
await (AreEqualAsync(
// Actual
q => q.OrderBy(i => i.Name)
.Select(i => (i.Project.BillingClient ?? i.Project.Client ?? i.Client).Name),
// Expected
q => q.OrderBy(i => i.Name)
.Select(i => i.Project.BillingClient != null ? i.Project.BillingClient.Name : i.Project.Client != null ? i.Project.Client.Name : i.Client.Name)
));
}

[Test]
public async Task SelectClauseToAnonAsync()
{
await (AreEqualAsync(
// Actual
q => q.OrderBy(i => i.Name)
.Select(i => new { Key = i.Name, Client = (i.Project.BillingClient ?? i.Project.Client ?? i.Client).Name }),
// Expected
q => q.OrderBy(i => i.Name)
.Select(i => new { Key = i.Name, Client = i.Project.BillingClient != null ? i.Project.BillingClient.Name : i.Project.Client != null ? i.Project.Client.Name : i.Client.Name })
));
}

[Test]
public async Task OrderByClauseAsync()
{
await (AreEqualAsync(
// Actual
q => q.OrderBy(i => (i.Project.BillingClient ?? i.Project.Client ?? i.Client).Name ?? "ZZZ")
.ThenBy(i => i.Name)
.Select(i => i.Name),
// Expected
q => q.OrderBy(i => (i.Project.BillingClient != null ? i.Project.BillingClient.Name : i.Project.Client != null ? i.Project.Client.Name : i.Client.Name) ?? "ZZZ")
.ThenBy(i => i.Name)
.Select(i => i.Name)
));
}

[Test]
public async Task GroupByClauseAsync()
{
await (AreEqualAsync(
// Actual
q => q.GroupBy(i => (i.Project.BillingClient ?? i.Project.Client ?? i.Client).Name)
.OrderBy(x => x.Key ?? "ZZZ")
.Select(grp => new { grp.Key, Count = grp.Count() }),
// Expected
q => q.GroupBy(i => i.Project.BillingClient != null ? i.Project.BillingClient.Name : i.Project.Client != null ? i.Project.Client.Name : i.Client.Name)
.OrderBy(x => x.Key ?? "ZZZ")
.Select(grp => new { grp.Key, Count = grp.Count() })
));
}
}
}
Loading