forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNhRelinqQueryParser.cs
146 lines (127 loc) · 6.22 KB
/
NhRelinqQueryParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Engine;
using NHibernate.Linq.ExpressionTransformers;
using NHibernate.Linq.Visitors;
using NHibernate.Param;
using NHibernate.Util;
using Remotion.Linq;
using Remotion.Linq.EagerFetching.Parsing;
using Remotion.Linq.Parsing.ExpressionVisitors.Transformation;
using Remotion.Linq.Parsing.Structure;
using Remotion.Linq.Parsing.Structure.ExpressionTreeProcessors;
using Remotion.Linq.Parsing.Structure.NodeTypeProviders;
namespace NHibernate.Linq
{
public static class NhRelinqQueryParser
{
private static readonly QueryParser QueryParser;
static NhRelinqQueryParser()
{
var transformerRegistry = ExpressionTransformerRegistry.CreateDefault();
transformerRegistry.Register(new RemoveRedundantCast());
transformerRegistry.Register(new SimplifyCompareTransformer());
// If needing a compound processor for adding other processing, do not use
// ExpressionTreeParser.CreateDefaultProcessor(transformerRegistry), it would
// cause NH-3961 again by including a PartialEvaluatingExpressionTreeProcessor.
// Directly instantiate a CompoundExpressionTreeProcessor instead.
var processor = new TransformingExpressionTreeProcessor(transformerRegistry);
var nodeTypeProvider = new NHibernateNodeTypeProvider();
var expressionTreeParser = new ExpressionTreeParser(nodeTypeProvider, processor);
QueryParser = new QueryParser(expressionTreeParser);
}
// Obsolete since v5.3
/// <summary>
/// Applies the minimal transformations required before parametrization,
/// expression key computing and parsing.
/// </summary>
/// <param name="expression">The expression to transform.</param>
/// <returns>The transformed expression.</returns>
[Obsolete("Use overload with PreTransformationParameters parameter")]
public static Expression PreTransform(Expression expression)
{
// In order to keep the old behavior use a DML query mode to skip detecting variables,
// which will then generate parameters for each constant expression
return PreTransform(expression, new PreTransformationParameters(QueryMode.Delete, null)).Expression;
}
/// <summary>
/// Applies the minimal transformations required before parametrization,
/// expression key computing and parsing.
/// </summary>
/// <param name="expression">The expression to transform.</param>
/// <param name="parameters">The parameters used in the transformation process.</param>
/// <returns><see cref="PreTransformationResult"/> that contains the transformed expression.</returns>
public static PreTransformationResult PreTransform(Expression expression, PreTransformationParameters parameters)
{
parameters.EvaluatableExpressionFilter = new NhEvaluatableExpressionFilter(parameters.SessionFactory);
parameters.QueryVariables = new Dictionary<ConstantExpression, QueryVariable>();
var partiallyEvaluatedExpression = NhPartialEvaluatingExpressionVisitor
.EvaluateIndependentSubtrees(expression, parameters);
return new PreTransformationResult(
parameters.PreTransformer.Invoke(partiallyEvaluatedExpression),
parameters.SessionFactory,
parameters.QueryVariables);
}
public static QueryModel Parse(Expression expression)
{
return QueryParser.GetParsedQuery(expression);
}
internal static Func<Expression, Expression> CreatePreTransformer(IExpressionTransformerRegistrar expressionTransformerRegistrar)
{
var preTransformerRegistry = new ExpressionTransformerRegistry();
// NH-3247: must remove .Net compiler char to int conversion before
// parameterization occurs.
preTransformerRegistry.Register(new RemoveCharToIntConversion());
expressionTransformerRegistrar?.Register(preTransformerRegistry);
return new TransformingExpressionTreeProcessor(preTransformerRegistry).Process;
}
}
public class NHibernateNodeTypeProvider : INodeTypeProvider
{
private INodeTypeProvider defaultNodeTypeProvider;
public NHibernateNodeTypeProvider()
{
var methodInfoRegistry = new MethodInfoBasedNodeTypeRegistry();
methodInfoRegistry.Register(
new[] { ReflectHelper.FastGetMethodDefinition(EagerFetchingExtensionMethods.Fetch, default(IQueryable<object>), default(Expression<Func<object, object>>)) },
typeof(FetchOneExpressionNode));
methodInfoRegistry.Register(
new[] { ReflectHelper.FastGetMethodDefinition(EagerFetchingExtensionMethods.FetchLazyProperties, default(IQueryable<object>)) },
typeof(FetchLazyPropertiesExpressionNode));
methodInfoRegistry.Register(
new[] { ReflectHelper.FastGetMethodDefinition(EagerFetchingExtensionMethods.FetchMany, default(IQueryable<object>), default(Expression<Func<object, IEnumerable<object>>>)) },
typeof(FetchManyExpressionNode));
methodInfoRegistry.Register(
new[] { ReflectHelper.FastGetMethodDefinition(EagerFetchingExtensionMethods.ThenFetch, default(INhFetchRequest<object, object>), default(Expression<Func<object, object>>)) },
typeof(ThenFetchOneExpressionNode));
methodInfoRegistry.Register(
new[] { ReflectHelper.FastGetMethodDefinition( EagerFetchingExtensionMethods.ThenFetchMany, default(INhFetchRequest<object, object>), default(Expression<Func<object, IEnumerable<object>>>)) },
typeof(ThenFetchManyExpressionNode));
methodInfoRegistry.Register(
new[]
{
ReflectHelper.FastGetMethodDefinition(LinqExtensionMethods.WithLock, default(IQueryable<object>), default(LockMode)),
ReflectHelper.FastGetMethodDefinition(LinqExtensionMethods.WithLock, default(IEnumerable<object>), default(LockMode))
},
typeof(LockExpressionNode));
var nodeTypeProvider = ExpressionTreeParser.CreateDefaultNodeTypeProvider();
nodeTypeProvider.InnerProviders.Add(methodInfoRegistry);
defaultNodeTypeProvider = nodeTypeProvider;
}
public bool IsRegistered(MethodInfo method)
{
// Avoid Relinq turning IDictionary.Contains into ContainsResultOperator. We do our own processing for that method.
if (method.DeclaringType == typeof(IDictionary) && method.Name == "Contains")
return false;
return defaultNodeTypeProvider.IsRegistered(method);
}
public System.Type GetNodeType(MethodInfo method)
{
return defaultNodeTypeProvider.GetNodeType(method);
}
}
}