-
Notifications
You must be signed in to change notification settings - Fork 934
Linq: add enum HasFlag method support #3238
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7695e5c
Add enum HasFlag method support #829
d3b7001
Generate async files
github-actions[bot] 1e03044
Move test to correct namespace / folder
ab4a1e8
fix for DeepSource analysis
4a16664
Generate async files
github-actions[bot] 3376407
remove unnecessary usage of IRuntimeMethodHqlGenerator
b11297a
Remove unrelated formatting changes
bahusoid 8243687
Test cleanup
bahusoid 31c74da
Generate async files
github-actions[bot] df21561
Merge branch 'master' into hasflagsupport
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
73 changes: 73 additions & 0 deletions
73
src/NHibernate.Test/Async/NHSpecificTest/GH0829/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,73 @@ | ||
//------------------------------------------------------------------------------ | ||
// <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.GH0829 | ||
{ | ||
using System.Threading.Tasks; | ||
[TestFixture] | ||
public class FixtureAsync : BugTestCase | ||
{ | ||
protected override void OnSetUp() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
|
||
var e1 = new Parent { Type = TestEnum.A | TestEnum.C }; | ||
session.Save(e1); | ||
|
||
var e2 = new Child { Type = TestEnum.D, Parent = e1 }; | ||
session.Save(e2); | ||
|
||
var e3 = new Child { Type = TestEnum.C, Parent = e1 }; | ||
session.Save(e3); | ||
|
||
transaction.Commit(); | ||
} | ||
|
||
[Test] | ||
public async Task SelectClassAsync() | ||
{ | ||
using var session = OpenSession(); | ||
|
||
var resultFound = await (session.Query<Parent>().Where(x => x.Type.HasFlag(TestEnum.A)).FirstOrDefaultAsync()); | ||
|
||
var resultNotFound = await (session.Query<Parent>().Where(x => x.Type.HasFlag(TestEnum.D)).FirstOrDefaultAsync()); | ||
|
||
Assert.That(resultFound, Is.Not.Null); | ||
Assert.That(resultNotFound, Is.Null); | ||
} | ||
|
||
[Test] | ||
public async Task SelectChildClassContainedInParentAsync() | ||
{ | ||
using var session = OpenSession(); | ||
|
||
var result = await (session.Query<Child>().Where(x => x.Parent.Type.HasFlag(x.Type)).FirstOrDefaultAsync()); | ||
|
||
Assert.That(result, Is.Not.Null); | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
foreach (var entity in new[] { nameof(Child), nameof(Parent) }) | ||
{ | ||
session.CreateQuery($"delete from {entity}").ExecuteUpdate(); | ||
} | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
} |
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,61 @@ | ||
using System.Linq; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH0829 | ||
{ | ||
[TestFixture] | ||
public class Fixture : BugTestCase | ||
{ | ||
protected override void OnSetUp() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
|
||
var e1 = new Parent { Type = TestEnum.A | TestEnum.C }; | ||
session.Save(e1); | ||
|
||
var e2 = new Child { Type = TestEnum.D, Parent = e1 }; | ||
session.Save(e2); | ||
|
||
var e3 = new Child { Type = TestEnum.C, Parent = e1 }; | ||
session.Save(e3); | ||
|
||
transaction.Commit(); | ||
} | ||
|
||
[Test] | ||
public void SelectClass() | ||
{ | ||
using var session = OpenSession(); | ||
|
||
var resultFound = session.Query<Parent>().Where(x => x.Type.HasFlag(TestEnum.A)).FirstOrDefault(); | ||
|
||
var resultNotFound = session.Query<Parent>().Where(x => x.Type.HasFlag(TestEnum.D)).FirstOrDefault(); | ||
|
||
Assert.That(resultFound, Is.Not.Null); | ||
Assert.That(resultNotFound, Is.Null); | ||
} | ||
|
||
[Test] | ||
public void SelectChildClassContainedInParent() | ||
{ | ||
using var session = OpenSession(); | ||
|
||
var result = session.Query<Child>().Where(x => x.Parent.Type.HasFlag(x.Type)).FirstOrDefault(); | ||
|
||
Assert.That(result, Is.Not.Null); | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using var session = OpenSession(); | ||
using var transaction = session.BeginTransaction(); | ||
foreach (var entity in new[] { nameof(Child), nameof(Parent) }) | ||
{ | ||
session.CreateQuery($"delete from {entity}").ExecuteUpdate(); | ||
} | ||
|
||
transaction.Commit(); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/NHibernate.Test/NHSpecificTest/GH0829/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,17 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.GH0829"> | ||
<class name="Parent" table="parent"> | ||
<id name="Id" generator="guid.comb" /> | ||
<property name="Type" type="NHibernate.Type.EnumType`1[[NHibernate.Test.NHSpecificTest.GH0829.TestEnum, NHibernate.Test]], NHibernate"/> | ||
<bag name="Children" lazy="true"> | ||
<key column="ParentId" /> | ||
<one-to-many class="Child" /> | ||
</bag> | ||
</class> | ||
|
||
<class name="Child" table="Child"> | ||
<id name="Id" generator="guid.comb" /> | ||
<property name="Type" type="NHibernate.Type.EnumType`1[[NHibernate.Test.NHSpecificTest.GH0829.TestEnum, NHibernate.Test]], NHibernate"/> | ||
<many-to-one name="Parent" column="ParentId" not-null="true" /> | ||
</class> | ||
</hibernate-mapping> |
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,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH0829 | ||
{ | ||
public class Parent | ||
{ | ||
public virtual Guid Id { get; set; } | ||
|
||
public virtual TestEnum Type { get; set; } | ||
|
||
public virtual IList<Child> Children { get; set; } = new List<Child>(); | ||
} | ||
|
||
public class Child | ||
{ | ||
public virtual Guid Id { get; set; } | ||
|
||
public virtual TestEnum Type { get; set; } | ||
|
||
public virtual Parent Parent { 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,13 @@ | ||
using System; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.GH0829 | ||
{ | ||
[Flags] | ||
public enum TestEnum | ||
{ | ||
A = 1 << 0, | ||
B = 1 << 1, | ||
C = 1 << 2, | ||
D = 1 << 3 | ||
} | ||
} |
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
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
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,30 @@ | ||
using System; | ||
using System.Collections.ObjectModel; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using NHibernate.Hql.Ast; | ||
using NHibernate.Linq.Visitors; | ||
using NHibernate.Util; | ||
|
||
namespace NHibernate.Linq.Functions | ||
{ | ||
internal class HasFlagGenerator : BaseHqlGeneratorForMethod | ||
{ | ||
private const string _bitAndFunctionName = "band"; | ||
|
||
public HasFlagGenerator() | ||
{ | ||
SupportedMethods = new[] { ReflectHelper.GetMethodDefinition<Enum>(x => x.HasFlag(default)) }; | ||
} | ||
|
||
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor) | ||
{ | ||
return treeBuilder.Equality( | ||
treeBuilder.MethodCall( | ||
_bitAndFunctionName, | ||
visitor.Visit(targetObject).AsExpression(), | ||
visitor.Visit(arguments[0]).AsExpression()), | ||
visitor.Visit(arguments[0]).AsExpression()); | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.