mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-21 09:35:39 -05:00
User management support classes (#1856)
* WIP * Fix nullable warnings in UserData class * WIP2 * WIP * Refresh database prototype classes * Fix some typos & merge issues * WIP * WIP * WIP * Additional updates * Remove unneded using
This commit is contained in:
185
src/Microsoft.SqlTools.ServiceLayer/Utility/LanguageUtils.cs
Normal file
185
src/Microsoft.SqlTools.ServiceLayer/Utility/LanguageUtils.cs
Normal file
@@ -0,0 +1,185 @@
|
||||
//
|
||||
// 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 Microsoft.SqlServer.Management.Common;
|
||||
using Microsoft.SqlServer.Management.Smo;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Summary description for CUtils.
|
||||
/// </summary>
|
||||
internal class LanguageUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets alias for a language name.
|
||||
/// </summary>
|
||||
/// <param name="connectedServer"></param>
|
||||
/// <param name="languageName"></param>
|
||||
/// <returns>Returns string.Empty in case it doesn't find a matching languageName on the server</returns>
|
||||
public static string GetLanguageAliasFromName(Server connectedServer,
|
||||
string languageName)
|
||||
{
|
||||
string languageAlias = string.Empty;
|
||||
|
||||
SetLanguageDefaultInitFieldsForDefaultLanguages(connectedServer);
|
||||
|
||||
foreach (Language lang in connectedServer.Languages)
|
||||
{
|
||||
if (lang.Name == languageName)
|
||||
{
|
||||
languageAlias = lang.Alias;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return languageAlias;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets name for a language alias.
|
||||
/// </summary>
|
||||
/// <param name="connectedServer"></param>
|
||||
/// <param name="languageAlias"></param>
|
||||
/// <returns>Returns string.Empty in case it doesn't find a matching languageAlias on the server</returns>
|
||||
public static string GetLanguageNameFromAlias(Server connectedServer,
|
||||
string languageAlias)
|
||||
{
|
||||
string languageName = string.Empty;
|
||||
|
||||
SetLanguageDefaultInitFieldsForDefaultLanguages(connectedServer);
|
||||
|
||||
foreach (Language lang in connectedServer.Languages)
|
||||
{
|
||||
if (lang.Alias == languageAlias)
|
||||
{
|
||||
languageName = lang.Name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return languageName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets lcid for a languageId.
|
||||
/// </summary>
|
||||
/// <param name="connectedServer"></param>
|
||||
/// <param name="languageAlias"></param>
|
||||
/// <returns>Throws exception in case it doesn't find a matching languageId on the server</returns>
|
||||
public static int GetLcidFromLangId(Server connectedServer,
|
||||
int langId)
|
||||
{
|
||||
int lcid = -1; //Unacceptable Lcid.
|
||||
|
||||
SetLanguageDefaultInitFieldsForDefaultLanguages(connectedServer);
|
||||
|
||||
foreach (Language lang in connectedServer.Languages)
|
||||
{
|
||||
if (lang.LangID == langId)
|
||||
{
|
||||
lcid = lang.LocaleID;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (lcid == -1) //Ideally this will never happen.
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("langId", "This language id is not present in sys.syslanguages catalog.");
|
||||
}
|
||||
|
||||
return lcid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets languageId for a lcid.
|
||||
/// </summary>
|
||||
/// <param name="connectedServer"></param>
|
||||
/// <param name="languageAlias"></param>
|
||||
/// <returns>Throws exception in case it doesn't find a matching lcid on the server</returns>
|
||||
public static int GetLangIdFromLcid(Server connectedServer,
|
||||
int lcid)
|
||||
{
|
||||
int langId = -1; //Unacceptable LangId.
|
||||
|
||||
SetLanguageDefaultInitFieldsForDefaultLanguages(connectedServer);
|
||||
|
||||
foreach (Language lang in connectedServer.Languages)
|
||||
{
|
||||
if (lang.LocaleID == lcid)
|
||||
{
|
||||
langId = lang.LangID;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (langId == -1) //Ideally this will never happen.
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("lcid", "This locale id is not present in sys.syslanguages catalog.");
|
||||
}
|
||||
|
||||
return langId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// returns a language choice alias for that language
|
||||
/// </summary>
|
||||
/// <param name="langid"></param>
|
||||
/// <returns></returns>
|
||||
public static LanguageChoice GetLanguageChoiceAlias(Server connectedServer,
|
||||
int lcid)
|
||||
{
|
||||
SetLanguageDefaultInitFieldsForDefaultLanguages(connectedServer);
|
||||
|
||||
foreach (Language smoL in connectedServer.Languages)
|
||||
{
|
||||
if (smoL.LocaleID == lcid)
|
||||
{
|
||||
string alias = smoL.Alias;
|
||||
return new LanguageChoice(alias, lcid);
|
||||
}
|
||||
}
|
||||
return new LanguageChoice(String.Empty, lcid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets exhaustive fields required for displaying and working with default languages in server,
|
||||
/// database and user dialogs as default init fields so that queries are not sent again and again.
|
||||
/// </summary>
|
||||
/// <param name="connectedServer">server on which languages will be enumerated</param>
|
||||
public static void SetLanguageDefaultInitFieldsForDefaultLanguages(Server connectedServer)
|
||||
{
|
||||
string[] fieldsNeeded = new string[] { "Alias", "Name", "LocaleID", "LangID" };
|
||||
connectedServer.SetDefaultInitFields(typeof(Language), fieldsNeeded);
|
||||
}
|
||||
}
|
||||
|
||||
#region interface - ILanguageLcidWithConnectionInfo - used by property editors to talk with data object
|
||||
interface ILanguageLcidWithConnectionInfo
|
||||
{
|
||||
int Lcid { get; }
|
||||
ServerConnection Connection { get; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region class - LanguageChoice
|
||||
internal class LanguageChoice
|
||||
{
|
||||
public string alias;
|
||||
public System.Int32 lcid;
|
||||
public LanguageChoice(string alias, System.Int32 lcid)
|
||||
{
|
||||
this.alias = alias;
|
||||
this.lcid = lcid;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return alias;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user