From b58c4a3477571e60d8e7c4fb42a93f8a3ae1f946 Mon Sep 17 00:00:00 2001 From: tarasevichvlad Date: Sat, 21 Dec 2024 22:25:12 +0300 Subject: [PATCH] FIx comments --- src/MySqlConnector/Core/CachedProcedure.cs | 3 --- src/MySqlConnector/Core/CommandExecutor.cs | 14 +++++++++----- src/MySqlConnector/Core/ServerSession.cs | 16 +++++++++++----- src/MySqlConnector/MySqlCommandBuilder.cs | 2 +- src/MySqlConnector/MySqlConnection.cs | 3 +++ 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/MySqlConnector/Core/CachedProcedure.cs b/src/MySqlConnector/Core/CachedProcedure.cs index fc2136d78..441a7982b 100644 --- a/src/MySqlConnector/Core/CachedProcedure.cs +++ b/src/MySqlConnector/Core/CachedProcedure.cs @@ -12,9 +12,6 @@ internal sealed class CachedProcedure { public static async Task FillAsync(IOBehavior ioBehavior, MySqlConnection connection, string schema, string component, ILogger logger, CancellationToken cancellationToken) { - if (!connection.Session.UseProcedureCache) - return null; - // try to use mysql.proc first, as it is much faster if (!connection.Session.ServerVersion.IsMariaDb && connection.Session.ServerVersion.Version < ServerVersions.RemovesMySqlProcTable && diff --git a/src/MySqlConnector/Core/CommandExecutor.cs b/src/MySqlConnector/Core/CommandExecutor.cs index a2581334a..f21d0b3fb 100644 --- a/src/MySqlConnector/Core/CommandExecutor.cs +++ b/src/MySqlConnector/Core/CommandExecutor.cs @@ -20,17 +20,21 @@ public static async ValueTask ExecuteReaderAsync(CommandListPos Log.CommandExecutorExecuteReader(command.Logger, connection.Session.Id, ioBehavior, commandListPosition.CommandCount); - Dictionary? cachedProcedures = null; + var cachedProcedures = new Dictionary(); for (var commandIndex = 0; commandIndex < commandListPosition.CommandCount; commandIndex++) { var command2 = commandListPosition.CommandAt(commandIndex); - if (command2.CommandType == CommandType.StoredProcedure) + if (command2.CommandType == CommandType.StoredProcedure && connection.Session.UseProcedureCache) { - cachedProcedures ??= []; var commandText = command2.CommandText!; if (!cachedProcedures.ContainsKey(commandText)) { - cachedProcedures.Add(commandText, await connection.GetCachedProcedure(commandText, revalidateMissing: false, ioBehavior, cancellationToken).ConfigureAwait(false)); + var cachedProcedure = await connection.GetCachedProcedure(commandText, revalidateMissing: false, ioBehavior, cancellationToken).ConfigureAwait(false); + + if (cachedProcedure != null) + { + cachedProcedures.Add(commandText, cachedProcedure); + } // because the connection was used to execute a MySqlDataReader with the connection's DefaultCommandTimeout, // we need to reapply the command's CommandTimeout (even if some of the time has elapsed) @@ -41,7 +45,7 @@ public static async ValueTask ExecuteReaderAsync(CommandListPos var writer = new ByteBufferWriter(); //// cachedProcedures will be non-null if there is a stored procedure, which is also the only time it will be read - if (!payloadCreator.WriteQueryCommand(ref commandListPosition, cachedProcedures!, writer, false)) + if (!payloadCreator.WriteQueryCommand(ref commandListPosition, cachedProcedures, writer, false)) throw new InvalidOperationException("ICommandPayloadCreator failed to write query payload"); cancellationToken.ThrowIfCancellationRequested(); diff --git a/src/MySqlConnector/Core/ServerSession.cs b/src/MySqlConnector/Core/ServerSession.cs index 614e05d3a..e30272439 100644 --- a/src/MySqlConnector/Core/ServerSession.cs +++ b/src/MySqlConnector/Core/ServerSession.cs @@ -149,14 +149,20 @@ public async Task PrepareAsync(IMySqlCommand command, IOBehavior ioBehavior, Can string commandToPrepare; if (command.CommandType == CommandType.StoredProcedure) { - var cachedProcedure = await command.Connection!.GetCachedProcedure(commandText, revalidateMissing: false, ioBehavior, cancellationToken).ConfigureAwait(false); - if (cachedProcedure is null) + var parameterCount = command.RawParameters?.Count ?? 0; + + if (UseProcedureCache) { - var name = NormalizedSchema.MustNormalize(command.CommandText!, command.Connection.Database); - throw new MySqlException($"Procedure or function '{name.Component}' cannot be found in database '{name.Schema}'."); + var cachedProcedure = await command.Connection!.GetCachedProcedure(commandText, revalidateMissing: false, ioBehavior, cancellationToken).ConfigureAwait(false); + if (cachedProcedure is null) + { + var name = NormalizedSchema.MustNormalize(command.CommandText!, command.Connection.Database); + throw new MySqlException($"Procedure or function '{name.Component}' cannot be found in database '{name.Schema}, or procedure caching is disabled"); + } + + parameterCount = cachedProcedure.Parameters.Count; } - var parameterCount = cachedProcedure.Parameters.Count; #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER commandToPrepare = string.Create(commandText.Length + 7 + (parameterCount * 2) + (parameterCount == 0 ? 1 : 0), (commandText, parameterCount), static (buffer, state) => { diff --git a/src/MySqlConnector/MySqlCommandBuilder.cs b/src/MySqlConnector/MySqlCommandBuilder.cs index 7b63dd5ef..64e9c4edf 100644 --- a/src/MySqlConnector/MySqlCommandBuilder.cs +++ b/src/MySqlConnector/MySqlCommandBuilder.cs @@ -31,7 +31,7 @@ private static async Task DeriveParametersAsync(IOBehavior ioBehavior, MySqlComm if (cachedProcedure is null) { var name = NormalizedSchema.MustNormalize(command.CommandText!, command.Connection.Database); - throw new MySqlException($"Procedure or function '{name.Component}' cannot be found in database '{name.Schema}'."); + throw new MySqlException($"Procedure or function '{name.Component}' cannot be found in database '{name.Schema}', or procedure caching is disabled"); } command.Parameters.Clear(); diff --git a/src/MySqlConnector/MySqlConnection.cs b/src/MySqlConnector/MySqlConnection.cs index 27219bb47..4055b8d42 100644 --- a/src/MySqlConnector/MySqlConnection.cs +++ b/src/MySqlConnector/MySqlConnection.cs @@ -962,6 +962,9 @@ internal void Cancel(ICancellableCommand command, int commandId, bool isCancel) internal async Task GetCachedProcedure(string name, bool revalidateMissing, IOBehavior ioBehavior, CancellationToken cancellationToken) { + if (!m_session!.UseProcedureCache) + return null; + Log.GettingCachedProcedure(m_logger, m_session!.Id, name); if (State != ConnectionState.Open) throw new InvalidOperationException("Connection is not open.");