forked from danbarua/Npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlDataReader.cs
More file actions
1584 lines (1426 loc) · 57.9 KB
/
NpgsqlDataReader.cs
File metadata and controls
1584 lines (1426 loc) · 57.9 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
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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Npgsql.NpgsqlDataReader.cs
//
// Author:
// Francisco Jr. (fxjrlists@yahoo.com.br)
//
// Copyright (C) 2002 The Npgsql Development Team
// npgsql-general@gborg.postgresql.org
// http://gborg.postgresql.org/project/npgsql/projdisplay.php
//
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using NpgsqlTypes;
namespace Npgsql
{
/// <summary>
/// Provides a means of reading a forward-only stream of rows from a PostgreSQL backend.
/// </summary>
public partial class NpgsqlDataReader : DbDataReader, IStreamOwner
{
internal NpgsqlConnector _connector;
internal NpgsqlConnection _connection;
internal DataTable _currentResultsetSchema;
internal CommandBehavior _behavior;
internal NpgsqlCommand Command { get; private set; }
internal Version Npgsql205 = new Version("2.0.5");
internal bool _isClosed = false;
/// <summary>
/// Is raised whenever Close() is called.
/// </summary>
public event EventHandler ReaderClosed;
internal NpgsqlRowDescription CurrentDescription { get; private set; }
private NpgsqlRow _currentRow = null;
private int? _recordsAffected = null;
private int? _nextRecordsAffected;
internal long? LastInsertedOID { get; private set; }
private long? _nextInsertOID = null;
internal bool _cleanedUp = false;
private bool _hasRows = false;
private readonly NpgsqlConnector.NotificationThreadBlock _threadBlock;
//Unfortunately we sometimes don't know we're going to be dealing with
//a description until it comes when we look for a row or a message, and
//we may also need test if we may have rows for HasRows before the first call
//to Read(), so we need to be able to cache one of each.
private NpgsqlRowDescription _pendingDescription = null;
private NpgsqlRow _pendingRow = null;
private readonly bool _preparedStatement;
internal NpgsqlDataReader(NpgsqlCommand command, CommandBehavior behavior,
NpgsqlConnector.NotificationThreadBlock threadBlock,
bool preparedStatement = false, NpgsqlRowDescription rowDescription = null)
{
_behavior = behavior;
_connector = command.Connector;
Command = command;
_connection = command.Connection;
command.Connector.CurrentReader = this;
_threadBlock = threadBlock;
_preparedStatement = preparedStatement;
CurrentDescription = rowDescription;
}
internal void UpdateOutputParameters()
{
if (CurrentDescription != null)
{
IEnumerable<NpgsqlParameter> inputOutputAndOutputParams = Command.Parameters.Cast<NpgsqlParameter>()
.Where(p => p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Output);
if (!inputOutputAndOutputParams.Any())
{
return;
}
NpgsqlRow row = ParameterUpdateRow;
if (row == null)
{
return;
}
Queue<NpgsqlParameter> pending = new Queue<NpgsqlParameter>();
List<int> taken = new List<int>();
foreach (NpgsqlParameter p in inputOutputAndOutputParams)
{
int idx = CurrentDescription.TryFieldIndex(p.CleanName);
if (idx == -1)
{
pending.Enqueue(p);
}
else
{
p.Value = row.Get(idx);
taken.Add(idx);
}
}
for (int i = 0; pending.Count != 0 && i != row.NumFields; ++i)
{
if (!taken.Contains(i))
{
pending.Dequeue().Value = row.Get(i);
}
}
}
}
// We always receive a ForwardsOnlyRow, but if we are not
// doing SequentialAccess we want the flexibility of CachingRow,
// so here we either return the ForwardsOnlyRow we received, or
// build a CachingRow from it, as appropriate.
private NpgsqlRow BuildRow(ForwardsOnlyRow fo)
{
if (fo == null)
{
return null;
}
else
{
fo.SetRowDescription(CurrentDescription);
if ((_behavior & CommandBehavior.SequentialAccess) == CommandBehavior.SequentialAccess)
{
return fo;
}
else
{
return new CachingRow(fo);
}
}
}
private NpgsqlRow ParameterUpdateRow
{
get
{
NpgsqlRow ret = CurrentRow ?? _pendingRow ?? GetNextRow(false);
if (ret is ForwardsOnlyRow)
{
ret = _pendingRow = new CachingRow((ForwardsOnlyRow) ret);
}
return ret;
}
}
private NpgsqlRow CurrentRow
{
get { return _currentRow; }
set
{
if (_currentRow != null)
{
_currentRow.Dispose();
}
_currentRow = value;
}
}
internal void CheckHaveRow()
{
if (CurrentRow == null)
{
throw new InvalidOperationException("Invalid attempt to read when no data is present.");
}
}
/// <summary>
/// Gets the number of rows changed, inserted, or deleted by execution of the SQL statement.
/// </summary>
public override Int32 RecordsAffected
{
get { return _recordsAffected ?? -1; }
}
#region Result traversal
/// <summary>
/// Advances the data reader to the next row.
/// </summary>
/// <returns>True if the reader was advanced, otherwise false.</returns>
public override bool Read()
{
return ReadInternal();
}
/// <summary>
/// Asynchronous version of <see cref="Read"/>.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task representing the asynchronous operation, with true if the reader
/// was advanced, otherwise false.</returns>
#if NET45
public override Task<bool> ReadAsync(CancellationToken cancellationToken)
#else
public Task<bool> ReadAsync(CancellationToken cancellationToken)
#endif
{
cancellationToken.ThrowIfCancellationRequested();
// TODO: What is the desired behavior when cancelling here?
// cancellationToken.Register(...)
return ReadInternalAsync();
}
#if !NET45
public Task<bool> ReadAsync()
{
return ReadAsync(CancellationToken.None);
}
#endif
#if NET45
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
[GenerateAsync]
bool ReadInternal()
{
try
{
//CurrentRow = null;
return (CurrentRow = GetNextRow(true)) != null;
}
catch (IOException)
{
Command.Connection.ClearPool();
throw;
}
}
/// <summary>
/// Indicates if NpgsqlDatareader has rows to be read.
/// </summary>
public override Boolean HasRows
{
get
{
// Return true even after the last row has been read in this result.
// the first call to GetNextRow will set _hasRows to true if rows are found.
return _hasRows || (GetNextRow(false) != null);
}
}
[GenerateAsync]
private NpgsqlRow GetNextRow(bool clearPending)
{
if (_pendingDescription != null)
{
return null;
}
if (((_behavior & CommandBehavior.SingleRow) != 0 && CurrentRow != null && _pendingDescription == null) ||
((_behavior & CommandBehavior.SchemaOnly) != 0))
{
if (!clearPending)
{
return null;
}
//We should only have one row, and we've already had it. Move to end
//of recordset.
CurrentRow = null;
for (object skip = GetNextServerMessage();
skip != null && (_pendingDescription = skip as NpgsqlRowDescription) == null;
skip = GetNextServerMessage())
{
if (skip is NpgsqlRow)
{
(skip as NpgsqlRow).Dispose();
}
}
return null;
}
if (_pendingRow != null)
{
NpgsqlRow ret = _pendingRow;
if (clearPending)
{
_pendingRow = null;
}
if (!_hasRows)
{
// when rows are found, store that this result has rows.
_hasRows = (ret != null);
}
return ret;
}
CurrentRow = null;
object objNext = GetNextServerMessage();
if (clearPending)
{
_pendingRow = null;
}
if (objNext is NpgsqlRowDescription)
{
_pendingDescription = objNext as NpgsqlRowDescription;
return null;
}
if (!_hasRows)
{
// when rows are found, store that this result has rows.
_hasRows = objNext is NpgsqlRow;
}
return objNext as NpgsqlRow;
}
/// <summary>
/// Advances the data reader to the next result, when multiple result sets were returned by the PostgreSQL backend.
/// </summary>
/// <returns>True if the reader was advanced, otherwise false.</returns>
public override bool NextResult()
{
if (_preparedStatement) {
// Prepared statements can never have multiple results.
return false;
}
return NextResultInternal();
}
/// <summary>
/// Asynchronous version of <see cref="NextResult"/>.
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A task representing the asynchronous operation, with true if the reader
/// was advanced, otherwise false.</returns>
#if NET45
public override Task<bool> NextResultAsync(CancellationToken cancellationToken)
#else
public Task<bool> NextResultAsync(CancellationToken cancellationToken)
#endif
{
cancellationToken.ThrowIfCancellationRequested();
// TODO: What is the desired behavior when cancelling here?
// cancellationToken.Register(...)
if (_preparedStatement) {
// Prepared statements can never have multiple results.
return PGUtil.TaskFromResult(false);
}
return NextResultInternalAsync();
}
#if !NET45
public Task<bool> NextResultAsync()
{
return NextResultAsync(CancellationToken.None);
}
#endif
#if NET45
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
[GenerateAsync]
internal bool NextResultInternal()
{
try
{
CurrentRow = null;
_currentResultsetSchema = null;
_hasRows = false; // set to false and let the reading code determine if the set has rows.
return (CurrentDescription = GetNextRowDescription()) != null;
}
catch (IOException)
{
Command.Connection.ClearPool();
throw;
}
}
/// <summary>
/// Iterate through the objects returned through from the server.
/// If it's a CompletedResponse the rowsaffected count is updated appropriately,
/// and we iterate again, otherwise we return it (perhaps updating our cache of pending
/// rows if appropriate).
/// </summary>
/// <returns>The next <see cref="IServerMessage"/> we will deal with.</returns>
[GenerateAsync]
private IServerMessage GetNextServerMessage(bool cleanup = false)
{
if (_cleanedUp)
return null;
try
{
CurrentRow = null;
if (_pendingRow != null)
{
_pendingRow.Dispose();
}
_pendingRow = null;
while (true)
{
var msg = _connector.ReadMessage();
if (msg is RowReader)
{
RowReader reader = msg as RowReader;
if (cleanup)
{
// V3 rows can dispose by simply reading MessageLength bytes.
reader.Dispose();
return reader;
}
else
{
return _pendingRow = BuildRow(new ForwardsOnlyRow(reader));
}
}
else if (msg is CompletedResponse)
{
CompletedResponse cr = msg as CompletedResponse;
if (cr.RowsAffected.HasValue)
{
_nextRecordsAffected = (_nextRecordsAffected ?? 0) + cr.RowsAffected.Value;
}
_nextInsertOID = cr.LastInsertedOID ?? _nextInsertOID;
}
// TODO: The check for the three COPY protocol messages slow down our normal
// processing. Simply separate the flow (don't use ExecuteNonQuery)
else if (msg is ReadyForQueryMsg ||
msg is CopyInResponseMsg || msg is CopyOutResponseMsg || msg is CopyDataMsg)
{
CleanUp(true);
return null;
}
else
{
return msg;
}
}
}
catch
{
CleanUp(true);
throw;
}
}
/// <summary>
/// Advances the data reader to the next result, when multiple result sets were returned by the PostgreSQL backend.
/// </summary>
/// <returns>True if the reader was advanced, otherwise false.</returns>
[GenerateAsync]
private NpgsqlRowDescription GetNextRowDescription()
{
if ((_behavior & CommandBehavior.SingleResult) != 0 && CurrentDescription != null)
{
CleanUp(false);
return null;
}
NpgsqlRowDescription rd = _pendingDescription;
while (rd == null)
{
object objNext = GetNextServerMessage();
if (objNext == null)
{
break;
}
if (objNext is NpgsqlRow)
{
(objNext as NpgsqlRow).Dispose();
}
rd = objNext as NpgsqlRowDescription;
}
_pendingDescription = null;
// If there were records affected before, keep track of their values.
if (_recordsAffected != null)
_recordsAffected += (_nextRecordsAffected ?? 0);
else
_recordsAffected = _nextRecordsAffected;
_nextRecordsAffected = null;
LastInsertedOID = _nextInsertOID;
_nextInsertOID = null;
return rd;
}
/// <summary>
/// Get enumerator.
/// </summary>
/// <returns></returns>
public override IEnumerator GetEnumerator()
{
return new DbEnumerator(this);
}
#endregion
#region Cleanup / close
private void CleanUp(bool finishedMessages)
{
lock (this)
{
if (_cleanedUp) {
return;
}
if (!finishedMessages)
{
do
{
if ((Thread.CurrentThread.ThreadState & (ThreadState.Aborted | ThreadState.AbortRequested)) != 0)
{
_connection.EmergencyClose();
return;
}
}
while (GetNextServerMessage(true) != null);
}
_cleanedUp = true;
}
_threadBlock.Dispose();
}
/// <summary>
/// Closes the data reader object.
/// </summary>
public override void Close()
{
CleanUp(false);
if ((_behavior & CommandBehavior.CloseConnection) == CommandBehavior.CloseConnection)
{
_connection.Close();
}
_isClosed = true;
if (ReaderClosed != null) {
ReaderClosed(this, EventArgs.Empty);
}
}
/// <summary>
/// Gets a value indicating whether the data reader is closed.
/// </summary>
public override Boolean IsClosed
{
get { return _isClosed; }
}
#endregion Cleanup / close
#region Get column data
/// <summary>
/// Gets the value of the specified column as an instance of <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// The value of the specified column.
/// </returns>
/// <param name="ordinal">The zero-based column ordinal.</param><filterpriority>1</filterpriority>
public override object GetValue(int ordinal)
{
return GetValueInternal(ordinal);
}
/// <summary>
/// Asynchronously gets the value of the specified column as a type.
/// </summary>
/// <typeparam name="T">The type of the value to be returned.</typeparam>
/// <param name="ordinal">The type of the value to be returned.</param>
/// <param name="cancellationToken">Currently not implemented.</param>
/// <returns></returns>
#if NET45
public override async Task<T> GetFieldValueAsync<T>(int ordinal, CancellationToken cancellationToken)
#else
public async Task<T> GetFieldValueAsync<T>(int ordinal, CancellationToken cancellationToken)
#endif
{
return (T)await GetValueInternalAsync(ordinal);
}
#if !NET45
/// <summary>
/// Asynchronously gets the value of the specified column as a type.
/// </summary>
/// <typeparam name="T">The type of the value to be returned.</typeparam>
/// <param name="ordinal">The type of the value to be returned.</param>
/// <returns></returns>
public Task<T> GetFieldValueAsync<T>(int ordinal)
{
return GetFieldValueAsync<T>(ordinal, CancellationToken.None);
}
#endif
#if NET45
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
[GenerateAsync]
object GetValueInternal(int ordinal)
{
object providerValue = GetProviderSpecificValue(ordinal);
NpgsqlBackendTypeInfo backendTypeInfo;
if (Command.ExpectedTypes != null && Command.ExpectedTypes.Length > ordinal && Command.ExpectedTypes[ordinal] != null)
{
return ExpectedTypeConverter.ChangeType(providerValue, Command.ExpectedTypes[ordinal]);
}
else if ((_connection == null || !_connection.UseExtendedTypes) && TryGetTypeInfo(ordinal, out backendTypeInfo))
return backendTypeInfo.ConvertToFrameworkType(providerValue);
return providerValue;
}
[GenerateAsync]
public override object GetProviderSpecificValue(int ordinal)
{
if (ordinal < 0 || ordinal >= CurrentDescription.NumFields)
{
throw new IndexOutOfRangeException("Column index out of range");
}
CheckHaveRow();
return CurrentRow.Get(ordinal);
}
/// <summary>
/// Gets the value of a column in its native format.
/// </summary>
public override Object this[String name]
{
get
{
return GetValueInternal(GetOrdinal(name));
}
}
/// <summary>
/// Gets the value of a column as Boolean.
/// </summary>
public override Boolean GetBoolean(Int32 i)
{
// Should this be done using the GetValue directly and not by converting to String
// and parsing from there?
return (Boolean)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column as Byte.
/// </summary>
public override Byte GetByte(Int32 i)
{
return (Byte)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column as Char.
/// </summary>
public override Char GetChar(Int32 i)
{
//This is an interesting one. In the world of databases we've the idea of chars which is 0 to n characters
//where n is stated (and can perhaps be infinite) and various variations upon that (postgres is admirable
//in being relatively consistent and in not generally encouraging limiting n purely for performance reasons,
//but across many different platforms we'll find such things as text, ntext, char, nchar, varchar, nvarchar,
//and so on with some platforms not having them all and many implementaiton differences).
//
//In the world of .NET, and many other languages, we have the idea of characters and of strings - which are
//sequences of characters with differing degress of encapsulation from C just having char* through to .NET
//having full-blown objects
//
//Database char, varchar, text, etc. are all generally mapped to strings. There's a bit of a question as to
//what maps to a .NET char. Interestingly enough, SQLDataReader doesn't support GetChar() and neither do
//a few other providers (Oracle for example). It would seem that IDataReader.GetChar() was defined largely
//to have a complete set of .NET base types. Still, the closest thing in the database world to a char value
//is a char(1) or varchar(1) - that is to say the value of a string of length one, so that's what is used here.
//
//In case of the type is "char", we can return the .NET Char directly.
Object value = GetValueInternal(i);
if (value is Char)
{
return (Char)value;
}
String s = (String)value;
if (s.Length != 1)
{
throw new InvalidCastException();
}
return s[0];
}
/// <summary>
/// Gets the value of a column as DateTime.
/// </summary>
public override DateTime GetDateTime(Int32 i)
{
return (DateTime)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column converted to a Guid.
/// </summary>
public override Guid GetGuid(Int32 i)
{
return (Guid)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column as Int16.
/// </summary>
public override Int16 GetInt16(Int32 i)
{
return (Int16)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column as Int32.
/// </summary>
public override Int32 GetInt32(Int32 i)
{
return (Int32)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column as Int64.
/// </summary>
public override Int64 GetInt64(Int32 i)
{
return (Int64)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column as Single.
/// </summary>
public override Single GetFloat(Int32 i)
{
return (Single)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column as Double.
/// </summary>
public override Double GetDouble(Int32 i)
{
return (Double)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column as String.
/// </summary>
public override String GetString(Int32 i)
{
return (String)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column as Decimal.
/// </summary>
public override Decimal GetDecimal(Int32 i)
{
return (Decimal)GetValueInternal(i);
}
/// <summary>
/// Gets the value of a column as TimeSpan.
/// </summary>
public TimeSpan GetTimeSpan(Int32 i)
{
return (TimeSpan)GetValueInternal(i);
}
/// <summary>
/// Get specified field value.
/// /// </summary>
/// <param name="i"></param>
/// <returns></returns>
public BitString GetBitString(int i)
{
object ret = GetValueInternal(i);
if (ret is bool)
return new BitString((bool)ret);
else
return (BitString)ret;
}
/// <summary>
/// Get the value of a column as a <see cref="NpgsqlInterval"/>.
/// <remarks>If the differences between <see cref="NpgsqlInterval"/> and <see cref="System.TimeSpan"/>
/// in handling of days and months is not important to your application, use <see cref="GetTimeSpan(Int32)"/>
/// instead.</remarks>
/// </summary>
/// <param name="i">Index of the field to find.</param>
/// <returns><see cref="NpgsqlInterval"/> value of the field.</returns>
public NpgsqlInterval GetInterval(Int32 i)
{
return (NpgsqlInterval)GetProviderSpecificValue(i);
}
/// <summary>
/// Get specified field value.
/// /// </summary>
/// <param name="i"></param>
/// <returns></returns>
public NpgsqlTime GetTime(int i)
{
return (NpgsqlTime)GetProviderSpecificValue(i);
}
/// <summary>
/// Get specified field value.
/// /// </summary>
/// <param name="i"></param>
/// <returns></returns>
public NpgsqlTimeTZ GetTimeTZ(int i)
{
return (NpgsqlTimeTZ)GetProviderSpecificValue(i);
}
/// <summary>
/// Get specified field value.
/// /// </summary>
/// <param name="i"></param>
/// <returns></returns>
public NpgsqlTimeStamp GetTimeStamp(int i)
{
return (NpgsqlTimeStamp)GetProviderSpecificValue(i);
}
/// <summary>
/// Get specified field value.
/// /// </summary>
/// <param name="i"></param>
/// <returns></returns>
public NpgsqlTimeStampTZ GetTimeStampTZ(int i)
{
return (NpgsqlTimeStampTZ)GetProviderSpecificValue(i);
}
/// <summary>
/// Get specified field value.
/// /// </summary>
/// <param name="i"></param>
/// <returns></returns>
public NpgsqlDate GetDate(int i)
{
return (NpgsqlDate)GetProviderSpecificValue(i);
}
/// <summary>
/// Gets raw data from a column.
/// </summary>
public override Int64 GetBytes(Int32 i, Int64 fieldOffset, Byte[] buffer, Int32 bufferoffset, Int32 length)
{
return CurrentRow.GetBytes(i, fieldOffset, buffer, bufferoffset, length);
}
/// <summary>
/// Gets raw data from a column.
/// </summary>
public override Int64 GetChars(Int32 i, Int64 fieldoffset, Char[] buffer, Int32 bufferoffset, Int32 length)
{
return CurrentRow.GetChars(i, fieldoffset, buffer, bufferoffset, length);
}
/// <summary>
/// Gets the value of a column in its native format.
/// </summary>
public override Object this[Int32 i]
{
get { return GetValueInternal(i); }
}
/// <summary>
/// Gets a value that indicates whether the column contains nonexistent or missing values.
/// </summary>
/// <param name="ordinal">The zero-based column to be retrieved.</param>
/// <returns>true if the specified column value is equivalent to DBNull otherwise false.</returns>
public override bool IsDBNull(int ordinal)
{
CheckHaveRow();
return CurrentRow.IsDBNull(ordinal);
}
/// <summary>
/// An asynchronous version of IsDBNull, which gets a value that indicates whether the
/// column contains non-existent or missing values.
/// </summary>
/// <param name="ordinal">The zero-based column to be retrieved.</param>
/// <param name="cancellationToken">Currently not implemented.</param>
/// <returns>true if the specified column value is equivalent to DBNull otherwise false.</returns>
#if NET45
public override Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
#else
public Task<bool> IsDBNullAsync(int ordinal, CancellationToken cancellationToken)
#endif
{
CheckHaveRow();
return CurrentRow.IsDBNullAsync(ordinal);
}
#if !NET45
/// <summary>
/// An asynchronous version of IsDBNull, which gets a value that indicates whether the
/// column contains non-existent or missing values.
/// </summary>
/// <param name="ordinal">The zero-based column to be retrieved.</param>
/// <returns>true if the specified column value is equivalent to DBNull otherwise false.</returns>
public Task<bool> IsDBNullAsync(int ordinal)
{
return IsDBNullAsync(ordinal, CancellationToken.None);
}
#endif
/// <summary>
/// Copy values from each column in the current row into <paramref name="values"/>.
/// </summary>
/// <param name="values">Destination for column values.</param>
/// <returns>The number of column values copied.</returns>
public override Int32 GetValues(Object[] values)
{
return LoadValues(values, GetValue);
}
/// <summary>
/// Copy values from each column in the current row into <paramref name="values"></paramref>.
/// </summary>
/// <param name="values">An array appropriately sized to store values from all columns.</param>
/// <returns>The number of column values copied.</returns>
public override int GetProviderSpecificValues(object[] values)
{
return LoadValues(values, GetProviderSpecificValue);
}
private delegate object ValueLoader(int ordinal);
private int LoadValues(object[] values, ValueLoader getValue)
{
CheckHaveRow();
// Only the number of elements in the array are filled.
// It's also possible to pass an array with more that FieldCount elements.
Int32 maxColumnIndex = (values.Length < FieldCount) ? values.Length : FieldCount;
for (Int32 i = 0; i < maxColumnIndex; i++)
{
values[i] = getValue(i);
}
return maxColumnIndex;
}
#endregion Column data retrieval
#region Result set metadata
/// <summary>
/// Return the column name of the column at index <param name="Index"></param>.
/// </summary>
public override String GetName(Int32 Index)
{
if (CurrentDescription == null)
{
throw new IndexOutOfRangeException(); //Essentially, all indices are out of range.
}
return CurrentDescription[Index].Name;
}
/// <summary>
/// Return the data type OID of the column at index <param name="Index"></param>.
/// </summary>
/// FIXME: Why this method returns String?
public String GetDataTypeOID(Int32 Index)
{
if (CurrentDescription == null)
{
throw new IndexOutOfRangeException(); //Essentially, all indices are out of range.
}
return CurrentDescription[Index].TypeOID.ToString();
}
internal bool TryGetTypeInfo(int fieldIndex, out NpgsqlBackendTypeInfo backendTypeInfo)
{
if (CurrentDescription == null)