set_var.cc 138 KB
Newer Older
Michael Widenius's avatar
Michael Widenius committed
1 2
/* Copyright (c) 2002, 2010, Oracle and/or its affiliates.
   2009-2010 Monty Program Ab
unknown's avatar
unknown committed
3 4 5

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
6
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
7 8 9 10 11 12 13 14 15 16

   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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

unknown's avatar
unknown committed
17 18 19 20
/**
  @file

  @brief
unknown's avatar
unknown committed
21 22
  Handling of MySQL SQL variables

unknown's avatar
unknown committed
23
  @details
unknown's avatar
unknown committed
24 25 26 27 28
  To add a new variable, one has to do the following:

  - Use one of the 'sys_var... classes from set_var.h or write a specific
    one for the variable type.
  - Define it in the 'variable definition list' in this file.
29 30
  - If the variable is thread specific, add it to 'system_variables' struct.
    If not, add it to mysqld.cc and an declaration in 'mysql_priv.h'
unknown's avatar
unknown committed
31
  - If the variable should be changed from the command line, add a definition
unknown's avatar
unknown committed
32
    of it in the my_option structure list in mysqld.cc
33 34
  - Don't forget to initialize new fields in global_system_variables and
    max_system_variables!
unknown's avatar
unknown committed
35

unknown's avatar
unknown committed
36 37 38 39 40 41 42 43 44 45
  @todo
    Add full support for the variable character_set (for 4.1)

  @todo
    When updating myisam_delay_key_write, we should do a 'flush tables'
    of all MyISAM tables to ensure that they are reopen with the
    new attribute.

  @note
    Be careful with var->save_result: sys_var::check() only updates
46 47 48
    ulonglong_value; so other members of the union are garbage then; to use
    them you must first assign a value to them (in specific ::check() for
    example).
unknown's avatar
unknown committed
49 50
*/

51
#ifdef USE_PRAGMA_IMPLEMENTATION
unknown's avatar
unknown committed
52 53 54 55
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
56
#include <mysql.h>
unknown's avatar
unknown committed
57
#include "slave.h"
58
#include "rpl_mi.h"
unknown's avatar
unknown committed
59
#include <my_getopt.h>
60
#include <thr_alarm.h>
unknown's avatar
unknown committed
61
#include <myisam.h>
Sergei Golubchik's avatar
Sergei Golubchik committed
62
#ifdef WITH_ARIA_STORAGE_ENGINE
63 64
#include <maria.h>
#endif
65
#include <my_dir.h>
66
#include <waiting_threads.h>
67
#include "events.h"
68

69
/* WITH_NDBCLUSTER_STORAGE_ENGINE */
70
#ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
71
extern ulong ndb_cache_check_time;
72
extern char opt_ndb_constrbuf[];
unknown's avatar
unknown committed
73
extern ulong ndb_extra_logging;
74 75
#endif

unknown's avatar
unknown committed
76 77 78 79
#ifdef HAVE_NDB_BINLOG
extern ulong ndb_report_thresh_binlog_epoch_slip;
extern ulong ndb_report_thresh_binlog_mem_usage;
#endif
80

unknown's avatar
unknown committed
81
extern CHARSET_INFO *character_set_filesystem;
82 83


unknown's avatar
unknown committed
84
static HASH system_variable_hash;
unknown's avatar
unknown committed
85

unknown's avatar
unknown committed
86 87 88
const char *bool_type_names[]= { "OFF", "ON", NullS };
TYPELIB bool_typelib=
{
89
  array_elements(bool_type_names)-1, "", bool_type_names, NULL
unknown's avatar
unknown committed
90 91
};

92 93 94
const char *delay_key_write_type_names[]= { "OFF", "ON", "ALL", NullS };
TYPELIB delay_key_write_typelib=
{
95 96
  array_elements(delay_key_write_type_names)-1, "",
  delay_key_write_type_names, NULL
97 98
};

99 100 101
static const char *slave_exec_mode_names[]= { "STRICT", "IDEMPOTENT", NullS };
static unsigned int slave_exec_mode_names_len[]= { sizeof("STRICT") - 1,
                                                   sizeof("IDEMPOTENT") - 1, 0 };
102 103 104
TYPELIB slave_exec_mode_typelib=
{
  array_elements(slave_exec_mode_names)-1, "",
105
  slave_exec_mode_names, slave_exec_mode_names_len
106 107
};

108 109 110
static int  sys_check_ftb_syntax(THD *thd,  set_var *var);
static bool sys_update_ftb_syntax(THD *thd, set_var * var);
static void sys_default_ftb_syntax(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
111 112 113 114
static bool sys_update_init_connect(THD*, set_var*);
static void sys_default_init_connect(THD*, enum_var_type type);
static bool sys_update_init_slave(THD*, set_var*);
static void sys_default_init_slave(THD*, enum_var_type type);
unknown's avatar
unknown committed
115
static bool set_option_bit(THD *thd, set_var *var);
116
static bool set_option_log_bin_bit(THD *thd, set_var *var);
unknown's avatar
unknown committed
117
static bool set_option_autocommit(THD *thd, set_var *var);
118
static int  check_log_update(THD *thd, set_var *var);
unknown's avatar
unknown committed
119
static bool set_log_update(THD *thd, set_var *var);
120
static int  check_pseudo_thread_id(THD *thd, set_var *var);
121
void fix_binlog_format_after_update(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
122
static void fix_low_priority_updates(THD *thd, enum_var_type type);
123
static int check_tx_isolation(THD *thd, set_var *var);
unknown's avatar
unknown committed
124
static void fix_tx_isolation(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
125 126
static int check_completion_type(THD *thd, set_var *var);
static void fix_completion_type(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
127 128
static void fix_net_read_timeout(THD *thd, enum_var_type type);
static void fix_net_write_timeout(THD *thd, enum_var_type type);
129
static void fix_net_retry_count(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
130 131
static void fix_max_join_size(THD *thd, enum_var_type type);
static void fix_query_cache_size(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
132
#ifdef HAVE_QUERY_CACHE
133
static void fix_query_cache_min_res_unit(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
134
#endif
135
static void fix_myisam_max_sort_file_size(THD *thd, enum_var_type type);
136 137
static void fix_max_binlog_size(THD *thd, enum_var_type type);
static void fix_max_relay_log_size(THD *thd, enum_var_type type);
138
static void fix_max_connections(THD *thd, enum_var_type type);
139
static int check_max_delayed_threads(THD *thd, set_var *var);
140 141
static void fix_thd_mem_root(THD *thd, enum_var_type type);
static void fix_trans_mem_root(THD *thd, enum_var_type type);
142
static void fix_server_id(THD *thd, enum_var_type type);
143 144
bool throw_bounds_warning(THD *thd, bool fixed, bool unsignd,
                          const char *name, longlong val);
unknown's avatar
unknown committed
145
static KEY_CACHE *create_key_cache(const char *name, uint length);
146
void fix_sql_mode_var(THD *thd, enum_var_type type);
147 148 149
static uchar *get_error_count(THD *thd);
static uchar *get_warning_count(THD *thd);
static uchar *get_tmpdir(THD *thd);
150 151 152 153 154
static int  sys_check_log_path(THD *thd,  set_var *var);
static bool sys_update_general_log_path(THD *thd, set_var * var);
static void sys_default_general_log_path(THD *thd, enum_var_type type);
static bool sys_update_slow_log_path(THD *thd, set_var * var);
static void sys_default_slow_log_path(THD *thd, enum_var_type type);
155
static void fix_sys_log_slow_filter(THD *thd, enum_var_type);
156
static uchar *get_myisam_mmap_size(THD *thd);
unknown's avatar
unknown committed
157 158 159 160 161

/*
  Variable definition list

  These are variables that can be set from the command line, in
162 163 164 165
  alphabetic order.

  The variables are linked into the list. A variable is added to
  it in the constructor (see sys_var class for details).
unknown's avatar
unknown committed
166 167
*/

unknown's avatar
unknown committed
168
static sys_var_chain vars = { NULL, NULL };
169

170 171 172 173 174 175 176 177
static sys_var_thd_ulong
sys_auto_increment_increment(&vars, "auto_increment_increment",
                             &SV::auto_increment_increment, NULL, NULL,
                             sys_var::SESSION_VARIABLE_IN_BINLOG);
static sys_var_thd_ulong
sys_auto_increment_offset(&vars, "auto_increment_offset",
                          &SV::auto_increment_offset, NULL, NULL,
                          sys_var::SESSION_VARIABLE_IN_BINLOG);
178

unknown's avatar
unknown committed
179
static sys_var_bool_ptr	sys_automatic_sp_privileges(&vars, "automatic_sp_privileges",
180 181
					      &sp_automatic_privileges);

182 183 184
static sys_var_const            sys_back_log(&vars, "back_log",
                                             OPT_GLOBAL, SHOW_LONG,
                                             (uchar*) &back_log);
185
static sys_var_const_os_str       sys_basedir(&vars, "basedir", mysql_home);
unknown's avatar
unknown committed
186
static sys_var_long_ptr	sys_binlog_cache_size(&vars, "binlog_cache_size",
unknown's avatar
unknown committed
187
					      &binlog_cache_size);
unknown's avatar
unknown committed
188
static sys_var_thd_binlog_format sys_binlog_format(&vars, "binlog_format",
189
                                            &SV::binlog_format);
190 191
static sys_var_thd_bool sys_binlog_direct_non_trans_update(&vars, "binlog_direct_non_transactional_updates",
                                                           &SV::binlog_direct_non_trans_update);
unknown's avatar
unknown committed
192
static sys_var_thd_ulong	sys_bulk_insert_buff_size(&vars, "bulk_insert_buffer_size",
unknown's avatar
unknown committed
193
						  &SV::bulk_insert_buff_size);
194
static sys_var_const_os         sys_character_sets_dir(&vars,
195 196 197 198
                                                       "character_sets_dir",
                                                       OPT_GLOBAL, SHOW_CHAR,
                                                       (uchar*)
                                                       mysql_charsets_dir);
199 200 201 202
static sys_var_character_set_sv
sys_character_set_server(&vars, "character_set_server",
                         &SV::collation_server, &default_charset_info, 0,
                         sys_var::SESSION_VARIABLE_IN_BINLOG);
unknown's avatar
unknown committed
203
sys_var_const_str       sys_charset_system(&vars, "character_set_system",
204
                                           (char *)my_charset_utf8_general_ci.name);
205 206 207 208 209 210 211 212 213 214 215 216 217
static sys_var_character_set_database
sys_character_set_database(&vars, "character_set_database",
                           sys_var::SESSION_VARIABLE_IN_BINLOG);
static sys_var_character_set_client
sys_character_set_client(&vars, "character_set_client",
                         &SV::character_set_client,
                         &default_charset_info,
                         sys_var::SESSION_VARIABLE_IN_BINLOG);
static sys_var_character_set_sv
sys_character_set_connection(&vars, "character_set_connection",
                             &SV::collation_connection,
                             &default_charset_info, 0,
                             sys_var::SESSION_VARIABLE_IN_BINLOG);
unknown's avatar
unknown committed
218 219 220 221 222 223 224
static sys_var_character_set_sv sys_character_set_results(&vars, "character_set_results",
                                        &SV::character_set_results,
                                        &default_charset_info, true);
static sys_var_character_set_sv sys_character_set_filesystem(&vars, "character_set_filesystem",
                                        &SV::character_set_filesystem,
                                        &character_set_filesystem);
static sys_var_thd_ulong	sys_completion_type(&vars, "completion_type",
unknown's avatar
unknown committed
225 226 227
					 &SV::completion_type,
					 check_completion_type,
					 fix_completion_type);
228 229 230 231 232 233 234 235 236 237 238 239
static sys_var_collation_sv
sys_collation_connection(&vars, "collation_connection",
                         &SV::collation_connection, &default_charset_info,
                         sys_var::SESSION_VARIABLE_IN_BINLOG);
static sys_var_collation_sv
sys_collation_database(&vars, "collation_database", &SV::collation_database,
                       &default_charset_info,
                       sys_var::SESSION_VARIABLE_IN_BINLOG);
static sys_var_collation_sv
sys_collation_server(&vars, "collation_server", &SV::collation_server,
                     &default_charset_info,
                     sys_var::SESSION_VARIABLE_IN_BINLOG);
unknown's avatar
unknown committed
240
static sys_var_long_ptr	sys_concurrent_insert(&vars, "concurrent_insert",
241
                                              &myisam_concurrent_insert);
unknown's avatar
unknown committed
242
static sys_var_long_ptr	sys_connect_timeout(&vars, "connect_timeout",
unknown's avatar
unknown committed
243
					    &connect_timeout);
244
static sys_var_const_os_str       sys_datadir(&vars, "datadir", mysql_real_data_home);
245 246 247 248 249 250 251 252 253 254 255 256 257

static sys_var_thd_ulong sys_deadlock_search_depth_short(&vars,
                                "deadlock_search_depth_short",
                                 &SV::wt_deadlock_search_depth_short);
static sys_var_thd_ulong sys_deadlock_search_depth_long(&vars,
                                "deadlock_search_depth_long",
                                 &SV::wt_deadlock_search_depth_long);
static sys_var_thd_ulong sys_deadlock_timeout_short(&vars,
                                "deadlock_timeout_short",
                                 &SV::wt_timeout_short);
static sys_var_thd_ulong sys_deadlock_timeout_long(&vars,
                                "deadlock_timeout_long",
                                 &SV::wt_timeout_long);
unknown's avatar
unknown committed
258
#ifndef DBUG_OFF
unknown's avatar
unknown committed
259
static sys_var_thd_dbug        sys_dbug(&vars, "debug");
unknown's avatar
unknown committed
260
#endif
unknown's avatar
unknown committed
261
static sys_var_enum		sys_delay_key_write(&vars, "delay_key_write",
262 263 264
					    &delay_key_write_options,
					    &delay_key_write_typelib,
					    fix_delay_key_write);
unknown's avatar
unknown committed
265
static sys_var_long_ptr	sys_delayed_insert_limit(&vars, "delayed_insert_limit",
unknown's avatar
unknown committed
266
						 &delayed_insert_limit);
unknown's avatar
unknown committed
267
static sys_var_long_ptr	sys_delayed_insert_timeout(&vars, "delayed_insert_timeout",
unknown's avatar
unknown committed
268
						   &delayed_insert_timeout);
unknown's avatar
unknown committed
269
static sys_var_long_ptr	sys_delayed_queue_size(&vars, "delayed_queue_size",
unknown's avatar
unknown committed
270
					       &delayed_queue_size);
271

272
#ifdef HAVE_EVENT_SCHEDULER
unknown's avatar
unknown committed
273
static sys_var_event_scheduler sys_event_scheduler(&vars, "event_scheduler");
274 275
#endif

276 277 278 279 280 281 282
static sys_var_const    sys_extra_port(&vars, "extra_port",
                                       OPT_GLOBAL, SHOW_INT,
                                       (uchar*) &mysqld_extra_port);
static sys_var_long_ptr	sys_extra_max_connections(&vars,
                                                  "extra_max_connections",
                                                  &extra_max_connections,
                                                  fix_max_connections);
unknown's avatar
unknown committed
283
static sys_var_long_ptr	sys_expire_logs_days(&vars, "expire_logs_days",
unknown's avatar
unknown committed
284
					     &expire_logs_days);
unknown's avatar
unknown committed
285 286
static sys_var_bool_ptr	sys_flush(&vars, "flush", &myisam_flush);
static sys_var_long_ptr	sys_flush_time(&vars, "flush_time", &flush_time);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static sys_var_str      sys_ft_boolean_syntax(&vars, "ft_boolean_syntax",
                                              sys_check_ftb_syntax,
                                              sys_update_ftb_syntax,
                                              sys_default_ftb_syntax,
                                              ft_boolean_syntax);
static sys_var_const    sys_ft_max_word_len(&vars, "ft_max_word_len",
                                            OPT_GLOBAL, SHOW_LONG,
                                            (uchar*) &ft_max_word_len);
static sys_var_const    sys_ft_min_word_len(&vars, "ft_min_word_len",
                                            OPT_GLOBAL, SHOW_LONG,
                                            (uchar*) &ft_min_word_len);
static sys_var_const    sys_ft_query_expansion_limit(&vars,
                                                     "ft_query_expansion_limit",
                                                     OPT_GLOBAL, SHOW_LONG,
                                                     (uchar*)
                                                     &ft_query_expansion_limit);
static sys_var_const    sys_ft_stopword_file(&vars, "ft_stopword_file",
                                             OPT_GLOBAL, SHOW_CHAR_PTR,
                                             (uchar*) &ft_stopword_file);
306 307 308 309 310

static sys_var_const    sys_ignore_builtin_innodb(&vars, "ignore_builtin_innodb",
                                                  OPT_GLOBAL, SHOW_BOOL,
                                                  (uchar*) &opt_ignore_builtin_innodb);

unknown's avatar
unknown committed
311
sys_var_str             sys_init_connect(&vars, "init_connect", 0,
312 313
                                         sys_update_init_connect,
                                         sys_default_init_connect,0);
314 315 316
static sys_var_const    sys_init_file(&vars, "init_file",
                                      OPT_GLOBAL, SHOW_CHAR_PTR,
                                      (uchar*) &opt_init_file);
unknown's avatar
unknown committed
317
sys_var_str             sys_init_slave(&vars, "init_slave", 0,
318 319
                                       sys_update_init_slave,
                                       sys_default_init_slave,0);
unknown's avatar
unknown committed
320
static sys_var_thd_ulong	sys_interactive_timeout(&vars, "interactive_timeout",
unknown's avatar
unknown committed
321
						&SV::net_interactive_timeout);
unknown's avatar
unknown committed
322
static sys_var_thd_ulong	sys_join_buffer_size(&vars, "join_buffer_size",
unknown's avatar
unknown committed
323
					     &SV::join_buff_size);
324 325 326
static sys_var_thd_ulonglong    sys_join_buffer_space_limit(&vars,
                                                "join_buffer_space_limit",
					        &SV::join_buff_space_limit);
327 328
static sys_var_thd_ulong	sys_join_cache_level(&vars, "join_cache_level",
					             &SV::join_cache_level);
unknown's avatar
unknown committed
329
static sys_var_key_buffer_size	sys_key_buffer_size(&vars, "key_buffer_size");
330 331 332 333 334 335 336 337 338 339
static sys_var_key_cache_long  sys_key_cache_block_size(&vars,
                                   "key_cache_block_size",
                                   offsetof(KEY_CACHE,param_block_size));
static sys_var_key_cache_long  sys_key_cache_division_limit(&vars,
                                   "key_cache_division_limit",
                                   offsetof(KEY_CACHE, param_division_limit));
static sys_var_key_cache_long  sys_key_cache_age_threshold(&vars,
                                   "key_cache_age_threshold",
                                   offsetof(KEY_CACHE, param_age_threshold));
static sys_var_key_cache_long  sys_key_cache_partitions(&vars,
340
                                   "key_cache_segments",
341
                                   offsetof(KEY_CACHE, param_partitions));
342 343 344 345 346 347 348 349 350 351 352 353
static sys_var_const    sys_language(&vars, "language",
                                     OPT_GLOBAL, SHOW_CHAR,
                                     (uchar*) language);
static sys_var_const    sys_large_files_support(&vars, "large_files_support",
                                                OPT_GLOBAL, SHOW_BOOL,
                                                (uchar*) &opt_large_files);
static sys_var_const    sys_large_page_size(&vars, "large_page_size",
                                            OPT_GLOBAL, SHOW_INT,
                                            (uchar*) &opt_large_page_size);
static sys_var_const    sys_large_pages(&vars, "large_pages",
                                        OPT_GLOBAL, SHOW_MY_BOOL,
                                        (uchar*) &opt_large_pages);
unknown's avatar
unknown committed
354
static sys_var_bool_ptr	sys_local_infile(&vars, "local_infile",
unknown's avatar
unknown committed
355
					 &opt_local_infile);
356 357 358 359 360 361 362 363
#ifdef HAVE_MLOCKALL
static sys_var_const    sys_locked_in_memory(&vars, "locked_in_memory",
                                             OPT_GLOBAL, SHOW_MY_BOOL,
                                             (uchar*) &locked_in_memory);
#endif
static sys_var_const    sys_log_bin(&vars, "log_bin",
                                    OPT_GLOBAL, SHOW_BOOL,
                                    (uchar*) &opt_bin_log);
unknown's avatar
unknown committed
364 365
static sys_var_trust_routine_creators
sys_trust_routine_creators(&vars, "log_bin_trust_routine_creators",
366
                           &trust_function_creators);
367
static sys_var_bool_ptr
unknown's avatar
unknown committed
368
sys_trust_function_creators(&vars, "log_bin_trust_function_creators",
369
                            &trust_function_creators);
370 371 372
static sys_var_const    sys_log_error(&vars, "log_error",
                                      OPT_GLOBAL, SHOW_CHAR,
                                      (uchar*) log_error_file);
unknown's avatar
unknown committed
373 374
static sys_var_bool_ptr
  sys_log_queries_not_using_indexes(&vars, "log_queries_not_using_indexes",
375
                                    &opt_log_queries_not_using_indexes);
unknown's avatar
unknown committed
376
static sys_var_thd_ulong	sys_log_warnings(&vars, "log_warnings", &SV::log_warnings);
377 378
static sys_var_microseconds	sys_var_long_query_time(&vars, "long_query_time",
                                                        &SV::long_query_time);
379 380 381
static sys_var_microseconds	sys_var_long_query_time2(&vars,
                                                         "log_slow_time",
                                                         &SV::long_query_time);
unknown's avatar
unknown committed
382
static sys_var_thd_bool	sys_low_priority_updates(&vars, "low_priority_updates",
unknown's avatar
unknown committed
383 384 385
						 &SV::low_priority_updates,
						 fix_low_priority_updates);
#ifndef TO_BE_DELETED	/* Alias for the low_priority_updates */
unknown's avatar
unknown committed
386
static sys_var_thd_bool	sys_sql_low_priority_updates(&vars, "sql_low_priority_updates",
unknown's avatar
unknown committed
387 388 389
						     &SV::low_priority_updates,
						     fix_low_priority_updates);
#endif
390 391 392 393 394 395 396 397 398 399
static sys_var_const    sys_lower_case_file_system(&vars,
                                                   "lower_case_file_system",
                                                   OPT_GLOBAL, SHOW_MY_BOOL,
                                                   (uchar*)
                                                   &lower_case_file_system);
static sys_var_const    sys_lower_case_table_names(&vars,
                                                   "lower_case_table_names",
                                                   OPT_GLOBAL, SHOW_INT,
                                                   (uchar*)
                                                   &lower_case_table_names);
400
static sys_var_thd_ulong_session_readonly sys_max_allowed_packet(&vars, "max_allowed_packet",
unknown's avatar
unknown committed
401
					       &SV::max_allowed_packet);
402 403
static sys_var_ulonglong_ptr sys_max_binlog_cache_size(&vars, "max_binlog_cache_size",
                                                       &max_binlog_cache_size);
unknown's avatar
unknown committed
404
static sys_var_long_ptr	sys_max_binlog_size(&vars, "max_binlog_size",
405 406
					    &max_binlog_size,
                                            fix_max_binlog_size);
unknown's avatar
unknown committed
407
static sys_var_long_ptr	sys_max_connections(&vars, "max_connections",
408 409
					    &max_connections,
                                            fix_max_connections);
unknown's avatar
unknown committed
410
static sys_var_long_ptr	sys_max_connect_errors(&vars, "max_connect_errors",
unknown's avatar
unknown committed
411
					       &max_connect_errors);
unknown's avatar
unknown committed
412
static sys_var_thd_ulong       sys_max_insert_delayed_threads(&vars, "max_insert_delayed_threads",
413 414 415
						       &SV::max_insert_delayed_threads,
                                                       check_max_delayed_threads,
                                                       fix_max_connections);
unknown's avatar
unknown committed
416
static sys_var_thd_ulong	sys_max_delayed_threads(&vars, "max_delayed_threads",
417
						&SV::max_insert_delayed_threads,
418 419
                                                check_max_delayed_threads,
                                                fix_max_connections);
unknown's avatar
unknown committed
420
static sys_var_thd_ulong	sys_max_error_count(&vars, "max_error_count",
421
					    &SV::max_error_count);
unknown's avatar
unknown committed
422
static sys_var_thd_ulonglong	sys_max_heap_table_size(&vars, "max_heap_table_size",
unknown's avatar
unknown committed
423
						&SV::max_heap_table_size);
424 425 426 427
static sys_var_thd_ulong sys_pseudo_thread_id(&vars, "pseudo_thread_id",
                                              &SV::pseudo_thread_id,
                                              check_pseudo_thread_id, 0,
                                              sys_var::SESSION_VARIABLE_IN_BINLOG);
unknown's avatar
unknown committed
428
static sys_var_thd_ha_rows	sys_max_join_size(&vars, "max_join_size",
unknown's avatar
unknown committed
429 430
					  &SV::max_join_size,
					  fix_max_join_size);
unknown's avatar
unknown committed
431
static sys_var_thd_ulong	sys_max_seeks_for_key(&vars, "max_seeks_for_key",
432
					      &SV::max_seeks_for_key);
unknown's avatar
unknown committed
433
static sys_var_thd_ulong   sys_max_length_for_sort_data(&vars, "max_length_for_sort_data",
434
                                                 &SV::max_length_for_sort_data);
unknown's avatar
unknown committed
435
#ifndef TO_BE_DELETED	/* Alias for max_join_size */
unknown's avatar
unknown committed
436
static sys_var_thd_ha_rows	sys_sql_max_join_size(&vars, "sql_max_join_size",
unknown's avatar
unknown committed
437 438 439
					      &SV::max_join_size,
					      fix_max_join_size);
#endif
440
static sys_var_long_ptr_global
unknown's avatar
unknown committed
441
sys_max_prepared_stmt_count(&vars, "max_prepared_stmt_count",
442 443
                            &max_prepared_stmt_count,
                            &LOCK_prepared_stmt_count);
unknown's avatar
unknown committed
444
static sys_var_long_ptr	sys_max_relay_log_size(&vars, "max_relay_log_size",
445 446
                                               &max_relay_log_size,
                                               fix_max_relay_log_size);
unknown's avatar
unknown committed
447
static sys_var_thd_ulong	sys_max_sort_length(&vars, "max_sort_length",
unknown's avatar
unknown committed
448
					    &SV::max_sort_length);
unknown's avatar
unknown committed
449
static sys_var_thd_ulong	sys_max_sp_recursion_depth(&vars, "max_sp_recursion_depth",
450
                                                   &SV::max_sp_recursion_depth);
unknown's avatar
unknown committed
451 452
static sys_var_max_user_conn   sys_max_user_connections(&vars, "max_user_connections");
static sys_var_thd_ulong	sys_max_tmp_tables(&vars, "max_tmp_tables",
unknown's avatar
unknown committed
453
					   &SV::max_tmp_tables);
unknown's avatar
unknown committed
454
static sys_var_long_ptr	sys_max_write_lock_count(&vars, "max_write_lock_count",
unknown's avatar
unknown committed
455
						 &max_write_lock_count);
456 457
static sys_var_thd_ulong       sys_min_examined_row_limit(&vars, "min_examined_row_limit",
                                                          &SV::min_examined_row_limit);
unknown's avatar
unknown committed
458
static sys_var_long_ptr	sys_myisam_data_pointer_size(&vars, "myisam_data_pointer_size",
unknown's avatar
unknown committed
459
                                                    &myisam_data_pointer_size);
unknown's avatar
unknown committed
460
static sys_var_thd_ulonglong	sys_myisam_max_sort_file_size(&vars, "myisam_max_sort_file_size", &SV::myisam_max_sort_file_size, fix_myisam_max_sort_file_size, 1);
461 462 463 464
static sys_var_const sys_myisam_recover_options(&vars, "myisam_recover_options",
                                                OPT_GLOBAL, SHOW_CHAR_PTR,
                                                (uchar*)
                                                &myisam_recover_options_str);
unknown's avatar
unknown committed
465 466
static sys_var_thd_ulong       sys_myisam_repair_threads(&vars, "myisam_repair_threads", &SV::myisam_repair_threads);
static sys_var_thd_ulong	sys_myisam_sort_buffer_size(&vars, "myisam_sort_buffer_size", &SV::myisam_sort_buff_size);
unknown's avatar
unknown committed
467
static sys_var_bool_ptr	sys_myisam_use_mmap(&vars, "myisam_use_mmap",
unknown's avatar
unknown committed
468
                                            &opt_myisam_use_mmap);
469

unknown's avatar
unknown committed
470
static sys_var_thd_enum         sys_myisam_stats_method(&vars, "myisam_stats_method",
471 472 473 474
                                                &SV::myisam_stats_method,
                                                &myisam_stats_method_typelib,
                                                NULL);

475 476 477 478
static sys_var_const            sys_myisam_block_size(&vars, "myisam_block_size",
                                             OPT_GLOBAL, SHOW_LONG,
                                             (uchar*) &myisam_block_size);

479 480 481 482 483 484 485
#ifdef __NT__
/* purecov: begin inspected */
static sys_var_const            sys_named_pipe(&vars, "named_pipe",
                                               OPT_GLOBAL, SHOW_MY_BOOL,
                                               (uchar*) &opt_enable_named_pipe);
/* purecov: end */
#endif
486
static sys_var_thd_ulong_session_readonly sys_net_buffer_length(&vars, "net_buffer_length",
unknown's avatar
unknown committed
487
					      &SV::net_buffer_length);
unknown's avatar
unknown committed
488
static sys_var_thd_ulong	sys_net_read_timeout(&vars, "net_read_timeout",
unknown's avatar
unknown committed
489
					     &SV::net_read_timeout,
490
					     0, fix_net_read_timeout);
unknown's avatar
unknown committed
491
static sys_var_thd_ulong	sys_net_write_timeout(&vars, "net_write_timeout",
unknown's avatar
unknown committed
492
					      &SV::net_write_timeout,
493
					      0, fix_net_write_timeout);
unknown's avatar
unknown committed
494
static sys_var_thd_ulong	sys_net_retry_count(&vars, "net_retry_count",
495
					    &SV::net_retry_count,
496
					    0, fix_net_retry_count);
unknown's avatar
unknown committed
497
static sys_var_thd_bool	sys_new_mode(&vars, "new", &SV::new_mode);
unknown's avatar
unknown committed
498
static sys_var_bool_ptr_readonly sys_old_mode(&vars, "old",
unknown's avatar
unknown committed
499
                                       &global_system_variables.old_mode);
unknown's avatar
unknown committed
500 501 502 503
/* these two cannot be static */
sys_var_thd_bool                sys_old_alter_table(&vars, "old_alter_table",
                                            &SV::old_alter_table);
sys_var_thd_bool                sys_old_passwords(&vars, "old_passwords", &SV::old_passwords);
504 505 506 507
static sys_var_const            sys_open_files_limit(&vars, "open_files_limit",
                                                     OPT_GLOBAL, SHOW_LONG,
                                                     (uchar*)
                                                     &open_files_limit);
unknown's avatar
unknown committed
508
static sys_var_thd_ulong        sys_optimizer_prune_level(&vars, "optimizer_prune_level",
509
                                                  &SV::optimizer_prune_level);
unknown's avatar
unknown committed
510
static sys_var_thd_ulong        sys_optimizer_search_depth(&vars, "optimizer_search_depth",
511
                                                   &SV::optimizer_search_depth);
512 513
static sys_var_thd_optimizer_switch   sys_optimizer_switch(&vars, "optimizer_switch",
                                     &SV::optimizer_switch);
514 515 516 517 518 519 520 521 522 523 524 525

const char *optimizer_use_mrr_names[] = {"auto", "force", "disable", NullS};
TYPELIB optimizer_use_mrr_typelib= {
  array_elements(optimizer_use_mrr_names) - 1, "",
  optimizer_use_mrr_names, NULL
};

static sys_var_thd_enum        sys_optimizer_use_mrr(&vars, "optimizer_use_mrr",
                                              &SV::optimizer_use_mrr,
                                              &optimizer_use_mrr_typelib,
                                              NULL);

526 527 528
static sys_var_const            sys_pid_file(&vars, "pid_file",
                                             OPT_GLOBAL, SHOW_CHAR,
                                             (uchar*) pidfile_name);
529
static sys_var_const_os         sys_plugin_dir(&vars, "plugin_dir",
530 531 532 533 534
                                               OPT_GLOBAL, SHOW_CHAR,
                                               (uchar*) opt_plugin_dir);
static sys_var_const            sys_port(&vars, "port",
                                         OPT_GLOBAL, SHOW_INT,
                                         (uchar*) &mysqld_port);
unknown's avatar
unknown committed
535
static sys_var_thd_ulong        sys_preload_buff_size(&vars, "preload_buffer_size",
unknown's avatar
unknown committed
536
                                              &SV::preload_buff_size);
537 538 539 540
static sys_var_const            sys_protocol_version(&vars, "protocol_version",
                                                     OPT_GLOBAL, SHOW_INT,
                                                     (uchar*)
                                                     &protocol_version);
unknown's avatar
unknown committed
541
static sys_var_thd_ulong	sys_read_buff_size(&vars, "read_buffer_size",
unknown's avatar
unknown committed
542
					   &SV::read_buff_size);
unknown's avatar
unknown committed
543
static sys_var_opt_readonly	sys_readonly(&vars, "read_only", &opt_readonly);
544 545 546
static sys_var_bool_ptr	        sys_userstat(&vars, "userstat",
                                             &opt_userstat_running);

unknown's avatar
unknown committed
547
static sys_var_thd_ulong	sys_read_rnd_buff_size(&vars, "read_rnd_buffer_size",
unknown's avatar
unknown committed
548
					       &SV::read_rnd_buff_size);
Sergey Petrunya's avatar
Sergey Petrunya committed
549 550
static sys_var_thd_ulong	sys_mrr_buff_size(&vars, "mrr_buffer_size",
					          &SV::mrr_buff_size);
unknown's avatar
unknown committed
551
static sys_var_thd_ulong	sys_div_precincrement(&vars, "div_precision_increment",
unknown's avatar
unknown committed
552
                                              &SV::div_precincrement);
unknown's avatar
unknown committed
553
static sys_var_long_ptr	sys_rpl_recovery_rank(&vars, "rpl_recovery_rank",
unknown's avatar
unknown committed
554
					      &rpl_recovery_rank);
unknown's avatar
unknown committed
555
static sys_var_long_ptr	sys_query_cache_size(&vars, "query_cache_size",
unknown's avatar
unknown committed
556 557
					     &query_cache_size,
					     fix_query_cache_size);
558

unknown's avatar
unknown committed
559
static sys_var_thd_ulong	sys_range_alloc_block_size(&vars, "range_alloc_block_size",
560
						   &SV::range_alloc_block_size);
561 562 563
static sys_var_thd_ulong	sys_rowid_merge_buff_size(&vars, "rowid_merge_buff_size",
					   &SV::rowid_merge_buff_size);

unknown's avatar
unknown committed
564
static sys_var_thd_ulong	sys_query_alloc_block_size(&vars, "query_alloc_block_size",
565 566
						   &SV::query_alloc_block_size,
						   0, fix_thd_mem_root);
unknown's avatar
unknown committed
567
static sys_var_thd_ulong	sys_query_prealloc_size(&vars, "query_prealloc_size",
568 569
						&SV::query_prealloc_size,
						0, fix_thd_mem_root);
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
#ifdef HAVE_SMEM
/* purecov: begin tested */
static sys_var_const    sys_shared_memory(&vars, "shared_memory",
                                          OPT_GLOBAL, SHOW_MY_BOOL,
                                          (uchar*)
                                          &opt_enable_shared_memory);
static sys_var_const    sys_shared_memory_base_name(&vars,
                                                    "shared_memory_base_name",
                                                    OPT_GLOBAL, SHOW_CHAR_PTR,
                                                    (uchar*)
                                                    &shared_memory_base_name);
/* purecov: end */
#endif
static sys_var_const    sys_skip_external_locking(&vars,
                                                  "skip_external_locking",
                                                  OPT_GLOBAL, SHOW_MY_BOOL,
                                                  (uchar*)
                                                  &my_disable_locking);
static sys_var_const    sys_skip_networking(&vars, "skip_networking",
                                            OPT_GLOBAL, SHOW_BOOL,
                                            (uchar*) &opt_disable_networking);
static sys_var_const    sys_skip_show_database(&vars, "skip_show_database",
                                            OPT_GLOBAL, SHOW_BOOL,
                                            (uchar*) &opt_skip_show_db);
594

595 596 597 598
static sys_var_const    sys_skip_name_resolve(&vars, "skip_name_resolve",
                                            OPT_GLOBAL, SHOW_BOOL,
                                            (uchar*) &opt_skip_name_resolve);

599 600 601
static sys_var_const    sys_socket(&vars, "socket",
                                   OPT_GLOBAL, SHOW_CHAR_PTR,
                                   (uchar*) &mysqld_unix_port);
602

603 604 605 606 607 608 609 610 611 612
#ifdef HAVE_THR_SETCONCURRENCY
/* purecov: begin tested */
static sys_var_const    sys_thread_concurrency(&vars, "thread_concurrency",
                                               OPT_GLOBAL, SHOW_LONG,
                                               (uchar*) &concurrency);
/* purecov: end */
#endif
static sys_var_const    sys_thread_stack(&vars, "thread_stack",
                                         OPT_GLOBAL, SHOW_LONG,
                                         (uchar*) &my_thread_stack_size);
613
static sys_var_readonly_os      sys_tmpdir(&vars, "tmpdir", OPT_GLOBAL, SHOW_CHAR, get_tmpdir);
unknown's avatar
unknown committed
614
static sys_var_thd_ulong	sys_trans_alloc_block_size(&vars, "transaction_alloc_block_size",
615 616
						   &SV::trans_alloc_block_size,
						   0, fix_trans_mem_root);
unknown's avatar
unknown committed
617
static sys_var_thd_ulong	sys_trans_prealloc_size(&vars, "transaction_prealloc_size",
618 619
						&SV::trans_prealloc_size,
						0, fix_trans_mem_root);
unknown's avatar
unknown committed
620
sys_var_enum_const        sys_thread_handling(&vars, "thread_handling",
621 622
                                              &thread_handling,
                                              &thread_handling_typelib);
623

unknown's avatar
unknown committed
624
#ifdef HAVE_QUERY_CACHE
unknown's avatar
unknown committed
625
static sys_var_long_ptr	sys_query_cache_limit(&vars, "query_cache_limit",
unknown's avatar
unknown committed
626
					      &query_cache.query_cache_limit);
unknown's avatar
unknown committed
627
static sys_var_long_ptr        sys_query_cache_min_res_unit(&vars, "query_cache_min_res_unit",
628 629
						     &query_cache_min_res_unit,
						     fix_query_cache_min_res_unit);
unknown's avatar
unknown committed
630
static sys_var_thd_enum	sys_query_cache_type(&vars, "query_cache_type",
unknown's avatar
unknown committed
631 632
					     &SV::query_cache_type,
					     &query_cache_type_typelib);
unknown's avatar
unknown committed
633 634
static sys_var_thd_bool
sys_query_cache_wlock_invalidate(&vars, "query_cache_wlock_invalidate",
635
				 &SV::query_cache_wlock_invalidate);
unknown's avatar
unknown committed
636
#endif /* HAVE_QUERY_CACHE */
unknown's avatar
unknown committed
637
static sys_var_bool_ptr	sys_secure_auth(&vars, "secure_auth", &opt_secure_auth);
638
static sys_var_const_str_ptr sys_secure_file_priv(&vars, "secure_file_priv",
639
                                             &opt_secure_file_priv);
unknown's avatar
unknown committed
640 641
static sys_var_long_ptr	sys_server_id(&vars, "server_id", &server_id, fix_server_id);
static sys_var_bool_ptr	sys_slave_compressed_protocol(&vars, "slave_compressed_protocol",
642
						      &opt_slave_compressed_protocol);
643 644 645 646 647
static sys_var_set_slave_mode slave_exec_mode(&vars,
                                              "slave_exec_mode",
                                              &slave_exec_mode_options,
                                              &slave_exec_mode_typelib,
                                              0);
unknown's avatar
unknown committed
648
static sys_var_long_ptr	sys_slow_launch_time(&vars, "slow_launch_time",
unknown's avatar
unknown committed
649
					     &slow_launch_time);
unknown's avatar
unknown committed
650
static sys_var_thd_ulong	sys_sort_buffer(&vars, "sort_buffer_size",
unknown's avatar
unknown committed
651
					&SV::sortbuff_size);
652 653 654 655 656 657
/*
  sql_mode should *not* have binlog_mode=SESSION_VARIABLE_IN_BINLOG:
  even though it is written to the binlog, the slave ignores the
  MODE_NO_DIR_IN_CREATE variable, so slave's value differs from
  master's (see log_event.cc: Query_log_event::do_apply_event()).
*/
unknown's avatar
unknown committed
658
static sys_var_thd_sql_mode    sys_sql_mode(&vars, "sql_mode",
659
                                            &SV::sql_mode);
660 661 662
#ifdef HAVE_OPENSSL
extern char *opt_ssl_ca, *opt_ssl_capath, *opt_ssl_cert, *opt_ssl_cipher,
            *opt_ssl_key;
663 664 665 666 667
static sys_var_const_os_str_ptr	sys_ssl_ca(&vars, "ssl_ca", &opt_ssl_ca);
static sys_var_const_os_str_ptr	sys_ssl_capath(&vars, "ssl_capath", &opt_ssl_capath);
static sys_var_const_os_str_ptr	sys_ssl_cert(&vars, "ssl_cert", &opt_ssl_cert);
static sys_var_const_os_str_ptr	sys_ssl_cipher(&vars, "ssl_cipher", &opt_ssl_cipher);
static sys_var_const_os_str_ptr	sys_ssl_key(&vars, "ssl_key", &opt_ssl_key);
668
#else
669 670 671 672 673
static sys_var_const_os_str	sys_ssl_ca(&vars, "ssl_ca", NULL);
static sys_var_const_os_str	sys_ssl_capath(&vars, "ssl_capath", NULL);
static sys_var_const_os_str	sys_ssl_cert(&vars, "ssl_cert", NULL);
static sys_var_const_os_str	sys_ssl_cipher(&vars, "ssl_cipher", NULL);
static sys_var_const_os_str	sys_ssl_key(&vars, "ssl_key", NULL);
674
#endif
unknown's avatar
unknown committed
675 676
static sys_var_thd_enum
sys_updatable_views_with_limit(&vars, "updatable_views_with_limit",
677 678
                               &SV::updatable_views_with_limit,
                               &updatable_views_with_limit_typelib);
unknown's avatar
VIEW  
unknown committed
679

unknown's avatar
unknown committed
680 681 682 683 684 685
static sys_var_thd_table_type  sys_table_type(&vars, "table_type",
				       &SV::table_plugin);
static sys_var_thd_storage_engine sys_storage_engine(&vars, "storage_engine",
				       &SV::table_plugin);
static sys_var_bool_ptr	sys_sync_frm(&vars, "sync_frm", &opt_sync_frm);
static sys_var_const_str	sys_system_time_zone(&vars, "system_time_zone",
686
                                             system_time_zone);
unknown's avatar
unknown committed
687
static sys_var_long_ptr	sys_table_def_size(&vars, "table_definition_cache",
unknown's avatar
unknown committed
688
                                           &table_def_size);
unknown's avatar
unknown committed
689
static sys_var_long_ptr	sys_table_cache_size(&vars, "table_open_cache",
unknown's avatar
unknown committed
690
					     &table_cache_size);
unknown's avatar
unknown committed
691
static sys_var_long_ptr	sys_table_lock_wait_timeout(&vars, "table_lock_wait_timeout",
692
                                                    &table_lock_wait_timeout);
693 694 695 696 697 698

#if defined(ENABLED_DEBUG_SYNC)
/* Debug Sync Facility. Implemented in debug_sync.cc. */
static sys_var_debug_sync sys_debug_sync(&vars, "debug_sync");
#endif /* defined(ENABLED_DEBUG_SYNC) */

unknown's avatar
unknown committed
699
static sys_var_long_ptr	sys_thread_cache_size(&vars, "thread_cache_size",
unknown's avatar
unknown committed
700
					      &thread_cache_size);
unknown's avatar
unknown committed
701
#if HAVE_POOL_OF_THREADS == 1
702 703
static sys_var_long_ptr sys_thread_pool_size(&vars, "thread_pool_size",
                                             &thread_pool_size);
unknown's avatar
unknown committed
704
#endif
unknown's avatar
unknown committed
705
static sys_var_thd_enum	sys_tx_isolation(&vars, "tx_isolation",
unknown's avatar
unknown committed
706 707
					 &SV::tx_isolation,
					 &tx_isolation_typelib,
708 709
					 fix_tx_isolation,
					 check_tx_isolation);
unknown's avatar
unknown committed
710
static sys_var_thd_ulonglong	sys_tmp_table_size(&vars, "tmp_table_size",
unknown's avatar
unknown committed
711
					   &SV::tmp_table_size);
unknown's avatar
unknown committed
712
static sys_var_bool_ptr  sys_timed_mutexes(&vars, "timed_mutexes",
unknown's avatar
unknown committed
713
                                    &timed_mutexes);
unknown's avatar
unknown committed
714 715
static sys_var_const_str	sys_version(&vars, "version", server_version);
static sys_var_const_str	sys_version_comment(&vars, "version_comment",
716
                                            MYSQL_COMPILATION_COMMENT);
unknown's avatar
unknown committed
717
static sys_var_const_str	sys_version_compile_machine(&vars, "version_compile_machine",
718
                                                    MACHINE_TYPE);
unknown's avatar
unknown committed
719
static sys_var_const_str	sys_version_compile_os(&vars, "version_compile_os",
720
                                               SYSTEM_TYPE);
unknown's avatar
unknown committed
721
static sys_var_thd_ulong	sys_net_wait_timeout(&vars, "wait_timeout",
unknown's avatar
unknown committed
722
					     &SV::net_wait_timeout);
unknown's avatar
unknown committed
723

unknown's avatar
unknown committed
724
/* Condition pushdown to storage engine */
unknown's avatar
unknown committed
725 726
static sys_var_thd_bool
sys_engine_condition_pushdown(&vars, "engine_condition_pushdown",
unknown's avatar
unknown committed
727 728
			      &SV::engine_condition_pushdown);

729
#ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
730
/* ndb thread specific variable settings */
unknown's avatar
unknown committed
731 732
static sys_var_thd_ulong
sys_ndb_autoincrement_prefetch_sz(&vars, "ndb_autoincrement_prefetch_sz",
733
				  &SV::ndb_autoincrement_prefetch_sz);
unknown's avatar
unknown committed
734 735
static sys_var_thd_bool
sys_ndb_force_send(&vars, "ndb_force_send", &SV::ndb_force_send);
unknown's avatar
unknown committed
736
#ifdef HAVE_NDB_BINLOG
unknown's avatar
unknown committed
737 738
static sys_var_long_ptr
sys_ndb_report_thresh_binlog_epoch_slip(&vars, "ndb_report_thresh_binlog_epoch_slip",
unknown's avatar
unknown committed
739
                                        &ndb_report_thresh_binlog_epoch_slip);
unknown's avatar
unknown committed
740 741
static sys_var_long_ptr
sys_ndb_report_thresh_binlog_mem_usage(&vars, "ndb_report_thresh_binlog_mem_usage",
unknown's avatar
unknown committed
742 743
                                       &ndb_report_thresh_binlog_mem_usage);
#endif
unknown's avatar
unknown committed
744 745 746 747 748 749
static sys_var_thd_bool
sys_ndb_use_exact_count(&vars, "ndb_use_exact_count", &SV::ndb_use_exact_count);
static sys_var_thd_bool
sys_ndb_use_transactions(&vars, "ndb_use_transactions", &SV::ndb_use_transactions);
static sys_var_long_ptr
sys_ndb_cache_check_time(&vars, "ndb_cache_check_time", &ndb_cache_check_time);
750 751
static sys_var_const_str
sys_ndb_connectstring(&vars, "ndb_connectstring", opt_ndb_constrbuf);
unknown's avatar
unknown committed
752 753
static sys_var_thd_bool
sys_ndb_index_stat_enable(&vars, "ndb_index_stat_enable",
754
                          &SV::ndb_index_stat_enable);
unknown's avatar
unknown committed
755 756
static sys_var_thd_ulong
sys_ndb_index_stat_cache_entries(&vars, "ndb_index_stat_cache_entries",
757
                                 &SV::ndb_index_stat_cache_entries);
unknown's avatar
unknown committed
758 759
static sys_var_thd_ulong
sys_ndb_index_stat_update_freq(&vars, "ndb_index_stat_update_freq",
760
                               &SV::ndb_index_stat_update_freq);
unknown's avatar
unknown committed
761 762 763 764
static sys_var_long_ptr
sys_ndb_extra_logging(&vars, "ndb_extra_logging", &ndb_extra_logging);
static sys_var_thd_bool
sys_ndb_use_copying_alter_table(&vars, "ndb_use_copying_alter_table", &SV::ndb_use_copying_alter_table);
765
#endif //WITH_NDBCLUSTER_STORAGE_ENGINE
766 767 768

/* Time/date/datetime formats */

unknown's avatar
unknown committed
769
static sys_var_thd_date_time_format sys_time_format(&vars, "time_format",
770
					     &SV::time_format,
771
					     MYSQL_TIMESTAMP_TIME);
unknown's avatar
unknown committed
772
static sys_var_thd_date_time_format sys_date_format(&vars, "date_format",
773
					     &SV::date_format,
774
					     MYSQL_TIMESTAMP_DATE);
unknown's avatar
unknown committed
775
static sys_var_thd_date_time_format sys_datetime_format(&vars, "datetime_format",
776
						 &SV::datetime_format,
777
						 MYSQL_TIMESTAMP_DATETIME);
778 779

/* Variables that are bits in THD */
unknown's avatar
unknown committed
780

unknown's avatar
unknown committed
781
sys_var_thd_bit sys_autocommit(&vars, "autocommit", 0,
782 783 784
                               set_option_autocommit,
                               OPTION_NOT_AUTOCOMMIT,
                               1);
unknown's avatar
unknown committed
785
static sys_var_thd_bit	sys_big_tables(&vars, "big_tables", 0,
unknown's avatar
unknown committed
786 787 788
				       set_option_bit,
				       OPTION_BIG_TABLES);
#ifndef TO_BE_DELETED	/* Alias for big_tables */
unknown's avatar
unknown committed
789
static sys_var_thd_bit	sys_sql_big_tables(&vars, "sql_big_tables", 0,
unknown's avatar
unknown committed
790 791 792
					   set_option_bit,
					   OPTION_BIG_TABLES);
#endif
unknown's avatar
unknown committed
793
static sys_var_thd_bit	sys_big_selects(&vars, "sql_big_selects", 0,
unknown's avatar
unknown committed
794
					set_option_bit,
795
					OPTION_BIG_SELECTS);
unknown's avatar
unknown committed
796
static sys_var_thd_bit	sys_log_off(&vars, "sql_log_off",
797
				    check_log_update,
unknown's avatar
unknown committed
798 799
				    set_option_bit,
				    OPTION_LOG_OFF);
unknown's avatar
unknown committed
800
static sys_var_thd_bit	sys_log_update(&vars, "sql_log_update",
801
                                       check_log_update,
unknown's avatar
unknown committed
802
				       set_log_update,
803
				       OPTION_BIN_LOG);
unknown's avatar
unknown committed
804
static sys_var_thd_bit	sys_log_binlog(&vars, "sql_log_bin",
805
                                       check_log_update,
806
                                       set_option_log_bin_bit,
807
				       OPTION_BIN_LOG);
unknown's avatar
unknown committed
808
static sys_var_thd_bit	sys_sql_warnings(&vars, "sql_warnings", 0,
unknown's avatar
unknown committed
809 810
					 set_option_bit,
					 OPTION_WARNINGS);
unknown's avatar
unknown committed
811
static sys_var_thd_bit	sys_sql_notes(&vars, "sql_notes", 0,
812
					 set_option_bit,
813
					 OPTION_SQL_NOTES);
unknown's avatar
unknown committed
814
static sys_var_thd_bit	sys_auto_is_null(&vars, "sql_auto_is_null", 0,
unknown's avatar
unknown committed
815
					 set_option_bit,
816 817
                                         OPTION_AUTO_IS_NULL, 0,
                                         sys_var::SESSION_VARIABLE_IN_BINLOG);
unknown's avatar
unknown committed
818
static sys_var_thd_bit	sys_safe_updates(&vars, "sql_safe_updates", 0,
unknown's avatar
unknown committed
819 820
					 set_option_bit,
					 OPTION_SAFE_UPDATES);
unknown's avatar
unknown committed
821
static sys_var_thd_bit	sys_buffer_results(&vars, "sql_buffer_result", 0,
unknown's avatar
unknown committed
822 823
					   set_option_bit,
					   OPTION_BUFFER_RESULT);
unknown's avatar
unknown committed
824
static sys_var_thd_bit	sys_quote_show_create(&vars, "sql_quote_show_create", 0,
unknown's avatar
unknown committed
825 826
					      set_option_bit,
					      OPTION_QUOTE_SHOW_CREATE);
unknown's avatar
unknown committed
827
static sys_var_thd_bit	sys_foreign_key_checks(&vars, "foreign_key_checks", 0,
828 829
					       set_option_bit,
					       OPTION_NO_FOREIGN_KEY_CHECKS,
830
                                               1, sys_var::SESSION_VARIABLE_IN_BINLOG);
unknown's avatar
unknown committed
831
static sys_var_thd_bit	sys_unique_checks(&vars, "unique_checks", 0,
832 833
					  set_option_bit,
					  OPTION_RELAXED_UNIQUE_CHECKS,
834 835
                                          1,
                                          sys_var::SESSION_VARIABLE_IN_BINLOG);
836
#if defined(ENABLED_PROFILING) && defined(COMMUNITY_SERVER)
837 838
static sys_var_thd_bit  sys_profiling(&vars, "profiling", NULL, 
                                      set_option_bit,
839
                                      ulonglong(OPTION_PROFILING));
840
static sys_var_thd_ulong	sys_profiling_history_size(&vars, "profiling_history_size",
841 842
					      &SV::profiling_history_size);
#endif
843

unknown's avatar
unknown committed
844 845
/* Local state variables */

unknown's avatar
unknown committed
846
static sys_var_thd_ha_rows	sys_select_limit(&vars, "sql_select_limit",
unknown's avatar
unknown committed
847
						 &SV::select_limit);
848 849 850 851 852 853 854 855 856 857 858
static sys_var_timestamp sys_timestamp(&vars, "timestamp",
                                       sys_var::SESSION_VARIABLE_IN_BINLOG);
static sys_var_last_insert_id
sys_last_insert_id(&vars, "last_insert_id",
                   sys_var::SESSION_VARIABLE_IN_BINLOG);
/*
  identity is an alias for last_insert_id(), so that we are compatible
  with Sybase
*/
static sys_var_last_insert_id
sys_identity(&vars, "identity", sys_var::SESSION_VARIABLE_IN_BINLOG);
unknown's avatar
unknown committed
859

860 861
static sys_var_thd_lc_time_names
sys_lc_time_names(&vars, "lc_time_names", sys_var::SESSION_VARIABLE_IN_BINLOG);
unknown's avatar
unknown committed
862

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
/*
  insert_id should *not* be marked as written to the binlog (i.e., it
  should *not* have binlog_status==SESSION_VARIABLE_IN_BINLOG),
  because we want any statement that refers to insert_id explicitly to
  be unsafe.  (By "explicitly", we mean using @@session.insert_id,
  whereas insert_id is used "implicitly" when NULL value is inserted
  into an auto_increment column).

  We want statements referring explicitly to @@session.insert_id to be
  unsafe, because insert_id is modified internally by the slave sql
  thread when NULL values are inserted in an AUTO_INCREMENT column.
  This modification interfers with the value of the
  @@session.insert_id variable if @@session.insert_id is referred
  explicitly by an insert statement (as is seen by executing "SET
  @@session.insert_id=0; CREATE TABLE t (a INT, b INT KEY
  AUTO_INCREMENT); INSERT INTO t(a) VALUES (@@session.insert_id);" in
  statement-based logging mode: t will be different on master and
  slave).
*/
static sys_var_insert_id sys_insert_id(&vars, "insert_id");
unknown's avatar
unknown committed
883
static sys_var_readonly		sys_error_count(&vars, "error_count",
884 885 886
						OPT_SESSION,
						SHOW_LONG,
						get_error_count);
unknown's avatar
unknown committed
887
static sys_var_readonly		sys_warning_count(&vars, "warning_count",
888 889 890 891
						  OPT_SESSION,
						  SHOW_LONG,
						  get_warning_count);

892 893 894 895
static sys_var_rand_seed1 sys_rand_seed1(&vars, "rand_seed1",
                                         sys_var::SESSION_VARIABLE_IN_BINLOG);
static sys_var_rand_seed2 sys_rand_seed2(&vars, "rand_seed2",
                                         sys_var::SESSION_VARIABLE_IN_BINLOG);
unknown's avatar
unknown committed
896

unknown's avatar
unknown committed
897
static sys_var_thd_ulong        sys_default_week_format(&vars, "default_week_format",
898
					                &SV::default_week_format);
unknown's avatar
unknown committed
899

unknown's avatar
unknown committed
900
sys_var_thd_ulong               sys_group_concat_max_len(&vars, "group_concat_max_len",
unknown's avatar
unknown committed
901 902
                                                         &SV::group_concat_max_len);

903 904
sys_var_thd_time_zone sys_time_zone(&vars, "time_zone",
                                    sys_var::SESSION_VARIABLE_IN_BINLOG);
905

906 907 908 909 910 911 912 913 914 915 916 917 918 919
/* Unique variables for MariaDB */
static sys_var_thd_ulong  sys_log_slow_rate_limit(&vars,
                                                  "log_slow_rate_limit",
                                                  &SV::log_slow_rate_limit);
static sys_var_thd_set sys_log_slow_filter(&vars, "log_slow_filter",
                                           &SV::log_slow_filter,
                                           &log_slow_filter_typelib,
                                           QPLAN_VISIBLE_MASK,
                                           fix_sys_log_slow_filter);
static sys_var_thd_set sys_log_slow_verbosity(&vars,
                                              "log_slow_verbosity",
                                              &SV::log_slow_verbosity,
                                              &log_slow_verbosity_typelib);

920 921
/* Global read-only variable containing hostname */
static sys_var_const_str        sys_hostname(&vars, "hostname", glob_hostname);
922 923
static sys_var_const_str_ptr    sys_log_basename(&vars, "log_basename",
                                                 &opt_log_basename);
924 925

#ifndef EMBEDDED_LIBRARY
926 927 928 929 930 931 932 933 934 935
static sys_var_const_str_ptr    sys_repl_report_host(&vars, "report_host", &report_host);
static sys_var_const_str_ptr    sys_repl_report_user(&vars, "report_user", &report_user);
static sys_var_const_str_ptr    sys_repl_report_password(&vars, "report_password", &report_password);

static uchar *slave_get_report_port(THD *thd)
{
  thd->sys_var_tmp.long_value= report_port;
  return (uchar*) &thd->sys_var_tmp.long_value;
}

unknown's avatar
unknown committed
936
static sys_var_readonly    sys_repl_report_port(&vars, "report_port", OPT_GLOBAL, SHOW_LONG, slave_get_report_port);
937

938
#endif
939

940
sys_var_thd_bool  sys_keep_files_on_create(&vars, "keep_files_on_create", 
unknown's avatar
unknown committed
941
                                           &SV::keep_files_on_create);
942 943
/* Read only variables */

unknown's avatar
unknown committed
944 945 946 947 948 949 950
static sys_var_have_variable sys_have_compress(&vars, "have_compress", &have_compress);
static sys_var_have_variable sys_have_crypt(&vars, "have_crypt", &have_crypt);
static sys_var_have_plugin sys_have_csv(&vars, "have_csv", C_STRING_WITH_LEN("csv"), MYSQL_STORAGE_ENGINE_PLUGIN);
static sys_var_have_variable sys_have_dlopen(&vars, "have_dynamic_loading", &have_dlopen);
static sys_var_have_variable sys_have_geometry(&vars, "have_geometry", &have_geometry);
static sys_var_have_plugin sys_have_innodb(&vars, "have_innodb", C_STRING_WITH_LEN("innodb"), MYSQL_STORAGE_ENGINE_PLUGIN);
static sys_var_have_plugin sys_have_ndbcluster(&vars, "have_ndbcluster", C_STRING_WITH_LEN("ndbcluster"), MYSQL_STORAGE_ENGINE_PLUGIN);
unknown's avatar
unknown committed
951 952
static sys_var_have_variable sys_have_openssl(&vars, "have_openssl", &have_ssl);
static sys_var_have_variable sys_have_ssl(&vars, "have_ssl", &have_ssl);
unknown's avatar
unknown committed
953 954
static sys_var_have_plugin sys_have_partition_db(&vars, "have_partitioning", C_STRING_WITH_LEN("partition"), MYSQL_STORAGE_ENGINE_PLUGIN);
static sys_var_have_variable sys_have_query_cache(&vars, "have_query_cache",
unknown's avatar
unknown committed
955
                                           &have_query_cache);
956
static sys_var_have_variable sys_have_community_features(&vars, "have_community_features", &have_community_features);
unknown's avatar
unknown committed
957 958
static sys_var_have_variable sys_have_rtree_keys(&vars, "have_rtree_keys", &have_rtree_keys);
static sys_var_have_variable sys_have_symlink(&vars, "have_symlink", &have_symlink);
959
/* Global read-only variable describing server license */
unknown's avatar
unknown committed
960
static sys_var_const_str	sys_license(&vars, "license", STRINGIFY_ARG(LICENSE));
961
/* Global variables which enable|disable logging */
unknown's avatar
unknown committed
962
static sys_var_log_state sys_var_general_log(&vars, "general_log", &opt_log,
963
                                      QUERY_LOG_GENERAL);
964 965 966
/* Synonym of "general_log" for consistency with SHOW VARIABLES output */
static sys_var_log_state sys_var_log(&vars, "log", &opt_log,
                                      QUERY_LOG_GENERAL);
unknown's avatar
unknown committed
967
static sys_var_log_state sys_var_slow_query_log(&vars, "slow_query_log", &opt_slow_log,
968
                                         QUERY_LOG_SLOW);
969 970 971
/* Synonym of "slow_query_log" for consistency with SHOW VARIABLES output */
static sys_var_log_state sys_var_log_slow(&vars, "log_slow_queries",
                                          &opt_slow_log, QUERY_LOG_SLOW);
unknown's avatar
unknown committed
972
sys_var_str sys_var_general_log_path(&vars, "general_log_file", sys_check_log_path,
973 974 975
				     sys_update_general_log_path,
				     sys_default_general_log_path,
				     opt_logname);
unknown's avatar
unknown committed
976
sys_var_str sys_var_slow_log_path(&vars, "slow_query_log_file", sys_check_log_path,
977 978 979
				  sys_update_slow_log_path, 
				  sys_default_slow_log_path,
				  opt_slow_logname);
unknown's avatar
unknown committed
980
static sys_var_log_output sys_var_log_output_state(&vars, "log_output", &log_output_options,
981
					    &log_output_typelib, 0);
982 983 984 985
static sys_var_readonly         sys_myisam_mmap_size(&vars, "myisam_mmap_size",
                                                     OPT_GLOBAL,
                                                     SHOW_LONGLONG,
                                                     get_myisam_mmap_size);
986

987 988 989
static sys_var_enum_const     sys_plugin_maturity(&vars, "plugin_maturity",
                                                  &plugin_maturity,
                                                  &plugin_maturity_values);
990

991 992 993 994 995 996
bool sys_var::check(THD *thd, set_var *var)
{
  var->save_result.ulonglong_value= var->value->val_int();
  return 0;
}

997 998 999 1000 1001 1002 1003
bool sys_var_str::check(THD *thd, set_var *var)
{
  int res;
  if (!check_func)
    return 0;

  if ((res=(*check_func)(thd, var)) < 0)
1004
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0),
1005
             name, var->value->str_value.c_ptr());
1006 1007 1008
  return res;
}

unknown's avatar
unknown committed
1009 1010 1011 1012
/*
  Functions to check and update variables
*/

unknown's avatar
unknown committed
1013

1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
/*
  Update variables 'init_connect, init_slave'.

  In case of 'DEFAULT' value
  (for example: 'set GLOBAL init_connect=DEFAULT')
  'var' parameter is NULL pointer.
*/

bool update_sys_var_str(sys_var_str *var_str, rw_lock_t *var_mutex,
			set_var *var)
unknown's avatar
unknown committed
1024
{
1025 1026 1027 1028
  char *res= 0, *old_value=(char *)(var ? var->value->str_value.ptr() : 0);
  uint new_length= (var ? var->value->str_value.length() : 0);
  if (!old_value)
    old_value= (char*) "";
1029
  if (!(res= my_strndup(old_value, new_length, MYF(0))))
1030
    return 1;
unknown's avatar
unknown committed
1031 1032 1033 1034
  /*
    Replace the old value in such a way that the any thread using
    the value will work.
  */
1035 1036 1037 1038
  rw_wrlock(var_mutex);
  old_value= var_str->value;
  var_str->value= res;
  var_str->value_length= new_length;
1039
  var_str->is_os_charset= FALSE;
1040
  rw_unlock(var_mutex);
unknown's avatar
unknown committed
1041 1042 1043 1044 1045
  my_free(old_value, MYF(MY_ALLOW_ZERO_PTR));
  return 0;
}


1046 1047 1048 1049 1050 1051
static bool sys_update_init_connect(THD *thd, set_var *var)
{
  return update_sys_var_str(&sys_init_connect, &LOCK_sys_init_connect, var);
}


unknown's avatar
unknown committed
1052 1053
static void sys_default_init_connect(THD* thd, enum_var_type type)
{
1054
  update_sys_var_str(&sys_init_connect, &LOCK_sys_init_connect, 0);
unknown's avatar
unknown committed
1055 1056 1057 1058 1059
}


static bool sys_update_init_slave(THD *thd, set_var *var)
{
1060
  return update_sys_var_str(&sys_init_slave, &LOCK_sys_init_slave, var);
unknown's avatar
unknown committed
1061 1062 1063 1064 1065
}


static void sys_default_init_slave(THD* thd, enum_var_type type)
{
1066
  update_sys_var_str(&sys_init_slave, &LOCK_sys_init_slave, 0);
unknown's avatar
unknown committed
1067 1068
}

1069 1070
static int sys_check_ftb_syntax(THD *thd,  set_var *var)
{
1071
  if (thd->security_ctx->master_access & SUPER_ACL)
1072
    return (ft_boolean_check_syntax_string((uchar*)
1073 1074
                                           var->value->str_value.c_ptr()) ?
            -1 : 0);
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
  else
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
}

static bool sys_update_ftb_syntax(THD *thd, set_var * var)
{
  strmake(ft_boolean_syntax, var->value->str_value.c_ptr(),
	  sizeof(ft_boolean_syntax)-1);
1086 1087 1088 1089 1090

#ifdef HAVE_QUERY_CACHE
  query_cache.flush();
#endif /* HAVE_QUERY_CACHE */

1091 1092 1093 1094 1095
  return 0;
}

static void sys_default_ftb_syntax(THD *thd, enum_var_type type)
{
1096
  strmake(ft_boolean_syntax, def_ft_boolean_syntax,
1097 1098
	  sizeof(ft_boolean_syntax)-1);
}
unknown's avatar
unknown committed
1099

unknown's avatar
unknown committed
1100

unknown's avatar
unknown committed
1101
/**
unknown's avatar
unknown committed
1102
  If one sets the LOW_PRIORIY UPDATES flag, we also must change the
unknown's avatar
unknown committed
1103
  used lock type.
unknown's avatar
unknown committed
1104 1105 1106 1107
*/

static void fix_low_priority_updates(THD *thd, enum_var_type type)
{
1108 1109 1110 1111 1112
  if (type == OPT_GLOBAL)
    thr_upgraded_concurrent_insert_lock= 
      (global_system_variables.low_priority_updates ?
       TL_WRITE_LOW_PRIORITY : TL_WRITE);
  else
unknown's avatar
unknown committed
1113 1114 1115 1116 1117
    thd->update_lock_default= (thd->variables.low_priority_updates ?
			       TL_WRITE_LOW_PRIORITY : TL_WRITE);
}


1118 1119 1120 1121 1122 1123 1124
static void
fix_myisam_max_sort_file_size(THD *thd, enum_var_type type)
{
  myisam_max_temp_length=
    (my_off_t) global_system_variables.myisam_max_sort_file_size;
}

unknown's avatar
unknown committed
1125 1126
/**
  Set the OPTION_BIG_SELECTS flag if max_join_size == HA_POS_ERROR.
unknown's avatar
unknown committed
1127 1128 1129 1130 1131 1132
*/

static void fix_max_join_size(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
  {
1133
    if (thd->variables.max_join_size == HA_POS_ERROR)
unknown's avatar
unknown committed
1134 1135 1136 1137 1138
      thd->options|= OPTION_BIG_SELECTS;
    else
      thd->options&= ~OPTION_BIG_SELECTS;
  }
}
1139

unknown's avatar
unknown committed
1140

unknown's avatar
unknown committed
1141
/**
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
  Can't change the 'next' tx_isolation while we are already in
  a transaction
*/
static int check_tx_isolation(THD *thd, set_var *var)
{
  if (var->type == OPT_DEFAULT && (thd->server_status & SERVER_STATUS_IN_TRANS))
  {
    my_error(ER_CANT_CHANGE_TX_ISOLATION, MYF(0));
    return 1;
  }
  return 0;
}

unknown's avatar
unknown committed
1155 1156
/*
  If one doesn't use the SESSION modifier, the isolation level
unknown's avatar
unknown committed
1157
  is only active for the next command.
unknown's avatar
unknown committed
1158 1159 1160 1161 1162 1163 1164 1165
*/
static void fix_tx_isolation(THD *thd, enum_var_type type)
{
  if (type == OPT_SESSION)
    thd->session_tx_isolation= ((enum_tx_isolation)
				thd->variables.tx_isolation);
}

1166 1167
static void fix_completion_type(THD *thd __attribute__((unused)),
				enum_var_type type __attribute__((unused))) {}
unknown's avatar
unknown committed
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

static int check_completion_type(THD *thd, set_var *var)
{
  longlong val= var->value->val_int();
  if (val < 0 || val > 2)
  {
    char buf[64];
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->var->name, llstr(val, buf));
    return 1;
  }
  return 0;
}

unknown's avatar
unknown committed
1181 1182 1183 1184 1185

/*
  If we are changing the thread variable, we have to copy it to NET too
*/

unknown's avatar
SCRUM  
unknown committed
1186
#ifdef HAVE_REPLICATION
unknown's avatar
unknown committed
1187 1188 1189
static void fix_net_read_timeout(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
1190
    my_net_set_read_timeout(&thd->net, thd->variables.net_read_timeout);
unknown's avatar
unknown committed
1191 1192 1193 1194 1195 1196
}


static void fix_net_write_timeout(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
1197
    my_net_set_write_timeout(&thd->net, thd->variables.net_write_timeout);
unknown's avatar
unknown committed
1198 1199
}

1200 1201 1202 1203 1204
static void fix_net_retry_count(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
    thd->net.retry_count=thd->variables.net_retry_count;
}
unknown's avatar
SCRUM  
unknown committed
1205
#else /* HAVE_REPLICATION */
1206 1207
static void fix_net_read_timeout(THD *thd __attribute__((unused)),
				 enum_var_type type __attribute__((unused)))
1208
{}
1209 1210
static void fix_net_write_timeout(THD *thd __attribute__((unused)),
				  enum_var_type type __attribute__((unused)))
1211
{}
1212 1213
static void fix_net_retry_count(THD *thd __attribute__((unused)),
				enum_var_type type __attribute__((unused)))
1214
{}
unknown's avatar
SCRUM  
unknown committed
1215
#endif /* HAVE_REPLICATION */
1216

unknown's avatar
unknown committed
1217 1218 1219 1220

static void fix_query_cache_size(THD *thd, enum_var_type type)
{
#ifdef HAVE_QUERY_CACHE
1221 1222 1223 1224 1225 1226 1227 1228
  ulong new_cache_size= query_cache.resize(query_cache_size);

  /*
     Note: query_cache_size is a global variable reflecting the 
     requested cache size. See also query_cache_size_arg
  */

  if (query_cache_size != new_cache_size)
1229 1230
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
			ER_WARN_QC_RESIZE, ER(ER_WARN_QC_RESIZE),
1231 1232 1233
			query_cache_size, new_cache_size);
  
  query_cache_size= new_cache_size;
unknown's avatar
unknown committed
1234 1235 1236 1237
#endif
}


1238 1239 1240 1241 1242 1243 1244 1245 1246
#ifdef HAVE_QUERY_CACHE
static void fix_query_cache_min_res_unit(THD *thd, enum_var_type type)
{
  query_cache_min_res_unit= 
    query_cache.set_min_res_unit(query_cache_min_res_unit);
}
#endif


1247
extern void fix_delay_key_write(THD *thd, enum_var_type type)
1248 1249 1250 1251
{
  switch ((enum_delay_key_write) delay_key_write_options) {
  case DELAY_KEY_WRITE_NONE:
    myisam_delay_key_write=0;
Sergei Golubchik's avatar
Sergei Golubchik committed
1252
#ifdef WITH_ARIA_STORAGE_ENGINE
1253 1254 1255
    maria_delay_key_write= 0;
#endif
    ha_open_options&= ~HA_OPEN_DELAY_KEY_WRITE;
1256 1257 1258
    break;
  case DELAY_KEY_WRITE_ON:
    myisam_delay_key_write=1;
Sergei Golubchik's avatar
Sergei Golubchik committed
1259
#ifdef WITH_ARIA_STORAGE_ENGINE
1260 1261 1262
    maria_delay_key_write= 1;
#endif
    ha_open_options&= ~HA_OPEN_DELAY_KEY_WRITE;
1263 1264 1265
    break;
  case DELAY_KEY_WRITE_ALL:
    myisam_delay_key_write=1;
Sergei Golubchik's avatar
Sergei Golubchik committed
1266
#ifdef WITH_ARIA_STORAGE_ENGINE
1267 1268
    maria_delay_key_write= 1;
#endif
1269 1270 1271 1272 1273
    ha_open_options|= HA_OPEN_DELAY_KEY_WRITE;
    break;
  }
}

1274 1275 1276 1277
bool sys_var_set::update(THD *thd, set_var *var)
{
  *value= var->save_result.ulong_value;
  return 0;
1278
}
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305

uchar *sys_var_set::value_ptr(THD *thd, enum_var_type type,
                              LEX_STRING *base)
{
  char buff[256];
  String tmp(buff, sizeof(buff), &my_charset_latin1);
  ulong length;
  ulong val= *value;

  tmp.length(0);
  for (uint i= 0; val; val>>= 1, i++)
  {
    if (val & 1)
    {
      tmp.append(enum_names->type_names[i],
                 enum_names->type_lengths[i]);
      tmp.append(',');
    }
  }

  if ((length= tmp.length()))
    length--;
  return (uchar*) thd->strmake(tmp.ptr(), length);
}

void sys_var_set_slave_mode::set_default(THD *thd, enum_var_type type)
{
1306
  slave_exec_mode_options= SLAVE_EXEC_MODE_STRICT;
1307 1308 1309 1310 1311 1312
}

bool sys_var_set_slave_mode::check(THD *thd, set_var *var)
{
  bool rc=  sys_var_set::check(thd, var);
  if (!rc &&
1313
      test_all_bits(var->save_result.ulong_value,
Michael Widenius's avatar
Michael Widenius committed
1314
                    SLAVE_EXEC_MODE_STRICT | SLAVE_EXEC_MODE_IDEMPOTENT))
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
  {
    rc= true;
    my_error(ER_SLAVE_AMBIGOUS_EXEC_MODE, MYF(0), "");
  }
  return rc;
}

bool sys_var_set_slave_mode::update(THD *thd, set_var *var)
{
  bool rc;
  pthread_mutex_lock(&LOCK_global_system_variables);
  rc= sys_var_set::update(thd, var);
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return rc;
}

1331
void fix_slave_exec_mode(void)
1332 1333
{
  DBUG_ENTER("fix_slave_exec_mode");
1334

1335
  if (test_all_bits(slave_exec_mode_options,
Michael Widenius's avatar
Michael Widenius committed
1336
                    SLAVE_EXEC_MODE_STRICT | SLAVE_EXEC_MODE_IDEMPOTENT))
1337
  {
1338 1339
    sql_print_error("Ambiguous slave modes combination. STRICT will be used");
    slave_exec_mode_options&= ~SLAVE_EXEC_MODE_IDEMPOTENT;
1340
  }
1341 1342
  if (!(slave_exec_mode_options & SLAVE_EXEC_MODE_IDEMPOTENT))
    slave_exec_mode_options|= SLAVE_EXEC_MODE_STRICT;
unknown's avatar
unknown committed
1343
  DBUG_VOID_RETURN;
1344
}
1345

Mats Kindahl's avatar
Mats Kindahl committed
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360

bool sys_var_thd_binlog_format::check(THD *thd, set_var *var) {
  /*
    All variables that affect writing to binary log (either format or
    turning logging on and off) use the same checking. We call the
    superclass ::check function to assign the variable correctly, and
    then check the value.
   */
  bool result= sys_var_thd_enum::check(thd, var);
  if (!result)
    result= check_log_update(thd, var);
  return result;
}


1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
bool sys_var_thd_binlog_format::is_readonly() const
{
  /*
    Under certain circumstances, the variable is read-only (unchangeable):
  */
  THD *thd= current_thd;
  /*
    If RBR and open temporary tables, their CREATE TABLE may not be in the
    binlog, so we can't toggle to SBR in this connection.
    The test below will also prevent SET GLOBAL, well it was not easy to test
    if global or not here.
    And this test will also prevent switching from RBR to RBR (a no-op which
    should not happen too often).
1374 1375 1376

    If we don't have row-based replication compiled in, the variable
    is always read-only.
1377 1378 1379 1380 1381 1382 1383 1384
  */
  if ((thd->variables.binlog_format == BINLOG_FORMAT_ROW) &&
      thd->temporary_tables)
  {
    my_error(ER_TEMP_TABLE_PREVENTS_SWITCH_OUT_OF_RBR, MYF(0));
    return 1;
  }
  /*
unknown's avatar
unknown committed
1385
    if in a stored function/trigger, it's too late to change mode
1386
  */
unknown's avatar
unknown committed
1387
  if (thd->in_sub_stmt)
1388 1389
  {
    my_error(ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_FORMAT, MYF(0));
unknown's avatar
unknown committed
1390
    return 1;
1391 1392 1393 1394
  }
  return sys_var_thd_enum::is_readonly();
}

unknown's avatar
unknown committed
1395

1396 1397 1398 1399 1400 1401
void fix_binlog_format_after_update(THD *thd, enum_var_type type)
{
  thd->reset_current_stmt_binlog_row_based();
}


1402
static void fix_max_binlog_size(THD *thd, enum_var_type type)
1403 1404 1405 1406 1407
{
  DBUG_ENTER("fix_max_binlog_size");
  DBUG_PRINT("info",("max_binlog_size=%lu max_relay_log_size=%lu",
                     max_binlog_size, max_relay_log_size));
  mysql_bin_log.set_max_size(max_binlog_size);
1408
#ifdef HAVE_REPLICATION
1409 1410
  if (!max_relay_log_size)
    active_mi->rli.relay_log.set_max_size(max_binlog_size);
unknown's avatar
unknown committed
1411
#endif
1412 1413 1414
  DBUG_VOID_RETURN;
}

1415
static void fix_max_relay_log_size(THD *thd, enum_var_type type)
1416 1417 1418 1419
{
  DBUG_ENTER("fix_max_relay_log_size");
  DBUG_PRINT("info",("max_binlog_size=%lu max_relay_log_size=%lu",
                     max_binlog_size, max_relay_log_size));
1420
#ifdef HAVE_REPLICATION
1421 1422
  active_mi->rli.relay_log.set_max_size(max_relay_log_size ?
                                        max_relay_log_size: max_binlog_size);
1423
#endif
1424 1425
  DBUG_VOID_RETURN;
}
1426

1427

1428 1429
static int check_max_delayed_threads(THD *thd, set_var *var)
{
unknown's avatar
unknown committed
1430
  longlong val= var->value->val_int();
1431
  if (var->type != OPT_GLOBAL && val != 0 &&
unknown's avatar
unknown committed
1432
      val != (longlong) global_system_variables.max_insert_delayed_threads)
1433 1434
  {
    char buf[64];
1435
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->var->name, llstr(val, buf));
1436 1437 1438 1439 1440
    return 1;
  }
  return 0;
}

1441
static void fix_max_connections(THD *thd, enum_var_type type)
1442
{
1443
#ifndef EMBEDDED_LIBRARY
1444
  resize_thr_alarm(max_connections + extra_max_connections +
1445
		   global_system_variables.max_insert_delayed_threads + 10);
1446
#endif
1447 1448 1449 1450 1451 1452
}


static void fix_thd_mem_root(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
unknown's avatar
unknown committed
1453
    reset_root_defaults(thd->mem_root,
1454 1455 1456 1457 1458 1459 1460
                        thd->variables.query_alloc_block_size,
                        thd->variables.query_prealloc_size);
}


static void fix_trans_mem_root(THD *thd, enum_var_type type)
{
1461
#ifdef USING_TRANSACTIONS
1462 1463 1464 1465
  if (type != OPT_GLOBAL)
    reset_root_defaults(&thd->transaction.mem_root,
                        thd->variables.trans_alloc_block_size,
                        thd->variables.trans_prealloc_size);
1466
#endif
1467 1468
}

1469

1470 1471 1472
static void fix_server_id(THD *thd, enum_var_type type)
{
  server_id_supplied = 1;
1473
  thd->server_id= server_id;
1474
}
1475

1476

1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
/**
  Throw warning (error in STRICT mode) if value for variable needed bounding.
  Only call from check(), not update(), because an error in update() would be
  bad mojo. Plug-in interface also uses this.

  @param thd      thread handle
  @param fixed    did we have to correct the value? (throw warn/err if so)
  @param unsignd  is value's type unsigned?
  @param name     variable's name
  @param val      variable's value

  @retval         TRUE on error, FALSE otherwise (warning or OK)
 */
1490 1491
bool throw_bounds_warning(THD *thd, bool fixed, bool unsignd,
                          const char *name, longlong val)
1492
{
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
  if (fixed)
  {
    char buf[22];

    if (unsignd)
      ullstr((ulonglong) val, buf);
    else
      llstr(val, buf);

    if (thd->variables.sql_mode & MODE_STRICT_ALL_TABLES)
    {
      my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, buf);
      return TRUE;
    }

    push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                        ER_TRUNCATED_WRONG_VALUE,
                        ER(ER_TRUNCATED_WRONG_VALUE), name, buf);
  }
  return FALSE;
1513 1514
}

1515 1516 1517 1518

/**
  Get unsigned system-variable.
  Negative value does not wrap around, but becomes zero.
1519 1520 1521
  Check user-supplied value for a systemvariable against bounds.
  If we needed to adjust the value, throw a warning or error depending
  on SQL-mode.
1522

1523 1524 1525
  @param thd             thread handle
  @param var             the system-variable to get
  @param user_max        a limit given with --maximum-variable-name=... or 0
1526
  @param var_type        function will bound on systems where necessary.
1527

1528
  @retval                TRUE on error, FALSE otherwise (warning or OK)
1529
 */
1530
static bool get_unsigned(THD *thd, set_var *var, ulonglong user_max,
1531
                         ulong var_type)
1532
{
1533 1534 1535
  int                     warnings= 0;
  ulonglong               unadjusted;
  const struct my_option *limits= var->var->option_limits;
1536
  struct my_option        fallback;
1537 1538

  /* get_unsigned() */
1539 1540 1541 1542 1543 1544
  if (var->value->unsigned_flag)
    var->save_result.ulonglong_value= (ulonglong) var->value->val_int();
  else
  {
    longlong v= var->value->val_int();
    var->save_result.ulonglong_value= (ulonglong) ((v < 0) ? 0 : v);
1545
    if (v < 0)
1546 1547 1548 1549 1550
    {
      warnings++;
      if (throw_bounds_warning(thd, TRUE, FALSE, var->var->name, v))
        return TRUE;  /* warning was promoted to error, give up */
    }
1551 1552
  }

1553
  unadjusted= var->save_result.ulonglong_value;
1554

1555
  /* max, if any */
1556

1557 1558 1559
  if ((user_max > 0) && (unadjusted > user_max))
  {
    var->save_result.ulonglong_value= user_max;
1560

1561 1562 1563 1564
    if ((warnings == 0) && throw_bounds_warning(thd, TRUE, TRUE,
                                                var->var->name,
                                                (longlong) unadjusted))
      return TRUE;
1565

1566 1567 1568
    warnings++;
  }

1569 1570 1571 1572 1573 1574 1575
  /*
    if the sysvar doesn't have a proper bounds record but the check
    function would like bounding to ULONG where its size differs from
    that of ULONGLONG, we make up a bogus limits record here and let
    the usual suspects handle the actual limiting.
  */

1576
  if (!limits && var_type != GET_ULL)
1577 1578
  {
    bzero(&fallback, sizeof(fallback));
1579
    fallback.var_type= var_type;
1580 1581 1582
    limits= &fallback;
  }

1583 1584 1585 1586 1587
  /* fix_unsigned() */
  if (limits)
  {
    my_bool   fixed;

1588 1589
    var->save_result.ulonglong_value= getopt_ull_limit_value(var->save_result.
                                                             ulonglong_value,
1590
                                                             limits, &fixed);
1591

1592 1593
    if ((warnings == 0) && throw_bounds_warning(thd, fixed, TRUE,
                                                var->var->name,
1594 1595 1596 1597 1598
                                                (longlong) unadjusted))
      return TRUE;
  }

  return FALSE;
1599 1600
}

1601

1602
sys_var_long_ptr::
unknown's avatar
unknown committed
1603
sys_var_long_ptr(sys_var_chain *chain, const char *name_arg, ulong *value_ptr_arg,
1604
                 sys_after_update_func after_update_arg)
unknown's avatar
unknown committed
1605
  :sys_var_long_ptr_global(chain, name_arg, value_ptr_arg,
1606 1607 1608 1609 1610
                           &LOCK_global_system_variables, after_update_arg)
{}


bool sys_var_long_ptr_global::check(THD *thd, set_var *var)
unknown's avatar
unknown committed
1611
{
1612
  return get_unsigned(thd, var, 0, GET_ULONG);
unknown's avatar
unknown committed
1613
}
1614

1615
bool sys_var_long_ptr_global::update(THD *thd, set_var *var)
unknown's avatar
unknown committed
1616
{
1617
  pthread_mutex_lock(guard);
1618
  *value= (ulong) var->save_result.ulonglong_value;
1619
  pthread_mutex_unlock(guard);
unknown's avatar
unknown committed
1620 1621 1622 1623
  return 0;
}


1624
void sys_var_long_ptr_global::set_default(THD *thd, enum_var_type type)
unknown's avatar
unknown committed
1625
{
1626
  my_bool not_used;
1627
  pthread_mutex_lock(guard);
1628 1629
  *value= (ulong) getopt_ull_limit_value((ulong) option_limits->def_value,
                                         option_limits, &not_used);
1630
  pthread_mutex_unlock(guard);
unknown's avatar
unknown committed
1631 1632 1633
}


1634 1635 1636 1637 1638 1639
bool sys_var_ulonglong_ptr::check(THD *thd, set_var *var)
{
  return get_unsigned(thd, var, 0, GET_ULL);
}


1640 1641
bool sys_var_ulonglong_ptr::update(THD *thd, set_var *var)
{
1642
  ulonglong tmp= var->save_result.ulonglong_value;
1643
  pthread_mutex_lock(&LOCK_global_system_variables);
1644
  *value= (ulonglong) tmp;
1645
  pthread_mutex_unlock(&LOCK_global_system_variables);
1646 1647 1648 1649 1650 1651
  return 0;
}


void sys_var_ulonglong_ptr::set_default(THD *thd, enum_var_type type)
{
1652
  my_bool not_used;
1653
  pthread_mutex_lock(&LOCK_global_system_variables);
1654 1655
  *value= getopt_ull_limit_value((ulonglong) option_limits->def_value,
                                 option_limits, &not_used);
1656
  pthread_mutex_unlock(&LOCK_global_system_variables);
1657 1658 1659
}


unknown's avatar
unknown committed
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
bool sys_var_bool_ptr::update(THD *thd, set_var *var)
{
  *value= (my_bool) var->save_result.ulong_value;
  return 0;
}


void sys_var_bool_ptr::set_default(THD *thd, enum_var_type type)
{
  *value= (my_bool) option_limits->def_value;
}


1673 1674 1675 1676 1677 1678 1679
bool sys_var_enum::update(THD *thd, set_var *var)
{
  *value= (uint) var->save_result.ulong_value;
  return 0;
}


1680
uchar *sys_var_enum::value_ptr(THD *thd, enum_var_type type, LEX_STRING *base)
1681
{
1682
  return (uchar*) enum_names->type_names[*value];
1683 1684
}

1685 1686
bool sys_var_thd_ulong::check(THD *thd, set_var *var)
{
1687
  if (get_unsigned(thd, var, max_system_variables.*offset, GET_ULONG))
1688
    return TRUE;
Tatiana A. Nurnberg's avatar
Tatiana A. Nurnberg committed
1689
  DBUG_ASSERT(var->save_result.ulonglong_value <= ULONG_MAX);
1690
  return ((check_func && (*check_func)(thd, var)));
1691
}
1692

unknown's avatar
unknown committed
1693 1694 1695
bool sys_var_thd_ulong::update(THD *thd, set_var *var)
{
  if (var->type == OPT_GLOBAL)
1696
    global_system_variables.*offset= (ulong) var->save_result.ulonglong_value;
unknown's avatar
unknown committed
1697
  else
1698
    thd->variables.*offset= (ulong) var->save_result.ulonglong_value;
1699

unknown's avatar
unknown committed
1700 1701 1702 1703 1704 1705 1706 1707
  return 0;
}


void sys_var_thd_ulong::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
  {
1708
    my_bool not_used;
unknown's avatar
unknown committed
1709
    /* We will not come here if option_limits is not set */
1710
    global_system_variables.*offset=
unknown's avatar
unknown committed
1711 1712
      (ulong) getopt_ull_limit_value((ulong) option_limits->def_value,
                                     option_limits, &not_used);
unknown's avatar
unknown committed
1713 1714 1715 1716 1717 1718
  }
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1719
uchar *sys_var_thd_ulong::value_ptr(THD *thd, enum_var_type type,
1720
				   LEX_STRING *base)
unknown's avatar
unknown committed
1721 1722
{
  if (type == OPT_GLOBAL)
1723 1724
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
unknown's avatar
unknown committed
1725 1726 1727
}


1728
bool sys_var_thd_ha_rows::check(THD *thd, set_var *var)
1729
{
1730 1731 1732 1733 1734 1735 1736 1737
  return get_unsigned(thd, var, max_system_variables.*offset,
#ifdef BIG_TABLES
                      GET_ULL
#else
                      GET_ULONG
#endif
                     );
}
1738

1739

1740 1741
bool sys_var_thd_ha_rows::update(THD *thd, set_var *var)
{
1742 1743 1744
  if (var->type == OPT_GLOBAL)
  {
    /* Lock is needed to make things safe on 32 bit systems */
unknown's avatar
unknown committed
1745
    pthread_mutex_lock(&LOCK_global_system_variables);
1746 1747
    global_system_variables.*offset= (ha_rows)
                                     var->save_result.ulonglong_value;
1748 1749 1750
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
1751
    thd->variables.*offset= (ha_rows) var->save_result.ulonglong_value;
1752 1753 1754 1755 1756 1757 1758 1759
  return 0;
}


void sys_var_thd_ha_rows::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
  {
1760
    my_bool not_used;
1761 1762
    /* We will not come here if option_limits is not set */
    pthread_mutex_lock(&LOCK_global_system_variables);
1763 1764 1765
    global_system_variables.*offset=
      (ha_rows) getopt_ull_limit_value((ha_rows) option_limits->def_value,
                                       option_limits, &not_used);
1766 1767 1768 1769 1770 1771 1772
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1773
uchar *sys_var_thd_ha_rows::value_ptr(THD *thd, enum_var_type type,
1774
				     LEX_STRING *base)
1775 1776
{
  if (type == OPT_GLOBAL)
1777 1778
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
1779 1780
}

1781 1782
bool sys_var_thd_ulonglong::check(THD *thd, set_var *var)
{
1783
  return get_unsigned(thd, var, max_system_variables.*offset, GET_ULL);
1784 1785
}

unknown's avatar
unknown committed
1786 1787 1788
bool sys_var_thd_ulonglong::update(THD *thd,  set_var *var)
{
  if (var->type == OPT_GLOBAL)
1789 1790 1791
  {
    /* Lock is needed to make things safe on 32 bit systems */
    pthread_mutex_lock(&LOCK_global_system_variables);
1792 1793
    global_system_variables.*offset= (ulonglong)
                                     var->save_result.ulonglong_value;
1794 1795
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
unknown's avatar
unknown committed
1796
  else
1797
    thd->variables.*offset= (ulonglong) var->save_result.ulonglong_value;
unknown's avatar
unknown committed
1798 1799 1800 1801 1802 1803 1804
  return 0;
}


void sys_var_thd_ulonglong::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
1805
  {
1806
    my_bool not_used;
1807
    pthread_mutex_lock(&LOCK_global_system_variables);
1808 1809 1810
    global_system_variables.*offset=
      getopt_ull_limit_value((ulonglong) option_limits->def_value,
                             option_limits, &not_used);
1811 1812
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
unknown's avatar
unknown committed
1813 1814 1815 1816 1817
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1818
uchar *sys_var_thd_ulonglong::value_ptr(THD *thd, enum_var_type type,
1819
				       LEX_STRING *base)
unknown's avatar
unknown committed
1820 1821
{
  if (type == OPT_GLOBAL)
1822 1823
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
unknown's avatar
unknown committed
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845
}


bool sys_var_thd_bool::update(THD *thd,  set_var *var)
{
  if (var->type == OPT_GLOBAL)
    global_system_variables.*offset= (my_bool) var->save_result.ulong_value;
  else
    thd->variables.*offset= (my_bool) var->save_result.ulong_value;
  return 0;
}


void sys_var_thd_bool::set_default(THD *thd,  enum_var_type type)
{
  if (type == OPT_GLOBAL)
    global_system_variables.*offset= (my_bool) option_limits->def_value;
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1846
uchar *sys_var_thd_bool::value_ptr(THD *thd, enum_var_type type,
1847
				  LEX_STRING *base)
unknown's avatar
unknown committed
1848 1849
{
  if (type == OPT_GLOBAL)
1850 1851
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
unknown's avatar
unknown committed
1852 1853 1854
}


1855
bool sys_var::check_enum(THD *thd, set_var *var, const TYPELIB *enum_names)
unknown's avatar
unknown committed
1856
{
unknown's avatar
unknown committed
1857
  char buff[STRING_BUFFER_USUAL_SIZE];
1858
  const char *value;
1859
  String str(buff, sizeof(buff) - 1, system_charset_info), *res;
unknown's avatar
unknown committed
1860 1861 1862 1863 1864

  if (var->value->result_type() == STRING_RESULT)
  {
    if (!(res=var->value->val_str(&str)) ||
	((long) (var->save_result.ulong_value=
1865 1866
		 (ulong) find_type(enum_names, res->ptr(),
				   res->length(),1)-1)) < 0)
unknown's avatar
unknown committed
1867
    {
1868
      value= res ? res->c_ptr() : "NULL";
unknown's avatar
unknown committed
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
      goto err;
    }
  }
  else
  {
    ulonglong tmp=var->value->val_int();
    if (tmp >= enum_names->count)
    {
      llstr(tmp,buff);
      value=buff;				// Wrong value is here
      goto err;
    }
    var->save_result.ulong_value= (ulong) tmp;	// Save for update
  }
  return 0;

err:
1886
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, value);
unknown's avatar
unknown committed
1887 1888 1889
  return 1;
}

1890 1891 1892 1893 1894 1895
/**
   Check vality of set

   Note that this sets 'save_result.ulong_value' for the update function,
   which means that we don't need a separate sys_var::update() function
*/
1896 1897 1898

bool sys_var::check_set(THD *thd, set_var *var, TYPELIB *enum_names)
{
1899
  bool not_used;
1900
  char buff[256], *error= 0;
1901
  uint error_len= 0;
1902
  String str(buff, sizeof(buff) - 1, system_charset_info), *res;
1903 1904 1905 1906

  if (var->value->result_type() == STRING_RESULT)
  {
    if (!(res= var->value->val_str(&str)))
1907
    {
unknown's avatar
unknown committed
1908
      strmov(buff, "NULL");
1909
      goto err;
1910
    }
1911

1912
    if (!m_allow_empty_value && res->length() == 0)
1913 1914 1915 1916 1917
    {
      buff[0]= 0;
      goto err;
    }

unknown's avatar
unknown committed
1918
    var->save_result.ulong_value= ((ulong)
1919
				   find_set(enum_names, res->ptr(),
1920 1921 1922
					    res->length(),
                                            NULL,
                                            &error, &error_len,
unknown's avatar
unknown committed
1923
					    &not_used));
1924 1925
    if (error_len)
    {
1926
      strmake(buff, error, min(sizeof(buff) - 1, error_len));
1927 1928 1929 1930 1931 1932
      goto err;
    }
  }
  else
  {
    ulonglong tmp= var->value->val_int();
1933

1934
    if (!m_allow_empty_value && tmp == 0)
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
    {
      buff[0]= '0';
      buff[1]= 0;
      goto err;
    }

    /*
      For when the enum is made to contain 64 elements, as 1ULL<<64 is
      undefined, we guard with a "count<64" test.
    */
1945 1946
    if (unlikely((tmp >= ((ULL(1)) << enum_names->count)) &&
                 (enum_names->count < 64)))
1947 1948 1949 1950 1951 1952 1953 1954 1955
    {
      llstr(tmp, buff);
      goto err;
    }
    var->save_result.ulong_value= (ulong) tmp;  // Save for update
  }
  return 0;

err:
1956
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, buff);
1957 1958 1959 1960
  return 1;
}


1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
/**
  Make string representation of set

  @param[in]  thd    thread handler
  @param[in]  val    sql_mode value
  @param[in]  names  names for the different bits
  @param[out] rep    Result string

  @return
    0 ok
    1 end of memory
*/

bool sys_var::make_set(THD *thd, ulonglong val, TYPELIB *names,
                       LEX_STRING *rep)
{
  /* Strings for typelib may be big; This is reallocated on demand */
  char buff[256];
  String tmp(buff, sizeof(buff) - 1, &my_charset_latin1);
  bool error= 0;

  tmp.length(0);
  for (uint i= 0; val; val>>= 1, i++)
  {
    if (val & 1)
    {
      error|= tmp.append(names->type_names[i],
                 names->type_lengths[i]);
      error|= tmp.append(',');
    }
  }

  if (tmp.length())
    tmp.length(tmp.length() - 1); /* trim the trailing comma */

  /* Allocate temporary copy of string */
  if (!(rep->str= thd->strmake(tmp.ptr(), tmp.length())))
    error= 1;
  rep->length= tmp.length();
  return error;                   /* Error in case of out of memory */
}


2004 2005 2006 2007 2008
CHARSET_INFO *sys_var::charset(THD *thd)
{
  return is_os_charset ? thd->variables.character_set_filesystem : 
    system_charset_info;
}
unknown's avatar
unknown committed
2009

2010

unknown's avatar
unknown committed
2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
bool sys_var_thd_enum::update(THD *thd, set_var *var)
{
  if (var->type == OPT_GLOBAL)
    global_system_variables.*offset= var->save_result.ulong_value;
  else
    thd->variables.*offset= var->save_result.ulong_value;
  return 0;
}


void sys_var_thd_enum::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
    global_system_variables.*offset= (ulong) option_limits->def_value;
  else
    thd->variables.*offset= global_system_variables.*offset;
}


2030
uchar *sys_var_thd_enum::value_ptr(THD *thd, enum_var_type type,
2031
				  LEX_STRING *base)
unknown's avatar
unknown committed
2032 2033 2034 2035
{
  ulong tmp= ((type == OPT_GLOBAL) ?
	      global_system_variables.*offset :
	      thd->variables.*offset);
2036
  return (uchar*) enum_names->type_names[tmp];
unknown's avatar
unknown committed
2037 2038
}

2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
uchar *sys_var_thd_set::value_ptr(THD *thd, enum_var_type type,
                                  LEX_STRING *base)
{
  LEX_STRING sql_mode;
  ulong val= ((type == OPT_GLOBAL) ? global_system_variables.*offset :
              thd->variables.*offset);
  (void) make_set(thd, val & visible_value_mask, enum_names, &sql_mode);
  return (uchar *) sql_mode.str;
}

2049 2050 2051 2052 2053
bool sys_var_thd_bit::check(THD *thd, set_var *var)
{
  return (check_enum(thd, var, &bool_typelib) ||
          (check_func && (*check_func)(thd, var)));
}
unknown's avatar
unknown committed
2054 2055 2056

bool sys_var_thd_bit::update(THD *thd, set_var *var)
{
2057
  int res= (*update_func)(thd, var);
unknown's avatar
unknown committed
2058 2059 2060 2061
  return res;
}


2062
uchar *sys_var_thd_bit::value_ptr(THD *thd, enum_var_type type,
2063
				 LEX_STRING *base)
unknown's avatar
unknown committed
2064 2065 2066 2067 2068 2069 2070
{
  /*
    If reverse is 0 (default) return 1 if bit is set.
    If reverse is 1, return 0 if bit is set
  */
  thd->sys_var_tmp.my_bool_value= ((thd->options & bit_flag) ?
				   !reverse : reverse);
2071
  return (uchar*) &thd->sys_var_tmp.my_bool_value;
unknown's avatar
unknown committed
2072 2073 2074
}


unknown's avatar
unknown committed
2075
/** Update a date_time format variable based on given value. */
2076 2077 2078 2079 2080 2081

void sys_var_thd_date_time_format::update2(THD *thd, enum_var_type type,
					   DATE_TIME_FORMAT *new_value)
{
  DATE_TIME_FORMAT *old;
  DBUG_ENTER("sys_var_date_time_format::update2");
2082
  DBUG_DUMP("positions", (uchar*) new_value->positions,
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
	    sizeof(new_value->positions));

  if (type == OPT_GLOBAL)
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
    old= (global_system_variables.*offset);
    (global_system_variables.*offset)= new_value;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
  {
    old= (thd->variables.*offset);
    (thd->variables.*offset)= new_value;
  }
  my_free((char*) old, MYF(MY_ALLOW_ZERO_PTR));
  DBUG_VOID_RETURN;
}


bool sys_var_thd_date_time_format::update(THD *thd, set_var *var)
{
  DATE_TIME_FORMAT *new_value;
  /* We must make a copy of the last value to get it into normal memory */
  new_value= date_time_format_copy((THD*) 0,
				   var->save_result.date_time_format);
  if (!new_value)
    return 1;					// Out of memory
  update2(thd, var->type, new_value);		// Can't fail
  return 0;
}


bool sys_var_thd_date_time_format::check(THD *thd, set_var *var)
{
unknown's avatar
unknown committed
2117
  char buff[STRING_BUFFER_USUAL_SIZE];
2118
  String str(buff,sizeof(buff) - 1, system_charset_info), *res;
2119 2120 2121 2122 2123 2124 2125 2126
  DATE_TIME_FORMAT *format;

  if (!(res=var->value->val_str(&str)))
    res= &my_empty_string;

  if (!(format= date_time_format_make(date_time_type,
				      res->ptr(), res->length())))
  {
2127
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, res->c_ptr());
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161
    return 1;
  }
  
  /*
    We must copy result to thread space to not get a memory leak if
    update is aborted
  */
  var->save_result.date_time_format= date_time_format_copy(thd, format);
  my_free((char*) format, MYF(0));
  return var->save_result.date_time_format == 0;
}


void sys_var_thd_date_time_format::set_default(THD *thd, enum_var_type type)
{
  DATE_TIME_FORMAT *res= 0;

  if (type == OPT_GLOBAL)
  {
    const char *format;
    if ((format= opt_date_time_formats[date_time_type]))
      res= date_time_format_make(date_time_type, format, strlen(format));
  }
  else
  {
    /* Make copy with malloc */
    res= date_time_format_copy((THD *) 0, global_system_variables.*offset);
  }

  if (res)					// Should always be true
    update2(thd, type, res);
}


2162
uchar *sys_var_thd_date_time_format::value_ptr(THD *thd, enum_var_type type,
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174
					      LEX_STRING *base)
{
  if (type == OPT_GLOBAL)
  {
    char *res;
    /*
      We do a copy here just to be sure things will work even if someone
      is modifying the original string while the copy is accessed
      (Can't happen now in SQL SHOW, but this is a good safety for the future)
    */
    res= thd->strmake((global_system_variables.*offset)->format.str,
		      (global_system_variables.*offset)->format.length);
2175
    return (uchar*) res;
2176
  }
2177
  return (uchar*) (thd->variables.*offset)->format.str;
2178 2179 2180
}


2181 2182
typedef struct old_names_map_st
{
unknown's avatar
unknown committed
2183 2184 2185
  const char *old_name;
  const char *new_name;
} my_old_conv;
unknown's avatar
unknown committed
2186

unknown's avatar
unknown committed
2187 2188
static my_old_conv old_conv[]= 
{
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
  {	"cp1251_koi8"		,	"cp1251"	},
  {	"cp1250_latin2"		,	"cp1250"	},
  {	"kam_latin2"		,	"keybcs2"	},
  {	"mac_latin2"		,	"MacRoman"	},
  {	"macce_latin2"		,	"MacCE"		},
  {	"pc2_latin2"		,	"pclatin2"	},
  {	"vga_latin2"		,	"pclatin1"	},
  {	"koi8_cp1251"		,	"koi8r"		},
  {	"win1251ukr_koi8_ukr"	,	"win1251ukr"	},
  {	"koi8_ukr_win1251ukr"	,	"koi8u"		},
  {	NULL			,	NULL		}
unknown's avatar
unknown committed
2200
};
unknown's avatar
unknown committed
2201

unknown's avatar
unknown committed
2202
CHARSET_INFO *get_old_charset_by_name(const char *name)
unknown's avatar
unknown committed
2203
{
unknown's avatar
unknown committed
2204
  my_old_conv *conv;
unknown's avatar
unknown committed
2205
 
unknown's avatar
unknown committed
2206
  for (conv= old_conv; conv->old_name; conv++)
2207
  {
unknown's avatar
unknown committed
2208 2209
    if (!my_strcasecmp(&my_charset_latin1, name, conv->old_name))
      return get_charset_by_csname(conv->new_name, MY_CS_PRIMARY, MYF(0));
2210
  }
unknown's avatar
unknown committed
2211
  return NULL;
unknown's avatar
unknown committed
2212 2213
}

unknown's avatar
unknown committed
2214

2215
bool sys_var_collation::check(THD *thd, set_var *var)
unknown's avatar
unknown committed
2216 2217
{
  CHARSET_INFO *tmp;
2218
  LINT_INIT(tmp);
unknown's avatar
unknown committed
2219

2220
  if (var->value->result_type() == STRING_RESULT)
2221
  {
unknown's avatar
unknown committed
2222
    char buff[STRING_BUFFER_USUAL_SIZE];
2223
    String str(buff,sizeof(buff) - 1, system_charset_info), *res;
2224 2225
    if (!(res=var->value->val_str(&str)))
    {
2226
      my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
2227 2228 2229 2230
      return 1;
    }
    if (!(tmp=get_charset_by_name(res->c_ptr(),MYF(0))))
    {
2231
      my_error(ER_UNKNOWN_COLLATION, MYF(0), res->c_ptr());
2232 2233
      return 1;
    }
2234
  }
2235
  else // INT_RESULT
2236
  {
2237
    if (!(tmp=get_charset((int) var->value->val_int(),MYF(0))))
2238 2239
    {
      char buf[20];
2240
      int10_to_str((int) var->value->val_int(), buf, -10);
2241
      my_error(ER_UNKNOWN_COLLATION, MYF(0), buf);
2242 2243
      return 1;
    }
2244 2245 2246 2247 2248
  }
  var->save_result.charset= tmp;	// Save for update
  return 0;
}

2249

2250 2251 2252
bool sys_var_character_set::check(THD *thd, set_var *var)
{
  CHARSET_INFO *tmp;
2253
  LINT_INIT(tmp);
2254

2255 2256
  if (var->value->result_type() == STRING_RESULT)
  {
unknown's avatar
unknown committed
2257
    char buff[STRING_BUFFER_USUAL_SIZE];
2258
    String str(buff,sizeof(buff) - 1, system_charset_info), *res;
2259
    if (!(res=var->value->val_str(&str)))
2260
    {
2261 2262
      if (!nullable)
      {
2263
        my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
2264 2265 2266 2267
        return 1;
      }
      tmp= NULL;
    }
2268
    else
2269
    {
2270 2271 2272 2273
      const char *name= res->c_ptr_safe();
      if (!(tmp=get_charset_by_csname(name,MY_CS_PRIMARY,MYF(0))) &&
             !(tmp=get_old_charset_by_name(name)))
      {
2274
        my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), name);
2275 2276
        return 1;
      }
2277 2278
    }
  }
2279
  else // INT_RESULT
unknown's avatar
unknown committed
2280
  {
2281
    if (!(tmp=get_charset((int) var->value->val_int(),MYF(0))))
2282 2283
    {
      char buf[20];
2284
      int10_to_str((int) var->value->val_int(), buf, -10);
2285 2286 2287
      my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), buf);
      return 1;
    }
unknown's avatar
unknown committed
2288 2289 2290 2291 2292
  }
  var->save_result.charset= tmp;	// Save for update
  return 0;
}

2293

2294
bool sys_var_character_set::update(THD *thd, set_var *var)
unknown's avatar
unknown committed
2295
{
2296
  ci_ptr(thd,var->type)[0]= var->save_result.charset;
unknown's avatar
unknown committed
2297
  thd->update_charset();
unknown's avatar
unknown committed
2298 2299 2300
  return 0;
}

2301

2302
uchar *sys_var_character_set::value_ptr(THD *thd, enum_var_type type,
2303
				       LEX_STRING *base)
unknown's avatar
unknown committed
2304
{
2305
  CHARSET_INFO *cs= ci_ptr(thd,type)[0];
2306
  return cs ? (uchar*) cs->csname : (uchar*) NULL;
unknown's avatar
unknown committed
2307 2308 2309
}


unknown's avatar
unknown committed
2310
void sys_var_character_set_sv::set_default(THD *thd, enum_var_type type)
2311
{
2312
  if (type == OPT_GLOBAL)
unknown's avatar
unknown committed
2313
    global_system_variables.*offset= *global_default;
2314
  else
unknown's avatar
unknown committed
2315 2316 2317 2318
  {
    thd->variables.*offset= global_system_variables.*offset;
    thd->update_charset();
  }
2319
}
unknown's avatar
unknown committed
2320
CHARSET_INFO **sys_var_character_set_sv::ci_ptr(THD *thd, enum_var_type type)
2321 2322
{
  if (type == OPT_GLOBAL)
unknown's avatar
unknown committed
2323
    return &(global_system_variables.*offset);
2324
  else
unknown's avatar
unknown committed
2325
    return &(thd->variables.*offset);
2326 2327
}

2328

2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
bool sys_var_character_set_client::check(THD *thd, set_var *var)
{
  if (sys_var_character_set_sv::check(thd, var))
    return 1;
  /* Currently, UCS-2 cannot be used as a client character set */
  if (var->save_result.charset->mbminlen > 1)
  {
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, 
             var->save_result.charset->csname);
    return 1;
  }
  return 0;
}


2344 2345
CHARSET_INFO ** sys_var_character_set_database::ci_ptr(THD *thd,
						       enum_var_type type)
2346 2347
{
  if (type == OPT_GLOBAL)
2348
    return &global_system_variables.collation_database;
2349
  else
2350
    return &thd->variables.collation_database;
2351 2352
}

2353

2354 2355 2356
void sys_var_character_set_database::set_default(THD *thd, enum_var_type type)
{
 if (type == OPT_GLOBAL)
2357
    global_system_variables.collation_database= default_charset_info;
2358
  else
unknown's avatar
unknown committed
2359
  {
2360
    thd->variables.collation_database= thd->db_charset;
unknown's avatar
unknown committed
2361 2362
    thd->update_charset();
  }
2363 2364
}

2365

unknown's avatar
unknown committed
2366
bool sys_var_collation_sv::update(THD *thd, set_var *var)
2367 2368
{
  if (var->type == OPT_GLOBAL)
unknown's avatar
unknown committed
2369
    global_system_variables.*offset= var->save_result.charset;
2370
  else
unknown's avatar
unknown committed
2371
  {
unknown's avatar
unknown committed
2372
    thd->variables.*offset= var->save_result.charset;
unknown's avatar
unknown committed
2373 2374
    thd->update_charset();
  }
2375 2376 2377
  return 0;
}

2378

unknown's avatar
unknown committed
2379
void sys_var_collation_sv::set_default(THD *thd, enum_var_type type)
2380
{
unknown's avatar
unknown committed
2381 2382
  if (type == OPT_GLOBAL)
    global_system_variables.*offset= *global_default;
2383 2384
  else
  {
unknown's avatar
unknown committed
2385
    thd->variables.*offset= global_system_variables.*offset;
2386 2387 2388 2389 2390
    thd->update_charset();
  }
}


2391
uchar *sys_var_collation_sv::value_ptr(THD *thd, enum_var_type type,
2392
                                       LEX_STRING *base)
2393 2394
{
  CHARSET_INFO *cs= ((type == OPT_GLOBAL) ?
2395
		     global_system_variables.*offset : thd->variables.*offset);
2396
  return cs ? (uchar*) cs->name : (uchar*) "NULL";
2397 2398
}

2399

2400
LEX_STRING default_key_cache_base= {(char *) "default", 7 };
unknown's avatar
unknown committed
2401

unknown's avatar
unknown committed
2402
static KEY_CACHE zero_key_cache;
unknown's avatar
unknown committed
2403

unknown's avatar
unknown committed
2404
KEY_CACHE *get_key_cache(LEX_STRING *cache_name)
unknown's avatar
unknown committed
2405
{
2406 2407
  safe_mutex_assert_owner(&LOCK_global_system_variables);
  if (!cache_name || ! cache_name->length)
unknown's avatar
unknown committed
2408
    cache_name= &default_key_cache_base;
unknown's avatar
unknown committed
2409
  return ((KEY_CACHE*) find_named(&key_caches,
2410
                                  cache_name->str, cache_name->length, 0));
unknown's avatar
unknown committed
2411 2412
}

2413
uchar *sys_var_key_cache_param::value_ptr(THD *thd, enum_var_type type,
unknown's avatar
unknown committed
2414
					 LEX_STRING *base)
2415
{
unknown's avatar
unknown committed
2416
  KEY_CACHE *key_cache= get_key_cache(base);
unknown's avatar
unknown committed
2417 2418
  if (!key_cache)
    key_cache= &zero_key_cache;
2419
  return (uchar*) key_cache + offset ;
unknown's avatar
unknown committed
2420
}
2421

2422

2423 2424 2425 2426 2427 2428
bool sys_var_key_buffer_size::check(THD *thd, set_var *var)
{
  return get_unsigned(thd, var, 0, GET_ULL);
}


2429 2430
bool sys_var_key_buffer_size::update(THD *thd, set_var *var)
{
2431
  ulonglong tmp= var->save_result.ulonglong_value;
unknown's avatar
unknown committed
2432
  LEX_STRING *base_name= &var->base;
unknown's avatar
unknown committed
2433
  KEY_CACHE *key_cache;
2434 2435 2436
  bool error= 0;

  /* If no basename, assume it's for the key cache named 'default' */
unknown's avatar
unknown committed
2437
  if (!base_name->length)
2438
    base_name= &default_key_cache_base;
2439 2440 2441

  pthread_mutex_lock(&LOCK_global_system_variables);
  key_cache= get_key_cache(base_name);
2442

2443 2444
  if (!key_cache)
  {
2445
    /* Key cache didn't exist */
2446
    if (!tmp)					// Tried to delete cache
2447 2448 2449 2450 2451 2452
      goto end;					// Ok, nothing to do
    if (!(key_cache= create_key_cache(base_name->str, base_name->length)))
    {
      error= 1;
      goto end;
    }
2453
  }
2454 2455 2456 2457 2458 2459 2460 2461 2462

  /*
    Abort if some other thread is changing the key cache
    TODO: This should be changed so that we wait until the previous
    assignment is done and then do the new assign
  */
  if (key_cache->in_init)
    goto end;

unknown's avatar
unknown committed
2463
  if (!tmp)					// Zero size means delete
2464
  {
unknown's avatar
unknown committed
2465
    if (key_cache == dflt_key_cache)
2466
    {
2467 2468
      error= 1;
      my_error(ER_WARN_CANT_DROP_DEFAULT_KEYCACHE, MYF(0));
2469
      goto end;					// Ignore default key cache
2470
    }
2471

unknown's avatar
unknown committed
2472
    if (key_cache->key_cache_inited)		// If initied
2473 2474
    {
      /*
2475 2476
	Move tables using this key cache to the default key cache
	and clear the old key cache.
2477
      */
2478
      NAMED_LIST *list; 
unknown's avatar
unknown committed
2479
      key_cache= (KEY_CACHE *) find_named(&key_caches, base_name->str,
2480
					      base_name->length, &list);
2481 2482
      key_cache->in_init= 1;
      pthread_mutex_unlock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
2483
      error= reassign_keycache_tables(thd, key_cache, dflt_key_cache);
2484 2485
      pthread_mutex_lock(&LOCK_global_system_variables);
      key_cache->in_init= 0;
2486
    }
2487 2488 2489 2490 2491
    /*
      We don't delete the key cache as some running threads my still be
      in the key cache code with a pointer to the deleted (empty) key cache
    */
    goto end;
2492 2493
  }

2494
  key_cache->param_buff_size= (ulonglong) tmp;
2495

2496
  /* If key cache didn't exist initialize it, else resize it */
2497 2498 2499
  key_cache->in_init= 1;
  pthread_mutex_unlock(&LOCK_global_system_variables);

unknown's avatar
unknown committed
2500
  if (!key_cache->key_cache_inited)
2501
    error= (bool) (ha_init_key_cache("", key_cache));
unknown's avatar
unknown committed
2502
  else
2503
    error= (bool)(ha_resize_key_cache(key_cache));
2504

2505 2506
  pthread_mutex_lock(&LOCK_global_system_variables);
  key_cache->in_init= 0;  
2507

2508 2509
end:
  pthread_mutex_unlock(&LOCK_global_system_variables);
2510 2511 2512

 var->save_result.ulonglong_value = SIZE_T_MAX;

2513
  return error;
2514 2515
}

2516

2517 2518 2519 2520 2521 2522
bool sys_var_key_cache_long::check(THD *thd, set_var *var)
{
  return get_unsigned(thd, var, 0, GET_ULONG);
}


unknown's avatar
unknown committed
2523 2524 2525 2526 2527 2528
/**
  @todo
  Abort if some other thread is changing the key cache.
  This should be changed so that we wait until the previous
  assignment is done and then do the new assign
*/
2529
bool sys_var_key_cache_long::update(THD *thd, set_var *var)
2530
{
2531
  LEX_STRING *base_name= &var->base;
2532 2533
  bool error= 0;

2534
  if (!base_name->length)
unknown's avatar
unknown committed
2535
    base_name= &default_key_cache_base;
2536 2537

  pthread_mutex_lock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
2538
  KEY_CACHE *key_cache= get_key_cache(base_name);
2539

unknown's avatar
unknown committed
2540 2541
  if (!key_cache && !(key_cache= create_key_cache(base_name->str,
				                  base_name->length)))
2542 2543 2544 2545
  {
    error= 1;
    goto end;
  }
2546

2547 2548 2549 2550 2551 2552 2553 2554
  /*
    Abort if some other thread is changing the key cache
    TODO: This should be changed so that we wait until the previous
    assignment is done and then do the new assign
  */
  if (key_cache->in_init)
    goto end;

2555 2556
  *((ulong*) (((char*) key_cache) + offset))= (ulong)
                                              var->save_result.ulonglong_value;
unknown's avatar
unknown committed
2557

2558 2559 2560 2561 2562
  /*
    Don't create a new key cache if it didn't exist
    (key_caches are created only when the user sets block_size)
  */
  key_cache->in_init= 1;
2563

2564
  pthread_mutex_unlock(&LOCK_global_system_variables);
2565

2566
  if (offset == offsetof(KEY_CACHE, param_block_size))
2567
    error= (bool) (ha_resize_key_cache(key_cache));
2568 2569 2570
  else
  if (offset == offsetof(KEY_CACHE, param_division_limit) ||
      offset == offsetof(KEY_CACHE, param_age_threshold))
2571
    error= (bool) (ha_change_key_cache_param(key_cache));
2572 2573
  else
  if (offset == offsetof(KEY_CACHE, param_partitions))
2574
    error= (bool) (ha_repartition_key_cache(key_cache));
2575 2576 2577 2578 2579 2580 2581

  pthread_mutex_lock(&LOCK_global_system_variables);
  key_cache->in_init= 0;  

end:
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return error;
2582 2583
}

2584

2585 2586
bool sys_var_log_state::update(THD *thd, set_var *var)
{
2587
  bool res;
2588 2589 2590 2591 2592
  if (this == &sys_var_log)
    WARN_DEPRECATED(thd, "7.0", "@@log", "'@@general_log'");
  else if (this == &sys_var_log_slow)
    WARN_DEPRECATED(thd, "7.0", "@@log_slow_queries", "'@@slow_query_log'");

2593 2594
  if (!var->save_result.ulong_value)
  {
2595 2596
    logger.deactivate_log_handler(thd, log_type);
    res= false;
2597
  }
2598 2599
  else
    res= logger.activate_log_handler(thd, log_type);
2600 2601 2602 2603 2604
  return res;
}

void sys_var_log_state::set_default(THD *thd, enum_var_type type)
{
2605 2606 2607 2608 2609
  if (this == &sys_var_log)
    WARN_DEPRECATED(thd, "7.0", "@@log", "'@@general_log'");
  else if (this == &sys_var_log_slow)
    WARN_DEPRECATED(thd, "7.0", "@@log_slow_queries", "'@@slow_query_log'");

2610 2611 2612 2613 2614 2615
  logger.deactivate_log_handler(thd, log_type);
}


static int  sys_check_log_path(THD *thd,  set_var *var)
{
2616
  char path[FN_REFLEN], buff[FN_REFLEN];
2617
  MY_STAT f_stat;
2618
  String str(buff, sizeof(buff), system_charset_info), *res;
Michael Widenius's avatar
Michael Widenius committed
2619
  const char *log_file_str= 0;
2620 2621
  size_t path_length;

2622 2623 2624
  if (!(res= var->value->val_str(&str)))
    goto err;

2625
  log_file_str= res->c_ptr_safe();
2626 2627
  bzero(&f_stat, sizeof(MY_STAT));

2628 2629 2630
  path_length= unpack_filename(path, log_file_str);

  if (!path_length)
2631
  {
2632 2633 2634
    /* File name is empty. */

    goto err;
2635
  }
2636 2637

  if (my_stat(path, &f_stat, MYF(0)))
2638 2639
  {
    /*
2640 2641
      A file system object exists. Check if argument is a file and we have
      'write' permission.
2642
    */
2643 2644 2645

    if (!MY_S_ISREG(f_stat.st_mode) ||
        !(f_stat.st_mode & MY_S_IWRITE))
2646
      goto err;
2647 2648

    return 0;
2649
  }
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664

  /* Get dirname of the file path. */
  (void) dirname_part(path, log_file_str, &path_length);

  /* Dirname is empty if file path is relative. */
  if (!path_length)
    return 0;

  /*
    Check if directory exists and we have permission to create file and
    write to file.
  */
  if (my_access(path, (F_OK|W_OK)))
    goto err;

2665
  return 0;
2666 2667 2668

err:
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->var->name, 
Michael Widenius's avatar
Michael Widenius committed
2669
           log_file_str ? log_file_str : "NULL");
2670
  return 1;
2671 2672 2673 2674 2675 2676 2677
}


bool update_sys_var_str_path(THD *thd, sys_var_str *var_str,
			     set_var *var, const char *log_ext,
			     bool log_state, uint log_type)
{
Michael Widenius's avatar
Michael Widenius committed
2678
  MYSQL_QUERY_LOG *file_log= 0;
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
  char buff[FN_REFLEN];
  char *res= 0, *old_value=(char *)(var ? var->value->str_value.ptr() : 0);
  bool result= 0;
  uint str_length= (var ? var->value->str_value.length() : 0);

  switch (log_type) {
  case QUERY_LOG_SLOW:
    file_log= logger.get_slow_log_file_handler();
    break;
  case QUERY_LOG_GENERAL:
    file_log= logger.get_log_file_handler();
    break;
  default:
2692
    MY_ASSERT_UNREACHABLE();
2693 2694 2695 2696 2697 2698 2699
  }

  if (!old_value)
  {
    old_value= make_default_log_name(buff, log_ext);
    str_length= strlen(old_value);
  }
2700
  if (!(res= my_strndup(old_value, str_length, MYF(MY_FAE+MY_WME))))
2701 2702 2703 2704 2705
  {
    result= 1;
    goto err;
  }

2706
  logger.lock_exclusive();
2707 2708 2709 2710

  if (file_log && log_state)
    file_log->close(0);
  if (file_log && log_state)
unknown's avatar
unknown committed
2711 2712 2713
  {
    switch (log_type) {
    case QUERY_LOG_SLOW:
2714
      file_log->open_slow_log(res);
unknown's avatar
unknown committed
2715 2716
      break;
    case QUERY_LOG_GENERAL:
2717
      file_log->open_query_log(res);
unknown's avatar
unknown committed
2718 2719 2720 2721 2722
      break;
    default:
      DBUG_ASSERT(0);
    }
  }
2723 2724

  logger.unlock();
2725 2726 2727 2728 2729 2730 2731

  /* update global variable */
  pthread_mutex_lock(&LOCK_global_system_variables);
  old_value= var_str->value;
  var_str->value= res;
  var_str->value_length= str_length;
  my_free(old_value, MYF(MY_ALLOW_ZERO_PTR));
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
  pthread_mutex_unlock(&LOCK_global_system_variables);

err:
  return result;
}


static bool sys_update_general_log_path(THD *thd, set_var * var)
{
  return update_sys_var_str_path(thd, &sys_var_general_log_path, 
				 var, ".log", opt_log, QUERY_LOG_GENERAL);
}


static void sys_default_general_log_path(THD *thd, enum_var_type type)
{
  (void) update_sys_var_str_path(thd, &sys_var_general_log_path,
				 0, ".log", opt_log, QUERY_LOG_GENERAL);
}


static bool sys_update_slow_log_path(THD *thd, set_var * var)
{
  return update_sys_var_str_path(thd, &sys_var_slow_log_path,
				 var, "-slow.log", opt_slow_log,
                                 QUERY_LOG_SLOW);
}


static void sys_default_slow_log_path(THD *thd, enum_var_type type)
{
  (void) update_sys_var_str_path(thd, &sys_var_slow_log_path,
				 0, "-slow.log", opt_slow_log,
                                 QUERY_LOG_SLOW);
}


bool sys_var_log_output::update(THD *thd, set_var *var)
{
2771
  logger.lock_exclusive();
2772 2773 2774 2775 2776 2777 2778 2779 2780 2781
  logger.init_slow_log(var->save_result.ulong_value);
  logger.init_general_log(var->save_result.ulong_value);
  *value= var->save_result.ulong_value;
  logger.unlock();
  return 0;
}


void sys_var_log_output::set_default(THD *thd, enum_var_type type)
{
2782 2783 2784 2785
  logger.lock_exclusive();
  logger.init_slow_log(LOG_FILE);
  logger.init_general_log(LOG_FILE);
  *value= LOG_FILE;
2786 2787 2788 2789
  logger.unlock();
}


2790
uchar *sys_var_log_output::value_ptr(THD *thd, enum_var_type type,
unknown's avatar
unknown committed
2791
                                    LEX_STRING *base)
2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810
{
  char buff[256];
  String tmp(buff, sizeof(buff), &my_charset_latin1);
  ulong length;
  ulong val= *value;

  tmp.length(0);
  for (uint i= 0; val; val>>= 1, i++)
  {
    if (val & 1)
    {
      tmp.append(log_output_typelib.type_names[i],
                 log_output_typelib.type_lengths[i]);
      tmp.append(',');
    }
  }

  if ((length= tmp.length()))
    length--;
2811
  return (uchar*) thd->strmake(tmp.ptr(), length);
2812 2813 2814
}


2815 2816 2817 2818
/*****************************************************************************
  Functions to handle SET NAMES and SET CHARACTER SET
*****************************************************************************/

unknown's avatar
unknown committed
2819
int set_var_collation_client::check(THD *thd)
2820
{
2821 2822 2823 2824 2825 2826 2827
  /* Currently, UCS-2 cannot be used as a client character set */
  if (character_set_client->mbminlen > 1)
  {
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), "character_set_client",
             character_set_client->csname);
    return 1;
  }
2828 2829 2830
  return 0;
}

unknown's avatar
unknown committed
2831
int set_var_collation_client::update(THD *thd)
2832
{
2833 2834
  thd->variables.character_set_client= character_set_client;
  thd->variables.character_set_results= character_set_results;
unknown's avatar
unknown committed
2835
  thd->variables.collation_connection= collation_connection;
unknown's avatar
unknown committed
2836
  thd->update_charset();
unknown's avatar
unknown committed
2837 2838
  thd->protocol_text.init(thd);
  thd->protocol_binary.init(thd);
2839 2840 2841 2842 2843
  return 0;
}

/****************************************************************************/

2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859
bool sys_var_timestamp::check(THD *thd, set_var *var)
{
  time_t val;
  var->save_result.ulonglong_value= var->value->val_int();
  val= (time_t) var->save_result.ulonglong_value;
  if (val < (time_t) MY_TIME_T_MIN || val > (time_t) MY_TIME_T_MAX)
  {
    my_message(ER_UNKNOWN_ERROR, 
               "This version of MySQL doesn't support dates later than 2038",
               MYF(0));
    return TRUE;
  }
  return FALSE;
}


unknown's avatar
unknown committed
2860 2861
bool sys_var_timestamp::update(THD *thd,  set_var *var)
{
2862
  thd->set_time((time_t) var->save_result.ulonglong_value);
2863
  return FALSE;
unknown's avatar
unknown committed
2864 2865 2866 2867 2868 2869 2870 2871 2872
}


void sys_var_timestamp::set_default(THD *thd, enum_var_type type)
{
  thd->user_time=0;
}


2873
uchar *sys_var_timestamp::value_ptr(THD *thd, enum_var_type type,
2874
				   LEX_STRING *base)
unknown's avatar
unknown committed
2875 2876
{
  thd->sys_var_tmp.long_value= (long) thd->start_time;
2877
  return (uchar*) &thd->sys_var_tmp.long_value;
unknown's avatar
unknown committed
2878 2879 2880 2881 2882
}


bool sys_var_last_insert_id::update(THD *thd, set_var *var)
{
2883 2884
  thd->first_successful_insert_id_in_prev_stmt= 
    var->save_result.ulonglong_value;
unknown's avatar
unknown committed
2885 2886 2887 2888
  return 0;
}


2889
uchar *sys_var_last_insert_id::value_ptr(THD *thd, enum_var_type type,
2890
					LEX_STRING *base)
unknown's avatar
unknown committed
2891
{
2892 2893 2894 2895 2896 2897
  /*
    this tmp var makes it robust againt change of type of 
    read_first_successful_insert_id_in_prev_stmt().
  */
  thd->sys_var_tmp.ulonglong_value= 
    thd->read_first_successful_insert_id_in_prev_stmt();
2898
  return (uchar*) &thd->sys_var_tmp.ulonglong_value;
unknown's avatar
unknown committed
2899 2900 2901 2902 2903
}


bool sys_var_insert_id::update(THD *thd, set_var *var)
{
2904
  thd->force_one_auto_inc_interval(var->save_result.ulonglong_value);
unknown's avatar
unknown committed
2905 2906 2907 2908
  return 0;
}


2909
uchar *sys_var_insert_id::value_ptr(THD *thd, enum_var_type type,
2910
				   LEX_STRING *base)
unknown's avatar
unknown committed
2911
{
2912 2913
  thd->sys_var_tmp.ulonglong_value= 
    thd->auto_inc_intervals_forced.minimum();
2914
  return (uchar*) &thd->sys_var_tmp.ulonglong_value;
unknown's avatar
unknown committed
2915
}
2916 2917


2918 2919
bool sys_var_rand_seed1::update(THD *thd, set_var *var)
{
2920
  thd->rand.seed1= (ulong) var->save_result.ulonglong_value;
2921 2922 2923 2924 2925
  return 0;
}

bool sys_var_rand_seed2::update(THD *thd, set_var *var)
{
2926
  thd->rand.seed2= (ulong) var->save_result.ulonglong_value;
2927 2928 2929 2930
  return 0;
}


2931 2932
bool sys_var_thd_time_zone::check(THD *thd, set_var *var)
{
2933
  char buff[MAX_TIME_ZONE_NAME_LENGTH];
2934 2935 2936
  String str(buff, sizeof(buff), &my_charset_latin1);
  String *res= var->value->val_str(&str);

2937
  if (!(var->save_result.time_zone= my_tz_find(thd, res)))
2938
  {
2939
    my_error(ER_UNKNOWN_TIME_ZONE, MYF(0), res ? res->c_ptr() : "NULL");
2940 2941 2942 2943 2944 2945 2946 2947
    return 1;
  }
  return 0;
}


bool sys_var_thd_time_zone::update(THD *thd, set_var *var)
{
2948 2949 2950 2951 2952 2953 2954 2955 2956
  /* We are using Time_zone object found during check() phase. */
  if (var->type == OPT_GLOBAL)
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
    global_system_variables.time_zone= var->save_result.time_zone;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.time_zone= var->save_result.time_zone;
2957 2958 2959 2960
  return 0;
}


2961
uchar *sys_var_thd_time_zone::value_ptr(THD *thd, enum_var_type type,
2962 2963 2964 2965 2966 2967 2968
				       LEX_STRING *base)
{
  /* 
    We can use ptr() instead of c_ptr() here because String contaning
    time zone name is guaranteed to be zero ended.
  */
  if (type == OPT_GLOBAL)
2969
    return (uchar *)(global_system_variables.time_zone->get_name()->ptr());
2970
  else
2971 2972 2973 2974 2975 2976 2977 2978 2979 2980
  {
    /*
      This is an ugly fix for replication: we don't replicate properly queries
      invoking system variables' values to update tables; but
      CONVERT_TZ(,,@@session.time_zone) is so popular that we make it
      replicable (i.e. we tell the binlog code to store the session
      timezone). If it's the global value which was used we can't replicate
      (binlog code stores session value only).
    */
    thd->time_zone_used= 1;
2981
    return (uchar *)(thd->variables.time_zone->get_name()->ptr());
2982
  }
2983 2984 2985 2986 2987
}


void sys_var_thd_time_zone::set_default(THD *thd, enum_var_type type)
{
2988
 pthread_mutex_lock(&LOCK_global_system_variables);
2989 2990 2991 2992 2993
 if (type == OPT_GLOBAL)
 {
   if (default_tz_name)
   {
     String str(default_tz_name, &my_charset_latin1);
2994 2995 2996 2997
     /*
       We are guaranteed to find this time zone since its existence
       is checked during start-up.
     */
2998
     global_system_variables.time_zone= my_tz_find(thd, &str);
2999 3000 3001 3002 3003 3004
   }
   else
     global_system_variables.time_zone= my_tz_SYSTEM;
 }
 else
   thd->variables.time_zone= global_system_variables.time_zone;
3005
 pthread_mutex_unlock(&LOCK_global_system_variables);
3006 3007
}

3008 3009 3010 3011 3012 3013 3014 3015 3016

bool sys_var_max_user_conn::check(THD *thd, set_var *var)
{
  if (var->type == OPT_GLOBAL)
    return sys_var_thd::check(thd, var);
  else
  {
    /*
      Per-session values of max_user_connections can't be set directly.
3017
      May be we should have a separate error message for this?
3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
    */
    my_error(ER_GLOBAL_VARIABLE, MYF(0), name);
    return TRUE;
  }
}

bool sys_var_max_user_conn::update(THD *thd, set_var *var)
{
  DBUG_ASSERT(var->type == OPT_GLOBAL);
  pthread_mutex_lock(&LOCK_global_system_variables);
3028
  max_user_connections= (uint)var->save_result.ulonglong_value;
3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return 0;
}


void sys_var_max_user_conn::set_default(THD *thd, enum_var_type type)
{
  DBUG_ASSERT(type == OPT_GLOBAL);
  pthread_mutex_lock(&LOCK_global_system_variables);
  max_user_connections= (ulong) option_limits->def_value;
  pthread_mutex_unlock(&LOCK_global_system_variables);
}


3043
uchar *sys_var_max_user_conn::value_ptr(THD *thd, enum_var_type type,
3044 3045 3046 3047
                                       LEX_STRING *base)
{
  if (type != OPT_GLOBAL &&
      thd->user_connect && thd->user_connect->user_resources.user_conn)
3048 3049
    return (uchar*) &(thd->user_connect->user_resources.user_conn);
  return (uchar*) &(max_user_connections);
3050 3051
}

3052

3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
bool sys_var_thd_ulong_session_readonly::check(THD *thd, set_var *var)
{
  if (var->type != OPT_GLOBAL)
  {
    my_error(ER_VARIABLE_IS_READONLY, MYF(0), "SESSION", name, "GLOBAL");
    return TRUE;
  }

  return sys_var_thd_ulong::check(thd, var);
}


unknown's avatar
unknown committed
3065 3066
bool sys_var_thd_lc_time_names::check(THD *thd, set_var *var)
{
3067
  MY_LOCALE *locale_match;
unknown's avatar
unknown committed
3068

3069
  if (var->value->result_type() == INT_RESULT)
unknown's avatar
unknown committed
3070
  {
3071 3072 3073 3074 3075 3076 3077
    if (!(locale_match= my_locale_by_number((uint) var->value->val_int())))
    {
      char buf[20];
      int10_to_str((int) var->value->val_int(), buf, -10);
      my_printf_error(ER_UNKNOWN_ERROR, "Unknown locale: '%s'", MYF(0), buf);
      return 1;
    }
unknown's avatar
unknown committed
3078
  }
3079
  else // STRING_RESULT
unknown's avatar
unknown committed
3080
  {
3081 3082 3083 3084 3085 3086 3087
    char buff[6]; 
    String str(buff, sizeof(buff), &my_charset_latin1), *res;
    if (!(res=var->value->val_str(&str)))
    {
      my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
      return 1;
    }
3088
    const char *locale_str= res->c_ptr_safe();
3089 3090 3091 3092 3093 3094
    if (!(locale_match= my_locale_by_name(locale_str)))
    {
      my_printf_error(ER_UNKNOWN_ERROR,
                      "Unknown locale: '%s'", MYF(0), locale_str);
      return 1;
    }
unknown's avatar
unknown committed
3095
  }
3096

3097 3098
  var->save_result.locale_value= locale_match;
  return 0;
unknown's avatar
unknown committed
3099 3100 3101 3102 3103
}


bool sys_var_thd_lc_time_names::update(THD *thd, set_var *var)
{
3104 3105 3106 3107
  if (var->type == OPT_GLOBAL)
    global_system_variables.lc_time_names= var->save_result.locale_value;
  else
    thd->variables.lc_time_names= var->save_result.locale_value;
unknown's avatar
unknown committed
3108 3109 3110 3111
  return 0;
}


3112
uchar *sys_var_thd_lc_time_names::value_ptr(THD *thd, enum_var_type type,
unknown's avatar
unknown committed
3113 3114
					  LEX_STRING *base)
{
3115
  return type == OPT_GLOBAL ?
3116 3117
                 (uchar *) global_system_variables.lc_time_names->name :
                 (uchar *) thd->variables.lc_time_names->name;
unknown's avatar
unknown committed
3118 3119 3120 3121 3122
}


void sys_var_thd_lc_time_names::set_default(THD *thd, enum_var_type type)
{
3123 3124 3125 3126
  if (type == OPT_GLOBAL)
    global_system_variables.lc_time_names= my_default_lc_time_names;
  else
    thd->variables.lc_time_names= global_system_variables.lc_time_names;
unknown's avatar
unknown committed
3127
}
3128

3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182
/*
  Handling of microseoncds given as seconds.part_seconds

  NOTES
    The argument to long query time is in seconds in decimal
    which is converted to ulonglong integer holding microseconds for storage.
    This is used for handling long_query_time
*/

bool sys_var_microseconds::update(THD *thd, set_var *var)
{
  double num= var->value->val_real();
  longlong microseconds;
  if (num > (double) option_limits->max_value)
    num= (double) option_limits->max_value;
  if (num < (double) option_limits->min_value)
    num= (double) option_limits->min_value;
  microseconds= (longlong) (num * 1000000.0 + 0.5);
  if (var->type == OPT_GLOBAL)
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
    (global_system_variables.*offset)= microseconds;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.*offset= microseconds;
  return 0;
}


void sys_var_microseconds::set_default(THD *thd, enum_var_type type)
{
  longlong microseconds= (longlong) (option_limits->def_value * 1000000.0);
  if (type == OPT_GLOBAL)
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
    global_system_variables.*offset= microseconds;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.*offset= microseconds;
}


uchar *sys_var_microseconds::value_ptr(THD *thd, enum_var_type type,
                                          LEX_STRING *base)
{
  thd->tmp_double_value= (double) ((type == OPT_GLOBAL) ?
                                   global_system_variables.*offset :
                                   thd->variables.*offset) / 1000000.0;
  return (uchar*) &thd->tmp_double_value;
}


unknown's avatar
unknown committed
3183 3184 3185 3186 3187 3188
/*
  Functions to update thd->options bits
*/

static bool set_option_bit(THD *thd, set_var *var)
{
3189 3190 3191
  sys_var_thd_bit *sys_var= ((sys_var_thd_bit*) var->var);
  if ((var->save_result.ulong_value != 0) == sys_var->reverse)
    thd->options&= ~sys_var->bit_flag;
unknown's avatar
unknown committed
3192
  else
3193
    thd->options|= sys_var->bit_flag;
unknown's avatar
unknown committed
3194 3195 3196
  return 0;
}

3197 3198 3199 3200 3201 3202 3203 3204 3205 3206
/*
  Functions to be only used to update thd->options OPTION_BIN_LOG bit
*/
static bool set_option_log_bin_bit(THD *thd, set_var *var)
{
  set_option_bit(thd, var);
  if (!thd->in_sub_stmt)
    thd->sql_log_bin_toplevel= thd->options & OPTION_BIN_LOG;
  return 0;
}
unknown's avatar
unknown committed
3207 3208 3209 3210 3211

static bool set_option_autocommit(THD *thd, set_var *var)
{
  /* The test is negative as the flag we use is NOT autocommit */

3212
  ulonglong org_options= thd->options;
unknown's avatar
unknown committed
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223

  if (var->save_result.ulong_value != 0)
    thd->options&= ~((sys_var_thd_bit*) var->var)->bit_flag;
  else
    thd->options|= ((sys_var_thd_bit*) var->var)->bit_flag;

  if ((org_options ^ thd->options) & OPTION_NOT_AUTOCOMMIT)
  {
    if ((org_options & OPTION_NOT_AUTOCOMMIT))
    {
      /* We changed to auto_commit mode */
3224 3225 3226 3227 3228 3229 3230
      if (thd->transaction.xid_state.xa_state != XA_NOTR)
      {
        thd->options= org_options;
        my_error(ER_XAER_RMFAIL, MYF(0),
                 xa_state_names[thd->transaction.xid_state.xa_state]);
        return 1;
      }
unknown's avatar
unknown committed
3231
      thd->options&= ~(ulonglong) (OPTION_BEGIN | OPTION_KEEP_LOG);
unknown's avatar
unknown committed
3232
      thd->transaction.all.modified_non_trans_table= FALSE;
unknown's avatar
unknown committed
3233 3234 3235 3236 3237 3238
      thd->server_status|= SERVER_STATUS_AUTOCOMMIT;
      if (ha_commit(thd))
	return 1;
    }
    else
    {
unknown's avatar
unknown committed
3239
      thd->transaction.all.modified_non_trans_table= FALSE;
unknown's avatar
unknown committed
3240 3241 3242 3243 3244 3245
      thd->server_status&= ~SERVER_STATUS_AUTOCOMMIT;
    }
  }
  return 0;
}

3246 3247 3248
static int check_log_update(THD *thd, set_var *var)
{
#ifndef NO_EMBEDDED_ACCESS_CHECKS
3249
  if (!(thd->security_ctx->master_access & SUPER_ACL))
3250 3251 3252 3253 3254 3255 3256
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
#endif
  return 0;
}
unknown's avatar
unknown committed
3257 3258 3259

static bool set_log_update(THD *thd, set_var *var)
{
3260 3261 3262 3263 3264
  /*
    The update log is not supported anymore since 5.0.
    See sql/mysqld.cc/, comments in function init_server_components() for an
    explaination of the different warnings we send below
  */
3265

unknown's avatar
unknown committed
3266
  if (opt_sql_bin_update)
3267 3268 3269 3270 3271 3272 3273 3274 3275
  {
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
                 ER_UPDATE_LOG_DEPRECATED_TRANSLATED,
                 ER(ER_UPDATE_LOG_DEPRECATED_TRANSLATED));
  }
  else
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
                 ER_UPDATE_LOG_DEPRECATED_IGNORED,
                 ER(ER_UPDATE_LOG_DEPRECATED_IGNORED));
unknown's avatar
unknown committed
3276 3277 3278 3279 3280
  set_option_bit(thd, var);
  return 0;
}


3281 3282 3283 3284
static int check_pseudo_thread_id(THD *thd, set_var *var)
{
  var->save_result.ulonglong_value= var->value->val_int();
#ifndef NO_EMBEDDED_ACCESS_CHECKS
3285
  if (thd->security_ctx->master_access & SUPER_ACL)
3286 3287 3288 3289 3290 3291 3292 3293 3294 3295
    return 0;
  else
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
#else
  return 0;
#endif
}
3296

3297
static uchar *get_warning_count(THD *thd)
3298 3299 3300
{
  thd->sys_var_tmp.long_value=
    (thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_NOTE] +
3301
     thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_ERROR] +
3302
     thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_WARN]);
3303
  return (uchar*) &thd->sys_var_tmp.long_value;
3304 3305
}

3306
static uchar *get_error_count(THD *thd)
3307 3308 3309
{
  thd->sys_var_tmp.long_value= 
    thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_ERROR];
3310
  return (uchar*) &thd->sys_var_tmp.long_value;
3311 3312
}

3313

unknown's avatar
unknown committed
3314 3315
/**
  Get the tmpdir that was specified or chosen by default.
3316

unknown's avatar
unknown committed
3317 3318 3319 3320 3321
  This is necessary because if the user does not specify a temporary
  directory via the command line, one is chosen based on the environment
  or system defaults.  But we can't just always use mysql_tmpdir, because
  that is actually a call to my_tmpdir() which cycles among possible
  temporary directories.
3322

unknown's avatar
unknown committed
3323
  @param thd		thread handle
3324

unknown's avatar
unknown committed
3325
  @retval
3326
    ptr		pointer to NUL-terminated string
unknown's avatar
unknown committed
3327
*/
3328
static uchar *get_tmpdir(THD *thd)
3329 3330
{
  if (opt_mysql_tmpdir)
3331 3332
    return (uchar *)opt_mysql_tmpdir;
  return (uchar*)mysql_tmpdir;
3333 3334
}

3335
static uchar *get_myisam_mmap_size(THD *thd)
3336
{
3337
  return (uchar *)&myisam_mmap_size;
3338 3339
}

3340

unknown's avatar
unknown committed
3341 3342 3343 3344 3345 3346 3347
/****************************************************************************
  Main handling of variables:
  - Initialisation
  - Searching during parsing
  - Update loop
****************************************************************************/

unknown's avatar
unknown committed
3348 3349 3350
/**
  Find variable name in option my_getopt structure used for
  command line args.
unknown's avatar
unknown committed
3351

unknown's avatar
unknown committed
3352 3353
  @param opt	option structure array to search in
  @param name	variable name
unknown's avatar
unknown committed
3354

unknown's avatar
unknown committed
3355
  @retval
unknown's avatar
unknown committed
3356
    0		Error
unknown's avatar
unknown committed
3357
  @retval
unknown's avatar
unknown committed
3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
    ptr		pointer to option structure
*/

static struct my_option *find_option(struct my_option *opt, const char *name) 
{
  uint length=strlen(name);
  for (; opt->name; opt++)
  {
    if (!getopt_compare_strings(opt->name, name, length) &&
	!opt->name[length])
    {
      /*
	Only accept the option if one can set values through it.
	If not, there is no default value or limits in the option.
      */
      return (opt->value) ? opt : 0;
    }
  }
  return 0;
}


unknown's avatar
unknown committed
3380 3381
/**
  Return variable name and length for hashing of variables.
unknown's avatar
unknown committed
3382 3383
*/

3384 3385
static uchar *get_sys_var_length(const sys_var *var, size_t *length,
                                 my_bool first)
unknown's avatar
unknown committed
3386 3387
{
  *length= var->name_length;
3388
  return (uchar*) var->name;
unknown's avatar
unknown committed
3389 3390 3391 3392
}


/*
unknown's avatar
unknown committed
3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
  Add variables to the dynamic hash of system variables
  
  SYNOPSIS
    mysql_add_sys_var_chain()
    first       Pointer to first system variable to add
    long_opt    (optional)command line arguments may be tied for limit checks.
  
  RETURN VALUES
    0           SUCCESS
    otherwise   FAILURE
unknown's avatar
unknown committed
3403 3404 3405
*/


unknown's avatar
unknown committed
3406
int mysql_add_sys_var_chain(sys_var *first, struct my_option *long_options)
unknown's avatar
unknown committed
3407
{
3408
  sys_var *var;
unknown's avatar
unknown committed
3409 3410 3411 3412
  
  /* A write lock should be held on LOCK_system_variables_hash */
  
  for (var= first; var; var= var->next)
unknown's avatar
unknown committed
3413
  {
3414
    var->name_length= strlen(var->name);
unknown's avatar
unknown committed
3415
    /* this fails if there is a conflicting variable name. see HASH_UNIQUE */
3416
    if (my_hash_insert(&system_variable_hash, (uchar*) var))
unknown's avatar
unknown committed
3417 3418 3419
      goto error;
    if (long_options)
      var->option_limits= find_option(long_options, var->name);
unknown's avatar
unknown committed
3420
  }
unknown's avatar
unknown committed
3421 3422 3423 3424
  return 0;

error:
  for (; first != var; first= first->next)
3425
    hash_delete(&system_variable_hash, (uchar*) first);
unknown's avatar
unknown committed
3426 3427
  return 1;
}
3428 3429
 
 
unknown's avatar
unknown committed
3430 3431
/*
  Remove variables to the dynamic hash of system variables
3432
   
unknown's avatar
unknown committed
3433 3434 3435
  SYNOPSIS
    mysql_del_sys_var_chain()
    first       Pointer to first system variable to remove
3436
   
unknown's avatar
unknown committed
3437 3438 3439 3440
  RETURN VALUES
    0           SUCCESS
    otherwise   FAILURE
*/
3441
 
unknown's avatar
unknown committed
3442 3443 3444
int mysql_del_sys_var_chain(sys_var *first)
{
  int result= 0;
3445
 
unknown's avatar
unknown committed
3446
  /* A write lock should be held on LOCK_system_variables_hash */
3447
   
unknown's avatar
unknown committed
3448
  for (sys_var *var= first; var; var= var->next)
3449
    result|= hash_delete(&system_variable_hash, (uchar*) var);
unknown's avatar
unknown committed
3450 3451 3452

  return result;
}
3453 3454
 
 
unknown's avatar
unknown committed
3455 3456 3457 3458
static int show_cmp(SHOW_VAR *a, SHOW_VAR *b)
{
  return strcmp(a->name, b->name);
}
3459 3460
 
 
unknown's avatar
unknown committed
3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476
/*
  Constructs an array of system variables for display to the user.
  
  SYNOPSIS
    enumerate_sys_vars()
    thd         current thread
    sorted      If TRUE, the system variables should be sorted
  
  RETURN VALUES
    pointer     Array of SHOW_VAR elements for display
    NULL        FAILURE
*/

SHOW_VAR* enumerate_sys_vars(THD *thd, bool sorted)
{
  int count= system_variable_hash.records, i;
3477
  int size= sizeof(SHOW_VAR) * (count + 1);
unknown's avatar
unknown committed
3478 3479 3480 3481
  SHOW_VAR *result= (SHOW_VAR*) thd->alloc(size);

  if (result)
  {
3482
    SHOW_VAR *show= result;
unknown's avatar
unknown committed
3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494

    for (i= 0; i < count; i++)
    {
      sys_var *var= (sys_var*) hash_element(&system_variable_hash, i);
      show->name= var->name;
      show->value= (char*) var;
      show->type= SHOW_SYS;
      show++;
    }

    /* sort into order */
    if (sorted)
3495
      my_qsort(result, count, sizeof(SHOW_VAR),
3496
               (qsort_cmp) show_cmp);
unknown's avatar
unknown committed
3497 3498 3499
    
    /* make last element empty */
    bzero(show, sizeof(SHOW_VAR));
unknown's avatar
unknown committed
3500
  }
unknown's avatar
unknown committed
3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520
  return result;
}


/*
  Initialize the system variables
  
  SYNOPSIS
    set_var_init()
  
  RETURN VALUES
    0           SUCCESS
    otherwise   FAILURE
*/

int set_var_init()
{
  uint count= 0;
  DBUG_ENTER("set_var_init");
  
3521
  for (sys_var *var=vars.first; var; var= var->next, count++) ;
unknown's avatar
unknown committed
3522 3523 3524 3525 3526 3527 3528 3529 3530

  if (hash_init(&system_variable_hash, system_charset_info, count, 0,
                0, (hash_get_key) get_sys_var_length, 0, HASH_UNIQUE))
    goto error;

  vars.last->next= NULL;
  if (mysql_add_sys_var_chain(vars.first, my_long_options))
    goto error;

unknown's avatar
unknown committed
3531 3532 3533 3534 3535 3536 3537
  /*
    Special cases
    Needed because MySQL can't find the limits for a variable it it has
    a different name than the command line option.
    As these variables are deprecated, this code will disappear soon...
  */
  sys_sql_max_join_size.option_limits= sys_max_join_size.option_limits;
unknown's avatar
unknown committed
3538 3539 3540 3541 3542 3543

  DBUG_RETURN(0);

error:
  fprintf(stderr, "failed to initialize system variables");
  DBUG_RETURN(1);
unknown's avatar
unknown committed
3544 3545 3546 3547 3548 3549 3550 3551 3552
}


void set_var_free()
{
  hash_free(&system_variable_hash);
}


unknown's avatar
unknown committed
3553 3554
/**
  Find a user set-table variable.
unknown's avatar
unknown committed
3555

unknown's avatar
unknown committed
3556 3557 3558 3559
  @param str	   Name of system variable to find
  @param length    Length of variable.  zero means that we should use strlen()
                   on the variable
  @param no_error  Refuse to emit an error, even if one occurred.
unknown's avatar
unknown committed
3560

unknown's avatar
unknown committed
3561
  @retval
unknown's avatar
unknown committed
3562
    pointer	pointer to variable definitions
unknown's avatar
unknown committed
3563
  @retval
unknown's avatar
unknown committed
3564 3565 3566
    0		Unknown variable (error message is given)
*/

unknown's avatar
unknown committed
3567
sys_var *intern_find_sys_var(const char *str, uint length, bool no_error)
unknown's avatar
unknown committed
3568
{
unknown's avatar
unknown committed
3569 3570 3571 3572 3573 3574 3575
  sys_var *var;

  /*
    This function is only called from the sql_plugin.cc.
    A lock on LOCK_system_variable_hash should be held
  */
  var= (sys_var*) hash_search(&system_variable_hash,
3576
			      (uchar*) str, length ? length : strlen(str));
unknown's avatar
unknown committed
3577
  if (!(var || no_error))
3578
    my_error(ER_UNKNOWN_SYSTEM_VARIABLE, MYF(0), (char*) str);
unknown's avatar
unknown committed
3579

unknown's avatar
unknown committed
3580 3581 3582 3583
  return var;
}


unknown's avatar
unknown committed
3584 3585
/**
  Execute update of all variables.
unknown's avatar
unknown committed
3586

unknown's avatar
unknown committed
3587 3588
  First run a check of all variables that all updates will go ok.
  If yes, then execute all updates, returning an error if any one failed.
unknown's avatar
unknown committed
3589

unknown's avatar
unknown committed
3590 3591
  This should ensure that in all normal cases none all or variables are
  updated.
unknown's avatar
unknown committed
3592

unknown's avatar
unknown committed
3593 3594
  @param THD		Thread id
  @param var_list       List of variables to update
unknown's avatar
unknown committed
3595

unknown's avatar
unknown committed
3596
  @retval
unknown's avatar
unknown committed
3597
    0	ok
unknown's avatar
unknown committed
3598
  @retval
3599
    1	ERROR, message sent (normally no variables was updated)
unknown's avatar
unknown committed
3600
  @retval
3601
    -1  ERROR, message not sent
unknown's avatar
unknown committed
3602 3603
*/

3604
int sql_set_variables(THD *thd, List<set_var_base> *var_list)
unknown's avatar
unknown committed
3605
{
unknown's avatar
unknown committed
3606
  int error;
unknown's avatar
unknown committed
3607
  List_iterator_fast<set_var_base> it(*var_list);
unknown's avatar
unknown committed
3608
  DBUG_ENTER("sql_set_variables");
unknown's avatar
unknown committed
3609 3610 3611 3612

  set_var_base *var;
  while ((var=it++))
  {
unknown's avatar
unknown committed
3613
    if ((error= var->check(thd)))
3614
      goto err;
unknown's avatar
unknown committed
3615
  }
3616
  if (!(error= test(thd->is_error())))
3617 3618 3619 3620 3621
  {
    it.rewind();
    while ((var= it++))
      error|= var->update(thd);         // Returns 0, -1 or 1
  }
unknown's avatar
unknown committed
3622

3623 3624
err:
  free_underlaid_joins(thd, &thd->lex->select_lex);
unknown's avatar
unknown committed
3625
  DBUG_RETURN(error);
unknown's avatar
unknown committed
3626 3627 3628
}


unknown's avatar
unknown committed
3629 3630 3631 3632
/**
  Say if all variables set by a SET support the ONE_SHOT keyword
  (currently, only character set and collation do; later timezones
  will).
3633

unknown's avatar
unknown committed
3634
  @param var_list	List of variables to update
3635

unknown's avatar
unknown committed
3636
  @note
3637 3638
    It has a "not_" because it makes faster tests (no need to "!")

unknown's avatar
unknown committed
3639
  @retval
3640
    0	all variables of the list support ONE_SHOT
unknown's avatar
unknown committed
3641
  @retval
3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657
    1	at least one does not support ONE_SHOT
*/

bool not_all_support_one_shot(List<set_var_base> *var_list)
{
  List_iterator_fast<set_var_base> it(*var_list);
  set_var_base *var;
  while ((var= it++))
  {
    if (var->no_support_one_shot())
      return 1;
  }
  return 0;
}


unknown's avatar
unknown committed
3658 3659 3660 3661
/*****************************************************************************
  Functions to handle SET mysql_internal_variable=const_expr
*****************************************************************************/

3662
int set_var::check(THD *thd)
unknown's avatar
unknown committed
3663
{
3664 3665 3666 3667 3668
  if (var->is_readonly())
  {
    my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0), var->name, "read only");
    return -1;
  }
unknown's avatar
unknown committed
3669 3670
  if (var->check_type(type))
  {
unknown's avatar
unknown committed
3671
    int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
3672
    my_error(err, MYF(0), var->name);
3673
    return -1;
unknown's avatar
unknown committed
3674 3675 3676 3677 3678 3679 3680 3681
  }
  if ((type == OPT_GLOBAL && check_global_access(thd, SUPER_ACL)))
    return 1;
  /* value is a NULL pointer if we are using SET ... = DEFAULT */
  if (!value)
  {
    if (var->check_default(type))
    {
3682
      my_error(ER_NO_DEFAULT, MYF(0), var->name);
3683
      return -1;
unknown's avatar
unknown committed
3684 3685 3686 3687
    }
    return 0;
  }

unknown's avatar
unknown committed
3688
  if ((!value->fixed &&
3689
       value->fix_fields(thd, &value)) || value->check_cols(1))
3690
    return -1;
unknown's avatar
unknown committed
3691 3692
  if (var->check_update_type(value->result_type()))
  {
3693
    my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var->name);
3694
    return -1;
3695
  }
3696
  return var->check(thd, this) ? -1 : 0;
unknown's avatar
unknown committed
3697 3698 3699
}


unknown's avatar
unknown committed
3700 3701
/**
  Check variable, but without assigning value (used by PS).
unknown's avatar
unknown committed
3702

unknown's avatar
unknown committed
3703
  @param thd		thread handler
unknown's avatar
unknown committed
3704

unknown's avatar
unknown committed
3705
  @retval
unknown's avatar
unknown committed
3706
    0	ok
unknown's avatar
unknown committed
3707
  @retval
unknown's avatar
unknown committed
3708
    1	ERROR, message sent (normally no variables was updated)
unknown's avatar
unknown committed
3709 3710
  @retval
    -1   ERROR, message not sent
unknown's avatar
unknown committed
3711
*/
3712 3713 3714 3715
int set_var::light_check(THD *thd)
{
  if (var->check_type(type))
  {
unknown's avatar
unknown committed
3716
    int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
3717
    my_error(err, MYF(0), var->name);
3718 3719
    return -1;
  }
unknown's avatar
unknown committed
3720
  if (type == OPT_GLOBAL && check_global_access(thd, SUPER_ACL))
3721 3722
    return 1;

3723
  if (value && ((!value->fixed && value->fix_fields(thd, &value)) ||
unknown's avatar
unknown committed
3724
                value->check_cols(1)))
3725 3726 3727 3728
    return -1;
  return 0;
}

3729 3730
/**
  Update variable
3731

3732 3733 3734 3735 3736 3737 3738 3739 3740
  @param   thd    thread handler
  @returns 0|1    ok or	ERROR

  @note ERROR can be only due to abnormal operations involving
  the server's execution evironment such as
  out of memory, hard disk failure or the computer blows up.
  Consider set_var::check() method if there is a need to return
  an error due to logics.
*/
3741
int set_var::update(THD *thd)
unknown's avatar
unknown committed
3742 3743 3744 3745
{
  if (!value)
    var->set_default(thd, type);
  else if (var->update(thd, this))
3746
    return -1;				// should never happen
unknown's avatar
unknown committed
3747 3748 3749 3750 3751 3752 3753 3754 3755 3756
  if (var->after_update)
    (*var->after_update)(thd, type);
  return 0;
}


/*****************************************************************************
  Functions to handle SET @user_variable=const_expr
*****************************************************************************/

3757
int set_var_user::check(THD *thd)
unknown's avatar
unknown committed
3758
{
unknown's avatar
unknown committed
3759 3760
  /*
    Item_func_set_user_var can't substitute something else on its place =>
3761
    0 can be passed as last argument (reference on item)
unknown's avatar
unknown committed
3762
  */
3763
  return (user_var_item->fix_fields(thd, (Item**) 0) ||
3764
	  user_var_item->check(0)) ? -1 : 0;
unknown's avatar
unknown committed
3765 3766 3767
}


unknown's avatar
unknown committed
3768 3769
/**
  Check variable, but without assigning value (used by PS).
unknown's avatar
unknown committed
3770

unknown's avatar
unknown committed
3771
  @param thd		thread handler
unknown's avatar
unknown committed
3772

unknown's avatar
unknown committed
3773
  @retval
unknown's avatar
unknown committed
3774
    0	ok
unknown's avatar
unknown committed
3775
  @retval
unknown's avatar
unknown committed
3776
    1	ERROR, message sent (normally no variables was updated)
unknown's avatar
unknown committed
3777 3778
  @retval
    -1   ERROR, message not sent
unknown's avatar
unknown committed
3779
*/
3780 3781 3782 3783 3784 3785
int set_var_user::light_check(THD *thd)
{
  /*
    Item_func_set_user_var can't substitute something else on its place =>
    0 can be passed as last argument (reference on item)
  */
3786
  return (user_var_item->fix_fields(thd, (Item**) 0));
3787 3788 3789
}


3790
int set_var_user::update(THD *thd)
unknown's avatar
unknown committed
3791 3792 3793 3794
{
  if (user_var_item->update())
  {
    /* Give an error if it's not given already */
unknown's avatar
unknown committed
3795
    my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY), MYF(0));
3796
    return -1;
unknown's avatar
unknown committed
3797 3798 3799 3800 3801 3802 3803 3804 3805
  }
  return 0;
}


/*****************************************************************************
  Functions to handle SET PASSWORD
*****************************************************************************/

3806
int set_var_password::check(THD *thd)
unknown's avatar
unknown committed
3807
{
unknown's avatar
unknown committed
3808
#ifndef NO_EMBEDDED_ACCESS_CHECKS
unknown's avatar
unknown committed
3809
  if (!user->host.str)
3810
  {
3811
    DBUG_ASSERT(thd->security_ctx->priv_host);
3812
    if (*thd->security_ctx->priv_host != 0)
3813
    {
3814 3815
      user->host.str= (char *) thd->security_ctx->priv_host;
      user->host.length= strlen(thd->security_ctx->priv_host);
3816 3817 3818 3819 3820 3821 3822
    }
    else
    {
      user->host.str= (char *)"%";
      user->host.length= 1;
    }
  }
3823 3824 3825 3826 3827 3828
  if (!user->user.str)
  {
    DBUG_ASSERT(thd->security_ctx->priv_user);
    user->user.str= (char *) thd->security_ctx->priv_user;
    user->user.length= strlen(thd->security_ctx->priv_user);
  }
3829
  /* Returns 1 as the function sends error to client */
3830 3831
  return check_change_password(thd, user->host.str, user->user.str,
                               password, strlen(password)) ? 1 : 0;
3832
#else
unknown's avatar
unknown committed
3833 3834
  return 0;
#endif
unknown's avatar
unknown committed
3835 3836
}

3837
int set_var_password::update(THD *thd)
unknown's avatar
unknown committed
3838
{
unknown's avatar
unknown committed
3839
#ifndef NO_EMBEDDED_ACCESS_CHECKS
3840
  /* Returns 1 as the function sends error to client */
3841 3842
  return change_password(thd, user->host.str, user->user.str, password) ?
	  1 : 0;
unknown's avatar
unknown committed
3843 3844 3845
#else
  return 0;
#endif
unknown's avatar
unknown committed
3846 3847
}

3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865
/****************************************************************************
 Functions to handle log_slow_filter
****************************************************************************/
  
/* Ensure that the proper bits are set for easy test of logging */
static void fix_sys_log_slow_filter(THD *thd, enum_var_type type)
{
  /* Maintain everything with filters */
  opt_log_slow_admin_statements= 1;
  if (type == OPT_GLOBAL)
    global_system_variables.log_slow_filter=
      fix_log_slow_filter(global_system_variables.log_slow_filter);
  else
    thd->variables.log_slow_filter=
      fix_log_slow_filter(thd->variables.log_slow_filter);
}


3866 3867 3868 3869
/****************************************************************************
 Functions to handle table_type
****************************************************************************/

unknown's avatar
unknown committed
3870 3871
/* Based upon sys_var::check_enum() */

unknown's avatar
unknown committed
3872
bool sys_var_thd_storage_engine::check(THD *thd, set_var *var)
3873
{
unknown's avatar
unknown committed
3874
  char buff[STRING_BUFFER_USUAL_SIZE];
3875
  const char *value;
3876
  String str(buff, sizeof(buff) - 1, &my_charset_latin1), *res;
3877

unknown's avatar
unknown committed
3878
  var->save_result.plugin= NULL;
3879 3880
  if (var->value->result_type() == STRING_RESULT)
  {
3881
    LEX_STRING engine_name;
unknown's avatar
unknown committed
3882
    handlerton *hton;
3883
    if (!(res=var->value->val_str(&str)) ||
3884 3885 3886
        !(engine_name.str= (char *)res->ptr()) ||
        !(engine_name.length= res->length()) ||
	!(var->save_result.plugin= ha_resolve_by_name(thd, &engine_name)) ||
unknown's avatar
unknown committed
3887 3888
        !(hton= plugin_data(var->save_result.plugin, handlerton *)) ||
        ha_checktype(thd, ha_legacy_type(hton), 1, 0) != hton)
3889 3890 3891 3892 3893 3894
    {
      value= res ? res->c_ptr() : "NULL";
      goto err;
    }
    return 0;
  }
unknown's avatar
unknown committed
3895
  value= "unknown";
3896 3897

err:
3898
  my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), value);
unknown's avatar
unknown committed
3899
  return 1;
3900 3901
}

unknown's avatar
unknown committed
3902

3903
uchar *sys_var_thd_storage_engine::value_ptr(THD *thd, enum_var_type type,
unknown's avatar
unknown committed
3904
					    LEX_STRING *base)
3905
{
3906
  uchar* result;
unknown's avatar
unknown committed
3907
  handlerton *hton;
3908
  LEX_STRING *engine_name;
unknown's avatar
unknown committed
3909 3910
  plugin_ref plugin= thd->variables.*offset;
  if (type == OPT_GLOBAL)
3911
    plugin= my_plugin_lock(thd, global_system_variables.*offset);
unknown's avatar
unknown committed
3912
  hton= plugin_data(plugin, handlerton*);
3913
  engine_name= hton_name(hton);
3914
  result= (uchar *) thd->strmake(engine_name->str, engine_name->length);
unknown's avatar
unknown committed
3915 3916 3917
  if (type == OPT_GLOBAL)
    plugin_unlock(thd, plugin);
  return result;
3918 3919
}

unknown's avatar
unknown committed
3920

unknown's avatar
unknown committed
3921
void sys_var_thd_storage_engine::set_default(THD *thd, enum_var_type type)
3922
{
unknown's avatar
unknown committed
3923
  plugin_ref old_value, new_value, *value;
3924
  if (type == OPT_GLOBAL)
unknown's avatar
unknown committed
3925 3926 3927 3928
  {
    value= &(global_system_variables.*offset);
    new_value= ha_lock_engine(NULL, myisam_hton);
  }
3929
  else
unknown's avatar
unknown committed
3930 3931
  {
    value= &(thd->variables.*offset);
3932
    new_value= my_plugin_lock(NULL, global_system_variables.*offset);
unknown's avatar
unknown committed
3933 3934 3935 3936 3937
  }
  DBUG_ASSERT(new_value);
  old_value= *value;
  *value= new_value;
  plugin_unlock(NULL, old_value);
3938 3939
}

unknown's avatar
unknown committed
3940

unknown's avatar
unknown committed
3941
bool sys_var_thd_storage_engine::update(THD *thd, set_var *var)
3942
{
unknown's avatar
unknown committed
3943 3944 3945 3946 3947 3948
  plugin_ref *value= &(global_system_variables.*offset), old_value;
   if (var->type != OPT_GLOBAL)
     value= &(thd->variables.*offset);
  old_value= *value;
  if (old_value != var->save_result.plugin)
  {
3949
    *value= my_plugin_lock(NULL, var->save_result.plugin);
unknown's avatar
unknown committed
3950 3951
    plugin_unlock(NULL, old_value);
  }
3952 3953 3954
  return 0;
}

unknown's avatar
unknown committed
3955 3956
void sys_var_thd_table_type::warn_deprecated(THD *thd)
{
3957
  WARN_DEPRECATED(thd, "6.0", "@@table_type", "'@@storage_engine'");
unknown's avatar
unknown committed
3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971
}

void sys_var_thd_table_type::set_default(THD *thd, enum_var_type type)
{
  warn_deprecated(thd);
  sys_var_thd_storage_engine::set_default(thd, type);
}

bool sys_var_thd_table_type::update(THD *thd, set_var *var)
{
  warn_deprecated(thd);
  return sys_var_thd_storage_engine::update(thd, var);
}

unknown's avatar
unknown committed
3972

3973 3974 3975
/****************************************************************************
 Functions to handle sql_mode
****************************************************************************/
3976

3977 3978 3979 3980 3981 3982
void fix_sql_mode_var(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
    global_system_variables.sql_mode=
      fix_sql_mode(global_system_variables.sql_mode);
  else
3983
  {
3984
    thd->variables.sql_mode= fix_sql_mode(thd->variables.sql_mode);
3985 3986 3987 3988 3989 3990 3991 3992
    /*
      Update thd->server_status
     */
    if (thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES)
      thd->server_status|= SERVER_STATUS_NO_BACKSLASH_ESCAPES;
    else
      thd->server_status&= ~SERVER_STATUS_NO_BACKSLASH_ESCAPES;
  }
3993 3994
}

unknown's avatar
unknown committed
3995
/** Map database specific bits to function bits. */
3996

3997 3998 3999 4000 4001 4002 4003 4004 4005
ulong fix_sql_mode(ulong sql_mode)
{
  /*
    Note that we dont set 
    MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS | MODE_NO_FIELD_OPTIONS
    to allow one to get full use of MySQL in this mode.
  */

  if (sql_mode & MODE_ANSI)
4006
  {
4007
    sql_mode|= (MODE_REAL_AS_FLOAT | MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
4008 4009 4010 4011 4012 4013
		MODE_IGNORE_SPACE);
    /* 
      MODE_ONLY_FULL_GROUP_BY removed from ANSI mode because it is currently
      overly restrictive (see BUG#8510).
    */
  }
4014 4015 4016 4017
  if (sql_mode & MODE_ORACLE)
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
4018
		MODE_NO_FIELD_OPTIONS | MODE_NO_AUTO_CREATE_USER);
4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033
  if (sql_mode & MODE_MSSQL)
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
		MODE_NO_FIELD_OPTIONS);
  if (sql_mode & MODE_POSTGRESQL)
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
		MODE_NO_FIELD_OPTIONS);
  if (sql_mode & MODE_DB2)
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
		MODE_NO_FIELD_OPTIONS);
4034
  if (sql_mode & MODE_MAXDB)
4035 4036 4037
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
4038
		MODE_NO_FIELD_OPTIONS | MODE_NO_AUTO_CREATE_USER);
4039
  if (sql_mode & MODE_MYSQL40)
4040
    sql_mode|= MODE_HIGH_NOT_PRECEDENCE;
4041
  if (sql_mode & MODE_MYSQL323)
4042
    sql_mode|= MODE_HIGH_NOT_PRECEDENCE;
unknown's avatar
unknown committed
4043 4044 4045
  if (sql_mode & MODE_TRADITIONAL)
    sql_mode|= (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES |
                MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
4046
                MODE_ERROR_FOR_DIVISION_BY_ZERO | MODE_NO_AUTO_CREATE_USER);
4047 4048
  return sql_mode;
}
4049 4050


4051 4052 4053 4054
bool
sys_var_thd_optimizer_switch::
symbolic_mode_representation(THD *thd, ulonglong val, LEX_STRING *rep)
{
4055
  char buff[STRING_BUFFER_USUAL_SIZE*18];
4056
  String tmp(buff, sizeof(buff), &my_charset_latin1);
4057 4058
  int i;
  ulonglong bit;
4059
  tmp.length(0);
4060 4061
 
  for (i= 0, bit=1; bit != OPTIMIZER_SWITCH_LAST; i++, bit= bit << 1)
4062
  {
4063 4064 4065 4066 4067
    tmp.append(optimizer_switch_typelib.type_names[i],
               optimizer_switch_typelib.type_lengths[i]);
    tmp.append('=');
    tmp.append((val & bit)? "on":"off");
    tmp.append(',');
4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091
  }

  if (tmp.length())
    tmp.length(tmp.length() - 1); /* trim the trailing comma */

  rep->str= thd->strmake(tmp.ptr(), tmp.length());

  rep->length= rep->str ? tmp.length() : 0;

  return rep->length != tmp.length();
}


uchar *sys_var_thd_optimizer_switch::value_ptr(THD *thd, enum_var_type type,
				               LEX_STRING *base)
{
  LEX_STRING opts;
  ulonglong val= ((type == OPT_GLOBAL) ? global_system_variables.*offset :
                  thd->variables.*offset);
  (void) symbolic_mode_representation(thd, val, &opts);
  return (uchar *) opts.str;
}


4092 4093 4094 4095 4096
/*
  Check (and actually parse) string representation of @@optimizer_switch.
*/

bool sys_var_thd_optimizer_switch::check(THD *thd, set_var *var)
4097
{
4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119
  bool not_used;
  char buff[STRING_BUFFER_USUAL_SIZE], *error= 0;
  uint error_len= 0;
  String str(buff, sizeof(buff), system_charset_info), *res;

  if (!(res= var->value->val_str(&str)))
  {
    strmov(buff, "NULL");
    goto err;
  }
  
  if (res->length() == 0)
  {
    buff[0]= 0;
    goto err;
  }

  var->save_result.ulong_value= 
    (ulong)find_set_from_flags(&optimizer_switch_typelib, 
                               optimizer_switch_typelib.count, 
                               thd->variables.optimizer_switch,
                               global_system_variables.optimizer_switch,
4120
                               res->ptr(), res->length(), NULL,
4121 4122 4123 4124 4125 4126 4127 4128 4129 4130
                               &error, &error_len, &not_used);
  if (error_len)
  {
    strmake(buff, error, min(sizeof(buff) - 1, error_len));
    goto err;
  }
  return FALSE;
err:
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, buff);
  return TRUE;
4131 4132 4133
}


4134 4135 4136 4137 4138 4139 4140 4141
void sys_var_thd_optimizer_switch::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
    global_system_variables.*offset= OPTIMIZER_SWITCH_DEFAULT;
  else
    thd->variables.*offset= global_system_variables.*offset;
}

4142 4143 4144 4145
/****************************************************************************
  Named list handling
****************************************************************************/

4146
uchar* find_named(I_List<NAMED_LIST> *list, const char *name, uint length,
unknown's avatar
unknown committed
4147
		NAMED_LIST **found)
4148 4149 4150 4151 4152 4153
{
  I_List_iterator<NAMED_LIST> it(*list);
  NAMED_LIST *element;
  while ((element= it++))
  {
    if (element->cmp(name, length))
unknown's avatar
unknown committed
4154
    {
4155 4156
      if (found)
        *found= element;
4157
      return element->data;
unknown's avatar
unknown committed
4158
    }
4159 4160 4161 4162 4163
  }
  return 0;
}


4164
void delete_elements(I_List<NAMED_LIST> *list,
4165
		     void (*free_element)(const char *name, uchar*))
4166 4167
{
  NAMED_LIST *element;
unknown's avatar
unknown committed
4168
  DBUG_ENTER("delete_elements");
4169 4170
  while ((element= list->get()))
  {
4171
    (*free_element)(element->name, element->data);
4172 4173
    delete element;
  }
unknown's avatar
unknown committed
4174
  DBUG_VOID_RETURN;
4175 4176 4177 4178 4179
}


/* Key cache functions */

unknown's avatar
unknown committed
4180
static KEY_CACHE *create_key_cache(const char *name, uint length)
4181
{
unknown's avatar
unknown committed
4182
  KEY_CACHE *key_cache;
4183 4184 4185
  DBUG_ENTER("create_key_cache");
  DBUG_PRINT("enter",("name: %.*s", length, name));
  
unknown's avatar
unknown committed
4186
  if ((key_cache= (KEY_CACHE*) my_malloc(sizeof(KEY_CACHE),
4187
					     MYF(MY_ZEROFILL | MY_WME))))
unknown's avatar
unknown committed
4188
  {
4189
    if (!new NAMED_LIST(&key_caches, name, length, (uchar*) key_cache))
4190 4191
    {
      my_free((char*) key_cache, MYF(0));
unknown's avatar
unknown committed
4192
      key_cache= 0;
4193 4194 4195 4196 4197 4198 4199 4200 4201
    }
    else
    {
      /*
	Set default values for a key cache
	The values in dflt_key_cache_var is set by my_getopt() at startup

	We don't set 'buff_size' as this is used to enable the key cache
      */
unknown's avatar
unknown committed
4202 4203 4204
      key_cache->param_block_size=     dflt_key_cache_var.param_block_size;
      key_cache->param_division_limit= dflt_key_cache_var.param_division_limit;
      key_cache->param_age_threshold=  dflt_key_cache_var.param_age_threshold;
4205
      key_cache->param_partitions=     dflt_key_cache_var.param_partitions;
4206
    }
unknown's avatar
unknown committed
4207
  }
4208
  DBUG_RETURN(key_cache);
4209 4210 4211
}


unknown's avatar
unknown committed
4212
KEY_CACHE *get_or_create_key_cache(const char *name, uint length)
4213
{
unknown's avatar
unknown committed
4214
  LEX_STRING key_cache_name;
unknown's avatar
unknown committed
4215
  KEY_CACHE *key_cache;
4216

unknown's avatar
unknown committed
4217 4218
  key_cache_name.str= (char *) name;
  key_cache_name.length= length;
4219 4220
  pthread_mutex_lock(&LOCK_global_system_variables);
  if (!(key_cache= get_key_cache(&key_cache_name)))
4221
    key_cache= create_key_cache(name, length);
4222
  pthread_mutex_unlock(&LOCK_global_system_variables);
4223 4224 4225 4226
  return key_cache;
}


unknown's avatar
unknown committed
4227
void free_key_cache(const char *name, KEY_CACHE *key_cache)
4228
{
4229 4230
  ha_end_key_cache(key_cache);
  my_free((char*) key_cache, MYF(0));
unknown's avatar
unknown committed
4231 4232 4233
}


4234
bool process_key_caches(process_key_cache_t func)
4235
{
unknown's avatar
unknown committed
4236 4237
  I_List_iterator<NAMED_LIST> it(key_caches);
  NAMED_LIST *element;
4238

unknown's avatar
unknown committed
4239 4240
  while ((element= it++))
  {
unknown's avatar
unknown committed
4241
    KEY_CACHE *key_cache= (KEY_CACHE *) element->data;
4242
    func(element->name, key_cache);
unknown's avatar
unknown committed
4243 4244
  }
  return 0;
4245 4246
}

4247

4248 4249
void sys_var_trust_routine_creators::warn_deprecated(THD *thd)
{
4250
  WARN_DEPRECATED(thd, VER_CELOSIA, "@@log_bin_trust_routine_creators",
4251
                      "'@@log_bin_trust_function_creators'");
4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265
}

void sys_var_trust_routine_creators::set_default(THD *thd, enum_var_type type)
{
  warn_deprecated(thd);
  sys_var_bool_ptr::set_default(thd, type);
}

bool sys_var_trust_routine_creators::update(THD *thd, set_var *var)
{
  warn_deprecated(thd);
  return sys_var_bool_ptr::update(thd, var);
}

unknown's avatar
unknown committed
4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313
bool sys_var_opt_readonly::update(THD *thd, set_var *var)
{
  bool result;

  DBUG_ENTER("sys_var_opt_readonly::update");

  /* Prevent self dead-lock */
  if (thd->locked_tables || thd->active_transaction())
  {
    my_error(ER_LOCK_OR_ACTIVE_TRANSACTION, MYF(0));
    DBUG_RETURN(true);
  }

  if (thd->global_read_lock)
  {
    /*
      This connection already holds the global read lock.
      This can be the case with:
      - FLUSH TABLES WITH READ LOCK
      - SET GLOBAL READ_ONLY = 1
    */
    result= sys_var_bool_ptr::update(thd, var);
    DBUG_RETURN(result);
  }

  /*
    Perform a 'FLUSH TABLES WITH READ LOCK'.
    This is a 3 step process:
    - [1] lock_global_read_lock()
    - [2] close_cached_tables()
    - [3] make_global_read_lock_block_commit()
    [1] prevents new connections from obtaining tables locked for write.
    [2] waits until all existing connections close their tables.
    [3] prevents transactions from being committed.
  */

  if (lock_global_read_lock(thd))
    DBUG_RETURN(true);

  /*
    This call will be blocked by any connection holding a READ or WRITE lock.
    Ideally, we want to wait only for pending WRITE locks, but since:
    con 1> LOCK TABLE T FOR READ;
    con 2> LOCK TABLE T FOR WRITE; (blocked by con 1)
    con 3> SET GLOBAL READ ONLY=1; (blocked by con 2)
    can cause to wait on a read lock, it's required for the client application
    to unlock everything, and acceptable for the server to wait on all locks.
  */
4314
  if ((result= close_cached_tables(thd, NULL, FALSE, TRUE, TRUE)))
unknown's avatar
unknown committed
4315 4316
    goto end_with_read_lock;

4317
  if ((result= make_global_read_lock_block_commit(thd)))
unknown's avatar
unknown committed
4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329
    goto end_with_read_lock;

  /* Change the opt_readonly system variable, safe because the lock is held */
  result= sys_var_bool_ptr::update(thd, var);

end_with_read_lock:
  /* Release the lock */
  unlock_global_read_lock(thd);
  DBUG_RETURN(result);
}


4330
#ifndef DBUG_OFF
unknown's avatar
unknown committed
4331 4332 4333 4334 4335 4336 4337 4338
/* even session variable here requires SUPER, because of -#o,file */
bool sys_var_thd_dbug::check(THD *thd, set_var *var)
{
  return check_global_access(thd, SUPER_ACL);
}

bool sys_var_thd_dbug::update(THD *thd, set_var *var)
{
4339 4340
  char buf[256];
  String str(buf, sizeof(buf), system_charset_info), *res;
Michael Widenius's avatar
Michael Widenius committed
4341 4342
  const char *command;
 
4343
  res= var->value->val_str(&str);
Michael Widenius's avatar
Michael Widenius committed
4344 4345 4346
  command= res ? res->c_ptr(): 0;
  if (!command)
    command= "";
4347

unknown's avatar
unknown committed
4348
  if (var->type == OPT_GLOBAL)
4349
    DBUG_SET_INITIAL(command);
unknown's avatar
unknown committed
4350
  else
4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365
  {
    if (_db_is_pushed_())
    {
      /* We have already a local state done with DBUG_PUSH; Modify the state */
      DBUG_SET(command);
    }
    else
    {
      /*
        We are sharing the state with the global state;
        Create a local state for this thread.
      */
      DBUG_PUSH(command);
    }
  }
unknown's avatar
unknown committed
4366 4367 4368
  return 0;
}

4369

4370
uchar *sys_var_thd_dbug::value_ptr(THD *thd, enum_var_type type, LEX_STRING *b)
unknown's avatar
unknown committed
4371
{
4372
  char buf[1024];
unknown's avatar
unknown committed
4373
  if (type == OPT_GLOBAL)
4374
  {
unknown's avatar
unknown committed
4375
    DBUG_EXPLAIN_INITIAL(buf, sizeof(buf));
4376
  }
unknown's avatar
unknown committed
4377
  else
4378
  {
unknown's avatar
unknown committed
4379
    DBUG_EXPLAIN(buf, sizeof(buf));
4380
  }
4381
  return (uchar*) thd->strdup(buf);
unknown's avatar
unknown committed
4382
}
4383 4384
#endif /* DBUG_OFF */

unknown's avatar
unknown committed
4385

4386
#ifdef HAVE_EVENT_SCHEDULER
4387 4388 4389 4390 4391
bool sys_var_event_scheduler::check(THD *thd, set_var *var)
{
  return check_enum(thd, var, &Events::var_typelib);
}

4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409
/*
   The update method of the global variable event_scheduler.
   If event_scheduler is switched from 0 to 1 then the scheduler main
   thread is resumed and if from 1 to 0 the scheduler thread is suspended

   SYNOPSIS
     sys_var_event_scheduler::update()
       thd  Thread context (unused)
       var  The new value

   Returns
     FALSE  OK
     TRUE   Error
*/

bool
sys_var_event_scheduler::update(THD *thd, set_var *var)
{
unknown's avatar
unknown committed
4410
  int res;
4411 4412
  /* here start the thread if not running. */
  DBUG_ENTER("sys_var_event_scheduler::update");
unknown's avatar
unknown committed
4413
  DBUG_PRINT("info", ("new_value: %d", (int) var->save_result.ulong_value));
4414

4415 4416 4417 4418
  enum Events::enum_opt_event_scheduler
    new_state=
    (enum Events::enum_opt_event_scheduler) var->save_result.ulong_value;

4419
  res= Events::switch_event_scheduler_state(new_state);
4420 4421 4422 4423 4424

  DBUG_RETURN((bool) res);
}


4425
uchar *sys_var_event_scheduler::value_ptr(THD *thd, enum_var_type type,
4426 4427
                                         LEX_STRING *base)
{
4428
  return (uchar *) Events::get_opt_event_scheduler_str();
4429
}
4430
#endif
4431

unknown's avatar
unknown committed
4432 4433 4434 4435
/****************************************************************************
  Used templates
****************************************************************************/

4436
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
unknown's avatar
unknown committed
4437
template class List<set_var_base>;
unknown's avatar
unknown committed
4438
template class List_iterator_fast<set_var_base>;
4439
template class I_List_iterator<NAMED_LIST>;
unknown's avatar
unknown committed
4440
#endif