forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringGenerator.cs
312 lines (275 loc) · 10.4 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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 LikeGenerator : IHqlGeneratorForMethod, IRuntimeMethodHqlGenerator
{
public IEnumerable<MethodInfo> SupportedMethods
{
get { throw new NotImplementedException(); }
}
public HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments,
HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
if (arguments.Count == 2)
{
return treeBuilder.Like(
visitor.Visit(arguments[0]).AsExpression(),
visitor.Visit(arguments[1]).AsExpression());
}
if (arguments[2].NodeType == ExpressionType.Constant)
{
var escapeCharExpression = (ConstantExpression)arguments[2];
return treeBuilder.Like(
visitor.Visit(arguments[0]).AsExpression(),
visitor.Visit(arguments[1]).AsExpression(),
treeBuilder.Constant(escapeCharExpression.Value));
}
throw new ArgumentException("The escape character must be specified as literal value or a string variable");
}
public bool SupportsMethod(MethodInfo method)
{
// This will match the following methods:
// NHibernate.Linq.SqlMethods.Like(string, string)
// System.Data.Linq.SqlClient.SqlMethods.Like(string, string) (from Linq2SQL)
// plus any 2-argument method named Like in a class named SqlMethods
//
// The latter case allows application developers to define their own placeholder method
// to avoid referencing Linq2Sql or Linq2NHibernate, if they so wish.
return method != null && method.Name == "Like" &&
(method.GetParameters().Length == 2 || method.GetParameters().Length == 3) &&
method.DeclaringType != null &&
method.DeclaringType.FullName.EndsWith("SqlMethods");
}
public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
{
return this;
}
}
public class LengthGenerator : BaseHqlGeneratorForProperty
{
public LengthGenerator()
{
SupportedProperties = new[] { ReflectHelper.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[] { ReflectHelper.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[] { ReflectHelper.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[] { ReflectHelper.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[]
{
ReflectHelper.GetMethodDefinition<string>(x => x.ToLower()),
ReflectHelper.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[]
{
ReflectHelper.GetMethodDefinition<string>(x => x.ToUpper()),
ReflectHelper.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[]
{
ReflectHelper.GetMethodDefinition<string>(s => s.Substring(0)),
ReflectHelper.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[]
{
ReflectHelper.GetMethodDefinition<string>(s => s.IndexOf(' ')),
ReflectHelper.GetMethodDefinition<string>(s => s.IndexOf(" ")),
ReflectHelper.GetMethodDefinition<string>(s => s.IndexOf(' ', 0)),
ReflectHelper.GetMethodDefinition<string>(s => s.IndexOf(" ", 0))
};
}
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
{
HqlMethodCall locate;
if (arguments.Count == 1)
{
locate = treeBuilder.MethodCall("locate",
visitor.Visit(arguments[0]).AsExpression(),
visitor.Visit(targetObject).AsExpression()); //,
//treeBuilder.Constant(0));
}
else
{
var start = treeBuilder.Add(visitor.Visit(arguments[1]).AsExpression(), treeBuilder.Constant(1));
locate = treeBuilder.MethodCall("locate",
visitor.Visit(arguments[0]).AsExpression(),
visitor.Visit(targetObject).AsExpression(),
start);
}
return treeBuilder.Subtract(locate,treeBuilder.Constant(1));
}
}
public class ReplaceGenerator : BaseHqlGeneratorForMethod
{
public ReplaceGenerator()
{
SupportedMethods = new[]
{
ReflectHelper.GetMethodDefinition<string>(s => s.Replace(' ', ' ')),
ReflectHelper.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 = typeof(string).GetMethods().Where(x => new[] { "Trim", "TrimStart", "TrimEnd" }.Contains(x.Name)).ToArray();
}
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";
var trimChars = ExtractTrimChars(arguments);
if (string.IsNullOrEmpty(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());
}
}
private static string ExtractTrimChars(IReadOnlyList<Expression> arguments)
{
if (arguments.Count > 0)
{
var constantExpression = (ConstantExpression) arguments[0];
if (constantExpression.Value is char c)
return c.ToString();
if (constantExpression.Value is char[] chars)
return new string(chars);
}
return string.Empty;
}
}
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());
}
}
}