forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhereClauseHandler.cs
More file actions
179 lines (149 loc) · 7.98 KB
/
WhereClauseHandler.cs
File metadata and controls
179 lines (149 loc) · 7.98 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
namespace Simple.Data.QueryPolyfills
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
internal class WhereClauseHandler
{
private readonly Dictionary<SimpleExpressionType, Func<SimpleExpression, Func<IDictionary<string, object>, bool>>> _expressionFormatters;
private readonly WhereClause _whereClause;
public WhereClauseHandler(WhereClause whereClause)
{
_whereClause = whereClause;
_expressionFormatters = new Dictionary<SimpleExpressionType, Func<SimpleExpression, Func<IDictionary<string,object>, bool>>>
{
{SimpleExpressionType.And, LogicalExpressionToWhereClause},
{SimpleExpressionType.Or, LogicalExpressionToWhereClause},
{SimpleExpressionType.Equal, EqualExpressionToWhereClause},
{SimpleExpressionType.NotEqual, NotEqualExpressionToWhereClause},
{SimpleExpressionType.Function, FunctionExpressionToWhereClause},
{SimpleExpressionType.GreaterThan, GreaterThanToWhereClause},
{SimpleExpressionType.LessThan, LessThanToWhereClause},
{SimpleExpressionType.GreaterThanOrEqual, GreaterThanOrEqualToWhereClause},
{SimpleExpressionType.LessThanOrEqual, LessThanOrEqualToWhereClause},
{SimpleExpressionType.Empty, expr => _ => true },
};
}
private Func<IDictionary<string, object>, bool> FunctionExpressionToWhereClause(SimpleExpression arg)
{
var function = arg.RightOperand as SimpleFunction;
if (ReferenceEquals(function, null)) throw new InvalidOperationException("Expression type of function but no function supplied.");
if (function.Name.Equals("like", StringComparison.OrdinalIgnoreCase) ||
function.Name.Equals("notlike", StringComparison.OrdinalIgnoreCase))
{
var pattern = function.Args[0].ToString();
if (pattern.Contains("%") || pattern.Contains("_")) // SQL Server LIKE
{
pattern = pattern.Replace("%", ".*").Replace('_', '.');
}
var regex = new Regex("^" + pattern + "$", RegexOptions.Multiline | RegexOptions.IgnoreCase);
if (function.Name.Equals("like", StringComparison.OrdinalIgnoreCase))
{
return d => Resolve(d, arg.LeftOperand).Count > 0 && Resolve(d, arg.LeftOperand).OfType<string>().Any(regex.IsMatch);
}
if (function.Name.Equals("notlike", StringComparison.OrdinalIgnoreCase))
{
return d => Resolve(d, arg.LeftOperand).Count > 0 && Resolve(d, arg.LeftOperand).OfType<string>().All(input => !regex.IsMatch(input));
}
}
throw new NotSupportedException("Expression Function not supported.");
}
private Func<IDictionary<string, object>, bool> GreaterThanToWhereClause(SimpleExpression arg)
{
return d => Resolve(d, arg.LeftOperand).OfType<IComparable>().Any(o => o.CompareTo(arg.RightOperand) > 0);
}
private Func<IDictionary<string, object>, bool> LessThanToWhereClause(SimpleExpression arg)
{
return d => Resolve(d, arg.LeftOperand).OfType<IComparable>().Any(o => o.CompareTo(arg.RightOperand) < 0);
}
private Func<IDictionary<string, object>, bool> GreaterThanOrEqualToWhereClause(SimpleExpression arg)
{
return d => Resolve(d, arg.LeftOperand).OfType<IComparable>().Any(o => o.CompareTo(arg.RightOperand) >= 0);
}
private Func<IDictionary<string, object>, bool> LessThanOrEqualToWhereClause(SimpleExpression arg)
{
return d => Resolve(d, arg.LeftOperand).OfType<IComparable>().Any(o => o.CompareTo(arg.RightOperand) <= 0);
}
private Func<IDictionary<string, object>, bool> NotEqualExpressionToWhereClause(SimpleExpression arg)
{
var equal = EqualExpressionToWhereClause(arg);
return d => !equal(d);
}
private Func<IDictionary<string, object>, bool> EqualExpressionToWhereClause(SimpleExpression arg)
{
if (ReferenceEquals(arg.RightOperand, null))
{
return d => Resolve(d, arg.LeftOperand).Count == 0 || Resolve(d, arg.LeftOperand).Any(o => ReferenceEquals(o, null));
}
if (arg.RightOperand.GetType().IsArray)
{
return
d =>
Resolve(d, arg.LeftOperand).OfType<IEnumerable>().Any(
o => o.Cast<object>().SequenceEqual(((IEnumerable) arg.RightOperand).Cast<object>()));
}
return d => Resolve(d, arg.LeftOperand).Contains(arg.RightOperand);
}
private Func<IDictionary<string,object>, bool> Format(SimpleExpression expression)
{
Func<SimpleExpression, Func<IDictionary<string,object>,bool>> formatter;
if (_expressionFormatters.TryGetValue(expression.Type, out formatter))
{
return formatter(expression);
}
return _ => true;
}
private Func<IDictionary<string, object>, bool> LogicalExpressionToWhereClause(SimpleExpression arg)
{
var left = Format((SimpleExpression) arg.LeftOperand);
var right = Format((SimpleExpression) arg.RightOperand);
if (arg.Type == SimpleExpressionType.Or)
{
return d => (left(d) || right(d));
}
return d => (left(d) && right(d));
}
private IList<object> Resolve(IDictionary<string, object> dict, object operand, string key = null)
{
var objectReference = operand as ObjectReference;
if (objectReference.IsNull()) return new object[0];
key = key ?? objectReference.GetAliasOrName();
var keys = objectReference.GetAllObjectNames();
if (keys.Length > 2)
{
return ResolveSubs(dict, objectReference.GetOwner(), key).ToList();
}
if (dict.ContainsKey(key))
return new[] {dict[key]};
return new object[0];
}
private IEnumerable<object> ResolveSubs(IDictionary<string, object> dict, ObjectReference objectReference, string key)
{
if (objectReference.IsNull()) return Enumerable.Empty<object>();
if (dict.ContainsKey(objectReference.GetName()))
{
var master = dict[objectReference.GetName()] as IDictionary<string,object>;
if (master != null)
{
if (master.ContainsKey(key))
{
return new[] {master[key]};
}
}
var detail = dict[objectReference.GetName()] as IEnumerable<IDictionary<string, object>>;
if (detail != null)
{
return detail.SelectMany(d => Resolve(d, objectReference, key));
}
}
return ResolveSubs(dict, objectReference.GetOwner(), key);
}
public IEnumerable<IDictionary<string, object>> Run(IEnumerable<IDictionary<string, object>> source)
{
var predicate = Format(_whereClause.Criteria);
return source.Where(predicate);
}
}
}