Exemplo n.º 1
0
        public ActionResult Execute(int querySetId, int revisionId, int siteId, bool?textResults, bool?withExecutionPlan, TargetSites?targetSites)
        {
            if (CurrentUser.IsAnonymous && !CaptchaController.CaptchaPassed(GetRemoteIP()))
            {
                return(Json(new { captcha = true }));
            }

            ActionResult response = null;

            try
            {
                if (!ValidateTargetSites(targetSites))
                {
                    throw new ApplicationException("Invalid target sites selection");
                }

                QuerySet querySet = null;


                querySet = Current.DB.QuerySets.Get(querySetId);

                if (querySet == null)
                {
                    throw new ApplicationException("Invalid query set ID");
                }

                Revision revision = Current.DB.Revisions.Get(revisionId);
                if (revision == null)
                {
                    throw new ApplicationException("Invalid revision ID");
                }

                Query query = Current.DB.Queries.Get(revision.QueryId);

                var parsedQuery = new ParsedQuery(
                    query.QueryBody,
                    Request.Params,
                    withExecutionPlan == true,
                    targetSites ?? TargetSites.Current
                    );

                QueryResults results = null;
                Site         site    = GetSite(siteId);
                ValidateQuery(parsedQuery, site);

                var contextData = new QueryContextData
                {
                    IsText   = textResults == true,
                    QuerySet = querySet,
                    Revision = revision
                };

                var asyncResults = AsyncQueryRunner.Execute(parsedQuery, CurrentUser, site, contextData);

                if (asyncResults.State == AsyncQueryRunner.AsyncState.Failure)
                {
                    throw asyncResults.Exception;
                }

                if (asyncResults.State == AsyncQueryRunner.AsyncState.Success)
                {
                    results = asyncResults.QueryResults;
                }
                else
                {
                    return(Json(new { running = true, job_id = asyncResults.JobId }));
                }

                response = CompleteResponse(results, parsedQuery, contextData, siteId);
            }
            catch (Exception ex)
            {
                response = TransformExecutionException(ex);
            }

            return(response);
        }
Exemplo n.º 2
0
        public ActionResult Save(string sql, string title, string description, int siteId, int?querySetId, bool?textResults, bool?withExecutionPlan, TargetSites?targetSites)
        {
            if (CurrentUser.IsAnonymous && !CaptchaController.CaptchaPassed(GetRemoteIP()))
            {
                return(Json(new { captcha = true }));
            }

            ActionResult response = null;

            try
            {
                if (!ValidateTargetSites(targetSites))
                {
                    throw new ApplicationException("Invalid target sites selection");
                }

                QuerySet querySet = null;

                if (querySetId.HasValue)
                {
                    querySet = Current.DB.QuerySets.Get(querySetId.Value);

                    if (querySet == null)
                    {
                        throw new ApplicationException("Invalid query set ID");
                    }
                }

                var parsedQuery = new ParsedQuery(
                    sql,
                    Request.Params,
                    withExecutionPlan == true,
                    targetSites ?? TargetSites.Current
                    );

                QueryResults results = null;
                Site         site    = GetSite(siteId);
                ValidateQuery(parsedQuery, site);

                if (title.HasValue() && title.Length > 100)
                {
                    throw new ApplicationException("Title must be no more than 100 characters");
                }

                if (description.HasValue() && description.Length > 1000)
                {
                    throw new ApplicationException("Description must be no more than 1000 characters");
                }

                var contextData = new QueryContextData
                {
                    Title       = title,
                    Description = description,
                    IsText      = textResults == true,
                    QuerySet    = querySet
                };

                var asyncResults = AsyncQueryRunner.Execute(parsedQuery, CurrentUser, site, contextData);

                if (asyncResults.State == AsyncQueryRunner.AsyncState.Failure)
                {
                    throw asyncResults.Exception;
                }

                if (asyncResults.State == AsyncQueryRunner.AsyncState.Success || asyncResults.State == AsyncQueryRunner.AsyncState.Cancelled)
                {
                    results = asyncResults.QueryResults;
                }
                else
                {
                    return(Json(new { running = true, job_id = asyncResults.JobId }));
                }

                response = CompleteResponse(results, parsedQuery, contextData, siteId);
            }
            catch (Exception ex)
            {
                response = TransformExecutionException(ex);
            }

            return(response);
        }
Exemplo n.º 3
0
        public async Task <ActionResult> Execute(int querySetId, int revisionId, int siteId, bool?textResults, bool?withExecutionPlan, bool?bypassCache, TargetSites?targetSites)
        {
            if (!(await Captcha.ChallengeIsvalidAsync(Request.Form)))
            {
                return(Json(new { captcha = true }));
            }

            ActionResult response;

            try
            {
                if (!ValidateTargetSites(targetSites))
                {
                    throw new ApplicationException("Invalid target sites selection");
                }

                var querySet = Current.DB.QuerySets.Get(querySetId);
                if (querySet == null)
                {
                    throw new ApplicationException("Invalid query set ID");
                }

                var revision = Current.DB.Revisions.Get(revisionId);
                if (revision == null)
                {
                    throw new ApplicationException("Invalid revision ID");
                }

                var query       = Current.DB.Queries.Get(revision.QueryId);
                var parsedQuery = new ParsedQuery(
                    query.QueryBody,
                    Request.Params,
                    withExecutionPlan == true,
                    targetSites ?? TargetSites.Current
                    );

                if (AppSettings.EnableBypassCache && bypassCache.HasValue && bypassCache.Value)
                {
                    QueryUtil.ClearCachedResults(parsedQuery, siteId);
                }

                var site = GetSite(siteId);
                ValidateQuery(parsedQuery, site);

                var contextData = new QueryContextData
                {
                    IsText   = textResults == true,
                    QuerySet = querySet,
                    Revision = revision
                };

                var asyncResults = AsyncQueryRunner.Execute(parsedQuery, CurrentUser, site, contextData);
                if (asyncResults.State == AsyncQueryRunner.AsyncState.Failure)
                {
                    throw asyncResults.Exception;
                }

                QueryResults results;
                if (asyncResults.State == AsyncQueryRunner.AsyncState.Success)
                {
                    results = asyncResults.QueryResults;
                }
                else
                {
                    return(Json(new { running = true, job_id = asyncResults.JobId }));
                }

                response = CompleteResponse(results, parsedQuery, contextData, siteId);
            }
            catch (Exception ex)
            {
                response = TransformExecutionException(ex);
            }

            return(response);
        }