diff --git a/include/proxysql_config.h b/include/proxysql_config.h index 6dffec6f6..6fb7aa45a 100644 --- a/include/proxysql_config.h +++ b/include/proxysql_config.h @@ -15,12 +15,25 @@ enum proxysql_config_type { PROXYSQL_CONFIG_PROXY_SERVERS, }; +/** + * @brief Configuration management class for ProxySQL. + * + * Handles reading and writing configuration from/to SQLite database and config files. + * This class provides methods to load configuration sections (variables, users, servers, + * query rules, scheduler, restapi) from config files into the database and vice versa. + * + * The class supports automatic prefix stripping for configuration variables to handle + * cases where users mistakenly include the module prefix (e.g., "mysql-") in variable names. + */ class ProxySQL_Config { public: SQLite3DB* admindb; + /** @brief Constructs ProxySQL_Config with a database handle */ ProxySQL_Config(SQLite3DB* db); + /** @brief Virtual destructor */ virtual ~ProxySQL_Config(); + /** @copydoc ProxySQL_Config::Read_Global_Variables_from_configfile */ int Read_Global_Variables_from_configfile(const char *prefix); int Read_MySQL_Users_from_configfile(std::string& error); int Read_MySQL_Query_Rules_from_configfile(); diff --git a/lib/ProxySQL_Config.cpp b/lib/ProxySQL_Config.cpp index 8c57ca66f..69a07eb78 100644 --- a/lib/ProxySQL_Config.cpp +++ b/lib/ProxySQL_Config.cpp @@ -60,6 +60,33 @@ void ProxySQL_Config::addField(std::string& data, const char* name, const char* data += ss.str(); } +/** + * @brief Reads global variables from configuration file for a given module prefix. + * + * This function parses the configuration file section named `_variables` + * (e.g., "mysql_variables", "pgsql_variables", "admin_variables") and inserts + * the variables into the global_variables table in the format "prefix-variable_name". + * + * The function automatically strips duplicate module prefixes from variable names. + * For example, if a user writes "mysql-log_unhealthy_connections" in the mysql_variables + * section, the prefix "mysql-" is automatically removed, and the variable is stored + * as "mysql-log_unhealthy_connections" (with a single prefix). + * + * @param prefix The module prefix: "mysql", "pgsql", or "admin". + * @return Number of variables processed (successfully read and stored). + * @retval 0 if the prefix_variables section does not exist in the config file. + * + * @note The function temporarily disables foreign keys for performance. + * @note Variable values are converted to strings: booleans become "true"/"false", + * integers are converted via std::to_string, and strings are used as-is. + * + * @par Example: + * For prefix="mysql", the function reads the "mysql_variables" section from config. + * If the config contains "mysql-log_unhealthy_connections=false", it's stored as + * "mysql-log_unhealthy_connections" with value "false". + * + * @see ProxySQL_Config::Write_Global_Variables_to_configfile() + */ int ProxySQL_Config::Read_Global_Variables_from_configfile(const char *prefix) { const Setting& root = GloVars.confFile->cfg.getRoot(); char *groupname=(char *)malloc(strlen(prefix)+strlen((char *)"_variables")+1);