forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleResultSet.cs
More file actions
318 lines (266 loc) · 9.87 KB
/
SimpleResultSet.cs
File metadata and controls
318 lines (266 loc) · 9.87 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace Simple.Data
{
public sealed class SimpleResultSet : DynamicObject, IEnumerable
{
public static readonly SimpleResultSet Empty = new SimpleResultSet();
private readonly IEnumerable<IEnumerable<dynamic>> _sources;
private readonly IEnumerator<IEnumerable<dynamic>> _sourceEnumerator;
private bool _hasCurrent;
private IDictionary<string, object> _outputValues;
private SimpleResultSet() : this(Enumerable.Empty<IEnumerable<dynamic>>())
{
}
public SimpleResultSet(params IEnumerable<dynamic>[] sources) : this (sources.AsEnumerable())
{
}
public SimpleResultSet(IEnumerable<IEnumerable<dynamic>> sources)
{
_sources = sources;
_sourceEnumerator = _sources.GetEnumerator();
_hasCurrent = _sourceEnumerator.MoveNext();
}
public IDictionary<string, object> OutputValues
{
get { return _outputValues; }
}
public object ReturnValue
{
get { return (_outputValues != null && _outputValues.ContainsKey("__ReturnValue")) ? _outputValues["__ReturnValue"] : 0; }
}
public bool NextResult()
{
return _hasCurrent = (_hasCurrent && _sourceEnumerator.MoveNext());
}
public IEnumerable<T> Cast<T>()
{
return new CastEnumerable<T>(_sourceEnumerator.Current);
}
public IEnumerable<T> OfType<T>()
{
return new OfTypeEnumerable<T>(_sourceEnumerator.Current);
}
public IList<dynamic> ToList()
{
return _sourceEnumerator.Current.ToList();
}
public dynamic[] ToArray()
{
return _sourceEnumerator.Current.ToArray();
}
public IList<T> ToList<T>()
{
return Cast<T>().ToList();
}
public T[] ToArray<T>()
{
return Cast<T>().ToArray();
}
public IList ToScalarList()
{
return ToScalarEnumerable().ToList();
}
public dynamic[] ToScalarArray()
{
return ToScalarEnumerable().ToArray();
}
public IList<T> ToScalarList<T>()
{
return ToScalarEnumerable().Cast<T>().ToList();
}
public T[] ToScalarArray<T>()
{
return ToScalarEnumerable().Cast<T>().ToArray();
}
private IEnumerable<dynamic> ToScalarEnumerable()
{
return _sourceEnumerator.Current.OfType<IDictionary<string, object>>().Select(dict => dict.Values.FirstOrDefault());
}
public dynamic First()
{
return _sourceEnumerator.Current.First();
}
public dynamic FirstOrDefault()
{
return _sourceEnumerator.Current.FirstOrDefault();
}
public T First<T>()
{
return Cast<T>().First();
}
public T FirstOrDefault<T>()
{
return Cast<T>().FirstOrDefault();
}
public T First<T>(Func<T,bool> predicate)
{
return Cast<T>().First(predicate);
}
public T FirstOrDefault<T>(Func<T,bool> predicate)
{
return Cast<T>().FirstOrDefault(predicate);
}
public dynamic Last()
{
return _sourceEnumerator.Current.Last();
}
public dynamic LastOrDefault()
{
return _sourceEnumerator.Current.LastOrDefault();
}
public T Last<T>()
{
return Cast<T>().Last();
}
public T LastOrDefault<T>()
{
return Cast<T>().LastOrDefault();
}
public T Last<T>(Func<T, bool> predicate)
{
return Cast<T>().Last(predicate);
}
public T LastOrDefault<T>(Func<T, bool> predicate)
{
return Cast<T>().LastOrDefault(predicate);
}
public dynamic Single()
{
return _sourceEnumerator.Current.Single();
}
public dynamic SingleOrDefault()
{
return _sourceEnumerator.Current.SingleOrDefault();
}
public T Single<T>()
{
return Cast<T>().Single();
}
public T SingleOrDefault<T>()
{
return Cast<T>().SingleOrDefault();
}
public T Single<T>(Func<T, bool> predicate)
{
return Cast<T>().Single(predicate);
}
public T SingleOrDefault<T>(Func<T, bool> predicate)
{
return Cast<T>().SingleOrDefault(predicate);
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.IEnumerator"/> that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>1</filterpriority>
public IEnumerator GetEnumerator()
{
return new DynamicEnumerator(_sourceEnumerator.Current);
}
public override bool TryConvert(ConvertBinder binder, out object result)
{
if (binder.Type == typeof(IEnumerable<object>))
{
result = Cast<object>();
return true;
}
if (ConcreteCollectionTypeCreator.IsCollectionType(binder.Type))
{
if (ConcreteCollectionTypeCreator.TryCreate(binder.Type, this, out result))
return true;
}
return base.TryConvert(binder, out result);
}
class DynamicEnumerator : IEnumerator, IDisposable
{
private readonly IEnumerator<dynamic> _enumerator;
public DynamicEnumerator(IEnumerable<dynamic> source)
{
_enumerator = source.GetEnumerator();
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
_enumerator.Dispose();
}
/// <summary>
/// Advances the enumerator to the next element of the collection.
/// </summary>
/// <returns>
/// true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
public bool MoveNext()
{
return _enumerator.MoveNext();
}
/// <summary>
/// Sets the enumerator to its initial position, which is before the first element in the collection.
/// </summary>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception><filterpriority>2</filterpriority>
public void Reset()
{
_enumerator.Reset();
}
/// <summary>
/// Gets the current element in the collection.
/// </summary>
/// <returns>
/// The current element in the collection.
/// </returns>
/// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element.</exception><filterpriority>2</filterpriority>
public object Current
{
get { return _enumerator.Current; }
}
}
/// <summary>
/// Creates a single-set <see cref="SimpleResultSet"/> from the specified source.
/// </summary>
/// <param name="source">The source.</param>
public static SimpleResultSet Create(IEnumerable<IDictionary<string,object>> source)
{
var q = from dict in source
select new SimpleRecord(dict);
return new SimpleResultSet(q);
}
/// <summary>
/// Creates a single-set <see cref="SimpleResultSet"/> from the specified source.
/// </summary>
/// <param name="source">The source.</param>
public static SimpleResultSet Create(IEnumerable<IDictionary<string, object>> source, string tableName, DataStrategy dataStrategy)
{
var q = from dict in source
where dict != null
select new SimpleRecord(dict, tableName, dataStrategy);
return new SimpleResultSet(q);
}
/// <summary>
/// Creates a multi-set <see cref="SimpleResultSet"/> from the specified sources.
/// </summary>
/// <param name="sources">The sources.</param>
/// <returns></returns>
public static SimpleResultSet Create(IEnumerable<IEnumerable<IDictionary<string, object>>> sources)
{
var q = from source in sources
select from dict in source
select new SimpleRecord(dict);
return new SimpleResultSet(q);
}
internal void SetOutputValues(IDictionary<string,object> outputValues)
{
_outputValues = outputValues;
}
}
}