Skip to content

Fix theta-style joins for entity joins #3137

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 2 commits into from
Aug 30, 2022
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
91 changes: 91 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH3113/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//------------------------------------------------------------------------------
// <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 NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Dialect;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH3113
{
using System.Threading.Tasks;
[TestFixture]
public class ByCodeFixtureAsync : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override bool AppliesTo(Dialect.Dialect dialect)
{
return dialect is Oracle9iDialect;
}

protected override void Configure(Configuration configuration)
{
var dialect = NHibernate.Dialect.Dialect.GetDialect(configuration.Properties);
if (dialect is Oracle8iDialect)
configuration.SetProperty(Environment.Dialect, typeof(Oracle9iDialect).FullName);

base.Configure(configuration);
}

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);

transaction.Commit();
}
}

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

transaction.Commit();
}
}

[Test]
public async Task JoinFailsOnOracle9DialectAsync()
{
using (var session = OpenSession())
{
var result = from e in session.Query<Entity>()
join e2 in session.Query<Entity>() on e.Id equals e2.Id
where e.Name == "Bob"
select e.Name;

Assert.That(await (result.ToListAsync()), Has.Count.EqualTo(1));
}
}
}
}
10 changes: 10 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3113/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace NHibernate.Test.NHSpecificTest.GH3113
{
class Entity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
}
79 changes: 79 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH3113/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Dialect;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH3113
{
[TestFixture]
public class ByCodeFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();
mapper.Class<Entity>(rc =>
{
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
rc.Property(x => x.Name);
});

return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override bool AppliesTo(Dialect.Dialect dialect)
{
return dialect is Oracle9iDialect;
}

protected override void Configure(Configuration configuration)
{
var dialect = NHibernate.Dialect.Dialect.GetDialect(configuration.Properties);
if (dialect is Oracle8iDialect)
configuration.SetProperty(Environment.Dialect, typeof(Oracle9iDialect).FullName);

base.Configure(configuration);
}

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);

transaction.Commit();
}
}

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

transaction.Commit();
}
}

[Test]
public void JoinFailsOnOracle9Dialect()
{
using (var session = OpenSession())
{
var result = from e in session.Query<Entity>()
join e2 in session.Query<Entity>() on e.Id equals e2.Id
where e.Name == "Bob"
select e.Name;

Assert.That(result.ToList(), Has.Count.EqualTo(1));
}
}
}
}
12 changes: 6 additions & 6 deletions src/NHibernate/Hql/Ast/ANTLR/Util/JoinProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ private void AddJoinNodes(IRestrictableStatement query, JoinSequence join, FromE
SqlString frag = joinFragment.ToFromFragmentString;
SqlString whereFrag = joinFragment.ToWhereFragmentString;

// If the from element represents a JOIN_FRAGMENT and it is
// a theta-style join, convert its type from JOIN_FRAGMENT
// to FROM_FRAGMENT
if ( fromElement.Type == HqlSqlWalker.JOIN_FRAGMENT &&
( join.IsThetaStyle || SqlStringHelper.IsNotEmpty( whereFrag ) ) )
// If the from element represents a JOIN_FRAGMENT or ENTITY_JOIN and it is
// a theta-style join, convert its type to FROM_FRAGMENT.
if ((fromElement.Type == HqlSqlWalker.JOIN_FRAGMENT || fromElement.Type == HqlSqlWalker.ENTITY_JOIN) &&
(join.IsThetaStyle || SqlStringHelper.IsNotEmpty(whereFrag)))
{
fromElement.Type = HqlSqlWalker.FROM_FRAGMENT;
fromElement.JoinSequence.SetUseThetaStyle( true ); // this is used during SqlGenerator processing
// This is used during SqlGenerator processing.
fromElement.JoinSequence.SetUseThetaStyle(true);
}

// If there is a FROM fragment and the FROM element is an explicit, then add the from part.
Expand Down