mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-28 17:24:27 -05:00
fixed the issues with restore options (#431)
* fixed the issues with restore options connecting to server without db name
This commit is contained in:
26
src/Microsoft.SqlTools.ServiceLayer/Utility/DatabaseUtils.cs
Normal file
26
src/Microsoft.SqlTools.ServiceLayer/Utility/DatabaseUtils.cs
Normal file
@@ -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.
|
||||
//
|
||||
|
||||
using System;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||
{
|
||||
public class DatabaseUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Check if the database is a system database
|
||||
/// </summary>
|
||||
/// <param name="databaseName">the name of database</param>
|
||||
/// <returns>return true if the database is a system database</returns>
|
||||
public static bool IsSystemDatabaseConnection(string databaseName)
|
||||
{
|
||||
return (string.IsNullOrWhiteSpace(databaseName) ||
|
||||
string.Compare(databaseName, CommonConstants.MasterDatabaseName, StringComparison.OrdinalIgnoreCase) == 0 ||
|
||||
string.Compare(databaseName, CommonConstants.MsdbDatabaseName, StringComparison.OrdinalIgnoreCase) == 0 ||
|
||||
string.Compare(databaseName, CommonConstants.ModelDatabaseName, StringComparison.OrdinalIgnoreCase) == 0 ||
|
||||
string.Compare(databaseName, CommonConstants.TempDbDatabaseName, StringComparison.OrdinalIgnoreCase) == 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,18 +25,7 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||
object value = Options[name];
|
||||
try
|
||||
{
|
||||
if (value != null && (typeof(T) != value.GetType()))
|
||||
{
|
||||
if (typeof(T) == typeof(int) || typeof(T) == typeof(int?))
|
||||
{
|
||||
value = Convert.ToInt32(value);
|
||||
}
|
||||
else if (typeof(T) == typeof(bool) || typeof(T) == typeof(bool?))
|
||||
{
|
||||
value = Convert.ToBoolean(value);
|
||||
}
|
||||
}
|
||||
result = value != null ? (T)value : default(T);
|
||||
result = GetValueAs<T>(value);
|
||||
}
|
||||
catch
|
||||
{
|
||||
@@ -48,6 +37,42 @@ namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||
return result;
|
||||
}
|
||||
|
||||
internal static T GetValueAs<T>(object value)
|
||||
{
|
||||
T result = default(T);
|
||||
|
||||
if (value != null && (typeof(T) != value.GetType()))
|
||||
{
|
||||
if (typeof(T) == typeof(int) || typeof(T) == typeof(int?))
|
||||
{
|
||||
value = Convert.ToInt32(value);
|
||||
}
|
||||
else if (typeof(T) == typeof(bool) || typeof(T) == typeof(bool?))
|
||||
{
|
||||
value = Convert.ToBoolean(value);
|
||||
}
|
||||
else if (typeof(T).IsEnum)
|
||||
{
|
||||
object enumValue;
|
||||
if (Enum.TryParse(typeof(T), value.ToString(), out enumValue))
|
||||
{
|
||||
value = (T)enumValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (value != null && (typeof(T).IsEnum))
|
||||
{
|
||||
object enumValue;
|
||||
if (Enum.TryParse(typeof(T), value.ToString(), out enumValue))
|
||||
{
|
||||
value = enumValue;
|
||||
}
|
||||
}
|
||||
result = value != null ? (T)value : default(T);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected void SetOptionValue<T>(string name, T value)
|
||||
{
|
||||
Options = Options ?? new Dictionary<string, object>();
|
||||
|
||||
Reference in New Issue
Block a user