diff --git a/Source/Core/ExpressiveCommandBase.cs b/Source/Core/ExpressiveCommandBase.cs index 396f107..012b14a 100644 --- a/Source/Core/ExpressiveCommandBase.cs +++ b/Source/Core/ExpressiveCommandBase.cs @@ -40,9 +40,28 @@ protected IDbConnectionPool ConnectionProvider readonly object _poolOrConnection; int _busy; - // Set while a parameter added by AddParam(value) or AddParams(object) is still waiting for its name to be resolved. + // Set when a parameter added by AddParam(value) or AddParams(object) + // has a name that is worked out in every run. It is never cleared. bool _hasDerivedParams; + // While a command with derived names is being set up (the one + // synchronous stretch in PrepareDerived), on one thread: the command, + // and the list of what this run sends under the resolved names. + // Per thread, so two threads running one command never see each + // other's list. + // Params returns the list to the command's own hook (AddParams) + // and to nobody else. + [ThreadStatic] + static object? _preparing; + + [ThreadStatic] + static List? _sendList; + + // The list kept for the next run on this thread + // (empty, so it holds no values). + [ThreadStatic] + static List? _spareList; + // The rarely-set members live in a block that is only allocated when one is set, so the usual // conn.Command(sql) object stays small (it is allocated on every call). private protected sealed class CommandOptions @@ -171,8 +190,23 @@ protected ExpressiveCommandBase( /// /// The list of params to apply to the command before execution. + /// Running the command never changes it: + /// it holds what was added, in order, under the names as added. + /// The names an AddParam(value) or AddParams(object) + /// parameter is sent under are worked out in every run + /// (they can differ per database) + /// and are visible in Execute(command => ...) + /// on the provider's command. /// - public List Params => _params ??= []; + public List Params => ActiveSendList ?? (_params ??= []); + + // The list of what this run sends, while this command is being set up + // on this thread; otherwise null. + // A command with no derived names pays one field read. + List? ActiveSendList + => _hasDerivedParams && ReferenceEquals(_preparing, this) + ? _sendList + : null; // Most commands have no params, so the list is only created when one is added. List? _params; @@ -206,7 +240,7 @@ protected TCommand PrepareCommand(TConnection connection) throw new InvalidCastException($"Actual command type ({cmd.GetType()}) is not compatible with expected command type ({typeof(TCommand)})."); if (Transaction != null) c.Transaction = Transaction; - if (_params is { Count: > 0 }) AddParams(c); + if ((ActiveSendList ?? _params) is { Count: > 0 }) AddParams(c); return c; } catch @@ -581,57 +615,108 @@ protected virtual ValueTask> GetProcedureParameterNamesAs // A text with no @name marker has a set with no names, which means "not known". static readonly ConditionalWeakTable TextNames = new(); - // Runs after the connection is open and before the command is built, once per command: it replaces the provisional names with the real ones. + // Runs in every run of a command that has derived names, + // after the connection is open and before the command is built. + // It finds the names the connection's procedure declares + // (or the @name markers of the text); nothing is written to Params. // What is remembered is a NameSet (the names and the match found for each argument), so a repeat execution allocates nothing to resolve. - void ResolveDerivedParams(TConnection connection) + NameSet? ResolveDerivedNames(TConnection connection) { - if (!_hasDerivedParams) return; - - NameSet? names = null; - bool remembered = false; if (Type == CommandType.StoredProcedure) { ProcedureKey key = ProcedureKey(connection); - remembered = ProcedureNameCache.TryGet(NameCacheOwner, key, out names); - if (!remembered) names = Remember(key, GetProcedureParameterNames(connection, Command)); - } - else if (Type == CommandType.Text) - { - names = TextNames.GetValue(Command, static text => new NameSet(SqlTextParameters.Parse(text))); // the @name markers found in the text - } + if (ProcedureNameCache.TryGet( + NameCacheOwner, + key, + out NameSet? remembered) + && Fits(remembered)) + { + return remembered; + } - if (ApplyDerivedNames(names, final: !remembered)) return; + // Nothing is remembered, or the remembered names did not fit: + // the procedure may have changed since they were read. + return Remember(key, GetProcedureParameterNames(connection, Command)); + } - // The remembered names did not fit: the procedure may have changed since they were read. Read them again before deciding that nothing matches. - names = Remember(ProcedureKey(connection), GetProcedureParameterNames(connection, Command)); - ApplyDerivedNames(names, final: true); + // The @name markers found in the text. + return Type == CommandType.Text + ? TextNames.GetValue( + Command, + static text => new NameSet(SqlTextParameters.Parse(text))) + : null; } // Not an async method itself: when the names are remembered (the usual case) it finishes without a state machine or an await; only reading them is asynchronous. - ValueTask ResolveDerivedParamsAsync(TConnection connection, CancellationToken cancellationToken) + ValueTask ResolveDerivedNamesAsync( + TConnection connection, + CancellationToken cancellationToken) { - NameSet? names = null; - bool remembered = false; if (Type == CommandType.StoredProcedure) { - remembered = ProcedureNameCache.TryGet(NameCacheOwner, ProcedureKey(connection), out names); - if (!remembered) return ReadNamesAndApplyAsync(connection, cancellationToken); - } - else if (Type == CommandType.Text) - { - names = TextNames.GetValue(Command, static text => new NameSet(SqlTextParameters.Parse(text))); // the @name markers found in the text + ProcedureKey key = ProcedureKey(connection); + return ProcedureNameCache.TryGet( + NameCacheOwner, + key, + out NameSet? remembered) + && Fits(remembered) + ? new(remembered) + : ReadNamesAsync(connection, key, cancellationToken); } - if (ApplyDerivedNames(names, final: !remembered)) return default; - - // The remembered names did not fit: the procedure may have changed since they were read. Read them again before deciding that nothing matches. - return ReadNamesAndApplyAsync(connection, cancellationToken); + // The @name markers found in the text. + return new( + Type == CommandType.Text + ? TextNames.GetValue( + Command, + static text => new NameSet(SqlTextParameters.Parse(text))) + : null); } - async ValueTask ReadNamesAndApplyAsync(TConnection connection, CancellationToken cancellationToken) + async ValueTask ReadNamesAsync( + TConnection connection, + ProcedureKey key, + CancellationToken cancellationToken) + => Remember( + key, + await GetProcedureParameterNamesAsync( + connection, + Command, + cancellationToken) + .ConfigureAwait(false)); + + TCommand PrepareResolved(TConnection connection) + => _hasDerivedParams + ? PrepareDerived(connection, ResolveDerivedNames(connection)) + : PrepareCommand(connection); + + // Builds the send list, sets up the command and resets, in one + // synchronous stretch: never across an await, as the list belongs + // to this thread. + // The caller's Params are not touched, so the next run (on another + // database, say) starts from what was added. + TCommand PrepareDerived(TConnection connection, NameSet? names) { - NameSet? names = Remember(ProcedureKey(connection), await GetProcedureParameterNamesAsync(connection, Command, cancellationToken).ConfigureAwait(false)); - ApplyDerivedNames(names, final: true); + // Taken out of the spare slot, so a hook that sets up another + // command on this thread builds its own list. + List send = _spareList ?? new(_params!.Count); + _spareList = null; + object? outerCommand = _preparing; + List? outerList = _sendList; + try + { + BuildSendList(send, names); + _preparing = this; + _sendList = send; + return PrepareCommand(connection); + } + finally + { + _preparing = outerCommand; + _sendList = outerList; + send.Clear(); + _spareList = send; + } } // Remembers the names a procedure reported and returns the set made from them; an empty answer is "not known" (null) and is not remembered. @@ -651,18 +736,44 @@ async ValueTask ReadNamesAndApplyAsync(TConnection connection, CancellationToken // The key is a struct of the three parts, so no string is built. ProcedureKey ProcedureKey(TConnection connection) => new((connection as DbConnection)?.DataSource, connection.Database, Command); - // Returns false, leaving the parameters untouched, when an argument named by the caller matches none of the real names and `final` is false (the names may be out of date). - // `real` is null when the names are not known (and has no names for a text without markers): each argument then keeps its last member as written. - bool ApplyDerivedNames(NameSet? real, bool final) + // False when an argument named by the caller matches none of the + // remembered names: the procedure may have changed since they were read. + // Nothing is built or kept, so a false answer leaves no state behind. + // `real` is null when the names are not known (and has no names for + // a text without markers): each argument then keeps its last member + // as written. + bool Fits(NameSet? real) { - List list = Params; + if (real is not { Names.Length: > 0 }) return true; + + List list = _params!; + for (int i = 0; i < list.Count; i++) + { + Param p = list[i]; + if (p.Expression is { } expression + && !p.FromObject + && real.Match(expression, Command) is null) + { + return false; + } + } + + return true; + } + + // Builds, in `send`, what one run sends: the caller's parameters + // under their resolved names, in order. + // The caller's list (_params) is only read. + void BuildSendList(List send, NameSet? real) + { + List list = _params!; int count = list.Count; // The name each parameter ends up with (null: not sent), in a pooled array; only the used part is cleared when it goes back. string?[] names = ArrayPool.Shared.Rent(count); try { - return Apply(list, count, names, real, final); + Apply(list, count, names, real, send); } finally { @@ -671,7 +782,12 @@ bool ApplyDerivedNames(NameSet? real, bool final) } } - bool Apply(List list, int count, string?[] names, NameSet? real, bool final) + void Apply( + List list, + int count, + string?[] names, + NameSet? real, + List send) { bool known = real is { Names.Length: > 0 }; @@ -690,7 +806,6 @@ bool Apply(List list, int count, string?[] names, NameSet? real, bool fin { // A member of an object that the procedure does not take is simply not sent; an argument named by the caller must match. if (p.FromObject) { names[i] = null; continue; } // the pooled slot holds whatever an earlier renter left: write it before it is read - if (!final) return false; throw NoMatch(expression, real!.Names); } @@ -723,18 +838,13 @@ bool Apply(List list, int count, string?[] names, NameSet? real, bool fin } } - // Keep what is sent, in order, under its resolved name. - int written = 0; + // What is sent, in order, under its resolved name. for (int i = 0; i < count; i++) { if (names[i] is not { } name) continue; Param p = list[i]; - list[written++] = p.Expression is null ? p : p.Resolved(name); + send.Add(p.Expression is null ? p : p.Resolved(name)); } - - list.RemoveRange(written, count - written); - _hasDerivedParams = false; - return true; } static bool SameName(string a, string b) => Bare(a).Equals(Bare(b), StringComparison.OrdinalIgnoreCase); @@ -777,8 +887,7 @@ private protected TResult ExecuteCore(TState state, Func ExecuteCoreAsync(TSt try { ConnectionState prior = await OpenIfNeededAsync(single, cancellationToken).ConfigureAwait(false); - if (_hasDerivedParams) await ResolveDerivedParamsAsync(single, cancellationToken).ConfigureAwait(false); - using TCommand cmd = PrepareCommand(single); + NameSet? names = null; + if (_hasDerivedParams) + { + names = await ResolveDerivedNamesAsync( + single, + cancellationToken) + .ConfigureAwait(false); + } + + using TCommand cmd = _hasDerivedParams + ? PrepareDerived(single, names) + : PrepareCommand(single); return await func(cmd, prior, state).ConfigureAwait(false); } finally @@ -831,8 +950,16 @@ private protected async ValueTask ExecuteCoreAsync(TSt try { ConnectionState prior = await OpenIfNeededAsync(conn, cancellationToken).ConfigureAwait(false); - if (_hasDerivedParams) await ResolveDerivedParamsAsync(conn, cancellationToken).ConfigureAwait(false); - using TCommand cmd = PrepareCommand(conn); + NameSet? names = null; + if (_hasDerivedParams) + { + names = await ResolveDerivedNamesAsync(conn, cancellationToken) + .ConfigureAwait(false); + } + + using TCommand cmd = _hasDerivedParams + ? PrepareDerived(conn, names) + : PrepareCommand(conn); return await func(cmd, prior, state).ConfigureAwait(false); } finally @@ -857,8 +984,7 @@ TResult ExecuteDirect(TConnection single, TState state, Func 0 } path) File.AppendAllText(path, line + Environment.NewLine); } - static void AssertWithinBudget(string label, long derived, long explicitName) + internal static void AssertWithinBudget(string label, long derived, long explicitName) { Report(label, derived, explicitName); Assert.True(derived <= explicitName + Slack, $"{label}: derived {derived} B per call, explicit {explicitName} B, budget explicit + {Slack}."); diff --git a/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunCostTests.cs b/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunCostTests.cs new file mode 100644 index 0000000..a46f7ba --- /dev/null +++ b/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunCostTests.cs @@ -0,0 +1,146 @@ +using System.Data.Common; + +namespace Open.Database.Extensions.Tests; + +#nullable enable + +// R20: the names are resolved in every run now, so a repeat run of one +// command object with derived names must still cost what the same command +// with explicit names costs: nothing per run for the send list once warm +// (the list is kept per thread and reused). The explicit run is the +// no-derived path, which is measured in the same pass. +[Collection("Heap measurements")] +[ExcludeFromCodeCoverage] +public static class DerivedNamesEveryRunCostTests +{ + sealed class NamedCommand( + DbConnection connection, + string[] names) + : ExpressiveDbCommand( + connection, + CommandType.StoredProcedure, + ParameterNameHarness.NewProcedure()) + { + int _lookups; + + public int Lookups => Volatile.Read(ref _lookups); + + protected override ImmutableArray GetProcedureParameterNames( + DbConnection connection, + string procedure) + { + Interlocked.Increment(ref _lookups); + return [.. names]; + } + + protected override ValueTask> + GetProcedureParameterNamesAsync( + DbConnection connection, + string procedure, + CancellationToken cancellationToken) + => new(GetProcedureParameterNames(connection, procedure)); + } + + sealed class Args + { + public int OrderId { get; set; } = 5; + public int CustomerId { get; set; } = 7; + public DateTime StartDate { get; set; } = new(2026, 1, 2); + } + + static readonly string[] Real = ["@order_id", "@customer_id", "@start_date"]; + + sealed class Rig + { + readonly DerivedParameterNameTests.RecordingDbCommand _db = new(); + public DbConnection Connection { get; } + + public Rig() + => Connection = new DerivedParameterNameTests.OpenDbConnection(_db); + + // One command object, run again and again: the names are cached + // after the first run. + public Action Sync(ExpressiveDbCommand command) + => () => + { + command.ExecuteNonQuery(); + _db.Sent.Clear(); + }; + + public Action Async(ExpressiveDbCommand command) + => () => + { + command.ExecuteNonQueryAsync().AsTask().GetAwaiter().GetResult(); + _db.Sent.Clear(); + }; + } + + static NamedCommand Derived(Rig rig) + { + var command = new NamedCommand(rig.Connection, Real); + int orderId = 5; + int customerId = 7; + var startDate = new DateTime(2026, 1, 2); + command + .AddParam(orderId) + .AddParam(customerId) + .AddParam(startDate); + return command; + } + + static NamedCommand Explicit(Rig rig) + { + var command = new NamedCommand(rig.Connection, Real); + command + .AddParam("@order_id", 5) + .AddParam("@customer_id", 7) + .AddParam("@start_date", new DateTime(2026, 1, 2)); + return command; + } + + [Fact] + public static void ARepeatRun_WithDerivedNames_CostsWhatExplicitNamesCost() + { + var rig = new Rig(); + long derived = DerivedNameCostTests.BytesPerCall(rig.Sync(Derived(rig))); + long named = DerivedNameCostTests.BytesPerCall(rig.Sync(Explicit(rig))); + + DerivedNameCostTests.AssertWithinBudget( + "repeat run, three derived parameters", + derived, + named); + } + + [Fact] + public static void ARepeatRun_WithAnObject_CostsWhatExplicitNamesCost() + { + var rig = new Rig(); + var command = new NamedCommand(rig.Connection, Real); + command.AddParams(new Args()); + + long derived = DerivedNameCostTests.BytesPerCall(rig.Sync(command)); + long named = DerivedNameCostTests.BytesPerCall(rig.Sync(Explicit(rig))); + + DerivedNameCostTests.AssertWithinBudget( + "repeat run, an object of three members", + derived, + named); + } + + [Fact] + public static void ARepeatRun_OnTheAsyncPath_CostsWhatExplicitNamesCost() + { + var rig = new Rig(); + NamedCommand command = Derived(rig); + long derived = DerivedNameCostTests.BytesPerCall(rig.Async(command)); + long named = DerivedNameCostTests.BytesPerCall(rig.Async(Explicit(rig))); + + DerivedNameCostTests.AssertWithinBudget( + "repeat run, async, three derived parameters", + derived, + named); + + // The names were read once, not in every run. + Assert.Equal(1, command.Lookups); + } +} diff --git a/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunSqlServerTests.cs b/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunSqlServerTests.cs new file mode 100644 index 0000000..ae6caf6 --- /dev/null +++ b/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunSqlServerTests.cs @@ -0,0 +1,169 @@ +using System.Data.Common; +using Microsoft.Data.SqlClient; + +namespace Open.Database.Extensions.Tests; + +#nullable enable + +// R20 on a real server: one command object, run against two databases where +// the procedure of the same name takes differently named parameters. +// The first run's names used to be written into Params, so the second run +// sent them to a database that does not know them (SQL Server error 201 or +// 8145), and a member the first database did not take was gone for good. +// The two databases are tempdb and master of LocalDB, so no file is created. +// These tests never skip: a missing server is a failure. +[ExcludeFromCodeCoverage] +public static class DerivedNamesEveryRunSqlServerTests +{ + static CancellationToken Token => TestContext.Current.CancellationToken; + + const string First = "tempdb"; + const string Second = "master"; + + static string ConnectionString(string database) + => new SqlConnectionStringBuilder(LocalDbFixture.ConnectionString) + { + InitialCatalog = database, + Pooling = false, + }.ConnectionString; + + static void Run(string database, string sql) + { + using var connection = new SqlConnection(ConnectionString(database)); + connection.Open(); + using SqlCommand command = connection.CreateCommand(); + command.CommandText = sql; + command.ExecuteNonQuery(); + } + + // The same procedure name in both databases, each with its own + // parameters and body, dropped when this goes. + sealed class Procedures : IDisposable + { + public Procedures( + string parametersOne, + string bodyOne, + string parametersTwo, + string bodyTwo) + { + LocalDbFixture.RequireAvailable(); + Name = LocalDbFixture.UniqueName("ode_a6"); + Run( + First, + $"create procedure dbo.{Name} {parametersOne} as {bodyOne}"); + Run( + Second, + $"create procedure dbo.{Name} {parametersTwo} as {bodyTwo}"); + } + + public string Name { get; } + + public string Qualified => "dbo." + Name; + + public void Dispose() + { + Run(First, $"drop procedure if exists dbo.{Name}"); + Run(Second, $"drop procedure if exists dbo.{Name}"); + } + } + + // The first database names the parameter @order_id, the second + // @p_order_id; each returns what it was given. + static Procedures DifferentlyNamed() + => new( + "@order_id int", + "select @order_id as one", + "@p_order_id int", + "select @p_order_id as one"); + + static int Value(object? scalar) => Convert.ToInt32(scalar); + + [Fact] + public static void AFactory_TwoDatabases_EachRunSendsItsOwnNames() + { + using Procedures procedures = DifferentlyNamed(); + string database = First; + var command = new ExpressiveDbCommand( + () => new SqlConnection(ConnectionString(database)), + CommandType.StoredProcedure, + procedures.Qualified); + int orderId = 5; + command.AddParam(orderId); + + Assert.Equal(5, Value(command.ExecuteScalar())); + + database = Second; + Assert.Equal(5, Value(command.ExecuteScalar())); + + database = First; + Assert.Equal(5, Value(command.ExecuteScalar())); + Assert.Equal(["orderId"], command.Params.Select(static p => p.Name)); + } + + [Fact] + public static async Task AFactory_TwoDatabases_EachRunSendsItsOwnNames_Async() + { + using Procedures procedures = DifferentlyNamed(); + string database = First; + var command = new ExpressiveDbCommand( + () => new SqlConnection(ConnectionString(database)), + CommandType.StoredProcedure, + procedures.Qualified); + int orderId = 5; + command.AddParam(orderId); + + Assert.Equal(5, Value(await command.ExecuteScalarAsync(Token))); + + database = Second; + Assert.Equal(5, Value(await command.ExecuteScalarAsync(Token))); + } + + [Fact] + public static void ChangeDatabase_SameCommand_EachRunSendsItsOwnNames() + { + using Procedures procedures = DifferentlyNamed(); + using var connection = new SqlConnection(ConnectionString(First)); + connection.Open(); + var command = new ExpressiveDbCommand( + connection, + CommandType.StoredProcedure, + procedures.Qualified); + int orderId = 5; + command.AddParam(orderId); + + Assert.Equal(5, Value(command.ExecuteScalar())); + + connection.ChangeDatabase(Second); + Assert.Equal(5, Value(command.ExecuteScalar())); + + connection.ChangeDatabase(First); + Assert.Equal(5, Value(command.ExecuteScalar())); + Assert.Equal(["orderId"], command.Params.Select(static p => p.Name)); + } + + [Fact] + public static void ADroppedMember_ComesBack_OnTheDatabaseThatTakesIt() + { + // The first database does not take Region; the second does, and its + // body shows whether it arrived. + using var procedures = new Procedures( + "@order_id int", + "select @order_id as one", + "@order_id int, @region nvarchar(20) = null", + "select case when @region = N'west' then 1 else 0 end as one"); + string database = First; + var command = new ExpressiveDbCommand( + () => new SqlConnection(ConnectionString(database)), + CommandType.StoredProcedure, + procedures.Qualified); + command.AddParams(new { OrderId = 5, Region = "west" }); + + Assert.Equal(5, Value(command.ExecuteScalar())); + + database = Second; + Assert.Equal(1, Value(command.ExecuteScalar())); + Assert.Equal( + ["OrderId", "Region"], + command.Params.Select(static p => p.Name)); + } +} diff --git a/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunTests.cs b/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunTests.cs new file mode 100644 index 0000000..1e0f295 --- /dev/null +++ b/Tests/Open.Database.Extensions.Core.Tests/DerivedNamesEveryRunTests.cs @@ -0,0 +1,640 @@ +using System.Collections.Concurrent; +using System.Data.Common; + +namespace Open.Database.Extensions.Tests; + +#nullable enable + +// R20: the names of AddParam(value) and AddParams(object) parameters are +// worked out in every run, against the connection that run uses, and the +// caller's Params are never rewritten. One command object then serves two +// databases whose procedure of the same name takes different parameter names. +[ExcludeFromCodeCoverage] +public static class DerivedNamesEveryRunTests +{ + static CancellationToken Token => TestContext.Current.CancellationToken; + + // ---- the fakes ---- + + // What one run was given: the database to connect to, and what was + // created for it. + sealed class Slot(string database) + { + public string Database { get; } = database; + public DerivedParameterNameTests.RecordingDbCommand? Command { get; set; } + public int Disposals; + } + + sealed class DatabaseConnection(string database, DbCommand command) + : DbConnection + { + [AllowNull] + public override string ConnectionString { get; set; } = ""; + public override string Database => database; + public override string DataSource => "fake"; + public override string ServerVersion => "1.0"; + public override ConnectionState State => ConnectionState.Open; + public override void ChangeDatabase(string databaseName) { } + public override void Open() { } + public override void Close() { } + + protected override DbTransaction BeginDbTransaction( + IsolationLevel isolationLevel) + => throw new NotSupportedException(); + + protected override DbCommand CreateDbCommand() => command; + } + + // Connections come from here, to the database the running flow chose: + // the choice travels with the flow, so it also holds across an await. + sealed class Rig + { + readonly AsyncLocal _current = new(); + + public Slot Use(string database) + { + var slot = new Slot(database); + _current.Value = slot; + return slot; + } + + public DatabaseConnection Create() + { + Slot slot = _current.Value!; + var command = new DerivedParameterNameTests.RecordingDbCommand(); + command.Disposed += (_, _) + => Interlocked.Increment(ref slot.Disposals); + slot.Command = command; + return new DatabaseConnection(slot.Database, command); + } + } + + // The same procedure on every database, declaring what that database says. + sealed class DeclaredCommand( + Rig rig, + Dictionary declared) + : ExpressiveDbCommand( + rig.Create, + CommandType.StoredProcedure, + ParameterNameHarness.NewProcedure()) + { + int _lookups; + + public int Lookups => Volatile.Read(ref _lookups); + + protected override ImmutableArray GetProcedureParameterNames( + DbConnection connection, + string procedure) + { + Interlocked.Increment(ref _lookups); + return [.. declared[connection.Database]]; + } + + protected override ValueTask> + GetProcedureParameterNamesAsync( + DbConnection connection, + string procedure, + CancellationToken cancellationToken) + => new(GetProcedureParameterNames(connection, procedure)); + } + + // A provider's command that reads Params in its own hook, as the + // library's four do. + sealed class HookCommand( + Rig rig, + Dictionary declared) + : ExpressiveDbCommand( + rig.Create, + CommandType.StoredProcedure, + ParameterNameHarness.NewProcedure()) + { + + // What Params held inside the hook. + public string[]? Seen { get; private set; } + public bool SawTheCallersList { get; private set; } + public List? Callers { get; set; } + + // Run before the hook reads Params (a second command to execute, + // or a failure). + public Action? Before { get; set; } + + protected override ImmutableArray GetProcedureParameterNames( + DbConnection connection, + string procedure) + => [.. declared[connection.Database]]; + + protected override void AddParams(DbCommand command) + { + Before?.Invoke(); + Seen = [.. Params.Select(static p => p.Name)]; + SawTheCallersList = ReferenceEquals(Params, Callers); + base.AddParams(command); + } + } + + sealed class Args + { + public int OrderId { get; set; } = 5; + public string Region { get; set; } = "west"; + } + + // The same procedure, as two databases declare it. + static Dictionary Twice() + => new() + { + ["a"] = ["@order_id", "@customer_id"], + ["b"] = ["@p_order_id", "@p_customer_id"], + }; + + static string[] Sent(Slot slot) + => [.. slot.Command!.Sent.Select(static p => p.ParameterName)]; + + static string[] Added(ExpressiveDbCommand command) + => [.. command.Params.Select(static p => p.Name)]; + + // ---- the defect ---- + + [Fact] + public static void OneCommand_TwoDatabases_EachRunSendsItsOwnNames() + { + var rig = new Rig(); + var command = new DeclaredCommand(rig, Twice()); + int orderId = 5; + int customerId = 7; + command + .AddParam(orderId) + .AddParam(customerId); + + Slot first = rig.Use("a"); + command.ExecuteNonQuery(); + Slot second = rig.Use("b"); + command.ExecuteNonQuery(); + Slot third = rig.Use("a"); + command.ExecuteNonQuery(); + + Assert.Equal(["@order_id", "@customer_id"], Sent(first)); + Assert.Equal(["@p_order_id", "@p_customer_id"], Sent(second)); + Assert.Equal(["@order_id", "@customer_id"], Sent(third)); + + // One read per database: the memory is right, only the rewrite was not. + Assert.Equal(2, command.Lookups); + } + + [Fact] + public static async Task OneCommand_TwoDatabases_EachRunSendsItsOwnNamesAsync() + { + var rig = new Rig(); + var command = new DeclaredCommand(rig, Twice()); + int orderId = 5; + int customerId = 7; + command + .AddParam(orderId) + .AddParam(customerId); + + Slot first = rig.Use("a"); + await command.ExecuteNonQueryAsync(Token); + Slot second = rig.Use("b"); + await command.ExecuteNonQueryAsync(Token); + Slot third = rig.Use("a"); + await command.ExecuteNonQueryAsync(Token); + + Assert.Equal(["@order_id", "@customer_id"], Sent(first)); + Assert.Equal(["@p_order_id", "@p_customer_id"], Sent(second)); + Assert.Equal(["@order_id", "@customer_id"], Sent(third)); + } + + [Fact] + public static void ADroppedMember_IsSentOnADatabaseThatTakesIt() + { + var rig = new Rig(); + var command = new DeclaredCommand( + rig, + new() + { + ["a"] = ["@order_id"], + ["b"] = ["@order_id", "@region"], + }); + command.AddParams(new Args()); + + Slot first = rig.Use("a"); + command.ExecuteNonQuery(); + Slot second = rig.Use("b"); + command.ExecuteNonQuery(); + Slot third = rig.Use("a"); + command.ExecuteNonQuery(); + + Assert.Equal(["@order_id"], Sent(first)); + Assert.Equal(["@order_id", "@region"], Sent(second)); + Assert.Equal(["@order_id"], Sent(third)); + Assert.Equal(["OrderId", "Region"], Added(command)); + } + + [Fact] + public static async Task ADroppedMember_IsSentOnADatabaseThatTakesIt_Async() + { + var rig = new Rig(); + var command = new DeclaredCommand( + rig, + new() + { + ["a"] = ["@order_id"], + ["b"] = ["@order_id", "@region"], + }); + command.AddParams(new Args()); + + Slot first = rig.Use("a"); + await command.ExecuteNonQueryAsync(Token); + Slot second = rig.Use("b"); + await command.ExecuteNonQueryAsync(Token); + + Assert.Equal(["@order_id"], Sent(first)); + Assert.Equal(["@order_id", "@region"], Sent(second)); + } + + // ---- Params is what the caller added ---- + + [Fact] + public static void Params_AreUnchangedByARun() + { + var rig = new Rig(); + var command = new DeclaredCommand(rig, Twice()); + int orderId = 5; + int customerId = 7; + command + .AddParam(orderId) + .AddParam(customerId); + List list = command.Params; + + rig.Use("b"); + command.ExecuteNonQuery(); + rig.Use("a"); + command.ExecuteNonQuery(); + + Assert.Same(list, command.Params); + Assert.Equal(["orderId", "customerId"], Added(command)); + Assert.Equal([5, 7], command.Params.Select(static p => p.Value)); + } + + [Fact] + public static async Task Params_AreUnchangedByARun_Async() + { + var rig = new Rig(); + var command = new DeclaredCommand(rig, Twice()); + int orderId = 5; + int customerId = 7; + command + .AddParam(orderId) + .AddParam(customerId); + List list = command.Params; + + rig.Use("b"); + await command.ExecuteNonQueryAsync(Token); + rig.Use("a"); + await command.ExecuteNonQueryAsync(Token); + + Assert.Same(list, command.Params); + Assert.Equal(["orderId", "customerId"], Added(command)); + } + + // ---- the same command on several threads at once ---- + + [Fact] + public static void OneCommand_ManyThreads_EveryRunSendsItsOwnNames() + { + const int threads = 8; + const int commands = 40; + const int rounds = 6; + var failures = new ConcurrentQueue(); + + for (int c = 0; c < commands; c++) + { + var rig = new Rig(); + var command = new DeclaredCommand(rig, Twice()); + int orderId = 5; + int customerId = 7; + command + .AddParam(orderId) + .AddParam(customerId); + var barrier = new Barrier(threads); + + // Own threads: a barrier of this size would starve the pool. + Thread[] all = [.. Enumerable.Range(0, threads).Select(t => + new Thread(() => + { + string database = t % 2 == 0 ? "a" : "b"; + string[] expected = database == "a" + ? ["@order_id", "@customer_id"] + : ["@p_order_id", "@p_customer_id"]; + for (int r = 0; r < rounds; r++) + { + barrier.SignalAndWait(TimeSpan.FromSeconds(30)); + try + { + Slot slot = rig.Use(database); + command.ExecuteNonQuery(); + string[] sent = Sent(slot); + if (!expected.SequenceEqual(sent)) + { + failures.Enqueue( + $"{database}: {string.Join(",", sent)}"); + } + } + catch (Exception ex) + { + failures.Enqueue($"{database}: {ex.Message}"); + } + } + }))]; + + foreach (Thread thread in all) thread.Start(); + foreach (Thread thread in all) thread.Join(); + barrier.Dispose(); + } + + Assert.Empty(failures); + } + + [Fact] + public static async Task OneCommand_ManyTasks_EveryRunSendsItsOwnNames() + { + const int tasks = 8; + const int rounds = 200; + var failures = new ConcurrentQueue(); + var rig = new Rig(); + var command = new DeclaredCommand(rig, Twice()); + int orderId = 5; + int customerId = 7; + command + .AddParam(orderId) + .AddParam(customerId); + + await Task.WhenAll(Enumerable.Range(0, tasks).Select(t => Task.Run( + async () => + { + string database = t % 2 == 0 ? "a" : "b"; + string[] expected = database == "a" + ? ["@order_id", "@customer_id"] + : ["@p_order_id", "@p_customer_id"]; + for (int r = 0; r < rounds; r++) + { + Slot slot = rig.Use(database); + await command.ExecuteNonQueryAsync(Token); + string[] sent = Sent(slot); + if (!expected.SequenceEqual(sent)) + failures.Enqueue($"{database}: {string.Join(",", sent)}"); + } + }, + Token))); + + Assert.Empty(failures); + } + + // ---- a subclass's hook reads Params ---- + + [Fact] + public static void AHook_SeesTheSendList_AndParamsOutsideIsTheCallers() + { + var rig = new Rig(); + var command = new HookCommand( + rig, + new() + { + ["a"] = ["@order_id"], + ["b"] = ["@order_id", "@region"], + }); + command.AddParams(new Args()); + command.Callers = command.Params; + + rig.Use("a"); + command.ExecuteNonQuery(); + + // The dropped member is absent inside the hook, with the resolved names. + Assert.Equal(["@order_id"], command.Seen!); + Assert.False(command.SawTheCallersList); + + // Outside the hook it is what was added. + Assert.Same(command.Callers, command.Params); + Assert.Equal(["OrderId", "Region"], Added(command)); + + rig.Use("b"); + command.ExecuteNonQuery(); + Assert.Equal(["@order_id", "@region"], command.Seen!); + } + + [Fact] + public static void AHook_WithOnlyExplicitNames_SeesTheCallersList() + { + var rig = new Rig(); + var command = new HookCommand(rig, Twice()); + command.AddParam("@order_id", 5); + command.Callers = command.Params; + + Slot slot = rig.Use("a"); + command.ExecuteNonQuery(); + + // Nothing is derived, so there is no send list at all. + Assert.True(command.SawTheCallersList); + Assert.Equal(["@order_id"], Sent(slot)); + } + + static DeclaredCommand InnerCommand(Rig rig) + { + var inner = new DeclaredCommand(rig, Twice()); + int orderId = 50; + int customerId = 70; + inner + .AddParam(orderId) + .AddParam(customerId); + return inner; + } + + [Fact] + public static void AHookThatRunsAnotherCommand_DoesNotBreakTheOuterRun() + { + var outerRig = new Rig(); + var innerRig = new Rig(); + DeclaredCommand inner = InnerCommand(innerRig); + + var outer = new HookCommand(outerRig, Twice()); + int orderId = 5; + int customerId = 7; + outer + .AddParam(orderId) + .AddParam(customerId); + outer.Callers = outer.Params; + + Slot innerSlot = null!; + outer.Before = () => + { + innerSlot = innerRig.Use("b"); + inner.ExecuteNonQuery(); + }; + + Slot outerSlot = outerRig.Use("a"); + outer.ExecuteNonQuery(); + + Assert.Equal(["@p_order_id", "@p_customer_id"], Sent(innerSlot)); + Assert.Equal(["@order_id", "@customer_id"], Sent(outerSlot)); + Assert.Equal(["@order_id", "@customer_id"], outer.Seen!); + Assert.Equal( + [5, 7], + outerSlot.Command!.Sent.Select(static p => p.Value)); + Assert.Same(outer.Callers, outer.Params); + Assert.Equal(["orderId", "customerId"], Added(outer)); + } + + // ---- edge cases ---- + + [Fact] + public static void NoParameters_RunsWithNoLookup() + { + var rig = new Rig(); + var command = new DeclaredCommand(rig, Twice()); + + Slot slot = rig.Use("a"); + command.ExecuteNonQuery(); + + Assert.Empty(Sent(slot)); + Assert.Empty(command.Params); + Assert.Equal(0, command.Lookups); + } + + [Fact] + public static void ACallerNamedParameter_BeatsAnObjectMember_OnEveryRun() + { + var rig = new Rig(); + var command = new DeclaredCommand(rig, Twice()); + command + .AddParams(new { OrderId = 1 }) + .AddParam("@order_id", 9); + + Slot first = rig.Use("a"); + command.ExecuteNonQuery(); + Slot second = rig.Use("a"); + command.ExecuteNonQuery(); + + Assert.Equal(["@order_id"], Sent(first)); + Assert.Equal([9], first.Command!.Sent.Select(static p => p.Value)); + Assert.Equal(["@order_id"], Sent(second)); + Assert.Equal([9], second.Command!.Sent.Select(static p => p.Value)); + Assert.Equal(2, command.Params.Count); + } + + [Fact] + public static void ACollision_Throws_ThenTheNextRunIsFine() + { + var rig = new Rig(); + var command = new HookCommand(rig, Twice()); + int orderId = 5; + var other = new Args(); + command + .AddParam(orderId) + .AddParam(other.OrderId); + command.Callers = command.Params; + + rig.Use("a"); + var ex = Assert.Throws( + () => command.ExecuteNonQuery()); + Assert.Contains("Two parameters resolve to", ex.Message); + + // Nothing is left behind: Params is still the caller's, as added. + Assert.Same(command.Callers, command.Params); + Assert.Equal(["orderId", "OrderId"], Added(command)); + + command.Params.RemoveAt(1); + Slot slot = rig.Use("a"); + command.ExecuteNonQuery(); + + Assert.Equal(["@order_id"], Sent(slot)); + Assert.Equal(["@order_id"], command.Seen!); + } + + [Fact] + public static void StaleNames_AreReadAgain_AndTheRunSendsTheNewOnes() + { + var rig = new Rig(); + var declared = new Dictionary + { + ["a"] = ["@order_id"], + }; + var command = new DeclaredCommand(rig, declared); + int orderId = 5; + command.AddParam(orderId); + + Slot first = rig.Use("a"); + command.ExecuteNonQuery(); + Assert.Equal(["@order_id"], Sent(first)); + Assert.Equal(1, command.Lookups); + + // The procedure changed: it now takes @customer_id as well. + declared["a"] = ["@order_id", "@customer_id"]; + int customerId = 7; + command.AddParam(customerId); + + Slot second = rig.Use("a"); + command.ExecuteNonQuery(); + + Assert.Equal(["@order_id", "@customer_id"], Sent(second)); + Assert.Equal(2, command.Lookups); + Assert.Equal(["orderId", "customerId"], Added(command)); + } + + [Fact] + public static async Task StaleNames_AreReadAgain_Async() + { + var rig = new Rig(); + var declared = new Dictionary + { + ["a"] = ["@order_id"], + }; + var command = new DeclaredCommand(rig, declared); + int orderId = 5; + command.AddParam(orderId); + + rig.Use("a"); + await command.ExecuteNonQueryAsync(Token); + + declared["a"] = ["@order_id", "@customer_id"]; + int customerId = 7; + command.AddParam(customerId); + + Slot second = rig.Use("a"); + await command.ExecuteNonQueryAsync(Token); + + Assert.Equal(["@order_id", "@customer_id"], Sent(second)); + Assert.Equal(2, command.Lookups); + } + + [Fact] + public static void AThrowWhileSettingUp_DisposesTheCommand_AndResetsTheState() + { + var rig = new Rig(); + var command = new HookCommand(rig, Twice()); + int orderId = 5; + int customerId = 7; + command + .AddParam(orderId) + .AddParam(customerId); + command.Callers = command.Params; + + bool fail = true; + command.Before = () => + { + if (fail) throw new InvalidOperationException("setup failed"); + }; + + Slot failed = rig.Use("a"); + Assert.Throws( + () => command.ExecuteNonQuery()); + Assert.Equal(1, failed.Disposals); + Assert.Same(command.Callers, command.Params); + + fail = false; + Slot next = rig.Use("b"); + command.ExecuteNonQuery(); + + Assert.Equal(["@p_order_id", "@p_customer_id"], Sent(next)); + Assert.Same(command.Callers, command.Params); + Assert.Equal(["orderId", "customerId"], Added(command)); + } +} diff --git a/Tests/Open.Database.Extensions.Core.Tests/ParameterNameAddParamsTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ParameterNameAddParamsTests.cs index dea405c..5d95131 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ParameterNameAddParamsTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ParameterNameAddParamsTests.cs @@ -223,7 +223,7 @@ public static void TwoObjectsWithTheSameMember_Collide_AndTheMessageSaysSo() // ---- what a caller can see of the names that were sent ---- [Fact] - public static void BeforeATerminalTheNamesAreAsWritten_TheProviderCommandHasTheRealOnes_AndAfterwardsParamsHoldsThem() + public static void BeforeATerminalTheNamesAreAsWritten_TheProviderCommandHasTheRealOnes_AndAfterwardsParamsIsStillWhatWasAdded() { var probe = new ConnectionProbe(ConnectionState.Open); var command = new DerivedParameterNameTests.ProcCommand(probe.Connection, CommandType.StoredProcedure, "dbo.what_was_sent", "@company_serial_num"); @@ -240,7 +240,7 @@ public static void BeforeATerminalTheNamesAreAsWritten_TheProviderCommandHasTheR Assert.NotNull(seen); Assert.Equal(["@company_serial_num"], seen); - Assert.Equal(["@company_serial_num"], command.Params.Select(p => p.Name)); + Assert.Equal(["CompanySerialNum"], command.Params.Select(p => p.Name)); // a run does not rewrite it Assert.Equal([42], command.Params.Select(p => p.Value)); } diff --git a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/LocalDbFixture.cs b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/LocalDbFixture.cs index 5a2d77a..a6e51d3 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/LocalDbFixture.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/TestHelpers/LocalDbFixture.cs @@ -58,6 +58,13 @@ public static void SkipUnlessAvailable() Assert.Skip(reason); } + /// Fails the calling test when LocalDB cannot be reached, whatever the environment says: for a test that proves a defect only a real server shows, so it is never reported as skipped. + public static void RequireAvailable() + { + if (Unavailable.Value is { } reason) + Assert.Fail($"This test needs SQL Server LocalDB and never skips: {reason}"); + } + /// A new, closed connection to LocalDB's tempdb. The caller disposes it. public static SqlConnection Create() => new(ConnectionString); diff --git a/docsite/articles/expressive-commands.md b/docsite/articles/expressive-commands.md index e6fb3ef..e2699cb 100644 --- a/docsite/articles/expressive-commands.md +++ b/docsite/articles/expressive-commands.md @@ -88,7 +88,7 @@ A procedure that changes can show its old names for up to five minutes. Only one #### What was sent? -Before a terminal runs, `command.Params` lists what you added: a name you wrote as written, an argument-derived name as its last member (nothing has been matched yet), and the members of an `AddParams` object under their member names. After a terminal has run, `command.Params` holds the names that were sent. To see what reached the provider, give `Execute(command => ...)` a function that reads the provider's command: its `CommandText`, `CommandType` and `Parameters` (name and value of each) already have the real names. The library keeps no log of the statement and has no other hook. +`command.Params` always lists what you added: a name you wrote as written, an argument-derived name as its last member, and the members of an `AddParams` object under their member names. A run never changes it. The real names are worked out in every run, for the connection that run uses, so one command object can run against two databases that name the parameters of a procedure differently. To see what the run sent, give `Execute(command => ...)` a function that reads the provider's command: its `CommandText`, `CommandType` and `Parameters` (name and value of each) already have the real names. The library keeps no log of the statement and has no other hook. A derived name stays as written when no real names were read: a text command, another provider, a procedure name with a database part (`otherdb.dbo.proc`), a procedure the login cannot see or that does not exist, or one that declares no parameters. Name the parameters yourself when you call a procedure in another database.