-
-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathQueryFutureEnumerable.cs
More file actions
195 lines (167 loc) · 5.53 KB
/
Copy pathQueryFutureEnumerable.cs
File metadata and controls
195 lines (167 loc) · 5.53 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Threading.Tasks;
#if EF5
using System.Data.Objects;
#elif EF6
using System.Data.Entity.Core.Objects;
#elif EFCORE
using System.Linq;
#endif
namespace Z.EntityFramework.Plus
{
/// <summary>Class for query future value.</summary>
/// <typeparam name="T">The type of elements of the query.</typeparam>
#if QUERY_INCLUDEOPTIMIZED
internal class QueryFutureEnumerable<T> : BaseQueryFuture, IEnumerable<T>
#else
public class QueryFutureEnumerable<T> : BaseQueryFuture, IEnumerable<T>
#endif
{
/// <summary>The result of the query future.</summary>
private IEnumerable<T> _result;
/// <summary>Constructor.</summary>
/// <param name="ownerBatch">The batch that owns this item.</param>
/// <param name="query">
/// The query to defer the execution and to add in the batch of future
/// queries.
/// </param>
#if EF5 || EF6
public QueryFutureEnumerable(QueryFutureBatch ownerBatch, ObjectQuery<T> query)
#elif EFCORE
public QueryFutureEnumerable(QueryFutureBatch ownerBatch, IQueryable query)
#endif
{
OwnerBatch = ownerBatch;
Query = query;
}
/// <summary>Gets the enumerator of the query future.</summary>
/// <returns>The enumerator of the query future.</returns>
public IEnumerator<T> GetEnumerator()
{
if (!HasValue)
{
OwnerBatch.ExecuteQueries();
}
if (_result == null)
{
return new List<T>().GetEnumerator();
}
return _result.GetEnumerator();
}
/// <summary>Gets the enumerator of the query future.</summary>
/// <returns>The enumerator of the query future.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#if NET45
public async Task<List<T>> ToListAsync()
{
if (!HasValue)
{
await OwnerBatch.ExecuteQueriesAsync().ConfigureAwait(false);
}
if (_result == null)
{
return new List<T>();
}
using (var enumerator = _result.GetEnumerator())
{
var list = new List<T>();
while (enumerator.MoveNext())
{
list.Add(enumerator.Current);
}
return list;
}
}
public async Task<T[]> ToArrayAsync()
{
if (!HasValue)
{
await OwnerBatch.ExecuteQueriesAsync().ConfigureAwait(false);
}
if (_result == null)
{
return new T[0];
}
using (var enumerator = _result.GetEnumerator())
{
var list = new List<T>();
while (enumerator.MoveNext())
{
list.Add(enumerator.Current);
}
return list.ToArray();
}
}
public async Task<T> FirstOrDefaultAsync()
{
if (!HasValue)
{
await OwnerBatch.ExecuteQueriesAsync().ConfigureAwait(false);
}
if (_result == null)
{
return default(T);
}
using (var enumerator = _result.GetEnumerator())
{
enumerator.MoveNext();
return enumerator.Current;
}
}
#endif
/// <summary>Sets the result of the query deferred.</summary>
/// <param name="reader">The reader returned from the query execution.</param>
public override void SetResult(DbDataReader reader)
{
if (reader.GetType().FullName.Contains("Oracle"))
{
var reader2 = new QueryFutureOracleDbReader(reader);
reader = reader2;
}
var enumerator = GetQueryEnumerator<T>(reader);
SetResult(enumerator);
}
public void SetResult(IEnumerator<T> enumerator)
{
// Enumerate on all items
var list = new List<T>();
while (enumerator.MoveNext())
{
list.Add(enumerator.Current);
}
_result = list;
HasValue = true;
}
#if EFCORE
public override void ExecuteInMemory()
{
HasValue = true;
_result = ((IQueryable<T>) Query).ToList();
}
#endif
public override void GetResultDirectly()
{
var query = ((IQueryable<T>)Query);
GetResultDirectly(query);
}
internal void GetResultDirectly(IQueryable<T> query)
{
using (var enumerator = query.GetEnumerator())
{
SetResult(enumerator);
}
}
}
}