public IEnumerable <IDataReader> Execute(IBatch batch) { IDbConnection connection = this.GetOpenConnection(); IDbCommand dbCommand = null; void applyException(Exception ex) => this.ApplyFilters(h => h.OnException, dbCommand: dbCommand, exception: ex, batch: batch, swallowExceptions: true); try { dbCommand = connection.CreateCommand(); batch.Build(dbCommand); } catch (Exception ex) { applyException(ex); dbCommand?.Dispose(); throw; } this.ApplyFilters(h => h.OnCommandCreated, dbCommand: dbCommand, batch: batch); IDataReader reader = null; try { reader = dbCommand.ExecuteReader(); } catch (Exception ex) { applyException(ex); throw; } return(innerReader()); IEnumerable <IDataReader> innerReader() { try { bool nextResult = true; while (nextResult) { yield return(reader); try { nextResult = reader.NextResult(); } catch (Exception ex) { applyException(ex); throw; } } this.ApplyFilters(h => h.OnCommandExecuted, dbCommand, batch: batch); } finally { reader.Dispose(); } } }
public async IAsyncEnumerable <DbDataReader> ExecuteAsync(IBatch batch, [EnumeratorCancellation] CancellationToken cancellationToken = default) { DbConnection connection = await this.GetOpenConnectionAsync(cancellationToken).ConfigureAwait(false); DbCommand dbCommand = null; Task applyException(Exception ex) => this.ApplyFilters(h => h.OnExceptionAsync, dbCommand: dbCommand, exception: ex, batch: batch, swallowExceptions: true); try { dbCommand = connection.CreateCommand(); batch.Build(dbCommand); } catch (Exception ex) { await applyException(ex).ConfigureAwait(false); dbCommand?.Dispose(); throw; } await this.ApplyFilters(h => h.OnCommandCreatedAsync, dbCommand : dbCommand, batch : batch).ConfigureAwait(false); DbDataReader reader = null; try { reader = dbCommand.ExecuteReader(); } catch (Exception ex) { await applyException(ex).ConfigureAwait(false); throw; } try { bool nextResult = true; while (nextResult) { yield return(reader); try { nextResult = await reader.NextResultAsync().ConfigureAwait(false); } catch (Exception ex) { await applyException(ex).ConfigureAwait(false); throw; } } await this.ApplyFilters(h => h.OnCommandExecutedAsync, dbCommand : dbCommand, batch : batch).ConfigureAwait(false); } finally { #if NET21_BASE await reader.DisposeAsync().ConfigureAwait(false); #else reader.Dispose(); #endif } }