public async Task <IQueryResults> ExecuteQueryAsync(Query query, IDictionary <string, object> parameters)
        {
            var contentItemQuery = query as Services.ContentItemQuery;
            var sqlQueryResults  = new SQLQueryResults();

            // var templateContext = new TemplateContext();
            //templateContext.SetValue("Query", contentItemQuery);
            // await templateContext.ContextualizeAsync(_serviceProvider);
            // if (parameters != null)
            // {
            //     foreach (var parameter in parameters)
            //     {
            //         templateContext.SetValue(parameter.Key, parameter.Value);
            //     }
            // }


            var queryResults = await _contentItemService.GetResults(contentItemQuery);

            //  var results = new List<JObject>();
            var results = new List <ContentItem>();

            foreach (var contentItem in queryResults)
            {
                results.Add(contentItem);
                // results.Add(JObject.FromObject(document));
            }

            sqlQueryResults.Items = results;
            return(sqlQueryResults);
        }
Example #2
0
        public async Task <IActionResult> ContentItemQuery(AdminQueryViewModel model)
        {
//            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageSqlQueries))
//            {
//                return Unauthorized();
//            }

//            if (String.IsNullOrWhiteSpace(model.Query))
//            {
//                return View(model);
//            }

            if (String.IsNullOrWhiteSpace(model.DecodedQuery))
            {
                return(View(model));
            }
            if (String.IsNullOrEmpty(model.Parameters))
            {
                model.Parameters = "{ }";
            }

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            // var connection = _store.Configuration.ConnectionFactory.CreateConnection();
            //  var dialect = SqlDialectFactory.For(connection);

            //load original json doc
            var querySource = ((Queries.Services.ContentItemQuery)(await _queryManager.GetQueryAsync(model.DecodedQuery)));

            var parameters = JsonConvert.DeserializeObject <Dictionary <string, object> >(model.Parameters);

            var templateContext = _liquidTemplateManager.Context;

            // await templateContext.ContextualizeAsync( _serviceProvider);
            foreach (var parameter in parameters)
            {
                templateContext.SetValue(parameter.Key, parameter.Value);
            }



            var tokenizedQuery = await _liquidTemplateManager.RenderAsync(model.DecodedQuery, NullEncoder.Default, templateContext);

            model.FactoryName = _store.Configuration.ConnectionFactory.GetType().FullName;

            //  if (SqlParser.TryParse(tokenizedQuery, dialect, _store.Configuration.TablePrefix, parameters, out var rawQuery, out var messages))
            //   {
            // model.RawSql = rawQuery;
            model.Parameters = JsonConvert.SerializeObject(parameters, Formatting.Indented);

            try
            {
//                    using (connection)
//                    {
//                        connection.Open();
//                        model.Documents = await connection.QueryAsync(rawQuery, parameters);
//                    }

                model.ContentItems = await _contentItemService.GetResults(querySource);
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", _stringLocalizer["An error occured while executing the Content Items query: {0}", e.Message]);
            }



            model.Elapsed = stopwatch.Elapsed;

            return(View(model));
        }