MySQL 9.1.0
Source Code Documentation
set_variables_helper.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SET_VARIABLES_HELPER_H
25#define SET_VARIABLES_HELPER_H
26
27#include <sql/set_var.h> // set_var_base, enum_var_type
28#include <sql/sql_lex.h> // LEX
29#include <sql/sql_list.h> // List
30
31class THD;
33
34/**
35 A helper class to fascilitate executing SET on system variables.
36
37 Runs in the current @ref THD, but in a temp @ref LEX to avoid
38 polluting the THD::lex with the changes.
39
40 Useful for service implementations looking to set values
41 to system variables.
42
43 Should be used in a single THD.
44
45 A proper usage is something of the sort:
46
47 <code>
48 THD *curr_thd = ...;
49 Item *value1 = Item_int(20), *val2= Item_int(30);
50
51 Set_variables_helper hlp(curr_thd);
52
53 if (hlp.add_variable(nullptr, 0, "foo", 3, enum_var_type::PERSIST_ONLY,
54 value1)) return true;
55
56 if (hlp.add_variable("bar", 3, "baz", 3, enum_var_type::PERSIST, val2))
57 return true;
58
59 ...
60
61 if (hlp->execute())
62 return true;
63 </code>
64
65 The above executes the following command
66 <code>
67 SET PERSIST_ONLY foo = 20, PERSIST bar.baz = 30;
68 </code>
69
70 @sa @ref sql_set_variables
71*/
77
78 public:
79 /**
80 Initializes the helper and switches to the canned temp @ref LEX
81 @param existing_thd the THD to execute on.
82 */
83 Set_variables_helper(THD *existing_thd);
84
86
87 /**
88 Adds setting one variable instruction.
89
90 Adds a:
91 @code
92 <var_type> [prefix.]suffix = variable_value
93 @endcode
94 to the SET command being constructed.
95
96 @param prefix the prefix for the variable name or nullptr if none
97 @param prefix_length length of prefix, 0 if empty
98 @param suffix the name of the variable to set. Must be nonnull
99 @param suffix_length the length of suffix
100 @param variable_value the value to set
101 @param var_type how to set: PERSIST, PERSIST_ONLY etc
102 @retval true failure
103 @retval false success
104 */
105 bool add_variable(const char *prefix, size_t prefix_length,
106 const char *suffix, size_t suffix_length,
107 Item *variable_value, enum_var_type var_type);
108
109 /**
110 Executes the SET command for all variables added.
111
112 @retval true failure
113 @retval false success
114
115 */
116 bool execute();
117
118 /* gets the current thread to execute the SET in */
119 THD *get_thd() { return m_thd; }
120
121 /* returns true if it's running on a local session */
122 bool is_auto_thd() { return m_thd_auto != nullptr; }
123
124 /**
125 @brief Checks the update type for the system variable and throws error if
126 any found
127 @param prefix the prefix for the variable name or nullptr if none
128 @param prefix_length length of prefix, 0 if empty
129 @param suffix the name of the variable to set. Must be nonnull
130 @param suffix_length the length of suffix
131 @param variable_value the value to set
132 @retval true failure
133 @retval false success
134 */
135 bool check_variable_update_type(const char *prefix, size_t prefix_length,
136 const char *suffix, size_t suffix_length,
137 Item *variable_value);
138};
139
140#endif /* SET_VARIABLES_HELPER_H */
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
Definition: sql_list.h:494
A helper class to fascilitate executing SET on system variables.
Definition: set_variables_helper.h:72
LEX * m_lex_save
Definition: set_variables_helper.h:75
List< set_var_base > m_sysvar_list
Definition: set_variables_helper.h:73
LEX m_lex_tmp
Definition: set_variables_helper.h:75
bool is_auto_thd()
Definition: set_variables_helper.h:122
THD * m_thd
Definition: set_variables_helper.h:74
bool check_variable_update_type(const char *prefix, size_t prefix_length, const char *suffix, size_t suffix_length, Item *variable_value)
Checks the update type for the system variable and throws error if any found.
Definition: set_variables_helper.cc:69
~Set_variables_helper()
Definition: set_variables_helper.cc:41
THD * get_thd()
Definition: set_variables_helper.h:119
bool execute()
Executes the SET command for all variables added.
Definition: set_variables_helper.cc:64
Set_variables_helper(THD *existing_thd)
Initializes the helper and switches to the canned temp LEX.
Definition: set_variables_helper.cc:28
Storing_auto_THD * m_thd_auto
Definition: set_variables_helper.h:76
bool add_variable(const char *prefix, size_t prefix_length, const char *suffix, size_t suffix_length, Item *variable_value, enum_var_type var_type)
Adds setting one variable instruction.
Definition: set_variables_helper.cc:50
A version of Auto_THD that:
Definition: storing_auto_thd.h:41
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
"public" interface to sys_var - server configuration variables.
enum_var_type
Definition: set_var.h:92
The LEX object currently serves three different purposes:
Definition: sql_lex.h:3844