File: rpl_sys_table_access.h

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (300 lines) | stat: -rw-r--r-- 9,139 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#ifndef RPL_SYS_TABLE_ACCESS_INCLUDED
#define RPL_SYS_TABLE_ACCESS_INCLUDED

#include "sql/field.h"
#include "sql/sql_class.h"
#include "sql/table.h"
#include "thr_lock.h"

class Json_wrapper;
class THD;
struct TABLE;

/**
  @class Rpl_sys_table_access

  The class is used to simplify table data access. It creates new thread/session
  context (THD) and open table on class object creation, and destroys thread and
  closes all open thread tables on class object destruction.
*/
class Rpl_sys_table_access {
 public:
  /**
    Construction.
    @param[in]  schema_name   Database where the table resides
    @param[in]  table_name    Table to be opened
    @param[in]  max_num_field Maximum number of fields
  */
  Rpl_sys_table_access(const std::string &schema_name,
                       const std::string &table_name, uint max_num_field);

  /**
    Destruction. All opened tables with the open_tables are closed during
    destruction if not already done in deinit().
  */
  ~Rpl_sys_table_access();

  /**
    Creates new thread/session context (THD) and open's table on class object
    creation.

    @param[in]  lock_type     How to lock the table

    @retval true  if there is error
    @retval false if there is no error
  */
  bool open(enum thr_lock_type lock_type);

  /**
    All opened tables with the open_tables are closed and removes
    THD created in close().

    @param[in]  error         State that there was a error on the table
                              operations
    @param[in]  ignore_global_read_lock
                              State that the global_read_lock must be
                              ignored

    @retval true  if there is error
    @retval false if there is no error
  */
  bool close(bool error, bool ignore_global_read_lock = false);

  /**
    Get TABLE object created for the table access purposes.

    @return TABLE pointer.
  */
  TABLE *get_table();

  /**
    Set error.
  */
  void set_error() { m_error = true; }

  /**
    Verify if error is set.

    @retval true  if there is error
    @retval false if there is no error
  */
  bool get_error() { return m_error; }

  /**
    Stores provided string to table's field.

    @param[in]  field        Field class object
    @param[in]  fld          The std::string value to be saved.
    @param[in]  cs           Charset info

    @retval true   Error
    @retval false  Success
  */
  bool store_field(Field *field, std::string fld,
                   CHARSET_INFO *cs = &my_charset_bin);

  /**
    Stores provided integer to table's field.

    @param[in]  field        Field class object
    @param[in]  fld          The long long value to be saved.
    @param[in]  unsigned_val If value is unsigned.

    @retval true   Error
    @retval false  Success
  */
  bool store_field(Field *field, long long fld, bool unsigned_val = true);

  /**
    Stores provided Json to table's field.

    @param[in]  field        Field class object
    @param[in]  wrapper      The Json_wrapper class object value

    @retval true   Error
    @retval false  Success
  */
  bool store_field(Field *field, const Json_wrapper &wrapper);

  /**
    Retrieves string field from provided table's field.

    @param[in]  field        Field class object
    @param[in]  fld          The std::string value to be retrieved.
    @param[in]  cs           Charset info

    @retval true   Error
    @retval false  Success
  */
  bool get_field(Field *field, std::string &fld,
                 CHARSET_INFO *cs = &my_charset_bin);

  /**
    Retrieves long long integer field from provided table's field.

    @param[in]  field        Field class object
    @param[in]  fld          The uint value to be retrieved.

    @retval true   Error
    @retval false  Success
  */
  bool get_field(Field *field, uint &fld);

  /**
    Retrieves Json field from provided table's field.

    @param[in]  field        Field class object
    @param[in]  wrapper      The retrieved field would be saved in
                             Json_wrapper format.

    @retval true   Error
    @retval false  Success
  */
  bool get_field(Field *field, Json_wrapper &wrapper);

  std::string get_field_error_msg(std::string field_name) const;

  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);

  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);

  template <class F, class... Ts, std::size_t... Is>
  static void for_each_in_tuple(std::tuple<Ts...> &tuple, F func,
                                std::index_sequence<Is...>) {
    using tuple_list = int[255];
    (void)tuple_list{0, ((void)func(Is, std::get<Is>(tuple)), 0)...};
  }

  template <class F, class... Ts>
  static void for_each_in_tuple(std::tuple<Ts...> &tuple, F func) {
    for_each_in_tuple(tuple, func, std::make_index_sequence<sizeof...(Ts)>());
  }

  template <class F, class... Ts, std::size_t... Is>
  static void for_each_in_tuple(const std::tuple<Ts...> &tuple, F func,
                                std::index_sequence<Is...>) {
    using tuple_list = int[255];
    (void)tuple_list{0, ((void)func(Is, std::get<Is>(tuple)), 0)...};
  }

  template <class F, class... Ts>
  static void for_each_in_tuple(const std::tuple<Ts...> &tuple, F func) {
    for_each_in_tuple(tuple, func, std::make_index_sequence<sizeof...(Ts)>());
  }

  /**
    Delete all rows on `m_schema_name.m_table_name`.

    @retval true  if there is error
    @retval false if there is no error
  */
  bool delete_all_rows();

  /**
    Return the version stored on `m_schema_version_name.m_table_version_name`
    for the `m_schema_name.m_table_name` table.

    @retval 0  if there is error
    @retval >0 if there is no error
  */
  ulonglong get_version();

  /**
    Increment the version stored on `m_schema_version_name.m_table_version_name`
    for the `m_schema_name.m_table_name` table.

    @retval true  if there is error
    @retval false if there is no error
  */
  bool increment_version();

  /**
    Update the version stored on `m_schema_version_name.m_table_version_name`
    for the `m_schema_name.m_table_name` table.

    @param[in]  version  the version value

    @retval true  if there is error
    @retval false if there is no error
  */
  bool update_version(ulonglong version);

  /**
    Delete the version stored on `m_schema_version_name.m_table_version_name`
    for the `m_schema_name.m_table_name` table.

    @retval true  if there is error
    @retval false if there is no error
  */
  bool delete_version();

  /**
    Get database name of table accessed.

    @return database name.
  */
  std::string get_db_name() { return m_schema_name; }

  /**
    Get table name of table accessed.

    @return table name.
  */
  std::string get_table_name() { return m_table_name; }

 private:
  /* THD created for TableAccess object purpose. */
  THD *m_thd{nullptr};

  /* THD associated with the calling thread. */
  THD *m_current_thd{nullptr};

  /* The variable determine if table is opened or closed successfully. */
  bool m_error{false};

  /* Table_ref object */
  Table_ref *m_table_list{nullptr};
  enum thr_lock_type m_lock_type;

  std::string m_schema_name;
  std::string m_table_name;
  uint m_max_num_field;

  const std::string m_schema_version_name{"mysql"};
  const std::string m_table_version_name{
      "replication_group_configuration_version"};
  const uint m_table_data_index = 0;
  const uint m_table_version_index = 1;
  const uint m_table_list_size = 2;
};

#endif /* RPL_SYS_TABLE_ACCESS_INCLUDED */