From 0b9c124a43e8374ee3ecdf8e7472feb91877b2b3 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:08:03 -0800 Subject: [PATCH 1/6] Remove the capture arm method that only repeated TryArm without its result The capture source interface and its one implementation had both TryArm and Arm, which did the same compare-exchange; Arm only dropped the result. The claim of a capture set now calls TryArm for the captures after the first and discards the result. Same compare-exchange per claim; one interface member and one method fewer. Internal types only, no public API change. Co-Authored-By: Claude Sonnet 5.5 --- Source/Core/Capture.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Source/Core/Capture.cs b/Source/Core/Capture.cs index aad4fe7e..163695a6 100644 --- a/Source/Core/Capture.cs +++ b/Source/Core/Capture.cs @@ -80,9 +80,6 @@ interface ICaptureSource /// if it was armed before. bool TryArm(); - /// Arms the capture, if it is not armed yet. - void Arm(); - /// Marks the capture as belonging to a stream whose enumerator is /// open. void BeginStream(); @@ -149,9 +146,6 @@ public bool TryArm() => Interlocked.CompareExchange(ref _state, Armed, NotStarted) == NotStarted; - public void Arm() - => Interlocked.CompareExchange(ref _state, Armed, NotStarted); - public void BeginStream() => Volatile.Write(ref _state, StreamOpen); @@ -741,7 +735,7 @@ public void Claim() if (Interlocked.Exchange(ref _claimed, 1) != 0 || !sources[0].TryArm()) throw new InvalidOperationException(CaptureMessages.SecondRun(set)); - for (int i = 1; i < sources.Length; i++) sources[i].Arm(); + for (int i = 1; i < sources.Length; i++) _ = sources[i].TryArm(); } public override CaptureRun Start(CancellationToken cancellationToken) From 22ddbf8d4eb32bb25c07c56643184c052b8912aa Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:08:05 -0800 Subject: [PATCH 2/6] Give the options of the definition's query core a default instead of an overload The two-argument QueryCore for a call made from a definition only forwarded to the three-argument one with no options. The three-argument one now takes options with a default of none, and the forwarding overload is gone. Every caller passes a call, so overload resolution is unchanged. Compile time only; no run-time cost. Internal type, no public API change. Co-Authored-By: Claude Sonnet 5.5 --- Source/Core/MappedViews.cs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/Source/Core/MappedViews.cs b/Source/Core/MappedViews.cs index 106f40d7..24f188aa 100644 --- a/Source/Core/MappedViews.cs +++ b/Source/Core/MappedViews.cs @@ -303,31 +303,20 @@ public static MappedQueryCore QueryCore( ? FromCommand(single.Command, source, true) : FromCommand(command, source, false); - /// - /// Makes the query core of a call made from a definition, which the query holds as it is. - /// - /// The type each row is mapped to. - /// The call. - /// Reads and maps one row. - /// The new query core. - public static MappedQueryCore QueryCore( - DbQueryCall call, - IRowSource source) - => NewCore(null, call, source, QueryOptions.None); - /// /// Makes the query core of a call made from a - /// definition, with the options the definition holds. + /// definition, which the query holds as it is, with the options the + /// definition holds. /// /// The type each row is mapped to. /// The call. /// Reads and maps one row. - /// The switches of the definition. + /// The switches of the definition; none by default. /// The new query core. public static MappedQueryCore QueryCore( DbQueryCall call, IRowSource source, - QueryOptions options) + QueryOptions options = QueryOptions.None) => NewCore(null, call, source, options); static QueryOptions Single(bool singleResult) From 9a5a1d2aa9871c86de3df35257314eef0c1e4a53 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:08:06 -0800 Subject: [PATCH 3/6] Drop the local that held a bound call before returning it with a capture The shared helper behind the capture methods of a bound call built the call into a local, set the out parameter, and returned the local. It now sets the out parameter and returns the call directly. The call is built the same way; nothing is allocated or read differently. Public signatures unchanged. Co-Authored-By: Claude Sonnet 5.5 --- Source/Core/BoundCall.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Source/Core/BoundCall.cs b/Source/Core/BoundCall.cs index a7f62eb5..a9a429d7 100644 --- a/Source/Core/BoundCall.cs +++ b/Source/Core/BoundCall.cs @@ -394,9 +394,8 @@ static BoundCall WithCapture( CaptureSource source, out DbCapture capture) { - BoundCall captured = WithCapture(call, source); capture = new(source); - return captured; + return WithCapture(call, source); } /// From 83e8f9fb7312b8f5bd70a62387facf9eda75092a Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:09:12 -0800 Subject: [PATCH 4/6] Make the text name scan benchmark build its name set and its match again The benchmark reaches the internal name set through expressions. It looked for a name set constructor with one or two parameters, but the constructor takes three (the names, where a procedure's set is kept, and its key), and the match call left out the third argument (the subject of an ambiguity message). Both expressions threw when the benchmark's setup ran. The constructor call now fills every parameter after the names with its default, and the match call passes a null subject. A dry run of the benchmark class (the Dry job, in process, all eight parameter combinations, five benchmarks each) now completes all 40 runs, which includes the setup check that every path finds the same names. No timed numbers were taken. Benchmark project only; no library change. Co-Authored-By: Claude Sonnet 5.5 --- .../TextNameScanBenchmarks.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs b/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs index 8c82c486..0c240bc1 100644 --- a/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs @@ -56,10 +56,12 @@ static Func, object> BuildMakeSet() { ParameterExpression names = Expression.Parameter(typeof(ImmutableArray)); ConstructorInfo constructor = NameSet.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0]; - NewExpression made = constructor.GetParameters().Length == 1 - ? Expression.New(constructor, names) - : Expression.New(constructor, names, Expression.Constant(false)); - return Expression.Lambda, object>>(Expression.Convert(made, typeof(object)), names).Compile(); + + // The parameters after the names (where a procedure's set is kept, and its key) are left at their defaults. + Expression[] arguments = [names, .. constructor.GetParameters().Skip(1).Select(p => Expression.Default(p.ParameterType))]; + return Expression.Lambda, object>>( + Expression.Convert(Expression.New(constructor, arguments), typeof(object)), + names).Compile(); } static Func BuildMatchIn() @@ -69,7 +71,9 @@ static Func, object> BuildMakeSet() ParameterExpression procedure = Expression.Parameter(typeof(string)); MethodInfo match = NameSet.GetMethod("Match", BindingFlags.Instance | BindingFlags.Public)!; return Expression.Lambda>( - Expression.Call(Expression.Convert(set, NameSet), match, expression, procedure), + Expression.Call( + Expression.Convert(set, NameSet), match, + expression, procedure, Expression.Constant(null, typeof(string))), set, expression, procedure).Compile(); From a4c95a43e00ae2dda40f52cfef02e1b49033a045 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:11:07 -0800 Subject: [PATCH 5/6] Give the blank command and concurrent connection messages one wording each Two exception messages said what was wrong but not what to do, while the same condition raised elsewhere in Core already said the fix. A blank command text from a query definition now says "Pass the SQL text or the name of the stored procedure.", as the command constructors do. Both now go through one check (DbQuery.Checked, which the command's own check calls), so the wording and the null and blank behavior live in one place. Taking a single connection that is still in use from the connection provider now says to wait for the running call to finish, or to create the command from a connection factory or pool. The provider and the command's own guard use one constant for the text. The test constants that pinned the old definition text now pin the one wording (the two constants for the same fact became one), and a new test pins the provider's message. Only the text of two exceptions changes; no run-time cost, no public API change. Co-Authored-By: Claude Sonnet 5.5 --- Source/Core/Core/DbConnectionProvider.cs | 8 +++++++- Source/Core/DbQuery.cs | 5 +++-- Source/Core/ExpressiveCommandBase.cs | 10 ++-------- .../ConnectionSourceScopeTests.cs | 13 +++++++++++++ .../DbQueryChecksTests.cs | 5 ++--- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Source/Core/Core/DbConnectionProvider.cs b/Source/Core/Core/DbConnectionProvider.cs index 0530f820..0db942ea 100644 --- a/Source/Core/Core/DbConnectionProvider.cs +++ b/Source/Core/Core/DbConnectionProvider.cs @@ -26,7 +26,7 @@ internal class DbConnectionProvider(TConnection connection) public TConnection Take() { if (_takenConnectionState.HasValue) - throw new InvalidOperationException("Concurrent use of a single connection is not supported."); + throw new InvalidOperationException(DbConnectionProvider.ConcurrentUseMessage); _takenConnectionState = Connection.State; return Connection; @@ -65,6 +65,12 @@ public void Give(IDbConnection connection) /// internal static class DbConnectionProvider { + /// + /// The message of the exception for a connection that is taken while it is + /// in use, whoever guards it (this provider, or a command's own guard). + /// + public const string ConcurrentUseMessage = "Concurrent use of a single connection is not supported. Wait for the running call to finish, or create the command from a connection factory or pool so each call gets its own connection."; + /// /// Creates a provider that hands out . /// diff --git a/Source/Core/DbQuery.cs b/Source/Core/DbQuery.cs index e747c402..1b4ba480 100644 --- a/Source/Core/DbQuery.cs +++ b/Source/Core/DbQuery.cs @@ -186,11 +186,12 @@ public static DbQuery Procedure(string name) -1, null); - static string Checked(string text, string paramName) + // The one check of a command text (a definition's, and a command's). + internal static string Checked(string text, string paramName) => DbQueryCall.CheckedText( text, paramName, - "The command cannot be empty or white space."); + "The command cannot be empty or white space. Pass the SQL text or the name of the stored procedure."); /// /// Returns a new definition with the command timeout set; diff --git a/Source/Core/ExpressiveCommandBase.cs b/Source/Core/ExpressiveCommandBase.cs index e9d31a4c..b3d3e1c5 100644 --- a/Source/Core/ExpressiveCommandBase.cs +++ b/Source/Core/ExpressiveCommandBase.cs @@ -107,13 +107,7 @@ private protected sealed class CommandOptions private protected CommandOptions EnsureOptions() => _options ??= new(); static string CheckedCommand(string command) - { - if (command is null) throw new ArgumentNullException(nameof(command)); - if (string.IsNullOrWhiteSpace(command)) throw new ArgumentException("The command cannot be empty or white space. Pass the SQL text or the name of the stored procedure.", nameof(command)); - Contract.EndContractBlock(); - - return command; - } + => DbQuery.Checked(command, nameof(command)); /// /// Gets the transaction the command runs in, or @@ -1510,7 +1504,7 @@ TResult ExecuteDirect(TConnection single, TState state, Func(pool.Take); + Assert.Equal( + "Concurrent use of a single connection is not supported. Wait for the running call to finish, or create the command from a connection factory or pool so each call gets its own connection.", + ex.Message); + } + [Theory] [InlineData(false)] [InlineData(true)] diff --git a/Tests/Open.Database.Extensions.Core.Tests/DbQueryChecksTests.cs b/Tests/Open.Database.Extensions.Core.Tests/DbQueryChecksTests.cs index 8463050d..c8049c3b 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/DbQueryChecksTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/DbQueryChecksTests.cs @@ -12,8 +12,7 @@ namespace Open.Database.Extensions.Tests; public static class DbQueryChecksTests { const string BlankName = "Parameter names cannot be empty or white space."; - const string BlankCommand = "The command cannot be empty or white space."; - const string BlankCommandOfACommand = "The command cannot be empty or white space. Pass the SQL text or the name of the stored procedure."; + const string BlankCommand = "The command cannot be empty or white space. Pass the SQL text or the name of the stored procedure."; static CancellationToken Token => TestContext.Current.CancellationToken; @@ -122,7 +121,7 @@ public static void ACommandText_Blank_IsRefused_ByEveryConstructorWithItsMessage { ArgumentException ex = Assert.Throws(make); Assert.Equal("command", ex.ParamName); - Assert.Equal(BlankCommandOfACommand, Text(ex)); + Assert.Equal(BlankCommand, Text(ex)); } } From c20b635e29c584203dd95f663c68addaa9b21318 Mon Sep 17 00:00:00 2001 From: electricessence <5899455+electricessence@users.noreply.github.com> Date: Thu, 3 Dec 2026 13:23:05 -0800 Subject: [PATCH 6/6] Wrap the long lines of the message and benchmark changes to the layout rules Layout only; no behavior change. The shared connection-use message and the blank command message are split into joined literals at phrase breaks (same text), the throws that use them are wrapped and braced, and the tests that pin them split their literals the same way while still asserting the full text. The benchmark's constructor-argument chain now follows the one call per line rule from the second call on, its comments are wrapped, and a comment on Run that repeated the header is removed. The options doc line of the query core is wrapped. Co-Authored-By: Claude Sonnet 5.5 --- .../TextNameScanBenchmarks.cs | 16 ++++++++++++---- Source/Core/Core/DbConnectionProvider.cs | 11 +++++++++-- Source/Core/DbQuery.cs | 3 ++- Source/Core/ExpressiveCommandBase.cs | 5 ++++- Source/Core/MappedViews.cs | 4 +++- .../ConnectionSourceScopeTests.cs | 8 ++++++-- .../DbQueryChecksTests.cs | 4 +++- 7 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs b/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs index 0c240bc1..7b87e2a1 100644 --- a/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs @@ -57,10 +57,19 @@ static Func, object> BuildMakeSet() ParameterExpression names = Expression.Parameter(typeof(ImmutableArray)); ConstructorInfo constructor = NameSet.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0]; - // The parameters after the names (where a procedure's set is kept, and its key) are left at their defaults. - Expression[] arguments = [names, .. constructor.GetParameters().Skip(1).Select(p => Expression.Default(p.ParameterType))]; + // The parameters after the names (where a procedure's + // set is kept, and its key) are left at their defaults. + ParameterInfo[] parameters = constructor.GetParameters(); + Expression[] arguments = + [ + names, + .. parameters.Skip(1) + .Select(p => Expression.Default(p.ParameterType)), + ]; return Expression.Lambda, object>>( - Expression.Convert(Expression.New(constructor, arguments), typeof(object)), + Expression.Convert( + Expression.New(constructor, arguments), + typeof(object)), names).Compile(); } @@ -153,7 +162,6 @@ public void Setup() } } - // One thread runs the calls directly; more run one iteration each of a Parallel.For, every iteration making the same number of calls. int Run(Func calls) { if (Threads == 1) return calls(); diff --git a/Source/Core/Core/DbConnectionProvider.cs b/Source/Core/Core/DbConnectionProvider.cs index 0db942ea..3c2faa11 100644 --- a/Source/Core/Core/DbConnectionProvider.cs +++ b/Source/Core/Core/DbConnectionProvider.cs @@ -26,7 +26,10 @@ internal class DbConnectionProvider(TConnection connection) public TConnection Take() { if (_takenConnectionState.HasValue) - throw new InvalidOperationException(DbConnectionProvider.ConcurrentUseMessage); + { + throw new InvalidOperationException( + DbConnectionProvider.ConcurrentUseMessage); + } _takenConnectionState = Connection.State; return Connection; @@ -69,7 +72,11 @@ internal static class DbConnectionProvider /// The message of the exception for a connection that is taken while it is /// in use, whoever guards it (this provider, or a command's own guard). /// - public const string ConcurrentUseMessage = "Concurrent use of a single connection is not supported. Wait for the running call to finish, or create the command from a connection factory or pool so each call gets its own connection."; + public const string ConcurrentUseMessage = + "Concurrent use of a single connection is not supported. " + + "Wait for the running call to finish, " + + "or create the command from a connection factory or pool " + + "so each call gets its own connection."; /// /// Creates a provider that hands out . diff --git a/Source/Core/DbQuery.cs b/Source/Core/DbQuery.cs index 1b4ba480..6e2ae34d 100644 --- a/Source/Core/DbQuery.cs +++ b/Source/Core/DbQuery.cs @@ -191,7 +191,8 @@ internal static string Checked(string text, string paramName) => DbQueryCall.CheckedText( text, paramName, - "The command cannot be empty or white space. Pass the SQL text or the name of the stored procedure."); + "The command cannot be empty or white space. " + + "Pass the SQL text or the name of the stored procedure."); /// /// Returns a new definition with the command timeout set; diff --git a/Source/Core/ExpressiveCommandBase.cs b/Source/Core/ExpressiveCommandBase.cs index b3d3e1c5..2fd8fe80 100644 --- a/Source/Core/ExpressiveCommandBase.cs +++ b/Source/Core/ExpressiveCommandBase.cs @@ -1504,7 +1504,10 @@ TResult ExecuteDirect(TConnection single, TState state, Func QueryCore( /// The type each row is mapped to. /// The call. /// Reads and maps one row. - /// The switches of the definition; none by default. + /// + /// The switches of the definition; none by default. + /// /// The new query core. public static MappedQueryCore QueryCore( DbQueryCall call, diff --git a/Tests/Open.Database.Extensions.Core.Tests/ConnectionSourceScopeTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ConnectionSourceScopeTests.cs index cef1a9f4..22d44906 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ConnectionSourceScopeTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ConnectionSourceScopeTests.cs @@ -302,9 +302,13 @@ public static void APoolOfOneConnection_TakenTwice_SaysWhatIsWrongAndTheFix() new ConnectionScopeTests.ScopeConnection()); _ = pool.Take(); - InvalidOperationException ex = Assert.Throws(pool.Take); + InvalidOperationException ex + = Assert.Throws(pool.Take); Assert.Equal( - "Concurrent use of a single connection is not supported. Wait for the running call to finish, or create the command from a connection factory or pool so each call gets its own connection.", + "Concurrent use of a single connection is not supported. " + + "Wait for the running call to finish, " + + "or create the command from a connection factory or pool " + + "so each call gets its own connection.", ex.Message); } diff --git a/Tests/Open.Database.Extensions.Core.Tests/DbQueryChecksTests.cs b/Tests/Open.Database.Extensions.Core.Tests/DbQueryChecksTests.cs index c8049c3b..016f49e3 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/DbQueryChecksTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/DbQueryChecksTests.cs @@ -12,7 +12,9 @@ namespace Open.Database.Extensions.Tests; public static class DbQueryChecksTests { const string BlankName = "Parameter names cannot be empty or white space."; - const string BlankCommand = "The command cannot be empty or white space. Pass the SQL text or the name of the stored procedure."; + const string BlankCommand = + "The command cannot be empty or white space. " + + "Pass the SQL text or the name of the stored procedure."; static CancellationToken Token => TestContext.Current.CancellationToken;