mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-02 17:24:50 -05:00
Cleaned up connection management code
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// 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.Data.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
{
|
||||
/// <summary>
|
||||
/// Information pertaining to a unique connection instance.
|
||||
/// </summary>
|
||||
public class ConnectionInfo
|
||||
{
|
||||
public ConnectionInfo(ISqlConnectionFactory factory, string ownerUri, ConnectionDetails details)
|
||||
{
|
||||
Factory = factory;
|
||||
OwnerUri = ownerUri;
|
||||
ConnectionDetails = details;
|
||||
ConnectionId = Guid.NewGuid();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unique Id, helpful to identify a connection info object
|
||||
/// </summary>
|
||||
public Guid ConnectionId { get; private set; }
|
||||
|
||||
public string OwnerUri { get; private set; }
|
||||
|
||||
public ISqlConnectionFactory Factory {get; private set;}
|
||||
|
||||
public ConnectionDetails ConnectionDetails { get; private set; }
|
||||
|
||||
public DbConnection SqlConnection { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -5,42 +5,16 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.EditorServices.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting;
|
||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Connection
|
||||
{
|
||||
public class ConnectionInfo
|
||||
{
|
||||
public ConnectionInfo(ISqlConnectionFactory factory, string ownerUri, ConnectionDetails details)
|
||||
{
|
||||
Factory = factory;
|
||||
OwnerUri = ownerUri;
|
||||
ConnectionDetails = details;
|
||||
ConnectionId = Guid.NewGuid();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unique Id, helpful to identify a connection info object
|
||||
/// </summary>
|
||||
public Guid ConnectionId { get; private set; }
|
||||
|
||||
public string OwnerUri { get; private set; }
|
||||
|
||||
public ISqlConnectionFactory Factory {get; private set;}
|
||||
|
||||
public ConnectionDetails ConnectionDetails { get; private set; }
|
||||
|
||||
public DbConnection SqlConnection { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Main class for the Connection Management services
|
||||
/// </summary>
|
||||
|
||||
@@ -27,4 +27,23 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods to ConnectionSummary
|
||||
/// </summary>
|
||||
public static class ConnectionSummaryExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a copy of a ConnectionSummary object
|
||||
/// </summary>
|
||||
public static ConnectionSummary Clone(this ConnectionSummary summary)
|
||||
{
|
||||
return new ConnectionSummary()
|
||||
{
|
||||
ServerName = summary.ServerName,
|
||||
DatabaseName = summary.DatabaseName,
|
||||
UserName = summary.UserName
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
//
|
||||
// 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.Generic;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Treats connections as the same if their server, db and usernames all match
|
||||
/// </summary>
|
||||
public class ConnectionSummaryComparer : IEqualityComparer<ConnectionSummary>
|
||||
{
|
||||
public bool Equals(ConnectionSummary x, ConnectionSummary y)
|
||||
{
|
||||
if(x == y) { return true; }
|
||||
else if(x != null)
|
||||
{
|
||||
if(y == null) { return false; }
|
||||
|
||||
// Compare server, db, username. Note: server is case-insensitive in the driver
|
||||
return string.Compare(x.ServerName, y.ServerName, StringComparison.OrdinalIgnoreCase) == 0
|
||||
&& string.Compare(x.DatabaseName, y.DatabaseName, StringComparison.Ordinal) == 0
|
||||
&& string.Compare(x.UserName, y.UserName, StringComparison.Ordinal) == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int GetHashCode(ConnectionSummary obj)
|
||||
{
|
||||
int hashcode = 31;
|
||||
if(obj != null)
|
||||
{
|
||||
if(obj.ServerName != null)
|
||||
{
|
||||
hashcode ^= obj.ServerName.GetHashCode();
|
||||
}
|
||||
if (obj.DatabaseName != null)
|
||||
{
|
||||
hashcode ^= obj.DatabaseName.GetHashCode();
|
||||
}
|
||||
if (obj.UserName != null)
|
||||
{
|
||||
hashcode ^= obj.UserName.GetHashCode();
|
||||
}
|
||||
}
|
||||
return hashcode;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user