Skip to content

Propagate ExpectedType to CaseNode in hql #2705

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 3 commits into from
Mar 20, 2021
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
82 changes: 82 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2704/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//------------------------------------------------------------------------------
// <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.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;
using NHibernate.Linq;

namespace NHibernate.Test.NHSpecificTest.GH2704
{
using System.Threading.Tasks;
[TestFixture]
public class EnhancedUserTypeFixtureAsync : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();

mapper.AddMapping<Entity1Map>();
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Save(new Entity1() {Id = "id1", IsChiusa = true});
session.Save(new Entity1() {Id = "id2", IsChiusa = false});
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 CompareWithConstantAsync()
{
var yes = true;
using (var s = OpenSession())
Assert.IsTrue(await (s.Query<Entity1>().Where(x => x.IsChiusa == yes).AnyAsync()));
}

[Test]
public async Task NotOnPropertyAsync()
{
using (var s = OpenSession())
Assert.IsTrue(await (s.Query<Entity1>().Where(x => !x.IsChiusa).AllAsync(x => !x.IsChiusa)));
}

[Test]
public async Task CompareWithInlineConstantAsync()
{
using (var s = OpenSession())
Assert.IsTrue(await (s.Query<Entity1>().Where(x => x.IsChiusa == false).AnyAsync()));
}

[Test]
public async Task CompareWithNotOnConstantAsync()
{
var no = false;
using (var s = OpenSession())
Assert.IsTrue(await (s.Query<Entity1>().Where(x => x.IsChiusa == !no).AnyAsync()));
}
}
}
22 changes: 22 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2704/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using NHibernate.Mapping.ByCode.Conformist;

namespace NHibernate.Test.NHSpecificTest.GH2704
{
public class Entity1
{
public virtual string Id { get; set; }
public virtual bool IsChiusa { get; set; }
}

class Entity1Map : ClassMapping<Entity1>
{
public Entity1Map()
{
Table("TA");

Id(x => x.Id);
Property(x => x.IsChiusa, m => m.Type<StringBoolToBoolUserType>());
}
}
}
70 changes: 70 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2704/FixtureByCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Linq;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Mapping.ByCode;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2704
{
[TestFixture]
public class EnhancedUserTypeFixture : TestCaseMappingByCode
{
protected override HbmMapping GetMappings()
{
var mapper = new ModelMapper();

mapper.AddMapping<Entity1Map>();
return mapper.CompileMappingForAllExplicitlyAddedEntities();
}

protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.Save(new Entity1() {Id = "id1", IsChiusa = true});
session.Save(new Entity1() {Id = "id2", IsChiusa = false});
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 CompareWithConstant()
{
var yes = true;
using (var s = OpenSession())
Assert.IsTrue(s.Query<Entity1>().Where(x => x.IsChiusa == yes).Any());
}

[Test]
public void NotOnProperty()
{
using (var s = OpenSession())
Assert.IsTrue(s.Query<Entity1>().Where(x => !x.IsChiusa).All(x => !x.IsChiusa));
}

[Test]
public void CompareWithInlineConstant()
{
using (var s = OpenSession())
Assert.IsTrue(s.Query<Entity1>().Where(x => x.IsChiusa == false).Any());
}

[Test]
public void CompareWithNotOnConstant()
{
var no = false;
using (var s = OpenSession())
Assert.IsTrue(s.Query<Entity1>().Where(x => x.IsChiusa == !no).Any());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Data;
using System.Data.Common;
using NHibernate.Engine;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;

namespace NHibernate.Test.NHSpecificTest.GH2704
{
public class StringBoolToBoolUserType : IEnhancedUserType
{
public object Assemble(object cached, object owner) => cached;

public bool IsMutable => false;
public object DeepCopy(object value) => value;
public object Disassemble(object value) => value;
public object Replace(object original, object target, object owner) => original;

public object FromXMLString(string xml) => xml;
public string ToXMLString(object value) => ((bool) value) ? "'S'" : "'N'";
public string ObjectToSQLString(object value) => ((bool) value) ? "'S'" : "'N'";

bool IUserType.Equals(object x, object y) => x == null ? false : x.Equals(y);
public int GetHashCode(object x) => x == null ? typeof(bool).GetHashCode() + 473 : x.GetHashCode();

public object NullSafeGet(DbDataReader rs, string[] names, ISessionImplementor session, object owner)
{
var value = NHibernateUtil.String.NullSafeGet(rs, names[0], session);
if (value == null) return false;

return (string) value == "S";
}

public void NullSafeSet(DbCommand cmd, object value, int index, ISessionImplementor session)
{
if (value == null)
{
NHibernateUtil.String.NullSafeSet(cmd, null, index, session);
return;
}

value = (bool) value ? "S" : "N";
NHibernateUtil.String.NullSafeSet(cmd, value, index, session);
}

public System.Type ReturnedType => typeof(bool);
public SqlType[] SqlTypes => new SqlType[] {new SqlType(DbType.String)};
}
}
78 changes: 47 additions & 31 deletions src/NHibernate/Hql/Ast/ANTLR/Tree/CaseNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Antlr.Runtime;
using NHibernate.Hql.Ast.ANTLR.Util;
using NHibernate.Type;
Expand All @@ -12,8 +14,10 @@ namespace NHibernate.Hql.Ast.ANTLR.Tree
/// Ported by: Steve Strong
/// </summary>
[CLSCompliant(false)]
public class CaseNode : AbstractSelectExpression, ISelectExpression
public class CaseNode : AbstractSelectExpression, ISelectExpression, IExpectedTypeAwareNode
{
private IType _expectedType;

public CaseNode(IToken token) : base(token)
{
}
Expand All @@ -22,46 +26,58 @@ public override IType DataType
{
get
{
for (int i = 0; i < ChildCount; i++)
if (ExpectedType != null)
return ExpectedType;

foreach (var node in GetResultNodes())
{
IASTNode whenOrElseClause = GetChild(i);
if (whenOrElseClause.Type == HqlParser.WHEN)
{
// WHEN Child(0) THEN Child(1)
IASTNode thenClause = whenOrElseClause.GetChild(1);
if (thenClause is ISelectExpression)
{
if (!(thenClause is ParameterNode))
{
return (thenClause as ISelectExpression).DataType;
}
}
}
else if (whenOrElseClause.Type == HqlParser.ELSE)
{
// ELSE Child(0)
IASTNode elseClause = whenOrElseClause.GetChild(0);
if (elseClause is ISelectExpression)
{
if (!(elseClause is ParameterNode))
{
return (elseClause as ISelectExpression).DataType;
}
}
}
else
{
throw new HibernateException("Was expecting a WHEN or ELSE, but found a: " + whenOrElseClause.Text);
}
if (node is ISelectExpression select && !(node is ParameterNode))
return select.DataType;
}

throw new HibernateException("Unable to determine data type of CASE statement.");
}
set { base.DataType = value; }
}

public IEnumerable<IASTNode> GetResultNodes()
{
for (int i = 0; i < ChildCount; i++)
{
IASTNode whenOrElseClause = GetChild(i);
if (whenOrElseClause.Type == HqlParser.WHEN)
{
// WHEN Child(0) THEN Child(1)
yield return whenOrElseClause.GetChild(1);
}
else if (whenOrElseClause.Type == HqlParser.ELSE)
{
// ELSE Child(0)
yield return whenOrElseClause.GetChild(0);
}
else
{
throw new HibernateException("Was expecting a WHEN or ELSE, but found a: " + whenOrElseClause.Text);
}
}
}

public override void SetScalarColumnText(int i)
{
ColumnHelper.GenerateSingleScalarColumn(ASTFactory, this, i );
}

public IType ExpectedType
{
get => _expectedType;
set
{
_expectedType = value;
foreach (var node in GetResultNodes().OfType<IExpectedTypeAwareNode>())
{
node.ExpectedType = ExpectedType;
}
}
}
}
}