32 lines
1.4 KiB
C#
32 lines
1.4 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Reflection;
|
|
|
|
namespace Kit.Helpers.SqlHelperAbstract
|
|
{
|
|
/// <summary>
|
|
/// SQL-тексты библиотеки, хранятся EmbeddedResource-файлами в SqlHelperAbstract\Sql\*.sql (см. Kit.Helpers.csproj).
|
|
/// </summary>
|
|
internal static class SqlTexts
|
|
{
|
|
private static readonly ConcurrentDictionary<string, string> _cache = new();
|
|
|
|
/// <summary>Lookup «хранимой функции» в sys_functions (SQLite).</summary>
|
|
public static string SelectStoredFunction => Load("sys_function_select.sql");
|
|
|
|
private static string Load(string fileName)
|
|
{
|
|
return _cache.GetOrAdd(fileName, key =>
|
|
{
|
|
Assembly assembly = typeof(SqlTexts).Assembly;
|
|
string resourceName = assembly.GetManifestResourceNames().SingleOrDefault(x => x.EndsWith(key, StringComparison.Ordinal))
|
|
?? throw new InvalidOperationException($"Embedded sql resource not found: {key}");
|
|
|
|
using Stream stream = assembly.GetManifestResourceStream(resourceName)
|
|
?? throw new InvalidOperationException($"Embedded sql resource is empty: {resourceName}");
|
|
using var reader = new StreamReader(stream);
|
|
return reader.ReadToEnd().Trim();
|
|
});
|
|
}
|
|
}
|
|
}
|