forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataReaderMultipleEnumerator.cs
More file actions
90 lines (81 loc) · 2.54 KB
/
DataReaderMultipleEnumerator.cs
File metadata and controls
90 lines (81 loc) · 2.54 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
namespace Simple.Data.Ado
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
class DataReaderMultipleEnumerator : IEnumerator<IEnumerable<IDictionary<string, object>>>
{
private readonly IDisposable _connectionDisposable;
private readonly IDbConnection _connection;
private IDictionary<string, int> _index;
private readonly IDbCommand _command;
private IDataReader _reader;
private bool _lastRead;
public DataReaderMultipleEnumerator(IDbCommand command, IDbConnection connection) : this(command, connection, null)
{
}
public DataReaderMultipleEnumerator(IDbCommand command, IDbConnection connection, IDictionary<string, int> index)
{
_command = command;
_connection = connection;
_connectionDisposable = _connection.MaybeDisposable();
_index = index;
}
public void Dispose()
{
using (_connectionDisposable)
using (_command)
using (_reader)
{
/* NO-OP */
}
}
public bool MoveNext()
{
if (_reader == null)
{
ExecuteReader();
_lastRead = true;
return true;
}
_lastRead = _reader.NextResult();
return _lastRead;
}
private void ExecuteReader()
{
try
{
_connection.OpenIfClosed();
_reader = _command.ExecuteReader();
_index = _index ?? _reader.CreateDictionaryIndex();
}
catch (DbException ex)
{
throw new AdoAdapterException(ex.Message, ex);
}
}
public void Reset()
{
if (_reader != null) _reader.Dispose();
ExecuteReader();
}
public IEnumerable<IDictionary<string, object>> Current
{
get
{
if (!_lastRead) throw new InvalidOperationException();
var index = _reader.CreateDictionaryIndex();
while (_reader.Read())
{
yield return _reader.ToDictionary(index);
}
}
}
object IEnumerator.Current
{
get { return Current; }
}
}
}