From 56f274d74a010f3733c94ce9ca90a3ae65222830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Sun, 24 Jun 2018 22:01:41 +0300 Subject: [PATCH] Add Connection helper class This is intended to make querying a "server" easier by wrapping common functionality into one class. --- maxscale-system-test/mariadb_func.h | 73 +++++++++++++++++++++++++++++ maxscale-system-test/maxscales.h | 24 ++++++++++ 2 files changed, 97 insertions(+) diff --git a/maxscale-system-test/mariadb_func.h b/maxscale-system-test/mariadb_func.h index 431fe5e9e..a235af7bb 100644 --- a/maxscale-system-test/mariadb_func.h +++ b/maxscale-system-test/mariadb_func.h @@ -232,4 +232,77 @@ Result get_result(MYSQL* conn, std::string sql); int get_int_version(std::string version); +// Helper class for performing queries +class Connection +{ +public: + Connection(Connection&) = delete; + Connection& operator=(Connection&) = delete; + + Connection(std::string host, int port, std::string user, std::string password, std::string db = "", bool ssl = false): + m_host(host), + m_port(port), + m_user(user), + m_pw(password), + m_db(db), + m_ssl(ssl) + { + } + + Connection(Connection&& rhs): + m_host(rhs.m_host), + m_port(rhs.m_port), + m_user(rhs.m_user), + m_pw(rhs.m_pw), + m_db(rhs.m_db), + m_ssl(rhs.m_ssl), + m_conn(rhs.m_conn) + { + rhs.m_conn = nullptr; + } + + virtual ~Connection() + { + mysql_close(m_conn); + } + + bool connect() + { + mysql_close(m_conn); + m_conn = open_conn_db(m_port, m_host, m_db, m_user, m_pw, m_ssl); + return m_conn != nullptr; + } + + void disconnect() + { + mysql_close(m_conn); + m_conn = nullptr; + } + + bool query(std::string q) + { + return execute_query_silent(m_conn, q.c_str()) == 0; + } + + bool check(std::string q, std::string res) + { + Row row = get_row(m_conn, q); + return !row.empty() && row[0] == res; + } + + const char* error() const + { + return mysql_error(m_conn); + } + +private: + std::string m_host; + int m_port; + std::string m_user; + std::string m_pw; + std::string m_db; + bool m_ssl; + MYSQL* m_conn = nullptr; +}; + #endif // MARIADB_FUNC_H diff --git a/maxscale-system-test/maxscales.h b/maxscale-system-test/maxscales.h index 5d9a9518f..a156dff97 100644 --- a/maxscale-system-test/maxscales.h +++ b/maxscale-system-test/maxscales.h @@ -156,6 +156,14 @@ public: return open_conn(rwsplit_port[m], IP[m], user_name, password, ssl); } + /** + * Get a readwritesplit Connection + */ + Connection rwsplit(int m = 0) + { + return Connection(IP[m], rwsplit_port[m], user_name, password, "test", ssl); + } + /** * @brief OpenReadMasterConn Opens new connections to ReadConn master and returns MYSQL struct * To close connection mysql_close() have to be called @@ -167,6 +175,14 @@ public: password, ssl); } + /** + * Get a readconnroute master Connection + */ + Connection readconn_master(int m = 0) + { + return Connection(IP[m], readconn_master_port[m], user_name, password, "test", ssl); + } + /** * @brief OpenReadSlaveConn Opens new connections to ReadConn slave and returns MYSQL struct * To close connection mysql_close() have to be called @@ -178,6 +194,14 @@ public: password, ssl); } + /** + * Get a readconnroute slave Connection + */ + Connection readconn_slave(int m = 0) + { + return Connection(IP[m], readconn_slave_port[m], user_name, password, "test", ssl); + } + /** * @brief CloseRWSplit Closes RWplit connections stored in maxscales->conn_rwsplit[0] */