-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathQueryableGenerator.cs
189 lines (163 loc) · 5.63 KB
/
QueryableGenerator.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Hql.Ast;
using NHibernate.Linq.Visitors;
using NHibernate.Util;
namespace NHibernate.Linq.Functions
{
public class AnyHqlGenerator : BaseHqlGeneratorForMethod
{
public AnyHqlGenerator()
{
SupportedMethods = new[]
{
ReflectionCache.QueryableMethods.AnyDefinition,
ReflectionCache.QueryableMethods.AnyWithPredicateDefinition,
ReflectHelper.FastGetMethodDefinition(Enumerable.Any, default(IEnumerable<object>)),
ReflectHelper.FastGetMethodDefinition(Enumerable.Any, default(IEnumerable<object>), default(Func<object, bool>))
};
}
public override bool AllowsNullableReturnType(MethodInfo method) => false;
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
HqlAlias alias = null;
HqlWhere where = null;
if (arguments.Count > 1)
{
var expr = (LambdaExpression) arguments[1];
alias = treeBuilder.Alias(expr.Parameters[0].Name);
where = treeBuilder.Where(visitor.Visit(arguments[1]).AsExpression());
}
return treeBuilder.Exists(
treeBuilder.Query(
treeBuilder.SelectFrom(
treeBuilder.From(
treeBuilder.Range(
visitor.Visit(arguments[0]),
alias)
)
),
where));
}
}
public class AllHqlGenerator : BaseHqlGeneratorForMethod
{
public AllHqlGenerator()
{
SupportedMethods = new[]
{
ReflectionCache.QueryableMethods.AllDefinition,
ReflectHelper.FastGetMethodDefinition(Enumerable.All, default(IEnumerable<object>), default(Func<object, bool>))
};
}
public override bool AllowsNullableReturnType(MethodInfo method) => false;
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
// All has two arguments. Arg 1 is the source and arg 2 is the predicate
var predicate = (LambdaExpression) arguments[1];
return treeBuilder.BooleanNot(
treeBuilder.Exists(
treeBuilder.Query(
treeBuilder.SelectFrom(
treeBuilder.From(
treeBuilder.Range(
visitor.Visit(arguments[0]),
treeBuilder.Alias(predicate.Parameters[0].Name))
)
),
treeBuilder.Where(
treeBuilder.BooleanNot(visitor.Visit(arguments[1]).ToBooleanExpression())
)
)
)
);
}
}
public class MinHqlGenerator : BaseHqlGeneratorForMethod
{
public MinHqlGenerator()
{
SupportedMethods = new[]
{
ReflectionCache.QueryableMethods.MinDefinition,
ReflectionCache.EnumerableMethods.MinDefinition
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.Min(visitor.Visit(arguments[1]).AsExpression());
}
}
public class MaxHqlGenerator : BaseHqlGeneratorForMethod
{
public MaxHqlGenerator()
{
SupportedMethods = new[]
{
ReflectionCache.QueryableMethods.MaxDefinition,
ReflectionCache.EnumerableMethods.MaxDefinition,
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.Max(visitor.Visit(arguments[1]).AsExpression());
}
}
public class CollectionContainsRuntimeHqlGenerator : IRuntimeMethodHqlGenerator
{
private readonly IHqlGeneratorForMethod containsGenerator = new CollectionContainsGenerator();
#region IRuntimeMethodHqlGenerator Members
public bool SupportsMethod(MethodInfo method)
{
// the check about the name is to make things a little be fasters
return method != null && method.Name == "Contains" && method.IsMethodOf(typeof(ICollection<>));
}
public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
{
return containsGenerator;
}
#endregion
}
public class CollectionContainsGenerator : BaseHqlGeneratorForMethod
{
public CollectionContainsGenerator()
{
SupportedMethods = new[]
{
ReflectHelper.FastGetMethodDefinition(Queryable.Contains, default(IQueryable<object>), default(object)),
ReflectHelper.FastGetMethodDefinition(Enumerable.Contains, default(IEnumerable<object>), default(object))
};
}
public override bool AllowsNullableReturnType(MethodInfo method) => false;
/// <inheritdoc />
public override bool TryGetCollectionParameter(MethodCallExpression expression, out ConstantExpression collectionParameter)
{
var argument = expression.Method.IsStatic ? expression.Arguments[0] : expression.Object;
collectionParameter = argument as ConstantExpression;
return collectionParameter != null;
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
// TODO - alias generator
HqlAlias alias = treeBuilder.Alias("x");
ParameterExpression param = Expression.Parameter(targetObject.Type, "x");
HqlWhere where = treeBuilder.Where(visitor.Visit(Expression.Lambda(
Expression.Equal(param, arguments[0]), param))
.AsExpression());
return treeBuilder.Exists(
treeBuilder.Query(
treeBuilder.SelectFrom(
treeBuilder.From(
treeBuilder.Range(
visitor.Visit(targetObject),
alias)
)
),
where));
}
}
}