26ff8d9b4f
Set up repository with legacy .NET Framework 4.8 source (OLD/), new .NET 10 Blazor solution (NEW/), OpenSpec specifications, documentation, and project configuration.
109 lines
3.4 KiB
C#
Executable File
109 lines
3.4 KiB
C#
Executable File
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using NLog;
|
|
|
|
namespace DataModel.Process
|
|
{
|
|
public class QueryRepository
|
|
{
|
|
/// <summary>
|
|
/// Repository name
|
|
/// </summary>
|
|
public string Name { get; }
|
|
|
|
/// <summary>
|
|
/// Repository base directory
|
|
/// </summary>
|
|
public string BaseDirectory { get; }
|
|
|
|
/// <summary>
|
|
/// In memory cache
|
|
/// </summary>
|
|
private static readonly Dictionary<string, string> cache = new Dictionary<string, string>();
|
|
|
|
/// <summary>
|
|
/// Shared logger instance
|
|
/// </summary>
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <param name="name">Repository name</param>
|
|
/// <param name="baseDirectory">Repository base directory</param>
|
|
public QueryRepository(string name, string baseDirectory)
|
|
{
|
|
Name = name;
|
|
BaseDirectory = baseDirectory;
|
|
|
|
if (!Directory.Exists(baseDirectory))
|
|
{
|
|
Directory.CreateDirectory(baseDirectory);
|
|
}
|
|
|
|
logger.Info($"Query repository '{name}' initialized at base directory '{baseDirectory}'.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets query SQL
|
|
/// </summary>
|
|
/// <param name="queryName">Name of query to get SQL for</param>
|
|
/// <returns>Query SQL</returns>
|
|
public string GetQuery(string queryName)
|
|
{
|
|
string querySource = string.Empty;
|
|
|
|
if (!cache.TryGetValue(queryName, out querySource))
|
|
{
|
|
//Check if file exists
|
|
string fullPath = Path.Combine(BaseDirectory, $"{queryName}.sql");
|
|
if (!File.Exists(fullPath))
|
|
{
|
|
throw new Exception($"Cannot retreive query '{queryName}': '{fullPath}' does not exist");
|
|
}
|
|
|
|
//Read file
|
|
querySource = File.ReadAllText(fullPath);
|
|
|
|
querySource = querySource.Replace("PRODDTA", "PRODDTA");
|
|
querySource = querySource.Replace("ARCDTAPD", "ARCDTAPD");
|
|
File.WriteAllText(fullPath, querySource);
|
|
|
|
//Cache source
|
|
cache.Add(queryName, querySource);
|
|
}
|
|
|
|
return querySource;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets querie's SQL
|
|
/// </summary>
|
|
/// <param name="queryName">Name of query to set SQL for</param>
|
|
/// <param name="querySource">SQL to set for query</param>
|
|
public void SetQuery(string queryName, string querySource)
|
|
{
|
|
logger.Info($"[{Name}] setting SQL for query '{queryName}'.");
|
|
|
|
//Update cache
|
|
cache[queryName] = querySource;
|
|
|
|
//Write source to file
|
|
string fullPath = Path.Combine(BaseDirectory, $"{queryName}.sql");
|
|
File.WriteAllText(fullPath, querySource);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Indexer override
|
|
/// </summary>
|
|
/// <param name="queryName">Name of query</param>
|
|
/// <returns>SQL for query</returns>
|
|
public string this[string queryName]
|
|
{
|
|
get => GetQuery(queryName);
|
|
set => SetQuery(queryName, value);
|
|
}
|
|
}
|
|
}
|