From 52dd4895157da932d152d8d762df0989613ffb9d Mon Sep 17 00:00:00 2001 From: Eugene Kosov Date: Mon, 13 Jan 2025 15:40:58 +0300 Subject: [PATCH] MDEV-22441 implement a generic way to change a value of a variable in a scope Example: { auto _= make_scope_value(var, tmp_value); } make_scope_value(): a function which returns RAII object which temporary changes a value of a variable detail::Scope_value: actual implementation of such RAII class. It shouldn't be used directly! That's why it's inside a namespace detail. --- include/scope.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ sql/sql_acl.cc | 6 ++++-- sql/sql_show.cc | 4 ++-- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/include/scope.h b/include/scope.h index e0e9fc62969..836c4f77b35 100644 --- a/include/scope.h +++ b/include/scope.h @@ -72,3 +72,48 @@ make_scope_exit(Callable &&f) #define ANONYMOUS_VARIABLE CONCAT(_anonymous_variable, __LINE__) #define SCOPE_EXIT auto ANONYMOUS_VARIABLE= make_scope_exit + +namespace detail +{ + +template class Scope_value +{ +public: + Scope_value(T &variable, const T &scope_value) + : variable_(variable), saved_value_(variable) + { + variable= scope_value; + } + + Scope_value(Scope_value &&rhs) + : variable_(rhs.variable_), saved_value_(rhs.saved_value_), + engaged_(rhs.engaged_) + { + rhs.engaged_= false; + } + + Scope_value(const Scope_value &)= delete; + Scope_value &operator=(const Scope_value &)= delete; + Scope_value &operator=(Scope_value &&)= delete; + + ~Scope_value() + { + if (engaged_) + variable_= saved_value_; + } + +private: + T &variable_; + T saved_value_; + bool engaged_= true; +}; + +} // namespace detail + +// Use like this: +// auto _= make_scope_value(var, tmp_value); +template +detail::Scope_value make_scope_value(T &variable, const T &scope_value) +{ + return detail::Scope_value(variable, scope_value); +} diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 59980264741..98e79babcc3 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -54,6 +54,7 @@ #include "sql_array.h" #include "sql_hset.h" #include "password.h" +#include "scope.h" #include "sql_plugin_compat.h" #include "wsrep_mysqld.h" @@ -2552,10 +2553,11 @@ static bool acl_load(THD *thd, const Grant_tables& tables) { READ_RECORD read_record_info; char tmp_name[SAFE_NAME_LEN+1]; - Sql_mode_save old_mode_save(thd); DBUG_ENTER("acl_load"); - thd->variables.sql_mode&= ~MODE_PAD_CHAR_TO_FULL_LENGTH; + auto _= make_scope_value(thd->variables.sql_mode, + thd->variables.sql_mode & + ~MODE_PAD_CHAR_TO_FULL_LENGTH); grant_version++; /* Privileges updated */ diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 2965cdf0199..ee3d85e6ba2 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -65,6 +65,7 @@ #include "transaction.h" #include "opt_trace.h" #include "my_cpu.h" +#include "scope.h" #include "lex_symbol.h" @@ -6437,8 +6438,7 @@ bool store_schema_params(THD *thd, TABLE *table, TABLE *proc_table, { Field *field; LEX_CSTRING tmp_string; - Sql_mode_save sql_mode_backup(thd); - thd->variables.sql_mode= sql_mode; + auto _= make_scope_value(thd->variables.sql_mode, sql_mode); if (sph->type() == SP_TYPE_FUNCTION) {