-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueryExecutor.cs
More file actions
355 lines (313 loc) · 19.2 KB
/
QueryExecutor.cs
File metadata and controls
355 lines (313 loc) · 19.2 KB
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
355
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using AutoMapper;
using AutoMapper.Internal;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Infragistics.QueryBuilder.Executor
{
/// <summary>
/// A generic query executor that can be used to execute queries on IQueryable data sources.
/// </summary>
public static class QueryExecutor
{
public static object[] Run<TEntity>(this IQueryable<TEntity> source, Query? query)
{
return source.Run<TEntity, TEntity>(query);
}
public static object[] Run<TSource, TTarget>(this IQueryable<TSource> source, Query? query, IMapper? mapper = null)
{
var infrastructure = source as IInfrastructure<IServiceProvider>;
var serviceProvider = infrastructure!.Instance;
var currentDbContext = serviceProvider.GetService(typeof(ICurrentDbContext)) as ICurrentDbContext;
var db = currentDbContext!.Context;
return db is not null ? BuildQuery<TSource, TTarget>(db, source, query, mapper).ToArray() : Array.Empty<object>();
}
public static object[] InvokeRunMethod(Type[] genericArgs, object?[] parameters)
{
var method = typeof(QueryExecutor)
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.FirstOrDefault(m =>
m.Name == "Run" &&
m.IsGenericMethodDefinition &&
m.GetGenericArguments().Length == genericArgs.Length)
?.MakeGenericMethod(genericArgs);
var result = method?.Invoke(null, parameters) ?? Array.Empty<object>();
return (object[])result;
}
private static IQueryable<object> BuildQuery<TSource, TTarget>(DbContext db, IQueryable<TSource> source, Query? query, IMapper? mapper = null)
{
if (query is null)
{
throw new InvalidOperationException("Null query");
}
var filterExpression = BuildExpression(db, source, query.FilteringOperands, query.Operator);
var filteredQuery = source.Where(filterExpression);
if (query.ReturnFields != null && query.ReturnFields.Any() && !query.ReturnFields.Contains("*"))
{
if (mapper is not null)
{
var projectionExpression = BuildProjectionExpression<TTarget, TTarget>(query.ReturnFields);
return filteredQuery.ProjectTo<TTarget>(mapper.ConfigurationProvider).Select(projectionExpression);
}
else
{
var projectionExpression = BuildProjectionExpression<TSource, TTarget>(query.ReturnFields);
return filteredQuery.Select(projectionExpression);
}
}
else if (mapper is not null)
{
return (IQueryable<object>)filteredQuery.ProjectTo<TTarget>(mapper.ConfigurationProvider);
}
else
{
return filteredQuery.Cast<object>();
}
}
private static Expression<Func<TEntity, bool>> BuildExpression<TEntity>(DbContext db, IQueryable<TEntity> source, QueryFilter[] filters, FilterType filterType)
{
var parameter = Expression.Parameter(typeof(TEntity), "entity");
var finalExpression = null as Expression;
foreach (var filter in filters)
{
var expression = BuildConditionExpression(db, source, filter, parameter);
if (finalExpression == null)
{
finalExpression = expression;
}
else
{
finalExpression = filterType == FilterType.And
? Expression.AndAlso(finalExpression, expression)
: Expression.OrElse(finalExpression, expression);
}
}
return finalExpression is not null
? Expression.Lambda<Func<TEntity, bool>>(finalExpression, parameter)
: (TEntity _) => true;
}
private static Expression BuildConditionExpression<TEntity>(DbContext db, IQueryable<TEntity> source, QueryFilter filter, ParameterExpression parameter)
{
if (filter.FieldName is not null && filter.Condition is not null)
{
var property = source.ElementType.GetProperty(filter.FieldName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
?? throw new InvalidOperationException($"Property '{filter.FieldName}' not found on type '{source.ElementType}'");
var field = Expression.Property(parameter, property);
var targetType = property.PropertyType;
var searchValue = GetSearchValue(filter.SearchVal, targetType);
var emptyValue = GetEmptyValue(targetType);
var today = DateTime.Now.Date;
Expression condition = filter.Condition.Name switch
{
"null" => targetType.IsNullableType() ? Expression.Equal(field, Expression.Constant(targetType.GetDefaultValue())) : Expression.Constant(false),
"notNull" => targetType.IsNullableType() ? Expression.NotEqual(field, Expression.Constant(targetType.GetDefaultValue())) : Expression.Constant(true),
"empty" => Expression.Or(Expression.Equal(field, emptyValue), targetType.IsNullableType() ? Expression.Equal(field, Expression.Constant(targetType.GetDefaultValue())) : Expression.Constant(false)),
"notEmpty" => Expression.And(Expression.NotEqual(field, emptyValue), targetType.IsNullableType() ? Expression.NotEqual(field, Expression.Constant(targetType.GetDefaultValue())) : Expression.Constant(true)),
"equals" => Expression.Equal(field, searchValue),
"doesNotEqual" => Expression.NotEqual(field, searchValue),
"inQuery" => BuildInExpression(db, filter.SearchTree, field),
"notInQuery" => Expression.Not(BuildInExpression(db, filter.SearchTree, field)),
"contains" => CallContains(field, searchValue),
"doesNotContain" => Expression.Not(CallContains(field, searchValue)),
"startsWith" => CallStartsWith(field, searchValue),
"endsWith" => CallEndsWith(field, searchValue),
"greaterThan" => Expression.GreaterThan(field, searchValue),
"lessThan" => Expression.LessThan(field, searchValue),
"greaterThanOrEqualTo" => Expression.GreaterThanOrEqual(field, searchValue),
"lessThanOrEqualTo" => Expression.LessThanOrEqual(field, searchValue),
"before" => Expression.LessThan(CallCompare(field, searchValue), Expression.Constant(0)),
"after" => Expression.GreaterThan(CallCompare(field, searchValue), Expression.Constant(0)),
"today" => CallStartsWith(field, today.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
"yesterday" => CallStartsWith(field, today.AddDays(-1).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
"thisMonth" => CallStartsWith(field, today.ToString("yyyy-MM", CultureInfo.InvariantCulture)),
"lastMonth" => CallStartsWith(field, today.AddMonths(-1).ToString("yyyy-MM", CultureInfo.InvariantCulture)),
"nextMonth" => CallStartsWith(field, today.AddMonths(1).ToString("yyyy-MM", CultureInfo.InvariantCulture)),
"thisYear" => CallStartsWith(field, today.ToString("yyyy", CultureInfo.InvariantCulture)),
"lastYear" => CallStartsWith(field, today.AddYears(-1).ToString("yyyy", CultureInfo.InvariantCulture)),
"nextYear" => CallStartsWith(field, today.AddYears(1).ToString("yyyy", CultureInfo.InvariantCulture)),
"at" => Expression.Equal(field, searchValue),
"not_at" => Expression.NotEqual(field, searchValue),
"at_before" => Expression.LessThan(CallCompare(field, searchValue), Expression.Constant(0)),
"at_after" => Expression.GreaterThan(CallCompare(field, searchValue), Expression.Constant(0)),
"all" => Expression.Constant(true),
"true" => Expression.Equal(field, Expression.Constant(true)),
"false" => Expression.Equal(field, Expression.Constant(false)),
_ => throw new NotImplementedException("Not implemented"),
};
if (filter.IgnoreCase == true && field.Type == typeof(string))
{
// TODO: Implement case-insensitive comparison
}
return condition;
}
else
{
var subexpressions = filter.FilteringOperands?.Select(f => BuildConditionExpression(db, source, f, parameter)).ToArray();
if (subexpressions == null || !subexpressions.Any())
{
return Expression.Constant(true);
}
return filter.Operator == FilterType.And
? subexpressions.Aggregate(Expression.AndAlso)
: subexpressions.Aggregate(Expression.OrElse);
}
}
private static Expression CallContains(Expression field, Expression searchValue)
{
var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
return Expression.Call(field, containsMethod!, searchValue);
}
private static Expression CallStartsWith(Expression field, Expression searchValue)
{
var startsWithMethod = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
return Expression.Call(field, startsWithMethod!, searchValue);
}
private static Expression CallStartsWith(Expression field, string literal)
{
return CallStartsWith(field, Expression.Constant(literal));
}
private static Expression CallEndsWith(Expression field, Expression searchValue)
{
var endsWithMethod = typeof(string).GetMethod("EndsWith", new[] { typeof(string) });
return Expression.Call(field, endsWithMethod!, searchValue);
}
private static Expression CallEndsWith(Expression field, string literal)
{
return CallEndsWith(field, Expression.Constant(literal));
}
private static Expression CallCompare(Expression field, Expression searchValue)
{
var compareMethod = typeof(string).GetMethod("Compare", new[] { typeof(string), typeof(string) });
return Expression.Call(compareMethod!, field, searchValue);
}
private static Expression BuildInExpression(DbContext db, Query? query, MemberExpression field)
{
return field.Type switch
{
{ } t when t == typeof(string) => BuildInExpression<string>(db, query, field),
{ } t when t == typeof(bool) => BuildInExpression<bool>(db, query, field),
{ } t when t == typeof(bool?) => BuildInExpression<bool?>(db, query, field),
{ } t when t == typeof(int) => BuildInExpression<int>(db, query, field),
{ } t when t == typeof(int?) => BuildInExpression<int?>(db, query, field),
{ } t when t == typeof(decimal) => BuildInExpression<decimal>(db, query, field),
{ } t when t == typeof(decimal?) => BuildInExpression<decimal?>(db, query, field),
{ } t when t == typeof(float) => BuildInExpression<float>(db, query, field),
{ } t when t == typeof(float?) => BuildInExpression<float?>(db, query, field),
{ } t when t == typeof(DateTime) => BuildInExpression<DateTime>(db, query, field),
{ } t when t == typeof(DateTime?) => BuildInExpression<DateTime?>(db, query, field),
_ => throw new InvalidOperationException($"Type '{field.Type}' not supported for 'IN' operation"),
};
}
private static Expression BuildInExpression<T>(DbContext db, Query? query, MemberExpression field)
{
var d = RunSubquery(db, query).Select(x => (T)ProjectField(x, query?.ReturnFields[0] ?? string.Empty)).Distinct();
var m = typeof(Enumerable).GetMethods()
.FirstOrDefault(method => method.Name == nameof(Enumerable.Contains) && method.GetParameters().Length == 2)
?.MakeGenericMethod(typeof(T)) ?? throw new InvalidOperationException("Missing method");
return Expression.Call(m, Expression.Constant(d), field);
}
private static IEnumerable<dynamic> RunSubquery(DbContext db, Query? query)
{
var propName = query?.Entity.ToLower(CultureInfo.InvariantCulture) ?? string.Empty;
var prop = db.GetType().GetProperty(propName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
?? throw new InvalidOperationException($"Property '{propName}' not found on type '{db.GetType()}'");
var methods = typeof(QueryExecutor).GetMethods(BindingFlags.Static | BindingFlags.Public);
var method = methods?.FirstOrDefault(m => m.CustomAttributes.Count() == 1);
var dbSet = prop.GetValue(db) ?? throw new ValidationException($"DbSet property '{prop.Name}' is null in DbContext.");
var genericType = prop.PropertyType.GetGenericArguments().FirstOrDefault() ?? throw new ValidationException($"Missing DbSet generic type");
var queryable = dbSet?.GetType().GetMethod("AsQueryable")?.Invoke(dbSet, null);
return InvokeRunMethod([genericType], [queryable, query]);
}
private static dynamic? ProjectField(object? obj, string field)
{
var property = obj?.GetType().GetMember(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)?.FirstOrDefault() ?? throw new InvalidOperationException($"Property '{field}' not found on type '{obj?.GetType()}'");
return property.GetMemberValue(obj);
}
private static Expression GetSearchValue(object? searchValue, Type targetType)
{
if (searchValue == null)
{
return GetEmptyValue(targetType);
}
var jsonVal = JsonValue.Create(searchValue);
var valueKind = jsonVal?.GetValueKind();
if (valueKind == null || valueKind == JsonValueKind.Null || valueKind == JsonValueKind.Undefined)
{
return GetEmptyValue(targetType);
}
var nonNullableType = Nullable.GetUnderlyingType(targetType) ?? targetType;
if (nonNullableType.IsEnum)
{
if (valueKind == JsonValueKind.String)
{
var enumValue = jsonVal.Deserialize<string>();
if (enumValue != null)
{
return Expression.Constant(Enum.Parse(nonNullableType, enumValue));
}
}
else if (valueKind == JsonValueKind.Number)
{
var enumValue = jsonVal.Deserialize<int?>();
if (enumValue != null)
{
return Expression.Constant(Enum.ToObject(nonNullableType, enumValue));
}
}
}
var value = jsonVal.Deserialize(targetType);
var convertedValue = Convert.ChangeType(value, nonNullableType, CultureInfo.InvariantCulture);
return Expression.Constant(convertedValue, targetType);
}
private static Expression GetEmptyValue(Type targetType)
{
return Expression.Constant(targetType == typeof(string) ? string.Empty : targetType.GetDefaultValue());
}
private static Expression<Func<TSource, object>> BuildProjectionExpression<TSource, TTarget>(string[] returnFields)
{
var tagetEntityType = typeof(TTarget);
var dbEntityType = typeof(TSource);
// Create the anonymous projection type
var projectionType = CreateProjectionType(tagetEntityType, returnFields);
var parameter = Expression.Parameter(dbEntityType, "entity");
var bindings = returnFields.Select(fieldName =>
{
var property = dbEntityType.GetProperty(fieldName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) ?? throw new InvalidOperationException($"Property '{fieldName}' not found on type '{dbEntityType}");
var field = projectionType.GetField(fieldName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) ?? throw new InvalidOperationException($"Property '{fieldName}' not found on type '{projectionType}'");
var propertyAccess = Expression.Property(parameter, property);
return Expression.Bind(field, propertyAccess);
}).ToArray();
// Get Microsoft.CSharp assembly where anonymous types are defined
var dynamicAssembly = typeof(Microsoft.CSharp.RuntimeBinder.Binder).Assembly;
var createExpression = Expression.MemberInit(Expression.New(projectionType), bindings);
return Expression.Lambda<Func<TSource, object>>(createExpression, parameter);
}
private static Type CreateProjectionType(Type input, string[] fields)
{
AssemblyName dynamicAssemblyName = new AssemblyName("TempAssembly");
AssemblyBuilder dynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(dynamicAssemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder dynamicModule = dynamicAssembly.DefineDynamicModule("TempAssembly");
var name = input.Name + "Projection";
TypeBuilder dynamicAnonymousType = dynamicModule.DefineType(name, TypeAttributes.Public);
foreach (var field in fields)
{
var property = input.GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
?? throw new InvalidOperationException($"Property '{field}' not found on type '{input}'");
FieldBuilder fieldBuilder = dynamicAnonymousType.DefineField(property.Name, property.GetMemberType(), FieldAttributes.Public);
ConstructorInfo jsonIncludeConstructor = typeof(JsonIncludeAttribute).GetConstructor(Type.EmptyTypes) ?? throw new InvalidOperationException("The constructor is missing in JsonIncludeAttribute");
CustomAttributeBuilder jsonIncludeAttributeBuilder = new(jsonIncludeConstructor, []);
fieldBuilder.SetCustomAttribute(jsonIncludeAttributeBuilder);
}
return dynamicAnonymousType.CreateType()!;
}
}
}