/// <summary> /// Executes the specified command and returns items of a specific type. /// </summary> /// <typeparam name="TObject">The type of the object.</typeparam> /// <param name="queries">The queries to run.</param> /// <returns>The resulting data</returns> public async Task <IEnumerable <dynamic> > ExecuteAsync <TObject>(IDictionary <IMappingSource, QueryData <TObject> > queries) where TObject : class { var KeyName = queries.Values.ToString(x => x + "_" + x.Source.Source.Name, "\n"); (queries?.Values ?.SelectMany(x => x.Parameters) ?.Distinct() ?? Array.Empty <IParameter>()) ?.ForEach(x => KeyName = x.AddParameter(KeyName)); if (QueryResults.IsCached(KeyName, Cache)) { return(QueryResults.GetCached(KeyName, Cache)?.SelectMany(x => x.ConvertValues <TObject>()) ?? Array.Empty <TObject>()); } var Results = new List <QueryResults>(); var FirstRun = true; var TempQueries = queries.Where(x => x.Value.Source.CanRead && x.Value.Source.GetChildMappings(typeof(TObject)).Any()); foreach (var Source in TempQueries.Where(x => x.Value.WhereClause.InternalOperator != null) .OrderBy(x => x.Key.Order)) { await GenerateQueryAsync(Results, FirstRun, Source).ConfigureAwait(false); FirstRun = false; } foreach (var Source in TempQueries.Where(x => x.Value.WhereClause.InternalOperator is null) .OrderBy(x => x.Key.Order)) { await GenerateQueryAsync(Results, FirstRun, Source).ConfigureAwait(false); FirstRun = false; } QueryResults.CacheValues(KeyName, Results, Cache); return(Results?.SelectMany(x => x.ConvertValues <TObject>())?.ToArray() ?? Array.Empty <TObject>()); }
/// <summary> /// Executes the specified command and returns items of a specific type. /// </summary> /// <typeparam name="TObject">The type of the object.</typeparam> /// <param name="command">The command.</param> /// <param name="type">The type.</param> /// <param name="connection">The connection name.</param> /// <param name="parameters">The parameters.</param> /// <returns>The list of objects</returns> /// <exception cref="ArgumentException"></exception> public async Task <IEnumerable <TObject> > ExecuteAsync <TObject>(string command, CommandType type, string connection, params object[] parameters) where TObject : class { parameters ??= Array.Empty <IParameter>(); var Parameters = ConvertParameters(parameters); var KeyName = command + "_" + connection; Parameters.ForEach(x => KeyName = x.AddParameter(KeyName)); if (QueryResults.IsCached(KeyName, Cache)) { return(QueryResults.GetCached(KeyName, Cache).SelectMany(x => x.ConvertValues <TObject>())); } var Source = MappingManager.ReadSources.FirstOrDefault(x => x.Source.Name == connection); if (Source is null) { throw new ArgumentException($"Source not found {connection}"); } var IDProperties = Source.GetParentMapping(typeof(TObject)).SelectMany(x => x.IDProperties); var ReturnValue = new List <Dynamo>(); var Batch = QueryProviderManager.CreateBatch(Source.Source, DynamoFactory); Batch.AddQuery(type, command, Parameters.ToArray()); var ObjectType = Source.GetChildMappings(typeof(TObject)).First().ObjectType; try { var Results = (await Batch.ExecuteAsync().ConfigureAwait(false)).Select(x => new QueryResults(new Query(ObjectType, CommandType.Text, command, QueryType.LinqQuery, Parameters.ToArray()), x.Cast <Dynamo>(), this)) .ToList(); QueryResults.CacheValues(KeyName, Results, Cache); return(Results.SelectMany(x => x.ConvertValues <TObject>()).ToArray()); } catch { Logger.Debug("Failed on query: " + Batch); throw; } }