-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
RelationalDataReader.cs
181 lines (163 loc) · 6.6 KB
/
RelationalDataReader.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Data.Common;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Utilities;
namespace Microsoft.EntityFrameworkCore.Storage
{
/// <summary>
/// <para>
/// Reads result sets from a relational database.
/// </para>
/// <para>
/// This type is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// </summary>
public class RelationalDataReader : IDisposable, IAsyncDisposable
{
private readonly IRelationalConnection _connection;
private readonly DbCommand _command;
private readonly DbDataReader _reader;
private readonly Guid _commandId;
private readonly IDiagnosticsLogger<DbLoggerCategory.Database.Command> _logger;
private readonly DateTimeOffset _startTime;
private readonly Stopwatch _stopwatch;
private int _readCount;
private bool _disposed;
/// <summary>
/// Initializes a new instance of the <see cref="RelationalDataReader" /> class.
/// </summary>
/// <param name="connection"> The connection. </param>
/// <param name="command"> The command that was executed. </param>
/// <param name="reader"> The underlying reader for the result set. </param>
/// <param name="commandId"> A correlation ID that identifies the <see cref="DbCommand" /> instance being used. </param>
/// <param name="logger"> The diagnostic source. </param>
public RelationalDataReader(
[NotNull] IRelationalConnection connection,
[NotNull] DbCommand command,
[NotNull] DbDataReader reader,
Guid commandId,
[CanBeNull] IDiagnosticsLogger<DbLoggerCategory.Database.Command> logger)
{
Check.NotNull(connection, nameof(connection));
Check.NotNull(command, nameof(command));
Check.NotNull(reader, nameof(reader));
_connection = connection;
_command = command;
_reader = reader;
_commandId = commandId;
_logger = logger;
_startTime = DateTimeOffset.UtcNow;
_stopwatch = Stopwatch.StartNew();
}
/// <summary>
/// Gets the underlying reader for the result set.
/// </summary>
public virtual DbDataReader DbDataReader
=> _reader;
/// <summary>
/// Gets the underlying command for the result set.
/// </summary>
public virtual DbCommand DbCommand
=> _command;
/// <summary>
/// Calls <see cref="System.Data.Common.DbDataReader.Read()" /> on the underlying <see cref="System.Data.Common.DbDataReader" />.
/// </summary>
/// <returns> <see langword="true" /> if there are more rows; otherwise <see langword="false" />. </returns>
public virtual bool Read()
{
_readCount++;
return _reader.Read();
}
/// <summary>
/// Calls <see cref="System.Data.Common.DbDataReader.ReadAsync(CancellationToken)" /> on the underlying
/// <see cref="System.Data.Common.DbDataReader" />.
/// </summary>
/// <returns> <see langword="true" /> if there are more rows; otherwise <see langword="false" />. </returns>
public virtual Task<bool> ReadAsync(CancellationToken cancellationToken = default)
{
_readCount++;
return _reader.ReadAsync(cancellationToken);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public virtual void Dispose()
{
if (!_disposed)
{
var interceptionResult = default(InterceptionResult);
try
{
_reader.Close(); // can throw
if (_logger != null)
{
interceptionResult = _logger.DataReaderDisposing(
_connection,
_command,
_reader,
_commandId,
_reader.RecordsAffected,
_readCount,
_startTime,
_stopwatch.Elapsed); // can throw
}
}
finally
{
_disposed = true;
if (!interceptionResult.IsSuppressed)
{
_reader.Dispose();
_command.Parameters.Clear();
_command.Dispose();
_connection.Close();
}
}
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public virtual async ValueTask DisposeAsync()
{
if (!_disposed)
{
var interceptionResult = default(InterceptionResult);
try
{
_reader.Close(); // can throw
if (_logger != null)
{
interceptionResult = _logger.DataReaderDisposing(
_connection,
_command,
_reader,
_commandId,
_reader.RecordsAffected,
_readCount,
_startTime,
_stopwatch.Elapsed); // can throw
}
}
finally
{
_disposed = true;
if (!interceptionResult.IsSuppressed)
{
await _reader.DisposeAsync().ConfigureAwait(false);
_command.Parameters.Clear();
await _command.DisposeAsync().ConfigureAwait(false);
await _connection.CloseAsync().ConfigureAwait(false);
}
}
}
}
}
}