Merge branch 'feature/connectUsingService' into feature/queryExecutionV1

This commit is contained in:
Benjamin Russell
2016-08-04 16:11:48 -07:00
13 changed files with 563 additions and 156 deletions

View File

@@ -9,89 +9,71 @@ 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.Hosting;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
{
/// <summary>
/// Main class for Autocomplete functionality
/// </summary>
public class AutoCompleteService
internal class IntellisenseCache
{
#region Singleton Instance Implementation
// connection used to query for intellisense info
private DbConnection connection;
/// <summary>
/// Singleton service instance
/// </summary>
private static Lazy<AutoCompleteService> instance
= new Lazy<AutoCompleteService>(() => new AutoCompleteService());
/// <summary>
/// Gets the singleton service instance
/// </summary>
public static AutoCompleteService Instance
public IntellisenseCache(ISqlConnectionFactory connectionFactory, ConnectionDetails connectionDetails)
{
get
{
return instance.Value;
}
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>
/// Default, parameterless constructor.
/// TODO: Figure out how to make this truely singleton even with dependency injection for tests
/// Used to identify a database for which this cache is used
/// </summary>
public AutoCompleteService()
{
public ConnectionSummary DatabaseInfo
{
get;
private set;
}
#endregion
/// <summary>
/// Gets the current autocomplete candidate list
/// </summary>
public IEnumerable<string> AutoCompleteList { get; private set; }
/// <summary>
/// Update the cached autocomplete candidate list when the user connects to a database
/// TODO: Update with refactoring/async
/// </summary>
/// <param name="connection"></param>
public async Task UpdateAutoCompleteCache(DbConnection connection)
public async Task UpdateCache()
{
DbCommand command = connection.CreateCommand();
command.CommandText = "SELECT name FROM sys.tables";
command.CommandTimeout = 15;
command.CommandType = CommandType.Text;
using (var reader = await command.ExecuteReaderAsync())
var reader = await command.ExecuteReaderAsync();
List<string> results = new List<string>();
while (await reader.ReadAsync())
{
List<string> results = new List<string>();
while (await reader.ReadAsync())
{
results.Add(reader[0].ToString());
}
AutoCompleteList = results;
results.Add(reader[0].ToString());
}
AutoCompleteList = results;
await Task.FromResult(0);
}
/// <summary>
/// Return the completion item list for the current text position
/// </summary>
/// <param name="textDocumentPosition"></param>
public CompletionItem[] GetCompletionItems(TextDocumentPosition textDocumentPosition)
public List<CompletionItem> GetAutoCompleteItems(TextDocumentPosition textDocumentPosition)
{
var completions = new List<CompletionItem>();
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 this.AutoCompleteList)
foreach (var autoCompleteItem in suggestions)
{
// convert the completion item candidates into CompletionItems
completions.Add(new CompletionItem()
@@ -126,8 +108,173 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
}
}
}
return completions.ToArray();
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>
/// Main class for Autocomplete functionality
/// </summary>
public class AutoCompleteService
{
#region Singleton Instance Implementation
/// <summary>
/// Singleton service instance
/// </summary>
private static Lazy<AutoCompleteService> instance
= new Lazy<AutoCompleteService>(() => new AutoCompleteService());
/// <summary>
/// Gets the singleton service instance
/// </summary>
public static AutoCompleteService Instance
{
get
{
return instance.Value;
}
}
/// <summary>
/// Default, parameterless constructor.
/// TODO: Figure out how to make this truely singleton even with dependency injection for tests
/// </summary>
public AutoCompleteService()
{
}
#endregion
// Dictionary of unique intellisense caches for each Connection
private Dictionary<ConnectionSummary, IntellisenseCache> caches =
new Dictionary<ConnectionSummary, IntellisenseCache>(new ConnectionSummaryComparer());
private ISqlConnectionFactory factory;
/// <summary>
/// Internal for testing purposes only
/// </summary>
internal ISqlConnectionFactory ConnectionFactory
{
get
{
// TODO consider protecting against multi-threaded access
if (factory == null)
{
factory = new SqlConnectionFactory();
}
return factory;
}
set
{
factory = value;
}
}
public void InitializeService(ServiceHost serviceHost)
{
// Register a callback for when a connection is created
ConnectionService.Instance.RegisterOnConnectionTask(UpdateAutoCompleteCache);
}
private async Task UpdateAutoCompleteCache(ConnectionInfo connectionInfo)
{
if (connectionInfo != null)
{
await UpdateAutoCompleteCache(connectionInfo.ConnectionDetails);
}
}
/// <summary>
/// Update the cached autocomplete candidate list when the user connects to a database
/// TODO: Update with refactoring/async
/// </summary>
/// <param name="details"></param>
public async Task UpdateAutoCompleteCache(ConnectionDetails details)
{
IntellisenseCache cache;
if (!caches.TryGetValue(details, out cache))
{
cache = new IntellisenseCache(ConnectionFactory, details);
caches[cache.DatabaseInfo] = cache;
}
await cache.UpdateCache();
}
/// <summary>
/// Return the completion item list for the current text position.
/// This method does not await cache builds since it expects to return quickly
/// </summary>
/// <param name="textDocumentPosition"></param>
public CompletionItem[] GetCompletionItems(TextDocumentPosition textDocumentPosition)
{
// Try to find a cache for the document's backing connection (if available)
// If we have a connection but no cache, we don't care - assuming the OnConnect and OnDisconnect listeners
// behave well, there should be a cache for any actively connected document. This also helps skip documents
// that are not backed by a SQL connection
ConnectionSummary connectionSummary;
IntellisenseCache cache;
if (ConnectionService.Instance.TryFindConnection(textDocumentPosition.Uri, out connectionSummary)
&& caches.TryGetValue(connectionSummary, out cache))
{
return cache.GetAutoCompleteItems(textDocumentPosition).ToArray();
}
return new CompletionItem[0];
}
}
}

View File

@@ -309,9 +309,9 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
/// Callback for when a user connection is done processing
/// </summary>
/// <param name="sqlConnection"></param>
public async Task OnConnection(DbConnection sqlConnection)
public async Task OnConnection(ConnectionInfo connectionInfo)
{
await AutoCompleteService.Instance.UpdateAutoCompleteCache(sqlConnection);
// TODO consider whether this is needed at all - currently AutoComplete service handles its own updating
await Task.FromResult(true);
}