In debug mode, the handlers for ProxySQL_Statistics are called even if the web interface plugin is not loaded. This way the code can be tested even if the plugin is not loaded. Added new member function ProxySQL_Statistics::knows_variable_name(), which is used to check if a variable_name is in the map. This is useful to do a check before get_variable_id_for_name() in instances where it is undesirable to load or create entries in the lookup table, while keeping direct access to the map private and mutex protected. In ProxySQL_Statistics::get_variable_id_for_name, wrapped the variable_id select code in a lambda so that it could be reused instead of calling sqlite3_last_insert_rowid(), as there were some thread safety concerns which potentially could produce incorrect ids. Changed the insert to use execute() instead of execute_statement() instead, sufficient and simpler. Added error in case the function could not find, load, or generate a new id.
/** @brief Map with the key being the variable_name and the value being the variable_id, used for history_mysql_variables data. Matches the history_mysql_variables_lookup. */
// @note: Perhaps a better solution here in case of SQLite errors? Originally considered returning std::optional (but that's c++17) or negative value if not found.
// This would also mean having to change the index calculations and prepared statement in MySQL_Threads_Handler_sets_v2() to skip variables with missing ids.
// So, for now, this code follows the convention originally used above which exits if there are SQLite errors.
if(error){
proxy_error("SQLITE ERROR %s",error);
free(error);
error=NULL;
exit(EXIT_SUCCESS);
}
if(result){
if(result->rows_count>0){
// matching variable_id for variable_name in lookup table
SQLite3_row*r=result->rows[0];
returnstrtoll(r->fields[0],NULL,10);
}
deleteresult;
result=NULL;
}
return-1;
};
if(error){
proxy_error("SQLITE ERROR %s",error);
free(error);
error=NULL;
exit(EXIT_SUCCESS);
}
if(result){
if(result->rows_count>0){
// matching variable_id for variable_name in lookup table
SQLite3_row*r=result->rows[0];
variable_id=strtoll(r->fields[0],NULL,10);
}
deleteresult;
result=NULL;
}
variable_id=select_var_id();// Check for variable name present in the lookup
if(variable_id<0){
// No match found, create a new record in the lookup table and then return the newly generated id
// No match found, create a new record in the lookup table and then select the newly generated id
stringinsert_var_query="INSERT INTO history_mysql_status_variables_lookup(variable_name) VALUES(\""+variable_name+"\")";