Skip to content

Commit cf7247a

Browse files
ngbrownhazzik
authored andcommitted
Upgrade to ReLinq 2.2.0 (#696)
- Use the new Remotion.Linq AsQueryableResultOperator - Revert workaround in CompareGenerator for VB - Add debug info to PolymorphicQuerySourceDetector
1 parent 6d98344 commit cf7247a

File tree

8 files changed

+94
-108
lines changed

8 files changed

+94
-108
lines changed

src/NHibernate/Hql/Ast/ANTLR/PolymorphicQuerySourceDetector.cs

Lines changed: 71 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,85 +8,86 @@
88

99
namespace NHibernate.Hql.Ast.ANTLR
1010
{
11-
internal class PolymorphicQuerySourceDetector
12-
{
13-
private readonly ISessionFactoryImplementor _sfi;
14-
private readonly Dictionary<IASTNode, IASTNode[]> _map = new Dictionary<IASTNode, IASTNode[]>();
15-
private readonly SessionFactoryHelper _sessionFactoryHelper;
11+
internal class PolymorphicQuerySourceDetector
12+
{
13+
private readonly ISessionFactoryImplementor _sfi;
14+
private readonly Dictionary<IASTNode, IASTNode[]> _map = new Dictionary<IASTNode, IASTNode[]>();
15+
private readonly SessionFactoryHelper _sessionFactoryHelper;
1616

17-
public PolymorphicQuerySourceDetector(ISessionFactoryImplementor sfi)
18-
{
19-
_sfi = sfi;
20-
_sessionFactoryHelper = new SessionFactoryHelper(sfi);
21-
}
17+
public PolymorphicQuerySourceDetector(ISessionFactoryImplementor sfi)
18+
{
19+
_sfi = sfi;
20+
_sessionFactoryHelper = new SessionFactoryHelper(sfi);
21+
}
2222

23-
public Dictionary<IASTNode, IASTNode[]> Process(IASTNode tree)
24-
{
25-
foreach (var querySource in new QuerySourceDetector(tree).LocateQuerySources())
26-
{
27-
var className = GetClassName(querySource);
28-
string[] implementors = _sfi.GetImplementors(className);
29-
AddImplementorsToMap(querySource, className, implementors);
30-
}
23+
public Dictionary<IASTNode, IASTNode[]> Process(IASTNode tree)
24+
{
25+
foreach (var querySource in new QuerySourceDetector(tree).LocateQuerySources())
26+
{
27+
var className = GetClassName(querySource);
28+
string[] implementors = _sfi.GetImplementors(className);
29+
AddImplementorsToMap(querySource, className, implementors);
30+
}
3131

32-
return _map;
33-
}
32+
return _map;
33+
}
3434

35-
private void AddImplementorsToMap(IASTNode querySource, string className, string[] implementors)
36-
{
37-
if (implementors.Length == 1 && implementors[0] == className)
38-
{
39-
// No need to change things
40-
return;
41-
}
35+
private void AddImplementorsToMap(IASTNode querySource, string className, string[] implementors)
36+
{
37+
if (implementors.Length == 1 && implementors[0] == className)
38+
{
39+
// No need to change things
40+
return;
41+
}
4242

43-
_map.Add(querySource,
44-
implementors.Select(implementor => MakeIdent(querySource, implementor)).ToArray());
45-
}
43+
_map.Add(querySource,
44+
implementors.Select(implementor => MakeIdent(querySource, implementor)).ToArray());
45+
}
4646

47-
private static string GetClassName(IASTNode querySource)
48-
{
49-
switch (querySource.Type)
50-
{
51-
case HqlSqlWalker.IDENT:
52-
return querySource.Text;
53-
case HqlSqlWalker.DOT:
54-
return BuildPath(querySource);
55-
}
47+
private static string GetClassName(IASTNode querySource)
48+
{
49+
switch (querySource.Type)
50+
{
51+
case HqlSqlWalker.IDENT:
52+
return querySource.Text;
53+
case HqlSqlWalker.DOT:
54+
return BuildPath(querySource);
55+
default:
56+
// TODO
57+
throw new NotSupportedException($"{querySource.ToString()} {querySource.ToStringTree()}");
58+
}
5659

57-
// TODO
58-
throw new NotSupportedException();
59-
}
60+
}
6061

61-
private static IASTNode MakeIdent(IASTNode source, string text)
62-
{
63-
var ident = source.DupNode();
64-
ident.Type = HqlSqlWalker.IDENT;
65-
ident.Text = text;
66-
return ident;
67-
}
62+
private static IASTNode MakeIdent(IASTNode source, string text)
63+
{
64+
var ident = source.DupNode();
65+
ident.Type = HqlSqlWalker.IDENT;
66+
ident.Text = text;
67+
return ident;
68+
}
6869

69-
private static string BuildPath(IASTNode node)
70-
{
71-
var sb = new StringBuilder();
72-
BuildPath(node, sb);
73-
return sb.ToString();
74-
}
70+
private static string BuildPath(IASTNode node)
71+
{
72+
var sb = new StringBuilder();
73+
BuildPath(node, sb);
74+
return sb.ToString();
75+
}
7576

76-
private static void BuildPath(IASTNode node, StringBuilder sb)
77-
{
78-
if (node.Type == HqlSqlWalker.DOT)
79-
{
80-
BuildPath(node.GetChild(0), sb);
77+
private static void BuildPath(IASTNode node, StringBuilder sb)
78+
{
79+
if (node.Type == HqlSqlWalker.DOT)
80+
{
81+
BuildPath(node.GetChild(0), sb);
8182

82-
sb.Append('.');
83-
sb.Append(node.GetChild(1).Text);
84-
}
85-
else
86-
{
87-
sb.Append(node.Text);
88-
}
89-
}
83+
sb.Append('.');
84+
sb.Append(node.GetChild(1).Text);
85+
}
86+
else
87+
{
88+
sb.Append(node.Text);
89+
}
90+
}
9091

91-
}
92-
}
92+
}
93+
}

src/NHibernate/Linq/Functions/CompareGenerator.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,9 @@ internal static bool IsCompareMethod(MethodInfo methodInfo)
4646
return true;
4747

4848
// This is .Net 4 only, and in the System.Data.Services assembly, which we don't depend directly on.
49-
if (methodInfo != null && methodInfo.Name == "Compare" &&
49+
return methodInfo != null && methodInfo.Name == "Compare" &&
5050
methodInfo.DeclaringType != null &&
51-
methodInfo.DeclaringType.FullName == "System.Data.Services.Providers.DataServiceProviderMethods")
52-
return true;
53-
54-
if (methodInfo != null && methodInfo.Name == "CompareString" &&
55-
methodInfo.DeclaringType != null &&
56-
(methodInfo.DeclaringType.FullName == "Microsoft.VisualBasic.CompilerServices.EmbeddedOperators"))
57-
return true;
58-
59-
return false;
51+
methodInfo.DeclaringType.FullName == "System.Data.Services.Providers.DataServiceProviderMethods";
6052
}
6153

6254

src/NHibernate/Linq/NhRelinqQueryParser.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,6 @@ public NHibernateNodeTypeProvider()
8484
new[] { ReflectHelper.GetMethodDefinition(() => EagerFetchingExtensionMethods.ThenFetchMany<object, object, object>(null, null)) },
8585
typeof(ThenFetchManyExpressionNode));
8686

87-
methodInfoRegistry.Register(
88-
new[]
89-
{
90-
ReflectHelper.GetMethodDefinition(() => Queryable.AsQueryable(null)),
91-
ReflectHelper.GetMethodDefinition(() => Queryable.AsQueryable<object>(null)),
92-
}, typeof(AsQueryableExpressionNode)
93-
);
94-
9587
var nodeTypeProvider = ExpressionTreeParser.CreateDefaultNodeTypeProvider();
9688
nodeTypeProvider.InnerProviders.Add(methodInfoRegistry);
9789
defaultNodeTypeProvider = nodeTypeProvider;
@@ -111,20 +103,4 @@ public System.Type GetNodeType(MethodInfo method)
111103
return defaultNodeTypeProvider.GetNodeType(method);
112104
}
113105
}
114-
115-
public class AsQueryableExpressionNode : MethodCallExpressionNodeBase
116-
{
117-
public AsQueryableExpressionNode(MethodCallExpressionParseInfo parseInfo) : base(parseInfo)
118-
{
119-
}
120-
121-
public override Expression Resolve(ParameterExpression inputParameter, Expression expressionToBeResolved, ClauseGenerationContext clauseGenerationContext)
122-
{
123-
return Source.Resolve(inputParameter, expressionToBeResolved, clauseGenerationContext);
124-
}
125-
126-
protected override void ApplyNodeSpecificSemantics(QueryModel queryModel, ClauseGenerationContext clauseGenerationContext)
127-
{
128-
}
129-
}
130106
}

src/NHibernate/Linq/ReWriters/ResultOperatorRewriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ private class ResultOperatorExpressionRewriter : RelinqExpressionVisitor
6666
{
6767
typeof(FetchRequestBase),
6868
typeof(OfTypeResultOperator),
69-
typeof(CastResultOperator)
69+
typeof(CastResultOperator),
70+
typeof(AsQueryableResultOperator),
7071
};
7172

7273
private readonly List<ResultOperatorBase> resultOperators = new List<ResultOperatorBase>();

src/NHibernate/Linq/Visitors/NhQueryModelVisitorBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using NHibernate.Linq.Clauses;
22
using Remotion.Linq;
3+
using Remotion.Linq.Clauses.ResultOperators;
34

45
namespace NHibernate.Linq.Visitors
56
{
6-
public class NhQueryModelVisitorBase : QueryModelVisitorBase, INhQueryModelVisitor
7+
public class NhQueryModelVisitorBase : QueryModelVisitorBase, INhQueryModelVisitor, AsQueryableResultOperator.ISupportedByIQueryModelVistor
78
{
89
public virtual void VisitNhHavingClause(NhHavingClause havingClause, QueryModel queryModel, int index)
910
{

src/NHibernate/Linq/Visitors/QueryModelVisitor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static QueryModelVisitor()
130130
ResultOperatorMap.Add<FetchManyRequest, ProcessFetchMany>();
131131
ResultOperatorMap.Add<OfTypeResultOperator, ProcessOfType>();
132132
ResultOperatorMap.Add<CastResultOperator, ProcessCast>();
133+
ResultOperatorMap.Add<AsQueryableResultOperator, ProcessAsQueryable>();
133134
}
134135

135136
private QueryModelVisitor(VisitorParameters visitorParameters, bool root, QueryModel queryModel,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Remotion.Linq.Clauses.ResultOperators;
2+
3+
namespace NHibernate.Linq.Visitors.ResultOperatorProcessors
4+
{
5+
public class ProcessAsQueryable : IResultOperatorProcessor<AsQueryableResultOperator>
6+
{
7+
public void Process(AsQueryableResultOperator resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree)
8+
{
9+
// Ignore AsQueryable
10+
// It could be used to detect accidental usage of IEnumerable-based extension methods
11+
// on query roots, resulting in in-memory queries instead of database queries.
12+
}
13+
}
14+
}

src/NHibernate/NHibernate.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
</PackageReference>
4242
<PackageReference Include="Antlr3.Runtime" Version="[3.5.1, 4.0)" />
4343
<PackageReference Include="Iesi.Collections" Version="[4.0.4, 5.0)" />
44-
<PackageReference Include="Remotion.Linq" Version="[2.1.2, 3.0)" />
45-
<PackageReference Include="Remotion.Linq.EagerFetching" Version="[2.1.0, 3.0)" />
44+
<PackageReference Include="Remotion.Linq" Version="[2.2.0, 3.0)" />
45+
<PackageReference Include="Remotion.Linq.EagerFetching" Version="[2.2.0, 3.0)" />
4646
</ItemGroup>
4747

4848
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">

0 commit comments

Comments
 (0)