@@ -114,30 +114,23 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) =>
114
114
public override Task < int > ExecuteNonQueryAsync ( CancellationToken cancellationToken ) =>
115
115
ExecuteNonQueryAsync ( Connection . AsyncIOBehavior , cancellationToken ) ;
116
116
117
- internal async Task < int > ExecuteNonQueryAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken )
118
- {
119
- VerifyValid ( ) ;
120
- return await m_commandExecutor . ExecuteNonQueryAsync ( CommandText , m_parameterCollection , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
121
- }
117
+ internal Task < int > ExecuteNonQueryAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken ) =>
118
+ ! IsValid ( out var exception ) ? Utility . TaskFromException < int > ( exception ) :
119
+ m_commandExecutor . ExecuteNonQueryAsync ( CommandText , m_parameterCollection , ioBehavior , cancellationToken ) ;
122
120
123
121
public override Task < object > ExecuteScalarAsync ( CancellationToken cancellationToken ) =>
124
122
ExecuteScalarAsync ( Connection . AsyncIOBehavior , cancellationToken ) ;
125
123
126
- internal async Task < object > ExecuteScalarAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken )
127
- {
128
- VerifyValid ( ) ;
129
- return await m_commandExecutor . ExecuteScalarAsync ( CommandText , m_parameterCollection , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
130
- }
124
+ internal Task < object > ExecuteScalarAsync ( IOBehavior ioBehavior , CancellationToken cancellationToken ) =>
125
+ ! IsValid ( out var exception ) ? Utility . TaskFromException < object > ( exception ) :
126
+ m_commandExecutor . ExecuteScalarAsync ( CommandText , m_parameterCollection , ioBehavior , cancellationToken ) ;
131
127
132
128
protected override Task < DbDataReader > ExecuteDbDataReaderAsync ( CommandBehavior behavior , CancellationToken cancellationToken ) =>
133
129
ExecuteReaderAsync ( behavior , Connection . AsyncIOBehavior , cancellationToken ) ;
134
130
135
- internal async Task < DbDataReader > ExecuteReaderAsync ( CommandBehavior behavior , IOBehavior ioBehavior ,
136
- CancellationToken cancellationToken )
137
- {
138
- VerifyValid ( ) ;
139
- return await m_commandExecutor . ExecuteReaderAsync ( CommandText , m_parameterCollection , behavior , ioBehavior , cancellationToken ) . ConfigureAwait ( false ) ;
140
- }
131
+ internal Task < DbDataReader > ExecuteReaderAsync ( CommandBehavior behavior , IOBehavior ioBehavior , CancellationToken cancellationToken ) =>
132
+ ! IsValid ( out var exception ) ? Utility . TaskFromException < DbDataReader > ( exception ) :
133
+ m_commandExecutor . ExecuteReaderAsync ( CommandText , m_parameterCollection , behavior , ioBehavior , cancellationToken ) ;
141
134
142
135
protected override void Dispose ( bool disposing )
143
136
{
@@ -156,6 +149,23 @@ protected override void Dispose(bool disposing)
156
149
157
150
internal new MySqlConnection Connection => ( MySqlConnection ) DbConnection ;
158
151
152
+ /// <summary>
153
+ /// Registers <see cref="Cancel"/> as a callback with <paramref name="token"/> if cancellation is supported.
154
+ /// </summary>
155
+ /// <param name="token">The <see cref="CancellationToken"/>.</param>
156
+ /// <returns>An object that must be disposed to revoke the cancellation registration.</returns>
157
+ /// <remarks>This method is more efficient than calling <code>token.Register(Command.Cancel)</code> because it avoids
158
+ /// unnecessary allocations.</remarks>
159
+ internal IDisposable RegisterCancel ( CancellationToken token )
160
+ {
161
+ if ( ! token . CanBeCanceled )
162
+ return null ;
163
+
164
+ if ( m_cancelAction == null )
165
+ m_cancelAction = Cancel ;
166
+ return token . Register ( m_cancelAction ) ;
167
+ }
168
+
159
169
private void VerifyNotDisposed ( )
160
170
{
161
171
if ( m_parameterCollection == null )
@@ -164,21 +174,32 @@ private void VerifyNotDisposed()
164
174
165
175
private void VerifyValid ( )
166
176
{
167
- VerifyNotDisposed ( ) ;
168
- if ( DbConnection == null )
169
- throw new InvalidOperationException ( "Connection property must be non-null." ) ;
170
- if ( DbConnection . State != ConnectionState . Open && DbConnection . State != ConnectionState . Connecting )
171
- throw new InvalidOperationException ( "Connection must be Open; current state is {0}" . FormatInvariant ( DbConnection . State ) ) ;
172
- if ( DbTransaction != Connection . CurrentTransaction )
173
- throw new InvalidOperationException ( "The transaction associated with this command is not the connection's active transaction." ) ;
174
- if ( string . IsNullOrWhiteSpace ( CommandText ) )
175
- throw new InvalidOperationException ( "CommandText must be specified" ) ;
177
+ Exception exception ;
178
+ if ( ! IsValid ( out exception ) )
179
+ throw exception ;
180
+ }
181
+
182
+ private bool IsValid ( out Exception exception )
183
+ {
184
+ exception = null ;
185
+ if ( m_parameterCollection == null )
186
+ exception = new ObjectDisposedException ( GetType ( ) . Name ) ;
187
+ else if ( DbConnection == null )
188
+ exception = new InvalidOperationException ( "Connection property must be non-null." ) ;
189
+ else if ( DbConnection . State != ConnectionState . Open && DbConnection . State != ConnectionState . Connecting )
190
+ exception = new InvalidOperationException ( "Connection must be Open; current state is {0}" . FormatInvariant ( DbConnection . State ) ) ;
191
+ else if ( DbTransaction != Connection . CurrentTransaction )
192
+ exception = new InvalidOperationException ( "The transaction associated with this command is not the connection's active transaction." ) ;
193
+ else if ( string . IsNullOrWhiteSpace ( CommandText ) )
194
+ exception = new InvalidOperationException ( "CommandText must be specified" ) ;
195
+ return exception == null ;
176
196
}
177
197
178
198
internal void ReaderClosed ( ) => ( m_commandExecutor as StoredProcedureCommandExecutor ) ? . SetParams ( ) ;
179
199
180
200
MySqlParameterCollection m_parameterCollection ;
181
201
CommandType m_commandType ;
182
202
ICommandExecutor m_commandExecutor ;
203
+ Action m_cancelAction ;
183
204
}
184
205
}
0 commit comments