Pass database as a parameter in connect

When connecting to a node, a database can now be optionally given as a
parameter. This makes testing with different databases easier as the need
to use the explicit functions is removed.
This commit is contained in:
Markus Mäkelä
2018-11-19 13:01:37 +02:00
parent cb67d4cee3
commit aa2572c677
4 changed files with 38 additions and 32 deletions

View File

@ -59,7 +59,7 @@ Mariadb_nodes::~Mariadb_nodes()
} }
} }
int Mariadb_nodes::connect(int i) int Mariadb_nodes::connect(int i, const std::string& db)
{ {
if (nodes[i] == NULL || mysql_ping(nodes[i]) != 0) if (nodes[i] == NULL || mysql_ping(nodes[i]) != 0)
{ {
@ -67,7 +67,7 @@ int Mariadb_nodes::connect(int i)
{ {
mysql_close(nodes[i]); mysql_close(nodes[i]);
} }
nodes[i] = open_conn_db_timeout(port[i], IP[i], "test", user_name, password, 50, ssl); nodes[i] = open_conn_db_timeout(port[i], IP[i], db.c_str(), user_name, password, 50, ssl);
} }
if ((nodes[i] != NULL) && (mysql_errno(nodes[i]) != 0)) if ((nodes[i] != NULL) && (mysql_errno(nodes[i]) != 0))
@ -80,13 +80,13 @@ int Mariadb_nodes::connect(int i)
} }
} }
int Mariadb_nodes::connect() int Mariadb_nodes::connect(const std::string& db)
{ {
int res = 0; int res = 0;
for (int i = 0; i < N; i++) for (int i = 0; i < N; i++)
{ {
res += connect(i); res += connect(i, db);
} }
return res; return res;

View File

@ -150,8 +150,8 @@ public:
*/ */
char* revert_snapshot_command; char* revert_snapshot_command;
int connect(int i); int connect(int i, const std::string& db = "test");
int connect(); int connect(const std::string& db = "test");
/** /**
* Repeatedly try to connect with one second sleep in between attempts * Repeatedly try to connect with one second sleep in between attempts

View File

@ -103,20 +103,22 @@ int Maxscales::read_env()
} }
int Maxscales::connect_rwsplit(int m) int Maxscales::connect_rwsplit(int m, const std::string& db)
{ {
if (use_ipv6) if (use_ipv6)
{ {
conn_rwsplit[m] = open_conn(rwsplit_port[m], conn_rwsplit[m] = open_conn_db(rwsplit_port[m],
IP6[m], IP6[m],
db,
user_name, user_name,
password, password,
ssl); ssl);
} }
else else
{ {
conn_rwsplit[m] = open_conn(rwsplit_port[m], conn_rwsplit[m] = open_conn_db(rwsplit_port[m],
IP[m], IP[m],
db,
user_name, user_name,
password, password,
ssl); ssl);
@ -138,20 +140,22 @@ int Maxscales::connect_rwsplit(int m)
return rc; return rc;
} }
int Maxscales::connect_readconn_master(int m) int Maxscales::connect_readconn_master(int m, const std::string& db)
{ {
if (use_ipv6) if (use_ipv6)
{ {
conn_master[m] = open_conn(readconn_master_port[m], conn_master[m] = open_conn_db(readconn_master_port[m],
IP6[m], IP6[m],
db,
user_name, user_name,
password, password,
ssl); ssl);
} }
else else
{ {
conn_master[m] = open_conn(readconn_master_port[m], conn_master[m] = open_conn_db(readconn_master_port[m],
IP[m], IP[m],
db,
user_name, user_name,
password, password,
ssl); ssl);
@ -173,20 +177,22 @@ int Maxscales::connect_readconn_master(int m)
return rc; return rc;
} }
int Maxscales::connect_readconn_slave(int m) int Maxscales::connect_readconn_slave(int m, const std::string& db)
{ {
if (use_ipv6) if (use_ipv6)
{ {
conn_slave[m] = open_conn(readconn_slave_port[m], conn_slave[m] = open_conn_db(readconn_slave_port[m],
IP6[m], IP6[m],
db,
user_name, user_name,
password, password,
ssl); ssl);
} }
else else
{ {
conn_slave[m] = open_conn(readconn_slave_port[m], conn_slave[m] = open_conn_db(readconn_slave_port[m],
IP[m], IP[m],
db,
user_name, user_name,
password, password,
ssl); ssl);
@ -208,11 +214,11 @@ int Maxscales::connect_readconn_slave(int m)
return rc; return rc;
} }
int Maxscales::connect_maxscale(int m) int Maxscales::connect_maxscale(int m, const std::string& db)
{ {
return connect_rwsplit(m) return connect_rwsplit(m, db)
+ connect_readconn_master(m) + connect_readconn_master(m, db)
+ connect_readconn_slave(m); + connect_readconn_slave(m, db);
} }
int Maxscales::close_maxscale_connections(int m) int Maxscales::close_maxscale_connections(int m)

View File

@ -114,10 +114,10 @@ public:
* maxscales->conn_slave[0] MYSQL structs * maxscales->conn_slave[0] MYSQL structs
* @return 0 in case of success * @return 0 in case of success
*/ */
int connect_maxscale(int m = 0); int connect_maxscale(int m = 0, const std::string& db = "test");
int connect(int m = 0) int connect(int m = 0, const std::string& db = "test")
{ {
return connect_maxscale(m); return connect_maxscale(m, db);
} }
/** /**
@ -135,28 +135,28 @@ public:
* maxscales->conn_rwsplit[0] * maxscales->conn_rwsplit[0]
* @return 0 in case of success * @return 0 in case of success
*/ */
int connect_rwsplit(int m = 0); int connect_rwsplit(int m = 0, const std::string& db = "test");
/** /**
* @brief ConnectReadMaster Opens connections to ReadConn master and store MYSQL struct in * @brief ConnectReadMaster Opens connections to ReadConn master and store MYSQL struct in
* maxscales->conn_master[0] * maxscales->conn_master[0]
* @return 0 in case of success * @return 0 in case of success
*/ */
int connect_readconn_master(int m = 0); int connect_readconn_master(int m = 0, const std::string& db = "test");
/** /**
* @brief ConnectReadSlave Opens connections to ReadConn slave and store MYSQL struct in * @brief ConnectReadSlave Opens connections to ReadConn slave and store MYSQL struct in
* maxscales->conn_slave[0] * maxscales->conn_slave[0]
* @return 0 in case of success * @return 0 in case of success
*/ */
int connect_readconn_slave(int m = 0); int connect_readconn_slave(int m = 0, const std::string& db = "test");
/** /**
* @brief OpenRWSplitConn Opens new connections to RWSplit and returns MYSQL struct * @brief OpenRWSplitConn Opens new connections to RWSplit and returns MYSQL struct
* To close connection mysql_close() have to be called * To close connection mysql_close() have to be called
* @return MYSQL struct * @return MYSQL struct
*/ */
MYSQL* open_rwsplit_connection(int m = 0) MYSQL* open_rwsplit_connection(int m = 0, const std::string& db = "test")
{ {
return open_conn(rwsplit_port[m], IP[m], user_name, password, ssl); return open_conn(rwsplit_port[m], IP[m], user_name, password, ssl);
} }
@ -164,9 +164,9 @@ public:
/** /**
* Get a readwritesplit Connection * Get a readwritesplit Connection
*/ */
Connection rwsplit(int m = 0) Connection rwsplit(int m = 0, const std::string& db = "test")
{ {
return Connection(IP[m], rwsplit_port[m], user_name, password, "test", ssl); return Connection(IP[m], rwsplit_port[m], user_name, password, db, ssl);
} }
/** /**
@ -186,9 +186,9 @@ public:
/** /**
* Get a readconnroute master Connection * Get a readconnroute master Connection
*/ */
Connection readconn_master(int m = 0) Connection readconn_master(int m = 0, const std::string& db = "test")
{ {
return Connection(IP[m], readconn_master_port[m], user_name, password, "test", ssl); return Connection(IP[m], readconn_master_port[m], user_name, password, db, ssl);
} }
/** /**
@ -208,9 +208,9 @@ public:
/** /**
* Get a readconnroute slave Connection * Get a readconnroute slave Connection
*/ */
Connection readconn_slave(int m = 0) Connection readconn_slave(int m = 0, const std::string& db = "test")
{ {
return Connection(IP[m], readconn_slave_port[m], user_name, password, "test", ssl); return Connection(IP[m], readconn_slave_port[m], user_name, password, db, ssl);
} }
/** /**