diff --git a/.vscode/launch.json b/.vscode/launch.json
index e6ab5c0d..208aac21 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -18,6 +18,20 @@
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
+ {
+ "name": "Kusto service Launch (console)",
+ "type": "coreclr",
+ "request": "launch",
+ "preLaunchTask": "build",
+ // If you have changed target frameworks, make sure to update the program path.
+ "program": "${workspaceFolder}/src/Microsoft.Kusto.ServiceLayer/bin/Debug/netcoreapp3.1/MicrosoftKustoServiceLayer.dll",
+ "args": [],
+ "cwd": "${workspaceFolder}/src/Microsoft.Kusto.ServiceLayer",
+ // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
+ "console": "externalTerminal",
+ "stopAtEntry": false,
+ "internalConsoleOptions": "openOnSessionStart"
+ },
{
"name": ".NET Core Attach",
"type": "coreclr",
@@ -26,6 +40,15 @@
"requireExactSource": false,
"justMyCode": false,
"enableStepFiltering": false
+ },
+ {
+ "name": "Kusto Core Attach",
+ "type": "coreclr",
+ "request": "attach",
+ "processId": "${command:pickProcess}",
+ "requireExactSource": false,
+ "justMyCode": false,
+ "enableStepFiltering": false
}
,]
}
\ No newline at end of file
diff --git a/Packages.props b/Packages.props
index 352c5189..6b4e1116 100644
--- a/Packages.props
+++ b/Packages.props
@@ -20,6 +20,8 @@
+
+
diff --git a/sqltoolsservice.sln b/sqltoolsservice.sln
index f0803e43..fd58bf20 100644
--- a/sqltoolsservice.sln
+++ b/sqltoolsservice.sln
@@ -104,6 +104,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.SqlTools.ManagedB
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.SqlTools.Test.CompletionExtension", "test\Microsoft.SqlTools.Test.CompletionExtension\Microsoft.SqlTools.Test.CompletionExtension.csproj", "{0EC2B30C-0652-49AE-9594-85B3C3E9CA21}"
EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Kusto.ServiceLayer", "src\Microsoft.Kusto.ServiceLayer\Microsoft.Kusto.ServiceLayer.csproj", "{E0C941C8-91F2-4BE1-8B79-AC88EDB78729}"
+EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "azure-pipelines", "azure-pipelines", "{48D5C134-4091-438D-9D35-6EB8CF0D1DCC}"
ProjectSection(SolutionItems) = preProject
azure-pipelines\build.yml = azure-pipelines\build.yml
@@ -249,6 +251,12 @@ Global
{0EC2B30C-0652-49AE-9594-85B3C3E9CA21}.Integration|Any CPU.Build.0 = Debug|Any CPU
{0EC2B30C-0652-49AE-9594-85B3C3E9CA21}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0EC2B30C-0652-49AE-9594-85B3C3E9CA21}.Release|Any CPU.Build.0 = Release|Any CPU
+ {E0C941C8-91F2-4BE1-8B79-AC88EDB78729}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {E0C941C8-91F2-4BE1-8B79-AC88EDB78729}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {E0C941C8-91F2-4BE1-8B79-AC88EDB78729}.Integration|Any CPU.ActiveCfg = Debug|Any CPU
+ {E0C941C8-91F2-4BE1-8B79-AC88EDB78729}.Integration|Any CPU.Build.0 = Debug|Any CPU
+ {E0C941C8-91F2-4BE1-8B79-AC88EDB78729}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {E0C941C8-91F2-4BE1-8B79-AC88EDB78729}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -278,6 +286,7 @@ Global
{3F82F298-700A-48DF-8A69-D048DFBA782C} = {2BBD7364-054F-4693-97CD-1C395E3E84A9}
{D3696EFA-FB1E-4848-A726-FF7B168AFB96} = {AB9CA2B8-6F70-431C-8A1D-67479D8A7BE4}
{0EC2B30C-0652-49AE-9594-85B3C3E9CA21} = {AB9CA2B8-6F70-431C-8A1D-67479D8A7BE4}
+ {E0C941C8-91F2-4BE1-8B79-AC88EDB78729} = {2BBD7364-054F-4693-97CD-1C395E3E84A9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B31CDF4B-2851-45E5-8C5F-BE97125D9DD8}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Admin/AdminService.cs b/src/Microsoft.Kusto.ServiceLayer/Admin/AdminService.cs
new file mode 100644
index 00000000..3c86dbbd
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Admin/AdminService.cs
@@ -0,0 +1,136 @@
+//
+// 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;
+using System.Linq;
+using System.Threading.Tasks;
+using Microsoft.SqlTools.Hosting.Protocol;
+using Microsoft.Kusto.ServiceLayer.Admin.Contracts;
+using Microsoft.Kusto.ServiceLayer.Connection;
+using Microsoft.Kusto.ServiceLayer.DataSource;
+using Microsoft.Kusto.ServiceLayer.DataSource.Metadata;
+using Microsoft.Kusto.ServiceLayer.Hosting;
+using Microsoft.Kusto.ServiceLayer.Utility;
+
+namespace Microsoft.Kusto.ServiceLayer.Admin
+{
+ ///
+ /// Admin task service class
+ ///
+ public class AdminService
+ {
+ private static readonly Lazy instance = new Lazy(() => new AdminService());
+
+ private static ConnectionService connectionService = null;
+
+ ///
+ /// Default, parameterless constructor.
+ ///
+ internal AdminService()
+ {
+ }
+
+ ///
+ /// Internal for testing purposes only
+ ///
+ internal static ConnectionService ConnectionServiceInstance
+ {
+ get
+ {
+ if (AdminService.connectionService == null)
+ {
+ AdminService.connectionService = ConnectionService.Instance;
+ }
+ return AdminService.connectionService;
+ }
+
+ set
+ {
+ AdminService.connectionService = value;
+ }
+ }
+
+ ///
+ /// Gets the singleton instance object
+ ///
+ public static AdminService Instance
+ {
+ get { return instance.Value; }
+ }
+
+ ///
+ /// Initializes the service instance
+ ///
+ public void InitializeService(ServiceHost serviceHost)
+ {
+ serviceHost.SetRequestHandler(GetDatabaseInfoRequest.Type, HandleGetDatabaseInfoRequest);
+ }
+
+ ///
+ /// Handle get database info request
+ ///
+ internal static async Task HandleGetDatabaseInfoRequest(
+ GetDatabaseInfoParams databaseParams,
+ RequestContext requestContext)
+ {
+ try
+ {
+ Func requestHandler = async () =>
+ {
+ ConnectionInfo connInfo;
+ AdminService.ConnectionServiceInstance.TryFindConnection(
+ databaseParams.OwnerUri,
+ out connInfo);
+ DatabaseInfo info = null;
+
+ if (connInfo != null)
+ {
+ info = GetDatabaseInfo(connInfo);
+ }
+
+ await requestContext.SendResult(new GetDatabaseInfoResponse()
+ {
+ DatabaseInfo = info
+ });
+ };
+
+ Task task = Task.Run(async () => await requestHandler()).ContinueWithOnFaulted(async t =>
+ {
+ await requestContext.SendError(t.Exception.ToString());
+ });
+
+ }
+ catch (Exception ex)
+ {
+ await requestContext.SendError(ex.ToString());
+ }
+ }
+
+ ///
+ /// Return database info for a specific database
+ ///
+ ///
+ ///
+ internal static DatabaseInfo GetDatabaseInfo(ConnectionInfo connInfo)
+ {
+ if(!string.IsNullOrEmpty(connInfo.ConnectionDetails.DatabaseName)){
+ ReliableDataSourceConnection connection;
+ connInfo.TryGetConnection("Default", out connection);
+ IDataSource dataSource = connection.GetUnderlyingConnection();
+ DataSourceObjectMetadata objectMetadata = DataSourceFactory.CreateClusterMetadata(connInfo.ConnectionDetails.ServerName);
+
+ List metadata = dataSource.GetChildObjects(objectMetadata, true).ToList();
+ var databaseMetadata = metadata.Where(o => o.Name == connInfo.ConnectionDetails.DatabaseName);
+
+ List databaseInfo = DataSourceFactory.ConvertToDatabaseInfo(databaseMetadata);
+
+ return databaseInfo.ElementAtOrDefault(0);
+ }
+
+ return null;
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Admin/Contracts/DatabaseInfo.cs b/src/Microsoft.Kusto.ServiceLayer/Admin/Contracts/DatabaseInfo.cs
new file mode 100644
index 00000000..b03e6ed6
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Admin/Contracts/DatabaseInfo.cs
@@ -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.
+//
+
+using System.Collections.Generic;
+
+namespace Microsoft.Kusto.ServiceLayer.Admin.Contracts
+{
+ public class DatabaseInfo
+ {
+ ///
+ /// Gets or sets the options
+ ///
+ public Dictionary Options { get; set; }
+
+ public DatabaseInfo()
+ {
+ Options = new Dictionary();
+ }
+
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Admin/Contracts/GetDatabaseInfoRequest.cs b/src/Microsoft.Kusto.ServiceLayer/Admin/Contracts/GetDatabaseInfoRequest.cs
new file mode 100644
index 00000000..de1c5bea
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Admin/Contracts/GetDatabaseInfoRequest.cs
@@ -0,0 +1,41 @@
+//
+// 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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Admin.Contracts
+{
+ ///
+ /// Params for a get database info request
+ ///
+ public class GetDatabaseInfoParams
+ {
+ ///
+ /// Uri identifier for the connection to get the database info for
+ ///
+ public string OwnerUri { get; set; }
+ }
+
+ ///
+ /// Response object for get database info
+ ///
+ public class GetDatabaseInfoResponse
+ {
+ ///
+ /// The object containing the database info
+ ///
+ public DatabaseInfo DatabaseInfo { get; set; }
+ }
+
+ ///
+ /// Get database info request mapping
+ ///
+ public class GetDatabaseInfoRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("admin/getdatabaseinfo");
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/CancelTokenKey.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/CancelTokenKey.cs
new file mode 100644
index 00000000..5b046ad4
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/CancelTokenKey.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+using Microsoft.Kusto.ServiceLayer.Connection.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection
+{
+ ///
+ /// Used to uniquely identify a CancellationTokenSource associated with both
+ /// a string URI and a string connection type.
+ ///
+ public class CancelTokenKey : CancelConnectParams, IEquatable
+ {
+ public override bool Equals(object obj)
+ {
+ CancelTokenKey other = obj as CancelTokenKey;
+ if (other == null)
+ {
+ return false;
+ }
+
+ return other.OwnerUri == OwnerUri && other.Type == Type;
+ }
+
+ public bool Equals(CancelTokenKey obj)
+ {
+ return obj.OwnerUri == OwnerUri && obj.Type == Type;
+ }
+
+ public override int GetHashCode()
+ {
+ return OwnerUri.GetHashCode() ^ Type.GetHashCode();
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionInfo.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionInfo.cs
new file mode 100644
index 00000000..fdaa7381
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionInfo.cs
@@ -0,0 +1,162 @@
+//
+// 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.Kusto.ServiceLayer.Connection.Contracts;
+using Microsoft.SqlTools.Utility;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection
+{
+ ///
+ /// Information pertaining to a unique connection instance.
+ ///
+ public class ConnectionInfo
+ {
+ ///
+ /// Constructor
+ ///
+ public ConnectionInfo(IDataSourceConnectionFactory 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 IDataSourceConnectionFactory 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 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 ReliableDataSourceConnection 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, ReliableDataSourceConnection 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);
+ ReliableDataSourceConnection connection;
+ ConnectionTypeToConnectionMap.TryRemove(connectionType, out connection);
+ }
+
+ ///
+ /// Removes all DbConnection instances held by this object
+ ///
+ public void RemoveAllConnections()
+ {
+ foreach (var type in AllConnectionTypes)
+ {
+ ReliableDataSourceConnection connection;
+ ConnectionTypeToConnectionMap.TryRemove(type, out connection);
+ }
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionProviderOptionsHelper.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionProviderOptionsHelper.cs
new file mode 100644
index 00000000..ede01b0b
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionProviderOptionsHelper.cs
@@ -0,0 +1,302 @@
+//
+// 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.Hosting.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection
+{
+ ///
+ /// Helper class for providing metadata about connection options
+ ///
+ internal class ConnectionProviderOptionsHelper
+ {
+ internal static ConnectionProviderOptions BuildConnectionProviderOptions()
+ {
+ return new ConnectionProviderOptions
+ {
+ Options = new ConnectionOption[]
+ {
+ new ConnectionOption
+ {
+ Name = "server",
+ DisplayName = "Server name",
+ Description = "Name of the SQL Server instance",
+ ValueType = ConnectionOption.ValueTypeString,
+ SpecialValueType = ConnectionOption.SpecialValueServerName,
+ IsIdentity = true,
+ IsRequired = true,
+ GroupName = "Source"
+ },
+ new ConnectionOption
+ {
+ Name = "database",
+ DisplayName = "Database name",
+ Description = "The name of the initial catalog or database int the data source",
+ ValueType = ConnectionOption.ValueTypeString,
+ SpecialValueType = ConnectionOption.SpecialValueDatabaseName,
+ IsIdentity = true,
+ IsRequired = false,
+ GroupName = "Source"
+ },
+ new ConnectionOption
+ {
+ Name = "authenticationType",
+ DisplayName = "Authentication type",
+ Description = "Specifies the method of authenticating with SQL Server",
+ ValueType = ConnectionOption.ValueTypeCategory,
+ SpecialValueType = ConnectionOption.SpecialValueAuthType,
+ CategoryValues = new CategoryValue[]
+ { new CategoryValue { DisplayName = "SQL Login", Name = "SqlLogin" },
+ new CategoryValue { DisplayName = "Windows Authentication", Name = "Integrated" },
+ new CategoryValue { DisplayName = "Azure Active Directory - Universal with MFA support", Name = "AzureMFA" }
+ },
+ IsIdentity = true,
+ IsRequired = true,
+ GroupName = "Security"
+ },
+ new ConnectionOption
+ {
+ Name = "user",
+ DisplayName = "User name",
+ Description = "Indicates the user ID to be used when connecting to the data source",
+ ValueType = ConnectionOption.ValueTypeString,
+ SpecialValueType = ConnectionOption.SpecialValueUserName,
+ IsIdentity = true,
+ IsRequired = true,
+ GroupName = "Security"
+ },
+ new ConnectionOption
+ {
+ Name = "password",
+ DisplayName = "Password",
+ Description = "Indicates the password to be used when connecting to the data source",
+ ValueType = ConnectionOption.ValueTypePassword,
+ SpecialValueType = ConnectionOption.SpecialValuePasswordName,
+ IsIdentity = true,
+ IsRequired = true,
+ GroupName = "Security"
+ },
+ new ConnectionOption
+ {
+ Name = "applicationIntent",
+ DisplayName = "Application intent",
+ Description = "Declares the application workload type when connecting to a server",
+ ValueType = ConnectionOption.ValueTypeCategory,
+ CategoryValues = new CategoryValue[] {
+ new CategoryValue { Name = "ReadWrite", DisplayName = "ReadWrite" },
+ new CategoryValue {Name = "ReadOnly", DisplayName = "ReadOnly" }
+ },
+ GroupName = "Initialization"
+ },
+ new ConnectionOption
+ {
+ Name = "asynchronousProcessing",
+ DisplayName = "Asynchronous processing enabled",
+ Description = "When true, enables usage of the Asynchronous functionality in the .Net Framework Data Provider",
+ ValueType = ConnectionOption.ValueTypeBoolean,
+ GroupName = "Initialization"
+ },
+ new ConnectionOption
+ {
+ Name = "connectTimeout",
+ DisplayName = "Connect timeout",
+ Description =
+ "The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error",
+ ValueType = ConnectionOption.ValueTypeNumber,
+ DefaultValue = "15",
+ GroupName = "Initialization"
+ },
+ new ConnectionOption
+ {
+ Name = "currentLanguage",
+ DisplayName = "Current language",
+ Description = "The SQL Server language record name",
+ ValueType = ConnectionOption.ValueTypeString,
+ GroupName = "Initialization"
+ },
+ new ConnectionOption
+ {
+ Name = "columnEncryptionSetting",
+ DisplayName = "Column encryption setting",
+ Description = "Default column encryption setting for all the commands on the connection",
+ ValueType = ConnectionOption.ValueTypeCategory,
+ GroupName = "Security",
+ CategoryValues = new CategoryValue[] {
+ new CategoryValue { Name = "Disabled" },
+ new CategoryValue {Name = "Enabled" }
+ }
+ },
+ new ConnectionOption
+ {
+ Name = "encrypt",
+ DisplayName = "Encrypt",
+ Description =
+ "When true, SQL Server uses SSL encryption for all data sent between the client and server if the servers has a certificate installed",
+ GroupName = "Security",
+ ValueType = ConnectionOption.ValueTypeBoolean
+ },
+ new ConnectionOption
+ {
+ Name = "persistSecurityInfo",
+ DisplayName = "Persist security info",
+ Description = "When false, security-sensitive information, such as the password, is not returned as part of the connection",
+ GroupName = "Security",
+ ValueType = ConnectionOption.ValueTypeBoolean
+ },
+ new ConnectionOption
+ {
+ Name = "trustServerCertificate",
+ DisplayName = "Trust server certificate",
+ Description = "When true (and encrypt=true), SQL Server uses SSL encryption for all data sent between the client and server without validating the server certificate",
+ GroupName = "Security",
+ ValueType = ConnectionOption.ValueTypeBoolean
+ },
+ new ConnectionOption
+ {
+ Name = "attachedDBFileName",
+ DisplayName = "Attached DB file name",
+ Description = "The name of the primary file, including the full path name, of an attachable database",
+ ValueType = ConnectionOption.ValueTypeString,
+ GroupName = "Source"
+ },
+ new ConnectionOption
+ {
+ Name = "contextConnection",
+ DisplayName = "Context connection",
+ Description = "When true, indicates the connection should be from the SQL server context. Available only when running in the SQL Server process",
+ ValueType = ConnectionOption.ValueTypeBoolean,
+ GroupName = "Source"
+ },
+ new ConnectionOption
+ {
+ Name = "port",
+ DisplayName = "Port",
+ ValueType = ConnectionOption.ValueTypeNumber
+ },
+ new ConnectionOption
+ {
+ Name = "connectRetryCount",
+ DisplayName = "Connect retry count",
+ Description = "Number of attempts to restore connection",
+ ValueType = ConnectionOption.ValueTypeNumber,
+ DefaultValue = "1",
+ GroupName = "Connection Resiliency"
+ },
+ new ConnectionOption
+ {
+ Name = "connectRetryInterval",
+ DisplayName = "Connect retry interval",
+ Description = "Delay between attempts to restore connection",
+ ValueType = ConnectionOption.ValueTypeNumber,
+ DefaultValue = "10",
+ GroupName = "Connection Resiliency"
+
+ },
+ new ConnectionOption
+ {
+ Name = "applicationName",
+ DisplayName = "Application name",
+ Description = "The name of the application",
+ ValueType = ConnectionOption.ValueTypeString,
+ GroupName = "Context",
+ SpecialValueType = ConnectionOption.SpecialValueAppName
+ },
+ new ConnectionOption
+ {
+ Name = "workstationId",
+ DisplayName = "Workstation Id",
+ Description = "The name of the workstation connecting to SQL Server",
+ ValueType = ConnectionOption.ValueTypeString,
+ GroupName = "Context"
+ },
+ new ConnectionOption
+ {
+ Name = "pooling",
+ DisplayName = "Pooling",
+ Description = "When true, the connection object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool",
+ ValueType = ConnectionOption.ValueTypeBoolean,
+ GroupName = "Pooling"
+ },
+ new ConnectionOption
+ {
+ Name = "maxPoolSize",
+ DisplayName = "Max pool size",
+ Description = "The maximum number of connections allowed in the pool",
+ ValueType = ConnectionOption.ValueTypeNumber,
+ GroupName = "Pooling"
+ },
+ new ConnectionOption
+ {
+ Name = "minPoolSize",
+ DisplayName = "Min pool size",
+ Description = "The minimum number of connections allowed in the pool",
+ ValueType = ConnectionOption.ValueTypeNumber,
+ GroupName = "Pooling"
+ },
+ new ConnectionOption
+ {
+ Name = "loadBalanceTimeout",
+ DisplayName = "Load balance timeout",
+ Description = "The minimum amount of time (in seconds) for this connection to live in the pool before being destroyed",
+ ValueType = ConnectionOption.ValueTypeNumber,
+ GroupName = "Pooling"
+ },
+ new ConnectionOption
+ {
+ Name = "replication",
+ DisplayName = "Replication",
+ Description = "Used by SQL Server in Replication",
+ ValueType = ConnectionOption.ValueTypeBoolean,
+ GroupName = "Replication"
+ },
+ new ConnectionOption
+ {
+ Name = "attachDbFilename",
+ DisplayName = "Attach DB filename",
+ ValueType = ConnectionOption.ValueTypeString
+ },
+ new ConnectionOption
+ {
+ Name = "failoverPartner",
+ DisplayName = "Failover partner",
+ Description = "the name or network address of the instance of SQL Server that acts as a failover partner",
+ ValueType = ConnectionOption.ValueTypeString,
+ GroupName = " Source"
+ },
+ new ConnectionOption
+ {
+ Name = "multiSubnetFailover",
+ DisplayName = "Multi subnet failover",
+ ValueType = ConnectionOption.ValueTypeBoolean
+ },
+ new ConnectionOption
+ {
+ Name = "multipleActiveResultSets",
+ DisplayName = "Multiple active result sets",
+ Description = "When true, multiple result sets can be returned and read from one connection",
+ ValueType = ConnectionOption.ValueTypeBoolean,
+ GroupName = "Advanced"
+ },
+ new ConnectionOption
+ {
+ Name = "packetSize",
+ DisplayName = "Packet size",
+ Description = "Size in bytes of the network packets used to communicate with an instance of SQL Server",
+ ValueType = ConnectionOption.ValueTypeNumber,
+ GroupName = "Advanced"
+ },
+ new ConnectionOption
+ {
+ Name = "typeSystemVersion",
+ DisplayName = "Type system version",
+ Description = "Indicates which server type system then provider will expose through the DataReader",
+ ValueType = ConnectionOption.ValueTypeString,
+ GroupName = "Advanced"
+ }
+ }
+ };
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionService.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionService.cs
new file mode 100644
index 00000000..da287c1d
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionService.cs
@@ -0,0 +1,1513 @@
+//
+// 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.Globalization;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using Microsoft.Data.SqlClient;
+using Microsoft.SqlTools.Hosting.Protocol;
+using Microsoft.SqlServer.Management.Common;
+using Microsoft.Kusto.ServiceLayer.Connection.Contracts;
+using Microsoft.Kusto.ServiceLayer.Admin.Contracts;
+using Microsoft.Kusto.ServiceLayer.LanguageServices;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
+using Microsoft.Kusto.ServiceLayer.Utility;
+using Microsoft.SqlTools.Utility;
+using System.Diagnostics;
+using Microsoft.Kusto.ServiceLayer.DataSource;
+using Microsoft.Kusto.ServiceLayer.DataSource.Metadata;
+using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection
+{
+ ///
+ /// Main class for the Connection Management services
+ ///
+ public class ConnectionService
+ {
+ public const string AdminConnectionPrefix = "ADMIN:";
+ internal const string PasswordPlaceholder = "******";
+ private const string SqlAzureEdition = "SQL Azure";
+
+ ///
+ /// Singleton service instance
+ ///
+ private static readonly Lazy instance
+ = new Lazy(() => new ConnectionService());
+
+ ///
+ /// Gets the singleton service instance
+ ///
+ public static ConnectionService Instance => instance.Value;
+
+ ///
+ /// The SQL connection factory object
+ ///
+ private IDataSourceConnectionFactory connectionFactory;
+
+ private DatabaseLocksManager lockedDatabaseManager;
+
+ ///
+ /// A map containing all CancellationTokenSource objects that are associated with a given URI/ConnectionType pair.
+ /// Entries in this map correspond to ReliableDataSourceClient instances that are in the process of connecting.
+ ///
+ private readonly ConcurrentDictionary cancelTupleToCancellationTokenSourceMap =
+ new ConcurrentDictionary();
+
+ private readonly object cancellationTokenSourceLock = new object();
+
+ private ConcurrentDictionary connectedQueues = new ConcurrentDictionary();
+
+ ///
+ /// Map from script URIs to ConnectionInfo objects
+ /// This is internal for testing access only
+ ///
+ internal Dictionary OwnerToConnectionMap { get; } = new Dictionary();
+
+ ///
+ /// Database Lock manager instance
+ ///
+ internal DatabaseLocksManager LockedDatabaseManager
+ {
+ get
+ {
+ if (lockedDatabaseManager == null)
+ {
+ lockedDatabaseManager = DatabaseLocksManager.Instance;
+ }
+ return lockedDatabaseManager;
+ }
+ set
+ {
+ this.lockedDatabaseManager = value;
+ }
+ }
+
+ ///
+ /// Service host object for sending/receiving requests/events.
+ /// Internal for testing purposes.
+ ///
+ internal IProtocolEndpoint ServiceHost { get; set; }
+
+ ///
+ /// Gets the connection queue
+ ///
+ internal IConnectedBindingQueue ConnectionQueue
+ {
+ get
+ {
+ return this.GetConnectedQueue("Default");
+ }
+ }
+
+
+ ///
+ /// Default constructor should be private since it's a singleton class, but we need a constructor
+ /// for use in unit test mocking.
+ ///
+ public ConnectionService()
+ {
+ var defaultQueue = new ConnectedBindingQueue(needsMetadata: false);
+ connectedQueues.AddOrUpdate("Default", defaultQueue, (key, old) => defaultQueue);
+ this.LockedDatabaseManager.ConnectionService = this;
+ }
+
+ ///
+ /// Returns a connection queue for given type
+ ///
+ ///
+ ///
+ public IConnectedBindingQueue GetConnectedQueue(string type)
+ {
+ IConnectedBindingQueue connectedBindingQueue;
+ if (connectedQueues.TryGetValue(type, out connectedBindingQueue))
+ {
+ return connectedBindingQueue;
+ }
+ return null;
+ }
+
+ ///
+ /// Returns all the connection queues
+ ///
+ public IEnumerable ConnectedQueues
+ {
+ get
+ {
+ return this.connectedQueues.Values;
+ }
+ }
+
+ ///
+ /// Register a new connection queue if not already registered
+ ///
+ ///
+ ///
+ public virtual void RegisterConnectedQueue(string type, IConnectedBindingQueue connectedQueue)
+ {
+ if (!connectedQueues.ContainsKey(type))
+ {
+ connectedQueues.AddOrUpdate(type, connectedQueue, (key, old) => connectedQueue);
+ }
+ }
+
+ ///
+ /// Callback for onconnection handler
+ ///
+ ///
+ public delegate Task OnConnectionHandler(ConnectionInfo info);
+
+ ///
+ /// Callback for ondisconnect handler
+ ///
+ public delegate Task OnDisconnectHandler(IConnectionSummary summary, string ownerUri);
+
+ ///
+ /// List of onconnection handlers
+ ///
+ private readonly List onConnectionActivities = new List();
+
+ ///
+ /// List of ondisconnect handlers
+ ///
+ private readonly List onDisconnectActivities = new List();
+
+ ///
+ /// Gets the SQL connection factory instance
+ ///
+ public IDataSourceConnectionFactory ConnectionFactory
+ {
+ get
+ {
+ if (this.connectionFactory == null)
+ {
+ this.connectionFactory = new DataSourceConnectionFactory();
+ }
+ return this.connectionFactory;
+ }
+
+ internal set { this.connectionFactory = value; }
+ }
+
+ ///
+ /// Test constructor that injects dependency interfaces
+ ///
+ ///
+ public ConnectionService(IDataSourceConnectionFactory testFactory) => this.connectionFactory = testFactory;
+
+ // Attempts to link a URI to an actively used connection for this URI
+ public virtual bool TryFindConnection(string ownerUri, out ConnectionInfo connectionInfo) => this.OwnerToConnectionMap.TryGetValue(ownerUri, out connectionInfo);
+
+ ///
+ /// Validates the given ConnectParams object.
+ ///
+ /// The params to validate
+ /// A ConnectionCompleteParams object upon validation error,
+ /// null upon validation success
+ public ConnectionCompleteParams ValidateConnectParams(ConnectParams connectionParams)
+ {
+ string paramValidationErrorMessage;
+ if (connectionParams == null)
+ {
+ return new ConnectionCompleteParams
+ {
+ Messages = SR.ConnectionServiceConnectErrorNullParams
+ };
+ }
+ if (!connectionParams.IsValid(out paramValidationErrorMessage))
+ {
+ return new ConnectionCompleteParams
+ {
+ OwnerUri = connectionParams.OwnerUri,
+ Messages = paramValidationErrorMessage
+ };
+ }
+
+ // return null upon success
+ return null;
+ }
+
+ ///
+ /// Open a connection with the specified ConnectParams
+ ///
+ public virtual async Task Connect(ConnectParams connectionParams)
+ {
+ // Validate parameters
+ ConnectionCompleteParams validationResults = ValidateConnectParams(connectionParams);
+ if (validationResults != null)
+ {
+ return validationResults;
+ }
+
+ TrySetConnectionType(connectionParams);
+
+ connectionParams.Connection.ApplicationName = GetApplicationNameWithFeature(connectionParams.Connection.ApplicationName, connectionParams.Purpose);
+ // If there is no ConnectionInfo in the map, create a new ConnectionInfo,
+ // but wait until later when we are connected to add it to the map.
+ ConnectionInfo connectionInfo;
+ bool connectionChanged = false;
+ if (!OwnerToConnectionMap.TryGetValue(connectionParams.OwnerUri, out connectionInfo))
+ {
+ connectionInfo = new ConnectionInfo(ConnectionFactory, connectionParams.OwnerUri, connectionParams.Connection);
+ }
+ else if (IsConnectionChanged(connectionParams, connectionInfo))
+ {
+ // We are actively changing the connection information for this connection. We must disconnect
+ // all active connections, since it represents a full context change
+ connectionChanged = true;
+ }
+
+ DisconnectExistingConnectionIfNeeded(connectionParams, connectionInfo, disconnectAll: connectionChanged);
+
+ if (connectionChanged)
+ {
+ connectionInfo = new ConnectionInfo(ConnectionFactory, connectionParams.OwnerUri, connectionParams.Connection);
+ }
+
+ // Try to open a connection with the given ConnectParams
+ ConnectionCompleteParams response = await TryOpenConnection(connectionInfo, connectionParams);
+ if (response != null)
+ {
+ return response;
+ }
+
+ // If this is the first connection for this URI, add the ConnectionInfo to the map
+ bool addToMap = connectionChanged || !OwnerToConnectionMap.ContainsKey(connectionParams.OwnerUri);
+ if (addToMap)
+ {
+ OwnerToConnectionMap[connectionParams.OwnerUri] = connectionInfo;
+ }
+
+ // Return information about the connected SQL Server instance
+ ConnectionCompleteParams completeParams = GetConnectionCompleteParams(connectionParams.Type, connectionInfo);
+ // Invoke callback notifications
+ InvokeOnConnectionActivities(connectionInfo, connectionParams);
+
+ TryCloseConnectionTemporaryConnection(connectionParams, connectionInfo);
+
+ return completeParams;
+ }
+
+ private void TryCloseConnectionTemporaryConnection(ConnectParams connectionParams, ConnectionInfo connectionInfo)
+ {
+ try
+ {
+ if (connectionParams.Purpose == ConnectionType.ObjectExplorer || connectionParams.Purpose == ConnectionType.Dashboard || connectionParams.Purpose == ConnectionType.GeneralConnection)
+ {
+ ReliableDataSourceConnection connection;
+ string type = connectionParams.Type;
+ if (connectionInfo.TryGetConnection(type, out connection))
+ {
+ // OE doesn't need to keep the connection open
+ connection.Close();
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ Logger.Write(TraceEventType.Information, "Failed to close temporary connections. error: " + ex.Message);
+ }
+ }
+
+ private static string GetApplicationNameWithFeature(string applicationName, string featureName)
+ {
+ string appNameWithFeature = applicationName;
+
+ if (!string.IsNullOrWhiteSpace(applicationName) && !string.IsNullOrWhiteSpace(featureName))
+ {
+ int index = applicationName.IndexOf('-');
+ string appName = applicationName;
+ if (index > 0)
+ {
+ appName = applicationName.Substring(0, index);
+ }
+ appNameWithFeature = $"{appName}-{featureName}";
+ }
+
+ return appNameWithFeature;
+ }
+
+ private void TrySetConnectionType(ConnectParams connectionParams)
+ {
+ if (connectionParams != null && connectionParams.Type == ConnectionType.Default && !string.IsNullOrWhiteSpace(connectionParams.OwnerUri))
+ {
+ if (connectionParams.OwnerUri.ToLowerInvariant().StartsWith("dashboard://"))
+ {
+ connectionParams.Purpose = ConnectionType.Dashboard;
+ }
+ else if (connectionParams.OwnerUri.ToLowerInvariant().StartsWith("connection://"))
+ {
+ connectionParams.Purpose = ConnectionType.GeneralConnection;
+ }
+ }
+ else if (connectionParams != null)
+ {
+ connectionParams.Purpose = connectionParams.Type;
+ }
+ }
+
+ private bool IsConnectionChanged(ConnectParams connectionParams, ConnectionInfo connectionInfo)
+ {
+ if (connectionInfo.HasConnectionType(connectionParams.Type)
+ && !connectionInfo.ConnectionDetails.IsComparableTo(connectionParams.Connection))
+ {
+ return true;
+ }
+ return false;
+ }
+
+ private bool IsDefaultConnectionType(string connectionType)
+ {
+ return string.IsNullOrEmpty(connectionType) || ConnectionType.Default.Equals(connectionType, StringComparison.CurrentCultureIgnoreCase);
+ }
+
+ private void DisconnectExistingConnectionIfNeeded(ConnectParams connectionParams, ConnectionInfo connectionInfo, bool disconnectAll)
+ {
+ // Resolve if it is an existing connection
+ // Disconnect active connection if the URI is already connected for this connection type
+ ReliableDataSourceConnection existingConnection;
+ if (connectionInfo.TryGetConnection(connectionParams.Type, out existingConnection))
+ {
+ var disconnectParams = new DisconnectParams()
+ {
+ OwnerUri = connectionParams.OwnerUri,
+ Type = disconnectAll ? null : connectionParams.Type
+ };
+ Disconnect(disconnectParams);
+ }
+ }
+
+ ///
+ /// Creates a ConnectionCompleteParams as a response to a successful connection.
+ /// Also sets the DatabaseName and IsAzure properties of ConnectionInfo.
+ ///
+ /// A ConnectionCompleteParams in response to the successful connection
+ private ConnectionCompleteParams GetConnectionCompleteParams(string connectionType, ConnectionInfo connectionInfo)
+ {
+ ConnectionCompleteParams response = new ConnectionCompleteParams { OwnerUri = connectionInfo.OwnerUri, Type = connectionType };
+
+ try
+ {
+ ReliableDataSourceConnection connection;
+ connectionInfo.TryGetConnection(connectionType, out connection);
+
+ // Update with the actual database name in connectionInfo and result
+ // Doing this here as we know the connection is open - expect to do this only on connecting
+ connectionInfo.ConnectionDetails.DatabaseName = connection.Database;
+ if (!string.IsNullOrEmpty(connectionInfo.ConnectionDetails.ConnectionString))
+ {
+ // If the connection was set up with a connection string, use the connection string to get the details
+ var connectionString = new SqlConnectionStringBuilder(connection.ConnectionString);
+ response.ConnectionSummary = new ConnectionSummary
+ {
+ ServerName = connectionString.DataSource,
+ DatabaseName = connectionString.InitialCatalog,
+ UserName = connectionString.UserID
+ };
+ }
+ else
+ {
+ response.ConnectionSummary = new ConnectionSummary
+ {
+ ServerName = connectionInfo.ConnectionDetails.ServerName,
+ DatabaseName = connectionInfo.ConnectionDetails.DatabaseName,
+ UserName = connectionInfo.ConnectionDetails.UserName
+ };
+ }
+
+ response.ConnectionId = connectionInfo.ConnectionId.ToString();
+
+ var reliableConnection = connection as ReliableDataSourceConnection;
+ IDataSource dataSource = reliableConnection.GetUnderlyingConnection();
+ DataSourceObjectMetadata clusterMetadata = DataSourceFactory.CreateClusterMetadata(connectionInfo.ConnectionDetails.ServerName);
+
+ DiagnosticsInfo clusterDiagnostics = dataSource.GetDiagnostics(clusterMetadata);
+ ReliableConnectionHelper.ServerInfo serverInfo = DataSourceFactory.ConvertToServerinfoFormat(DataSourceType.Kusto, clusterDiagnostics);
+
+ response.ServerInfo = new ServerInfo
+ {
+ Options = serverInfo.Options // Server properties are shown on "manage" dashboard.
+ };
+ connectionInfo.IsCloud = response.ServerInfo.IsCloud;
+ connectionInfo.MajorVersion = response.ServerInfo.ServerMajorVersion;
+ }
+ catch (Exception ex)
+ {
+ response.Messages = ex.ToString();
+ }
+
+ return response;
+ }
+
+ ///
+ /// Tries to create and open a connection with the given ConnectParams.
+ ///
+ /// null upon success, a ConnectionCompleteParams detailing the error upon failure
+ private async Task TryOpenConnection(ConnectionInfo connectionInfo, ConnectParams connectionParams)
+ {
+ CancellationTokenSource source = null;
+ ReliableDataSourceConnection connection = null;
+ CancelTokenKey cancelKey = new CancelTokenKey { OwnerUri = connectionParams.OwnerUri, Type = connectionParams.Type };
+ ConnectionCompleteParams response = new ConnectionCompleteParams { OwnerUri = connectionInfo.OwnerUri, Type = connectionParams.Type };
+ bool? currentPooling = connectionInfo.ConnectionDetails.Pooling;
+
+ try
+ {
+ connectionInfo.ConnectionDetails.Pooling = false;
+ // build the connection string from the input parameters
+ string connectionString = BuildConnectionString(connectionInfo.ConnectionDetails);
+
+ // create a sql connection instance
+ connection = connectionInfo.Factory.CreateDataSourceConnection(connectionString, connectionInfo.ConnectionDetails.AzureAccountToken);
+ connectionInfo.AddConnection(connectionParams.Type, connection);
+
+ // Add a cancellation token source so that the connection OpenAsync() can be cancelled
+ source = new CancellationTokenSource();
+ // Locking here to perform two operations as one atomic operation
+ lock (cancellationTokenSourceLock)
+ {
+ // If the URI is currently connecting from a different request, cancel it before we try to connect
+ CancellationTokenSource currentSource;
+ if (cancelTupleToCancellationTokenSourceMap.TryGetValue(cancelKey, out currentSource))
+ {
+ currentSource.Cancel();
+ }
+ cancelTupleToCancellationTokenSourceMap[cancelKey] = source;
+ }
+
+ // Open the connection
+ await connection.OpenAsync(source.Token);
+ }
+ catch (SqlException ex)
+ {
+ response.ErrorNumber = ex.Number;
+ response.ErrorMessage = ex.Message;
+ response.Messages = ex.ToString();
+ return response;
+ }
+ catch (OperationCanceledException)
+ {
+ // OpenAsync was cancelled
+ response.Messages = SR.ConnectionServiceConnectionCanceled;
+ return response;
+ }
+ catch (Exception ex)
+ {
+ response.ErrorMessage = ex.Message;
+ response.Messages = ex.ToString();
+ return response;
+ }
+ finally
+ {
+ // Remove our cancellation token from the map since we're no longer connecting
+ // Using a lock here to perform two operations as one atomic operation
+ lock (cancellationTokenSourceLock)
+ {
+ // Only remove the token from the map if it is the same one created by this request
+ CancellationTokenSource sourceValue;
+ if (cancelTupleToCancellationTokenSourceMap.TryGetValue(cancelKey, out sourceValue) && sourceValue == source)
+ {
+ cancelTupleToCancellationTokenSourceMap.TryRemove(cancelKey, out sourceValue);
+ }
+ source?.Dispose();
+ }
+ if (connectionInfo != null && connectionInfo.ConnectionDetails != null)
+ {
+ connectionInfo.ConnectionDetails.Pooling = currentPooling;
+ }
+ }
+
+ // Return null upon success
+ return null;
+ }
+
+ ///
+ /// Gets the existing connection with the given URI and connection type string. If none exists,
+ /// creates a new connection. This cannot be used to create a default connection or to create a
+ /// connection if a default connection does not exist.
+ ///
+ /// URI identifying the resource mapped to this connection
+ ///
+ /// What the purpose for this connection is. A single resource
+ /// such as a SQL file may have multiple connections - one for Intellisense, another for query execution
+ ///
+ ///
+ /// Workaround for .Net Core clone connection issues: should persist security be used so that
+ /// when SMO clones connections it can do so without breaking on SQL Password connections.
+ /// This should be removed once the core issue is resolved and clone works as expected
+ ///
+ /// A DB connection for the connection type requested
+ public virtual async Task GetOrOpenConnection(string ownerUri, string connectionType, bool alwaysPersistSecurity = false)
+ {
+ Validate.IsNotNullOrEmptyString(nameof(ownerUri), ownerUri);
+ Validate.IsNotNullOrEmptyString(nameof(connectionType), connectionType);
+
+ // Try to get the ConnectionInfo, if it exists
+ ConnectionInfo connectionInfo;
+ if (!OwnerToConnectionMap.TryGetValue(ownerUri, out connectionInfo))
+ {
+ throw new ArgumentOutOfRangeException(SR.ConnectionServiceListDbErrorNotConnected(ownerUri));
+ }
+
+ // Make sure a default connection exists
+ ReliableDataSourceConnection connection;
+ ReliableDataSourceConnection defaultConnection;
+ if (!connectionInfo.TryGetConnection(ConnectionType.Default, out defaultConnection))
+ {
+ throw new InvalidOperationException(SR.ConnectionServiceDbErrorDefaultNotConnected(ownerUri));
+ }
+
+ if (IsDedicatedAdminConnection(connectionInfo.ConnectionDetails))
+ {
+ // Since this is a dedicated connection only 1 is allowed at any time. Return the default connection for use in the requested action
+ connection = defaultConnection;
+ }
+ else
+ {
+ // Try to get the ReliableDataSourceClient and create if it doesn't already exist
+ if (!connectionInfo.TryGetConnection(connectionType, out connection) && ConnectionType.Default != connectionType)
+ {
+ connection = await TryOpenConnectionForConnectionType(ownerUri, connectionType, alwaysPersistSecurity, connectionInfo);
+ }
+ }
+
+ return connection;
+ }
+
+ private async Task TryOpenConnectionForConnectionType(string ownerUri, string connectionType,
+ bool alwaysPersistSecurity, ConnectionInfo connectionInfo)
+ {
+ // If the ReliableDataSourceClient does not exist and is not the default connection, create one.
+ // We can't create the default (initial) connection here because we won't have a ConnectionDetails
+ // if Connect() has not yet been called.
+ bool? originalPersistSecurityInfo = connectionInfo.ConnectionDetails.PersistSecurityInfo;
+ if (alwaysPersistSecurity)
+ {
+ connectionInfo.ConnectionDetails.PersistSecurityInfo = true;
+ }
+ ConnectParams connectParams = new ConnectParams
+ {
+ OwnerUri = ownerUri,
+ Connection = connectionInfo.ConnectionDetails,
+ Type = connectionType
+ };
+ try
+ {
+ await Connect(connectParams);
+ }
+ finally
+ {
+ connectionInfo.ConnectionDetails.PersistSecurityInfo = originalPersistSecurityInfo;
+ }
+
+ ReliableDataSourceConnection connection;
+ connectionInfo.TryGetConnection(connectionType, out connection);
+ return connection;
+ }
+
+ ///
+ /// Cancel a connection that is in the process of opening.
+ ///
+ public bool CancelConnect(CancelConnectParams cancelParams)
+ {
+ // Validate parameters
+ if (cancelParams == null || string.IsNullOrEmpty(cancelParams.OwnerUri))
+ {
+ return false;
+ }
+
+ CancelTokenKey cancelKey = new CancelTokenKey
+ {
+ OwnerUri = cancelParams.OwnerUri,
+ Type = cancelParams.Type
+ };
+
+ // Cancel any current connection attempts for this URI
+ CancellationTokenSource source;
+ if (cancelTupleToCancellationTokenSourceMap.TryGetValue(cancelKey, out source))
+ {
+ try
+ {
+ source.Cancel();
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ return false;
+ }
+
+ ///
+ /// Close a connection with the specified connection details.
+ ///
+ public virtual bool Disconnect(DisconnectParams disconnectParams)
+ {
+ // Validate parameters
+ if (disconnectParams == null || string.IsNullOrEmpty(disconnectParams.OwnerUri))
+ {
+ return false;
+ }
+
+ // Cancel if we are in the middle of connecting
+ if (CancelConnections(disconnectParams.OwnerUri, disconnectParams.Type))
+ {
+ return false;
+ }
+
+ // Lookup the ConnectionInfo owned by the URI
+ ConnectionInfo info;
+ if (!OwnerToConnectionMap.TryGetValue(disconnectParams.OwnerUri, out info))
+ {
+ return false;
+ }
+
+ // Call Close() on the connections we want to disconnect
+ // If no connections were located, return false
+ if (!CloseConnections(info, disconnectParams.Type))
+ {
+ return false;
+ }
+
+ // Remove the disconnected connections from the ConnectionInfo map
+ if (disconnectParams.Type == null)
+ {
+ info.RemoveAllConnections();
+ }
+ else
+ {
+ info.RemoveConnection(disconnectParams.Type);
+ }
+
+ // If the ConnectionInfo has no more connections, remove the ConnectionInfo
+ if (info.CountConnections == 0)
+ {
+ OwnerToConnectionMap.Remove(disconnectParams.OwnerUri);
+ }
+
+ // Handle Telemetry disconnect events if we are disconnecting the default connection
+ if (disconnectParams.Type == null || disconnectParams.Type == ConnectionType.Default)
+ {
+ HandleDisconnectTelemetry(info);
+ InvokeOnDisconnectionActivities(info);
+ }
+
+ // Return true upon success
+ return true;
+ }
+
+ ///
+ /// Cancel connections associated with the given ownerUri.
+ /// If connectionType is not null, cancel the connection with the given connectionType
+ /// If connectionType is null, cancel all pending connections associated with ownerUri.
+ ///
+ /// true if a single pending connection associated with the non-null connectionType was
+ /// found and cancelled, false otherwise
+ private bool CancelConnections(string ownerUri, string connectionType)
+ {
+ // Cancel the connection of the given type
+ if (connectionType != null)
+ {
+ // If we are trying to disconnect a specific connection and it was just cancelled,
+ // this will return true
+ return CancelConnect(new CancelConnectParams() { OwnerUri = ownerUri, Type = connectionType });
+ }
+
+ // Cancel all pending connections
+ foreach (var entry in cancelTupleToCancellationTokenSourceMap)
+ {
+ string entryConnectionUri = entry.Key.OwnerUri;
+ string entryConnectionType = entry.Key.Type;
+ if (ownerUri.Equals(entryConnectionUri))
+ {
+ CancelConnect(new CancelConnectParams() { OwnerUri = ownerUri, Type = entryConnectionType });
+ }
+ }
+
+ return false;
+ }
+
+ ///
+ /// Closes DbConnections associated with the given ConnectionInfo.
+ /// If connectionType is not null, closes the ReliableDataSourceClient with the type given by connectionType.
+ /// If connectionType is null, closes all DbConnections.
+ ///
+ /// true if connections were found and attempted to be closed,
+ /// false if no connections were found
+ private bool CloseConnections(ConnectionInfo connectionInfo, string connectionType)
+ {
+ ICollection connectionsToDisconnect = new List();
+ if (connectionType == null)
+ {
+ connectionsToDisconnect = connectionInfo.AllConnections;
+ }
+ else
+ {
+ // Make sure there is an existing connection of this type
+ ReliableDataSourceConnection connection;
+ if (!connectionInfo.TryGetConnection(connectionType, out connection))
+ {
+ return false;
+ }
+ connectionsToDisconnect.Add(connection);
+ }
+
+ if (connectionsToDisconnect.Count == 0)
+ {
+ return false;
+ }
+
+ foreach (ReliableDataSourceConnection connection in connectionsToDisconnect)
+ {
+ try
+ {
+ connection.Close();
+ }
+ catch (Exception)
+ {
+ // Ignore
+ }
+ }
+
+ return true;
+ }
+
+ ///
+ /// List all databases on the server specified
+ ///
+ public ListDatabasesResponse ListDatabases(ListDatabasesParams listDatabasesParams)
+ {
+ // Verify parameters
+ var owner = listDatabasesParams.OwnerUri;
+ if (string.IsNullOrEmpty(owner))
+ {
+ throw new ArgumentException(SR.ConnectionServiceListDbErrorNullOwnerUri);
+ }
+
+ // Use the existing connection as a base for the search
+ ConnectionInfo info;
+ if (!TryFindConnection(owner, out info))
+ {
+ throw new Exception(SR.ConnectionServiceListDbErrorNotConnected(owner));
+ }
+ ConnectionDetails connectionDetails = info.ConnectionDetails.Clone();
+
+ IDataSource dataSource = OpenDataSourceConnection(info);
+ DataSourceObjectMetadata objectMetadata = DataSourceFactory.CreateClusterMetadata(info.ConnectionDetails.ServerName);
+
+ ListDatabasesResponse response = new ListDatabasesResponse();
+
+ // Mainly used by "manage" dashboard
+ if(listDatabasesParams.IncludeDetails.HasTrue()){
+ IEnumerable databaseMetadataInfo = dataSource.GetChildObjects(objectMetadata, true);
+ List metadata = DataSourceFactory.ConvertToDatabaseInfo(databaseMetadataInfo);
+ response.Databases = metadata.ToArray();
+
+ return response;
+ }
+
+ IEnumerable databaseMetadata = dataSource.GetChildObjects(objectMetadata);
+ if(databaseMetadata != null) response.DatabaseNames = databaseMetadata.Select(objMeta => objMeta.PrettyName).ToArray();
+ return response;
+ }
+
+ public void InitializeService(IProtocolEndpoint serviceHost)
+ {
+ this.ServiceHost = serviceHost;
+
+ // Register request and event handlers with the Service Host
+ serviceHost.SetRequestHandler(ConnectionRequest.Type, HandleConnectRequest);
+ serviceHost.SetRequestHandler(CancelConnectRequest.Type, HandleCancelConnectRequest);
+ serviceHost.SetRequestHandler(DisconnectRequest.Type, HandleDisconnectRequest);
+ serviceHost.SetRequestHandler(ListDatabasesRequest.Type, HandleListDatabasesRequest);
+ serviceHost.SetRequestHandler(ChangeDatabaseRequest.Type, HandleChangeDatabaseRequest);
+ serviceHost.SetRequestHandler(GetConnectionStringRequest.Type, HandleGetConnectionStringRequest);
+ serviceHost.SetRequestHandler(BuildConnectionInfoRequest.Type, HandleBuildConnectionInfoRequest);
+ }
+
+ ///
+ /// Add a new method to be called when the onconnection request is submitted
+ ///
+ ///
+ public void RegisterOnConnectionTask(OnConnectionHandler activity)
+ {
+ onConnectionActivities.Add(activity);
+ }
+
+ ///
+ /// Add a new method to be called when the ondisconnect request is submitted
+ ///
+ public void RegisterOnDisconnectTask(OnDisconnectHandler activity)
+ {
+ onDisconnectActivities.Add(activity);
+ }
+
+ ///
+ /// Handle new connection requests
+ ///
+ ///
+ ///
+ ///
+ protected async Task HandleConnectRequest(
+ ConnectParams connectParams,
+ RequestContext requestContext)
+ {
+ Logger.Write(TraceEventType.Verbose, "HandleConnectRequest");
+
+ try
+ {
+ RunConnectRequestHandlerTask(connectParams);
+ await requestContext.SendResult(true);
+ }
+ catch
+ {
+ await requestContext.SendResult(false);
+ }
+ }
+
+ private void RunConnectRequestHandlerTask(ConnectParams connectParams)
+ {
+ // create a task to connect asynchronously so that other requests are not blocked in the meantime
+ Task.Run(async () =>
+ {
+ try
+ {
+ // result is null if the ConnectParams was successfully validated
+ ConnectionCompleteParams result = ValidateConnectParams(connectParams);
+ if (result != null)
+ {
+ await ServiceHost.SendEvent(ConnectionCompleteNotification.Type, result);
+ return;
+ }
+
+ // open connection based on request details
+ result = await Connect(connectParams);
+ await ServiceHost.SendEvent(ConnectionCompleteNotification.Type, result);
+ }
+ catch (Exception ex)
+ {
+ ConnectionCompleteParams result = new ConnectionCompleteParams()
+ {
+ Messages = ex.ToString()
+ };
+ await ServiceHost.SendEvent(ConnectionCompleteNotification.Type, result);
+ }
+ }).ContinueWithOnFaulted(null);
+ }
+
+ ///
+ /// Handle cancel connect requests
+ ///
+ protected async Task HandleCancelConnectRequest(
+ CancelConnectParams cancelParams,
+ RequestContext requestContext)
+ {
+ Logger.Write(TraceEventType.Verbose, "HandleCancelConnectRequest");
+
+ try
+ {
+ bool result = CancelConnect(cancelParams);
+ await requestContext.SendResult(result);
+ }
+ catch (Exception ex)
+ {
+ await requestContext.SendError(ex.ToString());
+ }
+ }
+
+ ///
+ /// Handle disconnect requests
+ ///
+ protected async Task HandleDisconnectRequest(
+ DisconnectParams disconnectParams,
+ RequestContext requestContext)
+ {
+ Logger.Write(TraceEventType.Verbose, "HandleDisconnectRequest");
+
+ try
+ {
+ bool result = Instance.Disconnect(disconnectParams);
+ await requestContext.SendResult(result);
+ }
+ catch (Exception ex)
+ {
+ await requestContext.SendError(ex.ToString());
+ }
+
+ }
+
+ ///
+ /// Handle requests to list databases on the current server
+ ///
+ protected async Task HandleListDatabasesRequest(
+ ListDatabasesParams listDatabasesParams,
+ RequestContext requestContext)
+ {
+ Logger.Write(TraceEventType.Verbose, "ListDatabasesRequest");
+
+ try
+ {
+ ListDatabasesResponse result = ListDatabases(listDatabasesParams);
+ await requestContext.SendResult(result);
+ }
+ catch (Exception ex)
+ {
+ await requestContext.SendError(ex.ToString());
+ }
+ }
+
+ ///
+ /// Checks if a ConnectionDetails object represents a DAC connection
+ ///
+ ///
+ public static bool IsDedicatedAdminConnection(ConnectionDetails connectionDetails)
+ {
+ Validate.IsNotNull(nameof(connectionDetails), connectionDetails);
+ SqlConnectionStringBuilder builder = CreateConnectionStringBuilder(connectionDetails);
+ string serverName = builder.DataSource;
+ return serverName != null && serverName.StartsWith(AdminConnectionPrefix, StringComparison.OrdinalIgnoreCase);
+ }
+
+ ///
+ /// Build a connection string from a connection details instance
+ ///
+ ///
+ public static string BuildConnectionString(ConnectionDetails connectionDetails)
+ {
+ return CreateConnectionStringBuilder(connectionDetails).ToString();
+ }
+
+ ///
+ /// Build a connection string builder a connection details instance
+ ///
+ ///
+ public static SqlConnectionStringBuilder CreateConnectionStringBuilder(ConnectionDetails connectionDetails)
+ {
+ SqlConnectionStringBuilder connectionBuilder;
+
+ // If connectionDetails has a connection string already, use it to initialize the connection builder, then override any provided options.
+ // Otherwise use the server name, username, and password from the connection details.
+ if (!string.IsNullOrEmpty(connectionDetails.ConnectionString))
+ {
+ connectionBuilder = new SqlConnectionStringBuilder(connectionDetails.ConnectionString);
+ }
+ else
+ {
+ // add alternate port to data source property if provided
+ string dataSource = !connectionDetails.Port.HasValue
+ ? connectionDetails.ServerName
+ : string.Format("{0},{1}", connectionDetails.ServerName, connectionDetails.Port.Value);
+
+ connectionBuilder = new SqlConnectionStringBuilder
+ {
+ ["Data Source"] = dataSource,
+ ["User Id"] = connectionDetails.UserName,
+ ["Password"] = connectionDetails.Password
+ };
+ }
+
+ // Check for any optional parameters
+ if (!string.IsNullOrEmpty(connectionDetails.DatabaseName))
+ {
+ connectionBuilder["Initial Catalog"] = connectionDetails.DatabaseName;
+ }
+ if (!string.IsNullOrEmpty(connectionDetails.AuthenticationType))
+ {
+ switch (connectionDetails.AuthenticationType)
+ {
+ case "Integrated":
+ connectionBuilder.IntegratedSecurity = true;
+ break;
+ case "SqlLogin":
+ break;
+ case "AzureMFA":
+ connectionBuilder.UserID = "";
+ connectionBuilder.Password = "";
+ break;
+ default:
+ throw new ArgumentException(SR.ConnectionServiceConnStringInvalidAuthType(connectionDetails.AuthenticationType));
+ }
+ }
+ if (connectionDetails.Encrypt.HasValue)
+ {
+ connectionBuilder.Encrypt = connectionDetails.Encrypt.Value;
+ }
+ if (connectionDetails.TrustServerCertificate.HasValue)
+ {
+ connectionBuilder.TrustServerCertificate = connectionDetails.TrustServerCertificate.Value;
+ }
+ if (connectionDetails.PersistSecurityInfo.HasValue)
+ {
+ connectionBuilder.PersistSecurityInfo = connectionDetails.PersistSecurityInfo.Value;
+ }
+ if (connectionDetails.ConnectTimeout.HasValue)
+ {
+ connectionBuilder.ConnectTimeout = connectionDetails.ConnectTimeout.Value;
+ }
+ if (connectionDetails.ConnectRetryCount.HasValue)
+ {
+ connectionBuilder.ConnectRetryCount = connectionDetails.ConnectRetryCount.Value;
+ }
+ if (connectionDetails.ConnectRetryInterval.HasValue)
+ {
+ connectionBuilder.ConnectRetryInterval = connectionDetails.ConnectRetryInterval.Value;
+ }
+ if (!string.IsNullOrEmpty(connectionDetails.ApplicationName))
+ {
+ connectionBuilder.ApplicationName = connectionDetails.ApplicationName;
+ }
+ if (!string.IsNullOrEmpty(connectionDetails.WorkstationId))
+ {
+ connectionBuilder.WorkstationID = connectionDetails.WorkstationId;
+ }
+ if (!string.IsNullOrEmpty(connectionDetails.ApplicationIntent))
+ {
+ ApplicationIntent intent;
+ switch (connectionDetails.ApplicationIntent)
+ {
+ case "ReadOnly":
+ intent = ApplicationIntent.ReadOnly;
+ break;
+ case "ReadWrite":
+ intent = ApplicationIntent.ReadWrite;
+ break;
+ default:
+ throw new ArgumentException(SR.ConnectionServiceConnStringInvalidIntent(connectionDetails.ApplicationIntent));
+ }
+ connectionBuilder.ApplicationIntent = intent;
+ }
+ if (!string.IsNullOrEmpty(connectionDetails.CurrentLanguage))
+ {
+ connectionBuilder.CurrentLanguage = connectionDetails.CurrentLanguage;
+ }
+ if (connectionDetails.Pooling.HasValue)
+ {
+ connectionBuilder.Pooling = connectionDetails.Pooling.Value;
+ }
+ if (connectionDetails.MaxPoolSize.HasValue)
+ {
+ connectionBuilder.MaxPoolSize = connectionDetails.MaxPoolSize.Value;
+ }
+ if (connectionDetails.MinPoolSize.HasValue)
+ {
+ connectionBuilder.MinPoolSize = connectionDetails.MinPoolSize.Value;
+ }
+ if (connectionDetails.LoadBalanceTimeout.HasValue)
+ {
+ connectionBuilder.LoadBalanceTimeout = connectionDetails.LoadBalanceTimeout.Value;
+ }
+ if (connectionDetails.Replication.HasValue)
+ {
+ connectionBuilder.Replication = connectionDetails.Replication.Value;
+ }
+ if (!string.IsNullOrEmpty(connectionDetails.AttachDbFilename))
+ {
+ connectionBuilder.AttachDBFilename = connectionDetails.AttachDbFilename;
+ }
+ if (!string.IsNullOrEmpty(connectionDetails.FailoverPartner))
+ {
+ connectionBuilder.FailoverPartner = connectionDetails.FailoverPartner;
+ }
+ if (connectionDetails.MultiSubnetFailover.HasValue)
+ {
+ connectionBuilder.MultiSubnetFailover = connectionDetails.MultiSubnetFailover.Value;
+ }
+ if (connectionDetails.MultipleActiveResultSets.HasValue)
+ {
+ connectionBuilder.MultipleActiveResultSets = connectionDetails.MultipleActiveResultSets.Value;
+ }
+ if (connectionDetails.PacketSize.HasValue)
+ {
+ connectionBuilder.PacketSize = connectionDetails.PacketSize.Value;
+ }
+ if (!string.IsNullOrEmpty(connectionDetails.TypeSystemVersion))
+ {
+ connectionBuilder.TypeSystemVersion = connectionDetails.TypeSystemVersion;
+ }
+ connectionBuilder.Pooling = false;
+
+ return connectionBuilder;
+ }
+
+ ///
+ /// Handles a request to get a connection string for the provided connection
+ ///
+ public async Task HandleGetConnectionStringRequest(
+ GetConnectionStringParams connStringParams,
+ RequestContext requestContext)
+ {
+ await Task.Run(async () =>
+ {
+ string connectionString = string.Empty;
+ ConnectionInfo info;
+ if (TryFindConnection(connStringParams.OwnerUri, out info))
+ {
+ try
+ {
+ if (!connStringParams.IncludePassword)
+ {
+ info.ConnectionDetails.Password = ConnectionService.PasswordPlaceholder;
+ }
+
+ info.ConnectionDetails.ApplicationName = "sqlops-connection-string";
+
+ connectionString = BuildConnectionString(info.ConnectionDetails);
+ }
+ catch (Exception e)
+ {
+ await requestContext.SendError(e.ToString());
+ }
+ }
+
+ await requestContext.SendResult(connectionString);
+ });
+ }
+
+ ///
+ /// Handles a request to serialize a connection string
+ ///
+ public async Task HandleBuildConnectionInfoRequest(
+ string connectionString,
+ RequestContext requestContext)
+ {
+ await Task.Run(async () =>
+ {
+ try
+ {
+ await requestContext.SendResult(ParseConnectionString(connectionString));
+ }
+ catch (Exception)
+ {
+ // If theres an error in the parse, it means we just can't parse, so we return undefined
+ // rather than an error.
+ await requestContext.SendResult(null);
+ }
+ });
+ }
+
+ public ConnectionDetails ParseConnectionString(string connectionString)
+ {
+ SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
+ ConnectionDetails details = new ConnectionDetails()
+ {
+ ApplicationIntent = builder.ApplicationIntent.ToString(),
+ ApplicationName = builder.ApplicationName,
+ AttachDbFilename = builder.AttachDBFilename,
+ AuthenticationType = builder.IntegratedSecurity ? "Integrated" : "SqlLogin",
+ ConnectRetryCount = builder.ConnectRetryCount,
+ ConnectRetryInterval = builder.ConnectRetryInterval,
+ ConnectTimeout = builder.ConnectTimeout,
+ CurrentLanguage = builder.CurrentLanguage,
+ DatabaseName = builder.InitialCatalog,
+ Encrypt = builder.Encrypt,
+ FailoverPartner = builder.FailoverPartner,
+ LoadBalanceTimeout = builder.LoadBalanceTimeout,
+ MaxPoolSize = builder.MaxPoolSize,
+ MinPoolSize = builder.MinPoolSize,
+ MultipleActiveResultSets = builder.MultipleActiveResultSets,
+ MultiSubnetFailover = builder.MultiSubnetFailover,
+ PacketSize = builder.PacketSize,
+ Password = !builder.IntegratedSecurity ? builder.Password : string.Empty,
+ PersistSecurityInfo = builder.PersistSecurityInfo,
+ Pooling = builder.Pooling,
+ Replication = builder.Replication,
+ ServerName = builder.DataSource,
+ TrustServerCertificate = builder.TrustServerCertificate,
+ TypeSystemVersion = builder.TypeSystemVersion,
+ UserName = builder.UserID,
+ WorkstationId = builder.WorkstationID,
+ };
+
+ return details;
+ }
+
+ ///
+ /// Handles a request to change the database for a connection
+ ///
+ public async Task HandleChangeDatabaseRequest(
+ ChangeDatabaseParams changeDatabaseParams,
+ RequestContext requestContext)
+ {
+ await requestContext.SendResult(ChangeConnectionDatabaseContext(changeDatabaseParams.OwnerUri, changeDatabaseParams.NewDatabase, true));
+ }
+
+ ///
+ /// Change the database context of a connection.
+ ///
+ /// URI of the owner of the connection
+ /// Name of the database to change the connection to
+ public bool ChangeConnectionDatabaseContext(string ownerUri, string newDatabaseName, bool force = false)
+ {
+ ConnectionInfo info;
+ if (TryFindConnection(ownerUri, out info))
+ {
+ try
+ {
+ info.ConnectionDetails.DatabaseName = newDatabaseName;
+
+ foreach (string key in info.AllConnectionTypes)
+ {
+ ReliableDataSourceConnection conn;
+ info.TryGetConnection(key, out conn);
+ if (conn != null && conn.Database != newDatabaseName)
+ {
+ if (info.IsCloud && force)
+ {
+ conn.Close();
+ conn.Dispose();
+ info.RemoveConnection(key);
+
+ string connectionString = BuildConnectionString(info.ConnectionDetails);
+
+ // create a sql connection instance
+ ReliableDataSourceConnection connection = info.Factory.CreateDataSourceConnection(connectionString, info.ConnectionDetails.AzureAccountToken);
+ connection.Open();
+ info.AddConnection(key, connection);
+ }
+ else
+ {
+ conn.ChangeDatabase(newDatabaseName);
+ }
+ }
+
+ }
+
+ // Fire a connection changed event
+ ConnectionChangedParams parameters = new ConnectionChangedParams();
+ IConnectionSummary summary = info.ConnectionDetails;
+ parameters.Connection = summary.Clone();
+ parameters.OwnerUri = ownerUri;
+ ServiceHost.SendEvent(ConnectionChangedNotification.Type, parameters);
+ return true;
+ }
+ catch (Exception e)
+ {
+ Logger.Write(
+ TraceEventType.Error,
+ string.Format(
+ "Exception caught while trying to change database context to [{0}] for OwnerUri [{1}]. Exception:{2}",
+ newDatabaseName,
+ ownerUri,
+ e.ToString())
+ );
+ }
+ }
+ return false;
+ }
+
+ ///
+ /// Invokes the initial on-connect activities if the provided ConnectParams represents the default
+ /// connection.
+ ///
+ private void InvokeOnConnectionActivities(ConnectionInfo connectionInfo, ConnectParams connectParams)
+ {
+ if (connectParams.Type != ConnectionType.Default && connectParams.Type != ConnectionType.GeneralConnection)
+ {
+ return;
+ }
+
+ foreach (var activity in this.onConnectionActivities)
+ {
+ // not awaiting here to allow handlers to run in the background
+ activity(connectionInfo);
+ }
+ }
+
+ ///
+ /// Invokes the final on-disconnect activities if the provided DisconnectParams represents the default
+ /// connection or is null - representing that all connections are being disconnected.
+ ///
+ private void InvokeOnDisconnectionActivities(ConnectionInfo connectionInfo)
+ {
+ foreach (var activity in this.onDisconnectActivities)
+ {
+ activity(connectionInfo.ConnectionDetails, connectionInfo.OwnerUri);
+ }
+ }
+
+ ///
+ /// Handles the Telemetry events that occur upon disconnect.
+ ///
+ ///
+ private void HandleDisconnectTelemetry(ConnectionInfo connectionInfo)
+ {
+ if (ServiceHost != null)
+ {
+ try
+ {
+ // Send a telemetry notification for intellisense performance metrics
+ ServiceHost.SendEvent(TelemetryNotification.Type, new TelemetryParams()
+ {
+ Params = new TelemetryProperties
+ {
+ Properties = new Dictionary
+ {
+ { TelemetryPropertyNames.IsAzure, connectionInfo.IsCloud.ToOneOrZeroString() }
+ },
+ EventName = TelemetryEventNames.IntellisenseQuantile,
+ Measures = connectionInfo.IntellisenseMetrics.Quantile
+ }
+ });
+ }
+ catch (Exception ex)
+ {
+ Logger.Write(TraceEventType.Verbose, "Could not send Connection telemetry event " + ex.ToString());
+ }
+ }
+ }
+
+ ///
+ /// Create and open a new SqlConnection from a ConnectionInfo object
+ /// Note: we need to audit all uses of this method to determine why we're
+ /// bypassing normal ConnectionService connection management
+ ///
+ /// The connection info to connect with
+ /// A plaintext string that will be included in the application name for the connection
+ /// A SqlConnection created with the given connection info
+ internal static SqlConnection OpenSqlConnection(ConnectionInfo connInfo, string featureName = null)
+ {
+ try
+ {
+ // capture original values
+ int? originalTimeout = connInfo.ConnectionDetails.ConnectTimeout;
+ bool? originalPersistSecurityInfo = connInfo.ConnectionDetails.PersistSecurityInfo;
+ bool? originalPooling = connInfo.ConnectionDetails.Pooling;
+
+ // increase the connection timeout to at least 30 seconds and and build connection string
+ connInfo.ConnectionDetails.ConnectTimeout = Math.Max(30, originalTimeout ?? 0);
+ // enable PersistSecurityInfo to handle issues in SMO where the connection context is lost in reconnections
+ connInfo.ConnectionDetails.PersistSecurityInfo = true;
+ // turn off connection pool to avoid hold locks on server resources after calling SqlConnection Close method
+ connInfo.ConnectionDetails.Pooling = false;
+ connInfo.ConnectionDetails.ApplicationName = GetApplicationNameWithFeature(connInfo.ConnectionDetails.ApplicationName, featureName);
+
+ // generate connection string
+ string connectionString = BuildConnectionString(connInfo.ConnectionDetails);
+
+ // restore original values
+ connInfo.ConnectionDetails.ConnectTimeout = originalTimeout;
+ connInfo.ConnectionDetails.PersistSecurityInfo = originalPersistSecurityInfo;
+ connInfo.ConnectionDetails.Pooling = originalPooling;
+
+ // open a dedicated binding server connection
+ using (SqlConnection sqlConn = new SqlConnection(connectionString))
+ {
+ // Fill in Azure authentication token if needed
+ if (connInfo.ConnectionDetails.AzureAccountToken != null)
+ {
+ sqlConn.AccessToken = connInfo.ConnectionDetails.AzureAccountToken;
+ }
+
+ sqlConn.Open();
+ return sqlConn;
+ }
+ }
+ catch (Exception ex)
+ {
+ string error = string.Format(CultureInfo.InvariantCulture,
+ "Failed opening a SqlConnection: error:{0} inner:{1} stacktrace:{2}",
+ ex.Message, ex.InnerException != null ? ex.InnerException.Message : string.Empty, ex.StackTrace);
+ Logger.Write(TraceEventType.Error, error);
+ }
+
+ return null;
+ }
+
+ ///
+ /// Create and open a new SqlConnection from a ConnectionInfo object
+ /// Note: we need to audit all uses of this method to determine why we're
+ /// bypassing normal ConnectionService connection management
+ ///
+ /// The connection info to connect with
+ /// A plaintext string that will be included in the application name for the connection
+ /// A SqlConnection created with the given connection info
+ internal static IDataSource OpenDataSourceConnection(ConnectionInfo connInfo, string featureName = null)
+ {
+ try
+ {
+ // generate connection string
+ string connectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
+
+ // TODOKusto: Pass in type of DataSource needed to make this generic. Hard coded to Kusto right now.
+ return DataSourceFactory.Create(DataSourceType.Kusto, connectionString, connInfo.ConnectionDetails.AzureAccountToken);
+ }
+ catch (Exception ex)
+ {
+ string error = string.Format(CultureInfo.InvariantCulture,
+ "Failed opening a DataSource of type {0}: error:{1} inner:{2} stacktrace:{3}",
+ DataSourceType.Kusto, ex.Message, ex.InnerException != null ? ex.InnerException.Message : string.Empty, ex.StackTrace);
+ Logger.Write(TraceEventType.Error, error);
+ }
+
+ return null;
+ }
+
+ ///
+ /// Create and open a new ServerConnection from a ConnectionInfo object.
+ /// This calls ConnectionService.OpenSqlConnection and then creates a
+ /// ServerConnection from it.
+ ///
+ /// The connection info to connect with
+ /// A plaintext string that will be included in the application name for the connection
+ /// A ServerConnection (wrapping a SqlConnection) created with the given connection info
+ internal static ServerConnection OpenServerConnection(ConnectionInfo connInfo, string featureName = null)
+ {
+ SqlConnection sqlConnection = OpenSqlConnection(connInfo, featureName);
+
+ return connInfo.ConnectionDetails.AzureAccountToken != null
+ ? new ServerConnection(sqlConnection, new AzureAccessToken(connInfo.ConnectionDetails.AzureAccountToken))
+ : new ServerConnection(sqlConnection);
+ }
+
+ public static void EnsureConnectionIsOpen(ReliableDataSourceConnection conn, bool forceReopen = false)
+ {
+ // verify that the connection is open
+ if (forceReopen)
+ {
+ try
+ {
+ // close it in case it's in some non-Closed state
+ conn.Close();
+ }
+ catch
+ {
+ // ignore any exceptions thrown from .Close
+ // if the connection is really broken the .Open call will throw
+ }
+ finally
+ {
+ // try to reopen the connection
+ conn.Open();
+ }
+ }
+ }
+ }
+
+ public class AzureAccessToken : IRenewableToken
+ {
+ public DateTimeOffset TokenExpiry { get; set; }
+ public string Resource { get; set; }
+ public string Tenant { get; set; }
+ public string UserId { get; set; }
+
+ private string accessToken;
+
+ public AzureAccessToken(string accessToken)
+ {
+ this.accessToken = accessToken;
+ }
+
+ public string GetAccessToken()
+ {
+ return this.accessToken;
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionType.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionType.cs
new file mode 100644
index 00000000..902aac3d
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/ConnectionType.cs
@@ -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.Kusto.ServiceLayer.Connection
+{
+ ///
+ /// String constants that represent connection types.
+ ///
+ /// Default: Connection used by the editor. Opened by the editor upon the initial connection.
+ /// Query: Connection used for executing queries. Opened when the first query is executed.
+ ///
+ public static class ConnectionType
+ {
+ public const string Default = "Default";
+ public const string Query = "Query";
+ public const string Edit = "Edit";
+ public const string ObjectExplorer = "ObjectExplorer";
+ public const string Dashboard = "Dashboard";
+ public const string GeneralConnection = "GeneralConnection";
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/BuildConnectionInfoRequest.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/BuildConnectionInfoRequest.cs
new file mode 100644
index 00000000..3190f26b
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/BuildConnectionInfoRequest.cs
@@ -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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Serialize Connection String request
+ ///
+ public class BuildConnectionInfoRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("connection/buildconnectioninfo");
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/CancelConnectParams.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/CancelConnectParams.cs
new file mode 100644
index 00000000..a697be10
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/CancelConnectParams.cs
@@ -0,0 +1,24 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Parameters for the Cancel Connect Request.
+ ///
+ public class CancelConnectParams
+ {
+ ///
+ /// 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.
+ ///
+ public string OwnerUri { get; set; }
+
+ ///
+ /// The type of connection we are trying to cancel
+ ///
+ public string Type { get; set; } = ConnectionType.Default;
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/CancelConnectRequest.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/CancelConnectRequest.cs
new file mode 100644
index 00000000..716a72a0
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/CancelConnectRequest.cs
@@ -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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Cancel connect request mapping entry
+ ///
+ public class CancelConnectRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("connection/cancelconnect");
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ChangeDatabaseParams.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ChangeDatabaseParams.cs
new file mode 100644
index 00000000..287c1bcf
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ChangeDatabaseParams.cs
@@ -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.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Parameters for the List Databases Request.
+ ///
+ public class ChangeDatabaseParams
+ {
+ ///
+ /// URI of the owner of the connection requesting the list of databases.
+ ///
+ public string OwnerUri { get; set; }
+
+ ///
+ /// The database to change to
+ ///
+ public string NewDatabase { get; set; }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ChangeDatabaseRequest.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ChangeDatabaseRequest.cs
new file mode 100644
index 00000000..946f9337
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ChangeDatabaseRequest.cs
@@ -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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// List databases request mapping entry
+ ///
+ public class ChangeDatabaseRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("connection/changedatabase");
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectParams.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectParams.cs
new file mode 100644
index 00000000..93f0ef3c
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectParams.cs
@@ -0,0 +1,37 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Parameters for the Connect Request.
+ ///
+ public class ConnectParams
+ {
+ ///
+ /// 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.
+ ///
+ public string OwnerUri { get; set; }
+
+ ///
+ /// 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.
+ ///
+ public ConnectionDetails Connection { get; set; }
+
+ ///
+ /// The type of this connection. By default, this is set to ConnectionType.Default.
+ ///
+ public string Type { get; set; } = ConnectionType.Default;
+
+ ///
+ /// The porpose of the connection to keep track of open connections
+ ///
+ public string Purpose { get; set; } = ConnectionType.GeneralConnection;
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectParamsExtensions.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectParamsExtensions.cs
new file mode 100644
index 00000000..5612828f
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectParamsExtensions.cs
@@ -0,0 +1,48 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Extension methods to ConnectParams
+ ///
+ public static class ConnectParamsExtensions
+ {
+ ///
+ /// Check that the fields in ConnectParams are all valid
+ ///
+ public static bool IsValid(this ConnectParams parameters, out string errorMessage)
+ {
+ errorMessage = string.Empty;
+ if (string.IsNullOrEmpty(parameters.OwnerUri))
+ {
+ errorMessage = SR.ConnectionParamsValidateNullOwnerUri;
+ }
+ else if (parameters.Connection == null)
+ {
+ errorMessage = SR.ConnectionParamsValidateNullConnection;
+ }
+ else if (!string.IsNullOrEmpty(parameters.Connection.ConnectionString))
+ {
+ // Do not check other connection parameters if a connection string is present
+ return string.IsNullOrEmpty(errorMessage);
+ }
+ else if (string.IsNullOrEmpty(parameters.Connection.ServerName))
+ {
+ errorMessage = SR.ConnectionParamsValidateNullServerName;
+ }
+ else if (string.IsNullOrEmpty(parameters.Connection.AuthenticationType) || parameters.Connection.AuthenticationType == "SqlLogin")
+ {
+ // For SqlLogin, username cannot be empty
+ if (string.IsNullOrEmpty(parameters.Connection.UserName))
+ {
+ errorMessage = SR.ConnectionParamsValidateNullSqlAuth("UserName");
+ }
+ }
+
+ return string.IsNullOrEmpty(errorMessage);
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionChangedNotification.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionChangedNotification.cs
new file mode 100644
index 00000000..8b7ba758
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionChangedNotification.cs
@@ -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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// ConnectionChanged notification mapping entry
+ ///
+ public class ConnectionChangedNotification
+ {
+ public static readonly
+ EventType Type =
+ EventType.Create("connection/connectionchanged");
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionChangedParams.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionChangedParams.cs
new file mode 100644
index 00000000..3874d8b0
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionChangedParams.cs
@@ -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.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Parameters for the ConnectionChanged Notification.
+ ///
+ public class ConnectionChangedParams
+ {
+ ///
+ /// 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.
+ ///
+ public string OwnerUri { get; set; }
+ ///
+ /// Contains the high-level properties about the connection, for display to the user.
+ ///
+ public ConnectionSummary Connection { get; set; }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionCompleteNotification.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionCompleteNotification.cs
new file mode 100644
index 00000000..6974ac09
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionCompleteNotification.cs
@@ -0,0 +1,66 @@
+//
+// 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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Parameters to be sent back with a connection complete event
+ ///
+ public class ConnectionCompleteParams
+ {
+ ///
+ /// 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.
+ ///
+ public string OwnerUri { get; set; }
+
+ ///
+ /// A GUID representing a unique connection ID
+ ///
+ public string ConnectionId { get; set; }
+
+ ///
+ /// Gets or sets any detailed connection error messages.
+ ///
+ public string Messages { get; set; }
+
+ ///
+ /// Error message returned from the engine for a connection failure reason, if any.
+ ///
+ public string ErrorMessage { get; set; }
+
+ ///
+ /// Error number returned from the engine for connection failure reason, if any.
+ ///
+ public int ErrorNumber { get; set; }
+
+ ///
+ /// Information about the connected server.
+ ///
+ public ServerInfo ServerInfo { get; set; }
+
+ ///
+ /// Gets or sets the actual Connection established, including Database Name
+ ///
+ public ConnectionSummary ConnectionSummary { get; set; }
+
+ ///
+ /// The type of connection that this notification is for
+ ///
+ public string Type { get; set; } = ConnectionType.Default;
+ }
+
+ ///
+ /// ConnectionComplete notification mapping entry
+ ///
+ public class ConnectionCompleteNotification
+ {
+ public static readonly
+ EventType Type =
+ EventType.Create("connection/complete");
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionDetails.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionDetails.cs
new file mode 100644
index 00000000..b722f12f
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionDetails.cs
@@ -0,0 +1,540 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+using Microsoft.Kusto.ServiceLayer.Utility;
+using Microsoft.SqlTools.Utility;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Message format for the initial connection request
+ ///
+ ///
+ /// If this contract is ever changed, be sure to update ConnectionDetailsExtensions methods.
+ ///
+ public class ConnectionDetails : GeneralRequestDetails, IConnectionSummary
+ {
+ public ConnectionDetails() : base()
+ {
+ }
+
+ ///
+ /// Gets or sets the connection password
+ ///
+ public string Password
+ {
+ get
+ {
+ return GetOptionValue("password");
+ }
+ set
+ {
+ SetOptionValue("password", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the connection server name
+ ///
+ public string ServerName
+ {
+ get
+ {
+ return GetOptionValue("server");
+ }
+
+ set
+ {
+ SetOptionValue("server", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the connection database name
+ ///
+ public string DatabaseName
+ {
+ get
+ {
+ return GetOptionValue("database");
+ }
+
+ set
+ {
+ SetOptionValue("database", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the connection user name
+ ///
+ public string UserName
+ {
+ get
+ {
+ return GetOptionValue("user");
+ }
+
+ set
+ {
+ SetOptionValue("user", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the authentication to use.
+ ///
+ public string AuthenticationType
+ {
+ get
+ {
+ return GetOptionValue("authenticationType");
+ }
+
+ set
+ {
+ SetOptionValue("authenticationType", value);
+ }
+ }
+
+ ///
+ /// Gets or sets a Boolean value that indicates whether SQL Server uses SSL encryption for all data sent between the client and server if the server has a certificate installed.
+ ///
+ public bool? Encrypt
+ {
+ get
+ {
+ return GetOptionValue("encrypt");
+ }
+
+ set
+ {
+ SetOptionValue("encrypt", value);
+ }
+ }
+
+ ///
+ /// Gets or sets a value that indicates whether the channel will be encrypted while bypassing walking the certificate chain to validate trust.
+ ///
+ public bool? TrustServerCertificate
+ {
+ get
+ {
+ return GetOptionValue("trustServerCertificate");
+ }
+
+ set
+ {
+ SetOptionValue("trustServerCertificate", value);
+ }
+ }
+
+ ///
+ /// Gets or sets a Boolean value that indicates if security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state.
+ ///
+ public bool? PersistSecurityInfo
+ {
+ get
+ {
+ return GetOptionValue("persistSecurityInfo");
+ }
+
+ set
+ {
+ SetOptionValue("persistSecurityInfo", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.
+ ///
+ public int? ConnectTimeout
+ {
+ get
+ {
+ return GetOptionValue("connectTimeout");
+ }
+
+ set
+ {
+ SetOptionValue("connectTimeout", value);
+ }
+ }
+
+ ///
+ /// The number of reconnections attempted after identifying that there was an idle connection failure.
+ ///
+ public int? ConnectRetryCount
+ {
+ get
+ {
+ return GetOptionValue("connectRetryCount");
+ }
+
+ set
+ {
+ SetOptionValue("connectRetryCount", value);
+ }
+ }
+
+ ///
+ /// Amount of time (in seconds) between each reconnection attempt after identifying that there was an idle connection failure.
+ ///
+ public int? ConnectRetryInterval
+ {
+ get
+ {
+ return GetOptionValue("connectRetryInterval");
+ }
+
+ set
+ {
+ SetOptionValue("connectRetryInterval", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the name of the application associated with the connection string.
+ ///
+ public string ApplicationName
+ {
+ get
+ {
+ return GetOptionValue("applicationName");
+ }
+
+ set
+ {
+ SetOptionValue("applicationName", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the name of the workstation connecting to SQL Server.
+ ///
+ public string WorkstationId
+ {
+ get
+ {
+ return GetOptionValue("workstationId");
+ }
+
+ set
+ {
+ SetOptionValue("workstationId", value);
+ }
+ }
+
+ ///
+ /// Declares the application workload type when connecting to a database in an SQL Server Availability Group.
+ ///
+ public string ApplicationIntent
+ {
+ get
+ {
+ return GetOptionValue("applicationIntent");
+ }
+
+ set
+ {
+ SetOptionValue("applicationIntent", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the SQL Server Language record name.
+ ///
+ public string CurrentLanguage
+ {
+ get
+ {
+ return GetOptionValue("currentLanguage");
+ }
+
+ set
+ {
+ SetOptionValue("currentLanguage", value);
+ }
+ }
+
+ ///
+ /// Gets or sets a Boolean value that indicates whether the connection will be pooled or explicitly opened every time that the connection is requested.
+ ///
+ public bool? Pooling
+ {
+ get
+ {
+ return GetOptionValue("pooling");
+ }
+
+ set
+ {
+ SetOptionValue("pooling", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the maximum number of connections allowed in the connection pool for this specific connection string.
+ ///
+ public int? MaxPoolSize
+ {
+ get
+ {
+ return GetOptionValue("maxPoolSize");
+ }
+
+ set
+ {
+ SetOptionValue("maxPoolSize", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the minimum number of connections allowed in the connection pool for this specific connection string.
+ ///
+ public int? MinPoolSize
+ {
+ get
+ {
+ return GetOptionValue("minPoolSize");
+ }
+
+ set
+ {
+ SetOptionValue("minPoolSize", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the minimum time, in seconds, for the connection to live in the connection pool before being destroyed.
+ ///
+ public int? LoadBalanceTimeout
+ {
+ get
+ {
+ return GetOptionValue("loadBalanceTimeout");
+ }
+
+ set
+ {
+ SetOptionValue("loadBalanceTimeout", value);
+ }
+ }
+
+ ///
+ /// Gets or sets a Boolean value that indicates whether replication is supported using the connection.
+ ///
+ public bool? Replication
+ {
+ get
+ {
+ return GetOptionValue("replication");
+ }
+
+ set
+ {
+ SetOptionValue("replication", value);
+ }
+ }
+
+ ///
+ /// Gets or sets a string that contains the name of the primary data file. This includes the full path name of an attachable database.
+ ///
+ public string AttachDbFilename
+ {
+ get
+ {
+ return GetOptionValue("attachDbFilename");
+ }
+
+ set
+ {
+ SetOptionValue("attachDbFilename", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the name or address of the partner server to connect to if the primary server is down.
+ ///
+ public string FailoverPartner
+ {
+ get
+ {
+ return GetOptionValue("failoverPartner");
+ }
+
+ set
+ {
+ SetOptionValue("failoverPartner", value);
+ }
+ }
+
+ ///
+ /// If your application is connecting to an AlwaysOn availability group (AG) on different subnets, setting MultiSubnetFailover=true provides faster detection of and connection to the (currently) active server.
+ ///
+ public bool? MultiSubnetFailover
+ {
+ get
+ {
+ return GetOptionValue("multiSubnetFailover");
+ }
+
+ set
+ {
+ SetOptionValue("multiSubnetFailover", value);
+ }
+ }
+
+ ///
+ /// When true, an application can maintain multiple active result sets (MARS).
+ ///
+ public bool? MultipleActiveResultSets
+ {
+ get
+ {
+ return GetOptionValue("multipleActiveResultSets");
+ }
+
+ set
+ {
+ SetOptionValue("multipleActiveResultSets", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the size in bytes of the network packets used to communicate with an instance of SQL Server.
+ ///
+ public int? PacketSize
+ {
+ get
+ {
+ return GetOptionValue("packetSize");
+ }
+
+ set
+ {
+ SetOptionValue("packetSize", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the port to use for the TCP/IP connection
+ ///
+ public int? Port
+ {
+ get
+ {
+ return GetOptionValue("port");
+ }
+
+ set
+ {
+ SetOptionValue("port", value);
+ }
+ }
+
+ ///
+ /// Gets or sets a string value that indicates the type system the application expects.
+ ///
+ public string TypeSystemVersion
+ {
+ get
+ {
+ return GetOptionValue("typeSystemVersion");
+ }
+
+ set
+ {
+ SetOptionValue("typeSystemVersion", value);
+ }
+ }
+
+ ///
+ /// Gets or sets a string value to be used as the connection string. If given, all other options will be ignored.
+ ///
+ public string ConnectionString
+ {
+ get
+ {
+ return GetOptionValue("connectionString");
+ }
+
+ set
+ {
+ SetOptionValue("connectionString", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the group ID
+ ///
+ public string GroupId
+ {
+ get
+ {
+ return GetOptionValue("groupId");
+ }
+ set
+ {
+ SetOptionValue("groupId", value);
+ }
+ }
+
+ ///
+ /// Gets or sets the database display name
+ ///
+ public string DatabaseDisplayName
+ {
+ get
+ {
+ return GetOptionValue("databaseDisplayName");
+ }
+ set
+ {
+ SetOptionValue("databaseDisplayName", value);
+ }
+ }
+
+ public string AzureAccountToken
+ {
+ get
+ {
+ return GetOptionValue("azureAccountToken");
+ }
+ set
+ {
+ SetOptionValue("azureAccountToken", value);
+ }
+ }
+
+ public bool IsComparableTo(ConnectionDetails other)
+ {
+ if (other == null)
+ {
+ return false;
+ }
+
+ if (ServerName != other.ServerName
+ || AuthenticationType != other.AuthenticationType
+ || UserName != other.UserName
+ || AzureAccountToken != other.AzureAccountToken)
+ {
+ return false;
+ }
+
+ // For database name, only compare if neither is empty. This is important
+ // Since it allows for handling of connections to the default database, but is
+ // not a 100% accurate heuristic.
+ if (!string.IsNullOrEmpty(DatabaseName)
+ && !string.IsNullOrEmpty(other.DatabaseName)
+ && DatabaseName != other.DatabaseName)
+ {
+ return false;
+ }
+
+ return true;
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionDetailsExtensions.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionDetailsExtensions.cs
new file mode 100644
index 00000000..edfbf651
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionDetailsExtensions.cs
@@ -0,0 +1,52 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Extension methods for the ConnectionDetails contract class
+ ///
+ public static class ConnectionDetailsExtensions
+ {
+ ///
+ /// Create a copy of a connection details object.
+ ///
+ public static ConnectionDetails Clone(this ConnectionDetails details)
+ {
+ return new ConnectionDetails()
+ {
+ ServerName = details.ServerName,
+ DatabaseName = details.DatabaseName,
+ UserName = details.UserName,
+ Password = details.Password,
+ AuthenticationType = details.AuthenticationType,
+ Encrypt = details.Encrypt,
+ TrustServerCertificate = details.TrustServerCertificate,
+ PersistSecurityInfo = details.PersistSecurityInfo,
+ ConnectTimeout = details.ConnectTimeout,
+ ConnectRetryCount = details.ConnectRetryCount,
+ ConnectRetryInterval = details.ConnectRetryInterval,
+ ApplicationName = details.ApplicationName,
+ WorkstationId = details.WorkstationId,
+ ApplicationIntent = details.ApplicationIntent,
+ CurrentLanguage = details.CurrentLanguage,
+ Pooling = details.Pooling,
+ MaxPoolSize = details.MaxPoolSize,
+ MinPoolSize = details.MinPoolSize,
+ LoadBalanceTimeout = details.LoadBalanceTimeout,
+ Replication = details.Replication,
+ AttachDbFilename = details.AttachDbFilename,
+ FailoverPartner = details.FailoverPartner,
+ MultiSubnetFailover = details.MultiSubnetFailover,
+ MultipleActiveResultSets = details.MultipleActiveResultSets,
+ PacketSize = details.PacketSize,
+ TypeSystemVersion = details.TypeSystemVersion,
+ ConnectionString = details.ConnectionString,
+ Port = details.Port,
+ AzureAccountToken = details.AzureAccountToken
+ };
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionRequest.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionRequest.cs
new file mode 100644
index 00000000..8a63e28a
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionRequest.cs
@@ -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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Connect request mapping entry
+ ///
+ public class ConnectionRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("connection/connect");
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionSummary.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionSummary.cs
new file mode 100644
index 00000000..b4f26536
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionSummary.cs
@@ -0,0 +1,48 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+
+ public interface IConnectionSummary
+ {
+ ///
+ /// Gets or sets the connection server name
+ ///
+ string ServerName { get; set; }
+
+ ///
+ /// Gets or sets the connection database name
+ ///
+ string DatabaseName { get; set; }
+
+ ///
+ /// Gets or sets the connection user name
+ ///
+ string UserName { get; set; }
+ }
+
+ ///
+ /// Provides high level information about a connection.
+ ///
+ public class ConnectionSummary : IConnectionSummary
+ {
+ ///
+ /// Gets or sets the connection server name
+ ///
+ public virtual string ServerName { get; set; }
+
+ ///
+ /// Gets or sets the connection database name
+ ///
+ public virtual string DatabaseName { get; set; }
+
+ ///
+ /// Gets or sets the connection user name
+ ///
+ public virtual string UserName { get; set; }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionSummaryComparer.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionSummaryComparer.cs
new file mode 100644
index 00000000..2c69be19
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionSummaryComparer.cs
@@ -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.Kusto.ServiceLayer.Connection.Contracts
+{
+
+ ///
+ /// Treats connections as the same if their server, db and usernames all match
+ ///
+ public class ConnectionSummaryComparer : IEqualityComparer
+ {
+ 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;
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionSummaryExtensions.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionSummaryExtensions.cs
new file mode 100644
index 00000000..476ded3a
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ConnectionSummaryExtensions.cs
@@ -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.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Extension methods to ConnectionSummary
+ ///
+ public static class ConnectionSummaryExtensions
+ {
+ ///
+ /// Create a copy of a ConnectionSummary object
+ ///
+ public static ConnectionSummary Clone(this IConnectionSummary summary)
+ {
+ return new ConnectionSummary()
+ {
+ ServerName = summary.ServerName,
+ DatabaseName = summary.DatabaseName,
+ UserName = summary.UserName
+ };
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/DisconnectParams.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/DisconnectParams.cs
new file mode 100644
index 00000000..f25979a3
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/DisconnectParams.cs
@@ -0,0 +1,25 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Parameters for the Disconnect Request.
+ ///
+ public class DisconnectParams
+ {
+ ///
+ /// 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.
+ ///
+ public string OwnerUri { get; set; }
+
+ ///
+ /// The type of connection we are disconnecting. If null, we will disconnect all connections.
+ /// connections.
+ ///
+ public string Type { get; set; }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/DisconnectRequest.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/DisconnectRequest.cs
new file mode 100644
index 00000000..eef7fb8d
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/DisconnectRequest.cs
@@ -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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Disconnect request mapping entry
+ ///
+ public class DisconnectRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("connection/disconnect");
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/GetConnectionStringParams.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/GetConnectionStringParams.cs
new file mode 100644
index 00000000..75c6a1f1
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/GetConnectionStringParams.cs
@@ -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.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Parameters for the Get Connection String Request.
+ ///
+ public class GetConnectionStringParams
+ {
+ ///
+ /// URI of the owner of the connection
+ ///
+ public string OwnerUri { get; set; }
+
+ ///
+ /// Indicates whether the password should be return in the connection string
+ ///
+ public bool IncludePassword { get; set; }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/GetConnectionStringRequest.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/GetConnectionStringRequest.cs
new file mode 100644
index 00000000..fa855b35
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/GetConnectionStringRequest.cs
@@ -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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Get Connection String request
+ ///
+ public class GetConnectionStringRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("connection/getconnectionstring");
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/LanguageFlavorChange.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/LanguageFlavorChange.cs
new file mode 100644
index 00000000..db97c23b
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/LanguageFlavorChange.cs
@@ -0,0 +1,42 @@
+//
+// 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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+
+ ///
+ /// Parameters for the Language Flavor Change notification.
+ ///
+ public class LanguageFlavorChangeParams
+ {
+ ///
+ /// A URI identifying the affected resource
+ ///
+ public string Uri { get; set; }
+
+ ///
+ /// The primary language
+ ///
+ public string Language { get; set; }
+
+ ///
+ /// The specific language flavor that is being set
+ ///
+ public string Flavor { get; set; }
+ }
+
+ ///
+ /// Defines an event that is sent from the client to notify that
+ /// the client is exiting and the server should as well.
+ ///
+ public class LanguageFlavorChangeNotification
+ {
+ public static readonly
+ EventType Type =
+ EventType.Create("connection/languageflavorchanged");
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ListDatabasesParams.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ListDatabasesParams.cs
new file mode 100644
index 00000000..039a93e8
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ListDatabasesParams.cs
@@ -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.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Parameters for the List Databases Request.
+ ///
+ public class ListDatabasesParams
+ {
+ ///
+ /// URI of the owner of the connection requesting the list of databases.
+ ///
+ public string OwnerUri { get; set; }
+
+ ///
+ /// whether to include the details of the databases. Called by manage dashboard
+ ///
+ public bool? IncludeDetails { get; set; }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ListDatabasesRequest.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ListDatabasesRequest.cs
new file mode 100644
index 00000000..45f21c17
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ListDatabasesRequest.cs
@@ -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.Hosting.Protocol.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// List databases request mapping entry
+ ///
+ public class ListDatabasesRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("connection/listdatabases");
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ListDatabasesResponse.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ListDatabasesResponse.cs
new file mode 100644
index 00000000..cf6e86a1
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ListDatabasesResponse.cs
@@ -0,0 +1,24 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+using Microsoft.Kusto.ServiceLayer.Admin.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Message format for the list databases response
+ ///
+ public class ListDatabasesResponse
+ {
+ ///
+ /// Gets or sets the list of database names.
+ ///
+ public string[] DatabaseNames { get; set; }
+
+ ///
+ /// Gets or sets the databases details.
+ ///
+ public DatabaseInfo[] Databases { get; set; }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ServerInfo.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ServerInfo.cs
new file mode 100644
index 00000000..4cb6032c
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/Contracts/ServerInfo.cs
@@ -0,0 +1,75 @@
+//
+// 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;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.Contracts
+{
+ ///
+ /// Contract for information on the connected SQL Server instance.
+ ///
+ public class ServerInfo
+ {
+ ///
+ /// The major version of the SQL Server instance.
+ ///
+ public int ServerMajorVersion { get; set; }
+
+ ///
+ /// The minor version of the SQL Server instance.
+ ///
+ public int ServerMinorVersion { get; set; }
+
+ ///
+ /// The build of the SQL Server instance.
+ ///
+ public int ServerReleaseVersion { get; set; }
+
+ ///
+ /// The ID of the engine edition of the SQL Server instance.
+ ///
+ public int EngineEditionId { get; set; }
+
+ ///
+ /// String containing the full server version text.
+ ///
+ public string ServerVersion { get; set; }
+
+ ///
+ /// String describing the product level of the server.
+ ///
+ public string ServerLevel { get; set; }
+
+ ///
+ /// The edition of the SQL Server instance.
+ ///
+ public string ServerEdition { get; set; }
+
+ ///
+ /// Whether the SQL Server instance is running in the cloud (Azure) or not.
+ ///
+ public bool IsCloud { get; set; }
+
+ ///
+ /// The version of Azure that the SQL Server instance is running on, if applicable.
+ ///
+ public int AzureVersion { get; set; }
+
+ ///
+ /// The Operating System version string of the machine running the SQL Server instance.
+ ///
+ public string OsVersion { get; set; }
+
+ ///
+ /// The Operating System version string of the machine running the SQL Server instance.
+ ///
+ public string MachineName { get; set; }
+
+ ///
+ /// Server options
+ ///
+ public Dictionary Options { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/DataSourceConnectionFactory.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/DataSourceConnectionFactory.cs
new file mode 100644
index 00000000..c676decd
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/DataSourceConnectionFactory.cs
@@ -0,0 +1,29 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+using System.Data.Common;
+using Microsoft.Kusto.ServiceLayer.Connection;
+using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection
+{
+ ///
+ /// Factory class to create SqlClientConnections
+ /// The purpose of the factory is to make it easier to mock out the database
+ /// in 'offline' unit test scenarios.
+ ///
+ public class DataSourceConnectionFactory : IDataSourceConnectionFactory
+ {
+ ///
+ /// Creates a new SqlConnection object
+ ///
+ public ReliableDataSourceConnection CreateDataSourceConnection(string connectionString, string azureAccountToken)
+ {
+ RetryPolicy connectionRetryPolicy = RetryPolicyFactory.CreateDefaultConnectionRetryPolicy();
+ RetryPolicy commandRetryPolicy = RetryPolicyFactory.CreateDefaultConnectionRetryPolicy();
+ return new ReliableDataSourceConnection(connectionString, connectionRetryPolicy, commandRetryPolicy, azureAccountToken);
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/DatabaseFullAccessException.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/DatabaseFullAccessException.cs
new file mode 100644
index 00000000..f1a2d11c
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/DatabaseFullAccessException.cs
@@ -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.
+//
+
+using System;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection
+{
+ public class DatabaseFullAccessException: Exception
+ {
+ public DatabaseFullAccessException()
+ : base()
+ {
+ }
+
+ public DatabaseFullAccessException(string message, Exception exception)
+ : base(message, exception)
+ {
+ }
+
+
+ public DatabaseFullAccessException(string message)
+ : base(message)
+ {
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/DatabaseLocksManager.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/DatabaseLocksManager.cs
new file mode 100644
index 00000000..c1aeb0df
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/DatabaseLocksManager.cs
@@ -0,0 +1,114 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+using Microsoft.Kusto.ServiceLayer.LanguageServices;
+using System;
+using System.Collections.Generic;
+using System.Threading;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection
+{
+ public class DatabaseLocksManager: IDisposable
+ {
+ internal DatabaseLocksManager(int waitToGetFullAccess)
+ {
+ this.waitToGetFullAccess = waitToGetFullAccess;
+ }
+
+ private static DatabaseLocksManager instance = new DatabaseLocksManager(DefaultWaitToGetFullAccess);
+
+ public static DatabaseLocksManager Instance
+ {
+ get
+ {
+ return instance;
+ }
+ }
+
+ public ConnectionService ConnectionService { get; set; }
+
+ private Dictionary databaseAccessEvents = new Dictionary();
+ private object databaseAccessLock = new object();
+ public const int DefaultWaitToGetFullAccess = 10000;
+ public int waitToGetFullAccess = DefaultWaitToGetFullAccess;
+
+ private ManualResetEvent GetResetEvent(string serverName, string databaseName)
+ {
+ string key = GenerateKey(serverName, databaseName);
+ ManualResetEvent resetEvent = null;
+ lock (databaseAccessLock)
+ {
+ if (!databaseAccessEvents.TryGetValue(key, out resetEvent))
+ {
+ resetEvent = new ManualResetEvent(true);
+ databaseAccessEvents.Add(key, resetEvent);
+ }
+ }
+
+ return resetEvent;
+ }
+
+ public bool GainFullAccessToDatabase(string serverName, string databaseName)
+ {
+ /*
+ * TODO: add the lock so not two process can get full access at the same time
+ ManualResetEvent resetEvent = GetResetEvent(serverName, databaseName);
+ if (resetEvent.WaitOne(this.waitToGetFullAccess))
+ {
+ resetEvent.Reset();
+
+ foreach (IConnectedBindingQueue item in ConnectionService.ConnectedQueues)
+ {
+ item.CloseConnections(serverName, databaseName);
+ }
+ return true;
+ }
+ else
+ {
+ throw new DatabaseFullAccessException($"Waited more than {waitToGetFullAccess} milli seconds for others to release the lock");
+ }
+ */
+ foreach (IConnectedBindingQueue item in ConnectionService.ConnectedQueues)
+ {
+ item.CloseConnections(serverName, databaseName, DefaultWaitToGetFullAccess);
+ }
+ return true;
+
+ }
+
+ public bool ReleaseAccess(string serverName, string databaseName)
+ {
+ /*
+ ManualResetEvent resetEvent = GetResetEvent(serverName, databaseName);
+
+ foreach (IConnectedBindingQueue item in ConnectionService.ConnectedQueues)
+ {
+ item.OpenConnections(serverName, databaseName);
+ }
+
+ resetEvent.Set();
+ */
+ foreach (IConnectedBindingQueue item in ConnectionService.ConnectedQueues)
+ {
+ item.OpenConnections(serverName, databaseName, DefaultWaitToGetFullAccess);
+ }
+ return true;
+
+ }
+
+ private string GenerateKey(string serverName, string databaseName)
+ {
+ return $"{serverName.ToLowerInvariant()}-{databaseName.ToLowerInvariant()}";
+ }
+
+ public void Dispose()
+ {
+ foreach (var resetEvent in databaseAccessEvents)
+ {
+ resetEvent.Value.Dispose();
+ }
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/FeatureWithFullDbAccess.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/FeatureWithFullDbAccess.cs
new file mode 100644
index 00000000..9be034c7
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/FeatureWithFullDbAccess.cs
@@ -0,0 +1,40 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+namespace Microsoft.Kusto.ServiceLayer.Connection
+{
+ ///
+ /// Any operation that needs full access to databas should implement this interface.
+ /// Make sure to call GainAccessToDatabase before the operation and ReleaseAccessToDatabase after
+ ///
+ public interface IFeatureWithFullDbAccess
+ {
+ ///
+ /// Database Lock Manager
+ ///
+ DatabaseLocksManager LockedDatabaseManager { get; set; }
+
+ ///
+ /// Makes sure the feature has fill access to the database
+ ///
+ bool GainAccessToDatabase();
+
+ ///
+ /// Release the access to db
+ ///
+ bool ReleaseAccessToDatabase();
+
+ ///
+ /// Server name
+ ///
+ string ServerName { get; }
+
+ ///
+ /// Database name
+ ///
+ string DatabaseName { get; }
+ }
+
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/IDataSourceConnectionFactory.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/IDataSourceConnectionFactory.cs
new file mode 100644
index 00000000..fbfd45b1
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/IDataSourceConnectionFactory.cs
@@ -0,0 +1,20 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+using System.Data.Common;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection
+{
+ ///
+ /// Interface for the SQL Connection factory
+ ///
+ public interface IDataSourceConnectionFactory
+ {
+ ///
+ /// Create a new SQL Connection object
+ ///
+ ReliableDataSourceConnection CreateDataSourceConnection(string connectionString, string azureAccountToken);
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Connection/ReliableConnection/RetryCallbackEventArgs.cs b/src/Microsoft.Kusto.ServiceLayer/Connection/ReliableConnection/RetryCallbackEventArgs.cs
new file mode 100644
index 00000000..b6a11bbf
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Connection/ReliableConnection/RetryCallbackEventArgs.cs
@@ -0,0 +1,58 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+// This code is copied from the source described in the comment below.
+
+// =======================================================================================
+// Microsoft Windows Server AppFabric Customer Advisory Team (CAT) Best Practices Series
+//
+// This sample is supplemental to the technical guidance published on the community
+// blog at http://blogs.msdn.com/appfabriccat/ and copied from
+// sqlmain ./sql/manageability/mfx/common/
+//
+// =======================================================================================
+// Copyright © 2012 Microsoft Corporation. All rights reserved.
+//
+// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
+// EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. YOU BEAR THE RISK OF USING IT.
+// =======================================================================================
+
+using System;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection.ReliableConnection
+{
+ ///
+ /// Defines a arguments for event handler which will be invoked whenever a retry condition is encountered.
+ ///
+ internal sealed class RetryCallbackEventArgs : EventArgs
+ {
+ private readonly int _retryCount;
+ private readonly Exception _exception;
+ private readonly TimeSpan _delay;
+
+ public RetryCallbackEventArgs(int retryCount, Exception exception, TimeSpan delay)
+ {
+ _retryCount = retryCount;
+ _exception = exception;
+ _delay = delay;
+ }
+
+ public TimeSpan Delay
+ {
+ get { return _delay; }
+ }
+
+ public Exception Exception
+ {
+ get { return _exception; }
+ }
+
+ public int RetryCount
+ {
+ get { return _retryCount; }
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/DataReaderWrapper.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataReaderWrapper.cs
new file mode 100644
index 00000000..89ee3616
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataReaderWrapper.cs
@@ -0,0 +1,59 @@
+//
+// Copyright (c) Microsoft. All Rights Reserved.
+//
+using System;
+using System.Text.RegularExpressions;
+using System.Linq;
+using System.Data;
+
+namespace Microsoft.Kusto.ServiceLayer.DataSource
+{
+ class DataReaderWrapper:IDataReader
+ {
+ private readonly IDataReader _inner ;
+ public DataReaderWrapper(IDataReader inner)
+ {
+ _inner = inner;
+ }
+
+ public object this[int i] => _inner[i];
+
+ public object this[string name] => _inner[name];
+
+ public int Depth => _inner.Depth;
+
+ public bool IsClosed => _inner.IsClosed;
+
+ public int RecordsAffected => _inner.RecordsAffected;
+
+ public int FieldCount => _inner.FieldCount;
+
+ public void Close() => _inner.Close();
+ public void Dispose() => _inner.Dispose();
+ public bool GetBoolean(int i) => _inner.GetBoolean(i);
+ public byte GetByte(int i) => _inner.GetByte(i);
+ public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) => _inner.GetBytes(i, fieldOffset, buffer, bufferoffset, length);
+ public char GetChar(int i) => _inner.GetChar(i);
+ public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) => _inner.GetChars(i, fieldoffset, buffer, bufferoffset, length);
+ public IDataReader GetData(int i) => _inner.GetData(i);
+ public string GetDataTypeName(int i) => _inner.GetDataTypeName(i);
+ public DateTime GetDateTime(int i) => _inner.GetDateTime(i);
+ public decimal GetDecimal(int i) => _inner.GetDecimal(i);
+ public double GetDouble(int i) => _inner.GetDouble(i);
+ public Type GetFieldType(int i) => _inner.GetFieldType(i);
+ public float GetFloat(int i) => _inner.GetFloat(i);
+ public Guid GetGuid(int i) => _inner.GetGuid(i);
+ public short GetInt16(int i) => _inner.GetInt16(i);
+ public int GetInt32(int i) => _inner.GetInt32(i);
+ public long GetInt64(int i) => _inner.GetInt64(i);
+ public string GetName(int i) => _inner.GetName(i);
+ public int GetOrdinal(string name) => _inner.GetOrdinal(name);
+ public DataTable GetSchemaTable() => _inner.GetSchemaTable();
+ public string GetString(int i) => _inner.GetString(i);
+ public object GetValue(int i) => _inner.GetValue(i);
+ public int GetValues(object[] values) => _inner.GetValues(values);
+ public bool IsDBNull(int i) => _inner.IsDBNull(i);
+ public virtual bool NextResult() => _inner.NextResult();
+ public bool Read() => _inner.Read();
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceBase.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceBase.cs
new file mode 100644
index 00000000..1cd32697
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceBase.cs
@@ -0,0 +1,132 @@
+//
+// Copyright (c) Microsoft. All Rights Reserved.
+//
+
+using System;
+using System.Collections.Generic;
+using System.Threading;
+using System.Data;
+using System.Threading.Tasks;
+using Microsoft.Kusto.ServiceLayer.Utility;
+using Microsoft.Kusto.ServiceLayer.DataSource.DataSourceIntellisense;
+using Microsoft.Kusto.ServiceLayer.DataSource.Metadata;
+using Microsoft.Kusto.ServiceLayer.LanguageServices;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
+using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Completion;
+
+namespace Microsoft.Kusto.ServiceLayer.DataSource
+{
+ ///
+ public abstract class DataSourceBase : IDataSource
+ {
+ protected Object dataSourceLock = new Object();
+
+ private string _database;
+
+ #region IDisposable
+
+ ///
+ /// Finalizes an instance of the class.
+ ///
+ ~DataSourceBase()
+ {
+ Dispose(false);
+ }
+
+ ///
+ /// Disposes resources.
+ ///
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ /// Disposes resources.
+ ///
+ /// true if disposing; false if finalizing.
+ protected virtual void Dispose(bool disposing)
+ {
+ }
+
+ #endregion
+
+ #region IDataSource
+
+ ///
+ public abstract Task ExecuteQueryAsync(string query, CancellationToken cancellationToken, string databaseName = null);
+
+ ///
+ public async Task ExecuteScalarQueryAsync(string query, CancellationToken cancellationToken, string databaseName = null)
+ {
+ ValidationUtils.IsArgumentNotNullOrWhiteSpace(query, nameof(query));
+
+ using (var records = await ExecuteQueryAsync(query, cancellationToken, databaseName))
+ {
+ return records.ToScalar();
+ }
+ }
+
+ public abstract Task> ExecuteControlCommandAsync(string command, bool throwOnError, CancellationToken cancellationToken);
+
+ ///
+ public abstract DiagnosticsInfo GetDiagnostics(DataSourceObjectMetadata parentMetadata);
+
+ ///
+ public abstract IEnumerable GetChildObjects(DataSourceObjectMetadata parentMetadata, bool includeSizeDetails = false);
+
+ ///
+ public abstract void Refresh();
+
+ ///
+ public abstract void Refresh(DataSourceObjectMetadata objectMetadata);
+
+ ///
+ public abstract void UpdateDatabase(string databaseName);
+
+ ///
+ public abstract CompletionItem[] GetAutoCompleteSuggestions(ScriptDocumentInfo queryText, Position index, bool throwOnError = false);
+ ///
+ public abstract Hover GetHoverHelp(ScriptDocumentInfo scriptDocumentInfo, Position textPosition, bool throwOnError = false);
+
+ ///
+ public abstract DefinitionResult GetDefinition(string queryText, int index, int startLine, int startColumn, bool throwOnError = false);
+
+ ///
+ public abstract ScriptFileMarker[] GetSemanticMarkers(ScriptParseInfo parseInfo, ScriptFile scriptFile, string queryText);
+
+ ///
+ public abstract Task Exists();
+
+ ///
+ public abstract bool Exists(DataSourceObjectMetadata objectMetadata);
+
+ public abstract string GenerateAlterFunctionScript(string functionName);
+
+ ///
+ public DataSourceType DataSourceType { get; protected set; }
+
+ ///
+ public string ClusterName { get; protected set; }
+
+ ///
+ public string DatabaseName {
+ get
+ {
+ return _database;
+ }
+
+ set
+ {
+ lock(dataSourceLock)
+ {
+ _database = value;
+ }
+ }
+ }
+
+ #endregion
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceFactory.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceFactory.cs
new file mode 100644
index 00000000..3e78d500
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceFactory.cs
@@ -0,0 +1,159 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.Kusto.ServiceLayer.Utility;
+using Microsoft.Kusto.ServiceLayer.Admin.Contracts;
+using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
+using Microsoft.Kusto.ServiceLayer.Metadata.Contracts;
+using Microsoft.Kusto.ServiceLayer.DataSource.DataSourceIntellisense;
+using Microsoft.Kusto.ServiceLayer.DataSource.Metadata;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
+using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Completion;
+
+namespace Microsoft.Kusto.ServiceLayer.DataSource
+{
+ ///
+ /// Data source factory.
+ ///
+ public static class DataSourceFactory
+ {
+ public static IDataSource Create(DataSourceType dataSourceType, string connectionString, string azureAccountToken)
+ {
+ ValidationUtils.IsArgumentNotNullOrWhiteSpace(connectionString, nameof(connectionString));
+ ValidationUtils.IsArgumentNotNullOrWhiteSpace(azureAccountToken, nameof(azureAccountToken));
+
+ switch (dataSourceType)
+ {
+ case DataSourceType.Kusto:
+ {
+ return new KustoDataSource(connectionString, azureAccountToken);
+ }
+
+ default:
+ throw new ArgumentException($"Unsupported data source type \"{dataSourceType}\"", nameof(dataSourceType));
+ }
+ }
+
+ public static DataSourceObjectMetadata CreateClusterMetadata(string clusterName)
+ {
+ ValidationUtils.IsArgumentNotNullOrWhiteSpace(clusterName, nameof(clusterName));
+
+ return new DataSourceObjectMetadata{
+ MetadataType = DataSourceMetadataType.Cluster,
+ MetadataTypeName = DataSourceMetadataType.Cluster.ToString(),
+ Name = clusterName,
+ PrettyName = clusterName,
+ Urn = $"{clusterName}"
+ };
+ }
+
+ public static DataSourceObjectMetadata CreateDatabaseMetadata(DataSourceObjectMetadata clusterMetadata, string databaseName)
+ {
+ ValidationUtils.IsTrue(clusterMetadata.MetadataType == DataSourceMetadataType.Cluster, nameof(clusterMetadata));
+ ValidationUtils.IsArgumentNotNullOrWhiteSpace(databaseName, nameof(databaseName));
+
+ return new DatabaseMetadata{
+ ClusterName = clusterMetadata.Name,
+ MetadataType = DataSourceMetadataType.Database,
+ MetadataTypeName = DataSourceMetadataType.Database.ToString(),
+ Name = databaseName,
+ PrettyName = databaseName,
+ Urn = $"{clusterMetadata.Urn}.{databaseName}"
+ };
+ }
+
+ public static FolderMetadata CreateFolderMetadata(DataSourceObjectMetadata parentMetadata, string path, string name)
+ {
+ ValidationUtils.IsNotNull(parentMetadata, nameof(parentMetadata));
+
+ return new FolderMetadata{
+ MetadataType = DataSourceMetadataType.Folder,
+ MetadataTypeName = DataSourceMetadataType.Folder.ToString(),
+ Name = name,
+ PrettyName = name,
+ ParentMetadata = parentMetadata,
+ Urn = $"{path}.{name}"
+ };
+ }
+
+ // Gets default keywords for intellisense when there is no connection.
+ public static CompletionItem[] GetDefaultAutoComplete(DataSourceType dataSourceType, ScriptDocumentInfo scriptDocumentInfo, Position textDocumentPosition){
+ switch (dataSourceType)
+ {
+ case DataSourceType.Kusto:
+ {
+ return KustoIntellisenseHelper.GetDefaultKeywords(scriptDocumentInfo, textDocumentPosition);
+ }
+
+ default:
+ throw new ArgumentException($"Unsupported data source type \"{dataSourceType}\"", nameof(dataSourceType));
+ }
+ }
+
+ // Gets default keywords errors related to intellisense when there is no connection.
+ public static ScriptFileMarker[] GetDefaultSemanticMarkers(DataSourceType dataSourceType, ScriptParseInfo parseInfo, ScriptFile scriptFile, string queryText){
+ switch (dataSourceType)
+ {
+ case DataSourceType.Kusto:
+ {
+ return KustoIntellisenseHelper.GetDefaultDiagnostics(parseInfo, scriptFile, queryText);
+ }
+
+ default:
+ throw new ArgumentException($"Unsupported data source type \"{dataSourceType}\"", nameof(dataSourceType));
+ }
+ }
+
+ // Converts database details shown on cluster manage dashboard to DatabaseInfo type. Add DataSourceType as param if required to show different properties
+ public static List ConvertToDatabaseInfo(IEnumerable clusterDBDetails)
+ {
+ var databaseDetails = new List();
+
+ if(typeof(DatabaseMetadata) == clusterDBDetails.FirstOrDefault().GetType()){
+ foreach(var dbDetail in clusterDBDetails)
+ {
+ DatabaseInfo databaseInfo = new DatabaseInfo();
+ Int64.TryParse(dbDetail.SizeInMB.ToString(), out long sum_OriginalSize);
+ databaseInfo.Options["name"] = dbDetail.Name;
+ databaseInfo.Options["sizeInMB"] = (sum_OriginalSize /(1024 * 1024)).ToString();
+ databaseDetails.Add(databaseInfo);
+ }
+ }
+
+ return databaseDetails;
+ }
+
+ // Converts tables details shown on database manage dashboard to ObjectMetadata type. Add DataSourceType as param if required to show different properties
+ public static List ConvertToObjectMetadata(IEnumerable dbChildDetails)
+ {
+ var databaseChildDetails = new List();
+
+ foreach(var childDetail in dbChildDetails)
+ {
+ ObjectMetadata dbChildInfo = new ObjectMetadata();
+ dbChildInfo.Name = childDetail.PrettyName;
+ dbChildInfo.MetadataTypeName = childDetail.MetadataTypeName;
+ dbChildInfo.MetadataType = MetadataType.Table; // Add mapping here.
+ databaseChildDetails.Add(dbChildInfo);
+ }
+ return databaseChildDetails;
+ }
+
+ public static ReliableConnectionHelper.ServerInfo ConvertToServerinfoFormat(DataSourceType dataSourceType, DiagnosticsInfo clusterDiagnostics)
+ {
+ switch (dataSourceType)
+ {
+ case DataSourceType.Kusto:
+ {
+ ReliableConnectionHelper.ServerInfo serverInfo = new ReliableConnectionHelper.ServerInfo();
+ serverInfo.Options = new Dictionary(clusterDiagnostics.Options);
+ return serverInfo;
+ }
+
+ default:
+ throw new ArgumentException($"Unsupported data source type \"{dataSourceType}\"", nameof(dataSourceType));
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceIntellisense/KustoIntellisenseHelper.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceIntellisense/KustoIntellisenseHelper.cs
new file mode 100644
index 00000000..fa423e3c
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceIntellisense/KustoIntellisenseHelper.cs
@@ -0,0 +1,329 @@
+//
+// 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;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Linq;
+using Kusto.Language;
+using Kusto.Language.Editor;
+using Kusto.Language.Syntax;
+using Kusto.Language.Symbols;
+using Microsoft.Kusto.ServiceLayer.LanguageServices;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Completion;
+using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.DataSource.DataSourceIntellisense
+{
+ ///
+ /// Kusto specific class for intellisense helper functions.
+ ///
+ public static class KustoIntellisenseHelper
+ {
+
+ public class ShowDatabasesResult
+ {
+ public string DatabaseName;
+ public string PersistentStorage;
+ public string Version;
+ public bool IsCurrent;
+ public string DatabaseAccessMode;
+ public string PrettyName;
+ public bool CurrentUserIsUnrestrictedViewer;
+ public string DatabaseId;
+ }
+
+ public class ShowDatabaseSchemaResult
+ {
+ public string DatabaseName;
+ public string TableName;
+ public string ColumnName;
+ public string ColumnType;
+ public bool IsDefaultTable;
+ public bool IsDefaultColumn;
+ public string PrettyName;
+ public string Version;
+ public string Folder;
+ public string DocName;
+ }
+
+ public class ShowFunctionsResult
+ {
+ public string Name;
+ public string Parameters;
+ public string Body;
+ public string Folder;
+ public string DocString;
+ }
+
+ ///
+ /// Convert CLR type name into a Kusto scalar type.
+ ///
+ private static ScalarSymbol GetKustoType(string clrTypeName)
+ {
+ switch (clrTypeName)
+ {
+ case "System.Byte":
+ case "Byte":
+ case "byte":
+ case "System.SByte":
+ case "SByte":
+ case "sbyte":
+ case "System.Int16":
+ case "Int16":
+ case "short":
+ case "System.UInt16":
+ case "UInt16":
+ case "ushort":
+ case "System.Int32":
+ case "System.Single":
+ case "Int32":
+ case "int":
+ return ScalarTypes.Int;
+ case "System.UInt32": // unsigned ints don't fit into int, use long
+ case "UInt32":
+ case "uint":
+ case "System.Int64":
+ case "Int64":
+ case "long":
+ return ScalarTypes.Long;
+ case "System.Double":
+ case "Double":
+ case "double":
+ case "float":
+ return ScalarTypes.Real;
+ case "System.UInt64": // unsigned longs do not fit into long, use decimal
+ case "UInt64":
+ case "ulong":
+ case "System.Decimal":
+ case "Decimal":
+ case "decimal":
+ case "System.Data.SqlTypes.SqlDecimal":
+ case "SqlDecimal":
+ return ScalarTypes.Decimal;
+ case "System.Guid":
+ case "Guid":
+ return ScalarTypes.Guid;
+ case "System.DateTime":
+ case "DateTime":
+ return ScalarTypes.DateTime;
+ case "System.TimeSpan":
+ case "TimeSpan":
+ return ScalarTypes.TimeSpan;
+ case "System.String":
+ case "String":
+ case "string":
+ return ScalarTypes.String;
+ case "System.Boolean":
+ case "Boolean":
+ case "bool":
+ return ScalarTypes.Bool;
+ case "System.Object":
+ case "Object":
+ case "object":
+ return ScalarTypes.Dynamic;
+ case "System.Type":
+ case "Type":
+ return ScalarTypes.Type;
+ default:
+ throw new InvalidOperationException($"Unhandled clr type: {clrTypeName}");
+ }
+ }
+
+ private static IReadOnlyList NoParameters = new Parameter[0];
+
+ ///
+ /// Translate Kusto parameter list declaration into into list of instances.
+ ///
+ private static IReadOnlyList TranslateParameters(string parameters)
+ {
+ parameters = parameters.Trim();
+
+ if (string.IsNullOrEmpty(parameters) || parameters == "()")
+ return NoParameters;
+
+ if (parameters[0] != '(')
+ parameters = "(" + parameters;
+ if (parameters[parameters.Length - 1] != ')')
+ parameters = parameters + ")";
+
+ var query = "let fn = " + parameters + " { };";
+ var code = KustoCode.ParseAndAnalyze(query);
+ var let = code.Syntax.GetFirstDescendant();
+ var variable = let.Name.ReferencedSymbol as VariableSymbol;
+ var function = variable.Type as FunctionSymbol;
+ return function.Signatures[0].Parameters;
+ }
+
+ ///
+ /// Loads the schema for the specified databasea into a a .
+ ///
+ public static async Task LoadDatabaseAsync(IDataSource dataSource, string databaseName, bool throwOnError = false)
+ {
+ var members = new List();
+ CancellationTokenSource source = new CancellationTokenSource();
+ CancellationToken cancellationToken = source.Token;
+
+ var tableSchemas = await dataSource.ExecuteControlCommandAsync($".show database {databaseName} schema", throwOnError, cancellationToken).ConfigureAwait(false);
+ if (tableSchemas == null)
+ return null;
+
+ tableSchemas = tableSchemas
+ .Where(r => !string.IsNullOrEmpty(r.TableName) && !string.IsNullOrEmpty(r.ColumnName))
+ .ToArray();
+
+ foreach (var table in tableSchemas.GroupBy(s => s.TableName))
+ {
+ var columns = table.Select(s => new ColumnSymbol(s.ColumnName, GetKustoType(s.ColumnType))).ToList();
+ var tableSymbol = new TableSymbol(table.Key, columns);
+ members.Add(tableSymbol);
+ }
+
+ var functionSchemas = await dataSource.ExecuteControlCommandAsync(".show functions", throwOnError, cancellationToken).ConfigureAwait(false);
+ if (functionSchemas == null)
+ return null;
+
+ foreach (var fun in functionSchemas)
+ {
+ var parameters = TranslateParameters(fun.Parameters);
+ var functionSymbol = new FunctionSymbol(fun.Name, fun.Body, parameters);
+ members.Add(functionSymbol);
+ }
+
+ var databaseSymbol = new DatabaseSymbol(databaseName, members);
+ return databaseSymbol;
+ }
+
+ public static CompletionItemKind CreateCompletionItemKind(CompletionKind kustoKind)
+ {
+ CompletionItemKind kind = CompletionItemKind.Variable;
+ switch (kustoKind)
+ {
+ case CompletionKind.Syntax:
+ kind = CompletionItemKind.Module;
+ break;
+ case CompletionKind.Column:
+ kind = CompletionItemKind.Field;
+ break;
+ case CompletionKind.Variable:
+ kind = CompletionItemKind.Variable;
+ break;
+ case CompletionKind.Table:
+ kind = CompletionItemKind.File;
+ break;
+ case CompletionKind.Database:
+ kind = CompletionItemKind.Method;
+ break;
+ case CompletionKind.LocalFunction:
+ case CompletionKind.DatabaseFunction:
+ case CompletionKind.BuiltInFunction:
+ case CompletionKind.AggregateFunction:
+ kind = CompletionItemKind.Function;
+ break;
+ default:
+ kind = CompletionItemKind.Keyword;
+ break;
+ }
+
+ return kind;
+ }
+
+ ///
+ /// Gets default keyword when user if not connected to any Kusto cluster.
+ ///
+ public static LanguageServices.Contracts.CompletionItem[] GetDefaultKeywords(ScriptDocumentInfo scriptDocumentInfo, Position textDocumentPosition){
+ var kustoCodeService = new KustoCodeService(scriptDocumentInfo.Contents, GlobalState.Default);
+ var script = CodeScript.From(scriptDocumentInfo.Contents, GlobalState.Default);
+ script.TryGetTextPosition(textDocumentPosition.Line + 1, textDocumentPosition.Character, out int position); // Gets the actual offset based on line and local offset
+ var completion = kustoCodeService.GetCompletionItems(position);
+
+ List completions = new List();
+ foreach (var autoCompleteItem in completion.Items)
+ {
+ var label = autoCompleteItem.DisplayText;
+ // convert the completion item candidates into vscode format CompletionItems
+ completions.Add(AutoCompleteHelper.CreateCompletionItem(label, label + " keyword", label, CompletionItemKind.Keyword, scriptDocumentInfo.StartLine, scriptDocumentInfo.StartColumn, textDocumentPosition.Character));
+ }
+
+ return completions.ToArray();
+ }
+
+ ///
+ /// Gets default diagnostics when user if not connected to any Kusto cluster.
+ ///
+ public static ScriptFileMarker[] GetDefaultDiagnostics(ScriptParseInfo parseInfo, ScriptFile scriptFile, string queryText){
+ var kustoCodeService = new KustoCodeService(queryText, GlobalState.Default);
+ var script = CodeScript.From(queryText, GlobalState.Default);
+ var parseResult = kustoCodeService.GetDiagnostics();
+
+ parseInfo.ParseResult = parseResult;
+
+ // build a list of Kusto script file markers from the errors.
+ List markers = new List();
+ if (parseResult != null && parseResult.Count() > 0)
+ {
+ foreach (var error in parseResult)
+ {
+ script.TryGetLineAndOffset(error.Start, out var startLine, out var startOffset);
+ script.TryGetLineAndOffset(error.End, out var endLine, out var endOffset);
+
+ // vscode specific format for error markers.
+ markers.Add(new ScriptFileMarker()
+ {
+ Message = error.Message,
+ Level = ScriptFileMarkerLevel.Error,
+ ScriptRegion = new ScriptRegion()
+ {
+ File = scriptFile.FilePath,
+ StartLineNumber = startLine,
+ StartColumnNumber = startOffset,
+ StartOffset = 0,
+ EndLineNumber = endLine,
+ EndColumnNumber = endOffset,
+ EndOffset = 0
+ }
+ });
+ }
+ }
+
+ return markers.ToArray();
+ }
+
+ ///
+ /// Loads the schema for the specified database and returns a new with the database added or updated.
+ ///
+ public static async Task AddOrUpdateDatabaseAsync(IDataSource dataSource, GlobalState globals, string databaseName, string clusterName, bool throwOnError)
+ { // try and show error from here.
+ DatabaseSymbol databaseSymbol = null;
+
+ if(databaseName != null){
+ databaseSymbol = await LoadDatabaseAsync(dataSource, databaseName, throwOnError).ConfigureAwait(false);
+ }
+
+ if(databaseSymbol == null){
+ return globals;
+ }
+
+ var cluster = globals.GetCluster(clusterName);
+ if (cluster == null)
+ {
+ cluster = new ClusterSymbol(clusterName, new[] { databaseSymbol }, isOpen: true);
+ globals = globals.AddOrUpdateCluster(cluster);
+ }
+ else
+ {
+ cluster = cluster.AddOrUpdateDatabase(databaseSymbol);
+ globals = globals.AddOrUpdateCluster(cluster);
+ }
+
+ globals = globals.WithCluster(cluster).WithDatabase(databaseSymbol);
+
+ return globals;
+ }
+
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceIntellisense/ScriptParseInfo.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceIntellisense/ScriptParseInfo.cs
new file mode 100644
index 00000000..d92433a3
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceIntellisense/ScriptParseInfo.cs
@@ -0,0 +1,48 @@
+//
+// 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 Kusto.Language;
+using Kusto.Language.Editor;
+
+namespace Microsoft.Kusto.ServiceLayer.DataSource.DataSourceIntellisense
+{
+ ///
+ /// Data Source specific class for storing cached metadata regarding a parsed KQL file.
+ ///
+ public class ScriptParseInfo
+ {
+ private object buildingMetadataLock = new object();
+
+ ///
+ /// Event which tells if MetadataProvider is built fully or not
+ ///
+ public object BuildingMetadataLock
+ {
+ get { return this.buildingMetadataLock; }
+ }
+
+ ///
+ /// Gets or sets a flag determining is the LanguageService is connected
+ ///
+ public bool IsConnected { get; set; }
+
+ ///
+ /// Gets or sets the binding queue connection context key
+ ///
+ public string ConnectionKey { get; set; }
+
+ ///
+ /// Gets or sets the previous Kusto diagnostics result. TODOKusto: Check exact usage.
+ ///
+ public IReadOnlyList ParseResult { get; set; }
+
+ ///
+ /// Gets or sets the current autocomplete suggestion list retrieved from the Kusto language library.
+ /// So that other details like documentation can be later retrieved in ResolveCompletionItem.
+ ///
+ public IEnumerable CurrentSuggestions { get; set; }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceType.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceType.cs
new file mode 100644
index 00000000..911a9116
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/DataSourceType.cs
@@ -0,0 +1,28 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource
+{
+ ///
+ /// Represents the type of a data source.
+ ///
+ public enum DataSourceType
+ {
+ ///
+ /// Unknown.
+ ///
+ None,
+
+ ///
+ /// A Kusto cluster.
+ ///
+ Kusto,
+
+ ///
+ /// An Application Insights subscription.
+ ///
+ ApplicationInsights,
+
+ ///
+ /// An Operations Management Suite (OMS) Log Analytics workspace.
+ ///
+ OmsLogAnalytics
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/DiagnosticsInfo.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/DiagnosticsInfo.cs
new file mode 100644
index 00000000..2a567a20
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/DiagnosticsInfo.cs
@@ -0,0 +1,17 @@
+using System.Collections.Generic;
+
+namespace Microsoft.Kusto.ServiceLayer.DataSource
+{
+ public class DiagnosticsInfo
+ {
+ ///
+ /// Gets or sets the options
+ ///
+ public Dictionary Options { get; set; }
+
+ public DiagnosticsInfo()
+ {
+ Options = new Dictionary();
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/IDataSource.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/IDataSource.cs
new file mode 100644
index 00000000..03eeab25
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/IDataSource.cs
@@ -0,0 +1,132 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Threading;
+using System.Threading.Tasks;
+using Microsoft.Kusto.ServiceLayer.DataSource.DataSourceIntellisense;
+using Microsoft.Kusto.ServiceLayer.DataSource.Metadata;
+using Microsoft.Kusto.ServiceLayer.LanguageServices;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Completion;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
+using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.DataSource
+{
+ ///
+ /// Represents data source utilities.
+ ///
+ public interface IDataSource : IDisposable
+ {
+ ///
+ /// The data source type.
+ ///
+ DataSourceType DataSourceType { get; }
+
+ ///
+ /// The cluster/server name.
+ ///
+ string ClusterName { get; }
+
+ ///
+ /// The current database name, if there is one.
+ ///
+ string DatabaseName { get; set; }
+
+ ///
+ /// Executes a query.
+ ///
+ /// The query.
+ /// The results.
+ Task ExecuteQueryAsync(string query, CancellationToken cancellationToken, string databaseName = null);
+
+ ///
+ /// Executes a Kusto query that returns a scalar value.
+ ///
+ /// The type of the result.
+ /// The query.
+ /// The result.
+ Task ExecuteScalarQueryAsync(string query, CancellationToken cancellationToken, string databaseName = null);
+
+ ///
+ /// Executes a Kusto query that returns a scalar value.
+ ///
+ /// The type of the result.
+ /// The query.
+ /// The result.
+ Task> ExecuteControlCommandAsync(string command, bool throwOnError, CancellationToken cancellationToken);
+
+ ///
+ /// Get children of the given parent
+ ///
+ /// Parent object metadata.
+ /// Metadata for all children.
+ DiagnosticsInfo GetDiagnostics(DataSourceObjectMetadata parentMetadata);
+
+ ///
+ /// Get children of the given parent
+ ///
+ /// Parent object metadata.
+ ///
+ /// Metadata for all children.
+ IEnumerable GetChildObjects(DataSourceObjectMetadata parentMetadata, bool includeSizeDetails = false);
+
+ ///
+ /// Refresh object list for entire cluster.
+ ///
+ void Refresh();
+
+ ///
+ /// Refresh object list for given object.
+ ///
+ /// Object metadata.
+ void Refresh(DataSourceObjectMetadata objectMetadata);
+
+ ///
+ /// Updates database and affected variables like GlobalState for given object.
+ ///
+ /// Object metadata.
+ void UpdateDatabase(string databaseName);
+
+ ///
+ /// Gets autocomplete suggestions at given position.
+ ///
+ /// Object metadata.
+ CompletionItem[] GetAutoCompleteSuggestions(ScriptDocumentInfo queryText, Position index, bool throwOnError = false);
+ ///
+ /// Gets quick info hover tooltips for the current position.
+ ///
+ /// Object metadata.
+ Hover GetHoverHelp(ScriptDocumentInfo scriptDocumentInfo, Position textPosition, bool throwOnError = false);
+
+ ///
+ /// Gets definition for a selected query text.
+ ///
+ /// Object metadata.
+ DefinitionResult GetDefinition(string queryText, int index, int startLine, int startColumn, bool throwOnError = false);
+
+ ///
+ /// Gets a list of semantic diagnostic marks for the provided script file
+ ///
+ /// Object metadata.
+ ScriptFileMarker[] GetSemanticMarkers(ScriptParseInfo parseInfo, ScriptFile scriptFile, string queryText);
+
+ ///
+ /// Tells whether the data source exists.
+ ///
+ /// true if it exists; false otherwise.
+ Task Exists();
+
+ ///
+ /// Tells whether the object exists.
+ ///
+ /// true if it exists; false otherwise.
+ bool Exists(DataSourceObjectMetadata objectMetadata);
+
+ ///
+ /// Gets FunctionInfo object for a function
+ ///
+ ///
+ ///
+ string GenerateAlterFunctionScript(string functionName);
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/KustoDataSource.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/KustoDataSource.cs
new file mode 100644
index 00000000..f54ffb86
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/KustoDataSource.cs
@@ -0,0 +1,1045 @@
+//
+// Copyright (c) Microsoft. All Rights Reserved.
+//
+using System;
+using System.Collections.Generic;
+using System.Threading;
+using System.Collections.Concurrent;
+using System.Data;
+using System.Data.SqlClient;
+using System.Diagnostics;
+using System.Globalization;
+using System.Linq;
+using System.Text;
+using Newtonsoft.Json;
+using System.Threading.Tasks;
+using Kusto.Cloud.Platform.Data;
+using Kusto.Data;
+using Kusto.Data.Common;
+using Kusto.Data.Data;
+using Kusto.Data.Net.Client;
+using Kusto.Language;
+using Kusto.Language.Editor;
+using Microsoft.Kusto.ServiceLayer.DataSource.DataSourceIntellisense;
+using Microsoft.Kusto.ServiceLayer.DataSource.Metadata;
+using Microsoft.Kusto.ServiceLayer.DataSource.Models;
+using Microsoft.Kusto.ServiceLayer.LanguageServices;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Completion;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
+using Microsoft.Kusto.ServiceLayer.Utility;
+using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.DataSource
+{
+ ///
+ /// Represents Kusto utilities.
+ ///
+ public class KustoDataSource : DataSourceBase
+ {
+ private ICslQueryProvider _kustoQueryProvider;
+
+ private ICslAdminProvider _kustoAdminProvider;
+
+ ///
+ /// List of databases.
+ ///
+ private IEnumerable _databaseMetadata;
+
+ ///
+ /// List of tables per database. Key - Parent Folder or Database Urn
+ ///
+ private ConcurrentDictionary> _tableMetadata = new ConcurrentDictionary>();
+
+ ///
+ /// List of columns per table. Key - DatabaseName.TableName
+ ///
+ private ConcurrentDictionary> _columnMetadata = new ConcurrentDictionary>();
+
+ ///
+ /// List of tables per database. Key - Parent Folder or Database Urn
+ ///
+ private ConcurrentDictionary> _folderMetadata = new ConcurrentDictionary>();
+
+ ///
+ /// List of functions per database. Key - Parent Folder or Database Urn
+ ///
+ private ConcurrentDictionary> _functionMetadata = new ConcurrentDictionary>();
+
+ // Some clusters have this signature. Queries might slightly differ for Aria
+ private const string AriaProxyURL = "kusto.aria.microsoft.com";
+
+ ///
+ /// The database schema query. Performance: ".show database schema" is more efficient than ".show schema",
+ /// especially for large clusters with many databases or tables.
+ ///
+ private const string ShowDatabaseSchema = ".show database [{0}] schema";
+
+ ///
+ /// The dashboard needs a list of all tables regardless of the folder structure of the table. The
+ /// tables are stored with the key in the following format: OnlyTables.ClusterName.DatabaseName
+ ///
+ private const string DatabaseKeyPrefix = "OnlyTables";
+
+ ///
+ /// Prevents a default instance of the class from being created.
+ ///
+ public KustoDataSource(string connectionString, string azureAccountToken)
+ {
+ ClusterName = GetClusterName(connectionString);
+ DatabaseName = GetDatabaseName(connectionString);
+ UserToken = azureAccountToken;
+ SchemaState = Task.Run(() => KustoIntellisenseHelper.AddOrUpdateDatabaseAsync(this, GlobalState.Default, DatabaseName, ClusterName, throwOnError: false)).Result;
+ // Check if a connection can be made
+ ValidationUtils.IsTrue(Exists().Result, $"Unable to connect. ClusterName = {ClusterName}, DatabaseName = {DatabaseName}");
+ }
+
+ ///
+ /// Extracts the cluster name from the connectionstring. The string looks like the following:
+ /// "Data Source=clustername.kusto.windows.net;User ID=;Password=;Pooling=False;Application Name=azdata-GeneralConnection"
+ ///
+ /// A connection string coming over the Data management protocol
+ private static string GetClusterName(string connectionString)
+ {
+ var csb = new SqlConnectionStringBuilder(connectionString);
+
+ // If there is no https:// prefix, add it
+ Uri uri;
+ if ((Uri.TryCreate(csb.DataSource, UriKind.Absolute, out uri) || Uri.TryCreate("https://" + csb.DataSource, UriKind.Absolute, out uri)) &&
+ (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
+ {
+ return uri.AbsoluteUri;
+ }
+
+ throw new ArgumentException("Expected a URL of the form clustername.kusto.windows.net");
+ }
+
+ ///
+ /// Extracts the database name from the connectionstring, if it exists
+ ///
+ /// A connection string coming over the Data management protocol
+ private static string GetDatabaseName(string connectionString)
+ {
+ var csb = new SqlConnectionStringBuilder(connectionString);
+
+ return csb.InitialCatalog;
+ }
+
+ ///
+ /// SchemaState used for getting intellisense info.
+ ///
+ public GlobalState SchemaState { get; private set; }
+
+ ///
+ /// The AAD user token.
+ ///
+ public string UserToken { get; private set; }
+
+ ///
+ /// The AAD application client id.
+ ///
+ public string ApplicationClientId { get; private set; }
+
+ ///
+ /// The AAD application client key.
+ ///
+ public string ApplicationKey { get; private set; }
+
+ // The Kusto query provider.
+ [DebuggerBrowsable(DebuggerBrowsableState.Never)]
+ private ICslQueryProvider KustoQueryProvider
+ {
+ get
+ {
+ if (_kustoQueryProvider == null)
+ {
+ var kcsb = GetKustoConnectionStringBuilder();
+ _kustoQueryProvider = KustoClientFactory.CreateCslQueryProvider(kcsb);
+ }
+
+ return _kustoQueryProvider;
+ }
+ }
+
+ [DebuggerBrowsable(DebuggerBrowsableState.Never)]
+ private ICslAdminProvider KustoAdminProvider
+ {
+ get
+ {
+ if (_kustoAdminProvider == null)
+ {
+ var kcsb = GetKustoConnectionStringBuilder();
+ _kustoAdminProvider = KustoClientFactory.CreateCslAdminProvider(kcsb);
+ if (!string.IsNullOrWhiteSpace(DatabaseName))
+ {
+ _kustoAdminProvider.DefaultDatabaseName = DatabaseName;
+ }
+ }
+
+ return _kustoAdminProvider;
+ }
+ }
+
+ ///
+ /// Disposes resources.
+ ///
+ /// True if disposing. False otherwise.
+ protected override void Dispose(bool disposing)
+ {
+ // Dispose managed resources.
+ if (disposing)
+ {
+ _kustoQueryProvider?.Dispose();
+ _kustoQueryProvider = null;
+
+ _kustoAdminProvider?.Dispose();
+ _kustoAdminProvider = null;
+ }
+
+ base.Dispose(disposing);
+ }
+
+ #region DataSourceUtils
+
+ ///
+ /// Executes a query.
+ ///
+ /// The query.
+ /// The results.
+ public override Task ExecuteQueryAsync(string query, CancellationToken cancellationToken, string databaseName = null)
+ {
+ var reader = ExecuteQuery(query, cancellationToken, databaseName);
+ return Task.FromResult(reader);
+ }
+
+ private IDataReader ExecuteQuery(string query, CancellationToken cancellationToken, string databaseName = null)
+ {
+ ValidationUtils.IsArgumentNotNullOrWhiteSpace(query, nameof(query));
+
+ var clientRequestProperties = new ClientRequestProperties
+ {
+ ClientRequestId = Guid.NewGuid().ToString()
+ };
+ clientRequestProperties.SetOption(ClientRequestProperties.OptionNoTruncation, true);
+
+ if(cancellationToken != null)
+ {
+ cancellationToken.Register(() => CancelQuery(clientRequestProperties.ClientRequestId));
+ }
+
+ IDataReader origReader = KustoQueryProvider.ExecuteQuery(
+ KustoQueryUtils.IsClusterLevelQuery(query) ? "" : databaseName,
+ query,
+ clientRequestProperties);
+
+ return new KustoResultsReader(origReader);
+ }
+
+ private void CancelQuery(string clientRequestId)
+ {
+ var query = ".cancel query " + clientRequestId;
+ CancellationTokenSource source = new CancellationTokenSource();
+ CancellationToken token = source.Token;
+
+ using (var reader = ExecuteQuery(query, token))
+ {
+ // No-op
+ }
+ }
+
+ ///
+ public override async Task Exists()
+ {
+ try
+ {
+ CancellationTokenSource source = new CancellationTokenSource();
+ CancellationToken token = source.Token;
+
+ var count = await ExecuteScalarQueryAsync(".show databases | count", token);
+ return count >= 0;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ #endregion
+
+ ///
+ /// Executes a Kusto control command.
+ ///
+ /// The command.
+ public void ExecuteControlCommand(string command)
+ {
+ ValidationUtils.IsArgumentNotNullOrWhiteSpace(command, nameof(command));
+
+ using (var adminOutput = KustoAdminProvider.ExecuteControlCommand(command, null))
+ {
+ }
+ }
+
+ private KustoConnectionStringBuilder GetKustoConnectionStringBuilder()
+ {
+ ValidationUtils.IsNotNull(ClusterName, nameof(ClusterName));
+ ValidationUtils.IsTrue(
+ !string.IsNullOrWhiteSpace(UserToken)
+ || (!string.IsNullOrWhiteSpace(ApplicationClientId) && !string.IsNullOrWhiteSpace(ApplicationKey)),
+ $"the Kusto authentication is not specified - either set {nameof(UserToken)}, or set {nameof(ApplicationClientId)} and {nameof(ApplicationKey)}");
+
+ var kcsb = new KustoConnectionStringBuilder
+ {
+ DataSource = ClusterName,
+
+ // Perform federated auth based on the AAD user token, or based on the AAD application client id and key.
+ FederatedSecurity = true
+ };
+
+ if (!string.IsNullOrWhiteSpace(DatabaseName))
+ {
+ kcsb.InitialCatalog = DatabaseName;
+ }
+
+ if (!string.IsNullOrWhiteSpace(UserToken))
+ {
+ kcsb.UserToken = UserToken;
+ }
+
+ if (!string.IsNullOrWhiteSpace(ApplicationClientId))
+ {
+ kcsb.ApplicationClientId = ApplicationClientId;
+ }
+
+ if (!string.IsNullOrWhiteSpace(ApplicationKey))
+ {
+ kcsb.ApplicationKey = ApplicationKey;
+ }
+
+ return kcsb;
+ }
+
+ #region IDataSource
+
+ protected DiagnosticsInfo GetClusterDiagnostics(){
+ CancellationTokenSource source = new CancellationTokenSource();
+ CancellationToken token = source.Token;
+ DiagnosticsInfo clusterDiagnostics = new DiagnosticsInfo();
+
+ var query = ".show diagnostics | extend Passed= (IsHealthy) and not(IsScaleOutRequired) | extend Summary = strcat('Cluster is ', iif(Passed, '', 'NOT'), 'healthy.'),Details=pack('MachinesTotal', MachinesTotal, 'DiskCacheCapacity', round(ClusterDataCapacityFactor,1)) | project Action = 'Cluster Diagnostics', Category='Info', Summary, Details;";
+ using (var reader = ExecuteQuery(query, token))
+ {
+ while(reader.Read())
+ {
+ var details = JsonConvert.DeserializeObject>(reader["Details"].ToString());
+ clusterDiagnostics.Options["summary"] = reader["Summary"].ToString();
+ clusterDiagnostics.Options["machinesTotal"] = details["MachinesTotal"].ToString();
+ clusterDiagnostics.Options["diskCacheCapacity"] = details["DiskCacheCapacity"].ToString() + "%";
+ }
+ }
+
+ return clusterDiagnostics;
+ }
+
+ ///
+ private IEnumerable GetDatabaseMetadata(bool includeSizeDetails)
+ {
+ if (_databaseMetadata == null)
+ {
+ CancellationTokenSource source = new CancellationTokenSource();
+ CancellationToken token = source.Token;
+
+ // Getting database names when we are connected to a specific database should not happen.
+ ValidationUtils.IsNotNull(DatabaseName, nameof(DatabaseName));
+
+ var query = ".show databases" + (this.ClusterName.IndexOf(AriaProxyURL, StringComparison.CurrentCultureIgnoreCase) == -1 ? " | project DatabaseName, PrettyName" : "");
+
+ if (includeSizeDetails == true){
+ query = ".show cluster extents | summarize sum(OriginalSize) by tostring(DatabaseName)";
+ }
+
+ using (var reader = ExecuteQuery(query, token))
+ {
+ _databaseMetadata = reader.ToEnumerable()
+ .Where(row => !string.IsNullOrWhiteSpace(row["DatabaseName"].ToString()))
+ .Select(row => new DatabaseMetadata
+ {
+ ClusterName = this.ClusterName,
+ MetadataType = DataSourceMetadataType.Database,
+ MetadataTypeName = DataSourceMetadataType.Database.ToString(),
+ SizeInMB = includeSizeDetails == true ? row["sum_OriginalSize"].ToString() : null,
+ Name = row["DatabaseName"].ToString(),
+ PrettyName = includeSizeDetails == true ? row["DatabaseName"].ToString(): (String.IsNullOrEmpty(row["PrettyName"]?.ToString()) ? row["DatabaseName"].ToString() : row["PrettyName"].ToString()),
+ Urn = $"{this.ClusterName}.{row["DatabaseName"].ToString()}"
+ })
+ .Materialize()
+ .OrderBy(row => row.Name, StringComparer.Ordinal); // case-sensitive
+ }
+ }
+
+ return _databaseMetadata;
+ }
+
+ ///
+ public override bool Exists(DataSourceObjectMetadata objectMetadata)
+ {
+ ValidationUtils.IsNotNull(objectMetadata, "Need a datasource object");
+
+ switch(objectMetadata.MetadataType)
+ {
+ case DataSourceMetadataType.Database: return DatabaseExists(objectMetadata.Name).Result;
+ default: throw new ArgumentException($"Unexpected type {objectMetadata.MetadataType}.");
+ }
+ }
+
+ ///
+ /// Executes a query or command against a kusto cluster and returns a sequence of result row instances.
+ ///
+ public override async Task> ExecuteControlCommandAsync(string command, bool throwOnError, CancellationToken cancellationToken)
+ {
+ try
+ {
+ var resultReader = await ExecuteQueryAsync(command, cancellationToken, DatabaseName);
+ var results = KustoDataReaderParser.ParseV1(resultReader, null);
+ var tableReader = results[WellKnownDataSet.PrimaryResult].Single().TableData.CreateDataReader();
+ return new ObjectReader(tableReader);
+ }
+ catch (Exception) when (!throwOnError)
+ {
+ return null;
+ }
+ }
+
+ ///
+ public override void UpdateDatabase(string databaseName){
+ DatabaseName = databaseName;
+ SchemaState = Task.Run(() => KustoIntellisenseHelper.AddOrUpdateDatabaseAsync(this, GlobalState.Default, DatabaseName, ClusterName, throwOnError: false)).Result;
+ }
+
+ ///
+ public override LanguageServices.Contracts.CompletionItem[] GetAutoCompleteSuggestions(ScriptDocumentInfo scriptDocumentInfo, Position textPosition, bool throwOnError = false){
+ var kustoCodeService = new KustoCodeService(scriptDocumentInfo.Contents, SchemaState);
+ var script = CodeScript.From(scriptDocumentInfo.Contents, SchemaState);
+ script.TryGetTextPosition(textPosition.Line + 1, textPosition.Character, out int position); // Gets the actual offset based on line and local offset
+
+ var completion = kustoCodeService.GetCompletionItems(position);
+ scriptDocumentInfo.ScriptParseInfo.CurrentSuggestions = completion.Items; // this is declaration item so removed for now, but keep the info when api gets updated
+
+ List completions = new List();
+ foreach (var autoCompleteItem in completion.Items)
+ {
+ var label = autoCompleteItem.DisplayText;
+ completions.Add(AutoCompleteHelper.CreateCompletionItem(label, label + " keyword", label, KustoIntellisenseHelper.CreateCompletionItemKind(autoCompleteItem.Kind), scriptDocumentInfo.StartLine, scriptDocumentInfo.StartColumn, textPosition.Character));
+ }
+
+ return completions.ToArray();
+ }
+
+ ///
+ public override Hover GetHoverHelp(ScriptDocumentInfo scriptDocumentInfo, Position textPosition, bool throwOnError = false){
+ var kustoCodeService = new KustoCodeService(scriptDocumentInfo.Contents, SchemaState);
+ var script = CodeScript.From(scriptDocumentInfo.Contents, SchemaState);
+ script.TryGetTextPosition(textPosition.Line + 1, textPosition.Character, out int position);
+
+ var quickInfo = kustoCodeService.GetQuickInfo(position);
+
+ return AutoCompleteHelper.ConvertQuickInfoToHover(
+ quickInfo.Text,
+ "kusto",
+ scriptDocumentInfo.StartLine,
+ scriptDocumentInfo.StartColumn,
+ textPosition.Character);
+
+ }
+
+ ///
+ public override DefinitionResult GetDefinition(string queryText, int index, int startLine, int startColumn, bool throwOnError = false){
+ var abc = KustoCode.ParseAndAnalyze(queryText, SchemaState); //TODOKusto: API wasnt working properly, need to check that part.
+ var kustoCodeService = new KustoCodeService(abc);
+ //var kustoCodeService = new KustoCodeService(queryText, globals);
+ var relatedInfo = kustoCodeService.GetRelatedElements(index);
+
+ if (relatedInfo != null && relatedInfo.Elements.Count > 1)
+ {
+
+ }
+
+ return null;
+ }
+
+ ///
+ public override ScriptFileMarker[] GetSemanticMarkers(ScriptParseInfo parseInfo, ScriptFile scriptFile, string queryText)
+ {
+ var kustoCodeService = new KustoCodeService(queryText, SchemaState);
+ var script = CodeScript.From(queryText, SchemaState);
+ var parseResult = kustoCodeService.GetDiagnostics();
+
+
+ parseInfo.ParseResult = parseResult;
+
+ // build a list of Kusto script file markers from the errors.
+ List markers = new List();
+ if (parseResult != null && parseResult.Count() > 0)
+ {
+ foreach (var error in parseResult)
+ {
+ script.TryGetLineAndOffset(error.Start, out var startLine, out var startOffset);
+ script.TryGetLineAndOffset(error.End, out var endLine, out var endOffset);
+
+ // vscode specific format for error markers.
+ markers.Add(new ScriptFileMarker()
+ {
+ Message = error.Message,
+ Level = ScriptFileMarkerLevel.Error,
+ ScriptRegion = new ScriptRegion()
+ {
+ File = scriptFile.FilePath,
+ StartLineNumber = startLine,
+ StartColumnNumber = startOffset,
+ StartOffset = 0,
+ EndLineNumber = endLine,
+ EndColumnNumber = endOffset,
+ EndOffset = 0
+ }
+ });
+ }
+ }
+
+ return markers.ToArray();
+ }
+
+ ///
+ public override void Refresh()
+ {
+ // This class caches objects. Throw them away so that the next call will re-query the data source for the objects.
+ _databaseMetadata = null;
+ _tableMetadata = new ConcurrentDictionary>();
+ _columnMetadata = new ConcurrentDictionary>();
+ _folderMetadata = new ConcurrentDictionary>();
+ _functionMetadata = new ConcurrentDictionary>();
+ }
+
+ ///
+ public override void Refresh(DataSourceObjectMetadata objectMetadata)
+ {
+ ValidationUtils.IsNotNull(objectMetadata, nameof(objectMetadata));
+
+ switch(objectMetadata.MetadataType)
+ {
+ case DataSourceMetadataType.Cluster:
+ Refresh();
+ break;
+
+ case DataSourceMetadataType.Database:
+ _tableMetadata.TryRemove(objectMetadata.Name, out _);
+ break;
+
+ case DataSourceMetadataType.Table:
+ var tm = objectMetadata as TableMetadata;
+ _columnMetadata.TryRemove(GenerateMetadataKey(tm.DatabaseName, tm.Name), out _);
+ break;
+
+ case DataSourceMetadataType.Column:
+ // Remove column metadata for the whole table
+ var cm = objectMetadata as ColumnMetadata;
+ _columnMetadata.TryRemove(GenerateMetadataKey(cm.DatabaseName, cm.TableName), out _);
+ break;
+
+ case DataSourceMetadataType.Function:
+ var fm = objectMetadata as FunctionMetadata;
+ _functionMetadata.TryRemove(GenerateMetadataKey(fm.DatabaseName, fm.Name), out _);
+ break;
+
+ case DataSourceMetadataType.Folder:
+ var folder = objectMetadata as FolderMetadata;
+ _folderMetadata.TryRemove(GenerateMetadataKey(folder.ParentMetadata.Name, folder.Name), out _);
+ break;
+
+ default:
+ throw new ArgumentException($"Unexpected type {objectMetadata.MetadataType}.");
+ }
+ }
+
+ ///
+ public override IEnumerable GetChildObjects(DataSourceObjectMetadata objectMetadata, bool includeSizeDetails)
+ {
+ ValidationUtils.IsNotNull(objectMetadata, nameof(objectMetadata));
+
+ switch(objectMetadata.MetadataType)
+ {
+ case DataSourceMetadataType.Cluster: // show databases
+ return GetDatabaseMetadata(includeSizeDetails);
+
+ case DataSourceMetadataType.Database: // show folders, tables, and functions
+ return GetDatabaseSchema(objectMetadata, includeSizeDetails);
+
+ case DataSourceMetadataType.Table: // show columns
+ var table = objectMetadata as TableMetadata;
+ return GetTableSchema(table);
+
+ case DataSourceMetadataType.Folder: // show subfolders, functions, and tables
+ var folder = objectMetadata as FolderMetadata;
+ return GetAllMetadata(folder.Urn);
+
+ default:
+ throw new ArgumentException($"Unexpected type {objectMetadata.MetadataType}.");
+ }
+ }
+
+ public override DiagnosticsInfo GetDiagnostics(DataSourceObjectMetadata objectMetadata)
+ {
+ ValidationUtils.IsNotNull(objectMetadata, nameof(objectMetadata));
+
+ // Add more cases when required.
+ switch(objectMetadata.MetadataType)
+ {
+ case DataSourceMetadataType.Cluster:
+ return GetClusterDiagnostics();
+
+ default:
+ throw new ArgumentException($"Unexpected type {objectMetadata.MetadataType}.");
+ }
+ }
+
+ internal async Task DatabaseExists(string databaseName)
+ {
+ ValidationUtils.IsArgumentNotNullOrWhiteSpace(databaseName, nameof(databaseName));
+
+ try
+ {
+ CancellationTokenSource source = new CancellationTokenSource();
+ CancellationToken token = source.Token;
+
+ var count = await ExecuteScalarQueryAsync(".show tables | count", token, databaseName);
+ return count >= 0;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ ///
+ private IEnumerable GetDatabaseSchema(DataSourceObjectMetadata objectMetadata, bool includeSizeDetails)
+ {
+ // Check if the database exists
+ ValidationUtils.IsTrue(DatabaseExists(objectMetadata.Name).Result, $"Database '{objectMetadata}' does not exist.");
+
+ var allMetadata = GetAllMetadata(objectMetadata.Urn);
+
+ // if the records have already been loaded them return them
+ if (allMetadata.Any())
+ {
+ return allMetadata;
+ }
+
+ LoadTableSchema(objectMetadata);
+ LoadFunctionSchema(objectMetadata);
+
+ if (!includeSizeDetails)
+ {
+ return GetAllMetadata(objectMetadata.Urn);
+ }
+
+ string newKey = $"{DatabaseKeyPrefix}.{objectMetadata.Urn}";
+ return _tableMetadata[newKey].OrderBy(x => x.PrettyName, StringComparer.OrdinalIgnoreCase);
+ }
+
+ private IEnumerable GetAllMetadata(string key)
+ {
+ var returnList = new List();
+
+ if (_folderMetadata.ContainsKey(key))
+ {
+ returnList.AddRange(_folderMetadata[key]
+ .OrderBy(x => x.PrettyName, StringComparer.OrdinalIgnoreCase));
+ }
+
+ if (_tableMetadata.ContainsKey(key))
+ {
+ returnList.AddRange(_tableMetadata[key]
+ .OrderBy(x => x.PrettyName, StringComparer.OrdinalIgnoreCase));
+ }
+
+ if (_functionMetadata.ContainsKey(key))
+ {
+ returnList.AddRange(_functionMetadata[key]
+ .OrderBy(x => x.PrettyName, StringComparer.OrdinalIgnoreCase));
+ }
+
+ return returnList;
+ }
+
+ ///
+ /// Gets column data which includes tables and table folders.
+ ///
+ ///
+ ///
+ ///
+ private IEnumerable GetColumnInfos(string databaseName, string tableName)
+ {
+ ValidationUtils.IsNotNullOrWhitespace(databaseName, nameof(databaseName));
+
+ CancellationTokenSource source = new CancellationTokenSource();
+ CancellationToken token = source.Token;
+
+ const string systemPrefix = "System.";
+ var query = new StringBuilder(string.Format(CultureInfo.InvariantCulture, ShowDatabaseSchema,
+ databaseName));
+ query.Append($" | where TableName == '{tableName}' ");
+ query.Append(" | project TableName, ColumnName, ColumnType, Folder");
+
+ using (var reader = ExecuteQuery(query.ToString(), token, databaseName))
+ {
+ var columns = reader.ToEnumerable()
+ .Select(row => new ColumnInfo
+ {
+ Table = row["TableName"]?.ToString(),
+ Name = row["ColumnName"]?.ToString(),
+ DataType = row["ColumnType"]?.ToString().TrimPrefix(systemPrefix),
+ Folder = row["Folder"]?.ToString()
+ })
+ .Materialize()
+ .OrderBy(row => row.Name, StringComparer.Ordinal); // case-sensitive
+
+ return columns;
+ }
+ }
+
+ private IEnumerable GetTableInfos(string databaseName)
+ {
+ CancellationTokenSource source = new CancellationTokenSource();
+ CancellationToken token = source.Token;
+
+ string query = $".show database {databaseName} cslschema";
+
+ using (var reader = ExecuteQuery(query, token, databaseName))
+ {
+ return reader.ToEnumerable()
+ .Select(row => new TableInfo
+ {
+ TableName = row["TableName"]?.ToString(),
+ Folder = row["Folder"]?.ToString()
+ })
+ .Materialize();
+ }
+ }
+
+ private void LoadTableSchema(DataSourceObjectMetadata databaseMetadata)
+ {
+ var tableInfos = GetTableInfos(databaseMetadata.Name);
+
+ if (!tableInfos.Any())
+ {
+ return;
+ }
+
+ var rootTableFolderKey = new StringBuilder($"{databaseMetadata.Urn}");
+ if (tableInfos.Any(x => !string.IsNullOrWhiteSpace(x.Folder)))
+ {
+ // create Table folder to hold functions tables
+ var tableFolder = DataSourceFactory.CreateFolderMetadata(databaseMetadata, rootTableFolderKey.ToString(), "Tables");
+ _folderMetadata.AddRange(rootTableFolderKey.ToString(), new List {tableFolder});
+ rootTableFolderKey.Append($".{tableFolder.Name}");
+
+ SetFolderMetadataForTables(databaseMetadata, tableInfos, rootTableFolderKey.ToString());
+ }
+
+ SetTableMetadata(databaseMetadata, tableInfos, rootTableFolderKey.ToString());
+ }
+
+ private IEnumerable GetTableSchema(TableMetadata tableMetadata)
+ {
+ var key = GenerateMetadataKey(tableMetadata.DatabaseName, tableMetadata.Name);
+ if (_columnMetadata.ContainsKey(key))
+ {
+ return _columnMetadata[key];
+ }
+
+ IEnumerable columnInfos = GetColumnInfos(tableMetadata.DatabaseName, tableMetadata.Name);
+
+ if (!columnInfos.Any())
+ {
+ return Enumerable.Empty();
+ }
+
+ SetColumnMetadata(tableMetadata.DatabaseName, tableMetadata.Name, columnInfos);
+
+ return _columnMetadata[key];
+ }
+
+ private void SetFolderMetadataForTables(DataSourceObjectMetadata objectMetadata, IEnumerable tableInfos, string rootTableFolderKey)
+ {
+ var tablesByFolder = tableInfos
+ .GroupBy(x => x.Folder, StringComparer.OrdinalIgnoreCase)
+ .ToList();
+
+ var tableFolders = new List();
+
+ foreach (var columnGroup in tablesByFolder)
+ {
+ // skip tables without folders
+ if (string.IsNullOrWhiteSpace(columnGroup.Key))
+ {
+ continue;
+ }
+
+ var folder = DataSourceFactory.CreateFolderMetadata(objectMetadata, rootTableFolderKey, columnGroup.Key);
+ tableFolders.Add(folder);
+ }
+
+ _folderMetadata.AddRange(rootTableFolderKey, tableFolders);
+ }
+
+ private void LoadFunctionSchema(DataSourceObjectMetadata databaseMetadata)
+ {
+ IEnumerable functionInfos = GetFunctionInfos(databaseMetadata.Name);
+
+ if (!functionInfos.Any())
+ {
+ return;
+ }
+
+ // create Functions folder to hold functions folders
+ var rootFunctionFolderKey = $"{databaseMetadata.Urn}";
+ var rootFunctionFolder = DataSourceFactory.CreateFolderMetadata(databaseMetadata, rootFunctionFolderKey, "Functions");
+ _folderMetadata.AddRange(rootFunctionFolderKey, new List {rootFunctionFolder});
+
+ // create each folder to hold functions
+ var functionsGroupByFolder = functionInfos
+ .GroupBy(x => x.Folder, StringComparer.OrdinalIgnoreCase)
+ .ToList();
+
+ if (functionsGroupByFolder.Any())
+ {
+ SetFolderMetadataForFunctions(databaseMetadata, functionsGroupByFolder, rootFunctionFolder);
+ }
+
+ SetFunctionMetadata(databaseMetadata.Name, rootFunctionFolder.Name, functionsGroupByFolder);
+ }
+
+ private void SetFolderMetadataForFunctions(DataSourceObjectMetadata databaseMetadata, List> functionsGroupByFolder,
+ FolderMetadata functionFolder)
+ {
+ var functionFolders = new Dictionary>();
+
+ foreach (var functionGroup in functionsGroupByFolder)
+ {
+ // skip functions with no folder
+ if (string.IsNullOrWhiteSpace(functionGroup.Key))
+ {
+ continue;
+ }
+
+ // folders are in the following format: folder1/folder2/folder3/folder4
+ var subFolders = functionGroup.Key.Replace(@"\", @"/").Split(@"/");
+ var topFolder = subFolders.First();
+
+ var folderKey = functionFolder.Urn;
+ var folder = DataSourceFactory.CreateFolderMetadata(databaseMetadata, folderKey, topFolder);
+ functionFolders.SafeAdd(folderKey, folder);
+
+ for (int i = 1; i < subFolders.Length; i++)
+ {
+ folderKey = $"{folderKey}.{subFolders[i - 1]}";
+ var subFolder = DataSourceFactory.CreateFolderMetadata(databaseMetadata, folderKey, subFolders[i]);
+ functionFolders.SafeAdd(folderKey, subFolder);
+ }
+ }
+
+ foreach (var folder in functionFolders)
+ {
+ _folderMetadata.AddRange(folder.Key, folder.Value.Values.ToList());
+ }
+ }
+
+ private void SetColumnMetadata(string databaseName, string tableName, IEnumerable columnInfos)
+ {
+ var columns = columnInfos
+ .Where(row =>
+ !string.IsNullOrWhiteSpace(row.Table)
+ && !string.IsNullOrWhiteSpace(row.Name)
+ && !string.IsNullOrWhiteSpace(row.DataType));
+
+ var columnMetadatas = new SortedList();
+
+ foreach (ColumnInfo columnInfo in columns)
+ {
+ var column = new ColumnMetadata
+ {
+ ClusterName = ClusterName,
+ DatabaseName = databaseName,
+ TableName = tableName,
+ MetadataType = DataSourceMetadataType.Column,
+ MetadataTypeName = DataSourceMetadataType.Column.ToString(),
+ Name = columnInfo.Name,
+ PrettyName = columnInfo.Name,
+ Urn = $"{ClusterName}.{databaseName}.{tableName}.{columnInfo.Name}",
+ DataType = columnInfo.DataType
+ };
+
+ columnMetadatas.Add(column.PrettyName, column);
+ }
+
+ _columnMetadata[GenerateMetadataKey(databaseName, tableName)] = columnMetadatas.Values;
+ }
+
+ private void SetTableMetadata(DataSourceObjectMetadata databaseName, IEnumerable tableInfos, string rootTableFolderKey)
+ {
+ var tableFolders = new Dictionary>
+ {
+ {$"{DatabaseKeyPrefix}.{databaseName.Urn}", new List()}
+ };
+
+ foreach (var table in tableInfos)
+ {
+ var tableKey = new StringBuilder(rootTableFolderKey);
+
+ if (!string.IsNullOrWhiteSpace(table.Folder))
+ {
+ tableKey.Append($".{table.Folder}");
+ }
+
+ var tableMetadata = new TableMetadata
+ {
+ ClusterName = ClusterName,
+ DatabaseName = databaseName.Name,
+ MetadataType = DataSourceMetadataType.Table,
+ MetadataTypeName = DataSourceMetadataType.Table.ToString(),
+ Name = table.TableName,
+ PrettyName = table.TableName,
+ Folder = table.Folder,
+ Urn = $"{tableKey}.{table.TableName}"
+ };
+
+ if (tableFolders.ContainsKey(tableKey.ToString()))
+ {
+ tableFolders[tableKey.ToString()].Add(tableMetadata);
+ }
+ else
+ {
+ tableFolders[tableKey.ToString()] = new List{tableMetadata};
+ }
+
+ // keep a list of all tables for the database
+ // this is used for the dashboard
+ tableFolders[$"{DatabaseKeyPrefix}.{databaseName.Urn}"].Add(tableMetadata);
+ }
+
+ foreach (var table in tableFolders)
+ {
+ _tableMetadata.AddRange(table.Key, table.Value);
+ }
+ }
+
+ private void SetFunctionMetadata(string databaseName, string rootFunctionFolderKey,
+ List> functionGroupByFolder)
+ {
+ foreach (var functionGroup in functionGroupByFolder)
+ {
+ var stringBuilder = new StringBuilder(rootFunctionFolderKey);
+
+ if (!string.IsNullOrWhiteSpace(functionGroup.Key))
+ {
+ stringBuilder.Append(".");
+
+ // folders are in the following format: folder1/folder2/folder3/folder4
+ var folderKey = functionGroup.Key
+ .Replace(@"\", ".")
+ .Replace(@"/", ".");
+
+ stringBuilder.Append(folderKey);
+ }
+
+ var functionKey = $"{ClusterName}.{databaseName}.{stringBuilder}";
+ var functions = new List();
+
+ foreach (FunctionInfo functionInfo in functionGroup)
+ {
+ var function = new FunctionMetadata
+ {
+ DatabaseName = databaseName,
+ Parameters = functionInfo.Parameters,
+ Body = functionInfo.Body,
+ MetadataType = DataSourceMetadataType.Function,
+ MetadataTypeName = DataSourceMetadataType.Function.ToString(),
+ Name = $"{functionInfo.Name}{functionInfo.Parameters}",
+ PrettyName = functionInfo.Name,
+ Urn = $"{functionKey}.{functionInfo.Name}"
+ };
+
+ functions.Add(function);
+ }
+
+ _functionMetadata.AddRange(functionKey, functions);
+ }
+ }
+
+ private IEnumerable GetFunctionInfos(string databaseName)
+ {
+ CancellationTokenSource source = new CancellationTokenSource();
+ CancellationToken token = source.Token;
+
+ string query = ".show functions";
+
+ using (var reader = ExecuteQuery(query, token, databaseName))
+ {
+ return reader.ToEnumerable()
+ .Select(row => new FunctionInfo
+ {
+ Name = row["Name"]?.ToString(),
+ Body = row["Body"]?.ToString(),
+ DocString = row["DocString"]?.ToString(),
+ Folder = row["Folder"]?.ToString(),
+ Parameters = row["Parameters"]?.ToString()
+ })
+ .Materialize();
+ }
+ }
+
+ private FunctionInfo GetFunctionInfo(string functionName)
+ {
+ CancellationTokenSource source = new CancellationTokenSource();
+ CancellationToken token = source.Token;
+
+ string query = $".show function {functionName}";
+
+ using (var reader = ExecuteQuery(query, token, DatabaseName))
+ {
+ return reader.ToEnumerable()
+ .Select(row => new FunctionInfo
+ {
+ Name = row["Name"]?.ToString(),
+ Body = row["Body"]?.ToString(),
+ DocString = row["DocString"]?.ToString(),
+ Folder = row["Folder"]?.ToString(),
+ Parameters = row["Parameters"]?.ToString()
+ })
+ .FirstOrDefault();
+ }
+ }
+
+ public override string GenerateAlterFunctionScript(string functionName)
+ {
+ var functionInfo = GetFunctionInfo(functionName);
+
+ if (functionInfo == null)
+ {
+ return string.Empty;
+ }
+
+ var alterCommand = new StringBuilder();
+
+ alterCommand.Append(".alter function with ");
+ alterCommand.Append($"(folder = \"{functionInfo.Folder}\", docstring = \"{functionInfo.DocString}\", skipvalidation = \"false\" ) ");
+ alterCommand.Append($"{functionInfo.Name}{functionInfo.Parameters} ");
+ alterCommand.Append($"{functionInfo.Body}");
+
+ return alterCommand.ToString();
+ }
+
+ private string GenerateMetadataKey(string databaseName, string objectName)
+ {
+ return string.IsNullOrWhiteSpace(objectName) ? databaseName : $"{databaseName}.{objectName}";
+ }
+ #endregion
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/KustoQueryUtils.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/KustoQueryUtils.cs
new file mode 100644
index 00000000..bfc2a201
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/KustoQueryUtils.cs
@@ -0,0 +1,115 @@
+//
+// Copyright (c) Microsoft. All Rights Reserved.
+//
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Text.RegularExpressions;
+using System.Linq;
+using Microsoft.Kusto.ServiceLayer.DataSource.Metadata;
+
+namespace Microsoft.Kusto.ServiceLayer.DataSource
+{
+ public static class KustoQueryUtils
+ {
+ public const string StatementSeparator = "\n | "; // Start each statement on a new line. Not required by Kusto, but doing this for readability of scripts generated from here.
+
+ ///
+ /// Escape table/column/database names for a Kusto query.
+ ///
+ /// The name to be escaped
+ /// Always escape if this flag is set
+ /// The escaped string
+ public static string EscapeName(string name, bool alwaysEscape = false)
+ {
+ if (name.StartsWith("[@") || name == "*") // Field already escaped. No escaping required for '*' operand
+ {
+ return name;
+ }
+
+ string result = name;
+ Regex rx = new Regex("[^_a-zA-Z0-9]");
+ string [] kustoKeywordList = {"and", "anomalychart", "areachart", "asc", "barchart", "between", "bool", "boolean", "by",
+ "columnchart", "consume", "contains", "containscs", "count", "date", "datetime", "default", "desc", "distinct",
+ "double", "dynamic", "endswith", "evaluate", "extend", "false", "filter", "find", "first", "flags", "float",
+ "getschema", "has", "hasprefix", "hassuffix", "in", "int", "join", "journal", "kind", "ladderchart", "last",
+ "like", "limit", "linechart", "long", "materialize", "mvexpand", "notcontains", "notlike", "of", "or", "order",
+ "parse", "piechart", "pivotchart", "print", "project", "queries", "real", "regex", "sample", "scatterchart",
+ "search", "set", "sort", "stacked", "stacked100", "stackedareachart", "startswith", "string", "summarize",
+ "take", "time", "timechart", "timeline", "timepivot", "timespan", "to", "top", "toscalar", "true", "union",
+ "unstacked", "viewers", "where", "withsource"}; // add more keywords here
+
+ var escapeName = rx.IsMatch(name) || kustoKeywordList.Any(name.Contains) || alwaysEscape;
+ if (escapeName)
+ {
+ if (name.IndexOf('"') > -1)
+ {
+ result = "[@'" + name + "']";
+ }
+ else
+ {
+ result = "[@\"" + name + "\"]";
+ }
+ }
+
+ return result;
+ }
+
+
+ public static bool IsClusterLevelQuery(string query)
+ {
+ string [] clusterLevelQueryPrefixes = {
+ ".show databases",
+ ".show schema"
+ };
+
+ return clusterLevelQueryPrefixes.Any(query.StartsWith);
+ }
+
+ ///
+ /// Adds an object of type DataSourceObjectMetadata to a dictionary>. If the key exists then the item is added
+ /// to the list. If not then the key is created and then added.
+ ///
+ /// The dictionary of the dictionary that the list should be added to.
+ /// The key to be added.
+ /// The metadata to be added to the list.
+ ///
+ public static void SafeAdd(this Dictionary> dictionary, string key,
+ T metadata) where T : DataSourceObjectMetadata
+ {
+ if (dictionary.ContainsKey(key))
+ {
+ if (dictionary[key].ContainsKey(metadata.Name))
+ {
+ return;
+ }
+
+ dictionary[key].Add(metadata.Name, metadata);
+ }
+ else
+ {
+ dictionary[key] = new Dictionary {{metadata.Name, metadata}};
+ }
+ }
+
+ ///
+ /// Add a range to a dictionary of ConcurrentDictionary. Adds range to existing IEnumerable within dictionary
+ /// at the same key.
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static void AddRange(this ConcurrentDictionary> dictionary, string key,
+ List metadatas) where T : DataSourceObjectMetadata
+ {
+ if (dictionary.ContainsKey(key))
+ {
+ metadatas.AddRange(dictionary[key]);
+ }
+
+ dictionary[key] = metadatas.OrderBy(x => x.PrettyName, StringComparer.OrdinalIgnoreCase).ToList();
+ }
+
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/KustoResultsReader.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/KustoResultsReader.cs
new file mode 100644
index 00000000..c5bea819
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/KustoResultsReader.cs
@@ -0,0 +1,22 @@
+using System.Data;
+
+namespace Microsoft.Kusto.ServiceLayer.DataSource
+{
+ internal class KustoResultsReader : DataReaderWrapper
+ {
+ public KustoResultsReader(IDataReader reader)
+ : base(reader)
+ {
+ }
+
+ ///
+ /// Kusto returns 3 results tables - QueryResults, QueryProperties, QueryStatus. When returning query results
+ /// we want the caller to only read the first table. We override the NextResult function here to only return one table
+ /// from the IDataReader.
+ ///
+ /*public override bool NextResult()
+ {
+ return false;
+ }*/
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/ColumnMetadata.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/ColumnMetadata.cs
new file mode 100644
index 00000000..183e9d1f
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/ColumnMetadata.cs
@@ -0,0 +1,11 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource.Metadata
+{
+ ///
+ /// Column metadata information
+ ///
+ public class ColumnMetadata : TableMetadata
+ {
+ public string TableName { get; set; }
+ public string DataType { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/DataSourceMetadataType.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/DataSourceMetadataType.cs
new file mode 100644
index 00000000..097b0974
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/DataSourceMetadataType.cs
@@ -0,0 +1,15 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource.Metadata
+{
+ ///
+ /// Metadata type enumeration
+ ///
+ public enum DataSourceMetadataType
+ {
+ Cluster = 0,
+ Database = 1,
+ Table = 2,
+ Column = 3,
+ Function = 4,
+ Folder = 5
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/DataSourceObjectMetadata.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/DataSourceObjectMetadata.cs
new file mode 100644
index 00000000..97d98c5a
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/DataSourceObjectMetadata.cs
@@ -0,0 +1,20 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource.Metadata
+{
+ ///
+ /// Object metadata information
+ ///
+ public class DataSourceObjectMetadata
+ {
+ public DataSourceMetadataType MetadataType { get; set; }
+
+ public string MetadataTypeName { get; set; }
+
+ public string Name { get; set; }
+
+ public string PrettyName { get; set; }
+
+ public string Urn { get; set; }
+
+ public string SizeInMB { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/DatabaseMetadata.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/DatabaseMetadata.cs
new file mode 100644
index 00000000..1b6e072a
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/DatabaseMetadata.cs
@@ -0,0 +1,10 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource.Metadata
+{
+ ///
+ /// Database metadata information
+ ///
+ public class DatabaseMetadata : DataSourceObjectMetadata
+ {
+ public string ClusterName { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/FolderMetadata.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/FolderMetadata.cs
new file mode 100644
index 00000000..eabde584
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/FolderMetadata.cs
@@ -0,0 +1,10 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource.Metadata
+{
+ ///
+ /// Folder metadata information
+ ///
+ public class FolderMetadata : DataSourceObjectMetadata
+ {
+ public DataSourceObjectMetadata ParentMetadata { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/FunctionMetadata.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/FunctionMetadata.cs
new file mode 100644
index 00000000..f0134cf4
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/FunctionMetadata.cs
@@ -0,0 +1,11 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource.Metadata
+{
+ public class FunctionMetadata : DatabaseMetadata
+ {
+ public string DatabaseName { get; set; }
+
+ public string Parameters { get; set; }
+
+ public string Body { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/TableMetadata.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/TableMetadata.cs
new file mode 100644
index 00000000..578e35cb
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/Metadata/TableMetadata.cs
@@ -0,0 +1,11 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource.Metadata
+{
+ ///
+ /// Database metadata information
+ ///
+ public class TableMetadata : DatabaseMetadata
+ {
+ public string DatabaseName { get; set; }
+ public string Folder { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/Models/ColumnInfo.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/Models/ColumnInfo.cs
new file mode 100644
index 00000000..01514f42
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/Models/ColumnInfo.cs
@@ -0,0 +1,26 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource.Models
+{
+ public class ColumnInfo
+ {
+ ///
+ /// The table name.
+ ///
+ public string Table { get; set; }
+
+ ///
+ /// The column name.
+ ///
+ public string Name { get; set; }
+
+ ///
+ /// The data type.
+ ///
+ public string DataType { get; set; }
+
+
+ ///
+ /// The folder name.
+ ///
+ public string Folder { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/Models/FunctionInfo.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/Models/FunctionInfo.cs
new file mode 100644
index 00000000..9db9096a
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/Models/FunctionInfo.cs
@@ -0,0 +1,11 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource.Models
+{
+ public class FunctionInfo
+ {
+ public string Name { get; set; }
+ public string Parameters { get; set; }
+ public string Body { get; set; }
+ public string Folder { get; set; }
+ public string DocString { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/Models/TableInfo.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/Models/TableInfo.cs
new file mode 100644
index 00000000..ad1c51f2
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/Models/TableInfo.cs
@@ -0,0 +1,8 @@
+namespace Microsoft.Kusto.ServiceLayer.DataSource.Models
+{
+ public class TableInfo
+ {
+ public string TableName { get; set; }
+ public string Folder { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/src/Microsoft.Kusto.ServiceLayer/DataSource/ReliableDataSourceConnection.cs b/src/Microsoft.Kusto.ServiceLayer/DataSource/ReliableDataSourceConnection.cs
new file mode 100644
index 00000000..dd1e0bb5
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/DataSource/ReliableDataSourceConnection.cs
@@ -0,0 +1,258 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+// This code is copied from the source described in the comment below.
+
+// =======================================================================================
+// Microsoft Windows Server AppFabric Customer Advisory Team (CAT) Best Practices Series
+//
+// This sample is supplemental to the technical guidance published on the community
+// blog at http://blogs.msdn.com/appfabriccat/ and copied from
+// sqlmain ./sql/manageability/mfx/common/
+//
+// =======================================================================================
+// Copyright © 2012 Microsoft Corporation. All rights reserved.
+//
+// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
+// EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. YOU BEAR THE RISK OF USING IT.
+// =======================================================================================
+
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Collections;
+using System.Data.Common;
+using System.Data.SqlClient;
+using System.Diagnostics;
+using System.Globalization;
+using System.Text;
+using System.Threading;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+using Microsoft.SqlTools.Utility;
+using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
+using Microsoft.Kusto.ServiceLayer.DataSource;
+
+namespace Microsoft.Kusto.ServiceLayer.Connection
+{
+ ///
+ /// Provides a reliable way of opening connections to and executing commands
+ /// taking into account potential network unreliability and a requirement for connection retry.
+ ///
+ public sealed partial class ReliableDataSourceConnection : IDisposable
+ {
+ private IDataSource _dataSource;
+ private readonly RetryPolicy _connectionRetryPolicy;
+ private RetryPolicy _commandRetryPolicy;
+ private Guid _azureSessionId = Guid.NewGuid();
+
+ private string _connectionString;
+ private string _azureAccountToken;
+
+ ///
+ /// Initializes a new instance of the ReliableKustoClient class with a given connection string
+ /// and a policy defining whether to retry a request if the connection fails to be opened or a command
+ /// fails to be successfully executed.
+ ///
+ /// The connection string used to open the SQL Azure database.
+ /// The retry policy defining whether to retry a request if a connection fails to be established.
+ /// The retry policy defining whether to retry a request if a command fails to be executed.
+ public ReliableDataSourceConnection(string connectionString, RetryPolicy connectionRetryPolicy, RetryPolicy commandRetryPolicy, string azureAccountToken)
+ {
+ _connectionString = connectionString;
+ _azureAccountToken = azureAccountToken;
+ _dataSource = DataSourceFactory.Create(DataSourceType.Kusto, connectionString, azureAccountToken);
+
+ _connectionRetryPolicy = connectionRetryPolicy ?? RetryPolicyFactory.CreateNoRetryPolicy();
+ _commandRetryPolicy = commandRetryPolicy ?? RetryPolicyFactory.CreateNoRetryPolicy();
+
+ _connectionRetryPolicy.RetryOccurred += RetryConnectionCallback;
+ _commandRetryPolicy.RetryOccurred += RetryCommandCallback;
+ }
+
+ private void RetryCommandCallback(RetryState retryState)
+ {
+ RetryPolicyUtils.RaiseSchemaAmbientRetryMessage(retryState, SqlSchemaModelErrorCodes.ServiceActions.CommandRetry, _azureSessionId);
+ }
+
+ private void RetryConnectionCallback(RetryState retryState)
+ {
+ RetryPolicyUtils.RaiseSchemaAmbientRetryMessage(retryState, SqlSchemaModelErrorCodes.ServiceActions.ConnectionRetry, _azureSessionId);
+ }
+
+ ///
+ /// Disposes resources.
+ ///
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ ///
+ /// Performs application-defined tasks associated with freeing, releasing, or
+ /// resetting managed and unmanaged resources.
+ ///
+ /// A flag indicating that managed resources must be released.
+ public void Dispose(bool disposing)
+ {
+ if (disposing)
+ {
+ if (_connectionRetryPolicy != null)
+ {
+ _connectionRetryPolicy.RetryOccurred -= RetryConnectionCallback;
+ }
+
+ if (_commandRetryPolicy != null)
+ {
+ _commandRetryPolicy.RetryOccurred -= RetryCommandCallback;
+ }
+
+ _dataSource.Dispose();
+ }
+ }
+
+ ///
+ /// Gets or sets the connection string for opening a connection to the SQL Azure database.
+ ///
+ public string ConnectionString { get; set; }
+
+ ///
+ /// Gets the policy which decides whether to retry a connection request, based on how many
+ /// times the request has been made and the reason for the last failure.
+ ///
+ public RetryPolicy ConnectionRetryPolicy
+ {
+ get { return _connectionRetryPolicy; }
+ }
+
+ ///
+ /// Gets the policy which decides whether to retry a command, based on how many
+ /// times the request has been made and the reason for the last failure.
+ ///
+ public RetryPolicy CommandRetryPolicy
+ {
+ get { return _commandRetryPolicy; }
+ set
+ {
+ Validate.IsNotNull(nameof(value), value);
+
+ if (_commandRetryPolicy != null)
+ {
+ _commandRetryPolicy.RetryOccurred -= RetryCommandCallback;
+ }
+
+ _commandRetryPolicy = value;
+ _commandRetryPolicy.RetryOccurred += RetryCommandCallback;
+ }
+ }
+
+ ///
+ /// Gets the server name from the underlying connection.
+ ///
+ public string ClusterName
+ {
+ get { return _dataSource.ClusterName; }
+ }
+
+ ///
+ /// If the underlying SqlConnection absolutely has to be accessed, for instance
+ /// to pass to external APIs that require this type of connection, then this
+ /// can be used.
+ ///
+ ///
+ public IDataSource GetUnderlyingConnection()
+ {
+ return _dataSource;
+ }
+
+ ///
+ /// Changes the current database for an open Connection object.
+ ///
+ /// The name of the database to use in place of the current database.
+ public void ChangeDatabase(string databaseName)
+ {
+ _dataSource.UpdateDatabase(databaseName);
+ }
+
+ ///
+ /// Opens a database connection with the settings specified by the ConnectionString
+ /// property of the provider-specific Connection object.
+ ///
+ public void Open()
+ {
+ // TODOKusto: Should we initialize in the constructor or here. Set a breapoint and check.
+ // Check if retry policy was specified, if not, disable retries by executing the Open method using RetryPolicy.NoRetry.
+ if(_dataSource == null)
+ {
+ _connectionRetryPolicy.ExecuteAction(() =>
+ {
+ _dataSource = DataSourceFactory.Create(DataSourceType.Kusto, _connectionString, _azureAccountToken);
+ });
+ }
+ }
+
+ ///
+ /// Opens a database connection with the settings specified by the ConnectionString
+ /// property of the provider-specific Connection object.
+ ///
+ public Task OpenAsync(CancellationToken token)
+ {
+ // Make sure that the token isn't cancelled before we try
+ if (token.IsCancellationRequested)
+ {
+ return Task.FromCanceled(token);
+ }
+
+ // Check if retry policy was specified, if not, disable retries by executing the Open method using RetryPolicy.NoRetry.
+ try
+ {
+ return _connectionRetryPolicy.ExecuteAction(async () =>
+ {
+ await Task.Run(() => Open());
+ });
+ }
+ catch (Exception e)
+ {
+ return Task.FromException(e);
+ }
+ }
+
+ ///
+ /// Closes the connection to the database.
+ ///
+ public void Close()
+ {
+ }
+
+ ///
+ /// Gets the time to wait while trying to establish a connection before terminating
+ /// the attempt and generating an error.
+ ///
+ public int ConnectionTimeout
+ {
+ get { return 30; }
+ }
+
+ ///
+ /// Gets the name of the current database or the database to be used after a
+ /// connection is opened.
+ ///
+ public string Database
+ {
+ get { return _dataSource.DatabaseName; }
+ }
+
+ private void VerifyConnectionOpen(ReliableDataSourceConnection conn)
+ {
+ if(conn.GetUnderlyingConnection() == null)
+ {
+ conn.Open();
+ }
+ }
+ }
+}
+
diff --git a/src/Microsoft.Kusto.ServiceLayer/Formatter/Contracts/DocumentFormatting.cs b/src/Microsoft.Kusto.ServiceLayer/Formatter/Contracts/DocumentFormatting.cs
new file mode 100644
index 00000000..dc09a86d
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Formatter/Contracts/DocumentFormatting.cs
@@ -0,0 +1,113 @@
+//
+// 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.Hosting.Protocol.Contracts;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
+using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.Formatter.Contracts
+{
+ ///
+ /// A formatting request to process an entire document
+ ///
+ public class DocumentFormattingRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("textDocument/formatting");
+ }
+
+ ///
+ /// A formatting request to process a specific range inside a document
+ ///
+ public class DocumentRangeFormattingRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("textDocument/rangeFormatting");
+ }
+
+ ///
+ /// A formatting request to handle a user typing, giving a chance to update the text based on this
+ ///
+ public class DocumentOnTypeFormattingRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("textDocument/onTypeFormatting");
+ }
+
+
+ ///
+ /// Params for the
+ ///
+ public class DocumentFormattingParams
+ {
+
+ ///
+ /// The document to format.
+ ///
+ public TextDocumentIdentifier TextDocument { get; set; }
+
+ ///
+ /// The formatting options
+ ///
+ public FormattingOptions Options { get; set; }
+
+ }
+
+
+ ///
+ /// Params for the
+ ///
+ public class DocumentRangeFormattingParams : DocumentFormattingParams
+ {
+
+ ///
+ /// The range to format
+ ///
+ public Range Range { get; set; }
+
+ }
+
+ ///
+ /// Params for the
+ ///
+ public class DocumentOnTypeFormattingParams : DocumentFormattingParams
+ {
+ ///
+ /// The position at which this request was sent.
+
+ ///
+ Position Position { get; set; }
+
+ ///
+ /// The character that has been typed.
+
+ ///
+ string Ch { get; set; }
+ }
+
+ ///
+ /// Value-object describing what options formatting should use.
+ ///
+ public class FormattingOptions
+ {
+ ///
+ /// Size of a tab in spaces
+ ///
+ public int TabSize { get; set; }
+
+ ///
+ /// Prefer spaces over tabs.
+ ///
+ public bool InsertSpaces { get; set; }
+
+ // TODO there may be other options passed by VSCode - format is
+ // [key: string]: boolean | number | string;
+ // Determine how these might be passed and add them here
+}
+
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Formatter/Impl/FormatOptions.cs b/src/Microsoft.Kusto.ServiceLayer/Formatter/Impl/FormatOptions.cs
new file mode 100644
index 00000000..82ff23ec
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Formatter/Impl/FormatOptions.cs
@@ -0,0 +1,171 @@
+//
+// 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.ComponentModel;
+
+namespace Microsoft.Kusto.ServiceLayer.Formatter
+{
+
+ public enum CasingOptions { None, Uppercase, Lowercase };
+
+ ///
+ /// The supported options to use when formatting text
+ ///
+ public class FormatOptions : INotifyPropertyChanged
+ {
+
+ private int spacesPerIndent;
+ private bool useTabs = false;
+ private bool encloseIdentifiersInSquareBrackets;
+ private bool placeCommasBeforeNextStatement;
+ private bool placeEachReferenceOnNewLineInQueryStatements;
+ private CasingOptions keywordCasing;
+ private CasingOptions datatypeCasing;
+ private bool alignColumnDefinitionsInColumns;
+
+ internal FormatOptions()
+ {
+ SpacesPerIndent = 4;
+ UseTabs = false;
+ PlaceCommasBeforeNextStatement = false;
+ EncloseIdentifiersInSquareBrackets = false;
+ PlaceEachReferenceOnNewLineInQueryStatements = false;
+ }
+
+ public int SpacesPerIndent
+ {
+ get { return spacesPerIndent; }
+ set { spacesPerIndent = value;
+ RaisePropertyChanged("SpacesPerIndent"); }
+ }
+
+ public bool UseTabs
+ {
+ get { return useTabs; }
+ set
+ {
+ useTabs = value;
+ // raise UseTabs & UseSpaces property changed events
+ RaisePropertyChanged("UseTabs");
+ RaisePropertyChanged("UseSpaces");
+ }
+ }
+
+ public bool UseSpaces
+ {
+ get { return !UseTabs; }
+ set { UseTabs = !value; }
+ }
+
+ public bool EncloseIdentifiersInSquareBrackets
+ {
+ get { return encloseIdentifiersInSquareBrackets; }
+ set
+ {
+ encloseIdentifiersInSquareBrackets = value;
+ RaisePropertyChanged("EncloseIdentifiersInSquareBrackets");
+ }
+ }
+
+ public bool PlaceCommasBeforeNextStatement
+ {
+ get { return placeCommasBeforeNextStatement; }
+ set
+ {
+ placeCommasBeforeNextStatement = value;
+ RaisePropertyChanged("PlaceCommasBeforeNextStatement");
+ }
+ }
+
+ public bool PlaceEachReferenceOnNewLineInQueryStatements
+ {
+ get { return placeEachReferenceOnNewLineInQueryStatements; }
+ set
+ {
+ placeEachReferenceOnNewLineInQueryStatements = value;
+ RaisePropertyChanged("PlaceEachReferenceOnNewLineInQueryStatements");
+ }
+ }
+
+ public CasingOptions KeywordCasing
+ {
+ get { return keywordCasing; }
+ set
+ {
+ keywordCasing = value;
+ RaisePropertyChanged("KeywordCasing");
+ }
+ }
+
+ public bool UppercaseKeywords
+ {
+ get { return KeywordCasing == CasingOptions.Uppercase; }
+ }
+ public bool LowercaseKeywords
+ {
+ get { return KeywordCasing == CasingOptions.Lowercase; }
+ }
+
+ public bool DoNotFormatKeywords
+ {
+ get { return KeywordCasing == CasingOptions.None; }
+ }
+
+ public CasingOptions DatatypeCasing
+ {
+ get { return datatypeCasing; }
+ set
+ {
+ datatypeCasing = value;
+ RaisePropertyChanged("DatatypeCasing");
+ }
+ }
+
+ public bool UppercaseDataTypes
+ {
+ get { return DatatypeCasing == CasingOptions.Uppercase; }
+ }
+ public bool LowercaseDataTypes
+ {
+ get { return DatatypeCasing == CasingOptions.Lowercase; }
+ }
+ public bool DoNotFormatDataTypes
+ {
+ get { return DatatypeCasing == CasingOptions.None; }
+ }
+
+ public bool AlignColumnDefinitionsInColumns
+ {
+ get { return alignColumnDefinitionsInColumns; }
+ set
+ {
+ alignColumnDefinitionsInColumns = value;
+ RaisePropertyChanged("AlignColumnDefinitionsInColumns");
+ }
+ }
+
+ private void RaisePropertyChanged(string propertyName)
+ {
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ public static void Copy(FormatOptions target, FormatOptions source)
+ {
+ target.AlignColumnDefinitionsInColumns = source.AlignColumnDefinitionsInColumns;
+ target.DatatypeCasing = source.DatatypeCasing;
+ target.EncloseIdentifiersInSquareBrackets = source.EncloseIdentifiersInSquareBrackets;
+ target.KeywordCasing = source.KeywordCasing;
+ target.PlaceCommasBeforeNextStatement = source.PlaceCommasBeforeNextStatement;
+ target.PlaceEachReferenceOnNewLineInQueryStatements = source.PlaceEachReferenceOnNewLineInQueryStatements;
+ target.SpacesPerIndent = source.SpacesPerIndent;
+ target.UseSpaces = source.UseSpaces;
+ target.UseTabs = source.UseTabs;
+ }
+ }
+
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/Formatter/TSqlFormatterService.cs b/src/Microsoft.Kusto.ServiceLayer/Formatter/TSqlFormatterService.cs
new file mode 100644
index 00000000..faeee98d
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/Formatter/TSqlFormatterService.cs
@@ -0,0 +1,320 @@
+//
+// 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;
+using System.Composition;
+using System.Diagnostics;
+using System.IO;
+using System.Threading.Tasks;
+//using Kusto.Language;
+//using Kusto.Language.Editor;
+using Microsoft.SqlTools.Extensibility;
+using Microsoft.SqlTools.Hosting;
+using Microsoft.SqlTools.Hosting.Protocol;
+using Microsoft.Kusto.ServiceLayer.Formatter.Contracts;
+using Microsoft.Kusto.ServiceLayer.LanguageServices;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
+using Microsoft.Kusto.ServiceLayer.SqlContext;
+using Microsoft.Kusto.ServiceLayer.Workspace;
+using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
+using Microsoft.SqlTools.Utility;
+using Range = Microsoft.Kusto.ServiceLayer.Workspace.Contracts.Range;
+
+namespace Microsoft.Kusto.ServiceLayer.Formatter
+{
+
+ [Export(typeof(IHostedService))]
+ public class TSqlFormatterService : HostedService, IComposableService
+ {
+ private FormatterSettings settings;
+ ///
+ /// The default constructor is required for MEF-based composable services
+ ///
+ public TSqlFormatterService()
+ {
+ settings = new FormatterSettings();
+ }
+
+
+
+ public override void InitializeService(IProtocolEndpoint serviceHost)
+ {
+ Logger.Write(TraceEventType.Verbose, "TSqlFormatter initialized");
+ serviceHost.SetRequestHandler(DocumentFormattingRequest.Type, HandleDocFormatRequest);
+ serviceHost.SetRequestHandler(DocumentRangeFormattingRequest.Type, HandleDocRangeFormatRequest);
+ WorkspaceService?.RegisterConfigChangeCallback(HandleDidChangeConfigurationNotification);
+ }
+
+ ///
+ /// Gets the workspace service. Note: should handle case where this is null in cases where unit tests do not set this up
+ ///
+ private WorkspaceService WorkspaceService
+ {
+ get { return ServiceProvider.GetService>(); }
+ }
+
+ ///
+ /// Gets the language service. Note: should handle case where this is null in cases where unit tests do not set this up
+ ///
+ private LanguageService LanguageService
+ {
+ get { return ServiceProvider.GetService(); }
+ }
+
+ ///
+ /// Ensure formatter settings are always up to date
+ ///
+ public Task HandleDidChangeConfigurationNotification(
+ SqlToolsSettings newSettings,
+ SqlToolsSettings oldSettings,
+ EventContext eventContext)
+ {
+ // update the current settings to reflect any changes (assuming formatter settings exist)
+ settings = newSettings?.SqlTools?.Format ?? settings;
+ return Task.FromResult(true);
+ }
+
+ public async Task HandleDocFormatRequest(DocumentFormattingParams docFormatParams, RequestContext requestContext)
+ {
+ Func> requestHandler = () =>
+ {
+ return FormatAndReturnEdits(docFormatParams);
+ };
+ await HandleRequest(requestHandler, requestContext, "HandleDocFormatRequest");
+
+ DocumentStatusHelper.SendTelemetryEvent(requestContext, CreateTelemetryProps(isDocFormat: true));
+ }
+
+ public async Task HandleDocRangeFormatRequest(DocumentRangeFormattingParams docRangeFormatParams, RequestContext requestContext)
+ {
+ Func> requestHandler = () =>
+ {
+ return FormatRangeAndReturnEdits(docRangeFormatParams);
+ };
+ await HandleRequest(requestHandler, requestContext, "HandleDocRangeFormatRequest");
+
+ DocumentStatusHelper.SendTelemetryEvent(requestContext, CreateTelemetryProps(isDocFormat: false));
+ }
+ private static TelemetryProperties CreateTelemetryProps(bool isDocFormat)
+ {
+ return new TelemetryProperties
+ {
+ Properties = new Dictionary
+ {
+ { TelemetryPropertyNames.FormatType,
+ isDocFormat ? TelemetryPropertyNames.DocumentFormatType : TelemetryPropertyNames.RangeFormatType }
+ },
+ EventName = TelemetryEventNames.FormatCode
+ };
+ }
+
+ private async Task FormatRangeAndReturnEdits(DocumentRangeFormattingParams docFormatParams)
+ {
+ return await Task.Factory.StartNew(() =>
+ {
+ if (ShouldSkipFormatting(docFormatParams))
+ {
+ return Array.Empty();
+ }
+
+ var range = docFormatParams.Range;
+ ScriptFile scriptFile = GetFile(docFormatParams);
+ if (scriptFile == null)
+ {
+ return new TextEdit[0];
+ }
+ TextEdit textEdit = new TextEdit { Range = range };
+ string text = scriptFile.GetTextInRange(range.ToBufferRange());
+ return DoFormat(docFormatParams, textEdit, text);
+ });
+ }
+
+ private bool ShouldSkipFormatting(DocumentFormattingParams docFormatParams)
+ {
+ if (docFormatParams == null
+ || docFormatParams.TextDocument == null
+ || docFormatParams.TextDocument.Uri == null)
+ {
+ return true;
+ }
+ return (LanguageService != null && LanguageService.ShouldSkipNonMssqlFile(docFormatParams.TextDocument.Uri));
+ }
+
+ private async Task FormatAndReturnEdits(DocumentFormattingParams docFormatParams)
+ {
+ return await Task.Factory.StartNew(() =>
+ {
+ if (ShouldSkipFormatting(docFormatParams))
+ {
+ return Array.Empty();
+ }
+
+ var scriptFile = GetFile(docFormatParams);
+ if (scriptFile == null
+ || scriptFile.FileLines.Count == 0)
+ {
+ return new TextEdit[0];
+ }
+ TextEdit textEdit = PrepareEdit(scriptFile);
+ string text = scriptFile.Contents;
+ return DoFormat(docFormatParams, textEdit, text);
+ });
+ }
+
+ private TextEdit[] DoFormat(DocumentFormattingParams docFormatParams, TextEdit edit, string text)
+ {
+ Validate.IsNotNull(nameof(docFormatParams), docFormatParams);
+
+ FormatOptions options = GetOptions(docFormatParams);
+ List edits = new List();
+ edit.NewText = Format(text, options, false);
+ // TODO do not add if no formatting needed?
+ edits.Add(edit);
+ return edits.ToArray();
+ }
+
+ private FormatOptions GetOptions(DocumentFormattingParams docFormatParams)
+ {
+ return MergeFormatOptions(docFormatParams.Options, settings);
+ }
+
+ internal static FormatOptions MergeFormatOptions(FormattingOptions formatRequestOptions, FormatterSettings settings)
+
+ {
+ FormatOptions options = new FormatOptions();
+ if (formatRequestOptions != null)
+ {
+ options.UseSpaces = formatRequestOptions.InsertSpaces;
+ options.SpacesPerIndent = formatRequestOptions.TabSize;
+ }
+ UpdateFormatOptionsFromSettings(options, settings);
+ return options;
+ }
+
+ internal static void UpdateFormatOptionsFromSettings(FormatOptions options, FormatterSettings settings)
+ {
+ Validate.IsNotNull(nameof(options), options);
+ if (settings != null)
+ {
+ if (settings.AlignColumnDefinitionsInColumns.HasValue) { options.AlignColumnDefinitionsInColumns = settings.AlignColumnDefinitionsInColumns.Value; }
+
+ if (settings.PlaceCommasBeforeNextStatement.HasValue) { options.PlaceCommasBeforeNextStatement = settings.PlaceCommasBeforeNextStatement.Value; }
+
+ if (settings.PlaceSelectStatementReferencesOnNewLine.HasValue) { options.PlaceEachReferenceOnNewLineInQueryStatements = settings.PlaceSelectStatementReferencesOnNewLine.Value; }
+
+ if (settings.UseBracketForIdentifiers.HasValue) { options.EncloseIdentifiersInSquareBrackets = settings.UseBracketForIdentifiers.Value; }
+
+ options.DatatypeCasing = settings.DatatypeCasing;
+ options.KeywordCasing = settings.KeywordCasing;
+ }
+ }
+
+ private ScriptFile GetFile(DocumentFormattingParams docFormatParams)
+ {
+ return WorkspaceService.Workspace.GetFile(docFormatParams.TextDocument.Uri);
+ }
+
+ private static TextEdit PrepareEdit(ScriptFile scriptFile)
+ {
+ int fileLines = scriptFile.FileLines.Count;
+ Position start = new Position { Line = 0, Character = 0 };
+ int lastChar = scriptFile.FileLines[scriptFile.FileLines.Count - 1].Length;
+ Position end = new Position { Line = scriptFile.FileLines.Count - 1, Character = lastChar };
+
+ TextEdit edit = new TextEdit
+ {
+ Range = new Range { Start = start, End = end }
+ };
+ return edit;
+ }
+
+ private async Task HandleRequest(Func> handler, RequestContext requestContext, string requestType)
+ {
+ Logger.Write(TraceEventType.Verbose, requestType);
+
+ try
+ {
+ T result = await handler();
+ await requestContext.SendResult(result);
+ }
+ catch (Exception ex)
+ {
+ await requestContext.SendError(ex.ToString());
+ }
+ }
+
+
+
+ public string Format(TextReader input)
+ {
+ string originalSql = input.ReadToEnd();
+ return Format(originalSql, new FormatOptions());
+ }
+
+ public string Format(string input, FormatOptions options)
+ {
+ return Format(input, options, true);
+ }
+
+ public string Format(string input, FormatOptions options, bool verifyOutput)
+ {
+ string result = null;
+ //TODOKusto: Implement formatting for Kusto generically here.
+ //var kustoCodeService = new KustoCodeService(input, GlobalState.Default);
+ //var formattedText = kustoCodeService.GetFormattedText();
+ //DoFormat(input, options, verifyOutput, visitor =>
+ //{
+ //result = formattedText.Text;
+ //});
+
+ return result;
+ }
+
+ /*public void Format(string input, FormatOptions options, bool verifyOutput, Replacement.OnReplace replace)
+ {
+ DoFormat(input, options, verifyOutput, visitor =>
+ {
+ foreach (Replacement r in visitor.Context.Replacements)
+ {
+ r.Apply(replace);
+ }
+ });
+ }
+
+ private void DoFormat(string input, FormatOptions options, bool verifyOutput, Action postFormatAction)
+ {
+ Validate.IsNotNull(nameof(input), input);
+ Validate.IsNotNull(nameof(options), options);
+
+ ParseResult result = Parser.Parse(input);
+ FormatContext context = new FormatContext(result.Script, options);
+
+ FormatterVisitor visitor = new FormatterVisitor(context, ServiceProvider);
+ result.Script.Accept(visitor);
+ if (verifyOutput)
+ {
+ visitor.VerifyFormat();
+ }
+
+ postFormatAction?.Invoke(visitor);
+ }*/
+ }
+
+ internal static class RangeExtensions
+ {
+ public static BufferRange ToBufferRange(this Range range)
+ {
+ // It turns out that VSCode sends Range objects as 0-indexed lines, while
+ // our BufferPosition and BufferRange logic assumes 1-indexed. Therefore
+ // need to increment all ranges by 1 when copying internally and reduce
+ // when returning to the caller
+ return new BufferRange(
+ new BufferPosition(range.Start.Line + 1, range.Start.Character + 1),
+ new BufferPosition(range.End.Line + 1, range.End.Character + 1)
+ );
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/HostLoader.cs b/src/Microsoft.Kusto.ServiceLayer/HostLoader.cs
new file mode 100644
index 00000000..c0c0e187
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/HostLoader.cs
@@ -0,0 +1,136 @@
+//
+// 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.Threading.Tasks;
+using Microsoft.SqlTools.Credentials;
+using Microsoft.SqlTools.Extensibility;
+using Microsoft.SqlTools.Hosting;
+using Microsoft.SqlTools.Hosting.Protocol;
+using Microsoft.Kusto.ServiceLayer.Admin;
+using Microsoft.Kusto.ServiceLayer.Metadata;
+using Microsoft.Kusto.ServiceLayer.Connection;
+using Microsoft.Kusto.ServiceLayer.Hosting;
+using Microsoft.Kusto.ServiceLayer.LanguageServices;
+using Microsoft.Kusto.ServiceLayer.QueryExecution;
+using Microsoft.Kusto.ServiceLayer.Scripting;
+using Microsoft.Kusto.ServiceLayer.SqlContext;
+using Microsoft.Kusto.ServiceLayer.Workspace;
+using SqlToolsContext = Microsoft.SqlTools.ServiceLayer.SqlContext.SqlToolsContext;
+
+namespace Microsoft.Kusto.ServiceLayer
+{
+ ///
+ /// Provides support for starting up a service host. This is a common responsibility
+ /// for both the main service program and test driver that interacts with it
+ ///
+ public static class HostLoader
+ {
+ private static object lockObject = new object();
+ private static bool isLoaded;
+
+ private static readonly string[] inclusionList =
+ {
+ "microsofsqltoolscredentials.dll",
+ "microsoft.sqltools.hosting.dll",
+ "microsoftkustoservicelayer.dll"
+ };
+
+ internal static ServiceHost CreateAndStartServiceHost(SqlToolsContext sqlToolsContext)
+ {
+ ServiceHost serviceHost = ServiceHost.Instance;
+ lock (lockObject)
+ {
+ if (!isLoaded)
+ {
+ // Grab the instance of the service host
+ serviceHost.Initialize();
+
+ InitializeRequestHandlersAndServices(serviceHost, sqlToolsContext);
+
+ // Start the service only after all request handlers are setup. This is vital
+ // as otherwise the Initialize event can be lost - it's processed and discarded before the handler
+ // is hooked up to receive the message
+ serviceHost.Start().Wait();
+ isLoaded = true;
+ }
+ }
+ return serviceHost;
+ }
+
+ private static void InitializeRequestHandlersAndServices(ServiceHost serviceHost, SqlToolsContext sqlToolsContext)
+ {
+ // Load extension provider, which currently finds all exports in current DLL. Can be changed to find based
+ // on directory or assembly list quite easily in the future
+ ExtensionServiceProvider serviceProvider = ExtensionServiceProvider.CreateDefaultServiceProvider(inclusionList);
+ serviceProvider.RegisterSingleService(sqlToolsContext);
+ serviceProvider.RegisterSingleService(serviceHost);
+
+ // Initialize and register singleton services so they're accessible for any MEF service. In the future, these
+ // could be updated to be IComposableServices, which would avoid the requirement to define a singleton instance
+ // and instead have MEF handle discovery & loading
+ WorkspaceService.Instance.InitializeService(serviceHost);
+ serviceProvider.RegisterSingleService(WorkspaceService.Instance);
+
+ LanguageService.Instance.InitializeService(serviceHost, sqlToolsContext);
+ serviceProvider.RegisterSingleService(LanguageService.Instance);
+
+ ConnectionService.Instance.InitializeService(serviceHost);
+ serviceProvider.RegisterSingleService(ConnectionService.Instance);
+
+ CredentialService.Instance.InitializeService(serviceHost);
+ serviceProvider.RegisterSingleService(CredentialService.Instance);
+
+ QueryExecutionService.Instance.InitializeService(serviceHost);
+ serviceProvider.RegisterSingleService(QueryExecutionService.Instance);
+
+ ScriptingService.Instance.InitializeService(serviceHost);
+ serviceProvider.RegisterSingleService(ScriptingService.Instance);
+
+ AdminService.Instance.InitializeService(serviceHost);
+ serviceProvider.RegisterSingleService(AdminService.Instance);
+
+ MetadataService.Instance.InitializeService(serviceHost);
+ serviceProvider.RegisterSingleService(MetadataService.Instance);
+
+ InitializeHostedServices(serviceProvider, serviceHost);
+ serviceHost.ServiceProvider = serviceProvider;
+
+ serviceHost.InitializeRequestHandlers();
+ }
+
+ ///
+ /// Internal to support testing. Initializes instances in the service,
+ /// and registers them for their preferred service type
+ ///
+ internal static void InitializeHostedServices(RegisteredServiceProvider provider, IProtocolEndpoint host)
+ {
+ // Pre-register all services before initializing. This ensures that if one service wishes to reference
+ // another one during initialization, it will be able to safely do so
+ foreach (IHostedService service in provider.GetServices())
+ {
+ provider.RegisterSingleService(service.ServiceType, service);
+ }
+
+ ServiceHost serviceHost = host as ServiceHost;
+ foreach (IHostedService service in provider.GetServices())
+ {
+ // Initialize all hosted services, and register them in the service provider for their requested
+ // service type. This ensures that when searching for the ConnectionService you can get it without
+ // searching for an IHostedService of type ConnectionService
+ service.InitializeService(host);
+
+ IDisposable disposable = service as IDisposable;
+ if (serviceHost != null && disposable != null)
+ {
+ serviceHost.RegisterShutdownTask(async (shutdownParams, shutdownRequestContext) =>
+ {
+ disposable.Dispose();
+ await Task.FromResult(0);
+ });
+ }
+ }
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/LanguageServices/AutoCompleteHelper.cs b/src/Microsoft.Kusto.ServiceLayer/LanguageServices/AutoCompleteHelper.cs
new file mode 100644
index 00000000..509e3722
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/LanguageServices/AutoCompleteHelper.cs
@@ -0,0 +1,112 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+using Range = Microsoft.Kusto.ServiceLayer.Workspace.Contracts.Range;
+using Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts;
+using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.LanguageServices
+{
+ ///
+ /// All the conversion of intellisense info to vscode format is done in this class.
+ ///
+ public static class AutoCompleteHelper
+ {
+ ///
+ /// Create a completion item from the default item text since VS Code expects CompletionItems
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static CompletionItem CreateCompletionItem(
+ string label,
+ string detail,
+ string insertText,
+ CompletionItemKind kind,
+ int row,
+ int startColumn,
+ int endColumn)
+ {
+ CompletionItem item = new CompletionItem
+ {
+ Label = label,
+ Kind = kind,
+ Detail = detail,
+ InsertText = insertText,
+ TextEdit = new TextEdit
+ {
+ NewText = insertText,
+ Range = new Range
+ {
+ Start = new Position
+ {
+ Line = row,
+ Character = startColumn
+ },
+ End = new Position
+ {
+ Line = row,
+ Character = endColumn
+ }
+ }
+ }
+ };
+
+ return item;
+ }
+
+ ///
+ /// Converts QuickInfo object into a VS Code Hover object
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static Hover ConvertQuickInfoToHover(
+ string quickInfoText,
+ string language,
+ int row,
+ int startColumn,
+ int endColumn)
+ {
+ // convert from the parser format to the VS Code wire format
+ var markedStrings = new MarkedString[1];
+ if (quickInfoText != null)
+ {
+ markedStrings[0] = new MarkedString()
+ {
+ Language = language,
+ Value = quickInfoText
+ };
+
+ return new Hover()
+ {
+ Contents = markedStrings,
+ Range = new Range
+ {
+ Start = new Position
+ {
+ Line = row,
+ Character = startColumn
+ },
+ End = new Position
+ {
+ Line = row,
+ Character = endColumn
+ }
+ }
+ };
+ }
+ else
+ {
+ return null;
+ }
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/LanguageServices/BindingQueue.cs b/src/Microsoft.Kusto.ServiceLayer/LanguageServices/BindingQueue.cs
new file mode 100644
index 00000000..cf5635ed
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/LanguageServices/BindingQueue.cs
@@ -0,0 +1,502 @@
+//
+// 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;
+using System.Data.SqlClient;
+using System.Diagnostics;
+using System.Linq;
+using System.Net.Sockets;
+using System.Threading;
+using System.Threading.Tasks;
+using Microsoft.Kusto.ServiceLayer.Utility;
+using Microsoft.SqlTools.Utility;
+
+
+namespace Microsoft.Kusto.ServiceLayer.LanguageServices
+{
+ ///
+ /// Main class for the Binding Queue
+ ///
+ public class BindingQueue : IDisposable where T : IBindingContext, new()
+ {
+ internal const int QueueThreadStackSize = 5 * 1024 * 1024;
+
+ private CancellationTokenSource processQueueCancelToken = null;
+
+ private ManualResetEvent itemQueuedEvent = new ManualResetEvent(initialState: false);
+
+ private object bindingQueueLock = new object();
+
+ private LinkedList bindingQueue = new LinkedList();
+
+ private object bindingContextLock = new object();
+
+ private Task queueProcessorTask;
+
+ public delegate void UnhandledExceptionDelegate(string connectionKey, Exception ex);
+
+ public event UnhandledExceptionDelegate OnUnhandledException;
+
+ ///
+ /// Map from context keys to binding context instances
+ /// Internal for testing purposes only
+ ///
+ internal Dictionary BindingContextMap { get; set; }
+
+ internal Dictionary BindingContextTasks { get; set; } = new Dictionary();
+
+ ///
+ /// Constructor for a binding queue instance
+ ///
+ public BindingQueue()
+ {
+ this.BindingContextMap = new Dictionary();
+ this.StartQueueProcessor();
+ }
+
+ public void StartQueueProcessor()
+ {
+ this.queueProcessorTask = StartQueueProcessorAsync();
+ }
+
+ ///
+ /// Stops the binding queue by sending cancellation request
+ ///
+ ///
+ public bool StopQueueProcessor(int timeout)
+ {
+ this.processQueueCancelToken.Cancel();
+ return this.queueProcessorTask.Wait(timeout);
+ }
+
+ ///
+ /// Returns true if cancellation is requested
+ ///
+ ///
+ public bool IsCancelRequested
+ {
+ get
+ {
+ return this.processQueueCancelToken.IsCancellationRequested;
+ }
+ }
+
+ ///
+ /// Queue a binding request item
+ ///
+ public virtual QueueItem QueueBindingOperation(
+ string key,
+ Func bindOperation,
+ Func timeoutOperation = null,
+ Func errorHandler = null,
+ int? bindingTimeout = null,
+ int? waitForLockTimeout = null)
+ {
+ // don't add null operations to the binding queue
+ if (bindOperation == null)
+ {
+ return null;
+ }
+
+ QueueItem queueItem = new QueueItem()
+ {
+ Key = key,
+ BindOperation = bindOperation,
+ TimeoutOperation = timeoutOperation,
+ ErrorHandler = errorHandler,
+ BindingTimeout = bindingTimeout,
+ WaitForLockTimeout = waitForLockTimeout
+ };
+
+ lock (this.bindingQueueLock)
+ {
+ this.bindingQueue.AddLast(queueItem);
+ }
+
+ this.itemQueuedEvent.Set();
+
+ return queueItem;
+ }
+
+ ///
+ /// Checks if a particular binding context is connected or not
+ ///
+ ///
+ public bool IsBindingContextConnected(string key)
+ {
+ lock (this.bindingContextLock)
+ {
+ IBindingContext context;
+ if (this.BindingContextMap.TryGetValue(key, out context))
+ {
+ return context.IsConnected;
+ }
+ return false;
+ }
+ }
+
+ ///
+ /// Gets or creates a binding context for the provided context key
+ ///
+ ///
+ protected IBindingContext GetOrCreateBindingContext(string key)
+ {
+ // use a default binding context for disconnected requests
+ if (string.IsNullOrWhiteSpace(key))
+ {
+ key = "disconnected_binding_context";
+ }
+
+ lock (this.bindingContextLock)
+ {
+ if (!this.BindingContextMap.ContainsKey(key))
+ {
+ var bindingContext = new T();
+ this.BindingContextMap.Add(key, bindingContext);
+ this.BindingContextTasks.Add(bindingContext, Task.Run(() => null));
+ }
+
+ return this.BindingContextMap[key];
+ }
+ }
+
+ protected IEnumerable GetBindingContexts(string keyPrefix)
+ {
+ // use a default binding context for disconnected requests
+ if (string.IsNullOrWhiteSpace(keyPrefix))
+ {
+ keyPrefix = "disconnected_binding_context";
+ }
+
+ lock (this.bindingContextLock)
+ {
+ return this.BindingContextMap.Where(x => x.Key.StartsWith(keyPrefix)).Select(v => v.Value);
+ }
+ }
+
+ ///
+ /// Checks if a binding context already exists for the provided context key
+ ///
+ protected bool BindingContextExists(string key)
+ {
+ lock (this.bindingContextLock)
+ {
+ return this.BindingContextMap.ContainsKey(key);
+ }
+ }
+
+ ///
+ /// Remove the binding queue entry
+ ///
+ protected void RemoveBindingContext(string key)
+ {
+ lock (this.bindingContextLock)
+ {
+ if (this.BindingContextMap.ContainsKey(key))
+ {
+ // disconnect existing connection
+ var bindingContext = this.BindingContextMap[key];
+ if (bindingContext.ServerConnection != null && bindingContext.ServerConnection.IsOpen)
+ {
+ // Disconnecting can take some time so run it in a separate task so that it doesn't block removal
+ Task.Run(() =>
+ {
+ bindingContext.ServerConnection.Cancel();
+ bindingContext.ServerConnection.Disconnect();
+ });
+ }
+
+ // remove key from the map
+ this.BindingContextMap.Remove(key);
+ this.BindingContextTasks.Remove(bindingContext);
+ }
+ }
+ }
+
+ public bool HasPendingQueueItems
+ {
+ get
+ {
+ lock (this.bindingQueueLock)
+ {
+ return this.bindingQueue.Count > 0;
+ }
+ }
+ }
+
+ ///
+ /// Gets the next pending queue item
+ ///
+ private QueueItem GetNextQueueItem()
+ {
+ lock (this.bindingQueueLock)
+ {
+ if (this.bindingQueue.Count == 0)
+ {
+ return null;
+ }
+
+ QueueItem queueItem = this.bindingQueue.First.Value;
+ this.bindingQueue.RemoveFirst();
+ return queueItem;
+ }
+ }
+
+ ///
+ /// Starts the queue processing thread
+ ///
+ private Task StartQueueProcessorAsync()
+ {
+ if (this.processQueueCancelToken != null)
+ {
+ this.processQueueCancelToken.Dispose();
+ }
+ this.processQueueCancelToken = new CancellationTokenSource();
+
+ return Task.Factory.StartNew(
+ ProcessQueue,
+ this.processQueueCancelToken.Token,
+ TaskCreationOptions.LongRunning,
+ TaskScheduler.Default);
+ }
+
+ ///
+ /// The core queue processing method
+ ///
+ ///
+ private void ProcessQueue()
+ {
+ CancellationToken token = this.processQueueCancelToken.Token;
+ WaitHandle[] waitHandles = new WaitHandle[2]
+ {
+ this.itemQueuedEvent,
+ token.WaitHandle
+ };
+
+ while (true)
+ {
+ // wait for with an item to be queued or the a cancellation request
+ WaitHandle.WaitAny(waitHandles);
+ if (token.IsCancellationRequested)
+ {
+ break;
+ }
+
+ try
+ {
+ // dispatch all pending queue items
+ while (this.HasPendingQueueItems)
+ {
+ QueueItem queueItem = GetNextQueueItem();
+ if (queueItem == null)
+ {
+ continue;
+ }
+
+ IBindingContext bindingContext = GetOrCreateBindingContext(queueItem.Key);
+ if (bindingContext == null)
+ {
+ queueItem.ItemProcessed.Set();
+ continue;
+ }
+
+ var bindingContextTask = this.BindingContextTasks[bindingContext];
+
+ // Run in the binding context task in case this task has to wait for a previous binding operation
+ this.BindingContextTasks[bindingContext] = bindingContextTask.ContinueWith((task) =>
+ {
+ bool lockTaken = false;
+ try
+ {
+ // prefer the queue item binding item, otherwise use the context default timeout
+ int bindTimeout = queueItem.BindingTimeout ?? bindingContext.BindingTimeout;
+
+ // handle the case a previous binding operation is still running
+ if (!bindingContext.BindingLock.WaitOne(queueItem.WaitForLockTimeout ?? 0))
+ {
+ try
+ {
+ Logger.Write(TraceEventType.Warning, "Binding queue operation timed out waiting for previous operation to finish");
+ queueItem.Result = queueItem.TimeoutOperation != null
+ ? queueItem.TimeoutOperation(bindingContext)
+ : null;
+ }
+ catch (Exception ex)
+ {
+ Logger.Write(TraceEventType.Error, "Exception running binding queue lock timeout handler: " + ex.ToString());
+ }
+ finally
+ {
+ queueItem.ItemProcessed.Set();
+ }
+
+ return;
+ }
+
+ bindingContext.BindingLock.Reset();
+
+ lockTaken = true;
+
+ // execute the binding operation
+ object result = null;
+ CancellationTokenSource cancelToken = new CancellationTokenSource();
+
+ // run the operation in a separate thread
+ var bindTask = Task.Run(() =>
+ {
+ try
+ {
+ result = queueItem.BindOperation(
+ bindingContext,
+ cancelToken.Token);
+ }
+ catch (Exception ex)
+ {
+ Logger.Write(TraceEventType.Error, "Unexpected exception on the binding queue: " + ex.ToString());
+ if (queueItem.ErrorHandler != null)
+ {
+ try
+ {
+ result = queueItem.ErrorHandler(ex);
+ }
+ catch (Exception ex2)
+ {
+ Logger.Write(TraceEventType.Error, "Unexpected exception in binding queue error handler: " + ex2.ToString());
+ }
+ }
+
+ if (IsExceptionOfType(ex, typeof(SqlException)) || IsExceptionOfType(ex, typeof(SocketException)))
+ {
+ if (this.OnUnhandledException != null)
+ {
+ this.OnUnhandledException(queueItem.Key, ex);
+ }
+
+ RemoveBindingContext(queueItem.Key);
+ }
+ }
+ });
+
+ Task.Run(() =>
+ {
+ try
+ {
+ // check if the binding tasks completed within the binding timeout
+ if (bindTask.Wait(bindTimeout))
+ {
+ queueItem.Result = result;
+ }
+ else
+ {
+ cancelToken.Cancel();
+
+ // if the task didn't complete then call the timeout callback
+ if (queueItem.TimeoutOperation != null)
+ {
+ queueItem.Result = queueItem.TimeoutOperation(bindingContext);
+ }
+
+ bindTask.ContinueWithOnFaulted(t => Logger.Write(TraceEventType.Error, "Binding queue threw exception " + t.Exception.ToString()));
+
+ // Give the task a chance to complete before moving on to the next operation
+ bindTask.Wait();
+ }
+ }
+ catch (Exception ex)
+ {
+ Logger.Write(TraceEventType.Error, "Binding queue task completion threw exception " + ex.ToString());
+ }
+ finally
+ {
+ // set item processed to avoid deadlocks
+ if (lockTaken)
+ {
+ bindingContext.BindingLock.Set();
+ }
+ queueItem.ItemProcessed.Set();
+ }
+ });
+ }
+ catch (Exception ex)
+ {
+ // catch and log any exceptions raised in the binding calls
+ // set item processed to avoid deadlocks
+ Logger.Write(TraceEventType.Error, "Binding queue threw exception " + ex.ToString());
+ // set item processed to avoid deadlocks
+ if (lockTaken)
+ {
+ bindingContext.BindingLock.Set();
+ }
+ queueItem.ItemProcessed.Set();
+ }
+ }, TaskContinuationOptions.None);
+
+ // if a queue processing cancellation was requested then exit the loop
+ if (token.IsCancellationRequested)
+ {
+ break;
+ }
+ }
+ }
+ finally
+ {
+ lock (this.bindingQueueLock)
+ {
+ // verify the binding queue is still empty
+ if (this.bindingQueue.Count == 0)
+ {
+ // reset the item queued event since we've processed all the pending items
+ this.itemQueuedEvent.Reset();
+ }
+ }
+ }
+ }
+ }
+
+ ///
+ /// Clear queued items
+ ///
+ public void ClearQueuedItems()
+ {
+ lock (this.bindingQueueLock)
+ {
+ if (this.bindingQueue.Count > 0)
+ {
+ this.bindingQueue.Clear();
+ }
+ }
+ }
+
+ public void Dispose()
+ {
+ if (this.processQueueCancelToken != null)
+ {
+ this.processQueueCancelToken.Dispose();
+ }
+
+ if (itemQueuedEvent != null)
+ {
+ itemQueuedEvent.Dispose();
+ }
+
+ if (this.BindingContextMap != null)
+ {
+ foreach (var item in this.BindingContextMap)
+ {
+ if (item.Value != null && item.Value.ServerConnection != null && item.Value.ServerConnection.SqlConnectionObject != null)
+ {
+ item.Value.ServerConnection.SqlConnectionObject.Close();
+ }
+ }
+ }
+ }
+
+ private bool IsExceptionOfType(Exception ex, Type t)
+ {
+ return ex.GetType() == t || (ex.InnerException != null && ex.InnerException.GetType() == t);
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/LanguageServices/ConnectedBindingContext.cs b/src/Microsoft.Kusto.ServiceLayer/LanguageServices/ConnectedBindingContext.cs
new file mode 100644
index 00000000..bde3a381
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/LanguageServices/ConnectedBindingContext.cs
@@ -0,0 +1,223 @@
+//
+// 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.Threading;
+using Microsoft.SqlServer.Management.Common;
+using Microsoft.SqlServer.Management.SmoMetadataProvider;
+using Microsoft.SqlServer.Management.SqlParser.Binder;
+using Microsoft.SqlServer.Management.SqlParser.Common;
+using Microsoft.SqlServer.Management.SqlParser.MetadataProvider;
+using Microsoft.SqlServer.Management.SqlParser.Parser;
+using Kusto.Data.Net.Client;
+using Kusto.Data.Common;
+using Kusto.Data;
+using Microsoft.Kusto.ServiceLayer.DataSource;
+
+namespace Microsoft.Kusto.ServiceLayer.LanguageServices
+{
+ ///
+ /// Class for the binding context for connected sessions
+ ///
+ public class ConnectedBindingContext : IBindingContext
+ {
+ private ParseOptions parseOptions;
+
+ private ManualResetEvent bindingLock;
+
+ private ServerConnection serverConnection;
+
+ ///
+ public IDataSource DataSource { get; set; }
+
+ ///
+ /// Connected binding context constructor
+ ///
+ public ConnectedBindingContext()
+ {
+ this.bindingLock = new ManualResetEvent(initialState: true);
+ this.BindingTimeout = ConnectedBindingQueue.DefaultBindingTimeout;
+ this.MetadataDisplayInfoProvider = new MetadataDisplayInfoProvider();
+ }
+
+ ///
+ /// Gets or sets a flag indicating if the binder is connected
+ ///
+ public bool IsConnected { get; set; }
+
+ ///
+ /// Gets or sets the binding server connection
+ ///
+ public ServerConnection ServerConnection
+ {
+ get
+ {
+ return this.serverConnection;
+ }
+ set
+ {
+ this.serverConnection = value;
+
+ // reset the parse options so the get recreated for the current connection
+ this.parseOptions = null;
+ }
+ }
+
+ ///
+ /// Gets or sets the metadata display info provider
+ ///
+ public MetadataDisplayInfoProvider MetadataDisplayInfoProvider { get; set; }
+
+ ///
+ /// Gets or sets the SMO metadata provider
+ ///
+ public SmoMetadataProvider SmoMetadataProvider { get; set; }
+
+ ///
+ /// Gets or sets the binder
+ ///
+ public IBinder Binder { get; set; }
+
+ ///
+ /// Gets the binding lock object
+ ///
+ public ManualResetEvent BindingLock
+ {
+ get
+ {
+ return this.bindingLock;
+ }
+ }
+
+ ///
+ /// Gets or sets the binding operation timeout in milliseconds
+ ///
+ public int BindingTimeout { get; set; }
+
+ ///
+ /// Gets the Language Service ServerVersion
+ ///
+ public ServerVersion ServerVersion
+ {
+ get
+ {
+ return this.ServerConnection != null
+ ? this.ServerConnection.ServerVersion
+ : null;
+ }
+ }
+
+ ///
+ /// Gets the current DataEngineType
+ ///
+ public DatabaseEngineType DatabaseEngineType
+ {
+ get
+ {
+ return this.ServerConnection != null
+ ? this.ServerConnection.DatabaseEngineType
+ : DatabaseEngineType.Standalone;
+ }
+ }
+
+ ///
+ /// Gets the current connections TransactSqlVersion
+ ///
+ public TransactSqlVersion TransactSqlVersion
+ {
+ get
+ {
+ return this.IsConnected
+ ? GetTransactSqlVersion(this.ServerVersion)
+ : TransactSqlVersion.Current;
+ }
+ }
+
+ ///
+ /// Gets the current DatabaseCompatibilityLevel
+ ///
+ public DatabaseCompatibilityLevel DatabaseCompatibilityLevel
+ {
+ get
+ {
+ return this.IsConnected
+ ? GetDatabaseCompatibilityLevel(this.ServerVersion)
+ : DatabaseCompatibilityLevel.Current;
+ }
+ }
+
+ ///
+ /// Gets the current ParseOptions
+ ///
+ public ParseOptions ParseOptions
+ {
+ get
+ {
+ if (this.parseOptions == null)
+ {
+ this.parseOptions = new ParseOptions(
+ batchSeparator: LanguageService.DefaultBatchSeperator,
+ isQuotedIdentifierSet: true,
+ compatibilityLevel: DatabaseCompatibilityLevel,
+ transactSqlVersion: TransactSqlVersion);
+ }
+ return this.parseOptions;
+ }
+ }
+
+
+ ///
+ /// Gets the database compatibility level from a server version
+ ///
+ ///
+ private static DatabaseCompatibilityLevel GetDatabaseCompatibilityLevel(ServerVersion serverVersion)
+ {
+ int versionMajor = Math.Max(serverVersion.Major, 8);
+
+ switch (versionMajor)
+ {
+ case 8:
+ return DatabaseCompatibilityLevel.Version80;
+ case 9:
+ return DatabaseCompatibilityLevel.Version90;
+ case 10:
+ return DatabaseCompatibilityLevel.Version100;
+ case 11:
+ return DatabaseCompatibilityLevel.Version110;
+ case 12:
+ return DatabaseCompatibilityLevel.Version120;
+ case 13:
+ return DatabaseCompatibilityLevel.Version130;
+ default:
+ return DatabaseCompatibilityLevel.Current;
+ }
+ }
+
+ ///
+ /// Gets the transaction sql version from a server version
+ ///
+ ///
+ private static TransactSqlVersion GetTransactSqlVersion(ServerVersion serverVersion)
+ {
+ int versionMajor = Math.Max(serverVersion.Major, 9);
+
+ switch (versionMajor)
+ {
+ case 9:
+ case 10:
+ // In case of 10.0 we still use Version 10.5 as it is the closest available.
+ return TransactSqlVersion.Version105;
+ case 11:
+ return TransactSqlVersion.Version110;
+ case 12:
+ return TransactSqlVersion.Version120;
+ case 13:
+ return TransactSqlVersion.Version130;
+ default:
+ return TransactSqlVersion.Current;
+ }
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/LanguageServices/ConnectedBindingQueue.cs b/src/Microsoft.Kusto.ServiceLayer/LanguageServices/ConnectedBindingQueue.cs
new file mode 100644
index 00000000..49391ea8
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/LanguageServices/ConnectedBindingQueue.cs
@@ -0,0 +1,234 @@
+//
+// 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.SqlClient;
+using Microsoft.SqlServer.Management.Common;
+using Microsoft.SqlServer.Management.SmoMetadataProvider;
+using Microsoft.SqlServer.Management.SqlParser.Binder;
+using Microsoft.SqlServer.Management.SqlParser.MetadataProvider;
+using Microsoft.Kusto.ServiceLayer.Connection;
+using Microsoft.Kusto.ServiceLayer.Connection.Contracts;
+using Microsoft.Kusto.ServiceLayer.SqlContext;
+using Microsoft.Kusto.ServiceLayer.Workspace;
+using Microsoft.Kusto.ServiceLayer.DataSource;
+using System.Threading;
+
+namespace Microsoft.Kusto.ServiceLayer.LanguageServices
+{
+ public interface IConnectedBindingQueue
+ {
+ void CloseConnections(string serverName, string databaseName, int millisecondsTimeout);
+ void OpenConnections(string serverName, string databaseName, int millisecondsTimeout);
+ string AddConnectionContext(ConnectionInfo connInfo, string featureName = null, bool overwrite = false);
+ void Dispose();
+ QueueItem QueueBindingOperation(
+ string key,
+ Func bindOperation,
+ Func timeoutOperation = null,
+ Func errorHandler = null,
+ int? bindingTimeout = null,
+ int? waitForLockTimeout = null);
+ }
+
+ public class SqlConnectionOpener
+ {
+ ///
+ /// Virtual method used to support mocking and testing
+ ///
+ public virtual ServerConnection OpenServerConnection(ConnectionInfo connInfo, string featureName)
+ {
+ return ConnectionService.OpenServerConnection(connInfo, featureName);
+ }
+ }
+
+ ///
+ /// ConnectedBindingQueue class for processing online binding requests
+ ///
+ public class ConnectedBindingQueue : BindingQueue, IConnectedBindingQueue
+ {
+ internal const int DefaultBindingTimeout = 500;
+
+ internal const int DefaultMinimumConnectionTimeout = 30;
+
+ ///
+ /// flag determing if the connection queue requires online metadata objects
+ /// it's much cheaper to not construct these objects if not needed
+ ///
+ private bool needsMetadata;
+ private SqlConnectionOpener connectionOpener;
+
+ ///
+ /// Gets the current settings
+ ///
+ internal SqlToolsSettings CurrentSettings
+ {
+ get { return WorkspaceService.Instance.CurrentSettings; }
+ }
+
+ public ConnectedBindingQueue()
+ : this(true)
+ {
+ }
+
+ public ConnectedBindingQueue(bool needsMetadata)
+ {
+ this.needsMetadata = needsMetadata;
+ this.connectionOpener = new SqlConnectionOpener();
+ }
+
+ // For testing purposes only
+ internal void SetConnectionOpener(SqlConnectionOpener opener)
+ {
+ this.connectionOpener = opener;
+ }
+
+ ///
+ /// Generate a unique key based on the ConnectionInfo object
+ ///
+ ///
+ internal static string GetConnectionContextKey(ConnectionDetails details)
+ {
+ string key = string.Format("{0}_{1}_{2}_{3}",
+ details.ServerName ?? "NULL",
+ details.DatabaseName ?? "NULL",
+ details.UserName ?? "NULL",
+ details.AuthenticationType ?? "NULL"
+ );
+
+ if (!string.IsNullOrEmpty(details.DatabaseDisplayName))
+ {
+ key += "_" + details.DatabaseDisplayName;
+ }
+
+ if (!string.IsNullOrEmpty(details.GroupId))
+ {
+ key += "_" + details.GroupId;
+ }
+
+ return Uri.EscapeUriString(key);
+ }
+
+ ///
+ /// Generate a unique key based on the ConnectionInfo object
+ ///
+ ///
+ private string GetConnectionContextKey(string serverName, string databaseName)
+ {
+ return string.Format("{0}_{1}",
+ serverName ?? "NULL",
+ databaseName ?? "NULL");
+
+ }
+
+ public void CloseConnections(string serverName, string databaseName, int millisecondsTimeout)
+ {
+ string connectionKey = GetConnectionContextKey(serverName, databaseName);
+ var contexts = GetBindingContexts(connectionKey);
+ foreach (var bindingContext in contexts)
+ {
+ if (bindingContext.BindingLock.WaitOne(millisecondsTimeout))
+ {
+ bindingContext.ServerConnection.Disconnect();
+ }
+ }
+ }
+
+ public void OpenConnections(string serverName, string databaseName, int millisecondsTimeout)
+ {
+ string connectionKey = GetConnectionContextKey(serverName, databaseName);
+ var contexts = GetBindingContexts(connectionKey);
+ foreach (var bindingContext in contexts)
+ {
+ if (bindingContext.BindingLock.WaitOne(millisecondsTimeout))
+ {
+ try
+ {
+ bindingContext.ServerConnection.Connect();
+ }
+ catch
+ {
+ //TODO: remove the binding context?
+ }
+ }
+ }
+ }
+
+ public void RemoveBindigContext(ConnectionInfo connInfo)
+ {
+ string connectionKey = GetConnectionContextKey(connInfo.ConnectionDetails);
+ if (BindingContextExists(connectionKey))
+ {
+ RemoveBindingContext(connectionKey);
+ }
+ }
+
+ ///
+ /// Use a ConnectionInfo item to create a connected binding context
+ ///
+ /// Connection info used to create binding context
+ /// Overwrite existing context
+ public virtual string AddConnectionContext(ConnectionInfo connInfo, string featureName = null, bool overwrite = false)
+ {
+ if (connInfo == null)
+ {
+ return string.Empty;
+ }
+
+ // lookup the current binding contextna
+ string connectionKey = GetConnectionContextKey(connInfo.ConnectionDetails);
+ if (BindingContextExists(connectionKey))
+ {
+ if (overwrite)
+ {
+ RemoveBindingContext(connectionKey);
+ }
+ else
+ {
+ // no need to populate the context again since the context already exists
+ return connectionKey;
+ }
+ }
+ IBindingContext bindingContext = this.GetOrCreateBindingContext(connectionKey);
+
+ if (bindingContext.BindingLock.WaitOne())
+ {
+ try
+ {
+ bindingContext.BindingLock.Reset();
+
+ // populate the binding context to work with the SMO metadata provider
+ bindingContext.ServerConnection = connectionOpener.OpenServerConnection(connInfo, featureName);
+
+ string connectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
+ bindingContext.DataSource = DataSourceFactory.Create(DataSourceType.Kusto, connectionString, connInfo.ConnectionDetails.AzureAccountToken);
+
+ if (this.needsMetadata)
+ {
+ bindingContext.SmoMetadataProvider = SmoMetadataProvider.CreateConnectedProvider(bindingContext.ServerConnection);
+ bindingContext.MetadataDisplayInfoProvider = new MetadataDisplayInfoProvider();
+ bindingContext.MetadataDisplayInfoProvider.BuiltInCasing =
+ this.CurrentSettings.SqlTools.IntelliSense.LowerCaseSuggestions.Value
+ ? CasingStyle.Lowercase : CasingStyle.Uppercase;
+ bindingContext.Binder = BinderProvider.CreateBinder(bindingContext.SmoMetadataProvider);
+ }
+
+ bindingContext.BindingTimeout = ConnectedBindingQueue.DefaultBindingTimeout;
+ bindingContext.IsConnected = true;
+ }
+ catch (Exception)
+ {
+ bindingContext.IsConnected = false;
+ }
+ finally
+ {
+ bindingContext.BindingLock.Set();
+ }
+ }
+
+ return connectionKey;
+ }
+ }
+}
diff --git a/src/Microsoft.Kusto.ServiceLayer/LanguageServices/Contracts/Completion.cs b/src/Microsoft.Kusto.ServiceLayer/LanguageServices/Contracts/Completion.cs
new file mode 100644
index 00000000..b9da23eb
--- /dev/null
+++ b/src/Microsoft.Kusto.ServiceLayer/LanguageServices/Contracts/Completion.cs
@@ -0,0 +1,111 @@
+//
+// Copyright (c) Microsoft. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+//
+
+using System.Diagnostics;
+using Microsoft.SqlTools.Hosting.Protocol.Contracts;
+using Microsoft.Kusto.ServiceLayer.Workspace.Contracts;
+
+namespace Microsoft.Kusto.ServiceLayer.LanguageServices.Contracts
+{
+ public class CompletionRequest
+ {
+ public static readonly
+ RequestType Type =
+ RequestType.Create("textDocument/completion");
+ }
+
+ public class CompletionResolveRequest
+ {
+ public static readonly
+ RequestType