Path: redb/redb.Core/Query/CollationNameValidator.cs
Snapshot: redb, 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
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Query/CollationNameValidator.cs
----
using System;
using System.Text.RegularExpressions;
namespace redb.Core.Query
{
///
/// Single source of truth for what may appear as a collation name in generated SQL.
///
///
/// This matters more than a normal setting. COLLATE takes an identifier, not a
/// value, so the name cannot be sent as a bound parameter: it is concatenated into the SQL text
/// the provider builds. An unchecked name is therefore a direct injection point, reachable from
/// application configuration.
///
///
///
/// Two independent defences, because either one alone is a single point of failure:
/// this validator rejects anything outside a conservative shape at the moment the value is
/// assigned, long before any SQL exists; and the emitters additionally quote the name
/// ( in C#, quote_ident() in the in-database functions). A name that
/// somehow passed the first would still be inert after the second.
///
///
///
/// The accepted shape is deliberately narrower than what PostgreSQL allows. Real collation names
/// are things like und-x-icu, ru-x-icu, en_US.utf8, C; none of them
/// need quotes, spaces or punctuation beyond the four characters below. Widening this later is
/// safe, narrowing it is not.
///
///
public static class CollationNameValidator
{
/// Longest name accepted. PostgreSQL identifiers truncate at 63 bytes.
public const int MaxLength = 63;
// Letters, digits, and the four separators that occur in real collation names.
// Notably absent: the double quote, which is what Quote() would have to escape.
private static readonly Regex AllowedShape =
new(@"^[A-Za-z0-9][A-Za-z0-9_.\-]*$", RegexOptions.Compiled | RegexOptions.CultureInvariant);
///
/// Throws unless is a plausible
/// collation name. Called when the setting is assigned, so a bad value fails at startup with
/// a message naming the setting, rather than as a driver-level syntax error on the first
/// search query.
///
public static void Validate(string name)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("Collation name must not be empty.", nameof(name));
if (name.Length > MaxLength)
throw new ArgumentException(
$"Collation name '{name}' is {name.Length} characters; the maximum is {MaxLength}.",
nameof(name));
if (!AllowedShape.IsMatch(name))
throw new ArgumentException(
$"Collation name '{name}' is not a valid identifier. Allowed: letters, digits, " +
"'_', '.' and '-', starting with a letter or digit. Examples: und-x-icu, ru-x-icu, en_US.utf8.",
nameof(name));
}
/// True when would pass .
public static bool IsValid(string? name) =>
!string.IsNullOrWhiteSpace(name)
&& name!.Length <= MaxLength
&& AllowedShape.IsMatch(name);
///
/// Renders the name as a quoted SQL identifier. The double quote is doubled even though
/// already rejects it: the escaping must be correct on its own, not
/// because something upstream promised it would never be needed.
///
public static string Quote(string name) => "\"" + name.Replace("\"", "\"\"") + "\"";
}
}