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