/// <summary> /// /// </summary> /// <param name="request"></param> /// <returns></returns> internal static string GetTruncateText(TruncateRequest request) { if (cache.TryGetValue(request, out var commandText) == false) { var statementBuilder = EnsureStatementBuilder(request.Connection, request.StatementBuilder); commandText = statementBuilder.CreateTruncate(new QueryBuilder(), request.Name); cache.TryAdd(request, commandText); } return(commandText); }
/// <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); }
/// <summary> /// Gets a command text from the cache for the <see cref="DbConnectionExtension.Truncate{TEntity}(IDbConnection, int?, ITrace, IStatementBuilder)"/> operation. /// </summary> /// <typeparam name="TEntity">The type of the target entity.</typeparam> /// <param name="request">The request object.</param> /// <returns>The cached command text.</returns> public static string GetTruncateText <TEntity>(TruncateRequest request) where TEntity : class { var commandText = (string)null; if (m_cache.TryGetValue(request, out commandText) == false) { var statementBuilder = EnsureStatementBuilder(request.Connection, request.StatementBuilder); commandText = statementBuilder.CreateTruncate(queryBuilder: new QueryBuilder <TEntity>()); m_cache.TryAdd(request, commandText); } return(commandText); }
/// <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); }
/// <summary> /// Truncates a table from the database. /// </summary> /// <typeparam name="TEntity">The type of the data entity object.</typeparam> /// <param name="connection">The connection object to be used.</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="statementBuilder">The statement builder object to be used.</param> /// <returns>The number of rows affected.</returns> internal static int TruncateInternal <TEntity>(this IDbConnection connection, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null, IStatementBuilder statementBuilder = null) where TEntity : class { // Variables var request = new TruncateRequest(typeof(TEntity), connection, statementBuilder); // Return the result return(TruncateInternalBase(connection: connection, request: request, commandTimeout: commandTimeout, transaction: transaction, trace: trace)); }
/// <summary> /// Truncates a table from the database in an asynchronous way. /// </summary> /// <param name="connection">The connection object to be used.</param> /// <param name="tableName">The name of the target table.</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="statementBuilder">The statement builder object to be used.</param> /// <returns>The number of rows affected.</returns> internal static Task <int> TruncateAsyncInternal(this IDbConnection connection, string tableName, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null, IStatementBuilder statementBuilder = null) { // Variables var request = new TruncateRequest(tableName, connection, statementBuilder); // Return the result return(TruncateAsyncInternalBase(connection: connection, request: request, commandTimeout: commandTimeout, transaction: transaction, trace: trace)); }
/// <summary> /// Truncates a table from the database in an asynchronous way. /// </summary> /// <typeparam name="TEntity">The type of the data entity.</typeparam> /// <param name="connection">The connection object to be used.</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="statementBuilder">The statement builder 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 Task <int> TruncateAsyncInternal <TEntity>(this IDbConnection connection, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null, IStatementBuilder statementBuilder = null, CancellationToken cancellationToken = default) where TEntity : class { // Variables var request = new TruncateRequest(typeof(TEntity), connection, transaction, statementBuilder); // Return the result return(TruncateAsyncInternalBase(connection: connection, request: request, commandTimeout: commandTimeout, transaction: transaction, trace: trace, cancellationToken: cancellationToken)); }