diff --git a/include/BackendSyncDecision.h b/include/BackendSyncDecision.h new file mode 100644 index 000000000..b0be1728a --- /dev/null +++ b/include/BackendSyncDecision.h @@ -0,0 +1,48 @@ +/** + * @file BackendSyncDecision.h + * @brief Pure decision functions for backend variable synchronization. + * + * Extracted from MySQL_Session's verify chain (handler_again___verify_*). + * Determines what sync actions are needed before a query can execute + * on a backend connection. + * + * @see Phase 3.6 (GitHub issue #5494) + */ + +#ifndef BACKEND_SYNC_DECISION_H +#define BACKEND_SYNC_DECISION_H + +/** + * @brief Actions that may be needed to synchronize backend state. + */ +enum BackendSyncAction { + SYNC_NONE = 0, ///< No synchronization needed. + SYNC_SCHEMA = 1, ///< Schema (USE db) needs to be sent. + SYNC_USER = 2, ///< Username mismatch, CHANGE USER required. + SYNC_AUTOCOMMIT = 4, ///< Autocommit state needs to be synced. +}; + +/** + * @brief Determine what sync actions are needed for the backend. + * + * Checks client vs backend state and returns a bitmask of required + * actions. Mirrors the MySQL_Session verify chain logic. + * + * @param client_user Client connection username. + * @param backend_user Backend connection username. + * @param client_schema Client connection schema. + * @param backend_schema Backend connection schema. + * @param client_autocommit Client autocommit setting. + * @param backend_autocommit Backend autocommit setting. + * @return Bitmask of BackendSyncAction values. + */ +int determine_backend_sync_actions( + const char *client_user, + const char *backend_user, + const char *client_schema, + const char *backend_schema, + bool client_autocommit, + bool backend_autocommit +); + +#endif // BACKEND_SYNC_DECISION_H diff --git a/lib/BackendSyncDecision.cpp b/lib/BackendSyncDecision.cpp new file mode 100644 index 000000000..04375b1b4 --- /dev/null +++ b/lib/BackendSyncDecision.cpp @@ -0,0 +1,45 @@ +/** + * @file BackendSyncDecision.cpp + * @brief Implementation of backend variable sync decisions. + * + * @see BackendSyncDecision.h + * @see Phase 3.6 (GitHub issue #5494) + */ + +#include "BackendSyncDecision.h" +#include + +int determine_backend_sync_actions( + const char *client_user, + const char *backend_user, + const char *client_schema, + const char *backend_schema, + bool client_autocommit, + bool backend_autocommit) +{ + int actions = SYNC_NONE; + + // Username mismatch → CHANGE USER required + if (client_user && backend_user) { + if (strcmp(client_user, backend_user) != 0) { + actions |= SYNC_USER; + } + } + + // Schema mismatch → USE required + // Only check if usernames match (user change handles schema too) + if (!(actions & SYNC_USER)) { + if (client_schema && backend_schema) { + if (strcmp(client_schema, backend_schema) != 0) { + actions |= SYNC_SCHEMA; + } + } + } + + // Autocommit mismatch → SET autocommit required + if (client_autocommit != backend_autocommit) { + actions |= SYNC_AUTOCOMMIT; + } + + return actions; +} diff --git a/lib/Makefile b/lib/Makefile index 7af5cd6ef..e91e240d8 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -108,6 +108,7 @@ _OBJ_CXX := ProxySQL_GloVars.oo network.oo debug.oo configfile.oo Query_Cache.oo MonitorHealthDecision.oo \ TransactionState.oo \ HostgroupRouting.oo \ + BackendSyncDecision.oo \ proxy_sqlite3_symbols.oo # TSDB object files