forked from IoTSharp/IoTSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryCollection.cs
232 lines (220 loc) · 10.7 KB
/
QueryCollection.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
using Newtonsoft.Json;
using System;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Reflection;
namespace IoTSharp.EasyEFQuery
{
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum Operators
{
None = 0,
Equal = 1,
GreaterThan = 2,
GreaterThanOrEqual = 3,
LessThan = 4,
LessThanOrEqual = 5,
Contains = 6,
StartWith = 7,
EndWidth = 8,
Range = 9
}
[System.Text.Json.Serialization.JsonConverter(typeof(System.Text.Json.Serialization.JsonStringEnumConverter))]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public enum Condition
{
OrElse = 1,
AndAlso = 2
}
public class Query
{
public string Name { get; set; }
public Operators Operator { get; set; }
public object Value { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public object ValueMin { get; set; }
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public object ValueMax { get; set; }
}
public class QueryCollection : Collection<Query>
{
public static QueryCollection Create()
{
return new QueryCollection();
}
public static QueryCollection Create(string json)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<QueryCollection>(json);
}
public override string ToString()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
}
public class ReplaceExpressionVisitor : ExpressionVisitor
{
private readonly Expression _oldValue;
private readonly Expression _newValue;
public ReplaceExpressionVisitor(Expression oldValue, Expression newValue)
{
_oldValue = oldValue;
_newValue = newValue;
}
public override Expression Visit(Expression node)
{
if (node == _oldValue)
{
return _newValue;
}
return base.Visit(node);
}
}
public static class QueryCollectionExtension
{
public static QueryCollection Parse(this QueryCollection queries, string json)
{
var jo= Newtonsoft.Json.Linq.JObject.Parse(json);
jo.Merge(queries);
return jo.ToObject<QueryCollection>();
}
public static Expression<Func<T, bool>> AndWith<T>(this Expression<Func<T, bool>> first, QueryCollection queries ) where T : class
{
return first.AndWith(queries.AsExpression<T>(), Expression.AndAlso);
}
public static Expression<Func<T, bool>> OrWith<T>(this Expression<Func<T, bool>> first, QueryCollection queries) where T : class
{
return first.AndWith(queries.AsExpression<T>(), Expression.OrElse);
}
private static Expression<Func<T, bool>> AndWith<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2, Func<Expression, Expression, BinaryExpression> func)
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(T));
Expression arg = new ReplaceExpressionVisitor(expr1.Parameters[0], parameterExpression).Visit(expr1.Body);
Expression arg2 = new ReplaceExpressionVisitor(expr2.Parameters[0], parameterExpression).Visit(expr2.Body);
return Expression.Lambda<Func<T, bool>>(func(arg, arg2), new ParameterExpression[1] { parameterExpression });
}
public static Expression<Func<T, bool>> AsExpression<T>(this QueryCollection queries, Condition? condition = Condition.OrElse) where T : class
{
Type targetType = typeof(T);
TypeInfo typeInfo = targetType.GetTypeInfo();
var parameter = Expression.Parameter(targetType, "m");
Expression expression = null;
Func<Expression, Expression, Expression> Append = (exp1, exp2) =>
{
if (exp1 == null)
{
return exp2;
}
return (condition ?? Condition.OrElse) == Condition.OrElse ? Expression.OrElse(exp1, exp2) : Expression.AndAlso(exp1, exp2);
};
foreach (var item in queries)
{
var property = typeInfo.GetProperty(item.Name);
if (property == null ||
!property.CanRead ||
(item.Operator != Operators.Range && item.Value == null) ||
(item.Operator == Operators.Range && item.ValueMin == null && item.ValueMax == null))
{
continue;
}
Type realType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
if (item.Value != null)
{
item.Value = Convert.ChangeType(item.Value, realType);
}
Expression<Func<object>> valueLamba = () => item.Value;
switch (item.Operator)
{
case Operators.Equal:
{
expression = Append(expression, Expression.Equal(Expression.Property(parameter, item.Name),
Expression.Convert(valueLamba.Body, property.PropertyType)));
break;
}
case Operators.GreaterThan:
{
expression = Append(expression, Expression.GreaterThan(Expression.Property(parameter, item.Name),
Expression.Convert(valueLamba.Body, property.PropertyType)));
break;
}
case Operators.GreaterThanOrEqual:
{
expression = Append(expression, Expression.GreaterThanOrEqual(Expression.Property(parameter, item.Name),
Expression.Convert(valueLamba.Body, property.PropertyType)));
break;
}
case Operators.LessThan:
{
expression = Append(expression, Expression.LessThan(Expression.Property(parameter, item.Name),
Expression.Convert(valueLamba.Body, property.PropertyType)));
break;
}
case Operators.LessThanOrEqual:
{
expression = Append(expression, Expression.LessThanOrEqual(Expression.Property(parameter, item.Name),
Expression.Convert(valueLamba.Body, property.PropertyType)));
break;
}
case Operators.Contains:
{
var nullCheck = Expression.Not(Expression.Call(typeof(string), "IsNullOrEmpty", null, Expression.Property(parameter, item.Name)));
var contains = Expression.Call(Expression.Property(parameter, item.Name), "Contains", null,
Expression.Convert(valueLamba.Body, property.PropertyType));
expression = Append(expression, Expression.AndAlso(nullCheck, contains));
break;
}
case Operators.StartWith:
{
var nullCheck = Expression.Not(Expression.Call(typeof(string), "IsNullOrEmpty", null, Expression.Property(parameter, item.Name)));
var startsWith = Expression.Call(Expression.Property(parameter, item.Name), "StartsWith", null,
Expression.Convert(valueLamba.Body, property.PropertyType));
expression = Append(expression, Expression.AndAlso(nullCheck, startsWith));
break;
}
case Operators.EndWidth:
{
var nullCheck = Expression.Not(Expression.Call(typeof(string), "IsNullOrEmpty", null, Expression.Property(parameter, item.Name)));
var endsWith = Expression.Call(Expression.Property(parameter, item.Name), "EndsWith", null,
Expression.Convert(valueLamba.Body, property.PropertyType));
expression = Append(expression, Expression.AndAlso(nullCheck, endsWith));
break;
}
case Operators.Range:
{
Expression minExp = null, maxExp = null;
if (item.ValueMin != null)
{
var minValue = Convert.ChangeType(item.ValueMin, realType);
Expression<Func<object>> minValueLamda = () => minValue;
minExp = Expression.GreaterThanOrEqual(Expression.Property(parameter, item.Name), Expression.Convert(minValueLamda.Body, property.PropertyType));
}
if (item.ValueMax != null)
{
var maxValue = Convert.ChangeType(item.ValueMax, realType);
Expression<Func<object>> maxValueLamda = () => maxValue;
maxExp = Expression.LessThanOrEqual(Expression.Property(parameter, item.Name), Expression.Convert(maxValueLamda.Body, property.PropertyType));
}
if (minExp != null && maxExp != null)
{
expression = Append(expression, Expression.AndAlso(minExp, maxExp));
}
else if (minExp != null)
{
expression = Append(expression, minExp);
}
else if (maxExp != null)
{
expression = Append(expression, maxExp);
}
break;
}
}
}
if (expression == null)
{
return null;
}
return ((Expression<Func<T, bool>>)Expression.Lambda(expression, parameter));
}
}
}