Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions Source/Core/DbQueryRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,15 @@ static DbCommand Make(
/// connections come from.
/// </summary>
/// <remarks>
/// That is the factory or pool (not the adapter <c>AsPool</c> makes around
/// a factory), or the connection itself.
/// That is the factory or pool, or the connection itself for a
/// transaction. What a pool made around a factory stands for is found by
/// <see cref="ProcedureParameterNames"/>.
/// </remarks>
/// <param name="source">The connection, transaction, pool or factory.</param>
/// <param name="connection">The connection of the run.</param>
/// <returns>The owner of the remembered names.</returns>
static object NameCacheOwner(object source, DbConnection connection)
=> source switch
{
DbTransaction => connection,
IConnectionIdentity adapter => adapter.Identity,
_ => source,
};
=> source is DbTransaction ? connection : source;

/// <summary>
/// Makes the key a procedure's names are remembered under.
Expand Down
9 changes: 8 additions & 1 deletion Source/Core/ExpressiveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,17 @@ public ExpressiveCommand(
CommandType type,
string command,
IEnumerable<Param>? @params = null)
: base(connFactory.AsGeneric(), type, command, @params)
: base(CreateOf(connFactory), type, command, @params)
{
}

// The function is the factory's Create method, so what the factory has
// remembered (the names of its procedures) is shared by every command made
// from it. Guarding here keeps the parameter name the public constructor
// has always reported.
static Func<IDbConnection> CreateOf(IDbConnectionFactory connectionFactory)
=> (connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory))).Create;

/// <summary>
/// Initializes a new instance that invokes a function to create a new connection for each execution.
/// </summary>
Expand Down
25 changes: 5 additions & 20 deletions Source/Core/ExpressiveCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,8 @@ protected virtual ValueTask<ImmutableArray<string>> GetProcedureParameterNamesAs
if (Type == CommandType.StoredProcedure)
{
ProcedureKey key = KeyOf(connection);
return ProcedureParameterNames.Find(NameCacheOwner, key)
?? ProcedureParameterNames.Remember(NameCacheOwner, key, GetProcedureParameterNames(connection, Command));
return ProcedureParameterNames.Find(_poolOrConnection, key)
?? ProcedureParameterNames.Remember(_poolOrConnection, key, GetProcedureParameterNames(connection, Command));
}

// The @name markers found in the text.
Expand All @@ -880,7 +880,7 @@ protected virtual ValueTask<ImmutableArray<string>> GetProcedureParameterNamesAs
if (Type == CommandType.StoredProcedure)
{
ProcedureKey key = KeyOf(connection);
return ProcedureParameterNames.Find(NameCacheOwner, key) is { } kept
return ProcedureParameterNames.Find(_poolOrConnection, key) is { } kept
? new(kept)
: ReadNamesAsync(connection, key, cancellationToken);
}
Expand All @@ -897,7 +897,7 @@ protected virtual ValueTask<ImmutableArray<string>> GetProcedureParameterNamesAs
ProcedureKey key,
CancellationToken cancellationToken)
=> ProcedureParameterNames.Remember(
NameCacheOwner,
_poolOrConnection,
key,
await GetProcedureParameterNamesAsync(
connection,
Expand All @@ -922,7 +922,7 @@ await GetProcedureParameterNamesAsync(
if (Type == CommandType.StoredProcedure)
{
ProcedureKey key = KeyOf(connection);
return ProcedureParameterNames.Find(NameCacheOwner, key) is { } kept
return ProcedureParameterNames.Find(_poolOrConnection, key) is { } kept
? kept.NeedsMetadata
? NamesWithMetadataAsync(connection, kept, cancellationToken)
: new(kept)
Expand Down Expand Up @@ -1043,21 +1043,6 @@ TCommand PrepareDerived(TConnection connection, NameSet? names, bool written = f
}
}

/// <summary>
/// Gets the object that remembered names belong to: the object commands
/// take connections from.
/// </summary>
/// <remarks>
/// That is a factory (not the wrapper made around it for each command),
/// a pool, or the bare connection.
/// </remarks>
object NameCacheOwner => _poolOrConnection switch
{
DbConnectionFactoryExtensions.ConnectionFactoryToPoolAdapter<TConnection> typed => typed.Identity,
DbConnectionFactoryExtensions.ConnectionFactoryToPoolAdapter untyped => untyped.Identity,
_ => _poolOrConnection,
};

/// <summary>
/// Makes the key a procedure's names are remembered under.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Extensions/ConnectionProvider.Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ public static ExpressiveCommand Command(
public static ExpressiveCommand StoredProcedure(
this Func<IDbConnection> connectionSource,
string procedureName)
// DbConnectionFactory.Create throws ArgumentNullException for a null source.
=> StoredProcedure(DbConnectionFactory.Create(connectionSource), procedureName);
// AsPool throws ArgumentNullException for a null source.
=> StoredProcedure(connectionSource.AsPool(), procedureName);

/// <summary>
/// Creates an <see cref="ExpressiveDbCommand"/> that invokes a function to create a new connection for every execution.
Expand Down
8 changes: 7 additions & 1 deletion Source/Core/IDbConnectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ public TConnection Take()
/// (the target of its <c>Create</c> method), which is what remembered procedure names belong to.
/// </summary>
/// <remarks>
/// A function with no target (a <see langword="static"/> lambda) has no such identity, and its own delegate is returned.
/// <para>
/// A function is identified by the object its method belongs to. A method of an object, and a lambda that captures only <see langword="this"/>, have that object. A lambda that captures nothing belongs to a class the compiler makes for the enclosing class, so such lambdas of one class are one source. A lambda that captures a local variable or a parameter has a new closure for each time it is made, so a function made again for each command is a new source each time.
/// A function with no target at all (a method group of a <see langword="static"/> method) has no such identity, and its own delegate is returned; whether converting the method again gives the same delegate is up to the compiler.
/// </para>
/// <para>
/// What is remembered for this identity lives as long as the object does (a singleton factory: the life of the process), at most 1024 procedures.
/// </para>
/// </remarks>
public object Identity => _factory.Target ?? _factory;

Expand Down
8 changes: 7 additions & 1 deletion Source/Core/IDbConnectionPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static class ConnectionPoolExtensions
/// </summary>
/// <param name="source">The pool to forward to.</param>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
class GenericPool(IDbConnectionPool source) : IDbConnectionPool<IDbConnection>
class GenericPool(IDbConnectionPool source) : IDbConnectionPool<IDbConnection>, IConnectionIdentity
{
private readonly IDbConnectionPool _source
= source ?? throw new ArgumentNullException(nameof(source));
Expand All @@ -83,6 +83,12 @@ public IDbConnection Take()
/// <inheritdoc />
public void Give(IDbConnection connection)
=> _source.Give(connection);

// What remembered names belong to is the pool this one forwards to (or
// what that one stands for), not this wrapper, which is made again for
// each command.
object IConnectionIdentity.Identity
=> _source is IConnectionIdentity identity ? identity.Identity : _source;
}

/// <summary>
Expand Down
16 changes: 14 additions & 2 deletions Source/Core/ProcedureParameterNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,30 @@ public sealed class Entries
/// <param name="key">The server, database and procedure.</param>
/// <returns>The set, or <see langword="null"/> when none is kept.</returns>
public static NameSet? Find(object source, ProcedureKey key)
=> Sources.TryGetValue(source, out Entries? entries) && entries.ByProcedure.TryGetValue(key, out NameSet? names)
=> Sources.TryGetValue(OwnerOf(source), out Entries? entries) && entries.ByProcedure.TryGetValue(key, out NameSet? names)
? names
: null;

/// <summary>
/// Gets what the names of a source belong to: the factory a pool made from
/// it stands for, else the source itself.
/// </summary>
/// <param name="source">The object commands take their connections from.</param>
/// <returns>The owner of the remembered names.</returns>
// A pool made around a factory or a function (AsPool, AsGeneric) is made
// again for each command; its identity is what it stands for, so every
// command of one factory shares one set. One type check on each lookup.
static object OwnerOf(object source)
=> source is IConnectionIdentity identity ? identity.Identity : source;

/// <summary>Keeps what the lookup answered.</summary>
/// <param name="source">The object commands take their connections from.</param>
/// <param name="key">The server, database and procedure the names belong to.</param>
/// <param name="names">The names as the database reported them; empty or the default array when it reported none.</param>
/// <returns>The set kept for the procedure: this one, or the one another call kept first.</returns>
public static NameSet Remember(object source, ProcedureKey key, ImmutableArray<string> names)
{
Entries entries = Sources.GetValue(source, static _ => new Entries());
Entries entries = Sources.GetValue(OwnerOf(source), static _ => new Entries());
// Only here, where a lookup was just paid for, is the bound checked.
if (entries.ByProcedure.Count >= MaxProcedures && !entries.ByProcedure.ContainsKey(key)) entries.ByProcedure.Clear();
return entries.ByProcedure.GetOrAdd(key, new NameSet(names.IsDefault ? [] : names, entries.Weak, key));
Expand Down
6 changes: 4 additions & 2 deletions Source/MSSqlClient/Extensions/ConnectionProvider.Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ public static ExpressiveSqlCommand Command(
this Func<SqlConnection> connectionSource,
string command,
CommandType type = CommandType.Text)
=> Command(DbConnectionFactory.Create(connectionSource), command, type);
// AsPool throws ArgumentNullException for a null source.
=> Command(connectionSource.AsPool(), command, type);

/// <summary>
/// Creates an <see cref="ExpressiveSqlCommand"/> that calls a stored procedure, invoking a function to create a new connection for every execution.
Expand All @@ -142,5 +143,6 @@ public static ExpressiveSqlCommand Command(
public static ExpressiveSqlCommand StoredProcedure(
this Func<SqlConnection> connectionSource,
string procedureName)
=> StoredProcedure(DbConnectionFactory.Create(connectionSource), procedureName);
// AsPool throws ArgumentNullException for a null source.
=> StoredProcedure(connectionSource.AsPool(), procedureName);
}
6 changes: 4 additions & 2 deletions Source/SqlClient/Extensions/ConnectionProvider.Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public static ExpressiveSqlCommand Command(
this Func<SqlConnection> connectionSource,
string command,
CommandType type = CommandType.Text)
=> Command(DbConnectionFactory.Create(connectionSource), command, type);
// AsPool throws ArgumentNullException for a null source.
=> Command(connectionSource.AsPool(), command, type);

/// <summary>
/// Creates an <see cref="ExpressiveSqlCommand"/> that calls a stored procedure, that invokes a function to create a new connection for every execution.
Expand All @@ -142,5 +143,6 @@ public static ExpressiveSqlCommand Command(
public static ExpressiveSqlCommand StoredProcedure(
this Func<SqlConnection> connectionSource,
string procedureName)
=> StoredProcedure(DbConnectionFactory.Create(connectionSource), procedureName);
// AsPool throws ArgumentNullException for a null source.
=> StoredProcedure(connectionSource.AsPool(), procedureName);
}
Loading