Add helper function for reading values by field name
The helper function provides map-like access to row values. This is used to retrieve the values for all MariaDB 10.0+ versions as there are differences in the returned results between 10.1 and 10.2.
This commit is contained in:
parent
2d1e5f46fa
commit
0bc439641a
@ -66,6 +66,17 @@ int mxs_mysql_query(MYSQL* conn, const char* query);
|
||||
*/
|
||||
bool mxs_mysql_trim_quotes(char *s);
|
||||
|
||||
/**
|
||||
* Helper function for getting values by field name
|
||||
*
|
||||
* @param result Resultset
|
||||
* @param row Row where the value is read
|
||||
* @param key Name of the field
|
||||
*
|
||||
* @return The value of the field or NULL if value is not found. NULL values
|
||||
* are also returned as NULL pointers.
|
||||
*/
|
||||
const char* mxs_mysql_get_value(MYSQL_RES* result, MYSQL_ROW row, const char* key);
|
||||
|
||||
typedef enum mxs_pcre_quote_approach
|
||||
{
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include <maxscale/mysql_utils.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <stdbool.h>
|
||||
#include <errmsg.h>
|
||||
|
||||
@ -210,6 +211,22 @@ int mxs_mysql_query(MYSQL* conn, const char* query)
|
||||
return rc;
|
||||
}
|
||||
|
||||
const char* mxs_mysql_get_value(MYSQL_RES* result, MYSQL_ROW row, const char* key)
|
||||
{
|
||||
MYSQL_FIELD* f = mysql_fetch_fields(result);
|
||||
int nfields = mysql_num_fields(result);
|
||||
|
||||
for (int i = 0; i < nfields; i++)
|
||||
{
|
||||
if (strcasecmp(f[i].name, key) == 0)
|
||||
{
|
||||
return row[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool mxs_mysql_trim_quotes(char *s)
|
||||
{
|
||||
bool dequoted = true;
|
||||
|
@ -1089,22 +1089,22 @@ static bool do_show_slave_status(MYSQL_SERVER_INFO* serv_info, MXS_MONITORED_SER
|
||||
|
||||
if (server_version == MYSQL_SERVER_VERSION_100)
|
||||
{
|
||||
ss_debug(MYSQL_FIELD* f = mysql_fetch_fields(result));
|
||||
ss_dassert(strcmp(f[MARIA10_STATUS_HEARTBEATS].name, "Slave_received_heartbeats") == 0);
|
||||
ss_dassert(strcmp(f[MARIA10_STATUS_HEARTBEAT_PERIOD].name, "Slave_heartbeat_period") == 0);
|
||||
ss_dassert(strcmp(f[MARIA10_STATUS_SLAVE_GTID].name, "Gtid_Slave_Pos") == 0);
|
||||
const char* beats = mxs_mysql_get_value(result, row, "Slave_received_heartbeats");
|
||||
const char* period = mxs_mysql_get_value(result, row, "Slave_heartbeat_period");
|
||||
const char* gtid = mxs_mysql_get_value(result, row, "Gtid_Slave_Pos");
|
||||
ss_dassert(beats && period);
|
||||
|
||||
int heartbeats = atoi(row[MARIA10_STATUS_HEARTBEATS]);
|
||||
int heartbeats = atoi(beats);
|
||||
if (serv_info->slave_heartbeats < heartbeats)
|
||||
{
|
||||
serv_info->latest_event = time(NULL);
|
||||
serv_info->slave_heartbeats = heartbeats;
|
||||
serv_info->heartbeat_period = atof(row[MARIA10_STATUS_HEARTBEAT_PERIOD]);
|
||||
serv_info->heartbeat_period = atof(period);
|
||||
}
|
||||
|
||||
if (row[MARIA10_STATUS_SLAVE_GTID])
|
||||
if (gtid)
|
||||
{
|
||||
extract_slave_gtid(serv_info, row[MARIA10_STATUS_SLAVE_GTID]);
|
||||
extract_slave_gtid(serv_info, gtid);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user