Skip to content

Commit a696b45

Browse files
author
Tomas Lukac
committed
revert implementation to enum equals generator add object equals implementation
1 parent c76bc7d commit a696b45

File tree

7 files changed

+84
-153
lines changed

7 files changed

+84
-153
lines changed

src/NHibernate.Test/Async/Linq/CustomExtensionsExample.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,7 @@ public class CustomExtensionsExampleAsync : LinqTestCase
3232
protected override void Configure(NHibernate.Cfg.Configuration configuration)
3333
{
3434
configuration.LinqToHqlGeneratorsRegistry<MyLinqToHqlGeneratorsRegistry>();
35-
}
36-
37-
[Test]
38-
public async Task CanUseObjectEqualsAsync()
39-
{
40-
var users = await (db.Users.Where(o => ((object) EnumStoredAsString.Medium).Equals(o.NullableEnum1)).ToListAsync());
41-
Assert.That(users.Count, Is.EqualTo(2));
42-
Assert.That(users.All(c => c.NullableEnum1 == EnumStoredAsString.Medium), Is.True);
43-
}
35+
}
4436

4537
[Test(Description = "GH-2963")]
4638
public async Task CanUseComparisonWithExtensionOnMappedPropertyAsync()

src/NHibernate.Test/Linq/CustomExtensionsExample.cs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq.Expressions;
55
using System.Reflection;
66
using System.Text.RegularExpressions;
7-
using NHibernate.DomainModel.Northwind.Entities;
87
using NHibernate.Hql.Ast;
98
using NHibernate.Linq.Functions;
109
using NHibernate.Linq.Visitors;
@@ -19,7 +18,7 @@ public static bool IsLike(this string source, string pattern)
1918
{
2019
pattern = Regex.Escape(pattern);
2120
pattern = pattern.Replace("%", ".*?").Replace("_", ".");
22-
pattern = pattern.Replace(@"\[", "[").Replace(@"\]","]").Replace(@"\^", "^");
21+
pattern = pattern.Replace(@"\[", "[").Replace(@"\]", "]").Replace(@"\^", "^");
2322

2423
return Regex.IsMatch(source, pattern);
2524
}
@@ -30,13 +29,12 @@ public static TimeSpan GetTime(this DateTime dateTime)
3029
}
3130
}
3231

33-
public class MyLinqToHqlGeneratorsRegistry: DefaultLinqToHqlGeneratorsRegistry
32+
public class MyLinqToHqlGeneratorsRegistry : DefaultLinqToHqlGeneratorsRegistry
3433
{
35-
public MyLinqToHqlGeneratorsRegistry():base()
34+
public MyLinqToHqlGeneratorsRegistry() : base()
3635
{
3736
RegisterGenerator(ReflectHelper.GetMethodDefinition(() => MyLinqExtensions.IsLike(null, null)),
3837
new IsLikeGenerator());
39-
RegisterGenerator(ReflectHelper.GetMethodDefinition(() => new object().Equals(null)), new ObjectEqualsGenerator());
4038
RegisterGenerator(ReflectHelper.GetMethodDefinition(() => MyLinqExtensions.GetTime(default(DateTime))), new GetTimeGenerator());
4139
}
4240
}
@@ -58,32 +56,17 @@ public class IsLikeGenerator : BaseHqlGeneratorForMethod
5856
{
5957
public IsLikeGenerator()
6058
{
61-
SupportedMethods = new[] {ReflectHelper.GetMethodDefinition(() => MyLinqExtensions.IsLike(null, null))};
59+
SupportedMethods = new[] { ReflectHelper.GetMethodDefinition(() => MyLinqExtensions.IsLike(null, null)) };
6260
}
6361

64-
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
62+
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
6563
ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
6664
{
6765
return treeBuilder.Like(visitor.Visit(arguments[0]).AsExpression(),
6866
visitor.Visit(arguments[1]).AsExpression());
6967
}
7068
}
7169

72-
public class ObjectEqualsGenerator : BaseHqlGeneratorForMethod
73-
{
74-
public ObjectEqualsGenerator()
75-
{
76-
SupportedMethods = new[] { ReflectHelper.GetMethodDefinition(() => new object().Equals(null)) };
77-
}
78-
79-
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
80-
ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
81-
{
82-
return treeBuilder.Equality(visitor.Visit(targetObject).AsExpression(),
83-
visitor.Visit(arguments[0]).AsExpression());
84-
}
85-
}
86-
8770
[TestFixture]
8871
public class CustomExtensionsExample : LinqTestCase
8972
{
@@ -92,14 +75,6 @@ protected override void Configure(NHibernate.Cfg.Configuration configuration)
9275
configuration.LinqToHqlGeneratorsRegistry<MyLinqToHqlGeneratorsRegistry>();
9376
}
9477

95-
[Test]
96-
public void CanUseObjectEquals()
97-
{
98-
var users = db.Users.Where(o => ((object) EnumStoredAsString.Medium).Equals(o.NullableEnum1)).ToList();
99-
Assert.That(users.Count, Is.EqualTo(2));
100-
Assert.That(users.All(c => c.NullableEnum1 == EnumStoredAsString.Medium), Is.True);
101-
}
102-
10378
[Test(Description = "GH-2963")]
10479
public void CanUseComparisonWithExtensionOnMappedProperty()
10580
{

src/NHibernate.Test/Linq/FunctionTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,17 @@ where item.Gender.Equals(item.Gender)
507507
ObjectDumper.Write(query);
508508
}
509509

510+
511+
[Test]
512+
public void WhereObjectEqual()
513+
{
514+
var query = from item in db.PatientRecords
515+
where ((object) item.Gender).Equals(Gender.Female)
516+
select item;
517+
518+
ObjectDumper.Write(query);
519+
}
520+
510521
[Test]
511522
public void WhereEquatableEqual()
512523
{

src/NHibernate/Linq/ExpressionTransformers/EnumEqualsTransformer.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/NHibernate/Linq/Functions/EqualsGenerator.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ public class EqualsGenerator : BaseHqlGeneratorForMethod
5858
ReflectHelper.GetMethodDefinition<IEquatable<DateTime>>(x => x.Equals(default(DateTime))),
5959
ReflectHelper.GetMethodDefinition<IEquatable<DateTimeOffset>>(x => x.Equals(default(DateTimeOffset))),
6060
ReflectHelper.GetMethodDefinition<IEquatable<TimeSpan>>(x => x.Equals(default(TimeSpan))),
61-
ReflectHelper.GetMethodDefinition<IEquatable<bool>>(x => x.Equals(default(bool)))
61+
ReflectHelper.GetMethodDefinition<IEquatable<bool>>(x => x.Equals(default(bool))),
62+
ReflectHelper.GetMethodDefinition<object>(x => x.Equals(default(object))), // this covers also Enum.Equals
63+
ReflectHelper.GetMethodDefinition<IEquatable<object>>(x => x.Equals(default(object))),
64+
ReflectHelper.GetMethodDefinition<IEquatable<Enum>>(x => x.Equals(default(Enum)))
6265
};
6366

6467
public EqualsGenerator()
@@ -72,7 +75,10 @@ public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
7275
{
7376
Expression lhs = arguments.Count == 1 ? targetObject : arguments[0];
7477
Expression rhs = arguments.Count == 1 ? arguments[0] : arguments[1];
75-
78+
if (lhs.Type.IsEnum)
79+
{
80+
return visitor.Visit(Expression.Equal(lhs, Expression.Convert(rhs, lhs.Type)));
81+
}
7682
return visitor.Visit(Expression.Equal(lhs, rhs));
7783
}
7884
}

src/NHibernate/Linq/NHibernateNodeTypeProvider.cs

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/NHibernate/Linq/NhRelinqQueryParser.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
4+
using System.Linq;
35
using System.Linq.Expressions;
6+
using System.Reflection;
7+
using NHibernate.Engine;
48
using NHibernate.Linq.ExpressionTransformers;
59
using NHibernate.Linq.Visitors;
10+
using NHibernate.Param;
11+
using NHibernate.Util;
612
using Remotion.Linq;
13+
using Remotion.Linq.EagerFetching.Parsing;
714
using Remotion.Linq.Parsing.ExpressionVisitors.Transformation;
815
using Remotion.Linq.Parsing.Structure;
916
using Remotion.Linq.Parsing.Structure.ExpressionTreeProcessors;
17+
using Remotion.Linq.Parsing.Structure.NodeTypeProviders;
1018

1119
namespace NHibernate.Linq
1220
{
@@ -19,7 +27,6 @@ static NhRelinqQueryParser()
1927
var transformerRegistry = ExpressionTransformerRegistry.CreateDefault();
2028
transformerRegistry.Register(new RemoveRedundantCast());
2129
transformerRegistry.Register(new SimplifyCompareTransformer());
22-
transformerRegistry.Register(new EnumEqualsTransformer());
2330

2431
// If needing a compound processor for adding other processing, do not use
2532
// ExpressionTreeParser.CreateDefaultProcessor(transformerRegistry), it would
@@ -85,4 +92,55 @@ internal static Func<Expression, Expression> CreatePreTransformer(IExpressionTra
8592
return new TransformingExpressionTreeProcessor(preTransformerRegistry).Process;
8693
}
8794
}
95+
96+
public class NHibernateNodeTypeProvider : INodeTypeProvider
97+
{
98+
private INodeTypeProvider defaultNodeTypeProvider;
99+
100+
public NHibernateNodeTypeProvider()
101+
{
102+
var methodInfoRegistry = new MethodInfoBasedNodeTypeRegistry();
103+
104+
methodInfoRegistry.Register(
105+
new[] { ReflectHelper.FastGetMethodDefinition(EagerFetchingExtensionMethods.Fetch, default(IQueryable<object>), default(Expression<Func<object, object>>)) },
106+
typeof(FetchOneExpressionNode));
107+
methodInfoRegistry.Register(
108+
new[] { ReflectHelper.FastGetMethodDefinition(EagerFetchingExtensionMethods.FetchLazyProperties, default(IQueryable<object>)) },
109+
typeof(FetchLazyPropertiesExpressionNode));
110+
methodInfoRegistry.Register(
111+
new[] { ReflectHelper.FastGetMethodDefinition(EagerFetchingExtensionMethods.FetchMany, default(IQueryable<object>), default(Expression<Func<object, IEnumerable<object>>>)) },
112+
typeof(FetchManyExpressionNode));
113+
methodInfoRegistry.Register(
114+
new[] { ReflectHelper.FastGetMethodDefinition(EagerFetchingExtensionMethods.ThenFetch, default(INhFetchRequest<object, object>), default(Expression<Func<object, object>>)) },
115+
typeof(ThenFetchOneExpressionNode));
116+
methodInfoRegistry.Register(
117+
new[] { ReflectHelper.FastGetMethodDefinition( EagerFetchingExtensionMethods.ThenFetchMany, default(INhFetchRequest<object, object>), default(Expression<Func<object, IEnumerable<object>>>)) },
118+
typeof(ThenFetchManyExpressionNode));
119+
methodInfoRegistry.Register(
120+
new[]
121+
{
122+
ReflectHelper.FastGetMethodDefinition(LinqExtensionMethods.WithLock, default(IQueryable<object>), default(LockMode)),
123+
ReflectHelper.FastGetMethodDefinition(LinqExtensionMethods.WithLock, default(IEnumerable<object>), default(LockMode))
124+
},
125+
typeof(LockExpressionNode));
126+
127+
var nodeTypeProvider = ExpressionTreeParser.CreateDefaultNodeTypeProvider();
128+
nodeTypeProvider.InnerProviders.Add(methodInfoRegistry);
129+
defaultNodeTypeProvider = nodeTypeProvider;
130+
}
131+
132+
public bool IsRegistered(MethodInfo method)
133+
{
134+
// Avoid Relinq turning IDictionary.Contains into ContainsResultOperator. We do our own processing for that method.
135+
if (method.DeclaringType == typeof(IDictionary) && method.Name == "Contains")
136+
return false;
137+
138+
return defaultNodeTypeProvider.IsRegistered(method);
139+
}
140+
141+
public System.Type GetNodeType(MethodInfo method)
142+
{
143+
return defaultNodeTypeProvider.GetNodeType(method);
144+
}
145+
}
88146
}

0 commit comments

Comments
 (0)