diff --git a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs index c684e86c..55a55c00 100644 --- a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs +++ b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/ReliableConnectionHelper.cs @@ -706,36 +706,49 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection azureAccountToken: azureAccountToken); return serverInfo; - } - - /// - /// Gets the server host information from sys.dm_os_host_info view - /// - /// The connection + } + + /// + /// Gets the server host information from sys.dm_os_host_info view + /// + /// The connection public static ServerHostInfo GetServerHostInfo(IDbConnection connection) { - // SQL Server 2016 and below does not provide sys.dm_os_host_info + var hostInfo = new ServerHostInfo(); + // SQL Server 2016 and earlier versions does not provide sys.dm_os_host_info and we know the host OS can only be Windows. if (!Version.TryParse(ReadServerVersion(connection), out var hostVersion) || hostVersion.Major <= 13) { - return new ServerHostInfo + try { - Platform = "Windows" - }; + hostInfo.Platform = "Windows"; + ExecuteReader( + connection, + SqlConnectionHelperScripts.GetHostWindowsVersion, + reader => + { + reader.Read(); + hostInfo.Release = reader[0].ToString(); + }); + } + catch + { + // Ignore the error and only set the Platform to Windows by default + } + } + else + { + ExecuteReader( + connection, + SqlConnectionHelperScripts.GetHostInfo, + reader => + { + reader.Read(); + hostInfo.Platform = reader[0].ToString(); + hostInfo.Distribution = reader[1].ToString(); + hostInfo.Release = reader[2].ToString(); + hostInfo.ServicePackLevel = reader[3].ToString(); + }); } - - var hostInfo = new ServerHostInfo(); - ExecuteReader( - connection, - SqlConnectionHelperScripts.GetHostInfo, - reader => - { - reader.Read(); - hostInfo.Platform = reader[0].ToString(); - hostInfo.Distribution = reader[1].ToString(); - hostInfo.Release = reader[2].ToString(); - hostInfo.ServicePackLevel = reader[3].ToString(); - }); - return hostInfo; } @@ -804,14 +817,14 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection }); // Also get the OS Version - ExecuteReader( - connection, - SqlConnectionHelperScripts.GetOsVersion, - delegate (IDataReader reader) - { - reader.Read(); - serverInfo.OsVersion = reader[0].ToString(); - }); + var hostInfo = GetServerHostInfo(connection); + + // Examples: + // SQL Server on Linux : Ubuntu 16.04 + // SQL Server on Windows: + // major version <= 13 (SQL Server 2016) - Windows 6.5 + // otherwise - Windows Server 2019 Standard 10.0 + serverInfo.OsVersion = hostInfo.Distribution != null ? string.Format("{0} {1}", hostInfo.Distribution, hostInfo.Release) : string.Format("{0} {1}", hostInfo.Platform, hostInfo.Release); serverInfo.Options = new Dictionary(); @@ -864,7 +877,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection { while (reader.Read()) { - clusterEndpoints.Add(new ClusterEndpoint { + clusterEndpoints.Add(new ClusterEndpoint + { ServiceName = reader.GetString(0), Description = reader.GetString(1), Endpoint = reader.GetString(2), diff --git a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/SqlConnectionHelperScripts.cs b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/SqlConnectionHelperScripts.cs index c19815aa..d93c9238 100644 --- a/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/SqlConnectionHelperScripts.cs +++ b/src/Microsoft.SqlTools.ManagedBatchParser/ReliableConnection/SqlConnectionHelperScripts.cs @@ -45,9 +45,8 @@ IF (@filepath IS NULL) SELECT @filepath AS FilePath "; - - public const string GetOsVersion = @"SELECT OSVersion = RIGHT(@@version, LEN(@@version)- 3 -charindex (' on ', LOWER(@@version)))"; public const string GetClusterEndpoints = @"SELECT [name], [description], [endpoint], [protocol_desc] FROM .[sys].[dm_cluster_endpoints];"; public const string GetHostInfo = @"SELECT [host_platform], [host_distribution], [host_release], [host_service_pack_level], [host_sku], [os_language_version] FROM sys.dm_os_host_info"; + public const string GetHostWindowsVersion = @"SELECT windows_release FROM sys.dm_os_windows_info"; } }