Добавьте файлы проекта.
This commit is contained in:
59
SQLLinter/Common/SQLHelpers.cs
Normal file
59
SQLLinter/Common/SQLHelpers.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Microsoft.SqlServer.TransactSql.ScriptDom;
|
||||
using System.Text;
|
||||
|
||||
namespace SQLLinter.Common.Helpers;
|
||||
|
||||
public static class SQLHelpers
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Проверяет, что строка может быть закодирована в указанной SQL кодировке (collation).
|
||||
/// </summary>
|
||||
/// <param name="input">Строка для проверки</param>
|
||||
/// <param name="sqlEncodingName">Имя кодировки, например "windows-1251" или "iso-8859-1"</param>
|
||||
/// <returns>true, если строка полностью совместима</returns>
|
||||
public static bool IsValidForEncoding(string input, string sqlEncodingName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
return true;
|
||||
|
||||
Encoding enc;
|
||||
try
|
||||
{
|
||||
// Включаем поддержку старых кодировок (ANSI, OEM)
|
||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||
|
||||
enc = Encoding.GetEncoding(sqlEncodingName,
|
||||
new EncoderReplacementFallback("?"),
|
||||
new DecoderReplacementFallback("?"));
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
throw new ArgumentException($"Неизвестная кодировка: {sqlEncodingName}");
|
||||
}
|
||||
|
||||
return IsValidForEncoding(input, enc);
|
||||
}
|
||||
|
||||
public static bool IsValidForEncoding(string input, Encoding enc)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
return true;
|
||||
|
||||
// Пробуем закодировать и декодировать обратно
|
||||
byte[] bytes = enc.GetBytes(input);
|
||||
string roundtrip = enc.GetString(bytes);
|
||||
|
||||
// Если после кодирования/декодирования строка совпала - значит все символы поддерживаются
|
||||
return roundtrip == input;
|
||||
}
|
||||
|
||||
public static string ObjectGetFullName(SchemaObjectName name) => ObjectGetFullName(name.Identifiers);
|
||||
|
||||
|
||||
public static string ObjectGetFullName(IList<Identifier> identifiers)
|
||||
{
|
||||
return string.Join(".", identifiers.Select(i => "[" + i.Value + "]"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user