MySQL 9.0.0
Source Code Documentation
mysql_system_variable.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 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 MYSQL_SYSTEM_VARIABLES_H
25#define MYSQL_SYSTEM_VARIABLES_H
26
29#include <mysql/components/services/mysql_string.h> // my_h_string
30
31/**
32 @ingroup group_components_services_inventory
33
34 Service to set the value of system variables.
35 This is an example of using the service:
36
37 @code
38
39 MYSQL_THD thd = nullptr;
40
41 if (!make_new_thread &&
42 mysql_service_mysql_current_thread_reader->get(&thd)) {
43 *error = 1;
44 return 0;
45 }
46
47 const char *cs = "latin1";
48 const char *base_input = "my_component"; //nullptr if not a component variable
49 const char *name_input = "my_variable";
50 const char *value_input = "value";
51 const char *type = "GLOBAL";
52
53 my_h_string base = nullptr, name = nullptr, value = nullptr;
54
55 mysql_service_mysql_string_factory->create(&base);
56 mysql_service_mysql_string_factory->create(&name);
57 mysql_service_mysql_string_factory->create(&value);
58
59 if ((base_input != nullptr &&
60 mysql_service_mysql_string_converter->convert_from_buffer(
61 &base, base_input, strlen(base_input), cs)) ||
62 mysql_service_mysql_string_converter->convert_from_buffer(
63 &name, name_input, strlen(name_input), cs) ||
64 mysql_service_mysql_string_converter->convert_from_buffer(
65 &value, value_input, strlen(value_input), cs) {
66 if (base) mysql_service_mysql_string_factory->destroy(base);
67 if (name) mysql_service_mysql_string_factory->destroy(name);
68 if (value) mysql_service_mysql_string_factory->destroy(value);
69 *error = 1;
70 return 0;
71 }
72
73 if (mysql_service_mysql_system_variable_update_string->set(thd, type,
74 base, name, value)) *error = 1;
75
76 if (base) mysql_service_mysql_string_factory->destroy(base);
77 if (name) mysql_service_mysql_string_factory->destroy(name);
78 if (value) mysql_service_mysql_string_factory->destroy(value);
79 @endcode
80
81
82 @sa @ref mysql_system_variable_update_imp
83*/
84BEGIN_SERVICE_DEFINITION(mysql_system_variable_update_string)
85
86/**
87 Sets the value of a system variable to a new string value.
88
89 @param thd thread session handle. if NULL a temp one will be created and then
90 removed.
91 @param set_variable_type: one of [GLOBAL, PERSIST, PERSIST_ONLY].
92 If NULL, then assumes GLOBAL.
93 @param variable_name_base: a mysql string of the variable name prefix. NULL
94 of none
95 @param variable_name: a mysql string of the variable name
96 @param variable_value: a mysql string to set as value
97 @retval FALSE: success
98 @retval TRUE: failure, see THD if supplied.
99*/
101 (MYSQL_THD thd, const char *set_variable_type,
102 my_h_string variable_name_base, my_h_string variable_name,
103 my_h_string variable_value));
104END_SERVICE_DEFINITION(mysql_system_variable_update_string)
105
106/**
107 @ingroup group_components_services_inventory
108
109 Fetches the session/global/persist value of a system variable
110
111 This is an example of using the service:
112
113 Call this to get the session/global/persist value of a variable.
114 You can access both the component variables
115 (SELECT ["@@"][scope "."]component_name "." variable_name;
116 where scope ::= SESSION | LOCAL | GLOBAL)
117 and the "legacy" system variables
118 (SELECT ["@@"][scope "."].variable_name;
119 where scope ::= SESSION | LOCAL | GLOBAL) that are registered
120 by the server component.
121 To access the latter you need to pass "mysql_server" (lowercase) as a
122 component name.
123
124 A pointer to the value is returned into the val input/output argument. And the
125 length of the value (as applicable) is returned into the out_length_of_val
126 argument.
127
128 In case when the user buffer was too small to copy the value, the call fails
129 and needed buffer size is returned by 'out_length_of_val'.
130
131 Decide on a variable storage type among one of "GLOBAL" or "SESSION"
132
133 Typical use (char * variable):
134 @code
135
136 char *value, buffer_for_value[160];
137 size_t value_length;
138 const char *type = "GLOBAL";
139
140 value= &buffer_for_value[0];
141 value_length= sizeof(buffer_for_value) - 1;
142 mysql_service_mysql_system_variable_reader->get(nullptr, type, "foo", "bar",
143 &value, &value_length);
144 printf("%.*s", (int) value_length, value);
145
146 To get the "SESSION" value the "hthd" should be passed :
147 MYSQL_THD hthd;
148 if (mysql_service_mysql_current_thread_reader->get(&hthd)) {
149 my_error("%s\n", "mysql_current_thread_reader get() api failed");
150 }
151 mysql_service_mysql_system_variable_reader->get(
152 hthd, "SESSION", "foo", "bar",
153 &value, &value_length);
154
155 @endcode
156*/
157BEGIN_SERVICE_DEFINITION(mysql_system_variable_reader)
158/**
159 Gets the value of a system variable.
160
161 @param hthd thread session handle. if NULL, the GLOBAL value of the variable
162 is returned.
163 @param variable_type one of [GLOBAL, SESSION].
164 @param component_name Name of the component or "mysql_server" for the legacy
165 ones.
166 @param variable_name Name of the variable
167 @param[in,out] val On input: a buffer to hold the value. On output a pointer
168 to the value.
169 @param[in,out] out_length_of_val On input: the buffer size. On output the
170 length of the data copied.
171
172 @retval true failure
173 @retval false success
174*/
175DECLARE_BOOL_METHOD(get, (MYSQL_THD hthd, const char *variable_type,
176 const char *component_name, const char *variable_name,
177 void **val, size_t *out_length_of_val));
178
179END_SERVICE_DEFINITION(mysql_system_variable_reader)
180
181/**
182 @ingroup group_components_services_inventory
183
184 Service to set the value of integer system variables.
185
186 Passing non-NULL THD input to setter methods means that the operation will be
187 executed within the scope of existing transaction, thus any operation side
188 effects impacting transaction itself (for example it may generate an SQL error
189 that it stores into the current THD). If using existing THD, security context
190 of the thread is checked to make sure that required privileges exist. Passing
191 NULL makes a temporary THD created as a execution context (and destroyed
192 afterwards), i.e. no impacts on existing transactions. It doesn't make sense
193 to change SESSION variable on a temporary THD, so this operation will generate
194 error.
195
196 This is an example of using the service:
197
198 @code
199
200 MYSQL_THD thd = nullptr;
201
202 if (!make_new_thread &&
203 mysql_service_mysql_current_thread_reader->get(&thd)) {
204 *error = 1;
205 return 0;
206 }
207
208 const char *cs = "latin1";
209 const char *base_input = "my_component"; //nullptr if not a component variable
210 const char *name_input = "my_variable";
211 const char *type = "SESSION";
212 long long value_signed = 100000;
213 unsigned long long value_unsigned = 500000;
214
215 my_h_string base = nullptr, name = nullptr;
216
217 mysql_service_mysql_string_factory->create(&base);
218 mysql_service_mysql_string_factory->create(&name);
219
220 if ((base_input != nullptr &&
221 mysql_service_mysql_string_converter->convert_from_buffer(
222 &base, base_input, strlen(base_input), cs)) ||
223 mysql_service_mysql_string_converter->convert_from_buffer(
224 &name, name_input, strlen(name_input), cs)) {
225 if (base) mysql_service_mysql_string_factory->destroy(base);
226 if (name) mysql_service_mysql_string_factory->destroy(name);
227 *error = 1;
228 return 0;
229 }
230
231 // example call for signed integer system variable type
232 if (mysql_service_mysql_system_variable_update_integer->set_signed(
233 thd, type, base, name, value_signed))
234 *error = 1;
235
236 // alternative call for unsigned integer type
237 if (mysql_service_mysql_system_variable_update_integer->set_unsigned(
238 thd, type, base, name, value_unsigned))
239 *error = 1;
240
241 if (base) mysql_service_mysql_string_factory->destroy(base);
242 if (name) mysql_service_mysql_string_factory->destroy(name);
243 @endcode
244
245
246 @sa @ref mysql_system_variable_update_imp
247*/
248BEGIN_SERVICE_DEFINITION(mysql_system_variable_update_integer)
249
250/**
251 Sets the value of a system variable to a new signed integer value.
252
253 Uses long long type to store the data, this type size may
254 differ on a different environments (32-bit vs 64-bit builds for example).
255 This means that the user must ensure that component using this API was
256 compiled using the same environment as for the server code.
257
258 @param thd thread session handle. if NULL a temp one will be created and then
259 removed.
260 @param variable_type: One of GLOBAL, SESSION, PERSIST, PERSIST_ONLY.
261 If NULL, then assumes GLOBAL. SESSION is not supported
262 for a temporary THD.
263 @param variable_base: a mysql string of the variable name prefix. NULL if
264 none
265 @param variable_name: MySQL string of the variable name
266 @param variable_value: long long to set as value
267 @retval FALSE: success
268 @retval TRUE: failure, error set. For error info, see THD if supplied.
269*/
270DECLARE_BOOL_METHOD(set_signed,
271 (MYSQL_THD thd, const char *variable_type,
272 my_h_string variable_base, my_h_string variable_name,
273 long long variable_value));
274
275/**
276 Sets the value of a system variable to a new unsigned integer value.
277
278 Uses unsigned long long type to store the data, this type size may
279 differ on a different environments (32-bit vs 64-bit builds for example).
280 This means that the user must ensure that component using this API was
281 compiled using the same environment as for the server code.
282
283 @param thd thread session handle. if NULL a temp one will be created and then
284 removed.
285 @param variable_type: One of GLOBAL, SESSION, PERSIST, PERSIST_ONLY.
286 If NULL, then assumes GLOBAL. SESSION is not supported
287 for a temporary THD.
288 @param variable_base: a mysql string of the variable name prefix. NULL if
289 none
290 @param variable_name: MySQL string of the variable name
291 @param variable_value: unsigned long long to set as value
292 @retval FALSE: success
293 @retval TRUE: failure, error set. For error info, see THD if supplied.
294 */
296 (MYSQL_THD thd, const char *variable_type,
297 my_h_string variable_base, my_h_string variable_name,
298 unsigned long long variable_value));
299END_SERVICE_DEFINITION(mysql_system_variable_update_integer)
300
301/**
302 @ingroup group_components_services_inventory
303
304 Service to set the default value of system variables.
305 This is an example of using the service:
306
307 @code
308
309 MYSQL_THD thd = nullptr;
310
311 if (!make_new_thread &&
312 mysql_service_mysql_current_thread_reader->get(&thd)) {
313 *error = 1;
314 return 0;
315 }
316
317 const char *cs = "latin1";
318 const char *base_input = "my_component"; //nullptr if not a component variable
319 const char *name_input = "my_variable";
320 const char *type = "PERSIST";
321
322 my_h_string base = nullptr, name = nullptr;
323
324 mysql_service_mysql_string_factory->create(&base);
325 mysql_service_mysql_string_factory->create(&name);
326
327 if ((base_input != nullptr &&
328 mysql_service_mysql_string_converter->convert_from_buffer(
329 &base, base_input, strlen(base_input), cs)) ||
330 mysql_service_mysql_string_converter->convert_from_buffer(
331 &name, name_input, strlen(name_input), cs)) {
332 if (base) mysql_service_mysql_string_factory->destroy(base);
333 if (name) mysql_service_mysql_string_factory->destroy(name);
334 *error = 1;
335 return 0;
336 }
337
338 if (mysql_service_mysql_system_variable_update_default->set(
339 thd, type, base, name))
340 *error = 1;
341
342 if (base) mysql_service_mysql_string_factory->destroy(base);
343 if (name) mysql_service_mysql_string_factory->destroy(name);
344 @endcode
345
346
347 @sa @ref mysql_system_variable_update_imp
348*/
349BEGIN_SERVICE_DEFINITION(mysql_system_variable_update_default)
350
351/**
352 Sets the value of a system variable to its default value.
353
354 @sa @ref group_components_services_inventory
355
356 @param thd thread session handle. if NULL a temp one will be created and then
357 removed.
358 @param variable_type: One of GLOBAL, SESSION, PERSIST, PERSIST_ONLY.
359 If NULL, then assumes GLOBAL. SESSION is not supported
360 for a temporary THD.
361 @param variable_base: a mysql string of the variable name prefix. NULL if
362 none
363 @param variable_name: MySQL string of the variable name
364 @retval FALSE: success
365 @retval TRUE: failure, error set. For error info, see THD if supplied.
366*/
368 (MYSQL_THD thd, const char *variable_type,
369 my_h_string variable_base, my_h_string variable_name));
370END_SERVICE_DEFINITION(mysql_system_variable_update_default)
371
372#endif /* MYSQL_SYSTEM_VARIABLES_H */
#define MYSQL_THD
Definition: backup_page_tracker.h:38
static mysql_service_status_t get(THD **thd) noexcept
Definition: mysql_current_thread_reader_all_empty.cc:31
void set_unsigned(PSI_field *, PSI_ulonglong) noexcept
Definition: pfs_plugin_column_bigint_v1_all_empty.cc:31
std::set< Key, Compare, ut::allocator< Key > > set
Specialization of set which uses ut_allocator.
Definition: ut0new.h:2883
#define END_SERVICE_DEFINITION(name)
A macro to end the last Service definition started with the BEGIN_SERVICE_DEFINITION macro.
Definition: service.h:91
#define BEGIN_SERVICE_DEFINITION(name)
Declares a new Service.
Definition: service.h:86
#define DECLARE_BOOL_METHOD(name, args)
Declares a method that returns bool as a part of the Service definition.
Definition: service.h:112
Definition: mysql_string_service.cc:60