-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathQueryableProvider.cs
266 lines (239 loc) · 12 KB
/
QueryableProvider.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
using FreeSql.Internal;
using FreeSql.Internal.CommonProvider;
using FreeSql.Internal.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace FreeSql.Extensions.Linq
{
class QueryableProvider<TCurrent, TSource> : IQueryable<TCurrent>, IOrderedQueryable<TCurrent> where TSource : class
{
Expression _expression;
IQueryProvider _provider;
internal Select1Provider<TSource> _select;
public QueryableProvider(Select1Provider<TSource> select)
{
_select = select;
_expression = Expression.Constant(this);
_provider = new QueryProvider<TCurrent, TSource>(_select, _expression);
}
public QueryableProvider(Expression expression, IQueryProvider provider, Select1Provider<TSource> select)
{
_select = select;
_expression = expression;
_provider = provider;
}
public IEnumerator<TCurrent> GetEnumerator()
{
var result = _provider.Execute<List<TCurrent>>(_expression);
if (result == null)
yield break;
foreach (var item in result)
{
yield return item;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public Type ElementType => typeof(QueryableProvider<TCurrent, TSource>);
public Expression Expression => _expression;
public IQueryProvider Provider => _provider;
}
class QueryProvider<TCurrent, TSource> : IQueryProvider where TSource : class
{
Select1Provider<TSource> _select;
Expression _oldExpression;
public QueryProvider(Select1Provider<TSource> select, Expression oldExpression)
{
_select = select;
_oldExpression = oldExpression;
}
public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
{
ExecuteExp(expression, null, false);
if (typeof(TElement) != typeof(TCurrent))
return new QueryableProvider<TElement, TSource>(expression, new QueryProvider<TElement, TSource>(_select, expression), _select);
_oldExpression = expression;
return new QueryableProvider<TElement, TSource>(expression, this, _select);
}
public IQueryable CreateQuery(Expression expression) => throw new NotImplementedException();
public TResult Execute<TResult>(Expression expression)
{
return (TResult)ExecuteExp(expression, typeof(TResult), _oldExpression == expression);
}
public object Execute(Expression expression) => throw new NotImplementedException();
public object ExecuteExp(Expression expression, Type tresult, bool isProcessed)
{
var callExp = expression as MethodCallExpression;
var isfirst = false;
if (callExp != null && isProcessed == false)
{
object throwCallExp(string message) => throw new Exception($"解析失败 {callExp.Method.Name} {message},提示:可以使用扩展方法 IQueryable.RestoreToSelect() 还原为 ISelect 再查询");
if (callExp.Method.DeclaringType != typeof(Queryable)) return throwCallExp($"必须属于 System.Linq.Queryable");
object tplMaxMinAvgSum(string method)
{
if (callExp.Arguments.Count == 2)
{
var avgParam = (callExp.Arguments[1] as UnaryExpression)?.Operand as LambdaExpression;
return Utils.GetDataReaderValue(tresult,
_select.GetType().GetMethod(method).MakeGenericMethod(avgParam.ReturnType).Invoke(_select, new object[] { avgParam }));
}
return throwCallExp($" 不支持 {callExp.Arguments.Count}个参数的方法");
}
object tplOrderBy(string method, bool isDescending)
{
if (callExp.Arguments.Count == 2)
{
var arg1 = (callExp.Arguments[1] as UnaryExpression)?.Operand as LambdaExpression;
_select.OrderByReflection(arg1, isDescending);
return tresult.CreateInstanceGetDefaultValue();
}
return throwCallExp($" 不支持 {callExp.Arguments.Count}个参数的方法");
}
switch (callExp.Method.Name)
{
case "Any":
if (callExp.Arguments.Count == 2) _select.InternalWhere(callExp.Arguments[1]);
return _select.Any();
case "AsQueryable":
break;
case "Max": return tplMaxMinAvgSum("Max");
case "Min": return tplMaxMinAvgSum("Min");
case "Sum": return tplMaxMinAvgSum("Sum");
case "Average": return tplMaxMinAvgSum("Avg");
case "Concat":
return throwCallExp(CoreErrorStrings.Not_Support);
case "Contains":
if (callExp.Arguments.Count == 2)
{
var dywhere = callExp.Arguments[1].GetConstExprValue();
if (dywhere == null) return throwCallExp($" 参数值不能为 null");
_select.WhereDynamic(dywhere);
return _select.Any();
}
return throwCallExp($" 不支持 {callExp.Arguments.Count}个参数的方法");
case "Count":
if (callExp.Arguments.Count == 2) _select.InternalWhere(callExp.Arguments[1]);
return Utils.GetDataReaderValue(tresult, _select.Count());
case "Distinct":
if (callExp.Arguments.Count == 1)
{
_select.Distinct();
break;
}
return throwCallExp(CoreErrorStrings.Not_Support);
case "ElementAt":
case "ElementAtOrDefault":
_select.Offset((int)callExp.Arguments[1].GetConstExprValue());
_select.Limit(1);
isfirst = true;
break;
case "First":
case "FirstOrDefault":
case "Single":
case "SingleOrDefault":
if (callExp.Arguments.Count == 2) _select.InternalWhere(callExp.Arguments[1]);
_select.Limit(1);
isfirst = true;
break;
case "OrderBy":
tplOrderBy("OrderByReflection", false);
break;
case "OrderByDescending":
tplOrderBy("OrderByReflection", true);
break;
case "ThenBy":
tplOrderBy("OrderByReflection", false);
break;
case "ThenByDescending":
tplOrderBy("OrderByReflection", true);
break;
case "Where":
var whereParam = (callExp.Arguments[1] as UnaryExpression)?.Operand as LambdaExpression;
if (whereParam.Parameters.Count == 1)
{
_select.InternalWhere(whereParam);
break;
}
return throwCallExp(CoreErrorStrings.Not_Support);
case "Skip":
_select.Offset((int)callExp.Arguments[1].GetConstExprValue());
break;
case "Take":
_select.Limit((int)callExp.Arguments[1].GetConstExprValue());
break;
case "ToList":
if (callExp.Arguments.Count == 1)
return _select.ToList();
return throwCallExp(CoreErrorStrings.Not_Support);
case "Select":
var selectParam = (callExp.Arguments[1] as UnaryExpression)?.Operand as LambdaExpression;
if (selectParam.Parameters.Count == 1)
{
_select._selectExpression = selectParam;
break;
}
return throwCallExp(CoreErrorStrings.Not_Support);
case "Join":
if (callExp.Arguments.Count == 5)
{
var arg2 = (callExp.Arguments[2] as UnaryExpression)?.Operand as LambdaExpression;
var arg3 = (callExp.Arguments[3] as UnaryExpression)?.Operand as LambdaExpression;
var arg4 = (callExp.Arguments[4] as UnaryExpression)?.Operand as LambdaExpression;
FreeSqlExtensionsLinqSql.InternalJoin2(_select, arg2, arg3, arg4);
_select._selectExpression = arg4.Body;
break;
}
return throwCallExp($" 不支持 {callExp.Arguments.Count}个参数的方法");
case "GroupJoin":
if (callExp.Arguments.Count == 5)
{
var arg2 = (callExp.Arguments[2] as UnaryExpression)?.Operand as LambdaExpression;
var arg3 = (callExp.Arguments[3] as UnaryExpression)?.Operand as LambdaExpression;
var arg4 = (callExp.Arguments[4] as UnaryExpression)?.Operand as LambdaExpression;
FreeSqlExtensionsLinqSql.InternalJoin2(_select, arg2, arg3, arg4);
_select._selectExpression = arg4.Body;
break;
}
return throwCallExp($" 不支持 {callExp.Arguments.Count}个参数的方法");
case "SelectMany":
if (callExp.Arguments.Count == 3)
{
var arg1 = (callExp.Arguments[1] as UnaryExpression)?.Operand as LambdaExpression;
var arg2 = (callExp.Arguments[2] as UnaryExpression)?.Operand as LambdaExpression;
FreeSqlExtensionsLinqSql.InternalSelectMany2(_select, arg1, arg2);
_select._selectExpression = arg2.Body;
break;
}
return throwCallExp($" 不支持 {callExp.Arguments.Count}个参数的方法");
case "DefaultIfEmpty":
break;
case "Last":
case "LastOrDefault":
return throwCallExp(CoreErrorStrings.Not_Support);
case "GroupBy":
return throwCallExp(CoreErrorStrings.Not_Support);
default:
return throwCallExp(CoreErrorStrings.Not_Support);
}
}
if (tresult == null) return null;
if (isfirst)
{
_select.Limit(1);
if (_select._selectExpression != null)
return _select.InternalToList<TCurrent>(_select._selectExpression).FirstOrDefault();
return _select.ToList().FirstOrDefault();
}
if (_select._selectExpression != null)
return _select.InternalToList<TCurrent>(_select._selectExpression);
return _select.ToList();
}
}
}