Skip to content

Apply filters for entity joins in hql #2550

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 5 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
82 changes: 82 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2549/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//------------------------------------------------------------------------------
// <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;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH2549
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.Save(new Person {Id = 1, Name = "Name"});
s.Save(new Customer {Deleted = false, Name = "Name", Id = 1});
s.Save(new Customer {Deleted = true, Name = "Name", Id = 2});

t.Commit();
}
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.CreateQuery("delete from System.Object").ExecuteUpdate();
t.Commit();
}
}

[Test]
public async Task EntityJoinFilterLinqAsync()
{
using (var s = OpenSession())
{
var list = await ((from p in s.Query<Person>()
join c in s.Query<Customer>() on p.Name equals c.Name
select p).ToListAsync());

s.EnableFilter("DeletedCustomer").SetParameter("deleted", false);

var filteredList = await ((from p in s.Query<Person>()
join c in s.Query<Customer>() on p.Name equals c.Name
select p).ToListAsync());

Assert.That(list, Has.Count.EqualTo(2));
Assert.That(filteredList, Has.Count.EqualTo(1));
}
}

[Test]
public async Task EntityJoinFilterQueryOverAsync()
{
using (var s = OpenSession())
{
Customer c = null;
Person p = null;
var list = await (s.QueryOver(() => p).JoinEntityAlias(() => c, () => c.Name == p.Name).ListAsync());

s.EnableFilter("DeletedCustomer").SetParameter("deleted", false);

var filteredList = await (s.QueryOver(() => p).JoinEntityAlias(() => c, () => c.Name == p.Name).ListAsync());

Assert.That(list, Has.Count.EqualTo(2));
Assert.That(filteredList, Has.Count.EqualTo(1));
}
}
}
}
70 changes: 70 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2549/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2549
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.Save(new Person {Id = 1, Name = "Name"});
s.Save(new Customer {Deleted = false, Name = "Name", Id = 1});
s.Save(new Customer {Deleted = true, Name = "Name", Id = 2});

t.Commit();
}
}

protected override void OnTearDown()
{
using (var s = OpenSession())
using (var t = s.BeginTransaction())
{
s.CreateQuery("delete from System.Object").ExecuteUpdate();
t.Commit();
}
}

[Test]
public void EntityJoinFilterLinq()
{
using (var s = OpenSession())
{
var list = (from p in s.Query<Person>()
join c in s.Query<Customer>() on p.Name equals c.Name
select p).ToList();

s.EnableFilter("DeletedCustomer").SetParameter("deleted", false);

var filteredList = (from p in s.Query<Person>()
join c in s.Query<Customer>() on p.Name equals c.Name
select p).ToList();

Assert.That(list, Has.Count.EqualTo(2));
Assert.That(filteredList, Has.Count.EqualTo(1));
}
}

[Test]
public void EntityJoinFilterQueryOver()
{
using (var s = OpenSession())
{
Customer c = null;
Person p = null;
var list = s.QueryOver(() => p).JoinEntityAlias(() => c, () => c.Name == p.Name).List();

s.EnableFilter("DeletedCustomer").SetParameter("deleted", false);

var filteredList = s.QueryOver(() => p).JoinEntityAlias(() => c, () => c.Name == p.Name).List();

Assert.That(list, Has.Count.EqualTo(2));
Assert.That(filteredList, Has.Count.EqualTo(1));
}
}
}
}
26 changes: 26 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2549/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH2549" >

<class name="Customer">
<id name="Id">
<generator class="assigned" />
</id>
<property name="Name" />
<property name="Deleted" type="Boolean" not-null="true" />

<filter name="DeletedCustomer" condition="Deleted = :deleted" />
</class>

<class name="Person">
<id name="Id">
<generator class="assigned" />
</id>
<property name="Name" />
</class>

<filter-def name="DeletedCustomer">
<filter-param name="deleted" type="Boolean"/>
</filter-def>

</hibernate-mapping>
15 changes: 15 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2549/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NHibernate.Test.NHSpecificTest.GH2549
{
public class Customer
{
public virtual int Id { get; set; }
public virtual bool Deleted { get; set; }
public virtual string Name { get; set; }
}

public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
}
1 change: 0 additions & 1 deletion src/NHibernate/Hql/Ast/ANTLR/Tree/EntityJoinFromElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public EntityJoinFromElement(FromClause fromClause, IQueryable entityPersister,
JoinSequence = new EntityJoinJoinSequenceImpl(
SessionFactoryHelper.Factory,
entityType,
entityPersister.TableName,
tableAlias,
joinType);

Expand Down
40 changes: 2 additions & 38 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/EntityJoinJoinSequenceImpl.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;
Expand All @@ -8,44 +7,9 @@ namespace NHibernate.Hql.Ast.ANTLR.Tree
{
class EntityJoinJoinSequenceImpl : JoinSequence
{
private readonly EntityType _entityType;
private readonly string _tableName;
private readonly string _tableAlias;
private readonly JoinType _joinType;

public EntityJoinJoinSequenceImpl(ISessionFactoryImplementor factory, EntityType entityType, string tableName, string tableAlias, JoinType joinType):base(factory)
public EntityJoinJoinSequenceImpl(ISessionFactoryImplementor factory, EntityType entityType, string tableAlias, JoinType joinType):base(factory)
{
_entityType = entityType;
_tableName = tableName;
_tableAlias = tableAlias;
_joinType = joinType;
}

internal override JoinFragment ToJoinFragment(
IDictionary<string, IFilter> enabledFilters,
bool includeExtraJoins,
SqlString withClauseFragment,
string withClauseJoinAlias,
string withRootAlias)
{
var joinFragment = new ANSIJoinFragment();

var on = withClauseFragment ?? SqlString.Empty;
//Note: filters logic commented due to following issues
//1) Original code is non functional as SqlString is immutable and so all Append results are lost. Correct code would look like: on = on.Append(filters);
//2) Also it seems GetOnCondition always returns empty string for entity join (as IsReferenceToPrimaryKey is always true).
// So if filters for entity join really make sense we need to inline GetOnCondition part that retrieves filters
// var filters = _entityType.GetOnCondition(_tableAlias, Factory, enabledFilters);
// if (!string.IsNullOrEmpty(filters))
// {
// on.Append(" and ").Append(filters);
// }
joinFragment.AddJoin(_tableName, _tableAlias, Array.Empty<string>(), Array.Empty<string>(), _joinType, on);
if (includeExtraJoins)
{
AddExtraJoins(joinFragment, _tableAlias, _entityType.GetAssociatedJoinable(Factory), _joinType == JoinType.InnerJoin);
}
return joinFragment;
AddJoin(entityType, tableAlias, joinType, Array.Empty<string>());
}
}
}