-
Notifications
You must be signed in to change notification settings - Fork 933
NH-3951 - support of .All as query result. Test cases, fix and refactoring #556
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
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.NH3951 | ||
{ | ||
class Entity | ||
{ | ||
public virtual Guid Id { get; set; } | ||
public virtual string Name { get; set; } | ||
public virtual Guid? RelatedId { get; set; } | ||
|
||
public virtual ISet<Entity> Related { 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,105 @@ | ||
using System.Linq; | ||
using NHibernate.Linq; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.NH3951 | ||
{ | ||
[TestFixture] | ||
public class Fixture : BugTestCase | ||
{ | ||
protected override void OnSetUp() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (ITransaction transaction = session.BeginTransaction()) | ||
{ | ||
var e1 = new Entity {Name = "Bob"}; | ||
session.Save(e1); | ||
|
||
var e2 = new Entity {Name = "Sally"}; | ||
session.Save(e2); | ||
|
||
session.Flush(); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
protected override void OnTearDown() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (ITransaction transaction = session.BeginTransaction()) | ||
{ | ||
session.Delete("from System.Object"); | ||
|
||
session.Flush(); | ||
transaction.Commit(); | ||
} | ||
} | ||
|
||
[Test] | ||
public void AllNamedBob() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (session.BeginTransaction()) | ||
{ | ||
var result = session.Query<Entity>() | ||
.All(e => e.Name == "Bob"); | ||
|
||
Assert.AreEqual(false, result); | ||
} | ||
} | ||
|
||
[Test] | ||
public void AllNamedWithAtLeast3Char() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (session.BeginTransaction()) | ||
{ | ||
var result = session.Query<Entity>() | ||
.All(e => e.Name.Length > 2); | ||
|
||
Assert.AreEqual(true, result); | ||
} | ||
} | ||
|
||
[Test] | ||
public void AllNamedBobWorkaround() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (session.BeginTransaction()) | ||
{ | ||
var result = !session.Query<Entity>() | ||
.Any(e => e.Name != "Bob"); | ||
|
||
Assert.AreEqual(false, result); | ||
} | ||
} | ||
|
||
[Test] | ||
public void AllNamedWithAtLeast3CharWorkaround() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (session.BeginTransaction()) | ||
{ | ||
var result = !session.Query<Entity>() | ||
.Any(e => e.Name.Length < 3); | ||
|
||
Assert.AreEqual(true, result); | ||
} | ||
} | ||
|
||
[Test] | ||
public void AnyAndAllInSubQueries() | ||
{ | ||
using (ISession session = OpenSession()) | ||
using (session.BeginTransaction()) | ||
{ | ||
var result = session.Query<Entity>() | ||
.Select(e => new { e.Id, hasRelated = e.Related.Any(), allBobRelated = e.Related.All(r => r.Name == "Bob") }) | ||
.ToList(); | ||
|
||
Assert.AreEqual(false, result[0].hasRelated); | ||
Assert.AreEqual(true, result[0].allBobRelated); | ||
} | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/NHibernate.Test/NHSpecificTest/NH3951/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,14 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test" namespace="NHibernate.Test.NHSpecificTest.NH3951"> | ||
|
||
<class name="Entity"> | ||
<id name="Id" generator="guid.comb" /> | ||
<property name="Name" /> | ||
<property name="RelatedId" /> | ||
<set name="Related"> | ||
<key column="RelatedId"/> | ||
<one-to-many class="Entity" /> | ||
</set> | ||
</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
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
36 changes: 25 additions & 11 deletions
36
src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessAll.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 |
---|---|---|
@@ -1,17 +1,31 @@ | ||
using NHibernate.Hql.Ast; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using NHibernate.Hql.Ast; | ||
using Remotion.Linq.Clauses.ResultOperators; | ||
|
||
namespace NHibernate.Linq.Visitors.ResultOperatorProcessors | ||
{ | ||
public class ProcessAll : IResultOperatorProcessor<AllResultOperator> | ||
{ | ||
public void Process(AllResultOperator resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree) | ||
{ | ||
tree.AddWhereClause(tree.TreeBuilder.BooleanNot( | ||
HqlGeneratorExpressionTreeVisitor.Visit(resultOperator.Predicate, queryModelVisitor.VisitorParameters). | ||
ToBooleanExpression())); | ||
public class ProcessAll : IResultOperatorProcessor<AllResultOperator> | ||
{ | ||
public void Process(AllResultOperator resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree) | ||
{ | ||
tree.AddWhereClause(tree.TreeBuilder.BooleanNot( | ||
HqlGeneratorExpressionTreeVisitor.Visit(resultOperator.Predicate, queryModelVisitor.VisitorParameters). | ||
ToBooleanExpression())); | ||
|
||
tree.SetRoot(tree.TreeBuilder.BooleanNot(tree.TreeBuilder.Exists((HqlQuery) tree.Root))); | ||
} | ||
} | ||
if (tree.IsRoot) | ||
{ | ||
tree.AddTakeClause(tree.TreeBuilder.Constant(1)); | ||
|
||
Expression<Func<IEnumerable<object>, bool>> x = l => !l.Any(); | ||
tree.AddListTransformer(x); | ||
} | ||
else | ||
{ | ||
tree.SetRoot(tree.TreeBuilder.BooleanNot(tree.TreeBuilder.Exists((HqlQuery)tree.Root))); | ||
} | ||
} | ||
} | ||
} |
30 changes: 22 additions & 8 deletions
30
src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessAny.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 |
---|---|---|
@@ -1,13 +1,27 @@ | ||
using NHibernate.Hql.Ast; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using NHibernate.Hql.Ast; | ||
using Remotion.Linq.Clauses.ResultOperators; | ||
|
||
namespace NHibernate.Linq.Visitors.ResultOperatorProcessors | ||
{ | ||
public class ProcessAny : IResultOperatorProcessor<AnyResultOperator> | ||
{ | ||
public void Process(AnyResultOperator anyOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree) | ||
{ | ||
tree.SetRoot(tree.TreeBuilder.Exists((HqlQuery) tree.Root)); | ||
} | ||
} | ||
public class ProcessAny : IResultOperatorProcessor<AnyResultOperator> | ||
{ | ||
public void Process(AnyResultOperator anyOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree) | ||
{ | ||
if (tree.IsRoot) | ||
{ | ||
tree.AddTakeClause(tree.TreeBuilder.Constant(1)); | ||
|
||
Expression<Func<IEnumerable<object>, bool>> x = l => l.Any(); | ||
tree.AddListTransformer(x); | ||
} | ||
else | ||
{ | ||
tree.SetRoot(tree.TreeBuilder.Exists((HqlQuery)tree.Root)); | ||
} | ||
} | ||
} | ||
} |
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.
Was formatted with spaces, got formatted with tabs.