forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringGenerator.cs
249 lines (220 loc) · 8.65 KB
/
StringGenerator.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;
using NHibernate.Hql.Ast;
using NHibernate.Linq.Visitors;
namespace NHibernate.Linq.Functions
{
public class LengthGenerator : BaseHqlGeneratorForProperty
{
public LengthGenerator()
{
SupportedProperties = new[] { ReflectionHelper.GetProperty((string x) => x.Length) };
}
public override HqlTreeNode BuildHql(MemberInfo member, Expression expression, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.MethodCall("length", visitor.Visit(expression).AsExpression());
}
}
public class StartsWithGenerator : BaseHqlGeneratorForMethod
{
public StartsWithGenerator()
{
SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition<string>(x => x.StartsWith(null)) };
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.Like(
visitor.Visit(targetObject).AsExpression(),
treeBuilder.Concat(
visitor.Visit(arguments[0]).AsExpression(),
treeBuilder.Constant("%")));
}
}
public class EndsWithGenerator : BaseHqlGeneratorForMethod
{
public EndsWithGenerator()
{
SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition<string>(x => x.EndsWith(null)) };
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.Like(
visitor.Visit(targetObject).AsExpression(),
treeBuilder.Concat(
treeBuilder.Constant("%"),
visitor.Visit(arguments[0]).AsExpression()));
}
}
public class ContainsGenerator : BaseHqlGeneratorForMethod
{
public ContainsGenerator()
{
SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition<string>(x => x.Contains(null)) };
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.Like(
visitor.Visit(targetObject).AsExpression(),
treeBuilder.Concat(
treeBuilder.Constant("%"),
visitor.Visit(arguments[0]).AsExpression(),
treeBuilder.Constant("%")));
}
}
public class ToLowerGenerator : BaseHqlGeneratorForMethod
{
public ToLowerGenerator()
{
SupportedMethods = new[]
{
ReflectionHelper.GetMethodDefinition<string>(x => x.ToLower()),
ReflectionHelper.GetMethodDefinition<string>(x => x.ToLowerInvariant())
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.MethodCall("lower", visitor.Visit(targetObject).AsExpression());
}
}
public class ToUpperGenerator : BaseHqlGeneratorForMethod
{
public ToUpperGenerator()
{
SupportedMethods = new[]
{
ReflectionHelper.GetMethodDefinition<string>(x => x.ToUpper()),
ReflectionHelper.GetMethodDefinition<string>(x => x.ToUpperInvariant()),
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.MethodCall("upper", visitor.Visit(targetObject).AsExpression());
}
}
public class SubStringGenerator : BaseHqlGeneratorForMethod
{
public SubStringGenerator()
{
SupportedMethods = new[]
{
ReflectionHelper.GetMethodDefinition<string>(s => s.Substring(0)),
ReflectionHelper.GetMethodDefinition<string>(s => s.Substring(0, 0))
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
var stringExpr = visitor.Visit(targetObject).AsExpression();
var start = treeBuilder.Add(visitor.Visit(arguments[0]).AsExpression(), treeBuilder.Constant(1));
if (arguments.Count == 1)
return treeBuilder.MethodCall("substring", stringExpr, start);
var length = visitor.Visit(arguments[1]).AsExpression();
return treeBuilder.MethodCall("substring", stringExpr, start, length);
}
}
public class IndexOfGenerator : BaseHqlGeneratorForMethod
{
public IndexOfGenerator()
{
SupportedMethods = new[]
{
ReflectionHelper.GetMethodDefinition<string>(s => s.IndexOf(' ')),
ReflectionHelper.GetMethodDefinition<string>(s => s.IndexOf(" ")),
ReflectionHelper.GetMethodDefinition<string>(s => s.IndexOf(' ', 0)),
ReflectionHelper.GetMethodDefinition<string>(s => s.IndexOf(" ", 0))
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
if (arguments.Count == 1)
{
return treeBuilder.MethodCall("locate",
visitor.Visit(arguments[0]).AsExpression(),
visitor.Visit(targetObject).AsExpression(),
treeBuilder.Constant(0));
}
return treeBuilder.MethodCall("locate",
visitor.Visit(arguments[0]).AsExpression(),
visitor.Visit(targetObject).AsExpression(),
visitor.Visit(arguments[1]).AsExpression());
}
}
public class ReplaceGenerator : BaseHqlGeneratorForMethod
{
public ReplaceGenerator()
{
SupportedMethods = new[]
{
ReflectionHelper.GetMethodDefinition<string>(s => s.Replace(' ', ' ')),
ReflectionHelper.GetMethodDefinition<string>(s => s.Replace("", ""))
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.MethodCall("replace",
visitor.Visit(targetObject).AsExpression(),
visitor.Visit(arguments[0]).AsExpression(),
visitor.Visit(arguments[1]).AsExpression());
}
}
public class TrimGenerator : BaseHqlGeneratorForMethod
{
public TrimGenerator()
{
SupportedMethods = new[]
{
ReflectionHelper.GetMethodDefinition<string>(s => s.Trim()),
ReflectionHelper.GetMethodDefinition<string>(s => s.Trim('a')),
ReflectionHelper.GetMethodDefinition<string>(s => s.TrimStart('a')),
ReflectionHelper.GetMethodDefinition<string>(s => s.TrimEnd('a'))
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
string trimWhere;
if (method.Name == "TrimStart")
trimWhere = "leading";
else if (method.Name == "TrimEnd")
trimWhere = "trailing";
else
trimWhere = "both";
string trimChars = "";
if (method.GetParameters().Length > 0)
foreach (char c in (char[])((ConstantExpression)arguments[0]).Value)
trimChars += c;
if (trimChars == "")
{
return treeBuilder.MethodCall("trim", treeBuilder.Ident(trimWhere), treeBuilder.Ident("from"), visitor.Visit(targetObject).AsExpression());
}
else
{
return treeBuilder.MethodCall("trim", treeBuilder.Ident(trimWhere), treeBuilder.Constant(trimChars), treeBuilder.Ident("from"), visitor.Visit(targetObject).AsExpression());
}
}
}
public class ToStringRuntimeMethodHqlGenerator : IRuntimeMethodHqlGenerator
{
private readonly ToStringHqlGeneratorForMethod generator = new ToStringHqlGeneratorForMethod();
public bool SupportsMethod(MethodInfo method)
{
return method != null && method.Name == "ToString" && method.GetBaseDefinition().DeclaringType == typeof(object);
}
public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
{
return generator;
}
}
public class ToStringHqlGeneratorForMethod : IHqlGeneratorForMethod
{
public IEnumerable<MethodInfo> SupportedMethods
{
get { throw new NotSupportedException(); }
}
public HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
return treeBuilder.MethodCall("str", visitor.Visit(targetObject).AsExpression());
}
}
}