stop getting os version by parsing string (#1070)

* stop getting os version by parsing string

* ignore error
This commit is contained in:
Alan Ren
2020-09-09 11:06:59 -07:00
committed by GitHub
parent f0ec7e14d4
commit b00e53c6b3
2 changed files with 48 additions and 35 deletions

View File

@@ -706,36 +706,49 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection
azureAccountToken: azureAccountToken); azureAccountToken: azureAccountToken);
return serverInfo; return serverInfo;
} }
/// <summary> /// <summary>
/// Gets the server host information from sys.dm_os_host_info view /// Gets the server host information from sys.dm_os_host_info view
/// </summary> /// </summary>
/// <param name="connection">The connection</param> /// <param name="connection">The connection</param>
public static ServerHostInfo GetServerHostInfo(IDbConnection 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) 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; return hostInfo;
} }
@@ -804,14 +817,14 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection
}); });
// Also get the OS Version // Also get the OS Version
ExecuteReader( var hostInfo = GetServerHostInfo(connection);
connection,
SqlConnectionHelperScripts.GetOsVersion, // Examples:
delegate (IDataReader reader) // SQL Server on Linux : Ubuntu 16.04
{ // SQL Server on Windows:
reader.Read(); // major version <= 13 (SQL Server 2016) - Windows 6.5
serverInfo.OsVersion = reader[0].ToString(); // 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<string, object>(); serverInfo.Options = new Dictionary<string, object>();
@@ -864,7 +877,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection
{ {
while (reader.Read()) while (reader.Read())
{ {
clusterEndpoints.Add(new ClusterEndpoint { clusterEndpoints.Add(new ClusterEndpoint
{
ServiceName = reader.GetString(0), ServiceName = reader.GetString(0),
Description = reader.GetString(1), Description = reader.GetString(1),
Endpoint = reader.GetString(2), Endpoint = reader.GetString(2),

View File

@@ -45,9 +45,8 @@ IF (@filepath IS NULL)
SELECT @filepath AS FilePath 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 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 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";
} }
} }