Ejemplo n.º 1
0
		public NodeDataProvider(
			MsSqlAuditorModel              model,
			ConcreteTemplateNodeDefinition nodeDefinition
		)
		{
			this._nodeDefinition = nodeDefinition;
			this._dataSource     = new QueryResultDataSource(model, nodeDefinition);

			IQueryResultXmlTransformer transformer = new QueryResultXmlTransformer();

			XmlDocument = transformer.Transform(this._dataSource);
		}
        public NodeDataProvider(
            MsSqlAuditorModel model,
            ConcreteTemplateNodeDefinition nodeDefinition
            )
        {
            this._nodeDefinition = nodeDefinition;
            this._dataSource     = new QueryResultDataSource(model, nodeDefinition);

            IQueryResultXmlTransformer transformer = new QueryResultXmlTransformer();

            XmlDocument = transformer.Transform(this._dataSource);
        }
        public XmlDocument Transform(QueryResultDataSource dataSource)
        {
            GroupDefinition      database      = dataSource.NodeDefinition.Group;
            MultyQueryResultInfo queriesResult = dataSource.QueriesResult;
            MsSqlAuditorModel    model         = dataSource.Model;

            if (queriesResult == null)
            {
                return(null);
            }

            try
            {
                DateTime    timestamp = queriesResult.Timestamp;
                XmlDocument xml       = new XmlDocument();

                xml.AppendChild(xml.CreateXmlDeclaration("1.0", "UTF-8", String.Empty));

                XmlElement rootNode = xml.CreateElement(Consts.ResultsTag);
                rootNode.SetAttribute(TimestampAttributeName, timestamp.ToInternetString());
                xml.AppendChild(rootNode);

                foreach (TemplateNodeResultItem tuple in queriesResult.List)
                {
                    TemplateNodeQueryInfo templateNodeQueryInfo = tuple.TemplateNodeQuery;
                    QueryResultInfo       queryResult           = tuple.QueryResult;

                    foreach (
                        KeyValuePair <InstanceInfo, QueryInstanceResultInfo> instancePair in queryResult.InstancesResult)
                    {
                        InstanceInfo instance = instancePair.Key;

                        QueryInstanceResultInfo queryInstanceResult = instancePair.Value;

                        if (queryInstanceResult.ErrorInfo == null)
                        {
                            try
                            {
                                QueryDatabaseResultInfo databaseResult = queryInstanceResult.DatabasesResult[database.Name];
                                int rowCount = 0;

                                if (databaseResult != null)
                                {
                                    rowCount = databaseResult.DataTables != null
                                                                                ? databaseResult.DataTables.Where(x => x != null).Select(x => x.Rows).Sum(x => x.Count)
                                                                                : 0;
                                }

                                GenerateResultDefinition(
                                    rootNode,
                                    templateNodeQueryInfo,
                                    databaseResult.ErrorInfo,
                                    instance,
                                    rowCount,
                                    (databaseResult.DataTables != null ? databaseResult.DataTables.Length : 0)
                                    );
                            }
                            catch (Exception ex)
                            {
                                log.Error("Error in 'Extracts data from queries and saves it to Xml-files'", ex);
                            }
                        }
                        else
                        {
                            GenerateResultDefinition(
                                rootNode,
                                templateNodeQueryInfo,
                                queryInstanceResult.ErrorInfo,
                                instance,
                                0,
                                0
                                );
                        }
                    }
                }

                foreach (TemplateNodeResultItem tuple in queriesResult.List)
                {
                    TemplateNodeQueryInfo templateNodeQueryInfo = tuple.TemplateNodeQuery;

                    if (templateNodeQueryInfo.GetType() != typeof(TemplateNodeSqlGuardQueryInfo))
                    {
                        model.GetQueryByTemplateNodeQueryInfo(templateNodeQueryInfo);
                    }

                    QueryResultInfo queryResult = tuple.QueryResult;

                    foreach (
                        KeyValuePair <InstanceInfo, QueryInstanceResultInfo> instancePair in queryResult.InstancesResult)
                    {
                        InstanceInfo            instance            = instancePair.Key;
                        QueryInstanceResultInfo queryInstanceResult = instancePair.Value;

                        if (queryInstanceResult.ErrorInfo == null)
                        {
                            foreach (KeyValuePair <string, QueryDatabaseResultInfo> namedResult in queryInstanceResult.DatabasesResult)
                            {
                                if (namedResult.Key == database.Name)
                                {
                                    XmlNode parent = rootNode.ChildNodes.OfType <XmlNode>().FirstOrDefault(
                                        x =>
                                        (
                                            x.Attributes["instance"] != null &&
                                            x.Attributes["instance"].Value == instance.Name
                                        )
                                        &&
                                        (
                                            x.Attributes["name"] != null &&
                                            x.Attributes["name"].Value == templateNodeQueryInfo.QueryName
                                        )
                                        &&
                                        (
                                            x.Attributes["hierarchy"] != null &&
                                            x.Attributes["hierarchy"].Value == (templateNodeQueryInfo.ResultHierarchy ?? string.Empty)
                                        )
                                        );

                                    QueryDatabaseResultInfo databaseResult = namedResult.Value;

                                    if (databaseResult.DataTables != null)
                                    {
                                        Int64 recordSet = 1L;

                                        foreach (DataTable curTable in databaseResult.DataTables)
                                        {
                                            if (parent != null)
                                            {
                                                parent.InnerXml = parent.InnerXml +
                                                                  ProcessDataTableAsStringXml(curTable, recordSet);
                                            }

                                            recordSet++;
                                        }
                                    }

                                    if (databaseResult.QueryItem.ParentQuery.Scope == QueryScope.Database)
                                    {
                                        if (!string.IsNullOrEmpty(databaseResult.Database))
                                        {
                                            XmlAttribute attr = xml.CreateAttribute("database");
                                            attr.Value = databaseResult.Database;
                                            parent.Attributes.Append(attr);
                                        }

                                        if (!string.IsNullOrEmpty(databaseResult.DatabaseId))
                                        {
                                            XmlAttribute attr = xml.CreateAttribute("databaseId");
                                            attr.Value = databaseResult.DatabaseId;
                                            parent.Attributes.Append(attr);
                                        }
                                    }
                                    else if (databaseResult.QueryItem.ParentQuery.Scope == QueryScope.InstanceGroup)
                                    {
                                        if (!string.IsNullOrEmpty(databaseResult.Database))
                                        {
                                            XmlAttribute attr = xml.CreateAttribute("InstanceGroupName");
                                            attr.Value = databaseResult.Database;
                                            parent.Attributes.Append(attr);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                return(xml);
            }
            catch (Exception ex)
            {
                log.Error("Error in 'Extracts data from queries and saves it to Xml-files'", ex);
            }

            return(null);
        }
		public XmlDocument Transform(QueryResultDataSource dataSource)
		{
			GroupDefinition      database      = dataSource.NodeDefinition.Group;
			MultyQueryResultInfo queriesResult = dataSource.QueriesResult;
			MsSqlAuditorModel    model         = dataSource.Model;

			if (queriesResult == null)
			{
				return null;
			}

			try
			{
				DateTime    timestamp = queriesResult.Timestamp;
				XmlDocument xml       = new XmlDocument();

				xml.AppendChild(xml.CreateXmlDeclaration("1.0", "UTF-8", String.Empty));

				XmlElement rootNode = xml.CreateElement(Consts.ResultsTag);
				rootNode.SetAttribute(TimestampAttributeName, timestamp.ToInternetString());
				xml.AppendChild(rootNode);

				foreach (TemplateNodeResultItem tuple in queriesResult.List)
				{
					TemplateNodeQueryInfo templateNodeQueryInfo = tuple.TemplateNodeQuery;
					QueryResultInfo       queryResult           = tuple.QueryResult;

					foreach (
						KeyValuePair<InstanceInfo, QueryInstanceResultInfo> instancePair in queryResult.InstancesResult)
					{
						InstanceInfo instance = instancePair.Key;

						QueryInstanceResultInfo queryInstanceResult = instancePair.Value;

						if (queryInstanceResult.ErrorInfo == null)
						{
							try
							{
								QueryDatabaseResultInfo databaseResult = queryInstanceResult.DatabasesResult[database.Name];
								int                     rowCount       = 0;

								if (databaseResult != null)
								{
									rowCount = databaseResult.DataTables != null
										? databaseResult.DataTables.Where(x => x != null).Select(x => x.Rows).Sum(x => x.Count)
										: 0;
								}

								GenerateResultDefinition(
									rootNode,
									templateNodeQueryInfo,
									databaseResult.ErrorInfo,
									instance,
									rowCount,
									(databaseResult.DataTables != null ? databaseResult.DataTables.Length : 0)
								);
							}
							catch (Exception ex)
							{
								log.Error("Error in 'Extracts data from queries and saves it to Xml-files'", ex);
							}
						}
						else
						{
							GenerateResultDefinition(
								rootNode,
								templateNodeQueryInfo,
								queryInstanceResult.ErrorInfo,
								instance,
								0,
								0
							);
						}
					}
				}

				foreach (TemplateNodeResultItem tuple in queriesResult.List)
				{
					TemplateNodeQueryInfo templateNodeQueryInfo = tuple.TemplateNodeQuery;

					if (templateNodeQueryInfo.GetType() != typeof(TemplateNodeSqlGuardQueryInfo))
					{
						model.GetQueryByTemplateNodeQueryInfo(templateNodeQueryInfo);
					}

					QueryResultInfo queryResult = tuple.QueryResult;

					foreach (
						KeyValuePair<InstanceInfo, QueryInstanceResultInfo> instancePair in queryResult.InstancesResult)
					{
						InstanceInfo            instance            = instancePair.Key;
						QueryInstanceResultInfo queryInstanceResult = instancePair.Value;

						if (queryInstanceResult.ErrorInfo == null)
						{
							foreach (KeyValuePair<string, QueryDatabaseResultInfo> namedResult in queryInstanceResult.DatabasesResult)
							{
								if (namedResult.Key == database.Name)
								{
									XmlNode parent = rootNode.ChildNodes.OfType<XmlNode>().FirstOrDefault(
										x =>
										(
											x.Attributes["instance"]        != null &&
											x.Attributes["instance"].Value  == instance.Name
										)
										&&
										(
											x.Attributes["name"]       != null &&
											x.Attributes["name"].Value == templateNodeQueryInfo.QueryName
										)
										&&
										(
											x.Attributes["hierarchy"]      != null &&
											x.Attributes["hierarchy"].Value == (templateNodeQueryInfo.ResultHierarchy ?? string.Empty)
										)
									);

									QueryDatabaseResultInfo databaseResult = namedResult.Value;

									if (databaseResult.DataTables != null)
									{
										Int64 recordSet = 1L;

										foreach (DataTable curTable in databaseResult.DataTables)
										{
											if (parent != null)
											{
												parent.InnerXml = parent.InnerXml +
													ProcessDataTableAsStringXml(curTable, recordSet);
											}

											recordSet++;
										}
									}

									if (databaseResult.QueryItem.ParentQuery.Scope == QueryScope.Database)
									{
										if (!string.IsNullOrEmpty(databaseResult.Database))
										{
											XmlAttribute attr = xml.CreateAttribute("database");
											attr.Value = databaseResult.Database;
											parent.Attributes.Append(attr);
										}

										if (!string.IsNullOrEmpty(databaseResult.DatabaseId))
										{
											XmlAttribute attr = xml.CreateAttribute("databaseId");
											attr.Value = databaseResult.DatabaseId;
											parent.Attributes.Append(attr);
										}
									}
									else if (databaseResult.QueryItem.ParentQuery.Scope == QueryScope.InstanceGroup)
									{
										if (!string.IsNullOrEmpty(databaseResult.Database))
										{
											XmlAttribute attr = xml.CreateAttribute("InstanceGroupName");
											attr.Value = databaseResult.Database;
											parent.Attributes.Append(attr);
										}
									}
								}
							}
						}
					}
				}

				return xml;
			}
			catch (Exception ex)
			{
				log.Error("Error in 'Extracts data from queries and saves it to Xml-files'", ex);
			}

			return null;
		}