forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNestedSelectRewriter.cs
284 lines (215 loc) · 10.7 KB
/
NestedSelectRewriter.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Linq.Clauses;
using NHibernate.Linq.GroupBy;
using NHibernate.Linq.Visitors;
using NHibernate.Util;
using Remotion.Linq;
using Remotion.Linq.Clauses;
using Remotion.Linq.Clauses.Expressions;
namespace NHibernate.Linq.NestedSelects
{
static class NestedSelectRewriter
{
private static readonly MethodInfo CastMethod =
ReflectHelper.FastGetMethod(Enumerable.Cast<object[]>, default(IEnumerable));
private static readonly MethodInfo GroupByMethod =
ReflectHelper.FastGetMethod(Enumerable.GroupBy, default(IEnumerable<object[]>), default(Func<object[], Tuple>), default(Func<object[], Tuple>));
private static readonly MethodInfo WhereMethod =
ReflectHelper.FastGetMethod(Enumerable.Where, default(IEnumerable<Tuple>), default(Func<Tuple, bool>));
private static readonly MethodInfo ObjectReferenceEquals =
ReflectHelper.FastGetMethod(ReferenceEquals, default(object), default(object));
private static readonly PropertyInfo IGroupingKeyProperty = (PropertyInfo)
ReflectHelper.GetProperty<IGrouping<Tuple, Tuple>, Tuple>(g => g.Key);
public static void ReWrite(QueryModel queryModel, ISessionFactory sessionFactory)
{
var nsqmv = new NestedSelectDetector(sessionFactory);
nsqmv.Visit(queryModel.SelectClause.Selector);
if (!nsqmv.HasSubqueries)
return;
var elementExpression = new List<ExpressionHolder>();
var group = Expression.Parameter(typeof (IGrouping<Tuple, Tuple>), "g");
var replacements = new Dictionary<Expression, Expression>();
foreach (var expression in nsqmv.Expressions)
{
var processed = ProcessExpression(queryModel, sessionFactory, expression, elementExpression, group);
if (processed != null)
replacements.Add(expression, processed);
}
if (!replacements.Any())
return;
var key = Expression.Property(group, IGroupingKeyProperty);
var expressions = new List<ExpressionHolder>();
var identifier = GetIdentifier(sessionFactory, new QuerySourceReferenceExpression(queryModel.MainFromClause));
var rewriter = new SelectClauseRewriter(key, expressions, identifier, replacements);
var resultSelector = rewriter.Visit(queryModel.SelectClause.Selector);
elementExpression.AddRange(expressions);
var keySelector = CreateSelector(elementExpression, 0);
var elementSelector = CreateSelector(elementExpression, 1);
var input = Expression.Parameter(typeof (IEnumerable<object>), "input");
var lambda = Expression.Lambda(
Expression.Call(GroupByMethod,
Expression.Call(CastMethod, input),
keySelector,
elementSelector),
input);
queryModel.ResultOperators.Add(new ClientSideSelect2(lambda));
queryModel.ResultOperators.Add(new ClientSideSelect(Expression.Lambda(resultSelector, @group)));
var initializers = elementExpression.Select(e => ConvertToObject(e.Expression));
queryModel.SelectClause.Selector = Expression.NewArrayInit(typeof (object), initializers);
}
private static Expression ProcessExpression(QueryModel queryModel, ISessionFactory sessionFactory, Expression expression, List<ExpressionHolder> elementExpression, ParameterExpression @group)
{
var memberExpression = expression as MemberExpression;
if (memberExpression != null)
return ProcessMemberExpression(sessionFactory, elementExpression, queryModel, @group, memberExpression);
var subQueryExpression = expression as SubQueryExpression;
if (subQueryExpression != null)
return ProcessSubquery(sessionFactory, elementExpression, queryModel, @group, subQueryExpression.QueryModel);
return null;
}
private static Expression ProcessSubquery(ISessionFactory sessionFactory, ICollection<ExpressionHolder> elementExpression, QueryModel queryModel, Expression @group, QueryModel subQueryModel)
{
var resultTypeOverride = subQueryModel.ResultTypeOverride;
if (resultTypeOverride != null && !resultTypeOverride.IsArray && !IsEnumerableOfT(resultTypeOverride))
return null;
SubQueryFromClauseFlattener.ReWrite(subQueryModel);
var subQueryMainFromClause = subQueryModel.MainFromClause;
var restrictions = subQueryModel.BodyClauses
.OfType<WhereClause>()
.Select(w => new NhWithClause(w.Predicate));
var join = new NhJoinClause(subQueryMainFromClause.ItemName,
subQueryMainFromClause.ItemType,
subQueryMainFromClause.FromExpression,
restrictions);
queryModel.BodyClauses.Add(@join);
var visitor = new SwapQuerySourceVisitor(subQueryMainFromClause, @join);
queryModel.TransformExpressions(visitor.Swap);
var selector = subQueryModel.SelectClause.Selector;
var collectionType = subQueryModel.GetResultType();
var elementType = selector.Type;
var source = new QuerySourceReferenceExpression(@join);
return BuildSubCollectionQuery(sessionFactory, elementExpression, @group, source, selector, elementType, collectionType);
}
private static bool IsEnumerableOfT(System.Type type)
{
if (!type.IsGenericType)
return false;
var typeDef = type.GetGenericTypeDefinition();
return typeDef == typeof(IEnumerable<>) || typeDef == typeof(IQueryable<>);
}
private static Expression ProcessMemberExpression(ISessionFactory sessionFactory, ICollection<ExpressionHolder> elementExpression, QueryModel queryModel, Expression @group, Expression memberExpression)
{
var join = new NhJoinClause(new NameGenerator(queryModel).GetNewName(),
GetElementType(memberExpression.Type),
memberExpression);
queryModel.BodyClauses.Add(@join);
var source = new QuerySourceReferenceExpression(@join);
return BuildSubCollectionQuery(sessionFactory, elementExpression, @group, source, source, source.Type, memberExpression.Type);
}
private static Expression BuildSubCollectionQuery(ISessionFactory sessionFactory, ICollection<ExpressionHolder> expressions, Expression @group, Expression source, Expression select, System.Type elementType, System.Type collectionType)
{
var predicate = MakePredicate(expressions.Count);
var identifier = GetIdentifier(sessionFactory, source);
var selector = MakeSelector(expressions, @select, identifier);
return SubCollectionQuery(collectionType, elementType, @group, predicate, selector);
}
private static LambdaExpression MakeSelector(ICollection<ExpressionHolder> elementExpression, Expression @select, Expression identifier)
{
var parameter = Expression.Parameter(typeof (Tuple), "value");
var rewriter = new SelectClauseRewriter(parameter, elementExpression, identifier, 1, new Dictionary<Expression, Expression>());
var selectorBody = rewriter.Visit(@select);
return Expression.Lambda(selectorBody, parameter);
}
private static Expression SubCollectionQuery(System.Type collectionType, System.Type elementType, Expression source, Expression predicate, Expression selector)
{
// source.Where(predicate).Select(selector).ToList();
var selectMethod = ReflectionCache.EnumerableMethods.SelectDefinition.MakeGenericMethod(new[] { typeof(Tuple), elementType });
var select = Expression.Call(selectMethod,
Expression.Call(WhereMethod, source, predicate),
selector);
if (collectionType.IsArray)
{
var toArrayMethod = ReflectionCache.EnumerableMethods.ToArrayDefinition.MakeGenericMethod(new[] { elementType });
var array = Expression.Call(toArrayMethod, @select);
return array;
}
var constructor = GetCollectionConstructor(collectionType, elementType);
if (constructor != null)
return Expression.New(constructor, (Expression) @select);
var toListMethod = ReflectionCache.EnumerableMethods.ToListDefinition.MakeGenericMethod(new[] { elementType });
return Expression.Call(Expression.Call(toListMethod, @select),
"AsReadonly",
System.Type.EmptyTypes);
}
private static ConstructorInfo GetCollectionConstructor(System.Type collectionType, System.Type elementType)
{
if (collectionType.IsInterface)
{
if (collectionType.IsGenericType && collectionType.GetGenericTypeDefinition() == typeof(ISet<>))
{
return typeof(HashSet<>).MakeGenericType(elementType).GetConstructor(new[] { typeof(IEnumerable<>).MakeGenericType(elementType) });
}
return null;
}
return collectionType.GetConstructor(new[] { typeof(IEnumerable<>).MakeGenericType(elementType) });
}
private static LambdaExpression MakePredicate(int index)
{
// t => Not(ReferenceEquals(t.Items[index], null))
var t = Expression.Parameter(typeof (Tuple), "t");
return Expression.Lambda(
Expression.Not(
Expression.Call(ObjectReferenceEquals,
ArrayIndex(Expression.Property(t, Tuple.ItemsProperty), index),
Expression.Constant(null))),
t);
}
private static Expression GetIdentifier(ISessionFactory sessionFactory, Expression expression)
{
if (expression.Type.IsPrimitive || expression.Type == typeof(string))
return expression;
var classMetadata = sessionFactory.GetClassMetadata(expression.Type);
if (classMetadata == null)
return Expression.Constant(null);
var propertyName=classMetadata.IdentifierPropertyName;
NHibernate.Type.EmbeddedComponentType componentType;
if (propertyName == null && (componentType=classMetadata.IdentifierType as NHibernate.Type.EmbeddedComponentType)!=null)
{
//The identifier is an embedded composite key. We only need one property from it for a null check
propertyName = componentType.PropertyNames.First();
}
return ConvertToObject(Expression.PropertyOrField(expression, propertyName));
}
private static LambdaExpression CreateSelector(IEnumerable<ExpressionHolder> expressions, int tuple)
{
var parameter = Expression.Parameter(typeof (object[]), "x");
var initializers = expressions.Select((x, index) => new { x.Tuple, index})
.Where(x => x.Tuple == tuple)
.Select(x => ArrayIndex(parameter, x.index));
var newArrayInit = Expression.NewArrayInit(typeof (object), initializers);
return Expression.Lambda(
Expression.New(Tuple.Constructor, newArrayInit),
parameter);
}
private static Expression ArrayIndex(Expression param, int value)
{
return Expression.ArrayIndex(param, Expression.Constant(value));
}
private static Expression ConvertToObject(Expression expression)
{
return Expression.Convert(expression, typeof (object));
}
private static System.Type GetElementType(System.Type type)
{
var elementType = ReflectHelper.GetCollectionElementType(type);
if (elementType == null)
throw new NotSupportedException("Unknown collection type " + type.FullName);
return elementType;
}
}
}