GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Npgsql/src/Npgsql/NpgsqlDataReader.cs at main · roji/Npgsql Expand file tree/ Copy pathNpgsqlDataReader.csMore file actions 2244 lines (1927 loc) · 92.6 KB / Copy pathNpgsqlDataReader.csFile metadata and controls2244 lines (1927 loc) · 92.6 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 using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.Data.Common; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; using Npgsql.BackendMessages; using Npgsql.Internal; using Npgsql.Internal.TypeHandlers; using Npgsql.Internal.TypeHandling; using Npgsql.Logging; using Npgsql.PostgresTypes; using Npgsql.Schema; using Npgsql.Util; using NpgsqlTypes; using static Npgsql.Util.Statics; #pragma warning disable CA2222 // Do not decrease inherited member visibility namespace Npgsql { /// <summary> /// Reads a forward-only stream of rows from a data source. /// </summary> #pragma warning disable CA1010 public sealed class NpgsqlDataReader : DbDataReader, IDbColumnSchemaGenerator #pragma warning restore CA1010 { internal NpgsqlCommand Command { get; private set; } = default!; internal NpgsqlConnector Connector { get; } NpgsqlConnection _connection = default!; /// <summary> /// The behavior of the command with which this reader was executed. /// </summary> CommandBehavior _behavior; /// <summary> /// In multiplexing, this is <see langword="null" /> as the sending is managed in the write multiplexing loop, /// and does not need to be awaited by the reader. /// </summary> Task? _sendTask; internal ReaderState State = ReaderState.Disposed; internal NpgsqlReadBuffer Buffer = default!; /// <summary> /// Holds the list of statements being executed by this reader. /// </summary> List<NpgsqlStatement> _statements = default!; /// <summary> /// The index of the current query resultset we're processing (within a multiquery) /// </summary> internal int StatementIndex { get; private set; } /// <summary> /// The number of columns in the current row /// </summary> int _numColumns; /// <summary> /// Records, for each column, its starting offset and length in the current row. /// Used only in non-sequential mode. /// </summary> readonly List<(int Offset, int Length)> _columns = new(); /// <summary> /// The index of the column that we're on, i.e. that has already been parsed, is /// is memory and can be retrieved. Initialized to -1, which means we're on the column /// count (which comes before the first column). /// </summary> int _column; /// <summary> /// For streaming types (e.g. bytea), holds the byte length of the column. /// Does not include the length prefix. /// </summary> internal int ColumnLen; internal int PosInColumn; /// <summary> /// The position in the buffer at which the current data row message ends. /// Used only when the row is consumed non-sequentially. /// </summary> int _dataMsgEnd; /// <summary> /// Determines, if we can consume the row non-sequentially. /// Mostly useful for a sequential mode, when the row is already in the buffer. /// Should always be true for the non-sequential mode. /// </summary> bool CanConsumeRowNonSequentially { get { var result = _dataMsgEnd - Buffer.ReadPosition <= Buffer.ReadBytesLeft; Debug.Assert(result || _isSequential); return result; } } int _charPos; /// <summary> /// The RowDescription message for the current resultset being processed /// </summary> internal RowDescriptionMessage? RowDescription; ulong? _recordsAffected; /// <summary> /// Whether the current result set has rows /// </summary> bool _hasRows; /// <summary> /// Is raised whenever Close() is called. /// </summary> public event EventHandler? ReaderClosed; bool _isSchemaOnly; bool _isSequential; /// <summary> /// A stream that has been opened on a column. /// </summary> NpgsqlReadBuffer.ColumnStream? _columnStream; /// <summary> /// Used for internal temporary purposes /// </summary> char[]? _tempCharBuf; static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(NpgsqlDataReader)); internal NpgsqlDataReader(NpgsqlConnector connector) { Connector = connector; } internal void Init( NpgsqlCommand command, CommandBehavior behavior, List<NpgsqlStatement> statements, Task? sendTask = null) { Command = command; _connection = command.Connection!; _behavior = behavior; _isSchemaOnly = _behavior.HasFlag(CommandBehavior.SchemaOnly); _isSequential = _behavior.HasFlag(CommandBehavior.SequentialAccess); _statements = statements; StatementIndex = -1; _sendTask = sendTask; State = ReaderState.BetweenResults; _recordsAffected = null; } #region Read /// <summary> /// Advances the reader to the next record in a result set. /// </summary> /// <returns><b>true</b> if there are more rows; otherwise <b>false</b>.</returns> /// <remarks> /// The default position of a data reader is before the first record. Therefore, you must call Read to begin accessing data. /// </remarks> public override bool Read() { CheckClosedOrDisposed(); var fastRead = TryFastRead(); return fastRead.HasValue ? fastRead.Value : Read(false).GetAwaiter().GetResult(); } /// <summary> /// This is the asynchronous version of <see cref="Read()"/> /// </summary> /// <param name="cancellationToken"> /// An optional token to cancel the asynchronous operation. The default value is <see cref="CancellationToken.None"/>. /// </param> /// <returns>A task representing the asynchronous operation.</returns> public override Task<bool> ReadAsync(CancellationToken cancellationToken) { CheckClosedOrDisposed(); var fastRead = TryFastRead(); if (fastRead.HasValue) return fastRead.Value ? PGUtil.TrueTask : PGUtil.FalseTask; using (NoSynchronizationContextScope.Enter()) return Read(true, cancellationToken); } bool? TryFastRead() { // This is an optimized execution path that avoids calling any async methods for the (usual) // case where the next row (or CommandComplete) is already in memory. if (_behavior.HasFlag(CommandBehavior.SingleRow)) return null; switch (State) { case ReaderState.BeforeResult: // First Read() after NextResult. Data row has already been processed. State = ReaderState.InResult; return true; case ReaderState.InResult: if (!CanConsumeRowNonSequentially) return null; // We get here, if we're in a non-sequential mode (or the row is already in the buffer) ConsumeRowNonSequential(); break; case ReaderState.BetweenResults: case ReaderState.Consumed: case ReaderState.Closed: case ReaderState.Disposed: return false; } var readBuf = Connector.ReadBuffer; if (readBuf.ReadBytesLeft < 5) return null; var messageCode = (BackendMessageCode)readBuf.ReadByte(); var len = readBuf.ReadInt32() - 4; // Transmitted length includes itself if (messageCode != BackendMessageCode.DataRow || readBuf.ReadBytesLeft < len) { readBuf.ReadPosition -= 5; return null; } var msg = Connector.ParseServerMessage(readBuf, messageCode, len, false)!; Debug.Assert(msg.Code == BackendMessageCode.DataRow); ProcessMessage(msg); return true; } async Task<bool> Read(bool async, CancellationToken cancellationToken = default) { var registration = Connector.StartNestedCancellableOperation(cancellationToken); try { switch (State) { case ReaderState.BeforeResult: // First Read() after NextResult. Data row has already been processed. State = ReaderState.InResult; return true; case ReaderState.InResult: await ConsumeRow(async); if (_behavior.HasFlag(CommandBehavior.SingleRow)) { // TODO: See optimization proposal in #410 await Consume(async); return false; } break; case ReaderState.BetweenResults: case ReaderState.Consumed: case ReaderState.Closed: case ReaderState.Disposed: return false; default: throw new ArgumentOutOfRangeException(); } var msg2 = await ReadMessage(async); ProcessMessage(msg2); return msg2.Code == BackendMessageCode.DataRow; } catch { State = ReaderState.Consumed; throw; } finally { registration.Dispose(); } } ValueTask<IBackendMessage> ReadMessage(bool async) { return _isSequential ? ReadMessageSequential(Connector, async) : Connector.ReadMessage(async); static async ValueTask<IBackendMessage> ReadMessageSequential(NpgsqlConnector connector, bool async2) { var msg = await connector.ReadMessage(async2, DataRowLoadingMode.Sequential); if (msg.Code == BackendMessageCode.DataRow) { // Make sure that the datarow's column count is already buffered await connector.ReadBuffer.Ensure(2, async2); return msg; } return msg; } } #endregion #region NextResult /// <summary> /// Advances the reader to the next result when reading the results of a batch of statements. /// </summary> /// <returns></returns> public override bool NextResult() => (_isSchemaOnly ? NextResultSchemaOnly(false) : NextResult(false)) .GetAwaiter().GetResult(); /// <summary> /// This is the asynchronous version of NextResult. /// </summary> /// <param name="cancellationToken"> /// An optional token to cancel the asynchronous operation. The default value is <see cref="CancellationToken.None"/>. /// </param> /// <returns>A task representing the asynchronous operation.</returns> public override Task<bool> NextResultAsync(CancellationToken cancellationToken) { using (NoSynchronizationContextScope.Enter()) return _isSchemaOnly ? NextResultSchemaOnly(async: true, cancellationToken: cancellationToken) : NextResult(async: true, cancellationToken: cancellationToken); } /// <summary> /// Internal implementation of NextResult /// </summary> [MethodImpl(MethodImplOptions.AggressiveInlining)] async Task<bool> NextResult(bool async, bool isConsuming = false, CancellationToken cancellationToken = default) { CheckClosedOrDisposed(); IBackendMessage msg; Debug.Assert(!_isSchemaOnly); using var registration = isConsuming ? default : Connector.StartNestedCancellableOperation(cancellationToken); try { // If we're in the middle of a resultset, consume it switch (State) { case ReaderState.BeforeResult: case ReaderState.InResult: await ConsumeRow(async); while (true) { var completedMsg = await Connector.ReadMessage(async, DataRowLoadingMode.Skip); switch (completedMsg.Code) { case BackendMessageCode.CommandComplete: case BackendMessageCode.EmptyQueryResponse: ProcessMessage(completedMsg); break; default: continue; } break; } break; case ReaderState.BetweenResults: break; case ReaderState.Consumed: case ReaderState.Closed: case ReaderState.Disposed: return false; default: throw new ArgumentOutOfRangeException(); } Debug.Assert(State == ReaderState.BetweenResults); _hasRows = false; if (_behavior.HasFlag(CommandBehavior.SingleResult) && StatementIndex == 0 && !isConsuming) { await Consume(async); return false; } // We are now at the end of the previous result set. Read up to the next result set, if any. // Non-prepared statements receive ParseComplete, BindComplete, DescriptionRow/NoData, // prepared statements receive only BindComplete for (StatementIndex++; StatementIndex < _statements.Count; StatementIndex++) { var statement = _statements[StatementIndex]; if (statement.IsPrepared) { Expect<BindCompleteMessage>(await Connector.ReadMessage(async), Connector); RowDescription = statement.Description; } else // Non-prepared/preparing flow { var pStatement = statement.PreparedStatement; if (pStatement != null) { Debug.Assert(!pStatement.IsPrepared); if (pStatement.StatementBeingReplaced != null) { Expect<CloseCompletedMessage>(await Connector.ReadMessage(async), Connector); pStatement.StatementBeingReplaced.CompleteUnprepare(); pStatement.StatementBeingReplaced = null; } } Expect<ParseCompleteMessage>(await Connector.ReadMessage(async), Connector); Expect<BindCompleteMessage>(await Connector.ReadMessage(async), Connector); msg = await Connector.ReadMessage(async); RowDescription = statement.Description = msg.Code switch { BackendMessageCode.NoData => null, // RowDescription messages are cached on the connector, but if we're auto-preparing, we need to // clone our own copy which will last beyond the lifetime of this invocation. BackendMessageCode.RowDescription => pStatement == null ? (RowDescriptionMessage)msg : ((RowDescriptionMessage)msg).Clone(), _ => throw Connector.UnexpectedMessageReceived(msg.Code) }; if (statement.IsPreparing) { statement.IsPreparing = false; pStatement!.CompletePrepare(); } } if (RowDescription == null) { // Statement did not generate a resultset (e.g. INSERT) // Read and process its completion message and move on to the next statement msg = await ReadMessage(async); switch (msg.Code) { case BackendMessageCode.CommandComplete: case BackendMessageCode.EmptyQueryResponse: break; default: throw Connector.UnexpectedMessageReceived(msg.Code); } ProcessMessage(msg); continue; } if (StatementIndex == 0 && Command.Parameters.HasOutputParameters) { // If output parameters are present and this is the first row of the first resultset, // we must always read it in non-sequential mode because it will be traversed twice (once // here for the parameters, then as a regular row). msg = await Connector.ReadMessage(async); ProcessMessage(msg); if (msg.Code == BackendMessageCode.DataRow) PopulateOutputParameters(); } else { msg = await ReadMessage(async); ProcessMessage(msg); } switch (msg.Code) { case BackendMessageCode.DataRow: case BackendMessageCode.CommandComplete: break; default: throw Connector.UnexpectedMessageReceived(msg.Code); } return true; } // There are no more queries, we're done. Read the RFQ. ProcessMessage(Expect<ReadyForQueryMessage>(await Connector.ReadMessage(async), Connector)); RowDescription = null; return false; } catch (Exception e) { State = ReaderState.Consumed; // Reference the triggering statement from the exception (for batching) if (e is PostgresException postgresException && StatementIndex >= 0 && StatementIndex < _statements.Count) { postgresException.Statement = _statements[StatementIndex]; } // An error means all subsequent statements were skipped by PostgreSQL. // If any of them were being prepared, we need to update our bookkeeping to put // them back in unprepared state. for (; StatementIndex < _statements.Count; StatementIndex++) { var statement = _statements[StatementIndex]; if (statement.IsPreparing) { statement.IsPreparing = false; statement.PreparedStatement!.CompleteUnprepare(); } } throw; } } void PopulateOutputParameters() { // The first row in a stored procedure command that has output parameters needs to be traversed twice - // once for populating the output parameters and once for the actual result set traversal. So in this // case we can't be sequential. Debug.Assert(Command.Parameters.Any(p => p.IsOutputDirection)); Debug.Assert(StatementIndex == 0); Debug.Assert(RowDescription != null); Debug.Assert(State == ReaderState.BeforeResult); var currentPosition = Buffer.ReadPosition; // Temporarily set our state to InResult to allow us to read the values State = ReaderState.InResult; var pending = new Queue<object>(); var taken = new List<NpgsqlParameter>(); for (var i = 0; i < FieldCount; i++) { if (Command.Parameters.TryGetValue(GetName(i), out var p) && p.IsOutputDirection) { p.Value = GetValue(i); taken.Add(p); } else pending.Enqueue(GetValue(i)); } // Not sure where this odd behavior comes from: all output parameters which did not get matched by // name now get populated with column values which weren't matched. Keeping this for backwards compat, // opened #2252 for investigation. foreach (var p in Command.Parameters.Where(p => p.IsOutputDirection && !taken.Contains(p))) { if (pending.Count == 0) break; p.Value = pending.Dequeue(); } State = ReaderState.BeforeResult; // Set the state back Buffer.ReadPosition = currentPosition; // Restore position _column = -1; ColumnLen = -1; PosInColumn = 0; } /// <summary> /// Note that in SchemaOnly mode there are no resultsets, and we read nothing from the backend (all /// RowDescriptions have already been processed and are available) /// </summary> async Task<bool> NextResultSchemaOnly(bool async, bool isConsuming = false, CancellationToken cancellationToken = default) { Debug.Assert(_isSchemaOnly); using var registration = isConsuming ? default : Connector.StartNestedCancellableOperation(cancellationToken); try { switch (State) { case ReaderState.BeforeResult: case ReaderState.InResult: case ReaderState.BetweenResults: break; case ReaderState.Consumed: case ReaderState.Closed: case ReaderState.Disposed: return false; default: throw new ArgumentOutOfRangeException(); } for (StatementIndex++; StatementIndex < _statements.Count; StatementIndex++) { var statement = _statements[StatementIndex]; if (statement.IsPrepared) { // Row descriptions have already been populated in the statement objects at the // Prepare phase RowDescription = _statements[StatementIndex].Description; } else { Expect<ParseCompleteMessage>(await Connector.ReadMessage(async), Connector); Expect<ParameterDescriptionMessage>(await Connector.ReadMessage(async), Connector); var msg = await Connector.ReadMessage(async); switch (msg.Code) { case BackendMessageCode.NoData: RowDescription = _statements[StatementIndex].Description = null; break; case BackendMessageCode.RowDescription: // We have a resultset RowDescription = _statements[StatementIndex].Description = (RowDescriptionMessage)msg; Command.FixupRowDescription(RowDescription, StatementIndex == 0); break; default: throw Connector.UnexpectedMessageReceived(msg.Code); } } // Found a resultset if (RowDescription != null) return true; } // There are no more queries, we're done. Read to the RFQ. if (!_statements.All(s => s.IsPrepared)) { ProcessMessage(Expect<ReadyForQueryMessage>(await Connector.ReadMessage(async), Connector)); RowDescription = null; } return false; } catch (Exception e) { State = ReaderState.Consumed; // Reference the triggering statement from the exception (for batching) if (e is PostgresException postgresException && StatementIndex >= 0 && StatementIndex < _statements.Count) { postgresException.Statement = _statements[StatementIndex]; } throw; } } #endregion #region ProcessMessage internal void ProcessMessage(IBackendMessage msg) { switch (msg.Code) { case BackendMessageCode.DataRow: ProcessDataRowMessage((DataRowMessage)msg); return; case BackendMessageCode.CommandComplete: var completed = (CommandCompleteMessage)msg; switch (completed.StatementType) { case StatementType.Update: case StatementType.Insert: case StatementType.Delete: case StatementType.Copy: case StatementType.Move: if (!_recordsAffected.HasValue) _recordsAffected = 0; _recordsAffected += completed.Rows; break; } _statements[StatementIndex].ApplyCommandComplete(completed); goto case BackendMessageCode.EmptyQueryResponse; case BackendMessageCode.EmptyQueryResponse: State = ReaderState.BetweenResults; return; case BackendMessageCode.ReadyForQuery: State = ReaderState.Consumed; return; default: throw new Exception("Received unexpected backend message of type " + msg.Code); } } void ProcessDataRowMessage(DataRowMessage msg) { Connector.State = ConnectorState.Fetching; // The connector's buffer can actually change between DataRows: // If a large DataRow exceeding the connector's current read buffer arrives, and we're // reading in non-sequential mode, a new oversize buffer is allocated. We thus have to // recapture the connector's buffer on each new DataRow. // Note that this can happen even in sequential mode, if the row description message is big // (see #2003) Buffer = Connector.ReadBuffer; _hasRows = true; _column = -1; ColumnLen = -1; PosInColumn = 0; // We assume that the row's number of columns is identical to the description's _numColumns = Buffer.ReadInt16(); Debug.Assert(_numColumns == RowDescription!.NumFields, $"Row's number of columns ({_numColumns}) differs from the row description's ({RowDescription.NumFields})"); _dataMsgEnd = Buffer.ReadPosition + msg.Length - 2; if (!_isSequential) { // Initialize our columns array with the offset and length of the first column _columns.Clear(); var len = Buffer.ReadInt32(); _columns.Add((Buffer.ReadPosition, len)); } switch (State) { case ReaderState.BetweenResults: State = ReaderState.BeforeResult; break; case ReaderState.BeforeResult: State = ReaderState.InResult; break; case ReaderState.InResult: break; default: throw Connector.UnexpectedMessageReceived(BackendMessageCode.DataRow); } } #endregion void Cancel() => Connector.PerformPostgresCancellation(); /// <summary> /// Gets a value indicating the depth of nesting for the current row. Always returns zero. /// </summary> public override int Depth => 0; /// <summary> /// Gets a value indicating whether the data reader is closed. /// </summary> public override bool IsClosed => State == ReaderState.Closed; /// <summary> /// Gets the number of rows changed, inserted, or deleted by execution of the SQL statement. /// </summary> public override int RecordsAffected => _recordsAffected.HasValue ? (int)_recordsAffected.Value : -1; /// <summary> /// Returns details about each statement that this reader will or has executed. /// </summary> /// <remarks> /// Note that some fields (i.e. rows and oid) are only populated as the reader /// traverses the result. /// /// For commands with multiple queries, this exposes the number of rows affected on /// a statement-by-statement basis, unlike <see cref="NpgsqlDataReader.RecordsAffected"/> /// which exposes an aggregation across all statements. /// </remarks> public IReadOnlyList<NpgsqlStatement> Statements => _statements.AsReadOnly(); /// <summary> /// Gets a value that indicates whether this DbDataReader contains one or more rows. /// </summary> public override bool HasRows => State == ReaderState.Closed ? throw new InvalidOperationException("Invalid attempt to call HasRows when reader is closed.") : _hasRows; /// <summary> /// Indicates whether the reader is currently positioned on a row, i.e. whether reading a /// column is possible. /// This property is different from <see cref="HasRows"/> in that <see cref="HasRows"/> will /// return true even if attempting to read a column will fail, e.g. before <see cref="Read()"/> /// has been called /// </summary> public bool IsOnRow => State == ReaderState.InResult; /// <summary> /// Gets the name of the column, given the zero-based column ordinal. /// </summary> /// <param name="ordinal">The zero-based column ordinal.</param> /// <returns>The name of the specified column.</returns> public override string GetName(int ordinal) => CheckRowDescriptionAndGetField(ordinal).Name; /// <summary> /// Gets the number of columns in the current row. /// </summary> public override int FieldCount { get { CheckClosedOrDisposed(); return RowDescription?.NumFields ?? 0; } } #region Cleanup / Dispose /// <summary> /// Consumes all result sets for this reader, leaving the connector ready for sending and processing further /// queries /// </summary> async Task Consume(bool async) { // Skip over the other result sets. Note that this does tally records affected // from CommandComplete messages, and properly sets state for auto-prepared statements if (_isSchemaOnly) while (await NextResultSchemaOnly(async, isConsuming: true)) {} else while (await NextResult(async, isConsuming: true)) {} } /// <summary> /// Releases the resources used by the <see cref="NpgsqlDataReader"/>. /// </summary> protected override void Dispose(bool disposing) { try { Close(connectionClosing: false, async: false, isDisposing: true).GetAwaiter().GetResult(); } catch (Exception e) { Log.Error("Exception caught while disposing a reader", e, Connector.Id); if (e is not PostgresException) State = ReaderState.Disposed; } } /// <summary> /// Releases the resources used by the <see cref="NpgsqlDataReader"/>. /// </summary> #if NETSTANDARD2_0 public ValueTask DisposeAsync() #else public override ValueTask DisposeAsync() #endif { using (NoSynchronizationContextScope.Enter()) return DisposeAsyncCore(); [MethodImpl(MethodImplOptions.AggressiveInlining)] async ValueTask DisposeAsyncCore() { try { await Close(connectionClosing: false, async: true, isDisposing: true); } catch (Exception e) { Log.Error("Exception caught while disposing a reader", e, Connector.Id); if (e is not PostgresException) State = ReaderState.Disposed; } } } /// <summary> /// Closes the <see cref="NpgsqlDataReader"/> reader, allowing a new command to be executed. /// </summary> public override void Close() => Close(connectionClosing: false, async: false, isDisposing: false).GetAwaiter().GetResult(); /// <summary> /// Closes the <see cref="NpgsqlDataReader"/> reader, allowing a new command to be executed. /// </summary> #if NETSTANDARD2_0 public Task CloseAsync() #else public override Task CloseAsync() #endif => Close(connectionClosing: false, async: true, isDisposing: false); internal async Task Close(bool connectionClosing, bool async, bool isDisposing) { if (State == ReaderState.Closed || State == ReaderState.Disposed) { if (isDisposing) State = ReaderState.Disposed; return; } switch (Connector.State) { case ConnectorState.Ready: case ConnectorState.Fetching: case ConnectorState.Executing: case ConnectorState.Connecting: if (State != ReaderState.Consumed) { try { await Consume(async); } catch (Exception ex) when ( ex is OperationCanceledException || ex is NpgsqlException && ex.InnerException is TimeoutException) { // Timeout/cancellation - completely normal, consume has basically completed. } catch (PostgresException) { // In the case of a PostgresException, the connection is fine and consume has basically completed. // Defer throwing the exception until Cleanup is complete. await Cleanup(async, connectionClosing, isDisposing); throw; } catch { Debug.Assert(Connector.IsBroken); throw; } } break; case ConnectorState.Closed: case ConnectorState.Broken: break; case ConnectorState.Waiting: case ConnectorState.Copy: case ConnectorState.Replication: Debug.Fail("Bad connector state when closing reader: " + Connector.State); break; default: throw new ArgumentOutOfRangeException(); } await Cleanup(async, connectionClosing, isDisposing); } internal async Task Cleanup(bool async, bool connectionClosing = false, bool isDisposing = false) { Log.Trace("Cleaning up reader", Connector.Id); // If multiplexing isn't on, _sendTask contains the task for the writing of this command. // Make sure that this task, which may have executed asynchronously and in parallel with the reading, // has completed, throwing any exceptions it generated. // Note: if the following is removed, mysterious concurrent connection usage errors start happening // on .NET Framework. if (_sendTask != null) { try { if (async) await _sendTask; else _sendTask.GetAwaiter().GetResult(); } catch (Exception e) { // TODO: think of a better way to handle exceptios, see #1323 and #3163 Log.Debug("Exception caught while sending the request", e, Connector.Id); } } State = ReaderState.Closed; Command.State = CommandState.Idle; Connector.CurrentReader = null; if (Log.IsEnabled(NpgsqlLogLevel.Debug)) { Connector.QueryLogStopWatch.Stop(); Log.Debug($"Query duration time: {Connector.QueryLogStopWatch.ElapsedMilliseconds}ms", Connector.Id); Connector.QueryLogStopWatch.Reset(); } Connector.EndUserAction(); NpgsqlEventSource.Log.CommandStop(); // The reader shouldn't be unbound, if we're disposing - so the state is set prematurely if (isDisposing) State = ReaderState.Disposed; if (_connection.ConnectorBindingScope == ConnectorBindingScope.Reader) { // We may unbind the current reader, which also sets the connector to null var connector = Connector; UnbindIfNecessary(); // TODO: Refactor... Use proper scope _connection.Connector = null; connector.Connection = null; _connection.ConnectorBindingScope = ConnectorBindingScope.None; // If the reader is being closed as part of the connection closing, we don't apply // the reader's CommandBehavior.CloseConnection if (_behavior.HasFlag(CommandBehavior.CloseConnection) && !connectionClosing) _connection.Close(); |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |