MySQL 9.0.0
Source Code Documentation
rpl_mysql_connect.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef RPL_MYSQL_CONNECT
25#define RPL_MYSQL_CONNECT
26
27#include <string>
28#include <vector>
29#include "include/mysql.h"
30#include "sql/rpl_mi.h"
31
32/**
33 result of executed query in rows<cols<value>> format where rows and cols
34 both are std::vector and value is std::string.
35*/
36using MYSQL_RES_VAL = std::vector<std::vector<std::string>>;
37
38/**
39 std::tuple<error number, result>
40 where
41 first element of tuple is function return value and determines:
42 0 Successful
43 !0 Error
44
45 second element of tuple is result of executed query in rows<cols<value>>
46 format where rows and cols both are std::vector and value is std::string.
47*/
48using MYSQL_RES_TUPLE = std::tuple<uint, std::vector<std::vector<std::string>>>;
49
50/**
51 @class Mysql_connection
52
53 Mysql client connection wrapper class to connect MySQL, execute SQL query and
54 fetch query results.
55*/
57 public:
58 /**
59 Mysql_connection class constructor.
60
61 @param[in] thd The thread object.
62 @param[in] mi the pointer to the Master_info object.
63 @param[in] host the host or ip address for mysql client connection.
64 @param[in] port the port for mysql client connection.
65 @param[in] network_namespace the network_namespace for mysql client
66 connection.
67 @param[in] is_io_thread to determine its IO or Monitor IO thread.
68 */
69
70 Mysql_connection(THD *thd, Master_info *mi, std::string host, uint port,
71 std::string network_namespace, bool is_io_thread = false);
72
73 /**
74 Mysql_connection class destructor.
75 */
77
78 /**
79 Determine if its connected to mysql server.
80
81 @return true if connected, false otherwise.
82 */
83 bool is_connected();
84
85 /**
86 Re-connect to mysql server.
87
88 @return true if successfully reconnected, false otherwise.
89 */
90 bool reconnect();
91
92 /**
93 Get Mysql client connection object.
94
95 @return Mysql client connection object.
96 */
97 MYSQL *get_mysql() { return m_conn; }
98
99 /**
100 Execute given sql query on connected mysql server.
101
102 @param[in] query sql query to execute.
103
104 @return result of executed query in rows<cols<result>> format
105 where rows and cols both are std::vector and result
106 is std::string. So other then character strings need to
107 be converted.
108 */
109 MYSQL_RES_TUPLE execute_query(std::string query) const;
110
111 private:
112 /* MySQL client connection object */
113 MYSQL *m_conn{nullptr};
114
115 /* The flag which stores if its connected to mysql server. */
116 bool m_connected{false};
117
118 /* The flag which stores if its initialized. */
119 bool m_init{false};
120
121 /* The THD object. */
122 THD *m_thd{nullptr};
123
124 /* The Master_info object. */
125 Master_info *m_mi{nullptr};
126
127 /* The host or ip address for mysql client connection. */
128 std::string m_host;
129
130 /* The port for mysql client connection. */
131 uint m_port{0};
132
133 /* The network_namespace for mysql client connection. */
135
136 /* The flag to determine its IO or Monitor IO thread. */
137 bool m_is_io_thread{false};
138
139 /**
140 To connect to mysql server.
141
142 @param[in] thd The thread object.
143 @param[in] mi the pointer to the Master_info object.
144 @param[in] host the host or ip address for mysql client connection.
145 @param[in] port the port for mysql client connection.
146 @param[in] network_namespace the network_namespace for mysql client
147 connection.
148 */
149 bool safe_connect(THD *thd, Master_info *mi, std::string host, uint port,
150 std::string network_namespace);
151
152 /**
153 To re-connect to mysql server.
154
155 @param[in] thd The thread object.
156 @param[in] mi the pointer to the Master_info object.
157 @param[in] suppress_warnings Suppress reconnect warning
158 @param[in] host the host or ip address for mysql client connection.
159 @param[in] port the port for mysql client connection.
160 */
161 bool safe_reconnect(THD *thd, Master_info *mi, bool suppress_warnings,
162 std::string host, uint port);
163};
164#endif // RPL_MYSQL_CONNECT
Definition: rpl_mi.h:87
Mysql client connection wrapper class to connect MySQL, execute SQL query and fetch query results.
Definition: rpl_mysql_connect.h:56
bool m_is_io_thread
Definition: rpl_mysql_connect.h:137
bool m_connected
Definition: rpl_mysql_connect.h:116
Mysql_connection(THD *thd, Master_info *mi, std::string host, uint port, std::string network_namespace, bool is_io_thread=false)
Mysql_connection class constructor.
Definition: rpl_mysql_connect.cc:31
bool safe_reconnect(THD *thd, Master_info *mi, bool suppress_warnings, std::string host, uint port)
To re-connect to mysql server.
Definition: rpl_mysql_connect.cc:94
std::string m_network_namespace
Definition: rpl_mysql_connect.h:134
bool is_connected()
Determine if its connected to mysql server.
Definition: rpl_mysql_connect.cc:60
bool safe_connect(THD *thd, Master_info *mi, std::string host, uint port, std::string network_namespace)
To connect to mysql server.
Definition: rpl_mysql_connect.cc:62
std::string m_host
Definition: rpl_mysql_connect.h:128
MYSQL_RES_TUPLE execute_query(std::string query) const
Execute given sql query on connected mysql server.
Definition: rpl_mysql_connect.cc:114
MYSQL * m_conn
Definition: rpl_mysql_connect.h:113
THD * m_thd
Definition: rpl_mysql_connect.h:122
~Mysql_connection()
Mysql_connection class destructor.
Definition: rpl_mysql_connect.cc:51
bool reconnect()
Re-connect to mysql server.
Definition: rpl_mysql_connect.cc:104
Master_info * m_mi
Definition: rpl_mysql_connect.h:125
uint m_port
Definition: rpl_mysql_connect.h:131
bool m_init
Definition: rpl_mysql_connect.h:119
MYSQL * get_mysql()
Get Mysql client connection object.
Definition: rpl_mysql_connect.h:97
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
static char * query
Definition: myisam_ftdump.cc:47
This file defines the client API to MySQL and also the ABI of the dynamically linked libmysqlclient.
const char * host
Definition: mysqladmin.cc:65
required string network_namespace
Definition: replication_asynchronous_connection_failover.proto:34
required uint64 port
Definition: replication_asynchronous_connection_failover.proto:33
std::vector< std::vector< std::string > > MYSQL_RES_VAL
result of executed query in rows<cols<value>> format where rows and cols both are std::vector and val...
Definition: rpl_mysql_connect.h:36
std::tuple< uint, std::vector< std::vector< std::string > > > MYSQL_RES_TUPLE
std::tuple<error number, result> where first element of tuple is function return value and determines...
Definition: rpl_mysql_connect.h:48
Definition: mysql.h:300