Exemple #1
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="DbCommand"/>.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="DbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler,
        ///     which should return the <see cref="DbDataReader"/> instance if it is an ExecuteReader operation.
        ///     If it is not ExecuteReader, it should return null.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public virtual void ExecuteDbCommand(DbExecuteType executeType, DbCommand command, Func <DbDataReader> execute, TagCollection tags)
        {
            if (execute == null)
            {
                return;
            }

            if (command == null)
            {
                execute();
                return;
            }

            var dbTiming = new DbTiming(_profiler, executeType, command)
            {
                Tags = tags
            };

            var dataReader = execute();

            if (dataReader == null)
            {
                // if not executing reader, stop the sql timing right after execute()
                dbTiming.Stop();
                return;
            }

            dbTiming.FirstFetch();
            var reader = dataReader as ProfiledDbDataReader ??
                         new ProfiledDbDataReader(dataReader, this);

            _inProgressDataReaders[reader] = dbTiming;
        }
Exemple #2
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="DbCommand"/> asynchronously.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="DbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler,
        ///     which should return a scalar value.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public async Task <object> ExecuteDbCommandAsync(DbExecuteType executeType, DbCommand command, Func <Task <object> > execute, TagCollection tags)
        {
            if (execute == null)
            {
                return(null);
            }

            if (executeType == DbExecuteType.Reader)
            {
                throw new NotSupportedException("ExecuteDbCommandAsync doesn't support executing data reader.");
            }

            if (command == null)
            {
                return(await execute());
            }

            var dbTiming = new DbTiming(_profiler, executeType, command)
            {
                Tags = tags
            };

            try
            {
                return(await execute());
            }
            finally
            {
                dbTiming.Stop();
            }
        }
Exemple #3
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="DbCommand"/> asynchronously.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="DbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler,
        ///     which should return a scalar value.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public async Task <object> ExecuteDbCommandAsync(DbExecuteType executeType, DbCommand command, Func <Task <object> > execute, TagCollection tags)
        {
            if (execute == null)
            {
                return(null);
            }

            if (command == null)
            {
                return(await execute());
            }

            var dbTiming = new DbTiming(_profiler, executeType, command)
            {
                Tags = tags
            };

            if (executeType == DbExecuteType.Reader)
            {
                // for ExecuteReader
                var dataReader = (await execute()) as DbDataReader;
                if (dataReader == null)
                {
                    // if not executing reader, stop the sql timing right after execute()
                    dbTiming.Stop();
                    return(null);
                }

                dbTiming.FirstFetch();
                var reader = dataReader as ProfiledDbDataReader ??
                             new ProfiledDbDataReader(dataReader, this);
                _inProgressDataReaders[reader] = dbTiming;

                return(reader);
            }

            // for ExecuteNonQuery and ExecuteScalar
            try
            {
                return(await execute());
            }
            finally
            {
                dbTiming.Stop();
            }
        }