-
-
Notifications
You must be signed in to change notification settings - Fork 407
/
Copy pathSafeDataReader.cs
887 lines (792 loc) · 32 KB
/
SafeDataReader.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
//-----------------------------------------------------------------------
// <copyright file="SafeDataReader.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>This is a DataReader that 'fixes' any null values before</summary>
//-----------------------------------------------------------------------
using System.Data;
using System.Diagnostics.CodeAnalysis;
#if !NETSTANDARD2_0 && !NET8_0_OR_GREATER
using System.Data.SqlClient;
#endif
namespace Csla.Data
{
/// <summary>
/// This is an IDataReader that 'fixes' any null values before
/// they are returned to our business code.
/// </summary>
public class SafeDataReader : IDataReader
{
#if !NETSTANDARD2_0 && !NET8_0_OR_GREATER
private SqlDataReader? _sqlDataReader;
#endif
/// <summary>
/// Get a reference to the underlying data reader
/// object that actually contains the data from
/// the data source.
/// </summary>
protected IDataReader DataReader { get; }
/// <summary>
/// Initializes the SafeDataReader object to use data from
/// the provided DataReader object.
/// </summary>
/// <param name="dataReader">The source DataReader object containing the data.</param>
/// <exception cref="ArgumentNullException"><paramref name="dataReader"/> is <see langword="null"/>.</exception>
public SafeDataReader(IDataReader dataReader)
{
DataReader = dataReader ?? throw new ArgumentNullException(nameof(dataReader));
#if !NETSTANDARD2_0 && !NET8_0_OR_GREATER
_sqlDataReader = DataReader as SqlDataReader;
#endif
}
/// <summary>
/// Gets a string value from the datareader.
/// </summary>
/// <remarks>
/// Returns empty string for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public string GetString(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetString(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a string value from the datareader.
/// </summary>
/// <remarks>
/// Returns empty string for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual string GetString(int i)
{
if (DataReader.IsDBNull(i))
return string.Empty;
else
return DataReader.GetString(i);
}
/// <summary>
/// Gets a value of type <see cref="System.Object" /> from the datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public object? GetValue(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetValue(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a value of type <see cref="System.Object" /> from the datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
#pragma warning disable CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes). We are suppressing it because we do return null
public virtual object? GetValue(int i)
#pragma warning restore CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes).
{
if (DataReader.IsDBNull(i))
return null;
else
return DataReader.GetValue(i);
}
/// <summary>
/// Gets an integer from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public int GetInt32(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetInt32(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets an integer from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual int GetInt32(int i)
{
if (DataReader.IsDBNull(i))
return 0;
else
return DataReader.GetInt32(i);
}
/// <summary>
/// Gets a double from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public double GetDouble(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetDouble(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a double from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual double GetDouble(int i)
{
if (DataReader.IsDBNull(i))
return 0;
else
return DataReader.GetDouble(i);
}
/// <summary>
/// Gets a <see cref="SmartDate" /> from the datareader.
/// </summary>
/// <remarks>
/// A null is converted into min possible date
/// See Chapter 5 for more details on the SmartDate class.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public SmartDate GetSmartDate(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetSmartDate(DataReader.GetOrdinal(name), true);
}
/// <summary>
/// Gets a <see cref="SmartDate" /> from the datareader.
/// </summary>
/// <remarks>
/// A null is converted into the min possible date
/// See Chapter 5 for more details on the SmartDate class.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual SmartDate GetSmartDate(int i)
{
return GetSmartDate(i, true);
}
/// <summary>
/// Gets a <see cref="SmartDate" /> from the datareader.
/// </summary>
/// <remarks>
/// A null is converted into either the min or max possible date
/// depending on the MinIsEmpty parameter. See Chapter 5 for more
/// details on the SmartDate class.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <param name="minIsEmpty">
/// A flag indicating whether the min or max
/// value of a data means an empty date.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public SmartDate GetSmartDate(string name, bool minIsEmpty)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetSmartDate(DataReader.GetOrdinal(name), minIsEmpty);
}
/// <summary>
/// Gets a <see cref="SmartDate"/> from the datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
/// <param name="minIsEmpty">
/// A flag indicating whether the min or max
/// value of a data means an empty date.</param>
public virtual SmartDate GetSmartDate(int i, bool minIsEmpty)
{
if (DataReader.IsDBNull(i))
return new SmartDate(minIsEmpty);
else
return new SmartDate(
DataReader.GetDateTime(i), minIsEmpty);
}
/// <summary>
/// Gets a Guid value from the datareader.
/// </summary>
/// <remarks>
/// Returns Guid.Empty for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public Guid GetGuid(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetGuid(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a Guid value from the datareader.
/// </summary>
/// <remarks>
/// Returns Guid.Empty for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual Guid GetGuid(int i)
{
if (DataReader.IsDBNull(i))
return Guid.Empty;
else
return DataReader.GetGuid(i);
}
/// <summary>
/// Reads the next row of data from the datareader.
/// </summary>
public bool Read()
{
return DataReader.Read();
}
/// <summary>
/// Moves to the next result set in the datareader.
/// </summary>
public bool NextResult()
{
return DataReader.NextResult();
}
/// <summary>
/// Closes the datareader.
/// </summary>
public void Close()
{
DataReader.Close();
}
/// <summary>
/// Returns the depth property value from the datareader.
/// </summary>
public int Depth => DataReader.Depth;
/// <summary>
/// Returns the FieldCount property from the datareader.
/// </summary>
public int FieldCount => DataReader.FieldCount;
/// <summary>
/// Gets a boolean value from the datareader.
/// </summary>
/// <remarks>
/// Returns false for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public bool GetBoolean(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetBoolean(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a boolean value from the datareader.
/// </summary>
/// <remarks>
/// Returns false for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual bool GetBoolean(int i)
{
if (DataReader.IsDBNull(i))
return false;
else
return DataReader.GetBoolean(i);
}
/// <summary>
/// Gets a byte value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public byte GetByte(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetByte(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a byte value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual byte GetByte(int i)
{
if (DataReader.IsDBNull(i))
return 0;
else
return DataReader.GetByte(i);
}
/// <summary>
/// Invokes the GetBytes method of the underlying datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <param name="buffer">Array containing the data.</param>
/// <param name="bufferOffset">Offset position within the buffer.</param>
/// <param name="fieldOffset">Offset position within the field.</param>
/// <param name="length">Length of data to read.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public Int64 GetBytes(string name, Int64 fieldOffset, byte[] buffer, int bufferOffset, int length)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetBytes(DataReader.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
}
/// <summary>
/// Invokes the GetBytes method of the underlying datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
/// <param name="buffer">Array containing the data.</param>
/// <param name="bufferOffset">Offset position within the buffer.</param>
/// <param name="fieldOffset">Offset position within the field.</param>
/// <param name="length">Length of data to read.</param>
public virtual Int64 GetBytes(int i, Int64 fieldOffset, byte[]? buffer, int bufferOffset, int length)
{
if (DataReader.IsDBNull(i))
return 0;
else
return DataReader.GetBytes(i, fieldOffset, buffer, bufferOffset, length);
}
/// <summary>
/// Gets a char value from the datareader.
/// </summary>
/// <remarks>
/// Returns Char.MinValue for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public char GetChar(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetChar(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a char value from the datareader.
/// </summary>
/// <remarks>
/// Returns Char.MinValue for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual char GetChar(int i)
{
if (DataReader.IsDBNull(i))
return char.MinValue;
else
{
char[] myChar = new char[1];
DataReader.GetChars(i, 0, myChar, 0, 1);
return myChar[0];
}
}
/// <summary>
/// Invokes the GetChars method of the underlying datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <param name="buffer">Array containing the data.</param>
/// <param name="bufferOffset">Offset position within the buffer.</param>
/// <param name="fieldOffset">Offset position within the field.</param>
/// <param name="length">Length of data to read.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public Int64 GetChars(string name, Int64 fieldOffset, char[] buffer, int bufferOffset, int length)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetChars(DataReader.GetOrdinal(name), fieldOffset, buffer, bufferOffset, length);
}
/// <summary>
/// Invokes the GetChars method of the underlying datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
/// <param name="buffer">Array containing the data.</param>
/// <param name="bufferOffset">Offset position within the buffer.</param>
/// <param name="fieldOffset">Offset position within the field.</param>
/// <param name="length">Length of data to read.</param>
public virtual Int64 GetChars(int i, Int64 fieldOffset, char[]? buffer, int bufferOffset, int length)
{
if (DataReader.IsDBNull(i))
return 0;
else
return DataReader.GetChars(i, fieldOffset, buffer, bufferOffset, length);
}
/// <summary>
/// Invokes the GetData method of the underlying datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public IDataReader GetData(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetData(DataReader.GetOrdinal(name));
}
/// <summary>
/// Invokes the GetData method of the underlying datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual IDataReader GetData(int i)
{
return DataReader.GetData(i);
}
/// <summary>
/// Invokes the GetDataTypeName method of the underlying datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public string GetDataTypeName(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetDataTypeName(DataReader.GetOrdinal(name));
}
/// <summary>
/// Invokes the GetDataTypeName method of the underlying datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual string GetDataTypeName(int i)
{
return DataReader.GetDataTypeName(i);
}
/// <summary>
/// Gets a date value from the datareader.
/// </summary>
/// <remarks>
/// Returns DateTime.MinValue for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public virtual DateTime GetDateTime(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetDateTime(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a date value from the datareader.
/// </summary>
/// <remarks>
/// Returns DateTime.MinValue for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual DateTime GetDateTime(int i)
{
if (DataReader.IsDBNull(i))
return DateTime.MinValue;
else
return DataReader.GetDateTime(i);
}
/// <summary>
/// Gets an UTC date value from the datareader.
/// </summary>
/// <remarks>
/// Returns DateTimeOffset.MinValue for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public virtual DateTimeOffset GetDateTimeOffset(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetDateTimeOffset(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets an UTC date value from the datareader.
/// </summary>
/// <remarks>
/// Returns DateTimeOffset.MinValue for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual DateTimeOffset GetDateTimeOffset(int i)
{
if (DataReader.IsDBNull(i))
return DateTimeOffset.MinValue;
else
return (DateTimeOffset)DataReader.GetValue(i);
}
/// <summary>
/// Gets a decimal value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public decimal GetDecimal(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetDecimal(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a decimal value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual decimal GetDecimal(int i)
{
if (DataReader.IsDBNull(i))
return 0;
else
return DataReader.GetDecimal(i);
}
/// <summary>
/// Invokes the GetFieldType method of the underlying datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public Type GetFieldType(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetFieldType(DataReader.GetOrdinal(name));
}
/// <summary>
/// Invokes the GetFieldType method of the underlying datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties)]
public virtual Type GetFieldType(int i)
{
return DataReader.GetFieldType(i);
}
/// <summary>
/// Gets a Single value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public float GetFloat(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetFloat(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a Single value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual float GetFloat(int i)
{
if (DataReader.IsDBNull(i))
return 0;
else
return DataReader.GetFloat(i);
}
/// <summary>
/// Gets a Short value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public short GetInt16(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetInt16(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a Short value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual short GetInt16(int i)
{
if (DataReader.IsDBNull(i))
return 0;
else
return DataReader.GetInt16(i);
}
/// <summary>
/// Gets a Long value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public Int64 GetInt64(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return GetInt64(DataReader.GetOrdinal(name));
}
/// <summary>
/// Gets a Long value from the datareader.
/// </summary>
/// <remarks>
/// Returns 0 for null.
/// </remarks>
/// <param name="i">Ordinal column position of the value.</param>
public virtual Int64 GetInt64(int i)
{
if (DataReader.IsDBNull(i))
return 0;
else
return DataReader.GetInt64(i);
}
/// <summary>
/// Invokes the GetName method of the underlying datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual string GetName(int i)
{
return DataReader.GetName(i);
}
/// <summary>
/// Gets an ordinal value from the datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public int GetOrdinal(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
return DataReader.GetOrdinal(name);
}
/// <summary>
/// Invokes the GetSchemaTable method of the underlying datareader.
/// </summary>
public DataTable? GetSchemaTable()
{
return DataReader.GetSchemaTable();
}
/// <summary>
/// Invokes the GetValues method of the underlying datareader.
/// </summary>
/// <param name="values">An array of System.Object to
/// copy the values into.</param>
public int GetValues(object[] values)
{
return DataReader.GetValues(values);
}
/// <summary>
/// Returns the IsClosed property value from the datareader.
/// </summary>
public bool IsClosed => DataReader.IsClosed;
/// <summary>
/// Invokes the IsDBNull method of the underlying datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual bool IsDBNull(int i)
{
return DataReader.IsDBNull(i);
}
/// <summary>
/// Invokes the IsDBNull method of the underlying datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public virtual bool IsDBNull(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
int index = GetOrdinal(name);
return IsDBNull(index);
}
/// <summary>
/// Returns a value from the datareader.
/// </summary>
/// <param name="name">Name of the column containing the value.</param>
/// <exception cref="ArgumentException"><paramref name="name"/> is <see langword="null"/>, <see cref="string.Empty"/> or only consists of white spaces.</exception>
public object? this[string name]
{
#pragma warning disable CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes). We are suppressing it because we do return null
get
#pragma warning restore CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes).
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException(string.Format(Properties.Resources.StringNotNullOrWhiteSpaceException, nameof(name)), nameof(name));
object val = DataReader[name];
if (DBNull.Value.Equals(val))
return null;
else
return val;
}
}
/// <summary>
/// Returns a value from the datareader.
/// </summary>
/// <param name="i">Ordinal column position of the value.</param>
public virtual object? this[int i]
{
#pragma warning disable CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes). We are suppressing it because we do return null
get
#pragma warning restore CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes).
{
if (DataReader.IsDBNull(i))
return null;
else
return DataReader[i];
}
}
/// <summary>
/// Returns the RecordsAffected property value from the underlying datareader.
/// </summary>
public int RecordsAffected => DataReader.RecordsAffected;
#region IDisposable Support
private bool _disposedValue; // To detect redundant calls
/// <summary>
/// Disposes the object.
/// </summary>
/// <param name="disposing">True if called by
/// the public Dispose method.</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// free unmanaged resources when explicitly called
DataReader.Dispose();
}
// free shared unmanaged resources
}
_disposedValue = true;
}
/// <summary>
/// Disposes the object.
/// </summary>
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Object finalizer.
/// </summary>
~SafeDataReader()
{
Dispose(false);
}
#endregion
}
}