From 83bec8d47713916dec75b0b05ae22e7120cf6843 Mon Sep 17 00:00:00 2001 From: Rahim Kanji Date: Wed, 8 Oct 2025 16:41:45 +0500 Subject: [PATCH] Refactored remove_quotes function --- include/PgSQL_Set_Stmt_Parser.h | 1 + lib/PgSQL_Session.cpp | 19 +----------- lib/PgSQL_Set_Stmt_Parser.cpp | 51 ++++++++++++++------------------- 3 files changed, 23 insertions(+), 48 deletions(-) diff --git a/include/PgSQL_Set_Stmt_Parser.h b/include/PgSQL_Set_Stmt_Parser.h index 1ccd6745d..1deb50a7b 100644 --- a/include/PgSQL_Set_Stmt_Parser.h +++ b/include/PgSQL_Set_Stmt_Parser.h @@ -36,6 +36,7 @@ class PgSQL_Set_Stmt_Parser { std::map> parse1v2(); void generateRE_parse1v2(); std::string remove_comments(const std::string& q); + static void unquote_if_quoted(std::string& v); }; #endif /* __CLASS_PGSQL_SET_STMT_PARSER_H */ diff --git a/lib/PgSQL_Session.cpp b/lib/PgSQL_Session.cpp index f26a2272b..d27cc139b 100644 --- a/lib/PgSQL_Session.cpp +++ b/lib/PgSQL_Session.cpp @@ -3936,19 +3936,6 @@ bool PgSQL_Session::is_multi_statement_command(const char* cmd) { return false; } -static void remove_quotes(string& v) { - if (v.length() > 2) { - char firstChar = v[0]; - char lastChar = v[v.length() - 1]; - if (firstChar == lastChar) { - if (firstChar == '\'' || firstChar == '"' || firstChar == '`') { - v.erase(v.length() - 1, 1); - v.erase(0, 1); - } - } - } -} - bool PgSQL_Session::handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___handle_SET_command(const char* dig, bool* lock_hostgroup) { // this code is executed only if locked_on_hostgroup is not set yet // if locked_on_hostgroup is set, we do not try to parse the SET statement @@ -4013,11 +4000,7 @@ bool PgSQL_Session::handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___handle_ if (idx != PGSQL_NAME_LAST_HIGH_WM) { if (IS_PGTRACKED_VAR_OPTION_SET_NO_STRIP_VALUE(pgsql_tracked_variables[idx]) == 0) { - if (value1 == "''" || value1 == "\"\"") { - value1.clear(); - } else { - remove_quotes(value1); - } + PgSQL_Set_Stmt_Parser::unquote_if_quoted(value1); } uint32_t current_hash = pgsql_variables.client_get_hash(this, idx); diff --git a/lib/PgSQL_Set_Stmt_Parser.cpp b/lib/PgSQL_Set_Stmt_Parser.cpp index b5016debb..b471e69b0 100644 --- a/lib/PgSQL_Set_Stmt_Parser.cpp +++ b/lib/PgSQL_Set_Stmt_Parser.cpp @@ -20,19 +20,6 @@ using namespace std; -static void remove_quotes(string& v) { - if (v.length() > 2) { - char firstChar = v[0]; - char lastChar = v[v.length()-1]; - if (firstChar == lastChar) { - if (firstChar == '\'' || firstChar == '"' || firstChar == '`') { - v.erase(v.length()-1, 1); - v.erase(0, 1); - } - } - } -} - #ifdef PARSERDEBUG PgSQL_Set_Stmt_Parser::PgSQL_Set_Stmt_Parser(std::string nq, int verb) { verbosity = verb; @@ -93,6 +80,18 @@ VALGRIND_DISABLE_ERROR_REPORTING; parse1v2_init = true; } +void PgSQL_Set_Stmt_Parser::unquote_if_quoted(std::string& v) { + if (v.length() >= 2) { + char firstChar = v[0]; + char lastChar = v[v.length() - 1]; + if (firstChar == lastChar) { + if (firstChar == '\'' || firstChar == '"' || firstChar == '`') { + v.erase(v.length() - 1, 1); + v.erase(0, 1); + } + } + } +} std::map> PgSQL_Set_Stmt_Parser::parse1v2() { @@ -123,32 +122,24 @@ VALGRIND_ENABLE_ERROR_REPORTING; else oper = ""; proxy_debug(PROXY_DEBUG_MYSQL_QUERY_PROCESSOR, 4, "SET parsing: scope='%s', parameter name='%s' , operator='%s' , parameter value='%s' , parameter_value_func='%s' , parameter_value_func_args='%s'\n", scope.c_str(), param_name.c_str(), oper.c_str(), param_val.c_str(), param_val_func.c_str(), param_val_func_args.c_str()); #endif // DEBUG - std::string key; if (param_val_func.empty() == false) return {}; - if (param_name.empty() || param_val.empty()) { - continue; - } - - key = param_name; - remove_quotes(key); + unquote_if_quoted(param_name); + size_t pos = param_val.find_last_not_of(" \n\r\t,"); if (pos != param_val.npos) { - param_val.erase(pos+1); + param_val.erase(pos + 1); } - /* - if (param_val == "''" || param_val == "\"\"") { - op.emplace_back(""); - } else { - remove_quotes(param_val); - op.emplace_back(param_val); - }*/ + if (param_name.empty() || param_val.empty()) { + continue; + } + op.emplace_back(param_val); - std::transform(key.begin(), key.end(), key.begin(), ::tolower); - result[key] = op; + std::transform(param_name.begin(), param_name.end(), param_name.begin(), ::tolower); + result[param_name] = op; } if (input.size() != 0) { #ifdef PARSERDEBUG