forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandardLinqExtensionMethodGenerator.cs
59 lines (49 loc) · 1.67 KB
/
StandardLinqExtensionMethodGenerator.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
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;
namespace NHibernate.Linq.Functions
{
public class StandardLinqExtensionMethodGenerator : IRuntimeMethodHqlGenerator
{
#region IRuntimeMethodHqlGenerator Members
public bool SupportsMethod(MethodInfo method)
{
return method.GetCustomAttributes(typeof (LinqExtensionMethodAttribute), false).Any();
}
public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
{
return new HqlGeneratorForExtensionMethod((LinqExtensionMethodAttribute) method.GetCustomAttributes(typeof (LinqExtensionMethodAttribute), false).First(), method);
}
#endregion
}
public class HqlGeneratorForExtensionMethod : BaseHqlGeneratorForMethod
{
private readonly string _name;
public HqlGeneratorForExtensionMethod(LinqExtensionMethodAttribute attribute, MethodInfo method)
{
_name = string.IsNullOrEmpty(attribute.Name) ? method.Name : attribute.Name;
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
IEnumerable<HqlExpression> args = visitor.Visit(targetObject)
.Union(arguments.Select(a => visitor.Visit(a)))
.Cast<HqlExpression>();
return treeBuilder.MethodCall(_name, args);
}
}
internal static class UnionExtension
{
public static IEnumerable<HqlTreeNode> Union(this HqlTreeNode first, IEnumerable<HqlTreeNode> rest)
{
yield return first;
foreach (HqlTreeNode x in rest)
{
yield return x;
}
}
}
}