/// <summary> /// Retrieves all tuples from the space matching the specified pattern. The operation is non-blocking. The operation will return an empty set if no elements match. /// </summary> public IEnumerable <ITuple> QueryAll(params object[] pattern) { QueryAllRequest request = new QueryAllRequest(this.GetSource(), this.GetSessionId(), this.connectionString.Target, pattern); QueryAllResponse response = this.GetMode()?.PerformRequest <QueryAllResponse>(request); return(response.Result == null ? null : response.Result.Select(x => this.tupleFactory.Create(x))); }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="fields"></param> /// <returns></returns> private static string GetQueryAllTextInternal(QueryAllRequest request, IEnumerable <Field> fields) { var statementBuilder = EnsureStatementBuilder(request.Connection, request.StatementBuilder); return(statementBuilder.CreateQueryAll(new QueryBuilder(), request.Name, fields, request.OrderBy, request.Hints)); }
private IMessage PerformQueryAll(IMessage request) { ISpace ts = this.repository.GetSpace(request.Target); if (ts != null) { QueryAllRequest getReq = (QueryAllRequest)request; IEnumerable <ITuple> tuples = ts.QueryAll(new Pattern(getReq.Template)); return(new QueryAllResponse(request.Source, request.Session, request.Target, tuples?.Select(x => x.Fields) ?? null, StatusCode.OK, StatusMessage.OK)); } return(new QueryAllResponse(request.Source, request.Session, request.Target, null, StatusCode.NOT_FOUND, StatusMessage.NOT_FOUND)); }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <returns></returns> internal static string GetQueryAllText(QueryAllRequest request) { if (cache.TryGetValue(request, out var commandText) == false) { var fields = GetActualFields(request.Connection, request.Name, request.Fields, request.Transaction); commandText = GetQueryAllTextInternal(request, fields); cache.TryAdd(request, commandText); } return(commandText); }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> internal static async Task <string> GetQueryAllTextAsync(QueryAllRequest request, CancellationToken cancellationToken = default) { if (cache.TryGetValue(request, out var commandText) == false) { var fields = await GetActualFieldsAsync(request.Connection, request.Name, request.Fields, request.Transaction, cancellationToken); commandText = GetQueryAllTextInternal(request, fields); cache.TryAdd(request, commandText); } return(commandText); }
/// <summary> /// Gets a command text from the cache for the query-all operation. /// </summary> /// <param name="request">The request object.</param> /// <returns>The cached command text.</returns> internal static string GetQueryAllText(QueryAllRequest request) { var commandText = (string)null; if (m_cache.TryGetValue(request, out commandText) == false) { var statementBuilder = EnsureStatementBuilder(request.Connection, request.StatementBuilder); var fields = GetActualFields(request.Connection, request.Name, request.Fields, request.Transaction); commandText = statementBuilder.CreateQueryAll(new QueryBuilder(), request.Name, fields, request.OrderBy, request.Hints); m_cache.TryAdd(request, commandText); } return(commandText); }
/// <summary> /// Query all the data from the database. /// </summary> /// <param name="connection">The connection object to be used.</param> /// <param name="tableName">The name of the target table.</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. See <see cref="SqlTableHints"/> class.</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 <dynamic> > QueryAllAsyncInternalBase(this IDbConnection connection, string tableName, 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) { // Get Cache if (cacheKey != null) { var item = cache?.Get(cacheKey, false); if (item != null) { return((IEnumerable <dynamic>)item.Value); } } // Check the fields if (fields?.Any() != true) { fields = DbFieldCache.Get(connection, tableName)?.Select(f => f.AsField()); } // Variables var commandType = CommandType.Text; var request = new QueryAllRequest(tableName, connection, fields, orderBy, hints, statementBuilder); var commandText = CommandTextCache.GetQueryAllText(request); var param = (object)null; // Before Execution if (trace != null) { var cancellableTraceLog = new CancellableTraceLog(commandText, param, null); trace.BeforeQueryAll(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(connection : connection, commandText : commandText, param : param, commandType : commandType, commandTimeout : commandTimeout, transaction : transaction, tableName : tableName, skipCommandArrayParametersCheck : true); // After Execution if (trace != null) { trace.AfterQueryAll(new TraceLog(commandText, param, result, DateTime.UtcNow.Subtract(beforeExecutionTime))); } // Set Cache if (cacheKey != null) { cache?.Add(cacheKey, result, cacheItemExpiration); } // Result return(result); }
/// <summary> /// Query all the data 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="orderBy">The order definition of the fields to be used.</param> /// <param name="hints">The table hints to be used. See <see cref="SqlTableHints"/> class.</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 Task <IEnumerable <TEntity> > QueryAllAsyncInternalBase <TEntity>(this IDbConnection connection, 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(Task.FromResult((IEnumerable <TEntity>)item.Value)); } } // Variables var commandType = CommandType.Text; var request = new QueryAllRequest(typeof(TEntity), connection, FieldCache.Get <TEntity>(), orderBy, hints, statementBuilder); var commandText = CommandTextCache.GetQueryAllText(request); var param = (object)null; // Database pre-touch for field definitions DbFieldCache.Get(connection, ClassMappedNameCache.Get <TEntity>()); // Before Execution if (trace != null) { var cancellableTraceLog = new CancellableTraceLog(commandText, param, null); trace.BeforeQueryAll(cancellableTraceLog); if (cancellableTraceLog.IsCancelled) { if (cancellableTraceLog.IsThrowException) { throw new CancelledExecutionException(commandText); } return(Task.FromResult <IEnumerable <TEntity> >(null)); } commandText = (cancellableTraceLog.Statement ?? commandText); param = (cancellableTraceLog.Parameter ?? param); } // Before Execution Time var beforeExecutionTime = DateTime.UtcNow; // Actual Execution var result = ExecuteQueryAsyncInternal <TEntity>(connection: connection, commandText: commandText, param: param, commandType: commandType, commandTimeout: commandTimeout, transaction: transaction, basedOnFields: false, skipCommandArrayParametersCheck: true); // After Execution if (trace != null) { trace.AfterQueryAll(new TraceLog(commandText, param, result, DateTime.UtcNow.Subtract(beforeExecutionTime))); } // Set Cache if (cacheKey != null /* && result.Result?.Any() == true */) { cache?.Add(cacheKey, result, cacheItemExpiration); } // Result return(result); }
/// <summary> /// Query all the data from the table. /// </summary> /// <typeparam name="TEntity">The type of the data entity.</typeparam> /// <param name="connection">The connection object to be used.</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 item.By setting this argument, it will return the item from the cache if present, otherwise it will query the database. /// This will only work if the 'cache' argument is set. /// </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 objects.</returns> internal static IEnumerable <TEntity> QueryAllInternalBase <TEntity>(this IDbConnection connection, 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 <IEnumerable <TEntity> >(cacheKey, false); if (item != null) { return(item.Value); } } // Variables var commandType = CommandType.Text; var request = new QueryAllRequest(typeof(TEntity), connection, transaction, FieldCache.Get <TEntity>(), orderBy, hints, statementBuilder); var commandText = CommandTextCache.GetQueryAllText(request); var param = (object)null; var sessionId = Guid.Empty; // Before Execution if (trace != null) { sessionId = Guid.NewGuid(); var cancellableTraceLog = new CancellableTraceLog(sessionId, commandText, param, null); trace.BeforeQueryAll(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 = ExecuteQueryInternal <TEntity>(connection: connection, commandText: commandText, param: param, commandType: commandType, commandTimeout: commandTimeout, transaction: transaction, skipCommandArrayParametersCheck: true); // After Execution if (trace != null) { trace.AfterQueryAll(new TraceLog(sessionId, commandText, param, result, DateTime.UtcNow.Subtract(beforeExecutionTime))); } // Set Cache if (cacheKey != null) { cache?.Add(cacheKey, result, cacheItemExpiration, false); } // Result return(result); }