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