Clean up mariadb_func.h
Combined functions into one which only used different default values. Removed unused functions. Used std::string where possible to make their usage easier. Hid code that isn't used externally.
This commit is contained in:
@ -20,8 +20,8 @@ int set_ssl(MYSQL * conn)
|
||||
char client_key[1024];
|
||||
char client_cert[1024];
|
||||
char ca[1024];
|
||||
char * test_dir;
|
||||
test_dir = getenv("test_dir");
|
||||
char* test_dir = getenv("test_dir");
|
||||
|
||||
if (test_dir == NULL)
|
||||
{
|
||||
sprintf(client_key, "./ssl-cert/client-key.pem");
|
||||
@ -37,7 +37,7 @@ int set_ssl(MYSQL * conn)
|
||||
return mysql_ssl_set(conn, client_key, client_cert, ca, NULL, NULL);
|
||||
}
|
||||
|
||||
MYSQL * open_conn_db_flags(int port, const char* ip, const char* db, const char* User, const char* Password,
|
||||
MYSQL* open_conn_db_flags(int port, std::string ip, std::string db, std::string user, std::string password,
|
||||
unsigned long flag, bool ssl)
|
||||
{
|
||||
MYSQL* conn = mysql_init(NULL);
|
||||
@ -53,25 +53,13 @@ MYSQL * open_conn_db_flags(int port, const char* ip, const char* db, const char*
|
||||
set_ssl(conn);
|
||||
}
|
||||
|
||||
if (!mysql_real_connect(conn,
|
||||
ip,
|
||||
User,
|
||||
Password,
|
||||
db,
|
||||
port,
|
||||
NULL,
|
||||
flag
|
||||
))
|
||||
{
|
||||
//printf("Error: can't connect to database, error is %s:\n", mysql_error(conn));
|
||||
mysql_real_connect(conn, ip.c_str(), user.c_str(), password.c_str(),
|
||||
db.c_str(), port, NULL, flag);
|
||||
return conn;
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
MYSQL * open_conn_db_timeout(int port, const char* ip, const char* db, const char* User, const char* Password,
|
||||
unsigned long timeout, bool ssl)
|
||||
MYSQL* open_conn_db_timeout(int port, std::string ip, std::string db, std::string user, std::string password,
|
||||
unsigned int timeout, bool ssl)
|
||||
{
|
||||
MYSQL* conn = mysql_init(NULL);
|
||||
|
||||
@ -81,60 +69,20 @@ MYSQL * open_conn_db_timeout(int port, const char* ip, const char* db, const cha
|
||||
return NULL;
|
||||
}
|
||||
|
||||
unsigned int conn_timeout = timeout;
|
||||
unsigned int read_timeout = timeout;
|
||||
unsigned int write_timeout = timeout;
|
||||
mysql_options(conn, MYSQL_OPT_CONNECT_TIMEOUT, &conn_timeout);
|
||||
mysql_options(conn, MYSQL_OPT_READ_TIMEOUT, &read_timeout);
|
||||
mysql_options(conn, MYSQL_OPT_WRITE_TIMEOUT, &write_timeout);
|
||||
mysql_options(conn, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);
|
||||
mysql_options(conn, MYSQL_OPT_READ_TIMEOUT, &timeout);
|
||||
mysql_options(conn, MYSQL_OPT_WRITE_TIMEOUT, &timeout);
|
||||
|
||||
if (ssl)
|
||||
{
|
||||
if (ssl)
|
||||
{
|
||||
set_ssl(conn);
|
||||
}
|
||||
}
|
||||
|
||||
if (!mysql_real_connect(conn,
|
||||
ip,
|
||||
User,
|
||||
Password,
|
||||
db,
|
||||
port,
|
||||
NULL,
|
||||
CLIENT_MULTI_STATEMENTS
|
||||
))
|
||||
{
|
||||
//printf("Error: can't connect to database, error is %s:\n", mysql_error(conn));
|
||||
mysql_real_connect(conn, ip.c_str(), user.c_str(), password.c_str(),
|
||||
db.c_str(), port, NULL, CLIENT_MULTI_STATEMENTS);
|
||||
return conn;
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
MYSQL* open_conn_db_timeout(int port, const std::string& ip, const std::string& db,
|
||||
const std::string& user, const std::string& password,
|
||||
unsigned long timeout, bool ssl)
|
||||
{
|
||||
return open_conn_db_timeout(port, ip.c_str(), db.c_str(), user.c_str(), password.c_str(), timeout, ssl);
|
||||
}
|
||||
|
||||
MYSQL * open_conn_db(int port, const char* ip, const char* db, const char* User, const char* Password,
|
||||
bool ssl)
|
||||
{
|
||||
return open_conn_db_flags(port, ip, db, User, Password, CLIENT_MULTI_STATEMENTS, ssl);
|
||||
}
|
||||
|
||||
MYSQL * open_conn(int port, const char* ip, const char* User, const char* Password, bool ssl)
|
||||
{
|
||||
return open_conn_db(port, ip, "test", User, Password, ssl);
|
||||
}
|
||||
|
||||
MYSQL * open_conn_no_db(int port, const char* ip, const char*User, const char*Password, bool ssl)
|
||||
{
|
||||
return open_conn_db_flags(port, ip, NULL, User, Password, CLIENT_MULTI_STATEMENTS, ssl);
|
||||
}
|
||||
|
||||
int execute_query(MYSQL* conn, const char* format, ...)
|
||||
{
|
||||
va_list valist;
|
||||
@ -149,7 +97,7 @@ int execute_query(MYSQL *conn, const char *format, ...)
|
||||
vsnprintf(sql, sizeof(sql), format, valist);
|
||||
va_end(valist);
|
||||
|
||||
return execute_query1(conn, sql, false);
|
||||
return execute_query_silent(conn, sql, false);
|
||||
}
|
||||
|
||||
int execute_query_from_file(MYSQL* conn, FILE* file)
|
||||
@ -175,7 +123,7 @@ int execute_query_from_file(MYSQL *conn, FILE *file)
|
||||
|
||||
if (*ptr)
|
||||
{
|
||||
rc = execute_query1(conn, buf, false);
|
||||
rc = execute_query_silent(conn, buf, false);
|
||||
}
|
||||
|
||||
}
|
||||
@ -188,12 +136,7 @@ int execute_query_from_file(MYSQL *conn, FILE *file)
|
||||
return rc;
|
||||
}
|
||||
|
||||
int execute_query_silent(MYSQL *conn, const char *sql)
|
||||
{
|
||||
return execute_query1(conn, sql, true);
|
||||
}
|
||||
|
||||
int execute_query1(MYSQL *conn, const char *sql, bool silent)
|
||||
int execute_query_silent(MYSQL* conn, const char* sql, bool silent)
|
||||
{
|
||||
MYSQL_RES *res;
|
||||
if (conn != NULL)
|
||||
@ -329,7 +272,7 @@ int execute_query_affected_rows(MYSQL *conn, const char *sql, my_ulonglong * aff
|
||||
}
|
||||
}
|
||||
|
||||
int execute_query_num_of_rows(MYSQL *conn, const char *sql, my_ulonglong num_of_rows[],
|
||||
int execute_query_num_of_rows(MYSQL* conn, const char* sql, my_ulonglong* num_of_rows,
|
||||
unsigned long long* i)
|
||||
{
|
||||
MYSQL_RES *res;
|
||||
@ -376,7 +319,7 @@ int execute_query_num_of_rows(MYSQL *conn, const char *sql, my_ulonglong num_of_
|
||||
}
|
||||
}
|
||||
|
||||
int execute_stmt_num_of_rows(MYSQL_STMT * stmt, my_ulonglong num_of_rows[], unsigned long long * i)
|
||||
int execute_stmt_num_of_rows(MYSQL_STMT* stmt, my_ulonglong* num_of_rows, unsigned long long* i)
|
||||
{
|
||||
my_ulonglong N;
|
||||
|
||||
@ -456,7 +399,7 @@ int execute_query_count_rows(MYSQL *conn, const char *sql)
|
||||
return rval;
|
||||
}
|
||||
|
||||
int get_conn_num(MYSQL *conn, char * ip, char *hostname, char * db)
|
||||
int get_conn_num(MYSQL* conn, std::string ip, std::string hostname, std::string db)
|
||||
{
|
||||
MYSQL_RES *res;
|
||||
MYSQL_ROW row;
|
||||
@ -466,13 +409,13 @@ int get_conn_num(MYSQL *conn, char * ip, char *hostname, char * db)
|
||||
unsigned int conn_num = 0;
|
||||
const char* hostname_internal;
|
||||
|
||||
if (strcmp(ip, "127.0.0.1") == 0)
|
||||
if (ip == "127.0.0.1")
|
||||
{
|
||||
hostname_internal = "localhost";
|
||||
}
|
||||
else
|
||||
{
|
||||
hostname_internal = hostname;
|
||||
hostname_internal = hostname.c_str();
|
||||
}
|
||||
|
||||
if (conn != NULL)
|
||||
@ -500,11 +443,11 @@ int get_conn_num(MYSQL *conn, char * ip, char *hostname, char * db)
|
||||
row = mysql_fetch_row(res);
|
||||
if ( (row[2] != NULL ) && (row[3] != NULL) )
|
||||
{
|
||||
if ((strstr(row[2], ip) != NULL) && (strstr(row[3], db) != NULL))
|
||||
if ((strstr(row[2], ip.c_str()) != NULL) && (strstr(row[3], db.c_str()) != NULL))
|
||||
{
|
||||
conn_num++;
|
||||
}
|
||||
if ((strstr(row[2], hostname_internal) != NULL) && (strstr(row[3], db) != NULL))
|
||||
if ((strstr(row[2], hostname_internal) != NULL) && (strstr(row[3], db.c_str()) != NULL))
|
||||
{
|
||||
conn_num++;
|
||||
}
|
||||
@ -514,7 +457,7 @@ int get_conn_num(MYSQL *conn, char * ip, char *hostname, char * db)
|
||||
mysql_free_result(res);
|
||||
}
|
||||
}
|
||||
if (strcmp(ip, "127.0.0.1") == 0)
|
||||
if (ip == "127.0.0.1")
|
||||
{
|
||||
// one extra connection is visible in the process list
|
||||
// output in case of local test
|
||||
@ -579,66 +522,7 @@ int find_field(MYSQL *conn, const char *sql, const char *field_name, char * valu
|
||||
return ret;
|
||||
}
|
||||
|
||||
unsigned int get_seconds_behind_master(MYSQL *conn)
|
||||
{
|
||||
char SBM_str[16];
|
||||
unsigned int SBM = 0;
|
||||
if (find_field(
|
||||
conn, (char *) "show slave status;",
|
||||
(char *) "Seconds_Behind_Master", &SBM_str[0]
|
||||
) != 1)
|
||||
{
|
||||
sscanf(SBM_str, "%u", &SBM);
|
||||
}
|
||||
return SBM;
|
||||
}
|
||||
|
||||
int read_log(char * name, char ** err_log_content_p)
|
||||
{
|
||||
FILE *f;
|
||||
*err_log_content_p = NULL;
|
||||
char * err_log_content;
|
||||
f = fopen(name, "rb");
|
||||
if (f != NULL)
|
||||
{
|
||||
|
||||
int prev = ftell(f);
|
||||
fseek(f, 0L, SEEK_END);
|
||||
long int size = ftell(f);
|
||||
fseek(f, prev, SEEK_SET);
|
||||
err_log_content = (char *)malloc(size + 2);
|
||||
if (err_log_content != NULL)
|
||||
{
|
||||
fread(err_log_content, 1, size, f);
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
if (err_log_content[i] == 0)
|
||||
{
|
||||
//printf("null detected at position %d\n", i);
|
||||
err_log_content[i] = '\n';
|
||||
}
|
||||
}
|
||||
//printf("s=%ld\n", strlen(err_log_content));
|
||||
err_log_content[size] = '\0';
|
||||
//printf("s=%ld\n", strlen(err_log_content));
|
||||
* err_log_content_p = err_log_content;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error allocationg memory for the log\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf ("Error reading log %s \n", name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int get_int_version(const std::string& version)
|
||||
int get_int_version(std::string version)
|
||||
{
|
||||
std::istringstream str(version);
|
||||
int major = 0;
|
||||
@ -649,8 +533,3 @@ int get_int_version(const std::string& version)
|
||||
str >> major >> dot >> minor >> dot >> patch;
|
||||
return major * 10000 + minor * 100 + patch;
|
||||
}
|
||||
|
||||
int get_int_version(const char* version)
|
||||
{
|
||||
return get_int_version(std::string(version));
|
||||
}
|
||||
|
||||
@ -31,13 +31,14 @@
|
||||
* @param port DB server port
|
||||
* @param ip DB server IP address
|
||||
* @param db name of DB to connect
|
||||
* @param User User name
|
||||
* @param Password Password
|
||||
* @param user user name
|
||||
* @param password password
|
||||
* @param flag Connections flags
|
||||
* @param ssl true if ssl should be used
|
||||
* @return MYSQL struct or NULL in case of error
|
||||
*
|
||||
* @return MYSQL struct
|
||||
*/
|
||||
MYSQL * open_conn_db_flags(int port, const char* ip, const char* db, const char* User, const char* Password,
|
||||
MYSQL* open_conn_db_flags(int port, std::string ip, std::string db, std::string user, std::string password,
|
||||
unsigned long flag, bool ssl);
|
||||
|
||||
|
||||
@ -47,18 +48,15 @@ MYSQL * open_conn_db_flags(int port, const char* ip, const char* db, const char*
|
||||
* @param port DB server port
|
||||
* @param ip DB server IP address
|
||||
* @param db name of DB to connect
|
||||
* @param User User name
|
||||
* @param Password Password
|
||||
* @param user user name
|
||||
* @param password password
|
||||
* @param timeout timeout on seconds
|
||||
* @param ssl true if ssl should be used
|
||||
* @return MYSQL struct or NULL in case of error
|
||||
*
|
||||
* @return MYSQL struct
|
||||
*/
|
||||
MYSQL * open_conn_db_timeout(int port, const char* ip, const char* db, const char* User, const char* Password,
|
||||
unsigned long timeout, bool ssl);
|
||||
|
||||
MYSQL* open_conn_db_timeout(int port, const std::string& ip, const std::string& db,
|
||||
const std::string& user, const std::string& password,
|
||||
unsigned long timeout, bool ssl);
|
||||
MYSQL* open_conn_db_timeout(int port, std::string ip, std::string db, std::string user, std::string password,
|
||||
unsigned int timeout, bool ssl);
|
||||
|
||||
/**
|
||||
* Opens connection to DB with default flags
|
||||
@ -66,46 +64,49 @@ MYSQL* open_conn_db_timeout(int port, const std::string& ip, const std::string&
|
||||
* @param port DB server port
|
||||
* @param ip DB server IP address
|
||||
* @param db name of DB to connect
|
||||
* @param User User name
|
||||
* @param Password Password
|
||||
* @param user user name
|
||||
* @param password password
|
||||
* @param ssl true if ssl should be used
|
||||
* @return MYSQL struct or NULL in case of error
|
||||
*
|
||||
* @return MYSQL struct
|
||||
*/
|
||||
MYSQL * open_conn_db(int port, const char* ip, const char* db, const char* User, const char* Password,
|
||||
bool ssl);
|
||||
|
||||
static MYSQL* open_conn_db(int port, std::string ip, std::string db, std::string user, std::string password,
|
||||
bool ssl)
|
||||
{
|
||||
return open_conn_db_flags(port, ip, db, user, password, CLIENT_MULTI_STATEMENTS, ssl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens connection to 'test' with default flags
|
||||
*
|
||||
* @param port DB server port
|
||||
* @param ip DB server IP address
|
||||
* @param User User name
|
||||
* @param Password Password
|
||||
* @param user user name
|
||||
* @param password password
|
||||
* @param ssl true if ssl should be used
|
||||
* @return MYSQL struct or NULL in case of error
|
||||
*
|
||||
* @return MYSQL struct
|
||||
*/
|
||||
MYSQL * open_conn(int port, const char* ip, const char* User, const char* Password, bool ssl);
|
||||
static MYSQL* open_conn(int port, std::string ip, std::string user, std::string password, bool ssl)
|
||||
{
|
||||
return open_conn_db(port, ip.c_str(), "test", user.c_str(), password.c_str(), ssl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens connection to with default flags without defning DB name (just conecto server)
|
||||
*
|
||||
* @param port DB server port
|
||||
* @param ip DB server IP address
|
||||
* @param User User name
|
||||
* @param Password Password
|
||||
* @param user user name
|
||||
* @param password password
|
||||
* @param ssl true if ssl should be used
|
||||
* @return MYSQL struct or NULL in case of error
|
||||
*
|
||||
* @return MYSQL struct
|
||||
*/
|
||||
MYSQL * open_conn_no_db(int port, const char* ip, const char* User, const char* Password, bool ssl);
|
||||
|
||||
/**
|
||||
* @brief set_ssl Configure SSL for given connection
|
||||
* Function assumes that certificates are in test_dir/ssl-cert/ directory
|
||||
* @param conn MYSQL handler
|
||||
* @return return of mysql_ssl_set() (always 0, see mysql_ssl_set() documentation)
|
||||
*/
|
||||
int set_ssl(MYSQL * conn);
|
||||
static MYSQL* open_conn_no_db(int port, std::string ip, std::string user, std::string password, bool ssl)
|
||||
{
|
||||
return open_conn_db_flags(port, ip, NULL, user, password, CLIENT_MULTI_STATEMENTS, ssl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Executes SQL query. Function also executes mysql_store_result() and mysql_free_result() to clean up returns
|
||||
@ -130,17 +131,7 @@ int execute_query_from_file(MYSQL *conn, FILE *file);
|
||||
* @param sql SQL string
|
||||
* @return 0 in case of success
|
||||
*/
|
||||
int execute_query_silent(MYSQL *conn, const char *sql);
|
||||
|
||||
/**
|
||||
* @brief Executes SQL query. Function also executes mysql_store_result() and mysql_free_result() to clean up returns
|
||||
* This function do not support 'printf' format for sql (in compare with execute_query()
|
||||
* @param conn MYSQL connection struct
|
||||
* @param sql SQL string
|
||||
* @param silent if true function do not produce any printing
|
||||
* @return 0 in case of success
|
||||
*/
|
||||
int execute_query1(MYSQL *conn, const char *sql, bool silent);
|
||||
int execute_query_silent(MYSQL* conn, const char* sql, bool silent = true);
|
||||
|
||||
/**
|
||||
* @brief Executes SQL query and store 'affected rows' number in affectet_rows parameter
|
||||
@ -170,7 +161,7 @@ int execute_query_count_rows(MYSQL *conn, const char *sql);
|
||||
* @param i pointer to variable to store number of result sets
|
||||
* @return 0 in case of success
|
||||
*/
|
||||
int execute_query_num_of_rows(MYSQL *conn, const char *sql, my_ulonglong num_of_rows[],
|
||||
int execute_query_num_of_rows(MYSQL* conn, const char* sql, my_ulonglong* num_of_rows,
|
||||
unsigned long long *i);
|
||||
|
||||
/**
|
||||
@ -182,7 +173,7 @@ int execute_query_num_of_rows(MYSQL *conn, const char *sql, my_ulonglong num_of_
|
||||
* @param i pointer to variable to store number of result sets
|
||||
* @return 0 in case of success
|
||||
*/
|
||||
int execute_stmt_num_of_rows(MYSQL_STMT *stmt, my_ulonglong num_of_rows[], unsigned long long * i);
|
||||
int execute_stmt_num_of_rows(MYSQL_STMT* stmt, my_ulonglong* num_of_rows, unsigned long long * i);
|
||||
|
||||
/**
|
||||
* @brief execute_query_check_one Executes query and check if first field of first row is equal to 'expected'
|
||||
@ -200,7 +191,7 @@ int execute_query_check_one(MYSQL *conn, const char *sql, const char *expected);
|
||||
* @param db name of DB to which connections are counted
|
||||
* @return number of connections
|
||||
*/
|
||||
int get_conn_num(MYSQL *conn, char * ip, char * hostname, char * db);
|
||||
int get_conn_num(MYSQL* conn, std::string ip, std::string hostname, std::string db);
|
||||
|
||||
/**
|
||||
* @brief Find given filed in the SQL query reply
|
||||
@ -213,23 +204,6 @@ int get_conn_num(MYSQL *conn, char * ip, char * hostname, char * db);
|
||||
*/
|
||||
int find_field(MYSQL* conn, const char* sql, const char* field_name, char* value);
|
||||
|
||||
/**
|
||||
* @brief Return the value of SECONDS_BEHIND_MASTER
|
||||
* @param conn MYSQL connection struct
|
||||
* @return value of SECONDS_BEHIND_MASTER
|
||||
*/
|
||||
unsigned int get_seconds_behind_master(MYSQL *conn);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Read MaxScale log file
|
||||
* @param name Name of log file (full path)
|
||||
* @param err_log_content pointer to the buffer to store log file content
|
||||
* @return 0 in case of success, 1 in case of error
|
||||
*/
|
||||
int read_log(char * name, char **err_log_content_p);
|
||||
|
||||
int get_int_version(const std::string& version);
|
||||
int get_int_version(const char* version);
|
||||
int get_int_version(std::string version);
|
||||
|
||||
#endif // MARIADB_FUNC_H
|
||||
|
||||
@ -1057,6 +1057,51 @@ void TestConnections::log_excludes(int m, const char* pattern)
|
||||
add_result(log_matches(m, pattern), "Log matches pattern '%s'", pattern);
|
||||
}
|
||||
|
||||
static int read_log(const char* name, char **err_log_content_p)
|
||||
{
|
||||
FILE *f;
|
||||
*err_log_content_p = NULL;
|
||||
char * err_log_content;
|
||||
f = fopen(name, "rb");
|
||||
if (f != NULL)
|
||||
{
|
||||
|
||||
int prev = ftell(f);
|
||||
fseek(f, 0L, SEEK_END);
|
||||
long int size = ftell(f);
|
||||
fseek(f, prev, SEEK_SET);
|
||||
err_log_content = (char *)malloc(size + 2);
|
||||
if (err_log_content != NULL)
|
||||
{
|
||||
fread(err_log_content, 1, size, f);
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
if (err_log_content[i] == 0)
|
||||
{
|
||||
//printf("null detected at position %d\n", i);
|
||||
err_log_content[i] = '\n';
|
||||
}
|
||||
}
|
||||
//printf("s=%ld\n", strlen(err_log_content));
|
||||
err_log_content[size] = '\0';
|
||||
//printf("s=%ld\n", strlen(err_log_content));
|
||||
* err_log_content_p = err_log_content;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Error allocationg memory for the log\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf ("Error reading log %s \n", name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TestConnections::check_log_err(int m, const char * err_msg, bool expected)
|
||||
{
|
||||
|
||||
@ -1698,7 +1743,7 @@ int TestConnections::try_query(MYSQL *conn, const char *format, ...)
|
||||
vsnprintf(sql, sizeof(sql), format, valist);
|
||||
va_end(valist);
|
||||
|
||||
int res = execute_query1(conn, sql, false);
|
||||
int res = execute_query_silent(conn, sql, false);
|
||||
add_result(res, "Query '%.*s%s' failed!\n", message_len < 100 ? message_len : 100, sql,
|
||||
message_len < 100 ? "" : "...");
|
||||
return res;
|
||||
|
||||
Reference in New Issue
Block a user