Ejemplo n.º 1
0
 /// <summary>
 /// Executes all registred requests plus the specified one query,
 /// returning <see cref="IEnumerator{Tuple}"/> for the last query.
 /// </summary>
 /// <param name="request">The request to execute.</param>
 /// <returns>A <see cref="IEnumerator{Tuple}"/> for the specified request.</returns>
 public abstract IEnumerator <Tuple> ExecuteTasksWithReader(QueryRequest request, CommandProcessorContext context);
Ejemplo n.º 2
0
 /// <summary>
 /// Asynchronously executes all registred requests plus the specified one query.
 /// Default implementation is synchronous and returns complete task.
 /// </summary>
 /// <param name="request">The request to execute.</param>
 /// <param name="context">The context in which the requests are executed.</param>
 /// <param name="token">Token to cancel operation.</param>
 /// <returns>A task performing this operation.</returns>
 public virtual Task <IEnumerator <Tuple> > ExecuteTasksWithReaderAsync(QueryRequest request,
                                                                        CommandProcessorContext context, CancellationToken token)
 {
     token.ThrowIfCancellationRequested();
     return(Task.FromResult(ExecuteTasksWithReader(request, context)));
 }
Ejemplo n.º 3
0
 private void RegisterQueryTask(QueryTask task, QueryRequest request)
 {
     task.Result = new List <Tuple>();
     commandProcessor.RegisterTask(new SqlLoadTask(request, task.Result, task.ParameterContext));
 }
Ejemplo n.º 4
0
        private async Task <Command> ExecuteBatchAsync(int numberOfTasks, QueryRequest lastRequest,
                                                       CommandProcessorContext context, CancellationToken token)
        {
            if (numberOfTasks == 0 && lastRequest == null)
            {
                return(null);
            }

            var tasksToProcess = context.ProcessingTasks;

            AllocateCommand(context);

            var shouldReturnReader = false;

            try {
                while (numberOfTasks > 0 && tasksToProcess.Count > 0)
                {
                    var task = tasksToProcess.Peek();
                    context.CurrentTask = task;
                    task.ProcessWith(this, context);
                    if (context.CurrentTask == null)
                    {
                        numberOfTasks--;
                        _ = tasksToProcess.Dequeue();
                    }
                    else
                    {
                        break;
                    }
                }

                var command = context.ActiveCommand;
                if (lastRequest != null && tasksToProcess.Count == 0)
                {
                    var part = Factory.CreateQueryPart(lastRequest, context.ParameterContext);
                    if (!PendCommandPart(command, part))
                    {
                        shouldReturnReader = true;
                        command.AddPart(part);
                    }
                }

                if (command.Count == 0)
                {
                    return(null);
                }
                var hasQueryTasks = context.ActiveTasks.Count > 0;
                if (!hasQueryTasks && !shouldReturnReader)
                {
                    _ = await command.ExecuteNonQueryAsync(token).ConfigureAwait(false);

                    return(null);
                }

                await command.ExecuteReaderAsync(token).ConfigureAwait(false);

                if (hasQueryTasks)
                {
                    var currentQueryTask = 0;
                    while (currentQueryTask < context.ActiveTasks.Count)
                    {
                        var queryTask = context.ActiveTasks[currentQueryTask];
                        var accessor  = queryTask.Request.GetAccessor();
                        var result    = queryTask.Output;
                        while (command.NextRow())
                        {
                            result.Add(command.ReadTupleWith(accessor));
                        }
                        _ = command.NextResult();
                        currentQueryTask++;
                    }
                }

                return(shouldReturnReader ? command : null);
            }
            finally {
                if (!shouldReturnReader)
                {
                    await context.ActiveCommand.DisposeAsync().ConfigureAwait(false);
                }

                ReleaseCommand(context);
            }
        }
 /// <summary>
 /// Executes all registered requests plus the specified one query,
 /// returning <see cref="IEnumerator{Tuple}"/> for the last query.
 /// </summary>
 /// <param name="request">A <see cref="QueryRequest"/> instance to be executed.</param>
 /// <param name="context">A contextual information to be used while executing
 /// the specified <paramref name="request"/>.</param>
 /// <returns>A <see cref="IEnumerator{Tuple}"/> for the specified request.</returns>
 public abstract DataReader ExecuteTasksWithReader(QueryRequest request, CommandProcessorContext context);
Ejemplo n.º 6
0
        public override async Task <IEnumerator <Tuple> > ExecuteTasksWithReaderAsync(QueryRequest request, CommandProcessorContext context, CancellationToken token)
        {
            context.ProcessingTasks = new Queue <SqlTask>(tasks);
            tasks.Clear();

            while (context.ProcessingTasks.Count >= batchSize)
            {
                await ExecuteBatchAsync(batchSize, null, context, token).ConfigureAwait(false);
            }

            return((await ExecuteBatchAsync(context.ProcessingTasks.Count, request, context, token).ConfigureAwait(false)).AsReaderOf(request));
        }
Ejemplo n.º 7
0
        private Command ExecuteBatch(int numberOfTasks, QueryRequest lastRequest, CommandProcessorContext context)
        {
            var shouldReturnReader = lastRequest != null;

            if (numberOfTasks == 0 && !shouldReturnReader)
            {
                return(null);
            }

            var tasksToProcess = context.ProcessingTasks;

            AllocateCommand(context);

            try {
                while (numberOfTasks > 0 && tasksToProcess.Count > 0)
                {
                    numberOfTasks--;
                    var task = tasksToProcess.Dequeue();
                    task.ProcessWith(this, context);
                }
                if (shouldReturnReader)
                {
                    var part = Factory.CreateQueryPart(lastRequest);
                    context.ActiveCommand.AddPart(part);
                }
                if (context.ActiveCommand.Count == 0)
                {
                    return(null);
                }
                var hasQueryTasks = context.ActiveTasks.Count > 0;
                if (!hasQueryTasks && !shouldReturnReader)
                {
                    context.ActiveCommand.ExecuteNonQuery();
                    return(null);
                }
                context.ActiveCommand.ExecuteReader();
                if (hasQueryTasks)
                {
                    int currentQueryTask = 0;
                    while (currentQueryTask < context.ActiveTasks.Count)
                    {
                        var queryTask = context.ActiveTasks[currentQueryTask];
                        var accessor  = queryTask.Request.GetAccessor();
                        var result    = queryTask.Output;
                        while (context.ActiveCommand.NextRow())
                        {
                            result.Add(context.ActiveCommand.ReadTupleWith(accessor));
                        }
                        context.ActiveCommand.NextResult();
                        currentQueryTask++;
                    }
                }
                return(shouldReturnReader ? context.ActiveCommand : null);
            }
            finally {
                if (!shouldReturnReader)
                {
                    context.ActiveCommand.Dispose();
                }
                ReleaseCommand(context);
            }
        }
 async Task <IEnumerator <Tuple> > IProviderExecutor.ExecuteTupleReaderAsync(QueryRequest request, CancellationToken token)
 {
     Prepare();
     using (var context = new CommandProcessorContext())
         return(await commandProcessor.ExecuteTasksWithReaderAsync(request, context, token).ConfigureAwait(false));
 }
        public override async Task <IEnumerator <Tuple> > ExecuteTasksWithReaderAsync(QueryRequest lastRequest, CommandProcessorContext context, CancellationToken token)
        {
            var oldValue = context.AllowPartialExecution;

            context.AllowPartialExecution = false;

            token.ThrowIfCancellationRequested();

            await ExecuteTasksAsync(context, token);

            context.AllowPartialExecution = oldValue;

            var lastRequestCommand = Factory.CreateCommand();
            var commandPart        = Factory.CreateQueryPart(lastRequest);

            lastRequestCommand.AddPart(commandPart);
            token.ThrowIfCancellationRequested();
            await lastRequestCommand.ExecuteReaderAsync(token);

            return(lastRequestCommand.AsReaderOf(lastRequest));
        }