Skip to content

Fix AND/OR negation logic in hql #3328

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 9 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
63 changes: 63 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3327/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//------------------------------------------------------------------------------
// <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.GH3327
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using var session = OpenSession();
using var t = session.BeginTransaction();
var parent = new Entity { Name = "Parent" };
var child = new ChildEntity { Name = "Child", Parent = parent };
session.Save(parent);
session.Save(child);
t.Commit();
}

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

transaction.Commit();
}

[Test]
public async Task NotIsCorrectlyHandledAsync()
{
using var session = OpenSession();
var q = session.CreateQuery(
@"SELECT COUNT(ROOT.Id)
FROM Entity AS ROOT
WHERE (
EXISTS (FROM ChildEntity AS CHILD WHERE CHILD.Parent = ROOT)
AND ROOT.Name = 'Parent'
)");
Assert.That((await (q.ListAsync()))[0], Is.EqualTo(1));

q = session.CreateQuery(
@"SELECT COUNT(ROOT.Id)
FROM Entity AS ROOT
WHERE NOT (
EXISTS (FROM ChildEntity AS CHILD WHERE CHILD.Parent = ROOT)
AND ROOT.Name = 'Parent'
)");
Assert.That((await (q.ListAsync()))[0], Is.EqualTo(0));
}
}
}
17 changes: 17 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3327/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

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

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

namespace NHibernate.Test.NHSpecificTest.GH3327
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using var session = OpenSession();
using var t = session.BeginTransaction();
var parent = new Entity { Name = "Parent" };
var child = new ChildEntity { Name = "Child", Parent = parent };
session.Save(parent);
session.Save(child);
t.Commit();
}

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

transaction.Commit();
}

[Test]
public void NotIsCorrectlyHandled()
{
using var session = OpenSession();
var q = session.CreateQuery(
@"SELECT COUNT(ROOT.Id)
FROM Entity AS ROOT
WHERE (
EXISTS (FROM ChildEntity AS CHILD WHERE CHILD.Parent = ROOT)
AND ROOT.Name = 'Parent'
)");
Assert.That(q.List()[0], Is.EqualTo(1));

q = session.CreateQuery(
@"SELECT COUNT(ROOT.Id)
FROM Entity AS ROOT
WHERE NOT (
EXISTS (FROM ChildEntity AS CHILD WHERE CHILD.Parent = ROOT)
AND ROOT.Name = 'Parent'
)");
Assert.That(q.List()[0], Is.EqualTo(0));
}
}
}
16 changes: 16 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3327/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH3327">

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

<class name="ChildEntity">
<id name="Id" generator="identity" />
<many-to-one name="Parent" class="Entity" not-null="true" />
<property name="Name"/>
</class>

</hibernate-mapping>
8 changes: 4 additions & 4 deletions src/NHibernate/Hql/Ast/ANTLR/HqlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ public IASTNode NegateNode(IASTNode node)
case OR:
node.Type = AND;
node.Text = "{and}";
NegateNode(node.GetChild(0));
NegateNode(node.GetChild(1));
node.SetChild(0, NegateNode(node.GetChild(0)));
node.SetChild(1, NegateNode(node.GetChild(1)));
return node;
case AND:
node.Type = OR;
node.Text = "{or}";
NegateNode(node.GetChild(0));
NegateNode(node.GetChild(1));
node.SetChild(0, NegateNode(node.GetChild(0)));
node.SetChild(1, NegateNode(node.GetChild(1)));
return node;
case EQ:
node.Type = NE;
Expand Down