MySQL 9.7.0
Source Code Documentation
rpl_mysql_connect.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2026, 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 /**
112 Checks whether the connected source server's version is
113 compatible with the replica's version.
114
115 @retval true The source and replica versions are compatible.
116 false The versions are incompatible or the connection
117 is not initialized.
118 */
119 [[nodiscard]] bool version_compatible() const;
120
121 private:
122 /* MySQL client connection object */
123 MYSQL *m_conn{nullptr};
124
125 /* The flag which stores if its connected to mysql server. */
126 bool m_connected{false};
127
128 /* The flag which stores if its initialized. */
129 bool m_init{false};
130
131 /* The THD object. */
132 THD *m_thd{nullptr};
133
134 /* The Master_info object. */
135 Master_info *m_mi{nullptr};
136
137 /* The host or ip address for mysql client connection. */
138 std::string m_host;
139
140 /* The port for mysql client connection. */
141 uint m_port{0};
142
143 /* The network_namespace for mysql client connection. */
145
146 /* The flag to determine its IO or Monitor IO thread. */
147 bool m_is_io_thread{false};
148
149 /**
150 To connect to mysql server.
151
152 @param[in] thd The thread object.
153 @param[in] mi the pointer to the Master_info object.
154 @param[in] host the host or ip address for mysql client connection.
155 @param[in] port the port for mysql client connection.
156 @param[in] network_namespace the network_namespace for mysql client
157 connection.
158 */
159 bool safe_connect(THD *thd, Master_info *mi, std::string host, uint port,
160 std::string network_namespace);
161
162 /**
163 To re-connect to mysql server.
164
165 @param[in] thd The thread object.
166 @param[in] mi the pointer to the Master_info object.
167 @param[in] suppress_warnings Suppress reconnect warning
168 @param[in] host the host or ip address for mysql client connection.
169 @param[in] port the port for mysql client connection.
170 */
171 bool safe_reconnect(THD *thd, Master_info *mi, bool suppress_warnings,
172 std::string host, uint port);
173};
174#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:147
bool m_connected
Definition: rpl_mysql_connect.h:126
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:144
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:138
MYSQL_RES_TUPLE execute_query(std::string query) const
Execute given sql query on connected mysql server.
Definition: rpl_mysql_connect.cc:119
MYSQL * m_conn
Definition: rpl_mysql_connect.h:123
THD * m_thd
Definition: rpl_mysql_connect.h:132
bool version_compatible() const
Checks whether the connected source server's version is compatible with the replica's version.
Definition: rpl_mysql_connect.cc:113
~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:135
uint m_port
Definition: rpl_mysql_connect.h:141
bool m_init
Definition: rpl_mysql_connect.h:129
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:66
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