MySQL 8.0.40
Source Code Documentation
rpl_sys_table_access.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef RPL_SYS_TABLE_ACCESS_INCLUDED
25#define RPL_SYS_TABLE_ACCESS_INCLUDED
26
27#include "sql/field.h"
28#include "sql/sql_class.h"
29#include "sql/table.h"
30#include "thr_lock.h"
31
32class Json_wrapper;
33class THD;
34struct TABLE;
35
36/**
37 @class Rpl_sys_table_access
38
39 The class is used to simplify table data access. It creates new thread/session
40 context (THD) and open table on class object creation, and destroys thread and
41 closes all open thread tables on class object destruction.
42*/
44 public:
45 /**
46 Construction.
47 @param[in] schema_name Database where the table resides
48 @param[in] table_name Table to be opened
49 @param[in] max_num_field Maximum number of fields
50 */
51 Rpl_sys_table_access(const std::string &schema_name,
52 const std::string &table_name, uint max_num_field);
53
54 /**
55 Destruction. All opened tables with the open_tables are closed during
56 destruction if not already done in deinit().
57 */
59
60 /**
61 Creates new thread/session context (THD) and open's table on class object
62 creation.
63
64 @param[in] lock_type How to lock the table
65
66 @retval true if there is error
67 @retval false if there is no error
68 */
69 bool open(enum thr_lock_type lock_type);
70
71 /**
72 All opened tables with the open_tables are closed and removes
73 THD created in close().
74
75 @param[in] error State that there was a error on the table
76 operations
77 @param[in] ignore_global_read_lock
78 State that the global_read_lock must be
79 ignored
80
81 @retval true if there is error
82 @retval false if there is no error
83 */
84 bool close(bool error, bool ignore_global_read_lock = false);
85
86 /**
87 Get TABLE object created for the table access purposes.
88
89 @return TABLE pointer.
90 */
92
93 /**
94 Set error.
95 */
96 void set_error() { m_error = true; }
97
98 /**
99 Verify if error is set.
100
101 @retval true if there is error
102 @retval false if there is no error
103 */
104 bool get_error() { return m_error; }
105
106 /**
107 Stores provided string to table's field.
108
109 @param[in] field Field class object
110 @param[in] fld The std::string value to be saved.
111 @param[in] cs Charset info
112
113 @retval true Error
114 @retval false Success
115 */
116 bool store_field(Field *field, std::string fld,
118
119 /**
120 Stores provided integer to table's field.
121
122 @param[in] field Field class object
123 @param[in] fld The long long value to be saved.
124 @param[in] unsigned_val If value is unsigned.
125
126 @retval true Error
127 @retval false Success
128 */
129 bool store_field(Field *field, long long fld, bool unsigned_val = true);
130
131 /**
132 Stores provided Json to table's field.
133
134 @param[in] field Field class object
135 @param[in] wrapper The Json_wrapper class object value
136
137 @retval true Error
138 @retval false Success
139 */
140 bool store_field(Field *field, const Json_wrapper &wrapper);
141
142 /**
143 Retrieves string field from provided table's field.
144
145 @param[in] field Field class object
146 @param[in] fld The std::string value to be retrieved.
147 @param[in] cs Charset info
148
149 @retval true Error
150 @retval false Success
151 */
152 bool get_field(Field *field, std::string &fld,
154
155 /**
156 Retrieves long long integer field from provided table's field.
157
158 @param[in] field Field class object
159 @param[in] fld The uint value to be retrieved.
160
161 @retval true Error
162 @retval false Success
163 */
164 bool get_field(Field *field, uint &fld);
165
166 /**
167 Retrieves Json field from provided table's field.
168
169 @param[in] field Field class object
170 @param[in] wrapper The retrieved field would be saved in
171 Json_wrapper format.
172
173 @retval true Error
174 @retval false Success
175 */
176 bool get_field(Field *field, Json_wrapper &wrapper);
177
178 std::string get_field_error_msg(std::string field_name) const;
179
180 static void handler_write_row_func(Rpl_sys_table_access &table_op,
181 bool &err_val, std::string &err_msg,
182 uint table_index = 0,
183 key_part_map keypart_map = HA_WHOLE_KEY);
184
185 static void handler_delete_row_func(Rpl_sys_table_access &table_op,
186 bool &err_val, std::string &err_msg,
187 uint table_index = 0,
188 key_part_map keypart_map = HA_WHOLE_KEY);
189
190 template <class F, class... Ts, std::size_t... Is>
191 static void for_each_in_tuple(std::tuple<Ts...> &tuple, F func,
192 std::index_sequence<Is...>) {
193 using tuple_list = int[255];
194 (void)tuple_list{0, ((void)func(Is, std::get<Is>(tuple)), 0)...};
195 }
196
197 template <class F, class... Ts>
198 static void for_each_in_tuple(std::tuple<Ts...> &tuple, F func) {
199 for_each_in_tuple(tuple, func, std::make_index_sequence<sizeof...(Ts)>());
200 }
201
202 template <class F, class... Ts, std::size_t... Is>
203 static void for_each_in_tuple(const std::tuple<Ts...> &tuple, F func,
204 std::index_sequence<Is...>) {
205 using tuple_list = int[255];
206 (void)tuple_list{0, ((void)func(Is, std::get<Is>(tuple)), 0)...};
207 }
208
209 template <class F, class... Ts>
210 static void for_each_in_tuple(const std::tuple<Ts...> &tuple, F func) {
211 for_each_in_tuple(tuple, func, std::make_index_sequence<sizeof...(Ts)>());
212 }
213
214 /**
215 Delete all rows on `m_schema_name.m_table_name`.
216
217 @retval true if there is error
218 @retval false if there is no error
219 */
220 bool delete_all_rows();
221
222 /**
223 Return the version stored on `m_schema_version_name.m_table_version_name`
224 for the `m_schema_name.m_table_name` table.
225
226 @retval 0 if there is error
227 @retval >0 if there is no error
228 */
230
231 /**
232 Increment the version stored on `m_schema_version_name.m_table_version_name`
233 for the `m_schema_name.m_table_name` table.
234
235 @retval true if there is error
236 @retval false if there is no error
237 */
238 bool increment_version();
239
240 /**
241 Update the version stored on `m_schema_version_name.m_table_version_name`
242 for the `m_schema_name.m_table_name` table.
243
244 @param[in] version the version value
245
246 @retval true if there is error
247 @retval false if there is no error
248 */
250
251 /**
252 Delete the version stored on `m_schema_version_name.m_table_version_name`
253 for the `m_schema_name.m_table_name` table.
254
255 @retval true if there is error
256 @retval false if there is no error
257 */
258 bool delete_version();
259
260 /**
261 Get database name of table accessed.
262
263 @return database name.
264 */
265 std::string get_db_name() { return m_schema_name; }
266
267 /**
268 Get table name of table accessed.
269
270 @return table name.
271 */
272 std::string get_table_name() { return m_table_name; }
273
274 private:
275 /* THD created for TableAccess object purpose. */
276 THD *m_thd{nullptr};
277
278 /* THD associated with the calling thread. */
280
281 /* The variable determine if table is opened or closed successfully. */
282 bool m_error{false};
283
284 /* Table_ref object */
287
288 std::string m_schema_name;
289 std::string m_table_name;
291
292 const std::string m_schema_version_name{"mysql"};
293 const std::string m_table_version_name{
294 "replication_group_configuration_version"};
298};
299
300#endif /* RPL_SYS_TABLE_ACCESS_INCLUDED */
Definition: field.h:575
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1161
The class are wrappers for handler index and random scan functions to simplify their usage.
Definition: rpl_sys_table_access.h:43
static void for_each_in_tuple(const std::tuple< Ts... > &tuple, F func, std::index_sequence< Is... >)
Definition: rpl_sys_table_access.h:203
static void for_each_in_tuple(std::tuple< Ts... > &tuple, F func, std::index_sequence< Is... >)
Definition: rpl_sys_table_access.h:191
bool delete_all_rows()
Delete all rows on m_schema_name.m_table_name.
Definition: rpl_sys_table_access.cc:264
~Rpl_sys_table_access()
Destruction.
Definition: rpl_sys_table_access.cc:49
std::string get_table_name()
Get table name of table accessed.
Definition: rpl_sys_table_access.h:272
std::string m_schema_name
Definition: rpl_sys_table_access.h:288
const uint m_table_data_index
Definition: rpl_sys_table_access.h:295
bool get_error()
Verify if error is set.
Definition: rpl_sys_table_access.h:104
Rpl_sys_table_access(const std::string &schema_name, const std::string &table_name, uint max_num_field)
Construction.
Definition: rpl_sys_table_access.cc:41
const uint m_table_list_size
Definition: rpl_sys_table_access.h:297
std::string m_table_name
Definition: rpl_sys_table_access.h:289
std::string get_db_name()
Get database name of table accessed.
Definition: rpl_sys_table_access.h:265
uint m_max_num_field
Definition: rpl_sys_table_access.h:290
bool open(enum thr_lock_type lock_type)
Creates new thread/session context (THD) and open's table on class object creation.
Definition: rpl_sys_table_access.cc:103
THD * m_current_thd
Definition: rpl_sys_table_access.h:279
static void handler_write_row_func(Rpl_sys_table_access &table_op, bool &err_val, std::string &err_msg, uint table_index=0, key_part_map keypart_map=HA_WHOLE_KEY)
Definition: rpl_sys_table_access.cc:211
bool delete_version()
Delete the version stored on m_schema_version_name.m_table_version_name for the m_schema_name....
Definition: rpl_sys_table_access.cc:379
bool store_field(Field *field, std::string fld, CHARSET_INFO *cs=&my_charset_bin)
Stores provided string to table's field.
Definition: rpl_sys_table_access.cc:51
std::string get_field_error_msg(std::string field_name) const
Definition: rpl_sys_table_access.cc:256
static void handler_delete_row_func(Rpl_sys_table_access &table_op, bool &err_val, std::string &err_msg, uint table_index=0, key_part_map keypart_map=HA_WHOLE_KEY)
Definition: rpl_sys_table_access.cc:224
const uint m_table_version_index
Definition: rpl_sys_table_access.h:296
bool close(bool error, bool ignore_global_read_lock=false)
All opened tables with the open_tables are closed and removes THD created in close().
Definition: rpl_sys_table_access.cc:173
enum thr_lock_type m_lock_type
Definition: rpl_sys_table_access.h:286
ulonglong get_version()
Return the version stored on m_schema_version_name.m_table_version_name for the m_schema_name....
Definition: rpl_sys_table_access.cc:357
static void for_each_in_tuple(const std::tuple< Ts... > &tuple, F func)
Definition: rpl_sys_table_access.h:210
bool get_field(Field *field, std::string &fld, CHARSET_INFO *cs=&my_charset_bin)
Retrieves string field from provided table's field.
Definition: rpl_sys_table_access.cc:76
const std::string m_table_version_name
Definition: rpl_sys_table_access.h:293
THD * m_thd
Definition: rpl_sys_table_access.h:276
static void for_each_in_tuple(std::tuple< Ts... > &tuple, F func)
Definition: rpl_sys_table_access.h:198
bool m_error
Definition: rpl_sys_table_access.h:282
bool update_version(ulonglong version)
Update the version stored on m_schema_version_name.m_table_version_name for the m_schema_name....
Definition: rpl_sys_table_access.cc:324
TABLE * get_table()
Get TABLE object created for the table access purposes.
Definition: rpl_sys_table_access.cc:203
bool increment_version()
Increment the version stored on m_schema_version_name.m_table_version_name for the m_schema_name....
Definition: rpl_sys_table_access.cc:287
void set_error()
Set error.
Definition: rpl_sys_table_access.h:96
const std::string m_schema_version_name
Definition: rpl_sys_table_access.h:292
Table_ref * m_table_list
Definition: rpl_sys_table_access.h:285
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Definition: table.h:2791
MYSQL_PLUGIN_IMPORT CHARSET_INFO my_charset_bin
Definition: ctype-bin.cc:511
#define HA_WHOLE_KEY
Definition: my_base.h:1008
ulong key_part_map
Definition: my_base.h:1007
unsigned long long int ulonglong
Definition: my_inttypes.h:56
Definition: commit_order_queue.h:34
const char * table_name
Definition: rules_table_service.cc:56
required uint64 version
Definition: replication_group_member_actions.proto:41
Definition: m_ctype.h:385
Definition: table.h:1399
thr_lock_type
Definition: thr_lock.h:51
unsigned int uint
Definition: uca9-dump.cc:75