/// <inheritdoc /> public async Task <Census> GetCensusAsync( IEnumerable <string> aggregationFields, DatasetQueryFile datasetQueryFile, Dictionary <string, AggregateQuery> aggregateQueries, string parameterName, string parameterValue, BuildCensusResultsCallback buildCensusResultsCallback, CancellationToken cancellationToken) { Census toReturn = null; if (datasetQueryFile == null) { throw new ArgumentNullException(nameof(datasetQueryFile)); } if (buildCensusResultsCallback == null) { throw new ArgumentNullException( nameof(buildCensusResultsCallback)); } QueryConfiguration queryConfiguration = datasetQueryFile.QueryConfiguration; string connectionString = this.GetConnectionString( queryConfiguration); string query = datasetQueryFile.Query; toReturn = await this.ExecuteQueryAsync( aggregationFields, aggregateQueries, connectionString, query, parameterName, parameterValue, buildCensusResultsCallback, cancellationToken) .ConfigureAwait(false); return(toReturn); }
private async Task <Census> ExecuteQueryAsync( IEnumerable <string> aggregationFields, Dictionary <string, AggregateQuery> aggregateQueries, string connectionString, string query, string parameterName, string parameterValue, BuildCensusResultsCallback buildCensusResultsCallback, CancellationToken cancellationToken) { Census toReturn = null; string[] requestedFields = aggregateQueries .SelectMany(x => x.Value.DataFilters) .Select(x => x.Field) .Distinct() .ToArray(); Stopwatch stopwatch = new Stopwatch(); using (SqlConnection sqlConnection = new SqlConnection(connectionString)) { this.loggerWrapper.Debug( $"Opening {nameof(SqlConnection)}..."); await sqlConnection.OpenAsync(cancellationToken) .ConfigureAwait(false); this.loggerWrapper.Info( $"Connection opened with success " + $"({nameof(sqlConnection.ClientConnectionId)}: " + $"{sqlConnection.ClientConnectionId})."); DbDataReader dbDataReader = null; using (SqlCommand sqlCommand = this.GetSqlCommand(sqlConnection, query, aggregationFields, requestedFields, parameterName, parameterValue)) { stopwatch.Start(); this.loggerWrapper.Debug( $"Executing query \"{query}\" with parameter name " + $"\"{parameterName}\", value \"{parameterValue}\"..."); dbDataReader = await sqlCommand.ExecuteReaderAsync(cancellationToken) .ConfigureAwait(false); stopwatch.Stop(); TimeSpan elapsed = stopwatch.Elapsed; this.loggerWrapper.Info( $"Query \"{query}\" with parameter name " + $"\"{parameterName}\", value \"{parameterValue}\" " + $"executed in {elapsed}."); this.loggerWrapper.Debug( $"Building results from {nameof(SqlDataReader)}..."); toReturn = buildCensusResultsCallback( aggregateQueries, dbDataReader); this.loggerWrapper.Info( $"{nameof(Census)} constructed: {toReturn}. " + $"Returning."); } } return(toReturn); }