• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 10320167017

09 Aug 2024 12:50PM UTC coverage: 53.334%. First build
10320167017

push

github

pwelter34
add DateOnly and TimeOnly reader extensions

1675 of 3736 branches covered (44.83%)

Branch coverage included in aggregate %.

8 of 36 new or added lines in 2 files covered. (22.22%)

4292 of 7452 relevant lines covered (57.6%)

223.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

24.54
/src/FluentCommand/Extensions/DataRecordExtensions.cs
1
using System;
2
using System.Data;
3
using System.Data.Common;
4

5
namespace FluentCommand.Extensions;
6

7
/// <summary>
8
/// Extension methods for <see cref="IDataRecord"/>.
9
/// </summary>
10
public static class DataRecordExtensions
11
{
12
    /// <summary>Gets the value of the specified column as a <see cref="bool"/>.</summary>
13
    /// <param name="dataRecord">The data record.</param>
14
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
15
    /// <returns>The value of the column.</returns>
16
    /// <remarks>Returns <see langword="false"/> for <see langword="null"/>.</remarks>
17
    public static bool GetBoolean(this IDataRecord dataRecord, string name)
18
    {
19
        int ordinal = dataRecord.GetOrdinal(name);
135✔
20
        return !dataRecord.IsDBNull(ordinal) && dataRecord.GetBoolean(ordinal);
135!
21
    }
22

23
    /// <summary>Gets the value of the specified column as a <see cref="bool"/>.</summary>
24
    /// <param name="dataRecord">The data record.</param>
25
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
26
    /// <returns>The value of the column.</returns>
27
    /// <remarks>Returns <see langword="false"/> for <see langword="null"/>.</remarks>
28
    public static bool? GetBooleanNull(this IDataRecord dataRecord, string name)
29
    {
30
        int ordinal = dataRecord.GetOrdinal(name);
×
31
        return !dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetBoolean(ordinal);
×
32
    }
33

34
    /// <summary>Gets the 8-bit unsigned integer value of the specified column.</summary>
35
    /// <param name="dataRecord">The data record.</param>
36
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
37
    /// <returns>The 8-bit unsigned integer value of the specified column.</returns>
38
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
39
    public static byte GetByte(this IDataRecord dataRecord, string name)
40
    {
41
        int ordinal = dataRecord.GetOrdinal(name);
×
42
        return dataRecord.IsDBNull(ordinal) ? (byte)0 : dataRecord.GetByte(ordinal);
×
43
    }
44

45
    /// <summary>Gets the 8-bit unsigned integer value of the specified column.</summary>
46
    /// <param name="dataRecord">The data record.</param>
47
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
48
    /// <returns>The 8-bit unsigned integer value of the specified column.</returns>
49
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
50
    public static byte? GetByteNull(this IDataRecord dataRecord, string name)
51
    {
52
        int ordinal = dataRecord.GetOrdinal(name);
×
53
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetByte(ordinal);
×
54
    }
55

56
    /// <summary>Gets a stream of bytes from the  specified column.</summary>
57
    /// <param name="dataRecord">The data record.</param>
58
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
59
    /// <returns>A stream of bytes of the specified column.</returns>
60
    /// <remarks>Returns empty array for <see langword="null"/>.</remarks>
61
    public static byte[] GetBytes(this IDataRecord dataRecord, string name)
62
    {
63
        int ordinal = dataRecord.GetOrdinal(name);
45✔
64
        if (dataRecord.IsDBNull(ordinal))
45✔
65
            return Array.Empty<byte>();
30✔
66

67
        return GetBytes(dataRecord, ordinal);
15✔
68
    }
69

70
    /// <summary>Gets a stream of bytes from the  specified column.</summary>
71
    /// <param name="dataRecord">The data record.</param>
72
    /// <param name="index">The zero-based column ordinal.</param>
73
    /// <returns>A stream of bytes of the specified column.</returns>
74
    public static byte[] GetBytes(this IDataRecord dataRecord, int index)
75
    {
76
        //get the length of data
77
        long size = dataRecord.GetBytes(index, 0, null, 0, 0);
1,197✔
78
        byte[] buffer = new byte[size];
1,197✔
79

80
        int bufferSize = size <= 1024 ? (int)size : 1024;
1,197!
81
        long bytesRead = 0;
1,197✔
82
        int offset = 0;
1,197✔
83

84
        while (bytesRead < size)
2,394✔
85
        {
86
            bytesRead += dataRecord.GetBytes(index, offset, buffer, offset, bufferSize);
1,197✔
87
            offset += bufferSize;
1,197✔
88
        }
89

90
        return buffer;
1,197✔
91

92
    }
93

94
    /// <summary>
95
    /// Reads a stream of bytes from the specified column offset into the buffer as an array, starting at the given buffer offset.
96
    /// </summary>
97
    /// <param name="dataRecord">The data record.</param>
98
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
99
    /// <param name="fieldOffset">The index within the field from which to start the read operation.</param>
100
    /// <param name="buffer">The buffer into which to read the stream of bytes.</param>
101
    /// <param name="bufferOffset">The index for buffer to start the read operation.</param>
102
    /// <param name="length">The number of bytes to read.</param>
103
    /// <returns>The actual number of bytes read.</returns>
104
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
105
    public static long GetBytes(this IDataRecord dataRecord, string name, long fieldOffset, byte[] buffer, int bufferOffset, int length)
106
    {
107
        int ordinal = dataRecord.GetOrdinal(name);
×
108
        return dataRecord.IsDBNull(ordinal) ? 0 : dataRecord.GetBytes(dataRecord.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
×
109
    }
110

111
    /// <summary>Gets the character value of the specified column.</summary>
112
    /// <param name="dataRecord">The data record.</param>
113
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
114
    /// <returns>The character value of the specified column.</returns>
115
    /// <remarks>Returns Char.MinValue for <see langword="null"/>.</remarks>
116
    public static char GetChar(this IDataRecord dataRecord, string name)
117
    {
118
        int ordinal = dataRecord.GetOrdinal(name);
×
119
        return dataRecord.IsDBNull(ordinal) ? char.MinValue : dataRecord.GetChar(ordinal);
×
120
    }
121

122
    /// <summary>Gets the character value of the specified column.</summary>
123
    /// <param name="dataRecord">The data record.</param>
124
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
125
    /// <returns>The character value of the specified column.</returns>
126
    /// <remarks>Returns Char.MinValue for <see langword="null"/>.</remarks>
127
    public static char? GetCharNull(this IDataRecord dataRecord, string name)
128
    {
129
        int ordinal = dataRecord.GetOrdinal(name);
×
130
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetChar(ordinal);
×
131
    }
132

133
    /// <summary>
134
    /// Reads a stream of characters from the specified column offset into the buffer as an array, starting at the given buffer offset.
135
    /// </summary>
136
    /// <param name="dataRecord">The data record.</param>
137
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
138
    /// <param name="fieldOffset">The index within the row from which to start the read operation.</param>
139
    /// <param name="buffer">The buffer into which to read the stream of bytes.</param>
140
    /// <param name="bufferOffset">The index for buffer to start the read operation. </param>
141
    /// <param name="length">The number of bytes to read.</param>
142
    /// <returns>The actual number of characters read.</returns>
143
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
144
    public static long GetChars(this IDataRecord dataRecord, string name, long fieldOffset, char[] buffer, int bufferOffset, int length)
145
    {
146
        int ordinal = dataRecord.GetOrdinal(name);
×
147
        return dataRecord.IsDBNull(ordinal) ? 0 : dataRecord.GetChars(dataRecord.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
×
148
    }
149

150
    /// <summary>
151
    /// Returns an <see cref="IDataReader"/> for the specified column <paramref name="name"/>.
152
    /// </summary>
153
    /// <param name="dataRecord">The data record.</param>
154
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
155
    /// <returns>The <see cref="IDataReader"/> for the specified column <paramref name="name"/>.</returns>
156
    public static IDataReader GetData(this IDataRecord dataRecord, string name)
157
    {
158
        int ordinal = dataRecord.GetOrdinal(name);
×
159
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetData(ordinal);
×
160
    }
161

162
    /// <summary>Gets the data type information for the specified field.</summary>
163
    /// <param name="dataRecord">The data record.</param>
164
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
165
    /// <returns>The data type information for the specified field.</returns>
166
    public static string GetDataTypeName(this IDataRecord dataRecord, string name)
167
    {
168
        int ordinal = dataRecord.GetOrdinal(name);
×
169
        return dataRecord.GetDataTypeName(ordinal);
×
170
    }
171

172
    /// <summary>Gets the date and time data value of the specified field.</summary>
173
    /// <param name="dataRecord">The data record.</param>
174
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
175
    /// <returns>The date and time data value of the specified field.</returns>
176
    /// <remarks>Returns DateTime.MinValue for <see langword="null"/>.</remarks>
177
    public static DateTime GetDateTime(this IDataRecord dataRecord, string name)
178
    {
179
        int ordinal = dataRecord.GetOrdinal(name);
×
180
        return dataRecord.IsDBNull(ordinal) ? DateTime.MinValue : dataRecord.GetDateTime(ordinal);
×
181
    }
182

183
    /// <summary>Gets the date and time data value of the specified field.</summary>
184
    /// <param name="dataRecord">The data record.</param>
185
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
186
    /// <returns>The date and time data value of the specified field.</returns>
187
    /// <remarks>Returns DateTime.MinValue for <see langword="null"/>.</remarks>
188
    public static DateTime? GetDateTimeNull(this IDataRecord dataRecord, string name)
189
    {
190
        int ordinal = dataRecord.GetOrdinal(name);
×
191
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetDateTime(ordinal);
×
192
    }
193

194
    /// <summary>Gets the date and time data value of the specified field.</summary>
195
    /// <param name="dataRecord">The data record.</param>
196
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
197
    /// <returns>The date and time data value of the specified field.</returns>
198
    /// <remarks>Returns DateTime.MinValue for <see langword="null"/>.</remarks>
199
    public static DateTimeOffset GetDateTimeOffset(this IDataRecord dataRecord, string name)
200
    {
201
        int ordinal = dataRecord.GetOrdinal(name);
90✔
202
        if (dataRecord.IsDBNull(ordinal))
90!
203
            return DateTimeOffset.MinValue;
×
204

205
        return GetDateTimeOffset(dataRecord, ordinal);
90✔
206
    }
207

208
    /// <summary>Gets the date and time data value of the specified field.</summary>
209
    /// <param name="dataRecord">The data record.</param>
210
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
211
    /// <returns>The date and time data value of the specified field.</returns>
212
    /// <remarks>Returns DateTime.MinValue for <see langword="null"/>.</remarks>
213
    public static DateTimeOffset? GetDateTimeOffsetNull(this IDataRecord dataRecord, string name)
214
    {
215
        int ordinal = dataRecord.GetOrdinal(name);
90✔
216
        if (dataRecord.IsDBNull(ordinal))
90!
217
            return null;
90✔
218

219
        return GetDateTimeOffset(dataRecord, ordinal);
×
220
    }
221

222
    /// <summary>Gets the date and time data value of the specified field.</summary>
223
    /// <param name="dataRecord">The data record.</param>
224
    /// <param name="index">The zero-based column ordinal.</param>
225
    /// <returns>The date and time data value of the specified field.</returns>
226
    public static DateTimeOffset GetDateTimeOffset(this IDataRecord dataRecord, int index)
227
    {
228
        if (dataRecord is DbDataReader dataReader)
2,625!
229
            return dataReader.GetFieldValue<DateTimeOffset>(index);
2,625✔
230

231
        var value = dataRecord.GetValue(index);
×
232
        if (value is DateTimeOffset offset)
×
233
            return offset;
×
234

235
        var date = dataRecord.GetDateTime(index);
×
236
        date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
×
237

238
        return new DateTimeOffset(date, TimeSpan.Zero);
×
239
    }
240

241
    /// <summary>Gets the fixed-position numeric value of the specified field.</summary>
242
    /// <param name="dataRecord">The data record.</param>
243
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
244
    /// <returns>The fixed-position numeric value of the specified field.</returns>
245
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
246
    public static decimal GetDecimal(this IDataRecord dataRecord, string name)
247
    {
248
        int ordinal = dataRecord.GetOrdinal(name);
×
249
        return dataRecord.IsDBNull(ordinal) ? 0 : dataRecord.GetDecimal(ordinal);
×
250
    }
251

252
    /// <summary>Gets the fixed-position numeric value of the specified field.</summary>
253
    /// <param name="dataRecord">The data record.</param>
254
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
255
    /// <returns>The fixed-position numeric value of the specified field.</returns>
256
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
257
    public static decimal? GetDecimalNull(this IDataRecord dataRecord, string name)
258
    {
259
        int ordinal = dataRecord.GetOrdinal(name);
×
260
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetDecimal(ordinal);
×
261
    }
262

263
    /// <summary>Gets the double-precision floating point number of the specified field.</summary>
264
    /// <param name="dataRecord">The data record.</param>
265
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
266
    /// <returns>The double-precision floating point number of the specified field.</returns>
267
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
268
    public static double GetDouble(this IDataRecord dataRecord, string name)
269
    {
270
        int ordinal = dataRecord.GetOrdinal(name);
×
271
        return dataRecord.IsDBNull(ordinal) ? 0D : dataRecord.GetDouble(ordinal);
×
272
    }
273

274
    /// <summary>Gets the double-precision floating point number of the specified field.</summary>
275
    /// <param name="dataRecord">The data record.</param>
276
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
277
    /// <returns>The double-precision floating point number of the specified field.</returns>
278
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
279
    public static double? GetDoubleNull(this IDataRecord dataRecord, string name)
280
    {
281
        int ordinal = dataRecord.GetOrdinal(name);
×
282
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetDouble(ordinal);
×
283
    }
284

285
    /// <summary>
286
    /// Gets the <see cref="Type"/> information corresponding to the type of <see cref="object"/> that would be returned from <see cref="GetValue"/>.
287
    /// </summary>
288
    /// <param name="dataRecord">The data record.</param>
289
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
290
    /// <returns>The <see cref="Type"/> information corresponding to the type of <see cref="object"/> that would be returned from <see cref="GetValue"/>.</returns>
291
    public static Type GetFieldType(this IDataRecord dataRecord, string name)
292
    {
293
        int ordinal = dataRecord.GetOrdinal(name);
1,200✔
294
        return dataRecord.GetFieldType(ordinal);
1,200✔
295
    }
296

297
    /// <summary>Gets the single-precision floating point number of the specified field.</summary>
298
    /// <param name="dataRecord">The data record.</param>
299
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
300
    /// <returns>The single-precision floating point number of the specified field.</returns>
301
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
302
    public static float GetFloat(this IDataRecord dataRecord, string name)
303
    {
304
        int ordinal = dataRecord.GetOrdinal(name);
×
305
        return dataRecord.IsDBNull(ordinal) ? 0F : dataRecord.GetFloat(ordinal);
×
306
    }
307

308
    /// <summary>Gets the single-precision floating point number of the specified field.</summary>
309
    /// <param name="dataRecord">The data record.</param>
310
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
311
    /// <returns>The single-precision floating point number of the specified field.</returns>
312
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
313
    public static float? GetFloatNull(this IDataRecord dataRecord, string name)
314
    {
315
        int ordinal = dataRecord.GetOrdinal(name);
×
316
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetFloat(ordinal);
×
317
    }
318

319
    /// <summary>
320
    /// Gets the <see cref="Guid"/> value of the specified field..
321
    /// </summary>
322
    /// <param name="dataRecord">The data record.</param>
323
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
324
    /// <returns>The <see cref="Guid"/> value of the specified field.</returns>
325
    /// <remarks>Returns Guid.Empty for <see langword="null"/>.</remarks>
326
    public static Guid GetGuid(this IDataRecord dataRecord, string name)
327
    {
328
        int ordinal = dataRecord.GetOrdinal(name);
45✔
329
        return dataRecord.IsDBNull(ordinal) ? Guid.Empty : dataRecord.GetGuid(ordinal);
45!
330
    }
331

332
    /// <summary>
333
    /// Gets the <see cref="Guid"/> value of the specified field..
334
    /// </summary>
335
    /// <param name="dataRecord">The data record.</param>
336
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
337
    /// <returns>The <see cref="Guid"/> value of the specified field.</returns>
338
    /// <remarks>Returns Guid.Empty for <see langword="null"/>.</remarks>
339
    public static Guid? GetGuidNull(this IDataRecord dataRecord, string name)
340
    {
341
        int ordinal = dataRecord.GetOrdinal(name);
×
342
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetGuid(ordinal);
×
343
    }
344

345
    /// <summary>Gets the 16-bit signed integer value of the specified field.</summary>
346
    /// <param name="dataRecord">The data record.</param>
347
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
348
    /// <returns>The 16-bit signed integer value of the specified field.</returns>
349
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
350
    public static short GetInt16(this IDataRecord dataRecord, string name)
351
    {
352
        int ordinal = dataRecord.GetOrdinal(name);
×
353
        return dataRecord.IsDBNull(ordinal) ? (short)0 : dataRecord.GetInt16(ordinal);
×
354
    }
355

356
    /// <summary>Gets the 16-bit signed integer value of the specified field.</summary>
357
    /// <param name="dataRecord">The data record.</param>
358
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
359
    /// <returns>The 16-bit signed integer value of the specified field.</returns>
360
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
361
    public static short? GetInt16Null(this IDataRecord dataRecord, string name)
362
    {
363
        int ordinal = dataRecord.GetOrdinal(name);
×
364
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetInt16(ordinal);
×
365
    }
366

367
    /// <summary>Gets the 32-bit signed integer value of the specified field.</summary>
368
    /// <param name="dataRecord">The data record.</param>
369
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
370
    /// <returns>The 32-bit signed integer value of the specified field.</returns>
371
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
372
    public static int GetInt32(this IDataRecord dataRecord, string name)
373
    {
374
        int ordinal = dataRecord.GetOrdinal(name);
45✔
375
        return dataRecord.IsDBNull(ordinal) ? 0 : dataRecord.GetInt32(ordinal);
45!
376
    }
377

378
    /// <summary>Gets the 32-bit signed integer value of the specified field.</summary>
379
    /// <param name="dataRecord">The data record.</param>
380
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
381
    /// <returns>The 32-bit signed integer value of the specified field.</returns>
382
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
383
    public static int? GetInt32Null(this IDataRecord dataRecord, string name)
384
    {
385
        int ordinal = dataRecord.GetOrdinal(name);
×
386
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetInt32(ordinal);
×
387
    }
388

389
    /// <summary>Gets the 64-bit signed integer value of the specified field.</summary>
390
    /// <param name="dataRecord">The data record.</param>
391
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
392
    /// <returns>The 64-bit signed integer value of the specified field.</returns>
393
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
394
    public static long GetInt64(this IDataRecord dataRecord, string name)
395
    {
396
        int ordinal = dataRecord.GetOrdinal(name);
×
397
        return dataRecord.IsDBNull(ordinal) ? 0 : dataRecord.GetInt64(ordinal);
×
398
    }
399

400
    /// <summary>Gets the 64-bit signed integer value of the specified field.</summary>
401
    /// <param name="dataRecord">The data record.</param>
402
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
403
    /// <returns>The 64-bit signed integer value of the specified field.</returns>
404
    /// <remarks>Returns 0 for <see langword="null"/>.</remarks>
405
    public static long? GetInt64Null(this IDataRecord dataRecord, string name)
406
    {
407
        int ordinal = dataRecord.GetOrdinal(name);
×
408
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetInt64(ordinal);
×
409
    }
410

411
    /// <summary>Gets the string value of the specified field.</summary>
412
    /// <param name="dataRecord">The data record.</param>
413
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
414
    /// <returns>The string value of the specified field.</returns>
415
    /// <remarks>Returns String.Empty for <see langword="null"/>.</remarks>
416
    public static string GetString(this IDataRecord dataRecord, string name)
417
    {
418
        int ordinal = dataRecord.GetOrdinal(name);
415✔
419
        return dataRecord.IsDBNull(ordinal) ? string.Empty : dataRecord.GetString(ordinal);
415✔
420
    }
421

422
    /// <summary>Gets the string value of the specified field.</summary>
423
    /// <param name="dataRecord">The data record.</param>
424
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
425
    /// <returns>The string value of the specified field.</returns>
426
    public static string GetStringNull(this IDataRecord dataRecord, string name)
427
    {
428
        int ordinal = dataRecord.GetOrdinal(name);
×
429
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetString(ordinal);
×
430
    }
431

432
#if NET6_0_OR_GREATER
433
    /// <summary>Gets the <see cref="DateOnly"/> value of the specified field.</summary>
434
    /// <param name="dataRecord">The data record.</param>
435
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
436
    /// <returns>The <see cref="DateOnly"/> value of the specified field.</returns>
437
    public static DateOnly GetDateOnly(this IDataRecord dataRecord, string name)
438
    {
NEW
439
        int ordinal = dataRecord.GetOrdinal(name);
×
NEW
440
        if (dataRecord.IsDBNull(ordinal))
×
NEW
441
            return default;
×
442

NEW
443
        if (dataRecord is DbDataReader dataReader)
×
NEW
444
            return dataReader.GetFieldValue<DateOnly>(ordinal);
×
445

NEW
446
        var dateTime = dataRecord.GetDateTime(ordinal);
×
NEW
447
        return DateOnly.FromDateTime(dateTime);
×
448
    }
449

450
    /// <summary>Gets the <see cref="DateOnly"/> value of the specified field.</summary>
451
    /// <param name="dataRecord">The data record.</param>
452
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
453
    /// <returns>The <see cref="DateOnly"/> value of the specified field.</returns>
454
    public static DateOnly? GetDateOnlyNull(this IDataRecord dataRecord, string name)
455
    {
NEW
456
        int ordinal = dataRecord.GetOrdinal(name);
×
NEW
457
        if (dataRecord.IsDBNull(ordinal))
×
NEW
458
            return null;
×
459

NEW
460
        if (dataRecord is DbDataReader dataReader)
×
NEW
461
            return dataReader.GetFieldValue<DateOnly>(ordinal);
×
462

NEW
463
        var dateTime = dataRecord.GetDateTime(ordinal);
×
NEW
464
        return DateOnly.FromDateTime(dateTime);
×
465
    }
466

467
    /// <summary>Gets the <see cref="TimeOnly"/> value of the specified field.</summary>
468
    /// <param name="dataRecord">The data record.</param>
469
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
470
    /// <returns>The <see cref="TimeOnly"/> value of the specified field.</returns>
471
    public static TimeOnly GetTimeOnly(this IDataRecord dataRecord, string name)
472
    {
NEW
473
        int ordinal = dataRecord.GetOrdinal(name);
×
NEW
474
        if (dataRecord.IsDBNull(ordinal))
×
NEW
475
            return default;
×
476

NEW
477
        if (dataRecord is DbDataReader dataReader)
×
NEW
478
            return dataReader.GetFieldValue<TimeOnly>(ordinal);
×
479

NEW
480
        var dateTime = dataRecord.GetDateTime(ordinal);
×
NEW
481
        return TimeOnly.FromDateTime(dateTime);
×
482
    }
483

484
    /// <summary>Gets the <see cref="TimeOnly"/> value of the specified field.</summary>
485
    /// <param name="dataRecord">The data record.</param>
486
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
487
    /// <returns>The <see cref="TimeOnly"/> value of the specified field.</returns>
488
    public static TimeOnly? GetTimeOnlyNull(this IDataRecord dataRecord, string name)
489
    {
NEW
490
        int ordinal = dataRecord.GetOrdinal(name);
×
NEW
491
        if (dataRecord.IsDBNull(ordinal))
×
NEW
492
            return null;
×
493

NEW
494
        if (dataRecord is DbDataReader dataReader)
×
NEW
495
            return dataReader.GetFieldValue<TimeOnly>(ordinal);
×
496

NEW
497
        var dateTime = dataRecord.GetDateTime(ordinal);
×
NEW
498
        return TimeOnly.FromDateTime(dateTime);
×
499
    }
500
#endif
501

502
    /// <summary>Gets the value of the specified field.</summary>
503
    /// <param name="dataRecord">The data record.</param>
504
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
505
    /// <returns>The <see cref="object"/> which will contain the field value upon return.</returns>
506
    public static object GetValue(this IDataRecord dataRecord, string name)
507
    {
508
        int ordinal = dataRecord.GetOrdinal(name);
2,400✔
509
        return dataRecord.IsDBNull(ordinal) ? null : dataRecord.GetValue(ordinal);
2,400✔
510
    }
511

512
    /// <summary>
513
    /// Gets the value of the specified field.
514
    /// </summary>
515
    /// <typeparam name="T">The record value type</typeparam>
516
    /// <param name="dataRecord">The data record.</param>
517
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
518
    /// <returns>
519
    /// The <typeparamref name="T"/> which will contain the field value upon return.
520
    /// </returns>
521
    public static T GetValue<T>(this IDataRecord dataRecord, string name)
522
    {
523
        int ordinal = dataRecord.GetOrdinal(name);
×
524
        return dataRecord.GetValue<T>(ordinal);
×
525
    }
526

527
    /// <summary>
528
    /// Gets the value of the specified field.
529
    /// </summary>
530
    /// <typeparam name="T">The record value type</typeparam>
531
    /// <param name="dataRecord">The data record.</param>
532
    /// <param name="index">The zero-based column ordinal.</param>
533
    /// <returns>
534
    /// The <typeparamref name="T"/> which will contain the field value upon return.
535
    /// </returns>
536
    public static T GetValue<T>(this IDataRecord dataRecord, int index)
537
    {
538
        if (dataRecord.IsDBNull(index))
37!
539
            return default;
×
540

541
        if (dataRecord is DbDataReader dataReader)
37!
542
            return dataReader.GetFieldValue<T>(index);
37✔
543

544
        return (T)dataRecord.GetValue(index);
×
545
    }
546

547
    /// <summary>Determines whether the specified field is set to <see langword="null"/>.</summary>
548
    /// <param name="dataRecord">The data record.</param>
549
    /// <param name="name">The <paramref name="name"/> of the field to find.</param>
550
    /// <returns><c>true</c> if the specified field is set to <see langword="null"/>; otherwise, <c>false</c>.</returns>
551
    public static bool IsDBNull(this IDataRecord dataRecord, string name)
552
    {
553
        int ordinal = dataRecord.GetOrdinal(name);
×
554
        return dataRecord.IsDBNull(ordinal);
×
555
    }
556
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc