diff --git a/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs b/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs index 8c82c486..7b87e2a1 100644 --- a/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs +++ b/Benchmarks/Open.Database.Extensions.Benchmarks/TextNameScanBenchmarks.cs @@ -56,10 +56,21 @@ 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. + 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)), + names).Compile(); } static Func BuildMatchIn() @@ -69,7 +80,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(); @@ -149,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/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); } /// 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) diff --git a/Source/Core/Core/DbConnectionProvider.cs b/Source/Core/Core/DbConnectionProvider.cs index 0530f820..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("Concurrent use of a single connection is not supported."); + { + throw new InvalidOperationException( + DbConnectionProvider.ConcurrentUseMessage); + } _takenConnectionState = Connection.State; return Connection; @@ -65,6 +68,16 @@ 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..6e2ae34d 100644 --- a/Source/Core/DbQuery.cs +++ b/Source/Core/DbQuery.cs @@ -186,11 +186,13 @@ 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..2fd8fe80 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,10 @@ TResult ExecuteDirect(TConnection single, TState state, Func 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) diff --git a/Tests/Open.Database.Extensions.Core.Tests/ConnectionSourceScopeTests.cs b/Tests/Open.Database.Extensions.Core.Tests/ConnectionSourceScopeTests.cs index f1c976e2..22d44906 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/ConnectionSourceScopeTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/ConnectionSourceScopeTests.cs @@ -295,6 +295,23 @@ public static async Task APoolThatRestoresTheState_ClosesWhatTheScopeOpened_AndL Assert.Empty(open.Log); } + [Fact] + public static void APoolOfOneConnection_TakenTwice_SaysWhatIsWrongAndTheFix() + { + var pool = Open.Database.Extensions.Core.DbConnectionProvider.Create( + new ConnectionScopeTests.ScopeConnection()); + _ = 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.", + 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..016f49e3 100644 --- a/Tests/Open.Database.Extensions.Core.Tests/DbQueryChecksTests.cs +++ b/Tests/Open.Database.Extensions.Core.Tests/DbQueryChecksTests.cs @@ -12,8 +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."; - 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 +123,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)); } }