public async Task RunAsync()
        {
            _source = new CancellationTokenSource();
            _view.ResetResultsView();
            ResetQueryOutput();

            //execute th interpretor and run against cosmos and connection
            if (SelectedConnection is Connection && SelectedConnection != null)
            {
                _variables.Clear();
                _view.SetStatusBarMessage("Executing Query...", true);

                var documentStore = _clientConnectionManager.CreateDocumentClientAndStore(SelectedConnection);
                //get each query and run it aggregating the results
                var queries = _queryManager.ConveryQueryTextToQueryParts(_view.Query);

                //check all the queries for deletes without transactions
                if (queries.Any(query => query.CleanQueryType == Constants.QueryParsingKeywords.DELETE && !query.IsTransaction) && _view.ShowMessage("Are you sure you want to delete documents without a transaction. This can not be undone?", "Delete Document Confirmation", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                {
                    return;
                }

                var hasResults = false;
                var hasError   = false;
                for (var i = 0; i < queries.Length; i++)
                {
                    if (_source.Token.IsCancellationRequested)
                    {
                        _logger.LogError($"Query has been requested to cancel.");
                        break;
                    }
                    var queryParts = queries[i];
                    var runner     = _queryRunners.FirstOrDefault(f => f.CanRun(queryParts));
                    if (runner != null)
                    {
                        if (queries.Length > 1)
                        {
                            AddToQueryOutput(new string('-', 300));
                            AddToQueryOutput($"Query statement {i + 1}");
                            AddToQueryOutput(new string('-', 300));
                        }

                        var response = await runner.RunAsync(documentStore, SelectedConnection, queryParts, true, _logger, _source.Token, _variables);

                        if (!response.success)
                        {
                            _view.ShowMessage($"Unable to execute query: {queryParts.CleanOrginalQuery.TruncateTo(500)}. Verify query and try again.", "Query Execution Error");
                            //on error stop loop and return
                            hasError = true;
                            break;
                        }
                        else if (response.results != null)
                        {
                            //add a header row if more then 1 query needs to be ran
                            await RenderResults(response.results, queryParts.CollectionName, queryParts, queries.Length > 1, i + 1);

                            hasResults = true;
                        }
                    }
                    else
                    {
                        //if we have comments then we can assume the whole query is a comment so skip and goto next
                        if (!queryParts.IsCommentOnly)
                        {
                            _logger.LogError($"Unable to find a query processor for query type. query: {queryParts.CleanOrginalQuery}");
                            //on error stop loop and return
                            hasError = true;
                            break;
                        }
                    }
                }

                if (!hasResults || hasError)
                {
                    ShowOutputTab();
                }
                _view.SetStatusBarMessage("Ready");
            }
            else
            {
                _view.ShowMessage("Invalid connection. Please select a valid connection and try again", "Data Connection Error");
            }
        }
        public async Task <int> RunAsync(string queryGroupName, string query, CancellationToken cancelToken)
        {
            _outputTraceInformation = new OutputTraceInformation();

            //execute th interpretor and run against cosmos and connection
            if (SelectedConnection is Connection && SelectedConnection != null)
            {
                _variables.Clear();

                var documentStore = _clientConnectionManager.CreateDocumentClientAndStore(SelectedConnection);
                //get each query and run it aggregating the results
                var queries = _queryManager.ConveryQueryTextToQueryParts(query);

                var hasNonTransactionDelete = queries.Any(q => q.CleanQueryType == Constants.QueryParsingKeywords.DELETE && !q.IsTransaction);
                //check all the queries for deletes without transactions
                if (hasNonTransactionDelete && !_options.IgnorePrompts)
                {
                    _console.WriteLine("Are you sure you want to delete documents without a transaction. This can not be undone? (Y/N): ");
                    if (_console.ReadLine() == "N")
                    {
                        return(-99);
                    }
                }

                var hasResults = false;
                var hasError   = false;
                //_source = new CancellationTokenSource();
                for (var i = 0; i < queries.Length; i++)
                {
                    var queryParts = queries[i];
                    var runner     = _queryRunners.FirstOrDefault(f => f.CanRun(queryParts));
                    if (runner != null)
                    {
                        var queryIndex = i + 1;
                        if (queries.Length > 1)
                        {
                            WriteHeader('/', 250, $"{queryGroupName} - Query statement {queryIndex}");
                        }

                        var response = await runner.RunAsync(documentStore, SelectedConnection, queryParts, true, _logger, cancelToken, _variables);

                        var querySummaryRecord = new OutputSummaryRecord
                        {
                            CollectionName      = queryParts.CollectionName,
                            Query               = queryParts.ToRawQuery(),
                            QueryStatementIndex = queryIndex
                        };
                        _outputTraceInformation.OutputSummaryRecords.Add(querySummaryRecord);
                        if (!response.success)
                        {
                            //on error stop loop and return
                            querySummaryRecord.HasError = hasError = true;
                            if (!_options.ContinueOnError)
                            {
                                break;
                            }
                        }
                        else if (response.results != null)
                        {
                            querySummaryRecord.ResultCount = response.results.Count;
                            //add a header row if more then 1 query needs to be ran
                            await RenderResults(response.results, queryParts.CollectionName, queryParts, queries.Length > 1, queryIndex);

                            hasResults = true;
                        }
                    }
                    else
                    {
                        //if we have comments then we can assume the whole query is a comment so skip and goto next
                        if (!queryParts.IsCommentOnly)
                        {
                            _logger.LogError($"Unable to find a query processor for query type. query: {queryParts.CleanOrginalQuery}");
                            //on error stop loop and return
                            hasError = true;
                            if (!_options.ContinueOnError)
                            {
                                break;
                            }
                        }
                    }
                }

                await WriteResults();

                if (hasError && !_options.ContinueOnError)
                {
                    //make sure process exits with non zero
                    return(-999);
                }
            }
            else
            {
                _logger.LogError("Invalid connection. Please select a valid connection and try again", "Data Connection Error");
                return(-99);
            }
            return(0);//success
        }