public override void ReadTo(MultyQueryResultInfo result)
		{
			foreach (TemplateNodeResultItem templateNodeResultItem in base.TemplateNode.Queries.Select(
				templateNodeQueryInfo => ReadTemplateNodeResult(templateNodeQueryInfo, result))
			)
			{
				result.Add(templateNodeResultItem);
			}
		}
Пример #2
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;
		}
Пример #3
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;
		}
		private void ProcessCodeGuardQuery(
			MultyQueryResultInfo          result,
			TemplateNodeSqlGuardQueryInfo guardQueryInfo)
		{
			var queryResultInfo = result.List.First(item => item.TemplateNodeQuery.Id == guardQueryInfo.SqlQueryId);
			var templateNodeQueryInfo = queryResultInfo.TemplateNodeQuery;
			var userParams = new List<ParameterValue>();

			if (base.Settings != null)
			{
				var querySettings = base.Settings.Connection.Activity.Parameters
					.Where(i => i.Key == guardQueryInfo.IdsHierarchy && i.Value != null);

				foreach (var info in querySettings)
				{
					switch (info.Type)
					{
						case ParameterInfoType.Attribute:
							guardQueryInfo.GetType().GetProperty("User" + info.Parameter)
								.SetValue(guardQueryInfo, info.Value, null);
							break;

						case ParameterInfoType.Parameter:
							var parameter =
								templateNodeQueryInfo.ParameterValues.FirstOrDefault(p => p.Name == info.Parameter);
							if (parameter != null)
							{
								parameter.UserValue = info.Value;
							}
							break;

						case ParameterInfoType.EditableParameter:
							var editparameter = new ParameterValue
							{
								Name = info.Parameter,
								StringValue = info.Default,
								UserValue = info.Value
							};
							userParams.Add(editparameter);
							break;
					}
				}
			}

			var guardQueryResult = new QueryResultInfo();

			foreach (var instanceResult in queryResultInfo.QueryResult.InstancesResult)
			{
				var instance = instanceResult.Key;
				var queryTable = instanceResult.Value.DatabasesResult.First().Value.DataTables.First();

				if (!queryTable.Columns.Contains(guardQueryInfo.QueryCodeColumn))
				{
					continue;
				}

				//var meta = ReadMeta(connectionGroup, templateNode, instance, database, templateNodeQueryInfo).FirstOrDefault();
				var meta = Storage.ReadLastMeta(
					base.TemplateNode,
					instance,
					templateNodeQueryInfo
				);

				if (meta == null)
				{
					continue;
				}

				QueryInstanceResultInfo guardInstanceResult;
				var timestamp = (DateTime)meta.Values[TableDefinition.DateCreated];

				result.RefreshTimestamp(timestamp);

				if (!string.IsNullOrEmpty(meta.Values[MetaResultTable.ErrorMessageFieldName].ToString()))
				{
					guardInstanceResult = new QueryInstanceResultInfo(
						new ErrorInfo(
							meta.Values[MetaResultTable.ErrorIdFieldName].ToString(),
							meta.Values[MetaResultTable.ErrorCodeFieldName].ToString(),
							meta.Values[MetaResultTable.ErrorMessageFieldName].ToString(),
							timestamp
						),
						instance
					);
				}
				else
				{
					guardInstanceResult = new QueryInstanceResultInfo(instance);
				}

				var dataTables = Storage.ReadSqlCodeGuardResult(guardQueryInfo, queryTable, userParams);

				QueryItemInfo queryItemInfo = new QueryItemInfo
				{
					ParentQuery = new QueryInfo { Name = guardQueryInfo.QueryName }
				};

				QueryDatabaseResultInfo databaseResult = new QueryDatabaseResultInfo(
					dataTables,
					queryItemInfo,
					base.GroupDefinition.Name,
					base.GroupDefinition.Id
				);

				guardInstanceResult.AddDatabaseResult(databaseResult);
				guardQueryResult.AddInstanceResult(guardInstanceResult);
			}

			var templateNodeResultItem = new TemplateNodeResultItem(guardQueryInfo, guardQueryResult);

			result.Add(templateNodeResultItem);
		}