This repository has been archived by the owner on Jul 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
QueryBuilder.cs
367 lines (325 loc) · 13.2 KB
/
QueryBuilder.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
using LinqToSQL.Net.Attributes.Columns;
using LinqToSQL.Net.Attributes.Columns;
using LinqToSQL.Net.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace LinqToSQL.Net
{
public class QueryBuilder<T>
{
List<ParameterExpression> _rootParameters;
private Expression<Func<T, bool>> _whereExpressions;
private List<string> _groupByExpressions;
private List<string> _orderByExpressions;
private List<string> _projectionExpressions;
private List<string> _sumExpressions;
private List<string> _countExpressions;
private Dictionary<Type, (string, string)> _joinExpressions;
private int _limitValue;
private int _offsetValue;
public QueryBuilder()
{
_rootParameters = new List<ParameterExpression>();
_whereExpressions = null;
_groupByExpressions = new List<string>();
_orderByExpressions = new List<string>();
_projectionExpressions = new List<string>();
_sumExpressions = new List<string>();
_countExpressions = new List<string>();
_joinExpressions = new Dictionary<Type, (string, string)>();
}
public QueryBuilder<T> Select(params Expression<Func<T, object>>[] expressions)
{
this._projectionExpressions.Add(string.Join(",", expressions.Select(x => ConvertExpressionToString(x))));
return this;
}
public QueryBuilder<T> SelectAll()
{
this._projectionExpressions = Properties();
return this;
}
public QueryBuilder<T> Where(Expression<Func<T, bool>> expression)
{
_rootParameters.AddRange(expression.Parameters);
_whereExpressions = _whereExpressions == null
? expression
: Expression.Lambda<Func<T, bool>>(
Expression.AndAlso(_whereExpressions.Body, expression.Body),
_whereExpressions.Parameters);
return this;
}
private string Where()
{
if (_whereExpressions != null)
{
var where = ConvertExpressionToString(_whereExpressions.Body);
return string.IsNullOrEmpty(where) ? string.Empty : string.Format("WHERE {0}", where);
}
return string.Empty;
}
public QueryBuilder<T> Join<J>(Expression<Func<T, object>> left, Expression<Func<J, object>> right)
{
_rootParameters.AddRange(left.Parameters);
_rootParameters.AddRange(right.Parameters);
_joinExpressions.Add(typeof(J),(ConvertExpressionToString(left), ConvertExpressionToString(right)));
return this;
}
private QueryBuilder<T> Join(Type joinTable)
{
if (_joinExpressions.ContainsKey(joinTable))
return this;
_joinExpressions.Add(joinTable,
(typeof(T)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(x => x.PropertyType == joinTable)
.GetCustomAttribute<ForeignKeyAttribute>().KeyName,
joinTable.GetCustomAttribute<PrimaryKeyAttribute>().ColumnName));
return this;
}
private string Join()
{
return string.Join(" ",
_joinExpressions.Select(
x =>
string.Format("INNER JOIN {0} ON {1}.{2}={3}.{4}", GetTableName(x.Key), GetTableName(typeof(T)), x.Value.Item1,
GetTableName(x.Key), x.Value.Item2)));
}
public string GetTableName(Type type)
{
return type.GetCustomAttribute<TableNameAttribute>().Name;
}
public QueryBuilder<T> GroupBy(params Expression<Func<T, object>>[] expression)
{
_groupByExpressions.AddRange(expression.Select(ConvertExpressionToString));
return this;
}
private string GroupBy()
{
return _groupByExpressions.Any() ? string.Format("GROUP BY {0}", string.Join(",", _groupByExpressions)) : string.Empty;
}
public QueryBuilder<T> OrderBy(params Expression<Func<T, object>>[] expression)
{
_orderByExpressions.AddRange(expression.Select(ConvertExpressionToString));
return this;
}
private string OrderBy()
{
return _orderByExpressions.Any() ? string.Format("ORDER BY {0}", string.Join(",", _orderByExpressions)) : string.Empty;
}
public QueryBuilder<T> Limit(int limit)
{
_limitValue = limit;
return this;
}
private string Limit()
{
if (_limitValue > 0)
return string.Format("LIMIT {0}", _limitValue);
return string.Empty;
}
public QueryBuilder<T> Offset(int offset)
{
_offsetValue = offset;
return this;
}
private string Offset()
{
if (_offsetValue > 0)
return string.Format("OFFSET {0}", _offsetValue);
return string.Empty;
}
public QueryBuilder<T> Sum(params Expression<Func<T, object>>[] expression)
{
_sumExpressions.AddRange(expression.Select(ConvertExpressionToString));
return this;
}
private string Sum()
{
return string.Join(", ", _sumExpressions.Select(x => string.Format("SUM({0}) AS `{0}`", x)));
}
public QueryBuilder<T> Count(params Expression<Func<T, object>>[] expression)
{
_countExpressions.AddRange(expression.Select(ConvertExpressionToString));
return this;
}
private string Count()
{
return string.Join(", ", _countExpressions.Select(x => string.Format("COUNT({0}) AS `{0}`", x)));
}
public List<string> Projections()
{
var properties = _projectionExpressions.Any() ? _projectionExpressions : new List<string>();
properties.AddRangeIf(_groupByExpressions, elem => !properties.Contains(elem));
var propertiesTabled = (_projectionExpressions.Any() ? _projectionExpressions : properties).Select(x => GetTableName(typeof(T)) + '.' + x).ToList();
return propertiesTabled;
}
private string Projection()
{
return string.Join(", ", Projections());
}
private List<string> Properties()
{
var properties = typeof(T).GetProperties().Where(prop => !Attribute.IsDefined(prop, typeof(IgnoreAttribute)));
return properties.Select(prop => prop.Name).ToList();
}
private string ConvertExpressionToString(Expression body)
{
if (body == null)
{
return string.Empty;
}
if (body is ConstantExpression)
{
return ValueToString(((ConstantExpression)body).Value);
}
if (body is MemberExpression)
{
var member = ((MemberExpression)body);
if (member.Member.MemberType == MemberTypes.Property && _rootParameters.Exists(x => x.Name == member.Member.Name))
return null;
if (member.Member.MemberType == MemberTypes.Property)
{
if (DatabaseExtensions.GetDbType(member.Type).HasValue)
{
string baseParameter = ConvertExpressionToString(member.Expression);
string sqlPart = !string.IsNullOrEmpty(baseParameter) ? baseParameter + '.' + member.Member.Name + (member.Type == typeof(bool) ? "=1" : "") : GetTableName(typeof(T)) + '.' + (member.Member.Name + (member.Type == typeof(bool) ? "=1" : ""));
return sqlPart.Trim();
}
else
{
this.Join(member.Type);
return GetTableName(member.Type).Trim();
}
}
var value = GetValueOfMemberExpression(member);
if (value is IEnumerable && !(value is string))
{
var sb = new StringBuilder();
foreach (var item in value as IEnumerable)
{
sb.AppendFormat("{0},", ValueToString(item));
}
return sb.Remove(sb.Length - 1, 1).ToString().Trim();
}
return ValueToString(value).Trim();
}
if (body is UnaryExpression)
{
string sqlPart = $"{ConvertExpressionTypeToString(((UnaryExpression)body).NodeType)} ({ConvertExpressionToString(((UnaryExpression)body).Operand)})";
return sqlPart.Trim();
}
if (body is BinaryExpression)
{
var binary = body as BinaryExpression;
return string.Format("({0}{1}{2})", ConvertExpressionToString(binary.Left),
ConvertExpressionTypeToString(binary.NodeType),
ConvertExpressionToString(binary.Right)).Trim();
}
if (body is MethodCallExpression)
{
var method = body as MethodCallExpression;
return string.Format("({0} IN ({1}))", ConvertExpressionToString(method.Arguments[0]),
ConvertExpressionToString(method.Object)).Trim();
}
if (body is LambdaExpression)
{
return ConvertExpressionToString(((LambdaExpression)body).Body).Trim();
}
return "";
}
private static string ValueToString(object value)
{
if (value is bool)
{
return Convert.ChangeType(value, typeof(int)).ToString();
}
if (value is string)
{
return string.Format("'{0}'", value);
}
if (value is DateTime)
{
return string.Format("'{0:yyyy-MM-dd HH:mm:ss}'", value);
}
return value.ToString();
}
private static object GetValueOfMemberExpression(MemberExpression member)
{
var objectMember = Expression.Convert(member, typeof(object));
var getterLambda = Expression.Lambda<Func<object>>(objectMember);
var getter = getterLambda.Compile();
return getter();
}
private static string ConvertExpressionTypeToString(ExpressionType nodeType)
{
switch (nodeType)
{
case ExpressionType.And:
return " AND ";
case ExpressionType.AndAlso:
return " AND ";
case ExpressionType.Or:
return " OR ";
case ExpressionType.OrElse:
return " OR ";
case ExpressionType.Not:
return "NOT";
case ExpressionType.NotEqual:
return "!=";
case ExpressionType.Equal:
return "=";
case ExpressionType.GreaterThan:
return ">";
case ExpressionType.GreaterThanOrEqual:
return ">=";
case ExpressionType.LessThan:
return "<";
case ExpressionType.LessThanOrEqual:
return "<=";
default:
return "";
}
}
public static implicit operator string(QueryBuilder<T> query) => query.GetQueryString();
private string GetQueryString()
{
string projection = CheckAndTrim(Projection());
string sum = CheckAndTrim(Sum());
string count = CheckAndTrim(Count());
string queries = "";
if (!string.IsNullOrEmpty(projection))
queries = projection;
if (!string.IsNullOrEmpty(sum) && !string.IsNullOrEmpty(queries))
queries = string.Join(", ", queries, sum);
else if(!string.IsNullOrEmpty(sum))
queries = sum;
if (!string.IsNullOrEmpty(count) && !string.IsNullOrEmpty(queries))
queries = string.Join(", ", queries, count);
else if (!string.IsNullOrEmpty(count))
queries = count;
string where = Where();
string join = Join();
string groupBy = GroupBy();
string orderBy = OrderBy();
string offset = Offset();
string limit = Limit();
return string.Format("SELECT {0} FROM {1} {2} {3} {4} {5} {6} {7}", queries,
GetTableName(typeof(T)), join, where, groupBy, orderBy, offset, limit).Trim();
}
public string CheckAndTrim(string value)
{
if (!string.IsNullOrEmpty(value))
{
value = value.Trim();
if (!string.IsNullOrEmpty(value))
return value;
}
return null;
}
}
}