Skip to content
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
12 changes: 12 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2437/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ public async Task Get_DateCustomType_NullableDateValueEqualsAsync()
}
}

[Test]
public async Task Get_DateCustomType_NullableDateValueEqualsMethodAsync()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var sessions = await (session.Query<UserSession>().Where(x => x.OpenDate.Value.Equals(DateTime.Now)).ToListAsync());

Assert.That(sessions, Has.Count.EqualTo(10));
}
}

[Test]
public async Task Get_DateTimeCustomType_NullableDateValueEqualsAsync()
{
Expand Down
31 changes: 31 additions & 0 deletions src/NHibernate.Test/Linq/ParameterTypeLocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ public void EqualStringEnumTest()
);
}

[Test]
public void EqualsMethodStringTest()
{
AssertResults(
new Dictionary<string, Predicate<IType>>
{
{"\"London\"", o => o is StringType stringType && stringType.SqlType.Length == 15}
},
db.Orders.Where(o => o.ShippingAddress.City.Equals("London")),
db.Orders.Where(o => "London".Equals(o.ShippingAddress.City)),
db.Orders.Where(o => string.Equals("London", o.ShippingAddress.City)),
db.Orders.Where(o => string.Equals(o.ShippingAddress.City, "London"))
);
}

[Test]
public void ContainsStringEnumTest()
{
Expand Down Expand Up @@ -158,6 +173,22 @@ public void EqualStringTest()
);
}

[Test]
public void CompareToStringTest()
{
AssertResults(
new Dictionary<string, Predicate<IType>>
{
{"1", o => o is Int32Type},
{"\"London\"", o => o is StringType stringType && stringType.SqlType.Length == 15}
},
db.Orders.Where(o => o.ShippingAddress.City.CompareTo("London") > 1),
db.Orders.Where(o => "London".CompareTo(o.ShippingAddress.City) > 1),
db.Orders.Where(o => string.Compare("London", o.ShippingAddress.City) > 1),
db.Orders.Where(o => string.Compare(o.ShippingAddress.City, "London") > 1)
);
}

[Test]
public void EqualEntityTest()
{
Expand Down
12 changes: 12 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2437/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ public void Get_DateCustomType_NullableDateValueEquals()
}
}

[Test]
public void Get_DateCustomType_NullableDateValueEqualsMethod()
{
using (var session = OpenSession())
using (session.BeginTransaction())
{
var sessions = session.Query<UserSession>().Where(x => x.OpenDate.Value.Equals(DateTime.Now)).ToList();

Assert.That(sessions, Has.Count.EqualTo(10));
}
}

[Test]
public void Get_DateTimeCustomType_NullableDateValueEquals()
{
Expand Down
89 changes: 46 additions & 43 deletions src/NHibernate/Linq/Functions/EqualsGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;
Expand All @@ -10,57 +11,59 @@ namespace NHibernate.Linq.Functions
{
public class EqualsGenerator : BaseHqlGeneratorForMethod
{
public EqualsGenerator()
internal static HashSet<MethodInfo> Methods = new HashSet<MethodInfo>
{
SupportedMethods = new[]
{
ReflectHelper.FastGetMethod(string.Equals, default(string), default(string)),
ReflectHelper.GetMethodDefinition<string>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<char>(x => x.Equals(x)),
ReflectHelper.FastGetMethod(string.Equals, default(string), default(string)),
ReflectHelper.GetMethodDefinition<string>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<char>(x => x.Equals(x)),

ReflectHelper.GetMethodDefinition<sbyte>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<byte>(x => x.Equals(x)),

ReflectHelper.GetMethodDefinition<short>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<ushort>(x => x.Equals(x)),

ReflectHelper.GetMethodDefinition<sbyte>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<byte>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<int>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<uint>(x => x.Equals(x)),

ReflectHelper.GetMethodDefinition<short>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<ushort>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<long>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<ulong>(x => x.Equals(x)),

ReflectHelper.GetMethodDefinition<int>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<uint>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<float>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<double>(x => x.Equals(x)),

ReflectHelper.GetMethodDefinition<long>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<ulong>(x => x.Equals(x)),
ReflectHelper.FastGetMethod(decimal.Equals, default(decimal), default(decimal)),
ReflectHelper.GetMethodDefinition<decimal>(x => x.Equals(x)),

ReflectHelper.GetMethodDefinition<float>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<double>(x => x.Equals(x)),
ReflectHelper.FastGetMethod(decimal.Equals, default(decimal), default(decimal)),
ReflectHelper.GetMethodDefinition<decimal>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<Guid>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<DateTime>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<DateTimeOffset>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<TimeSpan>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<bool>(x => x.Equals(default(bool))),

ReflectHelper.GetMethodDefinition<Guid>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<DateTime>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<DateTimeOffset>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<TimeSpan>(x => x.Equals(x)),
ReflectHelper.GetMethodDefinition<bool>(x => x.Equals(default(bool))),
ReflectHelper.GetMethodDefinition<IEquatable<string>>(x => x.Equals(default(string))),
ReflectHelper.GetMethodDefinition<IEquatable<char>>(x => x.Equals(default(char))),
ReflectHelper.GetMethodDefinition<IEquatable<sbyte>>(x => x.Equals(default(sbyte))),
ReflectHelper.GetMethodDefinition<IEquatable<byte>>(x => x.Equals(default(byte))),
ReflectHelper.GetMethodDefinition<IEquatable<short>>(x => x.Equals(default(short))),
ReflectHelper.GetMethodDefinition<IEquatable<ushort>>(x => x.Equals(default(ushort))),
ReflectHelper.GetMethodDefinition<IEquatable<int>>(x => x.Equals(default(int))),
ReflectHelper.GetMethodDefinition<IEquatable<uint>>(x => x.Equals(default(uint))),
ReflectHelper.GetMethodDefinition<IEquatable<long>>(x => x.Equals(default(long))),
ReflectHelper.GetMethodDefinition<IEquatable<ulong>>(x => x.Equals(default(ulong))),
ReflectHelper.GetMethodDefinition<IEquatable<float>>(x => x.Equals(default(float))),
ReflectHelper.GetMethodDefinition<IEquatable<double>>(x => x.Equals(default(double))),
ReflectHelper.GetMethodDefinition<IEquatable<decimal>>(x => x.Equals(default(decimal))),
ReflectHelper.GetMethodDefinition<IEquatable<Guid>>(x => x.Equals(default(Guid))),
ReflectHelper.GetMethodDefinition<IEquatable<DateTime>>(x => x.Equals(default(DateTime))),
ReflectHelper.GetMethodDefinition<IEquatable<DateTimeOffset>>(x => x.Equals(default(DateTimeOffset))),
ReflectHelper.GetMethodDefinition<IEquatable<TimeSpan>>(x => x.Equals(default(TimeSpan))),
ReflectHelper.GetMethodDefinition<IEquatable<bool>>(x => x.Equals(default(bool)))
};

ReflectHelper.GetMethodDefinition<IEquatable<string>>(x => x.Equals(default(string))),
ReflectHelper.GetMethodDefinition<IEquatable<char>>(x => x.Equals(default(char))),
ReflectHelper.GetMethodDefinition<IEquatable<sbyte>>(x => x.Equals(default(sbyte))),
ReflectHelper.GetMethodDefinition<IEquatable<byte>>(x => x.Equals(default(byte))),
ReflectHelper.GetMethodDefinition<IEquatable<short>>(x => x.Equals(default(short))),
ReflectHelper.GetMethodDefinition<IEquatable<ushort>>(x => x.Equals(default(ushort))),
ReflectHelper.GetMethodDefinition<IEquatable<int>>(x => x.Equals(default(int))),
ReflectHelper.GetMethodDefinition<IEquatable<uint>>(x => x.Equals(default(uint))),
ReflectHelper.GetMethodDefinition<IEquatable<long>>(x => x.Equals(default(long))),
ReflectHelper.GetMethodDefinition<IEquatable<ulong>>(x => x.Equals(default(ulong))),
ReflectHelper.GetMethodDefinition<IEquatable<float>>(x => x.Equals(default(float))),
ReflectHelper.GetMethodDefinition<IEquatable<double>>(x => x.Equals(default(double))),
ReflectHelper.GetMethodDefinition<IEquatable<decimal>>(x => x.Equals(default(decimal))),
ReflectHelper.GetMethodDefinition<IEquatable<Guid>>(x => x.Equals(default(Guid))),
ReflectHelper.GetMethodDefinition<IEquatable<DateTime>>(x => x.Equals(default(DateTime))),
ReflectHelper.GetMethodDefinition<IEquatable<DateTimeOffset>>(x => x.Equals(default(DateTimeOffset))),
ReflectHelper.GetMethodDefinition<IEquatable<TimeSpan>>(x => x.Equals(default(TimeSpan))),
ReflectHelper.GetMethodDefinition<IEquatable<bool>>(x => x.Equals(default(bool)))
};
public EqualsGenerator()
{
SupportedMethods = Methods;
}

public override bool AllowsNullableReturnType(MethodInfo method) => false;
Expand Down
12 changes: 12 additions & 0 deletions src/NHibernate/Linq/Visitors/ParameterTypeLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Linq.Expressions;
using NHibernate.Engine;
using NHibernate.Linq.Functions;
using NHibernate.Param;
using NHibernate.Persister.Collection;
using NHibernate.Type;
Expand Down Expand Up @@ -215,6 +216,17 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
: node;
}

if (EqualsGenerator.Methods.Contains(node.Method) || CompareGenerator.IsCompareMethod(node.Method))
{
node = (MethodCallExpression) base.VisitMethodCall(node);
var left = Unwrap(node.Method.IsStatic ? node.Arguments[0] : node.Object);
var right = Unwrap(node.Method.IsStatic ? node.Arguments[1] : node.Arguments[0]);
AddRelatedExpression(node, left, right);
AddRelatedExpression(node, right, left);

return node;
}

return base.VisitMethodCall(node);
}

Expand Down