forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNHybridDataReader.cs
442 lines (392 loc) · 10 KB
/
NHybridDataReader.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
namespace NHibernate.Driver
{
/// <summary>
/// An implementation of <see cref="DbDataReader"/> that will work with either an
/// <see cref="DbDataReader"/> returned by Execute or with an <see cref="DbDataReader"/>
/// whose contents have been read into a <see cref="NDataReader"/>.
/// </summary>
/// <remarks>
/// <para>
/// This allows NHibernate to use the underlying <see cref="DbDataReader"/> for as long as
/// possible without the need to read everything into the <see cref="NDataReader"/>.
/// </para>
/// <para>
/// The consumer of the <see cref="DbDataReader"/> returned from <see cref="Engine.IBatcher"/> does
/// not need to know the underlying reader and can use it the same even if it switches from an
/// <see cref="DbDataReader"/> to <see cref="NDataReader"/> in the middle of its use.
/// </para>
/// </remarks>
public partial class NHybridDataReader : DbDataReader
{
private readonly INHibernateLogger log = NHibernateLogger.For(typeof(NHybridDataReader));
private DbDataReader _reader;
private bool _isMidstream;
public DbDataReader Target { get { return _reader; } }
protected NHybridDataReader() { }
/// <summary>
/// Initializes a new instance of the <see cref="NHybridDataReader"/> class.
/// </summary>
/// <param name="reader">The underlying DbDataReader to use.</param>
public static NHybridDataReader Create(DbDataReader reader)
{
return Create(reader, false);
}
/// <summary>
/// Initializes a new instance of the NHybridDataReader class.
/// </summary>
/// <param name="reader">The underlying DbDataReader to use.</param>
/// <param name="inMemory"><see langword="true" /> if the contents of the DbDataReader should be read into memory right away.</param>
public static NHybridDataReader Create(DbDataReader reader, bool inMemory)
{
var dataReader = new NHybridDataReader();
if (inMemory)
{
dataReader._reader = NDataReader.Create(reader, false);
}
else
{
dataReader._reader = reader;
}
return dataReader;
}
/// <summary>
/// Reads all of the contents into memory because another <see cref="DbDataReader"/>
/// needs to be opened.
/// </summary>
/// <remarks>
/// This will result in a no op if the reader is closed or is already in memory.
/// </remarks>
public void ReadIntoMemory()
{
if (_reader.IsClosed == false && _reader.GetType() != typeof(NDataReader))
{
if (log.IsDebugEnabled())
{
log.Debug("Moving DbDataReader into an NDataReader. It was converted in midstream {0}", _isMidstream);
}
_reader = NDataReader.Create(_reader, _isMidstream);
}
}
/// <summary>
/// Gets if the object is in the middle of reading a Result.
/// </summary>
/// <value><see langword="true" /> if NextResult and Read have been called on the <see cref="DbDataReader"/>.</value>
public bool IsMidstream
{
get { return _isMidstream; }
}
/// <summary></summary>
public override int RecordsAffected
{
get { return _reader.RecordsAffected; }
}
public override bool HasRows
{
get { return _reader.HasRows; }
}
/// <summary></summary>
public override bool IsClosed
{
get { return _reader.IsClosed; }
}
/// <summary></summary>
public override bool NextResult()
{
// we are not in middle of a result
_isMidstream = false;
return _reader.NextResult();
}
/// <summary></summary>
public override void Close()
{
_reader.Close();
}
/// <summary></summary>
public override bool Read()
{
_isMidstream = _reader.Read();
return _isMidstream;
}
public override async Task<bool> ReadAsync(CancellationToken cancellationToken)
{
_isMidstream = await _reader.ReadAsync(cancellationToken).ConfigureAwait(false);
return _isMidstream;
}
public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
{
_isMidstream = false;
return _reader.NextResultAsync(cancellationToken);
}
public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
{
return _reader.IsDBNullAsync(ordinal, cancellationToken);
}
/// <summary></summary>
public override int Depth
{
get { return _reader.Depth; }
}
/// <summary></summary>
public override DataTable GetSchemaTable()
{
return _reader.GetSchemaTable();
}
/// <summary>
/// A flag to indicate if <c>Disose()</c> has been called.
/// </summary>
private bool disposed;
/// <summary>
/// Finalizer that ensures the object is correctly disposed of.
/// </summary>
~NHybridDataReader()
{
Dispose(false);
}
/// <summary>
/// Takes care of freeing the managed and unmanaged resources that
/// this class is responsible for.
/// </summary>
/// <param name="disposing">Indicates if this NHybridDataReader is being Disposed of or Finalized.</param>
/// <remarks>
/// If this NHybridDataReader is being Finalized (<c>isDisposing==false</c>) then make sure not
/// to call any methods that could potentially bring this NHybridDataReader back to life.
/// </remarks>
protected override void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing && _reader != null)
{
_reader.Dispose();
_reader = null;
}
disposed = true;
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override int GetInt32(int i)
{
return _reader.GetInt32(i);
}
/// <summary></summary>
public override object this[string name]
{
get { return _reader[name]; }
}
/// <summary></summary>
public override object this[int i]
{
get { return _reader[i]; }
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override object GetValue(int i)
{
return _reader.GetValue(i);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override bool IsDBNull(int i)
{
return _reader.IsDBNull(i);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <param name="fieldOffset"></param>
/// <param name="buffer"></param>
/// <param name="bufferoffset"></param>
/// <param name="length"></param>
/// <returns></returns>
public override long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
return _reader.GetBytes(i, fieldOffset, buffer, bufferoffset, length);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override byte GetByte(int i)
{
return _reader.GetByte(i);
}
public override IEnumerator GetEnumerator()
{
return _reader.GetEnumerator();
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override System.Type GetFieldType(int i)
{
return _reader.GetFieldType(i);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override decimal GetDecimal(int i)
{
return _reader.GetDecimal(i);
}
/// <summary>
///
/// </summary>
/// <param name="values"></param>
/// <returns></returns>
public override int GetValues(object[] values)
{
return _reader.GetValues(values);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override string GetName(int i)
{
return _reader.GetName(i);
}
/// <summary></summary>
public override int FieldCount
{
get { return _reader.FieldCount; }
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override long GetInt64(int i)
{
return _reader.GetInt64(i);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override double GetDouble(int i)
{
return _reader.GetDouble(i);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override bool GetBoolean(int i)
{
return _reader.GetBoolean(i);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override Guid GetGuid(int i)
{
return _reader.GetGuid(i);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override DateTime GetDateTime(int i)
{
return _reader.GetDateTime(i);
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public override int GetOrdinal(string name)
{
return _reader.GetOrdinal(name);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override string GetDataTypeName(int i)
{
return _reader.GetDataTypeName(i);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override float GetFloat(int i)
{
return _reader.GetFloat(i);
}
protected override DbDataReader GetDbDataReader(int ordinal)
{
return _reader.GetData(ordinal);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <param name="fieldoffset"></param>
/// <param name="buffer"></param>
/// <param name="bufferoffset"></param>
/// <param name="length"></param>
/// <returns></returns>
public override long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
{
return _reader.GetChars(i, fieldoffset, buffer, bufferoffset, length);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override string GetString(int i)
{
return _reader.GetString(i);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override char GetChar(int i)
{
return _reader.GetChar(i);
}
/// <summary>
///
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public override short GetInt16(int i)
{
return _reader.GetInt16(i);
}
}
}