mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
Merge pull request #20 from Microsoft/feature/connectionCleanup
Cleaned up connection management code
This commit is contained in:
@@ -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.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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor
|
||||||
|
/// </summary>
|
||||||
|
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; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// URI identifying the owner/user of the connection. Could be a file, service, resource, etc.
|
||||||
|
/// </summary>
|
||||||
|
public string OwnerUri { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Factory used for creating the SQL connection associated with the connection info.
|
||||||
|
/// </summary>
|
||||||
|
public ISqlConnectionFactory Factory {get; private set;}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Properties used for creating/opening the SQL connection.
|
||||||
|
/// </summary>
|
||||||
|
public ConnectionDetails ConnectionDetails { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The connection to the SQL database that commands will be run against.
|
||||||
|
/// </summary>
|
||||||
|
public DbConnection SqlConnection { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,42 +5,16 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.Common;
|
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.SqlTools.EditorServices.Utility;
|
using Microsoft.SqlTools.EditorServices.Utility;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Hosting;
|
|
||||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
||||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.Connection
|
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>
|
/// <summary>
|
||||||
/// Main class for the Connection Management services
|
/// Main class for the Connection Management services
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,89 +0,0 @@
|
|||||||
//
|
|
||||||
// Copyright (c) Microsoft. All rights reserved.
|
|
||||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
||||||
//
|
|
||||||
|
|
||||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
|
||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Parameters for the Connect Request.
|
|
||||||
/// </summary>
|
|
||||||
public class ConnectParams
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// A URI identifying the owner of the connection. This will most commonly be a file in the workspace
|
|
||||||
/// or a virtual file representing an object in a database.
|
|
||||||
/// </summary>
|
|
||||||
public string OwnerUri { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// Contains the required parameters to initialize a connection to a database.
|
|
||||||
/// A connection will identified by its server name, database name and user name.
|
|
||||||
/// This may be changed in the future to support multiple connections with different
|
|
||||||
/// connection properties to the same database.
|
|
||||||
/// </summary>
|
|
||||||
public ConnectionDetails Connection { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Message format for the connection result response
|
|
||||||
/// </summary>
|
|
||||||
public class ConnectResponse
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// A GUID representing a unique connection ID
|
|
||||||
/// </summary>
|
|
||||||
public string ConnectionId { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets any connection error messages
|
|
||||||
/// </summary>
|
|
||||||
public string Messages { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Provides high level information about a connection.
|
|
||||||
/// </summary>
|
|
||||||
public class ConnectionSummary
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the connection server name
|
|
||||||
/// </summary>
|
|
||||||
public string ServerName { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the connection database name
|
|
||||||
/// </summary>
|
|
||||||
public string DatabaseName { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the connection user name
|
|
||||||
/// </summary>
|
|
||||||
public string UserName { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Message format for the initial connection request
|
|
||||||
/// </summary>
|
|
||||||
public class ConnectionDetails : ConnectionSummary
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the connection password
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public string Password { get; set; }
|
|
||||||
|
|
||||||
// TODO Handle full set of properties
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Connect request mapping entry
|
|
||||||
/// </summary>
|
|
||||||
public class ConnectionRequest
|
|
||||||
{
|
|
||||||
public static readonly
|
|
||||||
RequestType<ConnectParams, ConnectResponse> Type =
|
|
||||||
RequestType<ConnectParams, ConnectResponse>.Create("connection/connect");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Parameters for the Connect Request.
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectParams
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A URI identifying the owner of the connection. This will most commonly be a file in the workspace
|
||||||
|
/// or a virtual file representing an object in a database.
|
||||||
|
/// </summary>
|
||||||
|
public string OwnerUri { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Contains the required parameters to initialize a connection to a database.
|
||||||
|
/// A connection will identified by its server name, database name and user name.
|
||||||
|
/// This may be changed in the future to support multiple connections with different
|
||||||
|
/// connection properties to the same database.
|
||||||
|
/// </summary>
|
||||||
|
public ConnectionDetails Connection { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Message format for the connection result response
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A GUID representing a unique connection ID
|
||||||
|
/// </summary>
|
||||||
|
public string ConnectionId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets any connection error messages
|
||||||
|
/// </summary>
|
||||||
|
public string Messages { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// ConnectionChanged notification mapping entry
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectionChangedNotification
|
||||||
|
{
|
||||||
|
public static readonly
|
||||||
|
EventType<ConnectionChangedParams> Type =
|
||||||
|
EventType<ConnectionChangedParams>.Create("connection/connectionchanged");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,8 +3,6 @@
|
|||||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
//
|
//
|
||||||
|
|
||||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
|
||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -22,15 +20,4 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public ConnectionSummary Connection { get; set; }
|
public ConnectionSummary Connection { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// ConnectionChanged notification mapping entry
|
|
||||||
/// </summary>
|
|
||||||
public class ConnectionChangedNotification
|
|
||||||
{
|
|
||||||
public static readonly
|
|
||||||
EventType<ConnectionChangedParams> Type =
|
|
||||||
EventType<ConnectionChangedParams>.Create("connection/connectionchanged");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Message format for the initial connection request
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectionDetails : ConnectionSummary
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the connection password
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public string Password { get; set; }
|
||||||
|
|
||||||
|
// TODO Handle full set of properties
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Connect request mapping entry
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectionRequest
|
||||||
|
{
|
||||||
|
public static readonly
|
||||||
|
RequestType<ConnectParams, ConnectResponse> Type =
|
||||||
|
RequestType<ConnectParams, ConnectResponse>.Create("connection/connect");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Provides high level information about a connection.
|
||||||
|
/// </summary>
|
||||||
|
public class ConnectionSummary
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the connection server name
|
||||||
|
/// </summary>
|
||||||
|
public string ServerName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the connection database name
|
||||||
|
/// </summary>
|
||||||
|
public string DatabaseName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the connection user name
|
||||||
|
/// </summary>
|
||||||
|
public string UserName { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,8 +3,6 @@
|
|||||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
//
|
//
|
||||||
|
|
||||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
|
||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -18,14 +16,4 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string OwnerUri { get; set; }
|
public string OwnerUri { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Disconnect request mapping entry
|
|
||||||
/// </summary>
|
|
||||||
public class DisconnectRequest
|
|
||||||
{
|
|
||||||
public static readonly
|
|
||||||
RequestType<DisconnectParams, bool> Type =
|
|
||||||
RequestType<DisconnectParams, bool>.Create("connection/disconnect");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.Connection.Contracts
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Disconnect request mapping entry
|
||||||
|
/// </summary>
|
||||||
|
public class DisconnectRequest
|
||||||
|
{
|
||||||
|
public static readonly
|
||||||
|
RequestType<DisconnectParams, bool> Type =
|
||||||
|
RequestType<DisconnectParams, bool>.Create("connection/disconnect");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,8 +5,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
|
||||||
using System.Data.Common;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||||
@@ -16,159 +14,6 @@ using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
|||||||
|
|
||||||
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||||
{
|
{
|
||||||
internal class IntellisenseCache
|
|
||||||
{
|
|
||||||
// connection used to query for intellisense info
|
|
||||||
private DbConnection connection;
|
|
||||||
|
|
||||||
// number of documents (URI's) that are using the cache for the same database
|
|
||||||
// the autocomplete service uses this to remove unreferenced caches
|
|
||||||
public int ReferenceCount { get; set; }
|
|
||||||
|
|
||||||
public IntellisenseCache(ISqlConnectionFactory connectionFactory, ConnectionDetails connectionDetails)
|
|
||||||
{
|
|
||||||
ReferenceCount = 0;
|
|
||||||
DatabaseInfo = CopySummary(connectionDetails);
|
|
||||||
|
|
||||||
// TODO error handling on this. Intellisense should catch or else the service should handle
|
|
||||||
connection = connectionFactory.CreateSqlConnection(ConnectionService.BuildConnectionString(connectionDetails));
|
|
||||||
connection.Open();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Used to identify a database for which this cache is used
|
|
||||||
/// </summary>
|
|
||||||
public ConnectionSummary DatabaseInfo
|
|
||||||
{
|
|
||||||
get;
|
|
||||||
private set;
|
|
||||||
}
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the current autocomplete candidate list
|
|
||||||
/// </summary>
|
|
||||||
public IEnumerable<string> AutoCompleteList { get; private set; }
|
|
||||||
|
|
||||||
public async Task UpdateCache()
|
|
||||||
{
|
|
||||||
DbCommand command = connection.CreateCommand();
|
|
||||||
command.CommandText = "SELECT name FROM sys.tables";
|
|
||||||
command.CommandTimeout = 15;
|
|
||||||
command.CommandType = CommandType.Text;
|
|
||||||
var reader = await command.ExecuteReaderAsync();
|
|
||||||
|
|
||||||
List<string> results = new List<string>();
|
|
||||||
while (await reader.ReadAsync())
|
|
||||||
{
|
|
||||||
results.Add(reader[0].ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
AutoCompleteList = results;
|
|
||||||
await Task.FromResult(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<CompletionItem> GetAutoCompleteItems(TextDocumentPosition textDocumentPosition)
|
|
||||||
{
|
|
||||||
List<CompletionItem> completions = new List<CompletionItem>();
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
// Take a reference to the list at a point in time in case we update and replace the list
|
|
||||||
var suggestions = AutoCompleteList;
|
|
||||||
// the completion list will be null is user not connected to server
|
|
||||||
if (this.AutoCompleteList != null)
|
|
||||||
{
|
|
||||||
|
|
||||||
foreach (var autoCompleteItem in suggestions)
|
|
||||||
{
|
|
||||||
// convert the completion item candidates into CompletionItems
|
|
||||||
completions.Add(new CompletionItem()
|
|
||||||
{
|
|
||||||
Label = autoCompleteItem,
|
|
||||||
Kind = CompletionItemKind.Keyword,
|
|
||||||
Detail = autoCompleteItem + " details",
|
|
||||||
Documentation = autoCompleteItem + " documentation",
|
|
||||||
TextEdit = new TextEdit
|
|
||||||
{
|
|
||||||
NewText = autoCompleteItem,
|
|
||||||
Range = new Range
|
|
||||||
{
|
|
||||||
Start = new Position
|
|
||||||
{
|
|
||||||
Line = textDocumentPosition.Position.Line,
|
|
||||||
Character = textDocumentPosition.Position.Character
|
|
||||||
},
|
|
||||||
End = new Position
|
|
||||||
{
|
|
||||||
Line = textDocumentPosition.Position.Line,
|
|
||||||
Character = textDocumentPosition.Position.Character + 5
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// only show 50 items
|
|
||||||
if (++i == 50)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return completions;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static ConnectionSummary CopySummary(ConnectionSummary summary)
|
|
||||||
{
|
|
||||||
return new ConnectionSummary()
|
|
||||||
{
|
|
||||||
ServerName = summary.ServerName,
|
|
||||||
DatabaseName = summary.DatabaseName,
|
|
||||||
UserName = summary.UserName
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Main class for Autocomplete functionality
|
/// Main class for Autocomplete functionality
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) Microsoft. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
//
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||||
|
|
||||||
|
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
||||||
|
{
|
||||||
|
internal class IntellisenseCache
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// connection used to query for intellisense info
|
||||||
|
/// </summary>
|
||||||
|
private DbConnection connection;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Number of documents (URI's) that are using the cache for the same database.
|
||||||
|
/// The autocomplete service uses this to remove unreferenced caches.
|
||||||
|
/// </summary>
|
||||||
|
public int ReferenceCount { get; set; }
|
||||||
|
|
||||||
|
public IntellisenseCache(ISqlConnectionFactory connectionFactory, ConnectionDetails connectionDetails)
|
||||||
|
{
|
||||||
|
ReferenceCount = 0;
|
||||||
|
DatabaseInfo = connectionDetails.Clone();
|
||||||
|
|
||||||
|
// TODO error handling on this. Intellisense should catch or else the service should handle
|
||||||
|
connection = connectionFactory.CreateSqlConnection(ConnectionService.BuildConnectionString(connectionDetails));
|
||||||
|
connection.Open();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Used to identify a database for which this cache is used
|
||||||
|
/// </summary>
|
||||||
|
public ConnectionSummary DatabaseInfo
|
||||||
|
{
|
||||||
|
get;
|
||||||
|
private set;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the current autocomplete candidate list
|
||||||
|
/// </summary>
|
||||||
|
public IEnumerable<string> AutoCompleteList { get; private set; }
|
||||||
|
|
||||||
|
public async Task UpdateCache()
|
||||||
|
{
|
||||||
|
DbCommand command = connection.CreateCommand();
|
||||||
|
command.CommandText = "SELECT name FROM sys.tables";
|
||||||
|
command.CommandTimeout = 15;
|
||||||
|
command.CommandType = CommandType.Text;
|
||||||
|
var reader = await command.ExecuteReaderAsync();
|
||||||
|
|
||||||
|
List<string> results = new List<string>();
|
||||||
|
while (await reader.ReadAsync())
|
||||||
|
{
|
||||||
|
results.Add(reader[0].ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
AutoCompleteList = results;
|
||||||
|
await Task.FromResult(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<CompletionItem> GetAutoCompleteItems(TextDocumentPosition textDocumentPosition)
|
||||||
|
{
|
||||||
|
List<CompletionItem> completions = new List<CompletionItem>();
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
// Take a reference to the list at a point in time in case we update and replace the list
|
||||||
|
var suggestions = AutoCompleteList;
|
||||||
|
// the completion list will be null is user not connected to server
|
||||||
|
if (this.AutoCompleteList != null)
|
||||||
|
{
|
||||||
|
|
||||||
|
foreach (var autoCompleteItem in suggestions)
|
||||||
|
{
|
||||||
|
// convert the completion item candidates into CompletionItems
|
||||||
|
completions.Add(new CompletionItem()
|
||||||
|
{
|
||||||
|
Label = autoCompleteItem,
|
||||||
|
Kind = CompletionItemKind.Keyword,
|
||||||
|
Detail = autoCompleteItem + " details",
|
||||||
|
Documentation = autoCompleteItem + " documentation",
|
||||||
|
TextEdit = new TextEdit
|
||||||
|
{
|
||||||
|
NewText = autoCompleteItem,
|
||||||
|
Range = new Range
|
||||||
|
{
|
||||||
|
Start = new Position
|
||||||
|
{
|
||||||
|
Line = textDocumentPosition.Position.Line,
|
||||||
|
Character = textDocumentPosition.Position.Character
|
||||||
|
},
|
||||||
|
End = new Position
|
||||||
|
{
|
||||||
|
Line = textDocumentPosition.Position.Line,
|
||||||
|
Character = textDocumentPosition.Position.Character + 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// only show 50 items
|
||||||
|
if (++i == 50)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return completions;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user