-
Notifications
You must be signed in to change notification settings - Fork 933
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
fredericDelaporte
merged 12 commits into
nhibernate:master
from
maca88:DmlSessionFilter
Dec 14, 2018
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0869301
Test dml with filter
fredericDelaporte 050a12e
Fix test not supported by SQLServerCE dialect
fredericDelaporte 709eb3f
Add session filter support for DML statements
maca88 e124a30
Fix .g indent style
fredericDelaporte 63f3d95
Obsolete some methods
fredericDelaporte 36acea8
Clean up some usings in files having changed them
fredericDelaporte 3c16957
Remove a copy
fredericDelaporte c947ac9
Test multi-table filtered DML
fredericDelaporte edc3546
Merge branch 'master' into DmlSessionFilter
fredericDelaporte cef9a8f
Fixed tests for multi update and delete
maca88 a94329c
Strengthen the multi-table assert and fix the insert case
fredericDelaporte daddf1a
Merge branch 'master' into DmlSessionFilter
hazzik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,6 @@ indent_size = 2 | |
[*.cshtml] | ||
indent_style = space | ||
indent_size = 4 | ||
|
||
[*.g] | ||
indent_style = tab |
172 changes: 172 additions & 0 deletions
172
src/NHibernate.Test/Async/NHSpecificTest/GH1921/Fixture.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")] | ||
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)); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/NHibernate.Test/NHSpecificTest/GH1921/Mappings.hbm.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.