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