/
DbDataReader.cs
340 lines (278 loc) · 11.6 KB
/
DbDataReader.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics.CodeAnalysis;
namespace System.Data.Common
{
public abstract class DbDataReader : MarshalByRefObject, IDataReader, IEnumerable, IAsyncDisposable
{
protected DbDataReader() : base() { }
public abstract int Depth { get; }
public abstract int FieldCount { get; }
public abstract bool HasRows { get; }
public abstract bool IsClosed { get; }
public abstract int RecordsAffected { get; }
public virtual int VisibleFieldCount => FieldCount;
public abstract object this[int ordinal] { get; }
public abstract object this[string name] { get; }
public virtual void Close() { }
public virtual Task CloseAsync()
{
try
{
Close();
return Task.CompletedTask;
}
catch (Exception e)
{
return Task.FromException(e);
}
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void Dispose() => Dispose(true);
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Close();
}
}
public virtual ValueTask DisposeAsync()
{
Dispose();
return default;
}
public abstract string GetDataTypeName(int ordinal);
[EditorBrowsable(EditorBrowsableState.Never)]
public abstract IEnumerator GetEnumerator();
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
public abstract Type GetFieldType(int ordinal);
public abstract string GetName(int ordinal);
public abstract int GetOrdinal(string name);
/// <summary>
/// Returns a <see cref="DataTable" /> that describes the column metadata of the ><see cref="DbDataReader" />.
///
/// Returns <see langword="null" /> if the executed command returned no resultset, or after
/// <see cref="NextResult" /> returns <see langword="false" />.
/// </summary>
/// <returns>A <see cref="DataTable" /> that describes the column metadata.</returns>
/// <exception cref="InvalidOperationException">The <see cref="DbDataReader" /> is closed.</exception>
public virtual DataTable? GetSchemaTable()
{
throw new NotSupportedException();
}
/// <summary>
/// This is the asynchronous version of <see cref="GetSchemaTable()" />.
/// Providers should override with an appropriate implementation.
/// The cancellation token can optionally be honored.
/// The default implementation invokes the synchronous <see cref="GetSchemaTable()" /> call and
/// returns a completed task.
/// The default implementation will return a cancelled task if passed an already cancelled cancellationToken.
/// Exceptions thrown by <see cref="GetSchemaTable()" /> will be communicated via the returned Task
/// Exception property.
/// </summary>
/// <param name="cancellationToken">The cancellation instruction.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public virtual Task<DataTable?> GetSchemaTableAsync(CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<DataTable?>(cancellationToken);
}
try
{
return Task.FromResult(GetSchemaTable());
}
catch (Exception e)
{
return Task.FromException<DataTable?>(e);
}
}
/// <summary>
/// This is the asynchronous version of <see cref="DbDataReaderExtensions.GetColumnSchema(DbDataReader)" />.
/// Providers should override with an appropriate implementation.
/// The cancellation token can optionally be honored.
/// The default implementation invokes the synchronous
/// <see cref="DbDataReaderExtensions.GetColumnSchema(DbDataReader)" /> call and returns a completed task.
/// The default implementation will return a cancelled task if passed an already cancelled cancellationToken.
/// Exceptions thrown by <see cref="DbDataReaderExtensions.GetColumnSchema(DbDataReader)" /> will be
/// communicated via the returned Task Exception property.
/// </summary>
/// <param name="cancellationToken">The cancellation instruction.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public virtual Task<ReadOnlyCollection<DbColumn>> GetColumnSchemaAsync(
CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<ReadOnlyCollection<DbColumn>>(cancellationToken);
}
try
{
return Task.FromResult(this.GetColumnSchema());
}
catch (Exception e)
{
return Task.FromException<ReadOnlyCollection<DbColumn>>(e);
}
}
public abstract bool GetBoolean(int ordinal);
public abstract byte GetByte(int ordinal);
public abstract long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOffset, int length);
public abstract char GetChar(int ordinal);
public abstract long GetChars(int ordinal, long dataOffset, char[]? buffer, int bufferOffset, int length);
[EditorBrowsable(EditorBrowsableState.Never)]
public DbDataReader GetData(int ordinal) => GetDbDataReader(ordinal);
IDataReader IDataRecord.GetData(int ordinal) => GetDbDataReader(ordinal);
protected virtual DbDataReader GetDbDataReader(int ordinal)
{
throw ADP.NotSupported();
}
public abstract DateTime GetDateTime(int ordinal);
public abstract decimal GetDecimal(int ordinal);
public abstract double GetDouble(int ordinal);
public abstract float GetFloat(int ordinal);
public abstract Guid GetGuid(int ordinal);
public abstract short GetInt16(int ordinal);
public abstract int GetInt32(int ordinal);
public abstract long GetInt64(int ordinal);
[EditorBrowsable(EditorBrowsableState.Never)]
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
public virtual Type GetProviderSpecificFieldType(int ordinal)
{
// NOTE: This is virtual because not all providers may choose to support
// this method, since it was added in Whidbey.
return GetFieldType(ordinal);
}
[
EditorBrowsable(EditorBrowsableState.Never)
]
public virtual object GetProviderSpecificValue(int ordinal)
{
// NOTE: This is virtual because not all providers may choose to support
// this method, since it was added in Whidbey
return GetValue(ordinal);
}
[
EditorBrowsable(EditorBrowsableState.Never)
]
public virtual int GetProviderSpecificValues(object[] values) => GetValues(values);
public abstract string GetString(int ordinal);
public virtual Stream GetStream(int ordinal)
{
using (MemoryStream bufferStream = new MemoryStream())
{
long bytesRead = 0;
long bytesReadTotal = 0;
byte[] buffer = new byte[4096];
do
{
bytesRead = GetBytes(ordinal, bytesReadTotal, buffer, 0, buffer.Length);
bufferStream.Write(buffer, 0, (int)bytesRead);
bytesReadTotal += bytesRead;
}
while (bytesRead > 0);
return new MemoryStream(bufferStream.ToArray(), false);
}
}
public virtual TextReader GetTextReader(int ordinal)
{
if (IsDBNull(ordinal))
{
return new StringReader(string.Empty);
}
else
{
return new StringReader(GetString(ordinal));
}
}
public abstract object GetValue(int ordinal);
public virtual T GetFieldValue<T>(int ordinal) => (T)GetValue(ordinal);
public Task<T> GetFieldValueAsync<T>(int ordinal) =>
GetFieldValueAsync<T>(ordinal, CancellationToken.None);
public virtual Task<T> GetFieldValueAsync<T>(int ordinal, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return ADP.CreatedTaskWithCancellation<T>();
}
else
{
try
{
return Task.FromResult<T>(GetFieldValue<T>(ordinal));
}
catch (Exception e)
{
return Task.FromException<T>(e);
}
}
}
public abstract int GetValues(object[] values);
public abstract bool IsDBNull(int ordinal);
public Task<bool> IsDBNullAsync(int ordinal) => IsDBNullAsync(ordinal, CancellationToken.None);
public virtual Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return ADP.CreatedTaskWithCancellation<bool>();
}
else
{
try
{
return IsDBNull(ordinal) ? ADP.TrueTask : ADP.FalseTask;
}
catch (Exception e)
{
return Task.FromException<bool>(e);
}
}
}
public abstract bool NextResult();
public abstract bool Read();
public Task<bool> ReadAsync() => ReadAsync(CancellationToken.None);
public virtual Task<bool> ReadAsync(CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return ADP.CreatedTaskWithCancellation<bool>();
}
else
{
try
{
return Read() ? ADP.TrueTask : ADP.FalseTask;
}
catch (Exception e)
{
return Task.FromException<bool>(e);
}
}
}
public Task<bool> NextResultAsync() => NextResultAsync(CancellationToken.None);
public virtual Task<bool> NextResultAsync(CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return ADP.CreatedTaskWithCancellation<bool>();
}
else
{
try
{
return NextResult() ? ADP.TrueTask : ADP.FalseTask;
}
catch (Exception e)
{
return Task.FromException<bool>(e);
}
}
}
}
}