-
-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathQuery.cs
executable file
·437 lines (360 loc) · 12.4 KB
/
Query.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace SqlKata
{
public partial class Query : BaseQuery<Query>
{
private string comment;
public bool IsDistinct { get; set; } = false;
public string QueryAlias { get; set; }
public string Method { get; set; } = "select";
public List<Include> Includes = new List<Include>();
public Dictionary<string, object> Variables = new Dictionary<string, object>();
public Query() : base()
{
}
public Query(string table, string comment = null) : base()
{
From(table);
Comment(comment);
}
public string GetComment() => comment ?? "";
public bool HasOffset(string engineCode = null) => GetOffset(engineCode) > 0;
public bool HasLimit(string engineCode = null) => GetLimit(engineCode) > 0;
internal long GetOffset(string engineCode = null)
{
engineCode = engineCode ?? EngineScope;
var offset = this.GetOneComponent<OffsetClause>("offset", engineCode);
return offset?.Offset ?? 0;
}
internal int GetLimit(string engineCode = null)
{
engineCode = engineCode ?? EngineScope;
var limit = this.GetOneComponent<LimitClause>("limit", engineCode);
return limit?.Limit ?? 0;
}
public override Query Clone()
{
var clone = base.Clone();
clone.Parent = Parent;
clone.QueryAlias = QueryAlias;
clone.IsDistinct = IsDistinct;
clone.Method = Method;
clone.Includes = Includes;
clone.Variables = Variables;
return clone;
}
public Query As(string alias)
{
QueryAlias = alias;
return this;
}
/// <summary>
/// Sets a comment for the query.
/// </summary>
/// <param name="comment">The comment.</param>
/// <returns></returns>
public Query Comment(string comment)
{
this.comment = comment;
return this;
}
public Query For(string engine, Func<Query, Query> fn)
{
EngineScope = engine;
var result = fn.Invoke(this);
// reset the engine
EngineScope = null;
return result;
}
public Query With(Query query)
{
// Clear query alias and add it to the containing clause
if (string.IsNullOrWhiteSpace(query.QueryAlias))
{
throw new InvalidOperationException("No Alias found for the CTE query");
}
query = query.Clone();
var alias = query.QueryAlias.Trim();
// clear the query alias
query.QueryAlias = null;
return AddComponent("cte", new QueryFromClause
{
Query = query,
Alias = alias,
});
}
public Query With(Func<Query, Query> fn)
{
return With(fn.Invoke(new Query()));
}
public Query With(string alias, Query query)
{
return With(query.As(alias));
}
public Query With(string alias, Func<Query, Query> fn)
{
return With(alias, fn.Invoke(new Query()));
}
/// <summary>
/// Constructs an ad-hoc table of the given data as a CTE.
/// </summary>
public Query With(string alias, IEnumerable<string> columns, IEnumerable<IEnumerable<object>> valuesCollection)
{
var columnsList = columns?.ToList();
var valuesCollectionList = valuesCollection?.ToList();
if ((columnsList?.Count ?? 0) == 0 || (valuesCollectionList?.Count ?? 0) == 0)
{
throw new InvalidOperationException("Columns and valuesCollection cannot be null or empty");
}
var clause = new AdHocTableFromClause()
{
Alias = alias,
Columns = columnsList,
Values = new List<object>(),
};
foreach (var values in valuesCollectionList)
{
var valuesList = values.ToList();
if (columnsList.Count != valuesList.Count)
{
throw new InvalidOperationException("Columns count should be equal to each Values count");
}
clause.Values.AddRange(valuesList);
}
return AddComponent("cte", clause);
}
public Query WithRaw(string alias, string sql, params object[] bindings)
{
return AddComponent("cte", new RawFromClause
{
Alias = alias,
Expression = sql,
Bindings = bindings,
});
}
public Query Limit(int value)
{
var newClause = new LimitClause
{
Limit = value
};
return AddOrReplaceComponent("limit", newClause);
}
public Query Offset(long value)
{
var newClause = new OffsetClause
{
Offset = value
};
return AddOrReplaceComponent("offset", newClause);
}
public Query Offset(int value)
{
return Offset((long)value);
}
/// <summary>
/// Alias for Limit
/// </summary>
/// <param name="limit"></param>
/// <returns></returns>
public Query Take(int limit)
{
return Limit(limit);
}
/// <summary>
/// Alias for Offset
/// </summary>
/// <param name="offset"></param>
/// <returns></returns>
public Query Skip(int offset)
{
return Offset(offset);
}
/// <summary>
/// Set the limit and offset for a given page.
/// </summary>
/// <param name="page"></param>
/// <param name="perPage"></param>
/// <returns></returns>
public Query ForPage(int page, int perPage = 15)
{
return Skip((page - 1) * perPage).Take(perPage);
}
public Query Distinct()
{
IsDistinct = true;
return this;
}
/// <summary>
/// Apply the callback's query changes if the given "condition" is true.
/// </summary>
/// <param name="condition"></param>
/// <param name="whenTrue">Invoked when the condition is true</param>
/// <param name="whenFalse">Optional, invoked when the condition is false</param>
/// <returns></returns>
public Query When(bool condition, Func<Query, Query> whenTrue, Func<Query, Query> whenFalse = null)
{
if (condition && whenTrue != null)
{
return whenTrue.Invoke(this);
}
if (!condition && whenFalse != null)
{
return whenFalse.Invoke(this);
}
return this;
}
/// <summary>
/// Apply the callback's query changes if the given "condition" is false.
/// </summary>
/// <param name="condition"></param>
/// <param name="callback"></param>
/// <returns></returns>
public Query WhenNot(bool condition, Func<Query, Query> callback)
{
if (!condition)
{
return callback.Invoke(this);
}
return this;
}
public Query OrderBy(params string[] columns)
{
foreach (var column in columns)
{
AddComponent("order", new OrderBy
{
Column = column,
Ascending = true
});
}
return this;
}
public Query OrderByDesc(params string[] columns)
{
foreach (var column in columns)
{
AddComponent("order", new OrderBy
{
Column = column,
Ascending = false
});
}
return this;
}
public Query OrderByRaw(string expression, params object[] bindings)
{
return AddComponent("order", new RawOrderBy
{
Expression = expression,
Bindings = Helper.Flatten(bindings).ToArray()
});
}
public Query OrderByRandom(string seed)
{
return AddComponent("order", new OrderByRandom { });
}
public Query GroupBy(params string[] columns)
{
foreach (var column in columns)
{
AddComponent("group", new Column
{
Name = column
});
}
return this;
}
public Query GroupByRaw(string expression, params object[] bindings)
{
AddComponent("group", new RawColumn
{
Expression = expression,
Bindings = bindings,
});
return this;
}
public override Query NewQuery()
{
return new Query();
}
public Query Include(string relationName, Query query, string foreignKey = null, string localKey = "Id", bool isMany = false)
{
Includes.Add(new Include
{
Name = relationName,
LocalKey = localKey,
ForeignKey = foreignKey,
Query = query,
IsMany = isMany,
});
return this;
}
public Query IncludeMany(string relationName, Query query, string foreignKey = null, string localKey = "Id")
{
return Include(relationName, query, foreignKey, localKey, isMany: true);
}
private static readonly ConcurrentDictionary<Type, PropertyInfo[]> CacheDictionaryProperties = new ConcurrentDictionary<Type, PropertyInfo[]>();
/// <summary>
/// Define a variable to be used within the query
/// </summary>
/// <param name="variable"></param>
/// <param name="value"></param>
/// <returns></returns>
public Query Define(string variable, object value)
{
Variables.Add(variable, value);
return this;
}
public object FindVariable(string variable)
{
var found = Variables.ContainsKey(variable);
if (found)
{
return Variables[variable];
}
if (Parent != null)
{
return (Parent as Query).FindVariable(variable);
}
throw new Exception($"Variable '{variable}' not found");
}
/// <summary>
/// Gather a list of key-values representing the properties of the object and their values.
/// </summary>
/// <param name="data">The plain C# object</param>
/// <param name="considerKeys">
/// When true it will search for properties with the [Key] attribute
/// and will add it automatically to the Where clause
/// </param>
/// <returns></returns>
private IEnumerable<KeyValuePair<string, object>> BuildKeyValuePairsFromObject(object data, bool considerKeys = false)
{
var dictionary = new Dictionary<string, object>();
var props = CacheDictionaryProperties.GetOrAdd(data.GetType(), type => type.GetRuntimeProperties().ToArray());
foreach (var property in props)
{
if (property.GetCustomAttribute(typeof(IgnoreAttribute)) != null)
{
continue;
}
var value = property.GetValue(data);
var colAttr = property.GetCustomAttribute(typeof(ColumnAttribute)) as ColumnAttribute;
var name = colAttr?.Name ?? property.Name;
dictionary.Add(name, value);
if (considerKeys && colAttr != null)
{
if ((colAttr as KeyAttribute) != null)
{
this.Where(name, value);
}
}
}
return dictionary;
}
}
}