Skip to content
Merged
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
7 changes: 4 additions & 3 deletions Source/Core/DbQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ internal DbQuery WithOutput(OutputLayout layout)
/// is made; <see langword="null"/> for a procedure.
/// </summary>
// A definition is a static field, so a call that derives names scans
// nothing. A procedure's names are remembered per source
// (ProcedureParameterNames). A set with no names means "not known": each
// argument is sent as written.
// nothing. A procedure's names are remembered per server and
// database when its name has a schema and the connection reports
// both, else per source (ProcedureParameterNames). A set with no
// names means "not known": each argument is sent as written.
internal NameSet? TextNames { get; }

/// <summary>
Expand Down
24 changes: 16 additions & 8 deletions Source/Core/DbQueryRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,22 @@ static object NameCacheOwner(object source, DbConnection connection)
/// <returns>The key.</returns>
// The server and the database are part of the key: one connection can be
// switched to another database, and one factory can hand out connections to
// different servers.
// different servers. So is the kind of provider: two kinds can report the same
// server and database.
static ProcedureKey KeyOf(DbConnection connection, DbQuery definition)
=> new(connection.DataSource, connection.Database, definition.CommandText);
=> new(
connection.DataSource, connection.Database,
definition.CommandText, connection.GetType());

/// <summary>
/// Finds the real names that the derived parameters are matched to.
/// </summary>
/// <remarks>
/// A text definition has the <c>@name</c> markers it found when it was made;
/// a procedure's names are read once per source and kept until a call that used
/// them cannot be built or the database rejects it.
/// a procedure's names are read once and kept until a call that used them
/// cannot be built or the database rejects it: once per server and database
/// for a schema-qualified name on a connection that reports both, else once
/// per source.
/// </remarks>
/// <param name="definition">The query definition.</param>
/// <param name="source">The connection, transaction, pool or factory.</param>
Expand All @@ -319,8 +324,11 @@ static ProcedureKey KeyOf(DbConnection connection, DbQuery definition)

object owner = NameCacheOwner(source, connection);
ProcedureKey key = KeyOf(connection, definition);
return ProcedureParameterNames.Find(owner, key)
?? ProcedureParameterNames.Remember(owner, key, ProcedureSchemaNames.Get(connection, definition.CommandText));
return ProcedureParameterNames.FindShared(owner, key)
?? ProcedureParameterNames.RememberShared(
owner,
key,
ProcedureSchemaNames.Get(connection, definition.CommandText));
}

/// <summary>
Expand All @@ -346,7 +354,7 @@ static ProcedureKey KeyOf(DbConnection connection, DbQuery definition)

object owner = NameCacheOwner(source, connection);
ProcedureKey key = KeyOf(connection, definition);
return ProcedureParameterNames.Find(owner, key) is { } kept
return ProcedureParameterNames.FindShared(owner, key) is { } kept
? new(kept)
: ReadNamesAsync(owner, key, connection, definition, cancellationToken);
}
Expand All @@ -357,7 +365,7 @@ static ProcedureKey KeyOf(DbConnection connection, DbQuery definition)
DbConnection connection,
DbQuery definition,
CancellationToken cancellationToken)
=> ProcedureParameterNames.Remember(
=> ProcedureParameterNames.RememberShared(
owner,
key,
await ProcedureSchemaNames
Expand Down
57 changes: 45 additions & 12 deletions Source/Core/ExpressiveCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,12 @@ public TThis AddParam(string name, string? value)
/// when the names are known, what the procedure declares.
/// </para>
/// <para>
/// A stored procedure's names are read once per connection factory, pool or connection, and again after a call that cannot be built (other than by
/// cancellation) or that the database rejects.
/// A stored procedure's names are read once per server and database when its name has a
/// schema (<c>dbo.get_author</c>), the connection is a <see cref="DbConnection"/> that
/// reports its server and its database, and this is one of the library's own command
/// types; otherwise once per connection factory, pool or connection. They are read again
/// after a call that cannot be built (other than by cancellation) or that the database
/// rejects.
/// </para>
/// <para>
/// Where the real names are not available (a text command with no
Expand Down Expand Up @@ -853,8 +857,8 @@ protected virtual ValueTask<ImmutableArray<string>> GetProcedureParameterNamesAs
if (Type == CommandType.StoredProcedure)
{
ProcedureKey key = KeyOf(connection);
return ProcedureParameterNames.Find(_poolOrConnection, key)
?? ProcedureParameterNames.Remember(_poolOrConnection, key, GetProcedureParameterNames(connection, Command));
return FindNames(key)
?? RememberNames(key, GetProcedureParameterNames(connection, Command));
}

// The @name markers found in the text.
Expand All @@ -880,7 +884,7 @@ protected virtual ValueTask<ImmutableArray<string>> GetProcedureParameterNamesAs
if (Type == CommandType.StoredProcedure)
{
ProcedureKey key = KeyOf(connection);
return ProcedureParameterNames.Find(_poolOrConnection, key) is { } kept
return FindNames(key) is { } kept
? new(kept)
: ReadNamesAsync(connection, key, cancellationToken);
}
Expand All @@ -896,8 +900,7 @@ protected virtual ValueTask<ImmutableArray<string>> GetProcedureParameterNamesAs
TConnection connection,
ProcedureKey key,
CancellationToken cancellationToken)
=> ProcedureParameterNames.Remember(
_poolOrConnection,
=> RememberNames(
key,
await GetProcedureParameterNamesAsync(
connection,
Expand All @@ -922,7 +925,7 @@ await GetProcedureParameterNamesAsync(
if (Type == CommandType.StoredProcedure)
{
ProcedureKey key = KeyOf(connection);
return ProcedureParameterNames.Find(_poolOrConnection, key) is { } kept
return FindNames(key) is { } kept
? kept.NeedsMetadata
? NamesWithMetadataAsync(connection, kept, cancellationToken)
: new(kept)
Expand Down Expand Up @@ -1052,12 +1055,42 @@ TCommand PrepareDerived(TConnection connection, NameSet? names, bool written = f
// switched to another database, and one factory can hand out connections to
// different servers, where the same procedure name may be a different
// procedure. IDbConnection has no server name, so a connection that is not
// a DbConnection contributes none. The key is a struct of the three parts,
// so no string is built.
// a DbConnection contributes none. The kind of provider (the connection's
// runtime type) is part of it too: two kinds can report the same server and
// database. The key is a struct of the four parts, so no string is built.
ProcedureKey KeyOf(TConnection connection)
=> new(
(connection as DbConnection)?.DataSource,
connection.Database, Command);
(connection as DbConnection)?.DataSource, connection.Database,
Command, connection.GetType());

/// <summary>
/// Finds the names kept for a procedure: for a command of the library's own
/// type, the ones shared by server and database first, then the source's;
/// for a command type of the user's own, the ones it kept for the source.
/// </summary>
/// <param name="key">The key made by <see cref="KeyOf"/>.</param>
/// <returns>The set, or <see langword="null"/> when none is kept.</returns>
// A command type of the user's own may answer names that are its own (it
// overrides how they are read), so what it keeps lives in a table of its
// own: never given to the library's commands, nor theirs to it. Per call:
// one virtual type test.
NameSet? FindNames(ProcedureKey key)
=> IsLibraryCommand
? ProcedureParameterNames.FindShared(_poolOrConnection, key)
: ProcedureParameterNames.FindUser(_poolOrConnection, key);

/// <summary>
/// Keeps the names a lookup answered, where <see cref="FindNames"/> finds them.
/// </summary>
/// <param name="key">The key made by <see cref="KeyOf"/>.</param>
/// <param name="names">
/// The names the provider reported; none when it reported none.
/// </param>
/// <returns>The set kept for the procedure.</returns>
NameSet RememberNames(ProcedureKey key, ImmutableArray<string> names)
=> IsLibraryCommand
? ProcedureParameterNames.RememberShared(_poolOrConnection, key, names)
: ProcedureParameterNames.RememberUser(_poolOrConnection, key, names);

/// <summary>
/// Builds, in <paramref name="send"/>, what one run of a command with written names sends: each name that the text declares
Expand Down
Loading