Skip to content

Add session filter support for DML statement #1931

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 12 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from 10 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
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ indent_size = 2
[*.cshtml]
indent_style = space
indent_size = 4

[*.g]
indent_style = tab
172 changes: 172 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH1921/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
//------------------------------------------------------------------------------
// <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 NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1921
{
using System.Threading.Tasks;
using System.Threading;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override bool AppliesTo(Dialect.Dialect dialect)
{
return TestDialect.NativeGeneratorSupportsBulkInsertion;
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Name = "Bob" };
session.Save(e1);

var e2 = new Entity { Name = "Sally" };
session.Save(e2);

var me1 = new MultiTableEntity { Name = "Bob", OtherName = "Bob" };
session.Save(me1);

var me2 = new MultiTableEntity { Name = "Sally", OtherName = "Sally" };
session.Save(me2);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Theory]
public async Task DmlInsertAsync(bool filtered)
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (filtered)
session.EnableFilter("NameFilter").SetParameter("name", "Bob");
var rowCount = await (session.CreateQuery("insert into Entity (Name) select e.Name from Entity e")
.ExecuteUpdateAsync());
await (transaction.CommitAsync());

Assert.That(rowCount, Is.EqualTo(filtered ? 1 : 2));
}
}

[Theory]
public async Task DmlUpdateAsync(bool filtered)
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (filtered)
session.EnableFilter("NameFilter").SetParameter("name", "Bob");
var rowCount = await (session.CreateQuery("update Entity e set Name = 'newName'").ExecuteUpdateAsync());
await (transaction.CommitAsync());

Assert.That(rowCount, Is.EqualTo(filtered ? 1 : 2));
}
}

[Theory]
public async Task DmlDeleteAsync(bool filtered)
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (filtered)
session.EnableFilter("NameFilter").SetParameter("name", "Bob");
var rowCount = await (session.CreateQuery("delete Entity").ExecuteUpdateAsync());
await (transaction.CommitAsync());

Assert.That(rowCount, Is.EqualTo(filtered ? 1 : 2));
}
}

[TestCase(null)]
[TestCase("NameFilter")]
[TestCase("OtherNameFilter", IgnoreReason = "Not supported")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the insert statement does not have a multi table executer as update/delete have.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the filter does not require a multi-table handling (which is meant to insert/update/delete many tables). It requires a join instead in the select clause.
Having the filter on the joined table silently ignored while it seems correctly taken into account for updates/deletes is a bit troublesome. (Granted, the test only check the count of entities considered updated by the statement. It does not check in the database that only expected entities have been updated. It should likely be checked too.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And in fact, it is supported, and it works. That is the test which is bugged...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for correcting me, I overlooked the query and mistakenly assumed that the issue was with the lack of the multi executor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's me who wrote the bugged test in the first place, it was my mistake indeed.

public async Task MultiTableDmlInsertAsync(string filter, CancellationToken cancellationToken = default(CancellationToken))
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (!string.IsNullOrEmpty(filter))
session.EnableFilter(filter).SetParameter("name", "Bob");
var rowCount =
await (session
.CreateQuery(
// No insert of OtherName: not supported (INSERT statements cannot refer to superclass/joined properties)
"insert into MultiTableEntity (Name) select e.Name from Entity e")
.ExecuteUpdateAsync(cancellationToken));
await (transaction.CommitAsync(cancellationToken));

Assert.That(rowCount, Is.EqualTo(string.IsNullOrEmpty(filter) ? 2 : 1));
}
}

[TestCase(null)]
[TestCase("NameFilter")]
[TestCase("OtherNameFilter")]
public async Task MultiTableDmlUpdateAsync(string filter, CancellationToken cancellationToken = default(CancellationToken))
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (!string.IsNullOrEmpty(filter))
session.EnableFilter(filter).SetParameter("name", "Bob");
var rowCount =
await (session
.CreateQuery(
"update MultiTableEntity e" +
" set Name = 'newName', OtherName = 'newOtherName'" +
// Check referencing columns is supported
" where e.Name is not null and e.OtherName is not null")
.ExecuteUpdateAsync(cancellationToken));
await (transaction.CommitAsync(cancellationToken));

Assert.That(rowCount, Is.EqualTo(string.IsNullOrEmpty(filter) ? 2 : 1));
}
}

[TestCase(null)]
[TestCase("NameFilter")]
[TestCase("OtherNameFilter")]
public async Task MultiTableDmlDeleteAsync(string filter, CancellationToken cancellationToken = default(CancellationToken))
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (!string.IsNullOrEmpty(filter))
session.EnableFilter(filter).SetParameter("name", "Bob");
var rowCount =
await (session
.CreateQuery(
"delete MultiTableEntity e" +
// Check referencing columns is supported
" where e.Name is not null and e.OtherName is not null")
.ExecuteUpdateAsync(cancellationToken));
await (transaction.CommitAsync(cancellationToken));

Assert.That(rowCount, Is.EqualTo(string.IsNullOrEmpty(filter) ? 2 : 1));
}
}
}
}
17 changes: 17 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1921/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH1921
{
class Entity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}

class MultiTableEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string OtherName { get; set; }
}
}
160 changes: 160 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH1921/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH1921
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override bool AppliesTo(Dialect.Dialect dialect)
{
return TestDialect.NativeGeneratorSupportsBulkInsertion;
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Name = "Bob" };
session.Save(e1);

var e2 = new Entity { Name = "Sally" };
session.Save(e2);

var me1 = new MultiTableEntity { Name = "Bob", OtherName = "Bob" };
session.Save(me1);

var me2 = new MultiTableEntity { Name = "Sally", OtherName = "Sally" };
session.Save(me2);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from System.Object").ExecuteUpdate();

transaction.Commit();
}
}

[Theory]
public void DmlInsert(bool filtered)
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (filtered)
session.EnableFilter("NameFilter").SetParameter("name", "Bob");
var rowCount = session.CreateQuery("insert into Entity (Name) select e.Name from Entity e")
.ExecuteUpdate();
transaction.Commit();

Assert.That(rowCount, Is.EqualTo(filtered ? 1 : 2));
}
}

[Theory]
public void DmlUpdate(bool filtered)
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (filtered)
session.EnableFilter("NameFilter").SetParameter("name", "Bob");
var rowCount = session.CreateQuery("update Entity e set Name = 'newName'").ExecuteUpdate();
transaction.Commit();

Assert.That(rowCount, Is.EqualTo(filtered ? 1 : 2));
}
}

[Theory]
public void DmlDelete(bool filtered)
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (filtered)
session.EnableFilter("NameFilter").SetParameter("name", "Bob");
var rowCount = session.CreateQuery("delete Entity").ExecuteUpdate();
transaction.Commit();

Assert.That(rowCount, Is.EqualTo(filtered ? 1 : 2));
}
}

[TestCase(null)]
[TestCase("NameFilter")]
[TestCase("OtherNameFilter", IgnoreReason = "Not supported")]
public void MultiTableDmlInsert(string filter)
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (!string.IsNullOrEmpty(filter))
session.EnableFilter(filter).SetParameter("name", "Bob");
var rowCount =
session
.CreateQuery(
// No insert of OtherName: not supported (INSERT statements cannot refer to superclass/joined properties)
"insert into MultiTableEntity (Name) select e.Name from Entity e")
.ExecuteUpdate();
transaction.Commit();

Assert.That(rowCount, Is.EqualTo(string.IsNullOrEmpty(filter) ? 2 : 1));
}
}

[TestCase(null)]
[TestCase("NameFilter")]
[TestCase("OtherNameFilter")]
public void MultiTableDmlUpdate(string filter)
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (!string.IsNullOrEmpty(filter))
session.EnableFilter(filter).SetParameter("name", "Bob");
var rowCount =
session
.CreateQuery(
"update MultiTableEntity e" +
" set Name = 'newName', OtherName = 'newOtherName'" +
// Check referencing columns is supported
" where e.Name is not null and e.OtherName is not null")
.ExecuteUpdate();
transaction.Commit();

Assert.That(rowCount, Is.EqualTo(string.IsNullOrEmpty(filter) ? 2 : 1));
}
}

[TestCase(null)]
[TestCase("NameFilter")]
[TestCase("OtherNameFilter")]
public void MultiTableDmlDelete(string filter)
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
if (!string.IsNullOrEmpty(filter))
session.EnableFilter(filter).SetParameter("name", "Bob");
var rowCount =
session
.CreateQuery(
"delete MultiTableEntity e" +
// Check referencing columns is supported
" where e.Name is not null and e.OtherName is not null")
.ExecuteUpdate();
transaction.Commit();

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

<class name="Entity">
<id name="Id" generator="native"/>
<property name="Name"/>

<filter name="NameFilter"/>
</class>

<class name="MultiTableEntity">
<id name="Id" generator="native"/>
<property name="Name"/>

<join table="SecondTable">
<key column="Id"/>
<property name="OtherName"/>
</join>

<filter name="NameFilter"/>
<filter name="OtherNameFilter"/>
</class>

<filter-def name="NameFilter" condition="Name = :name">
<filter-param name="name" type="System.String"/>
</filter-def>

<filter-def name="OtherNameFilter" condition="OtherName = :name">
<filter-param name="name" type="System.String"/>
</filter-def>
</hibernate-mapping>
Loading