/// <summary> /// Averages the target field from all data of the database table. /// </summary> /// <typeparam name="TEntity">The type of the data entity.</typeparam> /// <param name="connection">The connection object to be used.</param> /// <param name="field">The field to be averaged.</param> /// <param name="hints">The table hints 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 average value.</returns> internal static object AverageAllInternal <TEntity>(this IDbConnection connection, Field field, string hints = null, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null, IStatementBuilder statementBuilder = null) where TEntity : class { // Variables var request = new AverageAllRequest(typeof(TEntity), connection, transaction, field, hints, statementBuilder); var param = (object)null; // Return the result return(AverageAllInternalBase(connection: connection, request: request, param: param, commandTimeout: commandTimeout, transaction: transaction, trace: trace)); }
/// <averagemary> /// Computes the average value of the target field. /// </averagemary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="connection">The connection object to be used.</param> /// <param name="tableName">The name of the target table to be used.</param> /// <param name="field">The field to be averaged.</param> /// <param name="hints">The table hints 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 average value of the target field.</returns> internal static TResult AverageAllInternal <TResult>(this IDbConnection connection, string tableName, Field field, string hints = null, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null, IStatementBuilder statementBuilder = null) { // Variables var request = new AverageAllRequest(tableName, connection, transaction, field, hints, statementBuilder); // Return the result return(AverageAllInternalBase <TResult>(connection: connection, request: request, param: null, commandTimeout: commandTimeout, transaction: transaction, trace: trace)); }
/// <summary> /// Averages the target field from the database table in an asynchronous way. /// </summary> /// <param name="connection">The connection object to be used.</param> /// <param name="tableName">The name of the target table to be used.</param> /// <param name="field">The field to be averaged.</param> /// <param name="hints">The table hints 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 average value.</returns> internal static Task <object> AverageAllAsyncInternal(this IDbConnection connection, string tableName, Field field, string hints = null, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null, IStatementBuilder statementBuilder = null) { // Variables var request = new AverageAllRequest(tableName, connection, transaction, field, hints, statementBuilder); var param = (object)null; // Return the result return(AverageAllInternalAsyncBase(connection: connection, request: request, param: param, commandTimeout: commandTimeout, transaction: transaction, trace: trace)); }
/// <averagemary> /// Computes the average value of the target field. /// </averagemary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="connection">The connection object to be used.</param> /// <param name="request">The actual <see cref="AverageAllRequest"/> object.</param> /// <param name="param">The mapped object parameters.</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 average value of the target field.</returns> internal static TResult AverageAllInternalBase <TResult>(this IDbConnection connection, AverageAllRequest request, object param, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null) { // Variables var commandType = CommandType.Text; var commandText = CommandTextCache.GetAverageAllText(request); var sessionId = Guid.Empty; // Before Execution if (trace != null) { sessionId = Guid.NewGuid(); var cancellableTraceLog = new CancellableTraceLog(sessionId, commandText, param, null); trace.BeforeAverageAll(cancellableTraceLog); if (cancellableTraceLog.IsCancelled) { if (cancellableTraceLog.IsThrowException) { throw new CancelledExecutionException(commandText); } return(default);
/// <averagemary> /// Computes the average value of the target field in an asynchronous way. /// </averagemary> /// <typeparam name="TEntity">The type of the data entity.</typeparam> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="connection">The connection object to be used.</param> /// <param name="field">The field to be averaged.</param> /// <param name="hints">The table hints 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 average value of the target field.</returns> internal static Task <TResult> AverageAllAsyncInternal <TEntity, TResult>(this IDbConnection connection, Field field, string hints = null, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null, IStatementBuilder statementBuilder = null, CancellationToken cancellationToken = default) where TEntity : class { // Variables var request = new AverageAllRequest(typeof(TEntity), connection, transaction, field, hints, statementBuilder); var param = (object)null; // Return the result return(AverageAllAsyncInternalBase <TResult>(connection: connection, request: request, param: param, commandTimeout: commandTimeout, transaction: transaction, trace: trace, cancellationToken: cancellationToken)); }
/// <averagemary> /// Computes the average value of the target field in an asynchronous way. /// </averagemary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="connection">The connection object to be used.</param> /// <param name="request">The actual <see cref="AverageAllRequest"/> object.</param> /// <param name="param">The mapped object parameters.</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 average value of the target field.</returns> internal static async Task <TResult> AverageAllAsyncInternalBase <TResult>(this IDbConnection connection, AverageAllRequest request, object param, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null, CancellationToken cancellationToken = default) { // Variables var commandType = CommandType.Text; var commandText = CommandTextCache.GetAverageAllText(request); var sessionId = Guid.Empty; // Before Execution if (trace != null) { sessionId = Guid.NewGuid(); var cancellableTraceLog = new CancellableTraceLog(sessionId, commandText, param, null); trace.BeforeAverageAll(cancellableTraceLog); if (cancellableTraceLog.IsCancelled) { if (cancellableTraceLog.IsThrowException) { throw new CancelledExecutionException(commandText); } return(default(TResult)); } commandText = (cancellableTraceLog.Statement ?? commandText); param = (cancellableTraceLog.Parameter ?? param); } // Before Execution Time var beforeExecutionTime = DateTime.UtcNow; // Actual Execution var result = await ExecuteScalarAsyncInternal <TResult>(connection : connection, commandText : commandText, param : param, commandType : commandType, cacheKey : null, cacheItemExpiration : null, commandTimeout : commandTimeout, transaction : transaction, cache : null, cancellationToken : cancellationToken, entityType : request.Type, dbFields : await DbFieldCache.GetAsync(connection, request.Name, transaction, true, cancellationToken), skipCommandArrayParametersCheck : true); // After Execution if (trace != null) { trace.AfterAverageAll(new TraceLog(sessionId, commandText, param, result, DateTime.UtcNow.Subtract(beforeExecutionTime))); } // Result return(result); }
/// <summary> /// Computes the average value of the target field. /// </summary> /// <param name="connection">The connection object to be used.</param> /// <param name="request">The actual <see cref="AverageAllRequest"/> object.</param> /// <param name="param">The mapped object parameters.</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 average value of the target field.</returns> internal static object AverageAllInternalBase(this IDbConnection connection, AverageAllRequest request, object param, int?commandTimeout = null, IDbTransaction transaction = null, ITrace trace = null) { // Variables var commandType = CommandType.Text; var commandText = CommandTextCache.GetAverageAllText(request); var sessionId = Guid.Empty; // Before Execution if (trace != null) { sessionId = Guid.NewGuid(); var cancellableTraceLog = new CancellableTraceLog(sessionId, commandText, param, null); trace.BeforeAverageAll(cancellableTraceLog); if (cancellableTraceLog.IsCancelled) { if (cancellableTraceLog.IsThrowException) { throw new CancelledExecutionException(commandText); } return(default(int)); } commandText = (cancellableTraceLog.Statement ?? commandText); param = (cancellableTraceLog.Parameter ?? param); } // Before Execution Time var beforeExecutionTime = DateTime.UtcNow; // Actual Execution var result = ExecuteScalarInternal <object>(connection: connection, commandText: commandText, param: param, commandType: commandType, commandTimeout: commandTimeout, transaction: transaction, skipCommandArrayParametersCheck: true); // After Execution if (trace != null) { trace.AfterAverageAll(new TraceLog(sessionId, commandText, param, result, DateTime.UtcNow.Subtract(beforeExecutionTime))); } // Result return(result); }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <returns></returns> internal static string GetAverageAllText(AverageAllRequest request) { if (cache.TryGetValue(request, out var commandText) == false) { var statementBuilder = EnsureStatementBuilder(request.Connection, request.StatementBuilder); commandText = statementBuilder.CreateAverageAll(new QueryBuilder(), request.Name, request.Field, request.Hints); cache.TryAdd(request, commandText); } return(commandText); }