Skip to content

Commit

Permalink
FIx comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tarasevichvlad committed Dec 21, 2024
1 parent d3af31e commit b58c4a3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
3 changes: 0 additions & 3 deletions src/MySqlConnector/Core/CachedProcedure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ internal sealed class CachedProcedure
{
public static async Task<CachedProcedure?> 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 &&
Expand Down
14 changes: 9 additions & 5 deletions src/MySqlConnector/Core/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ public static async ValueTask<MySqlDataReader> ExecuteReaderAsync(CommandListPos

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

Dictionary<string, CachedProcedure?>? cachedProcedures = null;
var cachedProcedures = new Dictionary<string, CachedProcedure?>();
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)
Expand All @@ -41,7 +45,7 @@ public static async ValueTask<MySqlDataReader> 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();
Expand Down
16 changes: 11 additions & 5 deletions src/MySqlConnector/Core/ServerSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/MySqlConnector/MySqlCommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/MySqlConnector/MySqlConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,9 @@ internal void Cancel(ICancellableCommand command, int commandId, bool isCancel)

internal async Task<CachedProcedure?> 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.");
Expand Down

0 comments on commit b58c4a3

Please sign in to comment.