MySQL  8.0.22
Source Code Documentation
sql_admin.h
Go to the documentation of this file.
1 /* Copyright (c) 2010, 2020, 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 SQL_TABLE_MAINTENANCE_H
24 #define SQL_TABLE_MAINTENANCE_H
25 
26 #include <stddef.h>
27 #include <set>
28 
29 #include "lex_string.h"
30 #include "my_dbug.h"
31 #include "my_sqlcommand.h"
33 #include "sql/mem_root_allocator.h"
34 #include "sql/sql_cmd.h" // Sql_cmd
35 #include "sql/sql_cmd_ddl_table.h" // Sql_cmd_ddl_table
36 #include "sql/sql_plugin_ref.h"
37 
38 class Clone_handler;
39 class String;
40 class THD;
41 
42 struct TABLE_LIST;
43 template <class T>
44 class List;
45 
46 struct KEY_CACHE;
47 struct LEX_USER;
48 
50  bool operator()(const String *lhs, const String *rhs) const;
51 };
52 
53 /* Must be able to hold ALTER TABLE t PARTITION BY ... KEY ALGORITHM = 1 ... */
54 #define SQL_ADMIN_MSG_TEXT_SIZE 128 * 1024
55 
56 /**
57  Sql_cmd_analyze_table represents the ANALYZE TABLE statement.
58 
59  Also this class is a base class for Sql_cmd_alter_table_analyze_partition
60  which represents the ALTER TABLE ... ANALYZE PARTITION statement.
61 */
63  public:
64  /**
65  Specifies which (if any) of the commands UPDATE HISTOGRAM or DROP HISTOGRAM
66  that is specified after ANALYZE TABLE tbl.
67  */
68  enum class Histogram_command {
69  NONE, ///< Neither UPDATE or DROP histogram is specified
70  UPDATE_HISTOGRAM, ///< UPDATE HISTOGRAM ... is specified after ANALYZE
71  ///< TABLE
72  DROP_HISTOGRAM ///< DROP HISTOGRAM ... is specified after ANALYZE TABLE
73  };
74 
75  /**
76  Constructor, used to represent a ANALYZE TABLE statement.
77  */
78  Sql_cmd_analyze_table(THD *thd, Alter_info *alter_info,
79  Histogram_command histogram_command,
80  int histogram_buckets);
81 
82  bool execute(THD *thd) override;
83 
84  enum_sql_command sql_command_code() const override { return SQLCOM_ANALYZE; }
85 
86  /**
87  Set which fields to (try and) create/update or delete histogram statistics
88  for.
89  */
90  bool set_histogram_fields(List<String> *fields);
91 
92  private:
93  using columns_set =
94  std::set<String *, Column_name_comparator, Mem_root_allocator<String *>>;
95 
96  /// Which histogram command (if any) is specified
98 
99  /// The fields specified by the user in UPDATE/DROP HISTOGRAM
101 
102  /// The number of buckets specified by the user in UPDATE HISTOGRAM
104 
105  /// @return The histogram command specified, if any.
107  return m_histogram_command;
108  }
109 
110  /// @return The number of buckets specified in UPDATE HISTOGRAM.
111  int get_histogram_buckets() const { return m_histogram_buckets; }
112 
113  /// @return The fields specified in UPDATE/DROP HISTOGRAM
114  const columns_set &get_histogram_fields() const { return m_histogram_fields; }
115 
116  /**
117  Send the result of histogram operations back to the client as a result set.
118 
119  @param thd Thread handle.
120  @param results The messages to send back to the client.
121  @param table The table the operations was performed on.
122 
123  @return false on success, true otherwise.
124  */
125  bool send_histogram_results(THD *thd, const histograms::results_map &results,
126  const TABLE_LIST *table);
127 
128  /**
129  Update one or more histograms
130 
131  This is invoked by running the command "ANALYZE TABLE tbl UPDATE HISTOGRAM
132  ON col1, col2 WITH n BUCKETS". Note that the function expects exactly one
133  table to be specified, but multiple columns can be specified.
134 
135  @param thd Thread handler.
136  @param table The table specified in ANALYZE TABLE
137  @param results A map where the results of the operations will be stored.
138 
139  @return false on success, true on error.
140  */
141  bool update_histogram(THD *thd, TABLE_LIST *table,
142  histograms::results_map &results);
143 
144  /**
145  Drops one or more histograms
146 
147  This is invoked by running the command "ANALYZE TABLE tbl DROP HISTOGRAM ON
148  col1, col2;". Note that the function expects exactly one table to be
149  specified, but multiple columns can be specified.
150 
151  @param thd Thread handler.
152  @param table The table specified in ANALYZE TABLE
153  @param results A map where the results of the operations will be stored.
154 
155  @return false on success, true on error.
156  */
157  bool drop_histogram(THD *thd, TABLE_LIST *table,
158  histograms::results_map &results);
159 
160  bool handle_histogram_command(THD *thd, TABLE_LIST *table);
161 };
162 
163 /**
164  Sql_cmd_check_table represents the CHECK TABLE statement.
165 
166  Also this is a base class of Sql_cmd_alter_table_check_partition which
167  represents the ALTER TABLE ... CHECK PARTITION statement.
168 */
170  public:
172 
173  bool execute(THD *thd) override;
174 
175  enum_sql_command sql_command_code() const override { return SQLCOM_CHECK; }
176 };
177 
178 /**
179  Sql_cmd_optimize_table represents the OPTIMIZE TABLE statement.
180 
181  Also this is a base class of Sql_cmd_alter_table_optimize_partition.
182  represents the ALTER TABLE ... CHECK PARTITION statement.
183 */
185  public:
187 
188  bool execute(THD *thd) override;
189 
190  enum_sql_command sql_command_code() const override { return SQLCOM_OPTIMIZE; }
191 };
192 
193 /**
194  Sql_cmd_repair_table represents the REPAIR TABLE statement.
195 
196  Also this is a base class of Sql_cmd_alter_table_repair_partition which
197  represents the ALTER TABLE ... REPAIR PARTITION statement.
198 */
200  public:
202 
203  bool execute(THD *thd) override;
204 
205  enum_sql_command sql_command_code() const override { return SQLCOM_REPAIR; }
206 };
207 
208 /**
209  Sql_cmd_shutdown represents the SHUTDOWN statement.
210 */
211 class Sql_cmd_shutdown : public Sql_cmd {
212  public:
213  bool execute(THD *thd) override;
214  enum_sql_command sql_command_code() const override { return SQLCOM_SHUTDOWN; }
215 };
216 
218 
219 /**
220  Sql_cmd_set_role represetns the SET ROLE ... statement.
221 */
222 class Sql_cmd_set_role : public Sql_cmd {
223  friend class PT_set_role;
224 
228 
229  public:
231  const List<LEX_USER> *except_roles_arg)
232  : role_type(role_type_arg),
233  role_list(nullptr),
234  except_roles(except_roles_arg) {
235  DBUG_ASSERT(role_type == role_enum::ROLE_NONE ||
236  role_type == role_enum::ROLE_DEFAULT ||
237  role_type == role_enum::ROLE_ALL);
238  DBUG_ASSERT(role_type == role_enum::ROLE_ALL || except_roles == nullptr);
239  }
240  explicit Sql_cmd_set_role(const List<LEX_USER> *role_arg)
241  : role_type(role_enum::ROLE_NAME), role_list(role_arg) {}
242 
243  bool execute(THD *thd) override;
244  enum_sql_command sql_command_code() const override { return SQLCOM_SET_ROLE; }
245 };
246 
247 /**
248  Sql_cmd_create_role represetns the CREATE ROLE ... statement.
249 */
250 class Sql_cmd_create_role : public Sql_cmd {
251  friend class PT_create_role;
252 
253  const bool if_not_exists;
255 
256  public:
257  explicit Sql_cmd_create_role(bool if_not_exists_arg,
258  const List<LEX_USER> *roles_arg)
259  : if_not_exists(if_not_exists_arg), roles(roles_arg) {}
260 
261  bool execute(THD *thd) override;
263  return SQLCOM_CREATE_ROLE;
264  }
265 };
266 
267 /**
268  Sql_cmd_drop_role represetns the DROP ROLE ... statement.
269 */
270 class Sql_cmd_drop_role : public Sql_cmd {
271  friend class PT_drop_role;
272 
275 
276  public:
277  explicit Sql_cmd_drop_role(bool ignore_errors_arg,
278  const List<LEX_USER> *roles_arg)
279  : ignore_errors(ignore_errors_arg), roles(roles_arg) {}
280 
281  bool execute(THD *thd) override;
283  return SQLCOM_DROP_ROLE;
284  }
285 };
286 
287 /**
288  Sql_cmd_grant_roles represents the GRANT role-list TO ... statement.
289 */
290 class Sql_cmd_grant_roles : public Sql_cmd {
293  const bool with_admin_option;
294 
295  public:
296  explicit Sql_cmd_grant_roles(const List<LEX_USER> *roles_arg,
297  const List<LEX_USER> *users_arg,
298  bool with_admin_option_arg)
299  : roles(roles_arg),
300  users(users_arg),
301  with_admin_option(with_admin_option_arg) {}
302 
303  bool execute(THD *thd) override;
305  return SQLCOM_GRANT_ROLE;
306  }
307 };
308 
309 /**
310  Sql_cmd_revoke_roles represents the REVOKE [role list] TO ... statement.
311 */
315 
316  public:
317  explicit Sql_cmd_revoke_roles(const List<LEX_USER> *roles_arg,
318  const List<LEX_USER> *users_arg)
319  : roles(roles_arg), users(users_arg) {}
320 
321  bool execute(THD *thd) override;
323  return SQLCOM_REVOKE_ROLE;
324  }
325 };
326 
327 /**
328  Sql_cmd_alter_user_default_role ALTER USER ... DEFAULT ROLE ... statement.
329 */
332 
333  const bool if_exists;
337 
338  public:
339  explicit Sql_cmd_alter_user_default_role(bool if_exists_arg,
340  const List<LEX_USER> *users_arg,
341  const List<LEX_USER> *roles_arg,
342  const role_enum role_type_arg)
343  : if_exists(if_exists_arg),
344  users(users_arg),
345  roles(roles_arg),
346  role_type(role_type_arg) {}
347 
348  bool execute(THD *thd) override;
351  }
352 };
353 
359  LAST_MASTER_KEY, /* Add new master key type before this */
362 };
363 
364 /**
365  Sql_cmd_alter_instance represents the ROTATE alter_instance_action MASTER KEY
366  statement.
367 */
368 class Alter_instance;
369 
371  friend class PT_alter_instance;
372  const enum alter_instance_action_enum alter_instance_action;
375 
376  public:
378  enum alter_instance_action_enum alter_instance_action_arg,
379  const LEX_CSTRING &channel_name)
380  : alter_instance_action(alter_instance_action_arg),
381  channel_name_(channel_name),
382  alter_instance(nullptr) {}
383 
384  bool execute(THD *thd) override;
386  return SQLCOM_ALTER_INSTANCE;
387  }
388 };
389 
390 /**
391  Sql_cmd_clone implements CLONE ... statement.
392 */
393 
394 class Sql_cmd_clone : public Sql_cmd {
395  public:
396  /** Construct clone command for clone server */
397  explicit Sql_cmd_clone()
398  : m_host(),
399  m_port(),
400  m_user(),
401  m_passwd(),
402  m_data_dir(),
403  m_clone(),
404  m_is_local(false) {}
405 
406  /** Construct clone command for clone client
407  @param[in] user_info user, password and remote host information
408  @param[in] port port for remote server
409  @param[in] data_dir data directory to clone */
410  explicit Sql_cmd_clone(LEX_USER *user_info, ulong port, LEX_CSTRING data_dir);
411 
412  /** Construct clone command for local clone
413  @param[in] data_dir data directory to clone */
414  explicit Sql_cmd_clone(LEX_CSTRING data_dir)
415  : m_host(),
416  m_port(),
417  m_user(),
418  m_passwd(),
419  m_data_dir(data_dir),
420  m_clone(),
421  m_is_local(true) {}
422 
423  enum_sql_command sql_command_code() const override { return SQLCOM_CLONE; }
424 
425  bool execute(THD *thd) override;
426 
427  /** Execute clone server.
428  @param[in] thd server session
429  @return true, if error */
430  bool execute_server(THD *thd);
431 
432  /** Load clone plugin for clone server.
433  @param[in] thd server session
434  @return true, if error */
435  bool load(THD *thd);
436 
437  /** Re-write clone statement to hide password.
438  @param[in,out] thd server session
439  @param[in,out] rlb the buffer to return the rewritten query in. empty if none.
440  @return true iff query is re-written */
441  bool rewrite(THD *thd, String &rlb);
442 
443  /** @return true, if it is local clone command */
444  bool is_local() const { return (m_is_local); }
445 
446  private:
447  /** Remote server IP */
449 
450  /** Remote server port */
451  const ulong m_port;
452 
453  /** User name for remote connection */
455 
456  /** Password for remote connection */
458 
459  /** Data directory for cloned data */
461 
462  /** Clone handle in server */
464 
465  /** Loaded clone plugin reference */
467 
468  /** If it is local clone operation */
470 };
471 #endif
LEX_CSTRING m_passwd
Password for remote connection.
Definition: sql_admin.h:457
Sql_cmd_revoke_roles represents the REVOKE [role list] TO ...
Definition: sql_admin.h:312
Definition: parse_tree_nodes.h:3007
Definition: sql_plugin_ref.h:44
Sql_cmd_set_role(role_enum role_type_arg, const List< LEX_USER > *except_roles_arg)
Definition: sql_admin.h:230
Representation of an SQL command.
Definition: sql_cmd.h:63
int get_histogram_buckets() const
Definition: sql_admin.h:111
bool operator()(const String *lhs, const String *rhs) const
Definition: sql_admin.cc:102
A base class for CREATE/ALTER TABLE commands and friends.
Definition: sql_cmd_ddl_table.h:49
columns_set m_histogram_fields
The fields specified by the user in UPDATE/DROP HISTOGRAM.
Definition: sql_admin.h:100
Sql_cmd_clone(LEX_CSTRING data_dir)
Construct clone command for local clone.
Definition: sql_admin.h:414
Definition: my_sqlcommand.h:185
Sql_cmd_create_role represetns the CREATE ROLE ...
Definition: sql_admin.h:250
bool update_histogram(THD *thd, TABLE_LIST *table, const columns_set &columns, int num_buckets, results_map &results)
Create or update histograms for a set of columns of a given table.
Definition: histogram.cc:869
Histogram_command
Specifies which (if any) of the commands UPDATE HISTOGRAM or DROP HISTOGRAM that is specified after A...
Definition: sql_admin.h:68
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:385
plugin_ref m_plugin
Loaded clone plugin reference.
Definition: sql_admin.h:466
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:214
Definition: my_sqlcommand.h:189
bool load(THD *, const dd::String_type &fname, dd::String_type *buf)
Read an sdi file from disk and store in a buffer.
Definition: sdi_file.cc:306
Definition: my_sqlcommand.h:193
Definition: keycache.h:70
Definition: parse_tree_nodes.h:2872
const List< LEX_USER > * roles
Definition: sql_admin.h:313
const List< LEX_USER > * users
Definition: sql_admin.h:314
const List< LEX_USER > * roles
Definition: sql_admin.h:335
Definition: parse_tree_nodes.h:2882
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:282
Sql_cmd_shutdown represents the SHUTDOWN statement.
Definition: sql_admin.h:211
LEX_CSTRING m_data_dir
Data directory for cloned data.
Definition: sql_admin.h:460
Definition: my_sqlcommand.h:188
enum_sql_command
Definition: my_sqlcommand.h:45
Definition: mysql_lex_string.h:39
const List< LEX_USER > * roles
Definition: sql_admin.h:254
Sql_cmd_drop_role represetns the DROP ROLE ...
Definition: sql_admin.h:270
static bool execute(MYSQL_STMT *stmt, char *packet, ulong length)
Definition: libmysql.cc:1975
Representation of an SQL command.
Clone plugin handler to convenient way to.
Definition: clone_handler.h:53
Definition: my_sqlcommand.h:190
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:164
bool is_local() const
Definition: sql_admin.h:444
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:244
LEX_CSTRING m_user
User name for remote connection.
Definition: sql_admin.h:454
Definition: sql_admin.h:359
Definition: table.h:2432
#define DBUG_ASSERT(A)
Definition: my_dbug.h:199
static in_port_t port
Definition: testapp.c:33
LEX_CSTRING channel_name_
Definition: sql_admin.h:373
const bool if_exists
Definition: sql_admin.h:333
const List< LEX_USER > * roles
Definition: sql_admin.h:291
Sql_cmd_set_role(const List< LEX_USER > *role_arg)
Definition: sql_admin.h:240
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:322
Definition: auth_common.h:57
Sql_cmd_ddl_table(Alter_info *alter_info)
Definition: sql_cmd_ddl_table.cc:66
Sql_cmd_alter_user_default_role(bool if_exists_arg, const List< LEX_USER > *users_arg, const List< LEX_USER > *roles_arg, const role_enum role_type_arg)
Definition: sql_admin.h:339
std::set< String *, Column_name_comparator, Mem_root_allocator< String * > > columns_set
Definition: sql_admin.h:94
Definition: sql_admin.h:355
Sql_cmd_revoke_roles(const List< LEX_USER > *roles_arg, const List< LEX_USER > *users_arg)
Definition: sql_admin.h:317
struct passwd * user_info
Definition: mysql_ssl_rsa_setup.cc:118
#define true
Definition: config_static.h:44
Sql_cmd_alter_instance(enum alter_instance_action_enum alter_instance_action_arg, const LEX_CSTRING &channel_name)
Definition: sql_admin.h:377
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:304
Definition: my_sqlcommand.h:199
const List< LEX_USER > * except_roles
Definition: sql_admin.h:227
Definition: parse_tree_nodes.h:2862
Definition: my_sqlcommand.h:191
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:175
Definition: sql_admin.h:360
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:423
Sql_cmd_set_role represetns the SET ROLE ...
Definition: sql_admin.h:222
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:349
Alter_instance * alter_instance
Definition: sql_admin.h:374
Sql_cmd_repair_table represents the REPAIR TABLE statement.
Definition: sql_admin.h:199
Definition: my_sqlcommand.h:183
const role_enum role_type
Definition: sql_admin.h:336
const ulong m_port
Remote server port.
Definition: sql_admin.h:451
bool m_is_local
If it is local clone operation.
Definition: sql_admin.h:469
Sql_cmd_drop_role(bool ignore_errors_arg, const List< LEX_USER > *roles_arg)
Definition: sql_admin.h:277
Sql_cmd_grant_roles represents the GRANT role-list TO ...
Definition: sql_admin.h:290
const List< LEX_USER > * role_list
Definition: sql_admin.h:226
Top-level node for the ALTER INSTANCE statement.
Definition: parse_tree_nodes.h:2020
Sql_cmd_clone()
Construct clone command for clone server.
Definition: sql_admin.h:397
Definition: my_sqlcommand.h:85
Sql_cmd_analyze_table represents the ANALYZE TABLE statement.
Definition: sql_admin.h:62
Sql_cmd_clone implements CLONE ...
Definition: sql_admin.h:394
Clone_handler * m_clone
Clone handle in server.
Definition: sql_admin.h:463
Data describing the table being created by CREATE TABLE or altered by ALTER TABLE.
Definition: sql_alter.h:193
alter_instance_action_enum
Definition: sql_admin.h:354
Definition: my_sqlcommand.h:91
Definition: sql_admin.h:356
role_enum
Definition: sql_admin.h:217
const bool if_not_exists
Definition: sql_admin.h:253
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:262
Definition: sql_admin.h:358
Histogram base class.
Sql_cmd_create_role(bool if_not_exists_arg, const List< LEX_USER > *roles_arg)
Definition: sql_admin.h:257
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:205
Definition: sql_admin.h:370
Histogram_command get_histogram_command() const
Definition: sql_admin.h:106
Definition: my_sqlcommand.h:92
Sql_cmd_alter_user_default_role ALTER USER ...
Definition: sql_admin.h:330
Sql_cmd_optimize_table represents the OPTIMIZE TABLE statement.
Definition: sql_admin.h:184
int m_histogram_buckets
The number of buckets specified by the user in UPDATE HISTOGRAM.
Definition: sql_admin.h:103
Definition: sql_alter_instance.h:30
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:84
const bool with_admin_option
Definition: sql_admin.h:293
Definition: table.h:2540
Definition: my_sqlcommand.h:192
const List< LEX_USER > * roles
Definition: sql_admin.h:274
Definition: sql_admin.h:49
bool ignore_errors
Definition: sql_admin.h:273
const columns_set & get_histogram_fields() const
Definition: sql_admin.h:114
LEX_CSTRING m_host
Remote server IP.
Definition: sql_admin.h:448
const role_enum role_type
Definition: sql_admin.h:225
#define false
Definition: config_static.h:43
Definition: sql_admin.h:361
std::map< std::string, Message, std::less< std::string >, Histogram_key_allocator< std::pair< const std::string, Message > >> results_map
Definition: histogram.h:112
Sql_cmd_check_table represents the CHECK TABLE statement.
Definition: sql_admin.h:169
Sql_cmd_grant_roles(const List< LEX_USER > *roles_arg, const List< LEX_USER > *users_arg, bool with_admin_option_arg)
Definition: sql_admin.h:296
Definition: my_sqlcommand.h:97
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:803
Dialog Client Authentication nullptr
Definition: dialog.cc:353
const List< LEX_USER > * users
Definition: sql_admin.h:334
const List< LEX_USER > * users
Definition: sql_admin.h:292
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_admin.h:190