public void UpdateConnection(
			Int64             parentQueryId,
			DynamicConnection connection
		)
		{
			if (!this.ReadOnly)
			{
				var fields = new List<Field> {
					this.CreateField(ParentQueryIdFn, parentQueryId),
					this.CreateField(QueryIdFn, connection.QueryId),
					this.CreateField(ConnectionNameField, connection.Name),
					this.CreateField(ConnectionTypeField, connection.Type),
					this.CreateField(IsOdbcConnectionField, connection.IsOdbc)
				};

				var row = this.NewRow();

				foreach (var field in fields)
				{
					row.Values.Add(field.Item1, field.Item2);
				}

				this.InsertOrUpdateRow(row, this.BeforeRowUpdate, this.BeforeRowAdd);
			}
		}
		private void SaveDynamicConnections(
			TemplateNodeInfo     templateNode,
			MultyQueryResultInfo queriesResult
		)
		{
			foreach (TemplateNodeResultItem templateNodeResultItem in queriesResult.List)
			{
				TemplateNodeQueryInfo queryInfo         = templateNodeResultItem.TemplateNodeQuery;
				TemplateNodeInfo      templateNodeClone = templateNode;

				if (queryInfo.ConnectionsSelectId != null)
				{
					QueryResultInfo queryResult = templateNodeResultItem.QueryResult;

					foreach (InstanceInfo instanceInfo in templateNodeClone.ConnectionGroup.Connections)
					{
						Int64? destParentQueryId = this.CurrentStorage.QueryDirectory.GetQueryId(
							templateNode,
							queryInfo,
							instanceInfo,
							DateTime.Now,
							false
						);

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

							Int64? dynamicQueryId = this.CurrentStorage.QueryDirectory.GetQueryId(
								templateNodeClone,
								queryInfo,
								selectConnectionInstance,
								DateTime.Now,
								false
							);

							if (destParentQueryId == null)
							{
								continue;
							}

							DynamicConnection dynamicConnection = new DynamicConnection(
								selectConnectionInstance.Name,
								selectConnectionInstance.Type.ToString(),
								selectConnectionInstance.IsODBC,
								dynamicQueryId
							);

							this.CurrentStorage.DynamicConnectionDirectory.UpdateConnection(
								destParentQueryId.Value,
								dynamicConnection
							);
						}
					}
				}
			}
		}
Esempio n. 3
0
		public QueryResultInfo ExecuteConnectionsSelectQuery(
			TemplateNodeQueryInfo queryInfo,
			ConnectionGroupInfo   connectionGroup,
			List<QueryInfo>       queries,
			int                   maxthreadCount
		)
		{
			const string colConnectionName   = "ConnectionName";
			const string colConnectionType   = "ConnectionType";
			const string colConnectionIsOdbc = "IsOdbcConnection";

			IStorageManager storageManager = this._model.GetVaultProcessor(connectionGroup);
			CurrentStorage storage = storageManager.CurrentStorage;

			TemplateNodeQueryInfo connectionsQueryInfo = queryInfo.GetParentConnectionSelectQuery();

			List<QueryInfo> connectionsQueries = this._model.GetQueryByTemplateNodeQueryInfo(connectionsQueryInfo);

			QueryResultInfo connectionsQueryResult = ExecuteQuery(
				connectionGroup,
				connectionsQueries,
				QueryExecutionParams.CreateFrom(connectionsQueryInfo),
				maxthreadCount
			);

			QueryResultInfo queryResult = new QueryResultInfo();

			foreach (KeyValuePair<InstanceInfo, QueryInstanceResultInfo> resultInfo in connectionsQueryResult.InstancesResult)
			{
				QueryInstanceResultInfo instanceResult = resultInfo.Value;

				if (instanceResult.ErrorInfo == null)
				{
					InstanceInfo mainConnection = resultInfo.Key;

					Int64? parentQueryId  = storage.QueryDirectory.GetQueryId(
						queryInfo.TemplateNode,
						queryInfo,
						mainConnection,
						DateTime.Now,
						false
					);

					List<DynamicConnection> dynamicConnections = new List<DynamicConnection>();

					foreach (KeyValuePair<string, QueryDatabaseResultInfo> databaseResultInfo in instanceResult.DatabasesResult)
					{
						QueryDatabaseResultInfo queryDatabaseResultInfo = databaseResultInfo.Value;
						DataTable[]             dataTables              = queryDatabaseResultInfo.DataTables;

						if (dataTables != null)
						{
							foreach (DataTable dataTable in dataTables)
							{
								bool colConnectionNameExists = dataTable.Columns.Contains(colConnectionName);
								bool colConnectionTypeExists = dataTable.Columns.Contains(colConnectionType);
								bool colConnectionOdbcExists = dataTable.Columns.Contains(colConnectionIsOdbc);

								foreach (DataRow row in dataTable.Rows)
								{
									string connectionName   = colConnectionNameExists ? row[colConnectionName].ToString() : string.Empty;
									string connectionType   = colConnectionTypeExists ? row[colConnectionType].ToString() : QuerySource.MSSQL.ToString();
									bool   isOdbcConnection = false;

									if (colConnectionOdbcExists)
									{
										bool.TryParse(row[colConnectionIsOdbc].ToString(), out isOdbcConnection);
									}

									QuerySource sourceType;

									if (!Enum.TryParse(connectionType, true, out sourceType))
									{
										sourceType = QuerySource.MSSQL;
									}

									InstanceInfo selectConnectionInstance = InstanceInfoResolver.ResolveDynamicInstance(
										connectionName,
										sourceType,
										isOdbcConnection
									);

									selectConnectionInstance.ConnectionGroup = connectionGroup;

									if (selectConnectionInstance.IsEnabled)
									{
										var query = queries.FirstOrDefault(
											x => x.Source == (selectConnectionInstance.Type) || x.Source == QuerySource.SQLite
										);

										if (query == null)
										{
											continue;
										}

										InstanceVersion ver = selectConnectionInstance.InitServerProperties(
											storage,
											this._model.Settings.SqlTimeout
										).Version;

										QueryItemInfo queryItem = query.Items.GetQueryItemForVersion(ver);

										QueryInstanceResultInfo instanceResultInfo = GetConnectionSelectResults(
											selectConnectionInstance,
											query,
											queryItem,
											queryInfo
										);

										queryResult.AddInstanceResult(instanceResultInfo);

										Int64? dynamicQueryId = storage.QueryDirectory.GetQueryId(
											queryInfo.TemplateNode,
											queryInfo,
											selectConnectionInstance,
											DateTime.Now,
											false
										);

										if (parentQueryId.HasValue && dynamicQueryId.HasValue)
										{
											DynamicConnection dynamicConnection = new DynamicConnection(
												connectionName,
												connectionType,
												isOdbcConnection,
												dynamicQueryId
											);

											dynamicConnections.Add(dynamicConnection);
										}
									}
								}
							}
						}
					}

					if (dynamicConnections.Count > 0)
					{
						Int64                      pqId                       = parentQueryId.Value;
						DynamicConnectionDirectory dynamicConnectionDirectory = storage.DynamicConnectionDirectory;

						dynamicConnectionDirectory.RemoveConnections(pqId);
						dynamicConnectionDirectory.UpdateConnections(pqId, dynamicConnections);
					}
				}
			}

			return queryResult;
		}