Beispiel #1
0
        public Task <IList <IScreen> > RunQuery(DatabaseReference databaseReference, IQueryHistoryProvider queryHistoryProvider, string query)
        {
            var result = new List <IScreen>();

            if (string.IsNullOrWhiteSpace(query))
            {
                return(Task.FromResult <IList <IScreen> >(result));
            }

            var rawQueries = RemoveQueryComments(query)
                             .Split(new[] { "db.", "DB." }, StringSplitOptions.RemoveEmptyEntries)
                             .Select(q => $"db.{q.Trim()}")
                             .ToList();

            var resultCount = 0;

            foreach (var rawQuery in rawQueries)
            {
                resultCount++;

                try
                {
                    var resultViewModel = IoC.Get <QueryResultViewModel>();

                    IList <BsonValue> results;
                    using (resultViewModel.StartTime())
                    {
                        results = databaseReference.RunCommand(rawQuery);
                    }
                    resultViewModel.SetResult(
                        $"Result {resultCount}",
                        rawQuery,
                        new QueryResult(results, UserDefinedCultureFormat.Default));

                    result.Add(resultViewModel);
                }
                catch (Exception e)
                {
                    var title           = $"Query {resultCount} Error";
                    var exceptionScreen = new ExceptionScreenViewModel(title, $"Error on Query {resultCount}:\n'{rawQuery}'", e);
                    result.Add(exceptionScreen);
                }
            }

            var queryHistory = new RawQueryHistory
            {
                QueryHandlerName = Name,
                RawQuery         = query.Trim(),
                DatabaseLocation = databaseReference.Location,
                DatabaseVersion  = databaseReference.DatabaseVersion,
                CreatedAt        = DateTime.UtcNow,
                LastRunAt        = DateTime.UtcNow,
            };

            queryHistoryProvider.Upsert(queryHistory);

            return(Task.FromResult <IList <IScreen> >(result));
        }
Beispiel #2
0
        public void Upsert(RawQueryHistory item)
        {
            _historySourceList.Edit(list =>
            {
                var lastHistory = list
                                  .OrderByDescending(p => p.LastRunAt)
                                  .FirstOrDefault(p => RawQueryHistory.RawSourceComparer.Equals(item, p));

                if (lastHistory != null)
                {
                    lastHistory.LastRunAt = DateTime.UtcNow;
                }
                else
                {
                    list.Add(item);
                }
            });
        }
Beispiel #3
0
 public void Remove(RawQueryHistory item)
 {
     _historySourceList.Remove(item);
 }