public void ReadParameter(
			ConnectionGroupInfo   connectionGroup,
			Int64                 queryId,
			TemplateNodeQueryInfo query,
			ParameterValue        paramValue
		)
        {
            Int64? parameterId = GetParameterId(connectionGroup, query, paramValue.Name);

            if (parameterId != null)
            {
                List<ITableRow> rows = this.GetRows(
                    QueryIdFn + " = " + queryId + " AND " + ParameterIdFn + " = " + parameterId
                );

                if (rows != null)
                {
                    if (rows.Count != 0)
                    {
                        ITableRow paramRow = rows.ElementAt(0);

                        paramValue.UserValue = paramRow.GetValue<string>(ValueFn);
                    }
                }
            }
        }
        /// <summary>
        /// Get Id for data
        /// </summary>
        /// <param name="connectionGroup">Connection group</param>
        /// <param name="templateNode">Template node</param>
        /// <returns></returns>
        public Int64? GetId(ConnectionGroupInfo connectionGroup, TemplateNodeInfo templateNode)
        {
            Debug.Assert(!templateNode.IsInstance);

            Int64? parentId = null;

            if (templateNode.Parent != null)
            {
                parentId = this.GetId(connectionGroup, templateNode.Parent);
            }

            long?                 templateId = Storage.TemplateDirectory.GetId(connectionGroup);
            TemplateNodeQueryInfo pq         = templateNode.GetParentQuery();

            object parentQueryId = pq != null
                ? (object) Storage.TemplateNodeQueryGroupDirectory.GetId(connectionGroup, pq)
                : null;

            return this.GetRecordIdByFields(
                this.CreateField(TemplateIdFn,                 templateId),
                this.CreateField(ParentIdFn,                   parentId),
                this.CreateField(UserIdFieldName,              templateNode.Id),
                this.CreateField(NameFn,                       templateNode.Name),
                this.CreateField(IconFieldName,                templateNode.IconImageReferenceName),
                this.CreateField(ShowIfEmptyFieldName,         !templateNode.HideEmptyResultDatabases),
                this.CreateField(ShowNumberOfRecordsFieldName, templateNode.ShowNumberOfRecords),
                this.CreateField(ParentQueryGroupIdFn,         parentQueryId)
            );
        }
        /// <summary>
        /// Get Id for data
        /// </summary>
        /// <param name="connectionGroup">Connection group</param>
        /// <param name="instance">Instance</param>
        /// <returns></returns>
        public Int64? GetId(ConnectionGroupInfo connectionGroup, InstanceInfo instance)
        {
            connectionGroup.ReadGroupIdFrom(Storage.ConnectionGroupDirectory);

            Int64?           loginId    = Storage.LoginDirectory.GetId(instance);
            ServerProperties props      = instance.ServerProperties;
            string           serverName = null;
            string           serverVers = null;

            if (props != null)
            {
                serverName = props.Name;
                serverVers = props.Version.ToString();
            }

            return this.GetRecordIdByFields(
                this.CreateField(ConnectionGroupIdFn,     connectionGroup.Identity),
                this.CreateField(LoginIdFn,               loginId),
                this.CreateField(ConnectionNameFn,        instance.Name),
                this.CreateField(ServerInstanceNameFn,    serverName),
                this.CreateField(ServerInstanceVersionFn, serverVers),
                this.CreateField(DbTypeFn,                instance.DbType),
                this.CreateField(IsOdbcFn,                instance.IsODBC),
                this.CreateField(IsDynamicConnectionFn,   instance.IsDynamicConnection)
            );
        }
 /// <summary>
 /// Get Id for data
 /// </summary>
 /// <param name="connectionGroup">Connection group</param>
 /// <returns></returns>
 public Int64? GetId(ConnectionGroupInfo connectionGroup)
 {
     return this.GetRecordIdByFields(
         this.CreateField(IdFieldName,   connectionGroup.TemplateFileName),
         this.CreateField(NameFieldName, connectionGroup.TemplateId),
         this.CreateField(DirFieldName,  connectionGroup.TemplateDir ?? string.Empty)
     );
 }
        protected override long? GetParameterId(
			ConnectionGroupInfo   connectionGroup,
			TemplateNodeQueryInfo query,
			string                parameterName
		)
        {
            return Storage.TemplateNodeQueryGroupParameterDirectory
                .GetId(connectionGroup, query, parameterName);
        }
		public MultyQueryResultInfo ReadCurrentResult(
			ConnectionGroupInfo            connectionGroup,
			ConcreteTemplateNodeDefinition concreteTemplateNode
		)
		{
			return this._storageManager.ReadCurrentResult(
				connectionGroup,
				concreteTemplateNode
			);
		}
        public Int64? GetId(ConnectionGroupInfo connectionGroup, TemplateNodeQueryInfo groupQuery, string parameterName)
        {
            long? queryId = Storage.TemplateNodeQueryGroupDirectory
                .GetId(connectionGroup, groupQuery);

            return this.GetRecordIdByFields(
                this.CreateField(TemplateNodeQueryGroupDirectory.TableName.AsFk(), queryId),
                this.CreateField(ParameterNameFieldName, parameterName)
            );
        }
		public int? GetDataRowCount(
			ConnectionGroupInfo            connectionGroup,
			ConcreteTemplateNodeDefinition concreteTemplateNode
		)
		{
			int? result =
				this._storageManager.GetDataRowCount(connectionGroup, concreteTemplateNode) ??
				this._storageManager.CurrentStorage.GetRowCount(concreteTemplateNode.TemplateNode);

			return result;
		}
		public MultyQueryResultInfo Read(
			ConnectionGroupInfo            connectionGroup,
			ConcreteTemplateNodeDefinition concreteTemplateNode
		)
		{
			var result = new MultyQueryResultInfo();

			this.ReadFromMeta(connectionGroup, concreteTemplateNode, result);
			this.ReadFromCodeGuard(concreteTemplateNode, result);

			return result;
		}
		/// <summary>
		/// Get Id for data
		/// </summary>
		/// <param name="connectionGroup"></param>
		/// <returns></returns>
		public Int64? GetId(ConnectionGroupInfo connectionGroup)
		{
			Int64? connectionGroupId = null;

			if (connectionGroup != null)
			{
				connectionGroupId = this.GetRecordIdByFields(
					this.CreateField(NameFn, connectionGroup.Name),
					this.CreateField(IsDirectConnectionFn, connectionGroup.IsDirectConnection)
				);
			}

			return connectionGroupId;
		}
        public Int64? GetId(ConnectionGroupInfo connectionGroup, TemplateNodeQueryInfo query, string parameterName)
        {
            TemplateNodeInfo templateNode = query.TemplateNode.IsInstance
                ? query.TemplateNode.Template
                : query.TemplateNode;

            long? queryId = Storage.TemplateNodeQueryDirectory
                .GetId(connectionGroup, templateNode, query);

            return this.GetRecordIdByFields(
                this.CreateField(TemplateNodeQueryDirectory.TableName.AsFk(), queryId),
                this.CreateField(ParameterNameFieldName, parameterName)
            );
        }
		private void ReadFromMeta(
			ConnectionGroupInfo            connectionGroup,
			ConcreteTemplateNodeDefinition concreteTemplateNode,
			MultyQueryResultInfo           result
		)
		{
			NodeResultReader nodeResultReader = new MetaResultReader(
				this._msSqlAuditor,
				this._storageManager,
				connectionGroup,
				concreteTemplateNode
			);

			nodeResultReader.ReadTo(result);
		}
        public Int64? GetId(ConnectionGroupInfo connectionGroup, TemplateNodeQueryInfo query)
        {
            Debug.Assert(query.TemplateNode.GroupQueries.Contains(query));

            TemplateNodeInfo templateNode = query.TemplateNode.IsInstance
                ? query.TemplateNode.Template
                : query.TemplateNode;

            long? templateNodeId = Storage.TemplateNodeDirectory.GetId(connectionGroup, templateNode);

            return this.GetRecordIdByFields(
                this.CreateField(TemplateNodeDirectory.TableName.AsFk(), templateNodeId),
                this.CreateField(TemplateNodeQueryGroupIdFieldName,      query.Id),
                this.CreateField(TemplateNodeQueryGroupNameFieldName,    query.QueryName),
                this.CreateField(DefaultDatabaseFieldFn,                 query.DatabaseForChildrenFieldName)
            );
        }
		/// <summary>
		/// The create trial connection group.
		/// </summary>
		/// <param name="propertiesList">The properties list.</param>
		/// <param name="templateFile">Path to temlate file</param>
		/// <param name="dbType">Type DB</param>
		/// <param name="connectionGroupName">Name connection group</param>
		/// <param name="isExternalTemplate">Is opened from user file template </param>
		/// <returns>
		/// The <see cref="ConnectionGroupInfo" />.
		/// </returns>
		public static ConnectionGroupInfo CreateTrialConnectionGroup(
			List<Tuple<DbConnectionStringBuilder, bool>> propertiesList,
			string                                       templateFile,
			QuerySource                                  dbType,
			string                                       connectionGroupName,
			bool                                         isExternalTemplate
		)
		{
			ConnectionGroupInfo connectionGroup = new ConnectionGroupInfo
			{
				Connections      = InstanceInfoResolver.ResolveInstances(propertiesList, dbType),
				IsExternal       = isExternalTemplate,
				TemplateDir      = Path.GetDirectoryName(templateFile),
				TemplateFileName = Path.GetFileName(templateFile),
				Name             = connectionGroupName
			};

			return connectionGroup;
		}
        /// <summary>
        /// Get ID for data
        /// </summary>
        /// <param name="connectionGroup">Connection group</param>
        /// <param name="templateNode">Template node</param>
        /// <param name="templateNodeQuery">Template node query</param>
        /// <returns></returns>
        public Int64? GetId(
			ConnectionGroupInfo   connectionGroup,
			TemplateNodeInfo      templateNode,
			TemplateNodeQueryInfo templateNodeQuery
		)
        {
            long? templateNodeId = Storage.TemplateNodeDirectory
                .GetId(connectionGroup, templateNode);

            string id        = templateNodeQuery.Id;
            string name      = templateNodeQuery.QueryName;
            string hierarchy = templateNodeQuery.ResultHierarchy;

            return this.GetRecordIdByFields(
                this.CreateField(TemplateNodeIdFieldName, templateNodeId),
                this.CreateField(UserIdFieldName,         id),
                this.CreateField(NameFieldName,           name),
                this.CreateField(HierarchyFieldName,      hierarchy)
            );
        }
		public ConnectionGroupInfo GetGroup(long groupId)
		{
			ConnectionGroupRow groupRow = this._groupManager
				.GetGroupById(groupId);

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

			ConnectionGroupInfo group = new ConnectionGroupInfo
			{
				Identity           = groupId,
				Name               = groupRow.Name,
				IsDirectConnection = groupRow.IsDirect
			};

			group.Connections = GetGroupInstances(groupId);

			return group;
		}
		public EditDirectConnectionDialog(
			MsSqlAuditorModel   model,
			ConnectionGroupInfo connectionGroup
		)
		{
			this._model = model;

			CurrentStorage storage   = this._model.DefaultVaultProcessor.CurrentStorage;
			this._connectionsManager = new ConnectionsManager(model);

			this._connectionGroup = connectionGroup;
			this._updatesMade     = false;

			SetLocale();

			InitializeComponent();
			InitializeEventHandlers();
			InitializeBindings();

			UpdateControlsState();
		}
		private List<ConnectionGroupInfo> GetGroupsFromRows(string protocolType, List<ConnectionGroupRow> groupRows)
		{
			List<ConnectionGroupInfo> groups = new List<ConnectionGroupInfo>();

			foreach (ConnectionGroupRow groupRow in groupRows)
			{
				long? groupId = groupRow.Identity;

				ConnectionGroupInfo group = new ConnectionGroupInfo
				{
					Identity           = groupId,
					Name               = groupRow.Name,
					IsDirectConnection = groupRow.IsDirect
				};

				group.Connections = GetGroupInstances(groupId.Value, protocolType);

				groups.Add(group);
			}

			return groups;
		}
		public MetaResultReader(
			MsSqlAuditorModel              msSqlAuditor,
			StorageManager                 storageManager,
			ConnectionGroupInfo            connectionGroup,
			ConcreteTemplateNodeDefinition concreteTemplateNode
		) : base(
				msSqlAuditor,
				storageManager,
				concreteTemplateNode
			)
		{
			this._instances = new List<InstanceInfo>();

			if (base.GroupDefinition.Instance != null)
			{
				this._instances.Add(base.GroupDefinition.Instance);
			}
			else
			{
				this._instances.AddRange(connectionGroup.Connections.Where(info => info.IsEnabled));
			}

			this._histTable = ReadHistTable(connectionGroup, concreteTemplateNode);
		}
		private ConnectionGroupInfo PrepareGroupResult()
		{
			List<InstanceInfo> instances = this.lstConnectionStrings.Items
				.OfType<InstanceInfo>().ToList();

			ConnectionGroupInfo group = new ConnectionGroupInfo
			{
				Connections        = instances,
				IsExternal         = IsExternalTemplateFile,
				TemplateDir        = Path.GetDirectoryName(SelectedTemplateFile),
				TemplateFileName   = Path.GetFileName(SelectedTemplateFile),
				Name               = ConnectionGroupName,
				IsDirectConnection = true
			};

			foreach (InstanceInfo instanceInfo in instances)
			{
				instanceInfo.ConnectionGroup = group;
			}

			UpdateGroupInstances(group);

			return group;
		}
		private void UpdateGroupInstances(ConnectionGroupInfo group)
		{
			ConnectionType cnnType = SelectedConnectionType;
			if (cnnType != null)
			{
				this._connectionsManager.UpdateGroupInstances(group, cnnType.Id);
			}
		}
		/// <summary>
		/// Get vault processor for Connection group)
		/// </summary>
		/// <param name="connectionGroup"></param>
		/// <returns></returns>
		public IStorageManager GetVaultProcessor(ConnectionGroupInfo connectionGroup)
		{
			if (this._vaultProcessors.ContainsKey(connectionGroup))
			{
				return this._vaultProcessors[connectionGroup];
			}

			return DefaultVaultProcessor;
		}
		private CurrentStorage GetStorage(
			ConnectionGroupInfo connectionGroup
		)
		{
			return this._treeControl._model.GetVaultProcessor(connectionGroup).CurrentStorage;
		}
		/// <summary>
		/// Register association connection group -> vault processor
		/// </summary>
		/// <param name="vaultProcessor">Vault processor</param>
		/// <param name="connectionGroup">Connection group</param>
		public void AssociateVaultProcessor(IStorageManager vaultProcessor, ConnectionGroupInfo connectionGroup)
		{
			if (vaultProcessor != DefaultVaultProcessor)
			{
				this._vaultProcessors.Add(connectionGroup, vaultProcessor);
			}
		}
		/// <summary>
		/// Unregister association connection group -> vault processor
		/// </summary>
		/// <param name="connectionGroup">Connection group</param>
		public void DeassociateVaultProcessor(ConnectionGroupInfo connectionGroup)
		{
			if (this._vaultProcessors.ContainsKey(connectionGroup))
			{
				this._vaultProcessors.Remove(connectionGroup);
			}
		}
 /// <summary>
 /// Initializes the object ConnectionData.
 /// </summary>
 /// <param name="model">Model SqlAuditor.</param>
 /// <param name="connectionGroup">Info group connection.</param>
 public ConnectionData(MsSqlAuditorModel model, ConnectionGroupInfo connectionGroup)
     : this(model)
 {
     ConnectionGroup = connectionGroup;
     ReloadTemplate();
 }
		public void OpenConnection(ConnectionGroupInfo connectionGroup)
		{
			OpenConnection(new ConnectionData(this._model, connectionGroup));
		}
		private static List<ConnectionGroupInfo> ExecuteSelectGroupsCommand(SQLiteConnection connection, string sql)
		{
			List<ConnectionGroupInfo> result = new List<ConnectionGroupInfo>();

			new SqlSelectCommand(
				connection,
				sql,
				reader =>
				{
					long id = (long)reader[ConnectionGroupDirectory.TableIdentityField];

					ConnectionGroupInfo connectionGroup = new ConnectionGroupInfo
					{
						Identity           = id,
						Name               = reader[ConnectionGroupDirectory.NameFn].ToString(),
						IsDirectConnection = (bool)reader[ConnectionGroupDirectory.IsDirectConnectionFn],
						TemplateFileName   = reader[TemplateDirectory.IdFieldName].ToString(),
						TemplateDir        = reader[TemplateDirectory.DirFieldName].ToString(),
						TemplateId         = reader[TemplateDirectory.NameFieldName].ToString(),
					};

					connectionGroup.Identity = id;

					result.Add(connectionGroup);
				}
			).Execute(100);

			return result;
		}
		public int? GetDataRowCount(ConcreteTemplateNodeDefinition nodeDefinition, ConnectionGroupInfo connectionGroup)
		{
			return this._model.GetVaultProcessor(connectionGroup)
				.GetDataRowCount(
					connectionGroup,
					nodeDefinition
				);
		}
		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;
		}