forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectionsExtensions.cs
354 lines (307 loc) · 12.6 KB
/
ProjectionsExtensions.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Linq.Expressions;
using NHibernate.Impl;
using NHibernate.Type;
namespace NHibernate.Criterion
{
public static class ProjectionsExtensions
{
/// <summary>
/// Create an alias for a projection
/// </summary>
/// <param name="projection">the projection instance</param>
/// <param name="alias">LambdaExpression returning an alias</param>
/// <returns>return NHibernate.Criterion.IProjection</returns>
public static IProjection WithAlias(this IProjection projection,
Expression<Func<object>> alias)
{
string aliasContainer = ExpressionProcessor.FindPropertyExpression(alias.Body);
return Projections.Alias(projection, aliasContainer);
}
/// <summary>
/// Create an alias for a projection
/// </summary>
/// <param name="projection">the projection instance</param>
/// <param name="alias">alias</param>
/// <returns>return NHibernate.Criterion.IProjection</returns>
public static IProjection WithAlias(this IProjection projection, string alias)
{
return Projections.Alias(projection, alias);
}
internal static IProjection ProcessYear(System.Linq.Expressions.Expression expression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(expression).AsProjection();
return Projections.SqlFunction("year", NHibernateUtil.Int32, property);
}
internal static IProjection ProcessDay(System.Linq.Expressions.Expression expression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(expression).AsProjection();
return Projections.SqlFunction("day", NHibernateUtil.Int32, property);
}
internal static IProjection ProcessMonth(System.Linq.Expressions.Expression expression)
{
return SqlFunction("month", NHibernateUtil.Int32, expression);
}
private static IProjection SqlFunction(string name, IType type, System.Linq.Expressions.Expression projection)
{
IProjection property = ExpressionProcessor.FindMemberProjection(projection).AsProjection();
return Projections.SqlFunction(name, type, property);
}
internal static IProjection ProcessHour(System.Linq.Expressions.Expression expression)
{
return SqlFunction("hour", NHibernateUtil.Int32, expression);
}
internal static IProjection ProcessMinute(System.Linq.Expressions.Expression expression)
{
return SqlFunction("minute", NHibernateUtil.Int32, expression);
}
internal static IProjection ProcessSecond(System.Linq.Expressions.Expression expression)
{
return SqlFunction("second", NHibernateUtil.Int32, expression);
}
internal static IProjection ProcessDate(System.Linq.Expressions.Expression expression)
{
return SqlFunction("date", NHibernateUtil.Date, expression);
}
/// <summary>
/// Project SQL function sqrt()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static double Sqrt(this double numericProperty)
{
throw QueryOver.GetDirectUsageException();
}
/// <summary>
/// Project SQL function sqrt()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static double Sqrt(this int numericProperty)
{
throw QueryOver.GetDirectUsageException();
}
/// <summary>
/// Project SQL function sqrt()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static double Sqrt(this long numericProperty)
{
throw QueryOver.GetDirectUsageException();
}
/// <summary>
/// Project SQL function sqrt()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static double Sqrt(this decimal numericProperty)
{
throw QueryOver.GetDirectUsageException();
}
/// <summary>
/// Project SQL function sqrt()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static double Sqrt(this byte numericProperty)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessSqrt(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return Projections.SqlFunction("sqrt", NHibernateUtil.Double, property);
}
/// <summary>
/// Project SQL function lower()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static string Lower(this string stringProperty)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessLower(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return Projections.SqlFunction("lower", NHibernateUtil.String, property);
}
/// <summary>
/// Project SQL function upper()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static string Upper(this string stringProperty)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessUpper(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return Projections.SqlFunction("upper", NHibernateUtil.String, property);
}
/// <summary>
/// Project SQL function abs()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static int Abs(this int numericProperty)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessIntAbs(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return Projections.SqlFunction("abs", NHibernateUtil.Int32, property);
}
/// <summary>
/// Project SQL function abs()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static Int64 Abs(this Int64 numericProperty)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessInt64Abs(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return Projections.SqlFunction("abs", NHibernateUtil.Int64, property);
}
internal static IProjection ProcessRound(MethodCallExpression methodCallExpression)
{
IProjection innerProjection =
ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
IProjection digitsProjection = Projections.Constant(0);
if (methodCallExpression.Arguments.Count > 1)
digitsProjection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[1]).AsProjection();
return Projections.SqlFunction("round", NHibernateUtil.Double, innerProjection, digitsProjection);
}
/// <summary>
/// Project SQL function abs()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static double Abs(this double numericProperty)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessDoubleAbs(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return Projections.SqlFunction("abs", NHibernateUtil.Double, property);
}
/// <summary>
/// Project SQL function trim()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static string TrimStr(this string stringProperty)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessTrimStr(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return Projections.SqlFunction("trim", NHibernateUtil.String, property);
}
/// <summary>
/// Project SQL function length()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static int StrLength(this string stringProperty)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessStrLength(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return Projections.SqlFunction("length", NHibernateUtil.String, property);
}
/// <summary>
/// Project SQL function bit_length()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static int BitLength(this string stringProperty)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessBitLength(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
return Projections.SqlFunction("bit_length", NHibernateUtil.String, property);
}
/// <summary>
/// Project SQL function substring()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static string Substr(this string stringProperty, int startIndex, int length)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessSubstr(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
var startIndex = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[1]);
var length = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[2]);
return Projections.SqlFunction("substring", NHibernateUtil.String, property, startIndex.AsProjection(), length.AsProjection());
}
/// <summary>
/// Project SQL function locate()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static int CharIndex(this string stringProperty, string theChar, int startLocation)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessCharIndex(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
var theChar = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[1]);
var startLocation = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[2]);
return Projections.SqlFunction("locate", NHibernateUtil.String, theChar.AsProjection(), property, startLocation.AsProjection());
}
/// <summary>
/// Project SQL function coalesce()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static T Coalesce<T>(this T objectProperty, T replaceValueIfIsNull)
{
throw QueryOver.GetDirectUsageException();
}
/// <summary>
/// Project SQL function coalesce()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static T? Coalesce<T>(this T? objectProperty, T replaceValueIfIsNull) where T : struct
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessCoalesce(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
var replaceValueIfIsNull = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[1]);
return new SqlFunctionProjection("coalesce", returnTypeProjection: property, property, replaceValueIfIsNull.AsProjection());
}
/// <summary>
/// Project SQL function mod()
/// Note: throws an exception outside of a QueryOver expression
/// </summary>
public static int Mod(this int numericProperty, int divisor)
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessMod(MethodCallExpression methodCallExpression)
{
IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
var divisor = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[1]);
return Projections.SqlFunction("mod", NHibernateUtil.Int32, property, divisor.AsProjection());
}
/// <summary>
/// Project Entity
/// </summary>
public static T AsEntity<T>(this T alias) where T:class
{
throw QueryOver.GetDirectUsageException();
}
internal static IProjection ProcessAsEntity(MethodCallExpression methodCallExpression)
{
var expression = methodCallExpression.Arguments[0];
var aliasName = ExpressionProcessor.FindMemberExpression(expression);
return
string.IsNullOrEmpty(aliasName)
? Projections.RootEntity()
: Projections.Entity(expression.Type, aliasName);
}
}
}