forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHavingClauseHandler.cs
More file actions
162 lines (130 loc) · 6.96 KB
/
HavingClauseHandler.cs
File metadata and controls
162 lines (130 loc) · 6.96 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Simple.Data.QueryPolyfills
{
internal class HavingClauseHandler
{
private readonly Dictionary<SimpleExpressionType, Func<SimpleExpression, Func<IDictionary<string, object>, bool>>> _expressionFormatters;
private readonly WhereClause _whereClause;
public HavingClauseHandler(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 key = GetKeyFromLeftOperand(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))
{
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);
return d => d.ContainsKey(key) && (!ReferenceEquals(d[key], null)) && regex.IsMatch(d[key].ToString());
}
throw new NotSupportedException("Expression Function not supported.");
}
private Func<IDictionary<string, object>, bool> GreaterThanToWhereClause(SimpleExpression arg)
{
var key = GetKeyFromLeftOperand(arg);
return d => d.ContainsKey(key) && !ReferenceEquals(d[key], null) && ((IComparable)d[key]).CompareTo(arg.RightOperand) > 0;
}
private Func<IDictionary<string, object>, bool> LessThanToWhereClause(SimpleExpression arg)
{
var key = GetKeyFromLeftOperand(arg);
return d => d.ContainsKey(key) && !ReferenceEquals(d[key], null) && ((IComparable)d[key]).CompareTo(arg.RightOperand) < 0;
}
private Func<IDictionary<string, object>, bool> GreaterThanOrEqualToWhereClause(SimpleExpression arg)
{
var key = GetKeyFromLeftOperand(arg);
return d => d.ContainsKey(key) && !ReferenceEquals(d[key], null) && ((IComparable)d[key]).CompareTo(arg.RightOperand) >= 0;
}
private Func<IDictionary<string, object>, bool> LessThanOrEqualToWhereClause(SimpleExpression arg)
{
var key = GetKeyFromLeftOperand(arg);
return d => d.ContainsKey(key) && !ReferenceEquals(d[key], null) && ((IComparable)d[key]).CompareTo(arg.RightOperand) <= 0;
}
private Func<IDictionary<string, object>, bool> NotEqualExpressionToWhereClause(SimpleExpression arg)
{
var key = GetKeyFromLeftOperand(arg);
if (ReferenceEquals(arg.RightOperand, null))
{
return d => d.ContainsKey(key) && d[key] != null;
}
if (arg.RightOperand.GetType().IsArray)
{
return
d =>
d.ContainsKey(key) &&
!((IEnumerable)d[key]).Cast<object>().SequenceEqual(((IEnumerable)arg.RightOperand).Cast<object>());
}
return d => d.ContainsKey(key) && !Equals(d[key], arg.RightOperand);
}
private Func<IDictionary<string, object>, bool> EqualExpressionToWhereClause(SimpleExpression arg)
{
var key = GetKeyFromLeftOperand(arg);
if (ReferenceEquals(arg.RightOperand, null))
{
return d => (!d.ContainsKey(key)) || d[key] == null;
}
if (arg.RightOperand.GetType().IsArray)
{
return
d =>
d.ContainsKey(key) &&
((IEnumerable) d[key]).Cast<object>().SequenceEqual(((IEnumerable) arg.RightOperand).Cast<object>());
}
return d => d.ContainsKey(key) && Equals(d[key], arg.RightOperand);
}
private static string GetKeyFromLeftOperand(SimpleExpression arg)
{
var reference = arg.LeftOperand as ObjectReference;
if (reference.IsNull()) throw new NotSupportedException("Only ObjectReference types are supported.");
var key = reference.GetName();
return key;
}
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));
}
public IEnumerable<IDictionary<string, object>> Run(IEnumerable<IDictionary<string, object>> source)
{
var predicate = Format(_whereClause.Criteria);
return source.Where(predicate);
}
}
}