Path: redb-route/redb.Route.Sql/SqlParameterPlan.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/SqlParameterPlan.cs ---- using redb.Route.Abstractions; namespace redb.Route.Sql; /// /// The parameters of one statement, worked out once: its distinct :#name placeholders, the explicit param.name /// value each uses, the text in the provider's placeholder style and the parameters that text takes. A batch builds the plan /// once and binds every item with it. /// internal sealed class SqlParameterPlan { private readonly Dictionary _explicit; private SqlParameterPlan( string sourceSql, string sql, IReadOnlyList names, IReadOnlyList slots, Dictionary explicitParameters) { SourceSql = sourceSql; Sql = sql; Names = names; Slots = slots; _explicit = explicitParameters; HasExpressions = explicitParameters.Values.Any(p => p.IsExpression); } /// The statement as written, with :#name placeholders. internal string SourceSql { get; } /// The statement sent to the provider: placeholders in its style, everything else as written. internal string Sql { get; } /// Distinct placeholder names, without :#, in the order they first appear. internal IReadOnlyList Names { get; } /// /// The parameters of , in order: parameter i carries the value of Names[Slots[i]]. One per /// name for ; one per occurrence for the positional styles. /// internal IReadOnlyList Slots { get; } /// At least one explicit value is a ${...} expression, so binding needs an exchange to evaluate it in. internal bool HasExpressions { get; } /// /// Parses (with , as MySQL reads its literals), picks the explicit /// values its placeholders use and writes it in . /// /// A :# placeholder form that is not supported. internal static SqlParameterPlan Create( string sql, IReadOnlyDictionary explicitParameters, SqlPlaceholderStyle style = SqlPlaceholderStyle.At, bool backslashEscapes = false) { var placeholders = SqlParameterParser.FindPlaceholders(sql, backslashEscapes); var names = new List(); var indexByName = new Dictionary(StringComparer.OrdinalIgnoreCase); var occurrences = new List(placeholders.Count); // Names are compared ignoring case, and the text sent to the provider spells every occurrence the way the name was // first written: a provider that binds names case-sensitively (SQLite) must see one name for the one parameter. var canonical = new SqlPlaceholder[placeholders.Count]; for (var i = 0; i < placeholders.Count; i++) { var placeholder = placeholders[i]; if (!indexByName.TryGetValue(placeholder.Name, out var index)) { index = names.Count; indexByName[placeholder.Name] = index; names.Add(placeholder.Name); } occurrences.Add(index); canonical[i] = placeholder with { Name = names[index] }; } var used = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var name in names) { if (explicitParameters.TryGetValue(name, out var value)) used[name] = new ExplicitSqlParameter(value); } IReadOnlyList slots = style == SqlPlaceholderStyle.At ? Enumerable.Range(0, names.Count).ToArray() : occurrences; return new SqlParameterPlan(sql, SqlParameterParser.Rewrite(sql, canonical, style), names, slots, used); } /// The name of the parameter at of . internal string ParameterName(int slot) => Names[Slots[slot]]; /// The explicit param.name value for , if one is configured. internal bool TryGetExplicit(string name, out ExplicitSqlParameter parameter) => _explicit.TryGetValue(name, out parameter); } /// An explicit param.name value: a constant, or a ${...} expression evaluated per exchange. internal readonly struct ExplicitSqlParameter(string value) { /// The configured text. internal string Value { get; } = value; /// The value contains a ${...} expression. internal bool IsExpression { get; } = value.Contains("${", StringComparison.Ordinal); /// /// The value to bind; an expression is evaluated against . An explicit parameter always has a /// value: an expression that yields null, or an empty constant, binds NULL. /// internal object Resolve(IExchange? exchange) => SqlParameterBinder.ResolveParamValue(Value, exchange); }