MySQL 8.4.2
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;
34
35struct CHARSET_INFO;
36struct TABLE;
37
38/**
39 @class Rpl_sys_table_access
40
41 The class is used to simplify table data access. It creates new thread/session
42 context (THD) and open table on class object creation, and destroys thread and
43 closes all open thread tables on class object destruction.
44*/
46 public:
47 /**
48 Construction.
49 @param[in] schema_name Database where the table resides
50 @param[in] table_name Table to be opened
51 @param[in] max_num_field Maximum number of fields
52 */
53 Rpl_sys_table_access(const std::string &schema_name,
54 const std::string &table_name, uint max_num_field);
55
56 /**
57 Destruction. All opened tables with the open_tables are closed during
58 destruction if not already done in deinit().
59 */
61
62 /**
63 Creates new thread/session context (THD) and open's table on class object
64 creation.
65
66 @param[in] lock_type How to lock the table
67
68 @retval true if there is error
69 @retval false if there is no error
70 */
71 bool open(enum thr_lock_type lock_type);
72
73 /**
74 All opened tables with the open_tables are closed and removes
75 THD created in close().
76
77 @param[in] error State that there was a error on the table
78 operations
79 @param[in] ignore_global_read_lock
80 State that the global_read_lock must be
81 ignored
82
83 @retval true if there is error
84 @retval false if there is no error
85 */
86 bool close(bool error, bool ignore_global_read_lock = false);
87
88 /**
89 Get TABLE object created for the table access purposes.
90
91 @return TABLE pointer.
92 */
94
95 /**
96 Set error.
97 */
98 void set_error() { m_error = true; }
99
100 /**
101 Verify if error is set.
102
103 @retval true if there is error
104 @retval false if there is no error
105 */
106 bool get_error() { return m_error; }
107
108 /**
109 Stores provided string to table's field.
110
111 @param[in] field Field class object
112 @param[in] fld The std::string value to be saved.
113 @param[in] cs Charset info
114
115 @retval true Error
116 @retval false Success
117 */
118 bool store_field(Field *field, std::string fld,
120
121 /**
122 Stores provided integer to table's field.
123
124 @param[in] field Field class object
125 @param[in] fld The long long value to be saved.
126 @param[in] unsigned_val If value is unsigned.
127
128 @retval true Error
129 @retval false Success
130 */
131 bool store_field(Field *field, long long fld, bool unsigned_val = true);
132
133 /**
134 Stores provided Json to table's field.
135
136 @param[in] field Field class object
137 @param[in] wrapper The Json_wrapper class object value
138
139 @retval true Error
140 @retval false Success
141 */
142 bool store_field(Field *field, const Json_wrapper &wrapper);
143
144 /**
145 Retrieves string field from provided table's field.
146
147 @param[in] field Field class object
148 @param[in] fld The std::string value to be retrieved.
149 @param[in] cs Charset info
150
151 @retval true Error
152 @retval false Success
153 */
154 bool get_field(Field *field, std::string &fld,
156
157 /**
158 Retrieves long long integer field from provided table's field.
159
160 @param[in] field Field class object
161 @param[in] fld The uint value to be retrieved.
162
163 @retval true Error
164 @retval false Success
165 */
166 bool get_field(Field *field, uint &fld);
167
168 /**
169 Retrieves Json field from provided table's field.
170
171 @param[in] field Field class object
172 @param[in] wrapper The retrieved field would be saved in
173 Json_wrapper format.
174
175 @retval true Error
176 @retval false Success
177 */
178 bool get_field(Field *field, Json_wrapper &wrapper);
179
180 std::string get_field_error_msg(std::string field_name) const;
181
182 static void handler_write_row_func(Rpl_sys_table_access &table_op,
183 bool &err_val, std::string &err_msg,
184 uint table_index = 0,
185 key_part_map keypart_map = HA_WHOLE_KEY);
186
187 static void handler_delete_row_func(Rpl_sys_table_access &table_op,
188 bool &err_val, std::string &err_msg,
189 uint table_index = 0,
190 key_part_map keypart_map = HA_WHOLE_KEY);
191
192 template <class F, class... Ts, std::size_t... Is>
193 static void for_each_in_tuple(std::tuple<Ts...> &tuple, F func,
194 std::index_sequence<Is...>) {
195 using tuple_list = int[255];
196 (void)tuple_list{0, ((void)func(Is, std::get<Is>(tuple)), 0)...};
197 }
198
199 template <class F, class... Ts>
200 static void for_each_in_tuple(std::tuple<Ts...> &tuple, F func) {
201 for_each_in_tuple(tuple, func, std::make_index_sequence<sizeof...(Ts)>());
202 }
203
204 template <class F, class... Ts, std::size_t... Is>
205 static void for_each_in_tuple(const std::tuple<Ts...> &tuple, F func,
206 std::index_sequence<Is...>) {
207 using tuple_list = int[255];
208 (void)tuple_list{0, ((void)func(Is, std::get<Is>(tuple)), 0)...};
209 }
210
211 template <class F, class... Ts>
212 static void for_each_in_tuple(const std::tuple<Ts...> &tuple, F func) {
213 for_each_in_tuple(tuple, func, std::make_index_sequence<sizeof...(Ts)>());
214 }
215
216 /**
217 Delete all rows on `m_schema_name.m_table_name`.
218
219 @retval true if there is error
220 @retval false if there is no error
221 */
222 bool delete_all_rows();
223
224 /**
225 Return the version stored on `m_schema_version_name.m_table_version_name`
226 for the `m_schema_name.m_table_name` table.
227
228 @retval 0 if there is error
229 @retval >0 if there is no error
230 */
232
233 /**
234 Increment the version stored on `m_schema_version_name.m_table_version_name`
235 for the `m_schema_name.m_table_name` table.
236
237 @retval true if there is error
238 @retval false if there is no error
239 */
240 bool increment_version();
241
242 /**
243 Update the version stored on `m_schema_version_name.m_table_version_name`
244 for the `m_schema_name.m_table_name` table.
245
246 @param[in] version the version value
247
248 @retval true if there is error
249 @retval false if there is no error
250 */
252
253 /**
254 Delete the version stored on `m_schema_version_name.m_table_version_name`
255 for the `m_schema_name.m_table_name` table.
256
257 @retval true if there is error
258 @retval false if there is no error
259 */
260 bool delete_version();
261
262 /**
263 Get database name of table accessed.
264
265 @return database name.
266 */
267 std::string get_db_name() { return m_schema_name; }
268
269 /**
270 Get table name of table accessed.
271
272 @return table name.
273 */
274 std::string get_table_name() { return m_table_name; }
275
276 private:
277 /* THD created for TableAccess object purpose. */
278 THD *m_thd{nullptr};
279
280 /* THD associated with the calling thread. */
282
283 /* The variable determine if table is opened or closed successfully. */
284 bool m_error{false};
285
286 /* Table_ref object */
289
290 std::string m_schema_name;
291 std::string m_table_name;
293
294 const std::string m_schema_version_name{"mysql"};
295 const std::string m_table_version_name{
296 "replication_group_configuration_version"};
297 const uint m_table_data_index = 0;
298 const uint m_table_version_index = 1;
299 const uint m_table_list_size = 2;
300};
301
302#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:1153
The class are wrappers for handler index and random scan functions to simplify their usage.
Definition: rpl_sys_table_access.h:45
static void for_each_in_tuple(const std::tuple< Ts... > &tuple, F func, std::index_sequence< Is... >)
Definition: rpl_sys_table_access.h:205
static void for_each_in_tuple(std::tuple< Ts... > &tuple, F func, std::index_sequence< Is... >)
Definition: rpl_sys_table_access.h:193
bool delete_all_rows()
Delete all rows on m_schema_name.m_table_name.
Definition: rpl_sys_table_access.cc:266
~Rpl_sys_table_access()
Destruction.
Definition: rpl_sys_table_access.cc:51
std::string get_table_name()
Get table name of table accessed.
Definition: rpl_sys_table_access.h:274
std::string m_schema_name
Definition: rpl_sys_table_access.h:290
const uint m_table_data_index
Definition: rpl_sys_table_access.h:297
bool get_error()
Verify if error is set.
Definition: rpl_sys_table_access.h:106
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:43
const uint m_table_list_size
Definition: rpl_sys_table_access.h:299
std::string m_table_name
Definition: rpl_sys_table_access.h:291
std::string get_db_name()
Get database name of table accessed.
Definition: rpl_sys_table_access.h:267
uint m_max_num_field
Definition: rpl_sys_table_access.h:292
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:105
THD * m_current_thd
Definition: rpl_sys_table_access.h:281
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:213
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:381
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:53
std::string get_field_error_msg(std::string field_name) const
Definition: rpl_sys_table_access.cc:258
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:226
const uint m_table_version_index
Definition: rpl_sys_table_access.h:298
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:175
enum thr_lock_type m_lock_type
Definition: rpl_sys_table_access.h:288
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:359
static void for_each_in_tuple(const std::tuple< Ts... > &tuple, F func)
Definition: rpl_sys_table_access.h:212
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:78
const std::string m_table_version_name
Definition: rpl_sys_table_access.h:295
THD * m_thd
Definition: rpl_sys_table_access.h:278
static void for_each_in_tuple(std::tuple< Ts... > &tuple, F func)
Definition: rpl_sys_table_access.h:200
bool m_error
Definition: rpl_sys_table_access.h:284
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:326
TABLE * get_table()
Get TABLE object created for the table access purposes.
Definition: rpl_sys_table_access.cc:205
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:289
void set_error()
Set error.
Definition: rpl_sys_table_access.h:98
const std::string m_schema_version_name
Definition: rpl_sys_table_access.h:294
Table_ref * m_table_list
Definition: rpl_sys_table_access.h:287
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Definition: table.h:2864
MYSQL_STRINGS_EXPORT CHARSET_INFO my_charset_bin
Definition: ctype-bin.cc:509
#define HA_WHOLE_KEY
Definition: my_base.h:1009
ulong key_part_map
Definition: my_base.h:1008
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:423
Definition: table.h:1406
thr_lock_type
Definition: thr_lock.h:51