public RunningQueryViewModel(SqlQuery baseModel)
        {
            Id = baseModel.Id;
            Sql = baseModel.Sql;
            Databases = baseModel.Databases;
            UserId = baseModel.UserId;
            DateStart = baseModel.DateStart;
            DateEnd = baseModel.DateEnd;
            SingleTable = baseModel.SingleTable;

            Done = DateEnd.HasValue && DateEnd > DateStart;

            var runningTime = Done
                ? DateEnd.Value - DateStart
                : DateTime.Now - DateStart;

            RunningTime = runningTime.ToString(@"dd\.hh\:mm\:ss");

            Results = new List<WorkerResult>();
            using (var dbContext = new OctopusDbContext())
            {
                var tmpResults = dbContext
                    .QueryResults
                    .Where(qr => qr.QueryId == Id)
                    .ToList();
                foreach (var tmpResult in tmpResults)
                    Results.Add(JsonConvert.DeserializeObject<WorkerResult>(tmpResult.Result));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Execute query
        /// </summary>
        public static void ExecuteQuery(SqlQuery query)
        {
            var databaseIds = query
                .Databases
                .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(id => id
                    .Trim()
                    .ToLower())
                .ToList();

            bool isAllDatabases = databaseIds.Count == 1 && databaseIds[0] == "all";
            List<WorkerDb> executeInDatabases = (isAllDatabases)
                ? _databases.ToList()
                : new List<WorkerDb>();

            if (!isAllDatabases)
            {
                foreach (var id in databaseIds)
                {
                    var database = _databases
                        .Where(db => db.Id == id)
                        .FirstOrDefault();

                    if (database != null)
                        executeInDatabases.Add(database);
                }
            }

            QueryRunner runner = new QueryRunner(query, executeInDatabases);
            runner.ExecuteQuery();
        }
        public ActionResult Execute(UserQueryViewModel userQuery)
        {
            SqlQuery query = new SqlQuery();
            var preparedScript = PreparedScriptHelper.Get(userQuery.PreparedScriptId);
            if (preparedScript != null)
                query.PreparedScriptName = preparedScript.Name;
            query.Databases = userQuery.Databases;
            query.Sql = PreparedScriptHelper.SetParams(userQuery.Sql, userQuery.Params);

            query.UserId = UserHelper.GetUserId(User.Identity);
            query.DateStart = DateTime.Now;
            query.SingleTable = userQuery.SingleTable;
            _dbContext.Queries.Add(query);
            _dbContext.SaveChanges();

            Worker.ExecuteQuery(query);

            return Redirect(string.Format("~/Query/Execute/{0}", query.Id));
        }