Beispiel #1
0
        /// <summary>
        /// Queries a data from the database table function in an asynchronous way.
        /// </summary>
        /// <typeparam name="TEntity">The type of the data entity object.</typeparam>
        /// <param name="connection">The connection object to be used.</param>
        /// <param name="funcName">Function name.</param>
        /// <param name="parameters">Collection of function parameters.</param>
        /// <param name="fields">The list of fields to be queried.</param>
        /// <param name="orderBy">The order definition of the fields to be used.</param>
        /// <param name="hints">The table hints to be used.</param>
        /// <param name="cacheKey">
        /// The key to the cache. If the cache key is present in the cache, then the item from the cache will be returned instead. Setting this
        /// to null would force to query from the database.
        /// </param>
        /// <param name="cacheItemExpiration">The expiration in minutes of the cache item.</param>
        /// <param name="commandTimeout">The command timeout in seconds to be used.</param>
        /// <param name="transaction">The transaction to be used.</param>
        /// <param name="cache">The cache object 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>An enumerable list of data entity object.</returns>
        internal static async Task <IEnumerable <TEntity> > QueryAllTableFuncAsyncInternalBase <TEntity>(this IDbConnection connection,
                                                                                                         string funcName,
                                                                                                         IEnumerable <QueryField> parameters = null,
                                                                                                         IEnumerable <Field> fields          = null,
                                                                                                         IEnumerable <OrderField> orderBy    = null,
                                                                                                         string hints                       = null,
                                                                                                         string cacheKey                    = null,
                                                                                                         int cacheItemExpiration            = Constant.DefaultCacheItemExpirationInMinutes,
                                                                                                         int?commandTimeout                 = null,
                                                                                                         IDbTransaction transaction         = null,
                                                                                                         ICache cache                       = null,
                                                                                                         ITrace trace                       = null,
                                                                                                         IStatementBuilder statementBuilder = null)
            where TEntity : class
        {
            // Get Cache
            if (cacheKey != null)
            {
                var item = cache?.Get(cacheKey, false);
                if (item != null)
                {
                    return((IEnumerable <TEntity>)item.Value);
                }
            }

            // Variables
            var commandType = CommandType.Text;
            var request     = new QueryAllTableFuncRequest(typeof(TEntity),
                                                           funcName,
                                                           connection,
                                                           transaction,
                                                           parameters?.GetFields(),
                                                           fields,
                                                           orderBy,
                                                           hints,
                                                           statementBuilder);
            var commandText = CommandTextCache.GetQueryAllTableFuncText(request);
            var param       = (object)null;

            if (parameters != null)
            {
                param = parameters.AsObject();
            }

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

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

            // Actual Execution
            var result = await ExecuteQueryAsyncInternal <TEntity>(connection : connection,
                                                                   commandText : commandText,
                                                                   param : param,
                                                                   commandType : commandType,
                                                                   commandTimeout : commandTimeout,
                                                                   transaction : transaction,
                                                                   skipCommandArrayParametersCheck : true);

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

            // Set Cache
            if (cacheKey != null)
            {
                cache?.Add(cacheKey, result, cacheItemExpiration, false);
            }

            // Result
            return(result);
        }