private void RefreshQueries(
			TreeTask                       treeTask,
			ConcreteTemplateNodeDefinition nodeDefinition,
			ProgressItem                   progress
		)
		{
			log.DebugFormat("taskInfo.Connection:'{0}';nodeDefinition:'{1}'",
				treeTask.Info.Connection.ConnectionGroup.ToString() ?? "?",
				nodeDefinition.TemplateNode.Name ?? "?"
			);

			ErrorLog          errorLog      = new ErrorLog();
			MsSqlAuditorModel model         = this._treeControl._model;
			DateTime          startTime     = DateTime.Now;
			Stopwatch         durationWatch = new Stopwatch();

			durationWatch.Start();

			using (SqlProcessor sqlProcessor = model.GetNewSqlProcessor(treeTask.CancellationSource.Token))
			{
				if (!treeTask.Info.Connection.IsLiveConnection)
				{
					sqlProcessor.SetSkipMSSQLQueries();
				}

				progress.SetPromisedChildCount(1);

				TemplateNodeInfo     templateNode = nodeDefinition.TemplateNode;
				ConnectionGroupInfo  group        = nodeDefinition.Connection;
				MultyQueryResultInfo result;

				if (nodeDefinition.Group.Instance != null)
				{
					result = sqlProcessor.ExecuteMultyQuery(
						nodeDefinition.Group,
						templateNode.Queries,
						progress.GetChild()
					);
				}
				else
				{
					result = sqlProcessor.ExecuteMultyQuery(
						group,
						templateNode.Queries,
						progress.GetChild(),
						model.Settings.SystemSettings.MaximumDBRequestsThreadCount
					);
				}

				if (group != null && group.Connections != null)
				{
					group.Connections.ForEach(
						x => x.ConnectionGroup = x.ConnectionGroup ?? group
					);
				}

				errorLog.AppendErrorLog(result);

				IStorageManager storage = model.GetVaultProcessor(group);

				storage.SaveRequestedData(templateNode, result);

				durationWatch.Stop();

				DateTime duration = new DateTime(durationWatch.Elapsed.Ticks);

				storage.CurrentStorage.UpdateTreeNodeTimings(
					templateNode,
					startTime,
					duration
				);

				foreach (GroupDefinition database in result.ExtractDatabases())
				{
					ConcreteTemplateNodeDefinition nodeDef = new ConcreteTemplateNodeDefinition(
						templateNode,
						database,
						group
					);

					this._treeControl.VisualizeData(nodeDef);
				}
			}
		}
		private Dictionary<TemplateNodeQueryInfo, DataTable> ReadHistTable(
			ConnectionGroupInfo            connectionGroup,
			ConcreteTemplateNodeDefinition concreteTemplateNode
		)
		{
			Dictionary<TemplateNodeQueryInfo, DataTable> histTable        = new Dictionary<TemplateNodeQueryInfo, DataTable>();
			TemplateNodeInfo                             templateNodeInfo = concreteTemplateNode.TemplateNode;
			ProgressItem                                 progress         = new ProgressItem();

			using (SqlProcessor sqlProcessor = MsSqlAuditor.GetNewSqlProcessor(new CancellationToken()))
			{
				progress.SetPromisedChildCount(1);

				MultyQueryResultInfo resultQuery = sqlProcessor.ExecuteMultyQuery(
					connectionGroup,
					templateNodeInfo.Queries,
					progress.GetChild(),
					Program.Model.Settings.SystemSettings.MaximumDBRequestsThreadCount,
					true
				);

				if (resultQuery != null)
				{
					if (resultQuery.List != null)
					{
						if (resultQuery.List.Count != 0)
						{
							long     requestId = this.Storage.MetaResultTable.GetMaxRequestId() + 1L;
							DateTime timestamp = DateTime.Now;

							histTable = PrepareHistoryData(resultQuery);

							this.Storage.SaveMeta(
								templateNodeInfo,
								resultQuery,
								requestId,
								timestamp
							);
						}
					}
				}
			}

			return histTable;
		}
Esempio n. 3
0
		private QueryResultInfo ExecuteQuery(
			ConnectionGroupInfo   connectionGroupInfo,
			List<QueryInfo>       queries,
			QueryExecutionParams  parameters,
			int                   maxthreadCount,
			ProgressItem          progress = null
		)
		{
			var                result      = new QueryResultInfo();
			List<InstanceInfo> connections = connectionGroupInfo.Connections;

			if (progress != null)
			{
				progress.SetPromisedChildCount(connections.Count);
			}

			ParallelOptions op = new ParallelOptions { MaxDegreeOfParallelism = (maxthreadCount == 0 ? Int32.MaxValue : maxthreadCount) };

			Parallel.ForEach(connections, op, (connection) =>
			{
				var query = queries.FirstOrDefault(x => x.Source == (connection.Type) || x.Source == QuerySource.SQLite);

				if (query != null)
				{
					ProgressItem subProgress = null;

					if (progress != null)
					{
						subProgress = progress.GetChild();
					}

					result.AddInstanceResult(ExecuteQuery(connection, query, parameters, subProgress));
				}
			});

			return result;
		}
Esempio n. 4
0
		/// <summary>
		/// Executes multyquery
		/// </summary>
		/// <param name="connectionGroup">Connection group</param>
		/// <param name="templateNodeQueryInfos">Template nodes</param>
		/// <param name="progress">Progress item</param>
		/// <param name="maxthreadCount">Maximum thread</param>
		/// <returns>Multyquery result</returns>
		public MultyQueryResultInfo ExecuteMultyQuery(
			ConnectionGroupInfo         connectionGroup,
			List<TemplateNodeQueryInfo> templateNodeQueryInfos,
			ProgressItem                progress,
			int                         maxthreadCount,
			bool                        checkHist = false
		)
		{
			Settings.InstanceTemplate settings = null;

			////////////////////////////////////////////////////////////////////////////////////////
			// string strLogMessage = "DEBUG:MSSQLServerAuditor.Model.ExecuteMultyQuery(1)";
			// strLogMessage = strLogMessage + ";GD:" + connectionGroup.ToString();
			// log.Debug(strLogMessage);
			////////////////////////////////////////////////////////////////////////////////////////

			var result = new MultyQueryResultInfo();

			if (templateNodeQueryInfos.Count > 0)
			{
				progress.SetPromisedChildCount(templateNodeQueryInfos.Count);

				foreach (var qi in templateNodeQueryInfos)
				{
					TemplateNodeQueryInfo queryInfo   = qi;
					QueryResultInfo       queryResult = null;

					try
					{
						var queries = this._model.GetQueryByTemplateNodeQueryInfo(queryInfo);

						if (this._skipMsSqlQueries)
						{
							queries.RemoveAll(
								x => (
									x.Source == QuerySource.MSSQL
									|| x.Source == QuerySource.TDSQL
								)
							);
						}

						if (checkHist)
						{
							queries.RemoveAll(x => (x.Source != QuerySource.SQLite));
						}
						else
						{
							queries.RemoveAll(x => (x.Source == QuerySource.SQLite));
						}

						if (queryInfo.ConnectionsSelectId == null)
						{
							if (!queries.Any(
								x => connectionGroup.Connections.Select(y => y.Type).Contains(x.Source)
								|| x.Source == QuerySource.SQLite
							))
							{
								continue;
							}
						}

						// var settings = Program.Model.TemplateSettings.UserSettings.FirstOrDefault(i =>
						//    i.TemplateName == connectionGroup.TemplateFileName
						//    && i.Connection.ParentKey == queryInfo.TemplateNode.IdsHierarchy
						// );

						if (Program.Model != null)
						{
							settings = Program.Model.TemplateSettings.UserSettings.FirstOrDefault(i =>
								i.TemplateName == connectionGroup.TemplateFileName
								&& i.Connection.ParentKey == queryInfo.TemplateNode.IdsHierarchy
							);
						}
						else
						{
							settings = this._model.TemplateSettings.UserSettings.FirstOrDefault(i =>
								i.TemplateName == connectionGroup.TemplateFileName
								&& i.Connection.ParentKey == queryInfo.TemplateNode.IdsHierarchy
							);
						}

						queryInfo.ReadParametersFrom(settings);

						string connectionsSelectId = queryInfo.ConnectionsSelectId;

						if (connectionsSelectId != null)
						{
							queryResult = ExecuteConnectionsSelectQuery(
								queryInfo,
								connectionGroup,
								queries,
								maxthreadCount
							);
						}
						else
						{
							queryResult = ExecuteQuery(
								connectionGroup,
								queries,
								QueryExecutionParams.CreateFrom(queryInfo),
								maxthreadCount, progress.GetChild()
							);
						}
					}
					catch (OperationCanceledException ex)
					{
						log.Error(queryInfo.ToString(), ex);

						throw;
					}
					catch (AggregateException ex)
					{
						if (ex.InnerExceptions.All(e => e is OperationCanceledException))
						{
							throw;
						}

						queryResult = new QueryResultInfo(
							new ErrorInfo(
								ex.InnerExceptions.FirstOrDefault(
									e => !(e is OperationCanceledException)
								)
							)
						);

						progress.GetChild().SetProgress(100);

						log.Error(queryInfo.ToString(), ex);
					}
					catch (Exception ex)
					{
						queryResult = new QueryResultInfo(new ErrorInfo(ex));

						progress.GetChild().SetProgress(100);

						log.Error(queryInfo.ToString(), ex);
					}

					result.Add(new TemplateNodeResultItem(queryInfo, queryResult));
				}
			}
			else
			{
				progress.SetProgress(100);
			}

			return result;
		}
Esempio n. 5
0
		/// <summary>
		/// Executes multyquery for database
		/// </summary>
		/// <param name="group">Database definition</param>
		/// <param name="templateNodeQueryInfos">Template nodes</param>
		/// <param name="progress">Progress item</param>
		/// <returns>Multyquery result</returns>
		public MultyQueryResultInfo ExecuteMultyQuery(
			GroupDefinition             @group,
			List<TemplateNodeQueryInfo> templateNodeQueryInfos,
			ProgressItem                progress
		)
		{
			////////////////////////////////////////////////////////////////////////////////////////
			// string strLogMessage = "DEBUG:MSSQLServerAuditor.Model.ExecuteMultyQuery(2)";
			// strLogMessage = strLogMessage + ";GD:" + @group.ToString();
			// log.Debug(strLogMessage);
			////////////////////////////////////////////////////////////////////////////////////////

			MultyQueryResultInfo result = new MultyQueryResultInfo();

			progress.SetPromisedChildCount(templateNodeQueryInfos.Count);

			foreach (TemplateNodeQueryInfo queryInfo in templateNodeQueryInfos)
			{
				List<QueryInfo> queries = this._model.GetQueryByTemplateNodeQueryInfo(queryInfo);

				QueryInfo query = queries.FirstOrDefault(x =>
					x.Source == (@group.Instance.Type) || x.Source == QuerySource.SQLite);

				if ((query.Source == QuerySource.MSSQL || query.Source == QuerySource.TDSQL) && this._skipMsSqlQueries)
				{
					continue;
				}

				QueryExecutionParams parameters = QueryExecutionParams.CreateFrom(queryInfo);

				QueryResultInfo queryResult = ExecuteQuery(
					@group,
					query,
					parameters,
					progress.GetChild()
				);

				result.Add(new TemplateNodeResultItem(queryInfo, queryResult));

				if (this._cancellationToken.IsCancellationRequested)
				{
					break;
				}
			}

			return result;
		}