/// <summary> /// Asynchronously executes the command with a <see cref="RelationalDataReader" /> result. /// </summary> /// <param name="parameterObject"> Parameters for this method. </param> /// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param> /// <returns> /// A task that represents the asynchronous operation. The task result contains the result of the command. /// </returns> public virtual async Task <RelationalDataReader> ExecuteReaderAsync( RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken = default) { var connection = parameterObject.Connection; var context = parameterObject.Context; var readerColumns = parameterObject.ReaderColumns; var logger = parameterObject.Logger; var detailedErrorsEnabled = parameterObject.DetailedErrorsEnabled; var commandId = Guid.NewGuid(); var command = CreateDbCommand(parameterObject, commandId, DbCommandMethod.ExecuteReader); await connection.OpenAsync(cancellationToken).ConfigureAwait(false); var startTime = DateTimeOffset.UtcNow; var stopwatch = Stopwatch.StartNew(); var readerOpen = false; DbDataReader reader; try { var interceptionResult = logger == null ? default : await logger.CommandReaderExecutingAsync( connection, command, context, commandId, connection.ConnectionId, startTime, cancellationToken) .ConfigureAwait(false); reader = interceptionResult.HasResult ? interceptionResult.Result : await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false); if (logger != null) { reader = await logger.CommandReaderExecutedAsync( connection, command, context, commandId, connection.ConnectionId, reader, startTime, stopwatch.Elapsed, cancellationToken) .ConfigureAwait(false); } } catch (Exception exception) { if (logger != null) { await logger.CommandErrorAsync( connection, command, context, DbCommandMethod.ExecuteReader, commandId, connection.ConnectionId, exception, startTime, stopwatch.Elapsed, cancellationToken) .ConfigureAwait(false); } await CleanupCommandAsync(command, connection).ConfigureAwait(false); throw; } try { if (readerColumns != null) { reader = await new BufferedDataReader(reader, detailedErrorsEnabled).InitializeAsync(readerColumns, cancellationToken) .ConfigureAwait(false); } var result = CreateRelationalDataReader( connection, command, reader, commandId, logger); readerOpen = true; return(result); } finally { if (!readerOpen) { await CleanupCommandAsync(command, connection).ConfigureAwait(false); } } }
/// <summary> /// Executes the command with a <see cref="RelationalDataReader" /> result. /// </summary> /// <param name="parameterObject"> Parameters for this method. </param> /// <returns> The result of the command. </returns> public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObject parameterObject) { var connection = parameterObject.Connection; var context = parameterObject.Context; var readerColumns = parameterObject.ReaderColumns; var logger = parameterObject.Logger; var detailedErrorsEnabled = parameterObject.DetailedErrorsEnabled; var commandId = Guid.NewGuid(); var command = CreateDbCommand(parameterObject, commandId, DbCommandMethod.ExecuteReader); connection.Open(); var startTime = DateTimeOffset.UtcNow; var stopwatch = Stopwatch.StartNew(); var readerOpen = false; DbDataReader reader; try { var interceptionResult = logger?.CommandReaderExecuting( connection, command, context, commandId, connection.ConnectionId, startTime) ?? default; reader = interceptionResult.HasResult ? interceptionResult.Result : command.ExecuteReader(); if (logger != null) { reader = logger.CommandReaderExecuted( connection, command, context, commandId, connection.ConnectionId, reader, startTime, stopwatch.Elapsed); } } catch (Exception exception) { logger?.CommandError( connection, command, context, DbCommandMethod.ExecuteReader, commandId, connection.ConnectionId, exception, startTime, stopwatch.Elapsed); CleanupCommand(command, connection); throw; } try { if (readerColumns != null) { reader = new BufferedDataReader(reader, detailedErrorsEnabled).Initialize(readerColumns); } var result = CreateRelationalDataReader( connection, command, reader, commandId, logger); readerOpen = true; return(result); } finally { if (!readerOpen) { CleanupCommand(command, connection); } } }
/// <summary> /// Asynchronously executes the command with a single scalar result. /// </summary> /// <param name="parameterObject"> Parameters for this method. </param> /// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param> /// <returns> /// A task that represents the asynchronous operation. The task result contains the result of the command. /// </returns> public virtual async Task <object> ExecuteScalarAsync( RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken = default) { var(connection, context, logger) = (parameterObject.Connection, parameterObject.Context, parameterObject.Logger); var commandId = Guid.NewGuid(); var command = CreateDbCommand(parameterObject, commandId, DbCommandMethod.ExecuteScalar); await connection.OpenAsync(cancellationToken).ConfigureAwait(false); var startTime = DateTimeOffset.UtcNow; var stopwatch = Stopwatch.StartNew(); try { var interceptionResult = logger == null ? default : await logger.CommandScalarExecutingAsync( connection, command, context, commandId, connection.ConnectionId, startTime, cancellationToken) .ConfigureAwait(false); var result = interceptionResult.HasResult ? interceptionResult.Result : await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false); if (logger != null) { result = await logger.CommandScalarExecutedAsync( connection, command, context, commandId, connection.ConnectionId, result, startTime, stopwatch.Elapsed, cancellationToken).ConfigureAwait(false); } return(result); } catch (Exception exception) { if (logger != null) { await logger.CommandErrorAsync( connection, command, context, DbCommandMethod.ExecuteScalar, commandId, connection.ConnectionId, exception, startTime, stopwatch.Elapsed, cancellationToken) .ConfigureAwait(false); } throw; } finally { await CleanupCommandAsync(command, connection).ConfigureAwait(false); } }
/// <summary> /// Asynchronously executes the command with a <see cref="RelationalDataReader" /> result. /// </summary> /// <param name="parameterObject"> Parameters for this method. </param> /// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param> /// <returns> /// A task that represents the asynchronous operation. The task result contains the result of the command. /// </returns> public virtual async Task <RelationalDataReader> ExecuteReaderAsync( RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken = default) { var(connection, context, logger) = (parameterObject.Connection, parameterObject.Context, parameterObject.Logger); var command = CreateCommand(connection, parameterObject.ParameterValues); await connection.OpenAsync(cancellationToken); var commandId = Guid.NewGuid(); var startTime = DateTimeOffset.UtcNow; var stopwatch = Stopwatch.StartNew(); var readerOpen = false; try { var interceptionResult = logger == null ? default : await logger.CommandReaderExecutingAsync( command, context, commandId, connection.ConnectionId, startTime, cancellationToken); var reader = interceptionResult.HasResult ? interceptionResult.Result : await command.ExecuteReaderAsync(cancellationToken); if (logger != null) { reader = await logger.CommandReaderExecutedAsync( command, context, commandId, connection.ConnectionId, reader, startTime, stopwatch.Elapsed, cancellationToken); } var result = new RelationalDataReader( connection, command, reader, commandId, logger); readerOpen = true; return(result); } catch (Exception exception) { if (logger != null) { await logger.CommandErrorAsync( command, context, DbCommandMethod.ExecuteReader, commandId, connection.ConnectionId, exception, startTime, stopwatch.Elapsed, cancellationToken); } throw; } finally { if (!readerOpen) { await CleanupCommandAsync(command, connection); } } }
/// <summary> /// Executes the command with a <see cref="RelationalDataReader" /> result. /// </summary> /// <param name="parameterObject"> Parameters for this method. </param> /// <returns> The result of the command. </returns> public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObject parameterObject) { var(connection, context, logger) = (parameterObject.Connection, parameterObject.Context, parameterObject.Logger); var command = CreateCommand(connection, parameterObject.ParameterValues); connection.Open(); var commandId = Guid.NewGuid(); var startTime = DateTimeOffset.UtcNow; var stopwatch = Stopwatch.StartNew(); var readerOpen = false; try { var interceptionResult = logger?.CommandReaderExecuting( command, context, commandId, connection.ConnectionId, startTime) ?? default; var reader = interceptionResult.HasResult ? interceptionResult.Result : command.ExecuteReader(); if (logger != null) { reader = logger.CommandReaderExecuted( command, context, commandId, connection.ConnectionId, reader, startTime, stopwatch.Elapsed); } var result = new RelationalDataReader( connection, command, reader, commandId, logger); readerOpen = true; return(result); } catch (Exception exception) { logger?.CommandError( command, context, DbCommandMethod.ExecuteReader, commandId, connection.ConnectionId, exception, startTime, stopwatch.Elapsed); throw; } finally { if (!readerOpen) { CleanupCommand(command, connection); } } }
/// <summary> /// Asynchronously executes the command with no results. /// </summary> /// <param name="parameterObject"> Parameters for this method. </param> /// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param> /// <returns> /// A task that represents the asynchronous operation. The task result contains the number of rows affected. /// </returns> public virtual async Task <int> ExecuteNonQueryAsync( RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken = default) { var(connection, context, logger) = (parameterObject.Connection, parameterObject.Context, parameterObject.Logger); var command = CreateCommand(connection, parameterObject.ParameterValues); await connection.OpenAsync(cancellationToken); var commandId = Guid.NewGuid(); var startTime = DateTimeOffset.UtcNow; var stopwatch = Stopwatch.StartNew(); try { var interceptionResult = logger == null ? null : await logger.CommandNonQueryExecutingAsync( command, context, commandId, connection.ConnectionId, startTime, cancellationToken); var result = interceptionResult.HasValue ? interceptionResult.Value.Result : await command.ExecuteNonQueryAsync(cancellationToken); if (logger != null) { result = await logger.CommandNonQueryExecutedAsync( command, context, commandId, connection.ConnectionId, result, startTime, stopwatch.Elapsed, cancellationToken); } return(result); } catch (Exception exception) { logger?.CommandError( command, context, DbCommandMethod.ExecuteNonQuery, commandId, connection.ConnectionId, exception, true, startTime, stopwatch.Elapsed); throw; } finally { CleanupCommand(command, connection); } }