Move some mysql/mariadb utilities to maxutils
Can be used in system tests later on.
This commit is contained in:
@ -51,6 +51,7 @@ add_library(maxscale-common SHARED
|
||||
|
||||
target_link_libraries(maxscale-common
|
||||
maxbase
|
||||
maxsql
|
||||
${MARIADB_CONNECTOR_LIBRARIES}
|
||||
${LZMA_LINK_FLAGS}
|
||||
${PCRE2_LIBRARIES}
|
||||
|
@ -46,6 +46,7 @@
|
||||
|
||||
#include <maxbase/maxbase.hh>
|
||||
#include <maxbase/stacktrace.hh>
|
||||
#include <maxsql/mariadb.hh>
|
||||
#include <maxscale/alloc.h>
|
||||
#include <maxscale/adminusers.h>
|
||||
#include <maxscale/dcb.h>
|
||||
@ -3165,12 +3166,12 @@ static void disable_module_unloading(const char* arg)
|
||||
|
||||
static void enable_statement_logging(const char* arg)
|
||||
{
|
||||
mxs_mysql_set_log_statements(true);
|
||||
maxsql::mysql_set_log_statements(true);
|
||||
}
|
||||
|
||||
static void disable_statement_logging(const char* arg)
|
||||
{
|
||||
mxs_mysql_set_log_statements(false);
|
||||
maxsql::mysql_set_log_statements(false);
|
||||
}
|
||||
|
||||
static void redirect_output_to_file(const char* arg)
|
||||
|
@ -27,25 +27,12 @@
|
||||
#include <stdbool.h>
|
||||
#include <errmsg.h>
|
||||
|
||||
#include <maxsql/mariadb.hh>
|
||||
#include <maxscale/alloc.h>
|
||||
#include <maxscale/config.hh>
|
||||
#include <maxscale/log.h>
|
||||
#include <maxbase/atomic.hh>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct THIS_UNIT
|
||||
{
|
||||
bool log_statements; // Should all statements sent to server be logged?
|
||||
};
|
||||
|
||||
static THIS_UNIT this_unit =
|
||||
{
|
||||
false
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calculate the length of a length-encoded integer in bytes
|
||||
*
|
||||
@ -224,53 +211,10 @@ MYSQL* mxs_mysql_real_connect(MYSQL* con, SERVER* server, const char* user, cons
|
||||
return mysql;
|
||||
}
|
||||
|
||||
bool mxs_mysql_is_net_error(unsigned int errcode)
|
||||
{
|
||||
switch (errcode)
|
||||
{
|
||||
case CR_SOCKET_CREATE_ERROR:
|
||||
case CR_CONNECTION_ERROR:
|
||||
case CR_CONN_HOST_ERROR:
|
||||
case CR_IPSOCK_ERROR:
|
||||
case CR_SERVER_GONE_ERROR:
|
||||
case CR_TCP_CONNECTION:
|
||||
case CR_SERVER_LOST:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int mxs_mysql_query_ex(MYSQL* conn, const char* query, int query_retries, time_t query_retry_timeout)
|
||||
{
|
||||
time_t start = time(NULL);
|
||||
int rc = mysql_query(conn, query);
|
||||
|
||||
for (int n = 0; rc != 0 && n < query_retries
|
||||
&& mxs_mysql_is_net_error(mysql_errno(conn))
|
||||
&& time(NULL) - start < query_retry_timeout; n++)
|
||||
{
|
||||
rc = mysql_query(conn, query);
|
||||
}
|
||||
|
||||
if (this_unit.log_statements)
|
||||
{
|
||||
const char* host = "0.0.0.0";
|
||||
unsigned int port = 0;
|
||||
MXB_AT_DEBUG(int rc1 = ) mariadb_get_info(conn, MARIADB_CONNECTION_HOST, &host);
|
||||
MXB_AT_DEBUG(int rc2 = ) mariadb_get_info(conn, MARIADB_CONNECTION_PORT, &port);
|
||||
mxb_assert(!rc1 && !rc2);
|
||||
MXS_NOTICE("SQL([%s]:%u): %d, \"%s\"", host, port, rc, query);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int mxs_mysql_query(MYSQL* conn, const char* query)
|
||||
{
|
||||
MXS_CONFIG* cnf = config_get_global_options();
|
||||
return mxs_mysql_query_ex(conn, query, cnf->query_retries, cnf->query_retry_timeout);
|
||||
return maxsql::mysql_query_ex(conn, query, cnf->query_retries, cnf->query_retry_timeout);
|
||||
}
|
||||
|
||||
const char* mxs_mysql_get_value(MYSQL_RES* result, MYSQL_ROW row, const char* key)
|
||||
@ -415,13 +359,3 @@ void mxs_mysql_update_server_version(MYSQL* mysql, SERVER* server)
|
||||
mxb_assert(version_string != NULL && version_num != 0);
|
||||
server_set_version(server, version_string, version_num);
|
||||
}
|
||||
|
||||
void mxs_mysql_set_log_statements(bool enable)
|
||||
{
|
||||
this_unit.log_statements = enable;
|
||||
}
|
||||
|
||||
bool mxs_mysql_get_log_statements()
|
||||
{
|
||||
return this_unit.log_statements;
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <thread>
|
||||
#include <set>
|
||||
#include <maxbase/format.hh>
|
||||
#include <maxsql/mariadb.hh>
|
||||
#include <maxscale/mysql_utils.h>
|
||||
|
||||
|
||||
@ -114,7 +115,7 @@ bool MariaDBServer::execute_cmd_ex(const string& cmd, QueryRetryMode mode,
|
||||
}
|
||||
else
|
||||
{
|
||||
query_success = (mxs_mysql_query_ex(conn, cmd.c_str(), 0, 0) == 0);
|
||||
query_success = (maxsql::mysql_query_ex(conn, cmd, 0, 0) == 0);
|
||||
}
|
||||
|
||||
bool rval = false;
|
||||
@ -207,7 +208,7 @@ bool MariaDBServer::execute_cmd_time_limit(const std::string& cmd, maxbase::Dura
|
||||
Duration time_remaining = time_limit - timer.split();
|
||||
keep_trying = (time_remaining.secs() > 0)
|
||||
// either a connector-c timeout
|
||||
&& (mxs_mysql_is_net_error(errornum)
|
||||
&& (maxsql::mysql_is_net_error(errornum)
|
||||
// or query was interrupted by max_statement_time.
|
||||
|| (!cmd_prefix.empty() && errornum == ER_STATEMENT_TIMEOUT));
|
||||
if (!cmd_success)
|
||||
|
Reference in New Issue
Block a user