Move some mysql/mariadb utilities to maxutils
Can be used in system tests later on.
This commit is contained in:
@ -1 +1,2 @@
|
||||
add_subdirectory(maxbase)
|
||||
add_subdirectory(maxsql)
|
||||
|
||||
3
maxutils/maxsql/CMakeLists.txt
Normal file
3
maxutils/maxsql/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
||||
include_directories(include)
|
||||
add_subdirectory(include/maxsql)
|
||||
add_subdirectory(src)
|
||||
3
maxutils/maxsql/include/maxsql/CMakeLists.txt
Normal file
3
maxutils/maxsql/include/maxsql/CMakeLists.txt
Normal file
@ -0,0 +1,3 @@
|
||||
install_header(mariadb.hh devel)
|
||||
install_header(ccdefs.hh devel)
|
||||
|
||||
38
maxutils/maxsql/include/maxsql/ccdefs.hh
Normal file
38
maxutils/maxsql/include/maxsql/ccdefs.hh
Normal file
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2018 MariaDB Corporation Ab
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
|
||||
*
|
||||
* Change Date: 2022-01-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2 or later of the General
|
||||
* Public License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file ccdefs.hh
|
||||
*
|
||||
* This file should be included first by all maxsql headers.
|
||||
*/
|
||||
|
||||
#if !defined (__cplusplus)
|
||||
#error This file is only to be included by C++ code.
|
||||
#endif
|
||||
|
||||
#include <maxbase/ccdefs.hh>
|
||||
|
||||
/**
|
||||
* All classes of MaxSql are defined in the namespace @c maxsql.
|
||||
*/
|
||||
namespace maxsql
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand for the @c maxsql namespace.
|
||||
*/
|
||||
namespace mxq = maxsql;
|
||||
54
maxutils/maxsql/include/maxsql/mariadb.hh
Normal file
54
maxutils/maxsql/include/maxsql/mariadb.hh
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2018 MariaDB Corporation Ab
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
|
||||
*
|
||||
* Change Date: 2022-01-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2 or later of the General
|
||||
* Public License.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <maxsql/ccdefs.hh>
|
||||
#include <string>
|
||||
#include <mysql.h>
|
||||
|
||||
namespace maxsql
|
||||
{
|
||||
/**
|
||||
* Execute a query, manually defining retry limits.
|
||||
*
|
||||
* @param conn MySQL connection
|
||||
* @param query Query to execute
|
||||
* @param query_retries Maximum number of retries
|
||||
* @param query_retry_timeout Maximum time to spend retrying, in seconds
|
||||
* @return return value of mysql_query
|
||||
*/
|
||||
int mysql_query_ex(MYSQL* conn, const std::string& query, int query_retries, time_t query_retry_timeout);
|
||||
|
||||
/**
|
||||
* Check if the MYSQL error number is a connection error.
|
||||
*
|
||||
* @param Error code
|
||||
* @return True if the MYSQL error number is a connection error
|
||||
*/
|
||||
bool mysql_is_net_error(unsigned int errcode);
|
||||
|
||||
/**
|
||||
* Enable/disable the logging of all SQL statements MaxScale sends to
|
||||
* the servers.
|
||||
*
|
||||
* @param enable If true, enable, if false, disable.
|
||||
*/
|
||||
void mysql_set_log_statements(bool enable);
|
||||
|
||||
/**
|
||||
* Returns whether SQL statements sent to the servers are logged or not.
|
||||
*
|
||||
* @return True, if statements are logged, false otherwise.
|
||||
*/
|
||||
bool mysql_get_log_statements();
|
||||
}
|
||||
7
maxutils/maxsql/src/CMakeLists.txt
Normal file
7
maxutils/maxsql/src/CMakeLists.txt
Normal file
@ -0,0 +1,7 @@
|
||||
add_library(maxsql STATIC
|
||||
mariadb.cc
|
||||
)
|
||||
|
||||
target_link_libraries(maxsql maxbase ${MARIADB_CONNECTOR_LIBRARIES})
|
||||
set_target_properties(maxsql PROPERTIES VERSION "1.0.0" LINK_FLAGS -Wl,-z,defs)
|
||||
|
||||
87
maxutils/maxsql/src/mariadb.cc
Normal file
87
maxutils/maxsql/src/mariadb.cc
Normal file
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (c) 2016 MariaDB Corporation Ab
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
|
||||
*
|
||||
* Change Date: 2022-01-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2 or later of the General
|
||||
* Public License.
|
||||
*/
|
||||
#include <maxsql/mariadb.hh>
|
||||
#include <time.h>
|
||||
#include <errmsg.h>
|
||||
#include <maxbase/assert.h>
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
struct THIS_UNIT
|
||||
{
|
||||
bool log_statements; // Should all statements sent to server be logged?
|
||||
};
|
||||
|
||||
static THIS_UNIT this_unit =
|
||||
{
|
||||
false
|
||||
};
|
||||
}
|
||||
|
||||
namespace maxsql
|
||||
{
|
||||
|
||||
int mysql_query_ex(MYSQL* conn, const std::string& query, int query_retries, time_t query_retry_timeout)
|
||||
{
|
||||
const char* query_cstr = query.c_str();
|
||||
time_t start = time(NULL);
|
||||
int rc = mysql_query(conn, query_cstr);
|
||||
|
||||
for (int n = 0; rc != 0 && n < query_retries && mysql_is_net_error(mysql_errno(conn))
|
||||
&& time(NULL) - start < query_retry_timeout; n++)
|
||||
{
|
||||
rc = mysql_query(conn, query_cstr);
|
||||
}
|
||||
|
||||
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);
|
||||
MXB_NOTICE("SQL([%s]:%u): %d, \"%s\"", host, port, rc, query_cstr);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
bool 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;
|
||||
}
|
||||
}
|
||||
|
||||
void mysql_set_log_statements(bool enable)
|
||||
{
|
||||
this_unit.log_statements = enable;
|
||||
}
|
||||
|
||||
bool mysql_get_log_statements()
|
||||
{
|
||||
return this_unit.log_statements;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user