Skip to content

Commit b58c4a3

Browse files
FIx comments
1 parent d3af31e commit b58c4a3

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

src/MySqlConnector/Core/CachedProcedure.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ internal sealed class CachedProcedure
1212
{
1313
public static async Task<CachedProcedure?> FillAsync(IOBehavior ioBehavior, MySqlConnection connection, string schema, string component, ILogger logger, CancellationToken cancellationToken)
1414
{
15-
if (!connection.Session.UseProcedureCache)
16-
return null;
17-
1815
// try to use mysql.proc first, as it is much faster
1916
if (!connection.Session.ServerVersion.IsMariaDb &&
2017
connection.Session.ServerVersion.Version < ServerVersions.RemovesMySqlProcTable &&

src/MySqlConnector/Core/CommandExecutor.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@ public static async ValueTask<MySqlDataReader> ExecuteReaderAsync(CommandListPos
2020

2121
Log.CommandExecutorExecuteReader(command.Logger, connection.Session.Id, ioBehavior, commandListPosition.CommandCount);
2222

23-
Dictionary<string, CachedProcedure?>? cachedProcedures = null;
23+
var cachedProcedures = new Dictionary<string, CachedProcedure?>();
2424
for (var commandIndex = 0; commandIndex < commandListPosition.CommandCount; commandIndex++)
2525
{
2626
var command2 = commandListPosition.CommandAt(commandIndex);
27-
if (command2.CommandType == CommandType.StoredProcedure)
27+
if (command2.CommandType == CommandType.StoredProcedure && connection.Session.UseProcedureCache)
2828
{
29-
cachedProcedures ??= [];
3029
var commandText = command2.CommandText!;
3130
if (!cachedProcedures.ContainsKey(commandText))
3231
{
33-
cachedProcedures.Add(commandText, await connection.GetCachedProcedure(commandText, revalidateMissing: false, ioBehavior, cancellationToken).ConfigureAwait(false));
32+
var cachedProcedure = await connection.GetCachedProcedure(commandText, revalidateMissing: false, ioBehavior, cancellationToken).ConfigureAwait(false);
33+
34+
if (cachedProcedure != null)
35+
{
36+
cachedProcedures.Add(commandText, cachedProcedure);
37+
}
3438

3539
// because the connection was used to execute a MySqlDataReader with the connection's DefaultCommandTimeout,
3640
// we need to reapply the command's CommandTimeout (even if some of the time has elapsed)
@@ -41,7 +45,7 @@ public static async ValueTask<MySqlDataReader> ExecuteReaderAsync(CommandListPos
4145

4246
var writer = new ByteBufferWriter();
4347
//// cachedProcedures will be non-null if there is a stored procedure, which is also the only time it will be read
44-
if (!payloadCreator.WriteQueryCommand(ref commandListPosition, cachedProcedures!, writer, false))
48+
if (!payloadCreator.WriteQueryCommand(ref commandListPosition, cachedProcedures, writer, false))
4549
throw new InvalidOperationException("ICommandPayloadCreator failed to write query payload");
4650

4751
cancellationToken.ThrowIfCancellationRequested();

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,20 @@ public async Task PrepareAsync(IMySqlCommand command, IOBehavior ioBehavior, Can
149149
string commandToPrepare;
150150
if (command.CommandType == CommandType.StoredProcedure)
151151
{
152-
var cachedProcedure = await command.Connection!.GetCachedProcedure(commandText, revalidateMissing: false, ioBehavior, cancellationToken).ConfigureAwait(false);
153-
if (cachedProcedure is null)
152+
var parameterCount = command.RawParameters?.Count ?? 0;
153+
154+
if (UseProcedureCache)
154155
{
155-
var name = NormalizedSchema.MustNormalize(command.CommandText!, command.Connection.Database);
156-
throw new MySqlException($"Procedure or function '{name.Component}' cannot be found in database '{name.Schema}'.");
156+
var cachedProcedure = await command.Connection!.GetCachedProcedure(commandText, revalidateMissing: false, ioBehavior, cancellationToken).ConfigureAwait(false);
157+
if (cachedProcedure is null)
158+
{
159+
var name = NormalizedSchema.MustNormalize(command.CommandText!, command.Connection.Database);
160+
throw new MySqlException($"Procedure or function '{name.Component}' cannot be found in database '{name.Schema}, or procedure caching is disabled");
161+
}
162+
163+
parameterCount = cachedProcedure.Parameters.Count;
157164
}
158165

159-
var parameterCount = cachedProcedure.Parameters.Count;
160166
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
161167
commandToPrepare = string.Create(commandText.Length + 7 + (parameterCount * 2) + (parameterCount == 0 ? 1 : 0), (commandText, parameterCount), static (buffer, state) =>
162168
{

src/MySqlConnector/MySqlCommandBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private static async Task DeriveParametersAsync(IOBehavior ioBehavior, MySqlComm
3131
if (cachedProcedure is null)
3232
{
3333
var name = NormalizedSchema.MustNormalize(command.CommandText!, command.Connection.Database);
34-
throw new MySqlException($"Procedure or function '{name.Component}' cannot be found in database '{name.Schema}'.");
34+
throw new MySqlException($"Procedure or function '{name.Component}' cannot be found in database '{name.Schema}', or procedure caching is disabled");
3535
}
3636

3737
command.Parameters.Clear();

src/MySqlConnector/MySqlConnection.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,9 @@ internal void Cancel(ICancellableCommand command, int commandId, bool isCancel)
962962

963963
internal async Task<CachedProcedure?> GetCachedProcedure(string name, bool revalidateMissing, IOBehavior ioBehavior, CancellationToken cancellationToken)
964964
{
965+
if (!m_session!.UseProcedureCache)
966+
return null;
967+
965968
Log.GettingCachedProcedure(m_logger, m_session!.Id, name);
966969
if (State != ConnectionState.Open)
967970
throw new InvalidOperationException("Connection is not open.");

0 commit comments

Comments
 (0)