forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionaryGenerator.cs
103 lines (88 loc) · 3.29 KB
/
DictionaryGenerator.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Hql.Ast;
using NHibernate.Linq.Visitors;
using NHibernate.Util;
namespace NHibernate.Linq.Functions
{
public class DictionaryItemGenerator : BaseHqlGeneratorForMethod
{
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
string memberName;
if (VisitorUtil.IsDynamicComponentDictionaryGetter(method, targetObject, arguments, visitor.SessionFactory, out memberName))
{
return treeBuilder.Dot(visitor.Visit(targetObject).AsExpression(), treeBuilder.Ident(memberName));
}
return treeBuilder.Index(visitor.Visit(targetObject).AsExpression(), visitor.Visit(arguments[0]).AsExpression());
}
}
public class DictionaryContainsKeyGenerator : BaseHqlGeneratorForMethod
{
public override bool AllowsNullableReturnType(MethodInfo method) => false;
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.In(visitor.Visit(arguments[0]).AsExpression(), treeBuilder.Indices(visitor.Visit(targetObject).AsExpression()));
}
}
public abstract class GenericDictionaryRuntimeMethodHqlGeneratorBase<TGenerator> : IRuntimeMethodHqlGenerator
where TGenerator : IHqlGeneratorForMethod, new()
{
private readonly IHqlGeneratorForMethod generator = new TGenerator();
protected abstract string MethodName { get; }
public bool SupportsMethod(MethodInfo method)
{
return (method != null) && (method.Name == MethodName) && method.IsMethodOf(typeof(IDictionary<,>));
}
public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
{
return generator;
}
}
public abstract class DictionaryRuntimeMethodHqlGeneratorBase<TGenerator> : IRuntimeMethodHqlGenerator
where TGenerator : IHqlGeneratorForMethod, new()
{
private readonly IHqlGeneratorForMethod generator = new TGenerator();
protected abstract string MethodName { get; }
public bool SupportsMethod(MethodInfo method)
{
return (method != null) && (method.Name == MethodName) && method.IsMethodOf(typeof(IDictionary));
}
public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
{
return generator;
}
}
public class GenericDictionaryContainsKeyRuntimeHqlGenerator : GenericDictionaryRuntimeMethodHqlGeneratorBase<DictionaryContainsKeyGenerator>
{
protected override string MethodName
{
get { return "ContainsKey"; }
}
}
public class DictionaryContainsKeyRuntimeHqlGenerator : DictionaryRuntimeMethodHqlGeneratorBase<DictionaryContainsKeyGenerator>
{
protected override string MethodName
{
get { return "Contains"; }
}
}
public class DictionaryItemRuntimeHqlGenerator : DictionaryRuntimeMethodHqlGeneratorBase<DictionaryItemGenerator>
{
protected override string MethodName
{
get { return "get_Item"; }
}
}
public class GenericDictionaryItemRuntimeHqlGenerator : GenericDictionaryRuntimeMethodHqlGeneratorBase<DictionaryItemGenerator>
{
protected override string MethodName
{
get { return "get_Item"; }
}
}
}