Path: redb-route/redb.Route.Sql/SqlNamedQueryRegistry.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Sql/SqlNamedQueryRegistry.cs ---- using System.Collections.Concurrent; namespace redb.Route.Sql; /// /// Registry for named SQL queries. Queries are registered at startup and referenced /// by name using the ref:name syntax in SQL properties. /// Thread-safe via . /// public interface ISqlNamedQueryRegistry { /// Registers a named query. /// Query name (case-insensitive). /// SQL text. void Register(string name, string sql); /// Resolves a named query. Throws if not found. /// Query name. /// SQL text. string Resolve(string name); /// Attempts to resolve a named query. /// Query name. /// SQL text if found. /// True if the query was found. bool TryResolve(string name, out string? sql); /// Returns all registered queries. IReadOnlyDictionary GetAll(); /// /// Resolves a SQL string that may contain a ref:name reference. /// If the string starts with "ref:", resolves to the registered query. /// Otherwise, returns the original string unchanged. /// /// SQL or ref:name reference. /// Resolved SQL text. string ResolveRef(string sql); } /// /// Default named query registry. Thread-safe, case-insensitive name matching. /// public sealed class SqlNamedQueryRegistry : ISqlNamedQueryRegistry { /// Prefix for named query references in SQL properties. public const string RefPrefix = "ref:"; private readonly ConcurrentDictionary _queries = new(StringComparer.OrdinalIgnoreCase); /// public void Register(string name, string sql) { ArgumentException.ThrowIfNullOrWhiteSpace(name); ArgumentException.ThrowIfNullOrWhiteSpace(sql); if (!_queries.TryAdd(name, sql)) throw new ArgumentException($"Named query '{name}' is already registered."); } /// public string Resolve(string name) { if (_queries.TryGetValue(name, out var sql)) return sql; throw new KeyNotFoundException( $"Named query '{name}' is not registered. " + "Register it via AddNamedQuery() in AddRedbRouteSql() configuration."); } /// public bool TryResolve(string name, out string? sql) => _queries.TryGetValue(name, out sql); /// public IReadOnlyDictionary GetAll() => _queries.ToDictionary(x => x.Key, x => x.Value, StringComparer.OrdinalIgnoreCase); /// /// Resolves a SQL string that may contain a ref:name reference. /// If the string starts with "ref:", resolves to the registered query. /// Otherwise, returns the original string unchanged. /// /// SQL or ref:name reference. /// Resolved SQL text. public string ResolveRef(string sql) { if (sql.StartsWith(RefPrefix, StringComparison.OrdinalIgnoreCase)) return Resolve(sql[RefPrefix.Length..]); return sql; } }