Exemplo n.º 1
0
        public async IAsyncEnumerable <DbDataReader> ExecuteAsync(IOperation operation, [EnumeratorCancellation] CancellationToken cancellationToken)
        {
            DbConnection connection = await this.GetOpenConnectionAsync(cancellationToken).ConfigureAwait(false);

            DbCommand dbCommand = null;

            Task applyException(Exception ex) => this.ApplyFilters(h => h.OnExceptionAsync, dbCommand: dbCommand, exception: ex, source: operation.Source, swallowExceptions: true);

            try
            {
                dbCommand = connection.CreateCommand();

                operation.Build(dbCommand);
            }
            catch (Exception ex)
            {
                await applyException(ex).ConfigureAwait(false);

                dbCommand?.Dispose();

                throw;
            }

            await this.ApplyFilters(h => h.OnCommandCreatedAsync, dbCommand : dbCommand, source : operation.Source).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, source : operation.Source).ConfigureAwait(false);
            }
            finally
            {
#if !NETSTANDARD2_0
                await reader.DisposeAsync().ConfigureAwait(false);
#else
                reader.Dispose();
#endif
            }
        }
Exemplo n.º 2
0
        public IEnumerable <IDataReader> Execute(IOperation operation)
        {
            IDbConnection connection = this.GetOpenConnection();
            IDbCommand    dbCommand  = null;

            void applyException(Exception ex) => this.ApplyFilters(h => h.OnException, dbCommand: dbCommand, exception: ex, source: operation.Source, swallowExceptions: true);

            try
            {
                dbCommand = connection.CreateCommand();

                operation.Build(dbCommand);
            }
            catch (Exception ex)
            {
                applyException(ex);

                dbCommand?.Dispose();

                throw;
            }

            this.ApplyFilters(h => h.OnCommandCreated, dbCommand: dbCommand, source: operation.Source);

            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, source: operation.Source);
                }
                finally
                {
                    reader.Dispose();
                }
            }
        }