コード例 #1
0
ファイル: Truncate.cs プロジェクト: vikingcodes/RepoDB
        /// <summary>
        /// Truncates a table from the database in an asychronous way.
        /// </summary>
        /// <param name="connection">The connection object to be used.</param>
        /// <param name="request">The actual <see cref="TruncateRequest"/> object.</param>
        /// <param name="commandTimeout">The command timeout in seconds to be used.</param>
        /// <param name="transaction">The transaction to be used.</param>
        /// <param name="trace">The trace object to be used.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> object to be used during the asynchronous operation.</param>
        /// <returns>The number of rows affected.</returns>
        internal static async Task <int> TruncateAsyncInternalBase(this IDbConnection connection,
                                                                   TruncateRequest request,
                                                                   int?commandTimeout         = null,
                                                                   IDbTransaction transaction = null,
                                                                   ITrace trace = null,
                                                                   CancellationToken cancellationToken = default)
        {
            // Variables
            var commandType = CommandType.Text;
            var commandText = CommandTextCache.GetTruncateText(request);
            var sessionId   = Guid.Empty;

            // Before Execution
            if (trace != null)
            {
                sessionId = Guid.NewGuid();
                var cancellableTraceLog = new CancellableTraceLog(sessionId, commandText, null, null);
                trace.BeforeTruncate(cancellableTraceLog);
                if (cancellableTraceLog.IsCancelled)
                {
                    if (cancellableTraceLog.IsThrowException)
                    {
                        throw new CancelledExecutionException(commandText);
                    }
                }
                commandText = (cancellableTraceLog.Statement ?? commandText);
            }

            // Before Execution Time
            var beforeExecutionTime = DateTime.UtcNow;

            // Actual Execution
            var result = await ExecuteNonQueryAsyncInternal(connection : connection,
                                                            commandText : commandText,
                                                            param : null,
                                                            commandType : commandType,
                                                            commandTimeout : commandTimeout,
                                                            transaction : transaction,
                                                            cancellationToken : cancellationToken,
                                                            entityType : request.Type,
                                                            dbFields : await DbFieldCache.GetAsync(connection, request.Name, transaction, true, cancellationToken),
                                                            skipCommandArrayParametersCheck : true);

            // After Execution
            if (trace != null)
            {
                trace.AfterTruncate(new TraceLog(sessionId, commandText, null, result,
                                                 DateTime.UtcNow.Subtract(beforeExecutionTime)));
            }

            // Return the result
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// Truncates a table from the database in an asychronous way.
        /// </summary>
        /// <param name="connection">The connection object to be used.</param>
        /// <param name="request">The actual <see cref="TruncateRequest"/> object.</param>
        /// <param name="commandTimeout">The command timeout in seconds to be used.</param>
        /// <param name="transaction">The transaction to be used.</param>
        /// <param name="trace">The trace object to be used.</param>
        /// <returns>The number of rows affected.</returns>
        internal static async Task <int> TruncateAsyncInternalBase(this IDbConnection connection,
                                                                   TruncateRequest request,
                                                                   int?commandTimeout         = null,
                                                                   IDbTransaction transaction = null,
                                                                   ITrace trace = null)
        {
            // Validate
            InvokeValidatorValidateTruncateAsync(connection);

            // Variables
            var commandType = CommandType.Text;
            var commandText = CommandTextCache.GetTruncateText(request);

            // Before Execution
            if (trace != null)
            {
                var cancellableTraceLog = new CancellableTraceLog(commandText, null, null);
                trace.BeforeTruncate(cancellableTraceLog);
                if (cancellableTraceLog.IsCancelled)
                {
                    if (cancellableTraceLog.IsThrowException)
                    {
                        throw new CancelledExecutionException(commandText);
                    }
                }
                commandText = (cancellableTraceLog.Statement ?? commandText);
            }

            // Before Execution Time
            var beforeExecutionTime = DateTime.UtcNow;

            // Actual Execution
            var result = await ExecuteNonQueryAsyncInternal(connection : connection,
                                                            commandText : commandText,
                                                            param : null,
                                                            commandType : commandType,
                                                            commandTimeout : commandTimeout,
                                                            transaction : transaction,
                                                            skipCommandArrayParametersCheck : true);

            // After Execution
            if (trace != null)
            {
                trace.AfterTruncate(new TraceLog(commandText, null, result,
                                                 DateTime.UtcNow.Subtract(beforeExecutionTime)));
            }

            // Return the result
            return(result);
        }