// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Data.Common; using Microsoft.SqlTools.DataProtocol.Contracts.Connection; using Microsoft.SqlTools.Hosting.Utility; namespace Microsoft.SqlTools.CoreServices.Connection { /// /// Information pertaining to a unique connection instance. /// public class ConnectionInfo { /// /// Constructor /// public ConnectionInfo(ISqlConnectionFactory factory, string ownerUri, ConnectionDetails details) { Factory = factory; OwnerUri = ownerUri; ConnectionDetails = details; ConnectionId = Guid.NewGuid(); IntellisenseMetrics = new InteractionMetrics(new int[] {50, 100, 200, 500, 1000, 2000}); } /// /// Unique Id, helpful to identify a connection info object /// public Guid ConnectionId { get; private set; } /// /// URI identifying the owner/user of the connection. Could be a file, service, resource, etc. /// public string OwnerUri { get; private set; } /// /// Factory used for creating the SQL connection associated with the connection info. /// public ISqlConnectionFactory Factory { get; private set; } /// /// Properties used for creating/opening the SQL connection. /// public ConnectionDetails ConnectionDetails { get; private set; } /// /// A map containing all connections to the database that are associated with /// this ConnectionInfo's OwnerUri. /// This is internal for testing access only /// internal readonly ConcurrentDictionary ConnectionTypeToConnectionMap = new ConcurrentDictionary(); /// /// Intellisense Metrics /// public InteractionMetrics IntellisenseMetrics { get; private set; } /// /// Returns true if the db connection is to any cloud instance /// public bool IsCloud { get; set; } /// /// Returns true if the db connection is to a SQL db instance /// public bool IsSqlDb { get; set; } /// Returns true if the sql connection is to a DW instance /// public bool IsSqlDW { get; set; } /// /// Returns the major version number of the db we are connected to /// public int MajorVersion { get; set; } /// /// All DbConnection instances held by this ConnectionInfo /// public ICollection AllConnections { get { return ConnectionTypeToConnectionMap.Values; } } /// /// All connection type strings held by this ConnectionInfo /// /// public ICollection AllConnectionTypes { get { return ConnectionTypeToConnectionMap.Keys; } } public bool HasConnectionType(string connectionType) { connectionType = connectionType ?? ConnectionType.Default; return ConnectionTypeToConnectionMap.ContainsKey(connectionType); } /// /// The count of DbConnectioninstances held by this ConnectionInfo /// public int CountConnections { get { return ConnectionTypeToConnectionMap.Count; } } /// /// Try to get the DbConnection associated with the given connection type string. /// /// true if a connection with type connectionType was located and out connection was set, /// false otherwise /// Thrown when connectionType is null or empty public bool TryGetConnection(string connectionType, out DbConnection connection) { Validate.IsNotNullOrEmptyString("Connection Type", connectionType); return ConnectionTypeToConnectionMap.TryGetValue(connectionType, out connection); } /// /// Adds a DbConnection to this object and associates it with the given /// connection type string. If a connection already exists with an identical /// connection type string, it is not overwritten. Ignores calls where connectionType = null /// /// Thrown when connectionType is null or empty public void AddConnection(string connectionType, DbConnection connection) { Validate.IsNotNullOrEmptyString("Connection Type", connectionType); ConnectionTypeToConnectionMap.TryAdd(connectionType, connection); } /// /// Removes the single DbConnection instance associated with string connectionType /// /// Thrown when connectionType is null or empty public void RemoveConnection(string connectionType) { Validate.IsNotNullOrEmptyString("Connection Type", connectionType); DbConnection connection; ConnectionTypeToConnectionMap.TryRemove(connectionType, out connection); } /// /// Removes all DbConnection instances held by this object /// public void RemoveAllConnections() { foreach (var type in AllConnectionTypes) { DbConnection connection; ConnectionTypeToConnectionMap.TryRemove(type, out connection); } } } }