set_var.cc 126 KB
Newer Older
1
/* Copyright (C) 2000-2003 MySQL AB
2 3 4

   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
5
   the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15

   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 */

16 17 18 19
/**
  @file

  @brief
20 21
  Handling of MySQL SQL variables

22
  @details
23 24 25 26 27
  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.
28 29
  - 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'
30
  - If the variable should be changed from the command line, add a definition
ingo@mysql.com's avatar
ingo@mysql.com committed
31
    of it in the my_option structure list in mysqld.cc
32 33
  - Don't forget to initialize new fields in global_system_variables and
    max_system_variables!
34

35 36 37 38 39 40 41 42 43 44
  @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
45 46 47
    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).
48 49
*/

50
#ifdef USE_PRAGMA_IMPLEMENTATION
51 52 53 54
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
55
#include <mysql.h>
56
#include "slave.h"
57
#include "rpl_mi.h"
58
#include <my_getopt.h>
59
#include <thr_alarm.h>
60
#include <myisam.h>
61
#include <my_dir.h>
62

63
#include "events.h"
64

65
/* WITH_NDBCLUSTER_STORAGE_ENGINE */
66
#ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
67
extern ulong ndb_cache_check_time;
68
extern char opt_ndb_constrbuf[];
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
69
extern ulong ndb_extra_logging;
70 71
#endif

tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
72 73 74 75
#ifdef HAVE_NDB_BINLOG
extern ulong ndb_report_thresh_binlog_epoch_slip;
extern ulong ndb_report_thresh_binlog_mem_usage;
#endif
76

antony@ppcg5.local's avatar
antony@ppcg5.local committed
77
extern CHARSET_INFO *character_set_filesystem;
78 79


80
static HASH system_variable_hash;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
81

82 83 84
const char *bool_type_names[]= { "OFF", "ON", NullS };
TYPELIB bool_typelib=
{
85
  array_elements(bool_type_names)-1, "", bool_type_names, NULL
86 87
};

88 89 90
const char *delay_key_write_type_names[]= { "OFF", "ON", "ALL", NullS };
TYPELIB delay_key_write_typelib=
{
91 92
  array_elements(delay_key_write_type_names)-1, "",
  delay_key_write_type_names, NULL
93 94
};

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

105 106 107
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);
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
108 109 110 111
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);
112 113
static bool set_option_bit(THD *thd, set_var *var);
static bool set_option_autocommit(THD *thd, set_var *var);
114
static int  check_log_update(THD *thd, set_var *var);
115
static bool set_log_update(THD *thd, set_var *var);
116
static int  check_pseudo_thread_id(THD *thd, set_var *var);
117
void fix_binlog_format_after_update(THD *thd, enum_var_type type);
118
static void fix_low_priority_updates(THD *thd, enum_var_type type);
119
static int check_tx_isolation(THD *thd, set_var *var);
120
static void fix_tx_isolation(THD *thd, enum_var_type type);
121 122
static int check_completion_type(THD *thd, set_var *var);
static void fix_completion_type(THD *thd, enum_var_type type);
123 124
static void fix_net_read_timeout(THD *thd, enum_var_type type);
static void fix_net_write_timeout(THD *thd, enum_var_type type);
125
static void fix_net_retry_count(THD *thd, enum_var_type type);
126 127
static void fix_max_join_size(THD *thd, enum_var_type type);
static void fix_query_cache_size(THD *thd, enum_var_type type);
128
static void fix_query_cache_min_res_unit(THD *thd, enum_var_type type);
129
static void fix_myisam_max_sort_file_size(THD *thd, enum_var_type type);
130 131
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);
132
static void fix_max_connections(THD *thd, enum_var_type type);
133
static int check_max_delayed_threads(THD *thd, set_var *var);
134 135
static void fix_thd_mem_root(THD *thd, enum_var_type type);
static void fix_trans_mem_root(THD *thd, enum_var_type type);
136
static void fix_server_id(THD *thd, enum_var_type type);
137
static ulonglong fix_unsigned(THD *, ulonglong, const struct my_option *);
138
static bool get_unsigned(THD *thd, set_var *var);
139 140
bool throw_bounds_warning(THD *thd, bool fixed, bool unsignd,
                          const char *name, longlong val);
141
static KEY_CACHE *create_key_cache(const char *name, uint length);
142
void fix_sql_mode_var(THD *thd, enum_var_type type);
143 144 145
static uchar *get_error_count(THD *thd);
static uchar *get_warning_count(THD *thd);
static uchar *get_tmpdir(THD *thd);
146 147 148 149 150
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);
151 152 153 154 155

/*
  Variable definition list

  These are variables that can be set from the command line, in
156 157 158 159
  alphabetic order.

  The variables are linked into the list. A variable is added to
  it in the constructor (see sys_var class for details).
160 161
*/

antony@ppcg5.local's avatar
antony@ppcg5.local committed
162
static sys_var_chain vars = { NULL, NULL };
163

164 165 166 167 168 169 170 171
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);
172

antony@ppcg5.local's avatar
antony@ppcg5.local committed
173
static sys_var_bool_ptr	sys_automatic_sp_privileges(&vars, "automatic_sp_privileges",
174 175
					      &sp_automatic_privileges);

176 177 178
static sys_var_const            sys_back_log(&vars, "back_log",
                                             OPT_GLOBAL, SHOW_LONG,
                                             (uchar*) &back_log);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
179 180
static sys_var_const_str       sys_basedir(&vars, "basedir", mysql_home);
static sys_var_long_ptr	sys_binlog_cache_size(&vars, "binlog_cache_size",
181
					      &binlog_cache_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
182
static sys_var_thd_binlog_format sys_binlog_format(&vars, "binlog_format",
183
                                            &SV::binlog_format);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
184
static sys_var_thd_ulong	sys_bulk_insert_buff_size(&vars, "bulk_insert_buffer_size",
185
						  &SV::bulk_insert_buff_size);
186 187 188 189 190
static sys_var_const            sys_character_sets_dir(&vars,
                                                       "character_sets_dir",
                                                       OPT_GLOBAL, SHOW_CHAR,
                                                       (uchar*)
                                                       mysql_charsets_dir);
191 192 193 194
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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
195
sys_var_const_str       sys_charset_system(&vars, "character_set_system",
196
                                           (char *)my_charset_utf8_general_ci.name);
197 198 199 200 201 202 203 204 205 206 207 208 209
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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
210 211 212 213 214 215 216
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",
217 218 219
					 &SV::completion_type,
					 check_completion_type,
					 fix_completion_type);
220 221 222 223 224 225 226 227 228 229 230 231
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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
232
static sys_var_long_ptr	sys_concurrent_insert(&vars, "concurrent_insert",
233
                                              &myisam_concurrent_insert);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
234
static sys_var_long_ptr	sys_connect_timeout(&vars, "connect_timeout",
235
					    &connect_timeout);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
236
static sys_var_const_str       sys_datadir(&vars, "datadir", mysql_real_data_home);
serg@serg.mylan's avatar
serg@serg.mylan committed
237
#ifndef DBUG_OFF
antony@ppcg5.local's avatar
antony@ppcg5.local committed
238
static sys_var_thd_dbug        sys_dbug(&vars, "debug");
serg@serg.mylan's avatar
serg@serg.mylan committed
239
#endif
antony@ppcg5.local's avatar
antony@ppcg5.local committed
240
static sys_var_enum		sys_delay_key_write(&vars, "delay_key_write",
241 242 243
					    &delay_key_write_options,
					    &delay_key_write_typelib,
					    fix_delay_key_write);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
244
static sys_var_long_ptr	sys_delayed_insert_limit(&vars, "delayed_insert_limit",
245
						 &delayed_insert_limit);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
246
static sys_var_long_ptr	sys_delayed_insert_timeout(&vars, "delayed_insert_timeout",
247
						   &delayed_insert_timeout);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
248
static sys_var_long_ptr	sys_delayed_queue_size(&vars, "delayed_queue_size",
249
					       &delayed_queue_size);
250

251
#ifdef HAVE_EVENT_SCHEDULER
antony@ppcg5.local's avatar
antony@ppcg5.local committed
252
static sys_var_event_scheduler sys_event_scheduler(&vars, "event_scheduler");
253 254
#endif

antony@ppcg5.local's avatar
antony@ppcg5.local committed
255
static sys_var_long_ptr	sys_expire_logs_days(&vars, "expire_logs_days",
256
					     &expire_logs_days);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
257 258
static sys_var_bool_ptr	sys_flush(&vars, "flush", &myisam_flush);
static sys_var_long_ptr	sys_flush_time(&vars, "flush_time", &flush_time);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
278
sys_var_str             sys_init_connect(&vars, "init_connect", 0,
279 280
                                         sys_update_init_connect,
                                         sys_default_init_connect,0);
281 282 283
static sys_var_const    sys_init_file(&vars, "init_file",
                                      OPT_GLOBAL, SHOW_CHAR_PTR,
                                      (uchar*) &opt_init_file);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
284
sys_var_str             sys_init_slave(&vars, "init_slave", 0,
285 286
                                       sys_update_init_slave,
                                       sys_default_init_slave,0);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
287
static sys_var_thd_ulong	sys_interactive_timeout(&vars, "interactive_timeout",
288
						&SV::net_interactive_timeout);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
289
static sys_var_thd_ulong	sys_join_buffer_size(&vars, "join_buffer_size",
290
					     &SV::join_buff_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
291 292
static sys_var_key_buffer_size	sys_key_buffer_size(&vars, "key_buffer_size");
static sys_var_key_cache_long  sys_key_cache_block_size(&vars, "key_cache_block_size",
293 294
						 offsetof(KEY_CACHE,
							  param_block_size));
antony@ppcg5.local's avatar
antony@ppcg5.local committed
295
static sys_var_key_cache_long	sys_key_cache_division_limit(&vars, "key_cache_division_limit",
296 297
						     offsetof(KEY_CACHE,
							      param_division_limit));
antony@ppcg5.local's avatar
antony@ppcg5.local committed
298
static sys_var_key_cache_long  sys_key_cache_age_threshold(&vars, "key_cache_age_threshold",
299 300
						     offsetof(KEY_CACHE,
							      param_age_threshold));
301 302 303 304 305 306 307 308 309 310 311 312
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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
313
static sys_var_bool_ptr	sys_local_infile(&vars, "local_infile",
314
					 &opt_local_infile);
315 316 317 318 319 320 321 322
#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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
323 324
static sys_var_trust_routine_creators
sys_trust_routine_creators(&vars, "log_bin_trust_routine_creators",
325
                           &trust_function_creators);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
326 327
static sys_var_bool_ptr       
sys_trust_function_creators(&vars, "log_bin_trust_function_creators",
328
                            &trust_function_creators);
329 330 331
static sys_var_const    sys_log_error(&vars, "log_error",
                                      OPT_GLOBAL, SHOW_CHAR,
                                      (uchar*) log_error_file);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
332 333
static sys_var_bool_ptr
  sys_log_queries_not_using_indexes(&vars, "log_queries_not_using_indexes",
334
                                    &opt_log_queries_not_using_indexes);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
335
static sys_var_thd_ulong	sys_log_warnings(&vars, "log_warnings", &SV::log_warnings);
336 337
static sys_var_microseconds	sys_var_long_query_time(&vars, "long_query_time",
                                                        &SV::long_query_time);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
338
static sys_var_thd_bool	sys_low_priority_updates(&vars, "low_priority_updates",
339 340 341
						 &SV::low_priority_updates,
						 fix_low_priority_updates);
#ifndef TO_BE_DELETED	/* Alias for the low_priority_updates */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
342
static sys_var_thd_bool	sys_sql_low_priority_updates(&vars, "sql_low_priority_updates",
343 344 345
						     &SV::low_priority_updates,
						     fix_low_priority_updates);
#endif
346 347 348 349 350 351 352 353 354 355
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);
356
static sys_var_thd_ulong_session_readonly sys_max_allowed_packet(&vars, "max_allowed_packet",
357
					       &SV::max_allowed_packet);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
358
static sys_var_long_ptr	sys_max_binlog_cache_size(&vars, "max_binlog_cache_size",
359
						  &max_binlog_cache_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
360
static sys_var_long_ptr	sys_max_binlog_size(&vars, "max_binlog_size",
361 362
					    &max_binlog_size,
                                            fix_max_binlog_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
363
static sys_var_long_ptr	sys_max_connections(&vars, "max_connections",
364 365
					    &max_connections,
                                            fix_max_connections);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
366
static sys_var_long_ptr	sys_max_connect_errors(&vars, "max_connect_errors",
367
					       &max_connect_errors);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
368
static sys_var_thd_ulong       sys_max_insert_delayed_threads(&vars, "max_insert_delayed_threads",
369 370 371
						       &SV::max_insert_delayed_threads,
                                                       check_max_delayed_threads,
                                                       fix_max_connections);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
372
static sys_var_thd_ulong	sys_max_delayed_threads(&vars, "max_delayed_threads",
373
						&SV::max_insert_delayed_threads,
374 375
                                                check_max_delayed_threads,
                                                fix_max_connections);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
376
static sys_var_thd_ulong	sys_max_error_count(&vars, "max_error_count",
377
					    &SV::max_error_count);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
378
static sys_var_thd_ulonglong	sys_max_heap_table_size(&vars, "max_heap_table_size",
379
						&SV::max_heap_table_size);
380 381 382 383
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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
384
static sys_var_thd_ha_rows	sys_max_join_size(&vars, "max_join_size",
385 386
					  &SV::max_join_size,
					  fix_max_join_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
387
static sys_var_thd_ulong	sys_max_seeks_for_key(&vars, "max_seeks_for_key",
388
					      &SV::max_seeks_for_key);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
389
static sys_var_thd_ulong   sys_max_length_for_sort_data(&vars, "max_length_for_sort_data",
390
                                                 &SV::max_length_for_sort_data);
391
#ifndef TO_BE_DELETED	/* Alias for max_join_size */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
392
static sys_var_thd_ha_rows	sys_sql_max_join_size(&vars, "sql_max_join_size",
393 394 395
					      &SV::max_join_size,
					      fix_max_join_size);
#endif
396
static sys_var_long_ptr_global
antony@ppcg5.local's avatar
antony@ppcg5.local committed
397
sys_max_prepared_stmt_count(&vars, "max_prepared_stmt_count",
398 399
                            &max_prepared_stmt_count,
                            &LOCK_prepared_stmt_count);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
400
static sys_var_long_ptr	sys_max_relay_log_size(&vars, "max_relay_log_size",
401 402
                                               &max_relay_log_size,
                                               fix_max_relay_log_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
403
static sys_var_thd_ulong	sys_max_sort_length(&vars, "max_sort_length",
404
					    &SV::max_sort_length);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
405
static sys_var_thd_ulong	sys_max_sp_recursion_depth(&vars, "max_sp_recursion_depth",
406
                                                   &SV::max_sp_recursion_depth);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
407 408
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",
409
					   &SV::max_tmp_tables);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
410
static sys_var_long_ptr	sys_max_write_lock_count(&vars, "max_write_lock_count",
411
						 &max_write_lock_count);
412 413
static sys_var_thd_ulong       sys_min_examined_row_limit(&vars, "min_examined_row_limit",
                                                          &SV::min_examined_row_limit);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
414
static sys_var_thd_ulong       sys_multi_range_count(&vars, "multi_range_count",
ingo@mysql.com's avatar
ingo@mysql.com committed
415
                                              &SV::multi_range_count);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
416
static sys_var_long_ptr	sys_myisam_data_pointer_size(&vars, "myisam_data_pointer_size",
serg@serg.mylan's avatar
serg@serg.mylan committed
417
                                                    &myisam_data_pointer_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
418
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);
419 420 421 422
static sys_var_const sys_myisam_recover_options(&vars, "myisam_recover_options",
                                                OPT_GLOBAL, SHOW_CHAR_PTR,
                                                (uchar*)
                                                &myisam_recover_options_str);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
423 424
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);
serg@janus.mylan's avatar
serg@janus.mylan committed
425
static sys_var_bool_ptr	sys_myisam_use_mmap(&vars, "myisam_use_mmap",
vtkachenko@quadxeon.mysql.com's avatar
vtkachenko@quadxeon.mysql.com committed
426
                                            &opt_myisam_use_mmap);
427

serg@janus.mylan's avatar
serg@janus.mylan committed
428
static sys_var_thd_enum         sys_myisam_stats_method(&vars, "myisam_stats_method",
429 430 431 432
                                                &SV::myisam_stats_method,
                                                &myisam_stats_method_typelib,
                                                NULL);

433 434 435 436 437 438 439
#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
440
static sys_var_thd_ulong_session_readonly sys_net_buffer_length(&vars, "net_buffer_length",
441
					      &SV::net_buffer_length);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
442
static sys_var_thd_ulong	sys_net_read_timeout(&vars, "net_read_timeout",
443
					     &SV::net_read_timeout,
444
					     0, fix_net_read_timeout);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
445
static sys_var_thd_ulong	sys_net_write_timeout(&vars, "net_write_timeout",
446
					      &SV::net_write_timeout,
447
					      0, fix_net_write_timeout);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
448
static sys_var_thd_ulong	sys_net_retry_count(&vars, "net_retry_count",
449
					    &SV::net_retry_count,
450
					    0, fix_net_retry_count);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
451
static sys_var_thd_bool	sys_new_mode(&vars, "new", &SV::new_mode);
serg@janus.mylan's avatar
serg@janus.mylan committed
452
static sys_var_bool_ptr_readonly sys_old_mode(&vars, "old",
gkodinov/kgeorge@magare.gmz's avatar
gkodinov/kgeorge@magare.gmz committed
453
                                       &global_system_variables.old_mode);
serg@janus.mylan's avatar
serg@janus.mylan committed
454 455 456 457
/* 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);
458 459 460 461
static sys_var_const            sys_open_files_limit(&vars, "open_files_limit",
                                                     OPT_GLOBAL, SHOW_LONG,
                                                     (uchar*)
                                                     &open_files_limit);
serg@janus.mylan's avatar
serg@janus.mylan committed
462
static sys_var_thd_ulong        sys_optimizer_prune_level(&vars, "optimizer_prune_level",
463
                                                  &SV::optimizer_prune_level);
serg@janus.mylan's avatar
serg@janus.mylan committed
464
static sys_var_thd_ulong        sys_optimizer_search_depth(&vars, "optimizer_search_depth",
465
                                                   &SV::optimizer_search_depth);
466 467 468 469 470 471 472 473 474
static sys_var_const            sys_pid_file(&vars, "pid_file",
                                             OPT_GLOBAL, SHOW_CHAR,
                                             (uchar*) pidfile_name);
static sys_var_const            sys_plugin_dir(&vars, "plugin_dir",
                                               OPT_GLOBAL, SHOW_CHAR,
                                               (uchar*) opt_plugin_dir);
static sys_var_const            sys_port(&vars, "port",
                                         OPT_GLOBAL, SHOW_INT,
                                         (uchar*) &mysqld_port);
serg@janus.mylan's avatar
serg@janus.mylan committed
475
static sys_var_thd_ulong        sys_preload_buff_size(&vars, "preload_buffer_size",
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
476
                                              &SV::preload_buff_size);
477 478 479 480
static sys_var_const            sys_protocol_version(&vars, "protocol_version",
                                                     OPT_GLOBAL, SHOW_INT,
                                                     (uchar*)
                                                     &protocol_version);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
481
static sys_var_thd_ulong	sys_read_buff_size(&vars, "read_buffer_size",
482
					   &SV::read_buff_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
483 484
static sys_var_opt_readonly	sys_readonly(&vars, "read_only", &opt_readonly);
static sys_var_thd_ulong	sys_read_rnd_buff_size(&vars, "read_rnd_buffer_size",
485
					       &SV::read_rnd_buff_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
486
static sys_var_thd_ulong	sys_div_precincrement(&vars, "div_precision_increment",
487
                                              &SV::div_precincrement);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
488
static sys_var_long_ptr	sys_rpl_recovery_rank(&vars, "rpl_recovery_rank",
489
					      &rpl_recovery_rank);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
490
static sys_var_long_ptr	sys_query_cache_size(&vars, "query_cache_size",
491 492
					     &query_cache_size,
					     fix_query_cache_size);
493

antony@ppcg5.local's avatar
antony@ppcg5.local committed
494
static sys_var_thd_ulong	sys_range_alloc_block_size(&vars, "range_alloc_block_size",
495
						   &SV::range_alloc_block_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
496
static sys_var_thd_ulong	sys_query_alloc_block_size(&vars, "query_alloc_block_size",
497 498
						   &SV::query_alloc_block_size,
						   0, fix_thd_mem_root);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
499
static sys_var_thd_ulong	sys_query_prealloc_size(&vars, "query_prealloc_size",
500 501
						&SV::query_prealloc_size,
						0, fix_thd_mem_root);
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
#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);
#ifdef HAVE_SYS_UN_H
static sys_var_const    sys_socket(&vars, "socket",
                                   OPT_GLOBAL, SHOW_CHAR_PTR,
                                   (uchar*) &mysqld_unix_port);
#endif
#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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
541 542
static sys_var_readonly        sys_tmpdir(&vars, "tmpdir", OPT_GLOBAL, SHOW_CHAR, get_tmpdir);
static sys_var_thd_ulong	sys_trans_alloc_block_size(&vars, "transaction_alloc_block_size",
543 544
						   &SV::trans_alloc_block_size,
						   0, fix_trans_mem_root);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
545
static sys_var_thd_ulong	sys_trans_prealloc_size(&vars, "transaction_prealloc_size",
546 547
						&SV::trans_prealloc_size,
						0, fix_trans_mem_root);
548
sys_var_enum_const      sys_thread_handling(&vars, "thread_handling",
549 550 551
                                            &SV::thread_handling,
                                            &thread_handling_typelib,
                                            NULL);
552

553
#ifdef HAVE_QUERY_CACHE
antony@ppcg5.local's avatar
antony@ppcg5.local committed
554
static sys_var_long_ptr	sys_query_cache_limit(&vars, "query_cache_limit",
555
					      &query_cache.query_cache_limit);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
556
static sys_var_long_ptr        sys_query_cache_min_res_unit(&vars, "query_cache_min_res_unit",
557 558
						     &query_cache_min_res_unit,
						     fix_query_cache_min_res_unit);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
559
static sys_var_thd_enum	sys_query_cache_type(&vars, "query_cache_type",
560 561
					     &SV::query_cache_type,
					     &query_cache_type_typelib);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
562 563
static sys_var_thd_bool
sys_query_cache_wlock_invalidate(&vars, "query_cache_wlock_invalidate",
564
				 &SV::query_cache_wlock_invalidate);
565
#endif /* HAVE_QUERY_CACHE */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
566
static sys_var_bool_ptr	sys_secure_auth(&vars, "secure_auth", &opt_secure_auth);
567
static sys_var_const_str_ptr sys_secure_file_priv(&vars, "secure_file_priv",
568
                                             &opt_secure_file_priv);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
569 570
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",
571
						      &opt_slave_compressed_protocol);
572 573 574 575 576
static sys_var_set_slave_mode slave_exec_mode(&vars,
                                              "slave_exec_mode",
                                              &slave_exec_mode_options,
                                              &slave_exec_mode_typelib,
                                              0);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
577
static sys_var_long_ptr	sys_slow_launch_time(&vars, "slow_launch_time",
578
					     &slow_launch_time);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
579
static sys_var_thd_ulong	sys_sort_buffer(&vars, "sort_buffer_size",
580
					&SV::sortbuff_size);
581 582 583 584 585 586
/*
  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()).
*/
antony@ppcg5.local's avatar
antony@ppcg5.local committed
587
static sys_var_thd_sql_mode    sys_sql_mode(&vars, "sql_mode",
588
                                            &SV::sql_mode);
589 590 591
#ifdef HAVE_OPENSSL
extern char *opt_ssl_ca, *opt_ssl_capath, *opt_ssl_cert, *opt_ssl_cipher,
            *opt_ssl_key;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
592 593 594 595 596
static sys_var_const_str_ptr	sys_ssl_ca(&vars, "ssl_ca", &opt_ssl_ca);
static sys_var_const_str_ptr	sys_ssl_capath(&vars, "ssl_capath", &opt_ssl_capath);
static sys_var_const_str_ptr	sys_ssl_cert(&vars, "ssl_cert", &opt_ssl_cert);
static sys_var_const_str_ptr	sys_ssl_cipher(&vars, "ssl_cipher", &opt_ssl_cipher);
static sys_var_const_str_ptr	sys_ssl_key(&vars, "ssl_key", &opt_ssl_key);
597
#else
antony@ppcg5.local's avatar
antony@ppcg5.local committed
598 599 600 601 602
static sys_var_const_str	sys_ssl_ca(&vars, "ssl_ca", NULL);
static sys_var_const_str	sys_ssl_capath(&vars, "ssl_capath", NULL);
static sys_var_const_str	sys_ssl_cert(&vars, "ssl_cert", NULL);
static sys_var_const_str	sys_ssl_cipher(&vars, "ssl_cipher", NULL);
static sys_var_const_str	sys_ssl_key(&vars, "ssl_key", NULL);
603
#endif
antony@ppcg5.local's avatar
antony@ppcg5.local committed
604 605
static sys_var_thd_enum
sys_updatable_views_with_limit(&vars, "updatable_views_with_limit",
606 607
                               &SV::updatable_views_with_limit,
                               &updatable_views_with_limit_typelib);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
608

antony@ppcg5.local's avatar
antony@ppcg5.local committed
609 610 611 612 613 614
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",
615
                                             system_time_zone);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
616
static sys_var_long_ptr	sys_table_def_size(&vars, "table_definition_cache",
617
                                           &table_def_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
618
static sys_var_long_ptr	sys_table_cache_size(&vars, "table_open_cache",
619
					     &table_cache_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
620
static sys_var_long_ptr	sys_table_lock_wait_timeout(&vars, "table_lock_wait_timeout",
621
                                                    &table_lock_wait_timeout);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
622
static sys_var_long_ptr	sys_thread_cache_size(&vars, "thread_cache_size",
623
					      &thread_cache_size);
624
#if HAVE_POOL_OF_THREADS == 1
antony@ppcg5.local's avatar
antony@ppcg5.local committed
625
sys_var_long_ptr	sys_thread_pool_size(&vars, "thread_pool_size",
626 627
					      &thread_pool_size);
#endif
antony@ppcg5.local's avatar
antony@ppcg5.local committed
628
static sys_var_thd_enum	sys_tx_isolation(&vars, "tx_isolation",
629 630
					 &SV::tx_isolation,
					 &tx_isolation_typelib,
631 632
					 fix_tx_isolation,
					 check_tx_isolation);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
633
static sys_var_thd_ulonglong	sys_tmp_table_size(&vars, "tmp_table_size",
634
					   &SV::tmp_table_size);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
635
static sys_var_bool_ptr  sys_timed_mutexes(&vars, "timed_mutexes",
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
636
                                    &timed_mutexes);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
637 638
static sys_var_const_str	sys_version(&vars, "version", server_version);
static sys_var_const_str	sys_version_comment(&vars, "version_comment",
639
                                            MYSQL_COMPILATION_COMMENT);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
640
static sys_var_const_str	sys_version_compile_machine(&vars, "version_compile_machine",
641
                                                    MACHINE_TYPE);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
642
static sys_var_const_str	sys_version_compile_os(&vars, "version_compile_os",
643
                                               SYSTEM_TYPE);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
644
static sys_var_thd_ulong	sys_net_wait_timeout(&vars, "wait_timeout",
645
					     &SV::net_wait_timeout);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
646

mskold@mysql.com's avatar
mskold@mysql.com committed
647
/* Condition pushdown to storage engine */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
648 649
static sys_var_thd_bool
sys_engine_condition_pushdown(&vars, "engine_condition_pushdown",
mskold@mysql.com's avatar
mskold@mysql.com committed
650 651
			      &SV::engine_condition_pushdown);

652
#ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
653
/* ndb thread specific variable settings */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
654 655
static sys_var_thd_ulong
sys_ndb_autoincrement_prefetch_sz(&vars, "ndb_autoincrement_prefetch_sz",
656
				  &SV::ndb_autoincrement_prefetch_sz);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
657 658
static sys_var_thd_bool
sys_ndb_force_send(&vars, "ndb_force_send", &SV::ndb_force_send);
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
659
#ifdef HAVE_NDB_BINLOG
antony@ppcg5.local's avatar
antony@ppcg5.local committed
660 661
static sys_var_long_ptr
sys_ndb_report_thresh_binlog_epoch_slip(&vars, "ndb_report_thresh_binlog_epoch_slip",
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
662
                                        &ndb_report_thresh_binlog_epoch_slip);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
663 664
static sys_var_long_ptr
sys_ndb_report_thresh_binlog_mem_usage(&vars, "ndb_report_thresh_binlog_mem_usage",
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
665 666
                                       &ndb_report_thresh_binlog_mem_usage);
#endif
antony@ppcg5.local's avatar
antony@ppcg5.local committed
667 668 669 670 671 672
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);
673 674
static sys_var_const_str
sys_ndb_connectstring(&vars, "ndb_connectstring", opt_ndb_constrbuf);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
675 676
static sys_var_thd_bool
sys_ndb_index_stat_enable(&vars, "ndb_index_stat_enable",
677
                          &SV::ndb_index_stat_enable);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
678 679
static sys_var_thd_ulong
sys_ndb_index_stat_cache_entries(&vars, "ndb_index_stat_cache_entries",
680
                                 &SV::ndb_index_stat_cache_entries);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
681 682
static sys_var_thd_ulong
sys_ndb_index_stat_update_freq(&vars, "ndb_index_stat_update_freq",
683
                               &SV::ndb_index_stat_update_freq);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
684 685 686 687
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);
688
#endif //WITH_NDBCLUSTER_STORAGE_ENGINE
689 690 691

/* Time/date/datetime formats */

antony@ppcg5.local's avatar
antony@ppcg5.local committed
692
static sys_var_thd_date_time_format sys_time_format(&vars, "time_format",
693
					     &SV::time_format,
694
					     MYSQL_TIMESTAMP_TIME);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
695
static sys_var_thd_date_time_format sys_date_format(&vars, "date_format",
696
					     &SV::date_format,
697
					     MYSQL_TIMESTAMP_DATE);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
698
static sys_var_thd_date_time_format sys_datetime_format(&vars, "datetime_format",
699
						 &SV::datetime_format,
700
						 MYSQL_TIMESTAMP_DATETIME);
701 702

/* Variables that are bits in THD */
703

antony@ppcg5.local's avatar
antony@ppcg5.local committed
704
sys_var_thd_bit sys_autocommit(&vars, "autocommit", 0,
705 706 707
                               set_option_autocommit,
                               OPTION_NOT_AUTOCOMMIT,
                               1);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
708
static sys_var_thd_bit	sys_big_tables(&vars, "big_tables", 0,
709 710 711
				       set_option_bit,
				       OPTION_BIG_TABLES);
#ifndef TO_BE_DELETED	/* Alias for big_tables */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
712
static sys_var_thd_bit	sys_sql_big_tables(&vars, "sql_big_tables", 0,
713 714 715
					   set_option_bit,
					   OPTION_BIG_TABLES);
#endif
antony@ppcg5.local's avatar
antony@ppcg5.local committed
716
static sys_var_thd_bit	sys_big_selects(&vars, "sql_big_selects", 0,
717
					set_option_bit,
718
					OPTION_BIG_SELECTS);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
719
static sys_var_thd_bit	sys_log_off(&vars, "sql_log_off",
720
				    check_log_update,
721 722
				    set_option_bit,
				    OPTION_LOG_OFF);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
723
static sys_var_thd_bit	sys_log_update(&vars, "sql_log_update",
724
                                       check_log_update,
725
				       set_log_update,
726
				       OPTION_BIN_LOG);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
727
static sys_var_thd_bit	sys_log_binlog(&vars, "sql_log_bin",
728
                                       check_log_update,
729
				       set_option_bit,
730
				       OPTION_BIN_LOG);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
731
static sys_var_thd_bit	sys_sql_warnings(&vars, "sql_warnings", 0,
732 733
					 set_option_bit,
					 OPTION_WARNINGS);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
734
static sys_var_thd_bit	sys_sql_notes(&vars, "sql_notes", 0,
735
					 set_option_bit,
736
					 OPTION_SQL_NOTES);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
737
static sys_var_thd_bit	sys_auto_is_null(&vars, "sql_auto_is_null", 0,
738
					 set_option_bit,
739 740
                                         OPTION_AUTO_IS_NULL, 0,
                                         sys_var::SESSION_VARIABLE_IN_BINLOG);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
741
static sys_var_thd_bit	sys_safe_updates(&vars, "sql_safe_updates", 0,
742 743
					 set_option_bit,
					 OPTION_SAFE_UPDATES);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
744
static sys_var_thd_bit	sys_buffer_results(&vars, "sql_buffer_result", 0,
745 746
					   set_option_bit,
					   OPTION_BUFFER_RESULT);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
747
static sys_var_thd_bit	sys_quote_show_create(&vars, "sql_quote_show_create", 0,
748 749
					      set_option_bit,
					      OPTION_QUOTE_SHOW_CREATE);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
750
static sys_var_thd_bit	sys_foreign_key_checks(&vars, "foreign_key_checks", 0,
751 752
					       set_option_bit,
					       OPTION_NO_FOREIGN_KEY_CHECKS,
753
                                               1, sys_var::SESSION_VARIABLE_IN_BINLOG);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
754
static sys_var_thd_bit	sys_unique_checks(&vars, "unique_checks", 0,
755 756
					  set_option_bit,
					  OPTION_RELAXED_UNIQUE_CHECKS,
757 758
                                          1,
                                          sys_var::SESSION_VARIABLE_IN_BINLOG);
759
#if defined(ENABLED_PROFILING) && defined(COMMUNITY_SERVER)
760 761
static sys_var_thd_bit  sys_profiling(&vars, "profiling", NULL, 
                                      set_option_bit,
762
                                      ulonglong(OPTION_PROFILING));
763
static sys_var_thd_ulong	sys_profiling_history_size(&vars, "profiling_history_size",
764 765
					      &SV::profiling_history_size);
#endif
766

767 768
/* Local state variables */

antony@ppcg5.local's avatar
antony@ppcg5.local committed
769
static sys_var_thd_ha_rows	sys_select_limit(&vars, "sql_select_limit",
770
						 &SV::select_limit);
771 772 773 774 775 776 777 778 779 780 781
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);
bar@mysql.com's avatar
bar@mysql.com committed
782

783 784
static sys_var_thd_lc_time_names
sys_lc_time_names(&vars, "lc_time_names", sys_var::SESSION_VARIABLE_IN_BINLOG);
bar@mysql.com's avatar
bar@mysql.com committed
785

786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
/*
  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");
antony@ppcg5.local's avatar
antony@ppcg5.local committed
806
static sys_var_readonly		sys_error_count(&vars, "error_count",
807 808 809
						OPT_SESSION,
						SHOW_LONG,
						get_error_count);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
810
static sys_var_readonly		sys_warning_count(&vars, "warning_count",
811 812 813 814
						  OPT_SESSION,
						  SHOW_LONG,
						  get_warning_count);

815 816 817 818
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);
819

antony@ppcg5.local's avatar
antony@ppcg5.local committed
820
static sys_var_thd_ulong        sys_default_week_format(&vars, "default_week_format",
821
					                &SV::default_week_format);
822

antony@ppcg5.local's avatar
antony@ppcg5.local committed
823
sys_var_thd_ulong               sys_group_concat_max_len(&vars, "group_concat_max_len",
824 825
                                                         &SV::group_concat_max_len);

826 827
sys_var_thd_time_zone sys_time_zone(&vars, "time_zone",
                                    sys_var::SESSION_VARIABLE_IN_BINLOG);
828

829 830
/* Global read-only variable containing hostname */
static sys_var_const_str        sys_hostname(&vars, "hostname", glob_hostname);
831 832

#ifndef EMBEDDED_LIBRARY
833 834 835 836 837 838 839 840 841 842
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;
}

df@pippilotta.erinye.com's avatar
df@pippilotta.erinye.com committed
843
static sys_var_readonly    sys_repl_report_port(&vars, "report_port", OPT_GLOBAL, SHOW_LONG, slave_get_report_port);
844

845
#endif
846

847
sys_var_thd_bool  sys_keep_files_on_create(&vars, "keep_files_on_create", 
gkodinov/kgeorge@magare.gmz's avatar
gkodinov/kgeorge@magare.gmz committed
848
                                           &SV::keep_files_on_create);
849 850
/* Read only variables */

antony@ppcg5.local's avatar
antony@ppcg5.local committed
851 852 853 854 855 856 857
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);
serg@janus.mylan's avatar
serg@janus.mylan committed
858 859
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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
860 861
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",
elliot@mysql.com's avatar
elliot@mysql.com committed
862
                                           &have_query_cache);
863
static sys_var_have_variable sys_have_community_features(&vars, "have_community_features", &have_community_features);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
864 865
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);
866
/* Global read-only variable describing server license */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
867
static sys_var_const_str	sys_license(&vars, "license", STRINGIFY_ARG(LICENSE));
868
/* Global variables which enable|disable logging */
antony@ppcg5.local's avatar
antony@ppcg5.local committed
869
static sys_var_log_state sys_var_general_log(&vars, "general_log", &opt_log,
870
                                      QUERY_LOG_GENERAL);
871 872 873
/* 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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
874
static sys_var_log_state sys_var_slow_query_log(&vars, "slow_query_log", &opt_slow_log,
875
                                         QUERY_LOG_SLOW);
876 877 878
/* 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);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
879
sys_var_str sys_var_general_log_path(&vars, "general_log_file", sys_check_log_path,
880 881 882
				     sys_update_general_log_path,
				     sys_default_general_log_path,
				     opt_logname);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
883
sys_var_str sys_var_slow_log_path(&vars, "slow_query_log_file", sys_check_log_path,
884 885 886
				  sys_update_slow_log_path, 
				  sys_default_slow_log_path,
				  opt_slow_logname);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
887
static sys_var_log_output sys_var_log_output_state(&vars, "log_output", &log_output_options,
888 889
					    &log_output_typelib, 0);

890

891 892 893 894 895 896
bool sys_var::check(THD *thd, set_var *var)
{
  var->save_result.ulonglong_value= var->value->val_int();
  return 0;
}

897 898 899 900 901 902 903
bool sys_var_str::check(THD *thd, set_var *var)
{
  int res;
  if (!check_func)
    return 0;

  if ((res=(*check_func)(thd, var)) < 0)
904 905
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0),
             name, var->value->str_value.ptr());
906 907 908
  return res;
}

909 910 911 912
/*
  Functions to check and update variables
*/

gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
913

914 915 916 917 918 919 920 921 922 923
/*
  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)
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
924
{
925 926 927 928
  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*) "";
929
  if (!(res= my_strndup(old_value, new_length, MYF(0))))
930
    return 1;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
931 932 933 934
  /*
    Replace the old value in such a way that the any thread using
    the value will work.
  */
935 936 937 938 939
  rw_wrlock(var_mutex);
  old_value= var_str->value;
  var_str->value= res;
  var_str->value_length= new_length;
  rw_unlock(var_mutex);
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
940 941 942 943 944
  my_free(old_value, MYF(MY_ALLOW_ZERO_PTR));
  return 0;
}


945 946 947 948 949 950
static bool sys_update_init_connect(THD *thd, set_var *var)
{
  return update_sys_var_str(&sys_init_connect, &LOCK_sys_init_connect, var);
}


gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
951 952
static void sys_default_init_connect(THD* thd, enum_var_type type)
{
953
  update_sys_var_str(&sys_init_connect, &LOCK_sys_init_connect, 0);
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
954 955 956 957 958
}


static bool sys_update_init_slave(THD *thd, set_var *var)
{
959
  return update_sys_var_str(&sys_init_slave, &LOCK_sys_init_slave, var);
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
960 961 962 963 964
}


static void sys_default_init_slave(THD* thd, enum_var_type type)
{
965
  update_sys_var_str(&sys_init_slave, &LOCK_sys_init_slave, 0);
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
966 967
}

968 969
static int sys_check_ftb_syntax(THD *thd,  set_var *var)
{
970
  if (thd->security_ctx->master_access & SUPER_ACL)
971
    return (ft_boolean_check_syntax_string((uchar*)
972 973
                                           var->value->str_value.c_ptr()) ?
            -1 : 0);
974 975 976 977 978 979 980 981 982 983 984
  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);
985 986 987 988 989

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

990 991 992 993 994
  return 0;
}

static void sys_default_ftb_syntax(THD *thd, enum_var_type type)
{
995
  strmake(ft_boolean_syntax, def_ft_boolean_syntax,
996 997
	  sizeof(ft_boolean_syntax)-1);
}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
998

999

1000
/**
1001
  If one sets the LOW_PRIORIY UPDATES flag, we also must change the
1002
  used lock type.
1003 1004 1005 1006
*/

static void fix_low_priority_updates(THD *thd, enum_var_type type)
{
1007 1008 1009 1010 1011
  if (type == OPT_GLOBAL)
    thr_upgraded_concurrent_insert_lock= 
      (global_system_variables.low_priority_updates ?
       TL_WRITE_LOW_PRIORITY : TL_WRITE);
  else
1012 1013 1014 1015 1016
    thd->update_lock_default= (thd->variables.low_priority_updates ?
			       TL_WRITE_LOW_PRIORITY : TL_WRITE);
}


1017 1018 1019 1020 1021 1022 1023
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;
}

1024 1025
/**
  Set the OPTION_BIG_SELECTS flag if max_join_size == HA_POS_ERROR.
1026 1027 1028 1029 1030 1031
*/

static void fix_max_join_size(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
  {
1032
    if (thd->variables.max_join_size == HA_POS_ERROR)
1033 1034 1035 1036 1037
      thd->options|= OPTION_BIG_SELECTS;
    else
      thd->options&= ~OPTION_BIG_SELECTS;
  }
}
1038

1039

1040
/**
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
  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;
}

1054 1055
/*
  If one doesn't use the SESSION modifier, the isolation level
1056
  is only active for the next command.
1057 1058 1059 1060 1061 1062 1063 1064
*/
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);
}

1065 1066
static void fix_completion_type(THD *thd __attribute__((unused)),
				enum_var_type type __attribute__((unused))) {}
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079

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

1080 1081 1082 1083 1084

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

hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1085
#ifdef HAVE_REPLICATION
1086 1087 1088
static void fix_net_read_timeout(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
1089
    my_net_set_read_timeout(&thd->net, thd->variables.net_read_timeout);
1090 1091 1092 1093 1094 1095
}


static void fix_net_write_timeout(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
1096
    my_net_set_write_timeout(&thd->net, thd->variables.net_write_timeout);
1097 1098
}

1099 1100 1101 1102 1103
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;
}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1104
#else /* HAVE_REPLICATION */
1105 1106
static void fix_net_read_timeout(THD *thd __attribute__((unused)),
				 enum_var_type type __attribute__((unused)))
1107
{}
1108 1109
static void fix_net_write_timeout(THD *thd __attribute__((unused)),
				  enum_var_type type __attribute__((unused)))
1110
{}
1111 1112
static void fix_net_retry_count(THD *thd __attribute__((unused)),
				enum_var_type type __attribute__((unused)))
1113
{}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1114
#endif /* HAVE_REPLICATION */
1115

1116 1117 1118 1119

static void fix_query_cache_size(THD *thd, enum_var_type type)
{
#ifdef HAVE_QUERY_CACHE
1120 1121 1122 1123 1124 1125 1126 1127
  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)
1128 1129
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
			ER_WARN_QC_RESIZE, ER(ER_WARN_QC_RESIZE),
1130 1131 1132
			query_cache_size, new_cache_size);
  
  query_cache_size= new_cache_size;
1133 1134 1135 1136
#endif
}


1137 1138 1139 1140 1141 1142 1143 1144 1145
#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


1146
extern void fix_delay_key_write(THD *thd, enum_var_type type)
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
{
  switch ((enum_delay_key_write) delay_key_write_options) {
  case DELAY_KEY_WRITE_NONE:
    myisam_delay_key_write=0;
    break;
  case DELAY_KEY_WRITE_ON:
    myisam_delay_key_write=1;
    break;
  case DELAY_KEY_WRITE_ALL:
    myisam_delay_key_write=1;
    ha_open_options|= HA_OPEN_DELAY_KEY_WRITE;
    break;
  }
}

1162 1163 1164 1165
bool sys_var_set::update(THD *thd, set_var *var)
{
  *value= var->save_result.ulong_value;
  return 0;
1166
}
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234

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)
{
  slave_exec_mode_options= 0;
  bit_do_set(slave_exec_mode_options, SLAVE_EXEC_MODE_STRICT);
}

bool sys_var_set_slave_mode::check(THD *thd, set_var *var)
{
  bool rc=  sys_var_set::check(thd, var);
  if (!rc &&
      bit_is_set(var->save_result.ulong_value, SLAVE_EXEC_MODE_STRICT) == 1 &&
      bit_is_set(var->save_result.ulong_value, SLAVE_EXEC_MODE_IDEMPOTENT) == 1)
  {
    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;
}

void fix_slave_exec_mode(enum_var_type type)
{
  DBUG_ENTER("fix_slave_exec_mode");
  compile_time_assert(sizeof(slave_exec_mode_options) * CHAR_BIT
                      > SLAVE_EXEC_MODE_LAST_BIT - 1);
  if (bit_is_set(slave_exec_mode_options, SLAVE_EXEC_MODE_STRICT) == 1 &&
      bit_is_set(slave_exec_mode_options, SLAVE_EXEC_MODE_IDEMPOTENT) == 1)
  {
    sql_print_error("Ambiguous slave modes combination."
                    " STRICT will be used");
    bit_do_clear(slave_exec_mode_options, SLAVE_EXEC_MODE_IDEMPOTENT);
  }
  if (bit_is_set(slave_exec_mode_options, SLAVE_EXEC_MODE_IDEMPOTENT) == 0)
    bit_do_set(slave_exec_mode_options, SLAVE_EXEC_MODE_STRICT);
}
1235

Mats Kindahl's avatar
Mats Kindahl committed
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250

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


1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
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).
1264 1265 1266

    If we don't have row-based replication compiled in, the variable
    is always read-only.
1267 1268 1269 1270 1271 1272 1273 1274
  */
  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;
  }
  /*
1275
    if in a stored function/trigger, it's too late to change mode
1276
  */
1277
  if (thd->in_sub_stmt)
1278 1279 1280 1281 1282 1283 1284
  {
    my_error(ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_FORMAT, MYF(0));
    return 1;    
  }
  return sys_var_thd_enum::is_readonly();
}

lars@mysql.com's avatar
lars@mysql.com committed
1285

1286 1287 1288 1289 1290 1291
void fix_binlog_format_after_update(THD *thd, enum_var_type type)
{
  thd->reset_current_stmt_binlog_row_based();
}


1292
static void fix_max_binlog_size(THD *thd, enum_var_type type)
1293 1294 1295 1296 1297
{
  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);
1298
#ifdef HAVE_REPLICATION
1299 1300
  if (!max_relay_log_size)
    active_mi->rli.relay_log.set_max_size(max_binlog_size);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1301
#endif
1302 1303 1304
  DBUG_VOID_RETURN;
}

1305
static void fix_max_relay_log_size(THD *thd, enum_var_type type)
1306 1307 1308 1309
{
  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));
1310
#ifdef HAVE_REPLICATION
1311 1312
  active_mi->rli.relay_log.set_max_size(max_relay_log_size ?
                                        max_relay_log_size: max_binlog_size);
1313
#endif
1314 1315
  DBUG_VOID_RETURN;
}
1316

1317

1318 1319
static int check_max_delayed_threads(THD *thd, set_var *var)
{
monty@mishka.local's avatar
monty@mishka.local committed
1320
  longlong val= var->value->val_int();
1321
  if (var->type != OPT_GLOBAL && val != 0 &&
monty@mishka.local's avatar
monty@mishka.local committed
1322
      val != (longlong) global_system_variables.max_insert_delayed_threads)
1323 1324
  {
    char buf[64];
1325
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->var->name, llstr(val, buf));
1326 1327 1328 1329 1330
    return 1;
  }
  return 0;
}

1331
static void fix_max_connections(THD *thd, enum_var_type type)
1332
{
1333
#ifndef EMBEDDED_LIBRARY
1334 1335
  resize_thr_alarm(max_connections + 
		   global_system_variables.max_insert_delayed_threads + 10);
1336
#endif
1337 1338 1339 1340 1341 1342
}


static void fix_thd_mem_root(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
1343
    reset_root_defaults(thd->mem_root,
1344 1345 1346 1347 1348 1349 1350
                        thd->variables.query_alloc_block_size,
                        thd->variables.query_prealloc_size);
}


static void fix_trans_mem_root(THD *thd, enum_var_type type)
{
1351
#ifdef USING_TRANSACTIONS
1352 1353 1354 1355
  if (type != OPT_GLOBAL)
    reset_root_defaults(&thd->transaction.mem_root,
                        thd->variables.trans_alloc_block_size,
                        thd->variables.trans_prealloc_size);
1356
#endif
1357 1358
}

1359

1360 1361 1362
static void fix_server_id(THD *thd, enum_var_type type)
{
  server_id_supplied = 1;
1363
  thd->server_id= server_id;
1364
}
1365

1366

1367 1368
bool throw_bounds_warning(THD *thd, bool fixed, bool unsignd,
                          const char *name, longlong val)
1369
{
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
  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;
1390 1391 1392 1393 1394
}

static ulonglong fix_unsigned(THD *thd, ulonglong num,
                              const struct my_option *option_limits)
{
1395
  my_bool fixed= FALSE;
1396 1397
  ulonglong out= getopt_ull_limit_value(num, option_limits, &fixed);

1398
  throw_bounds_warning(thd, fixed, TRUE, option_limits->name, (longlong) num);
1399 1400 1401
  return out;
}

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
static bool get_unsigned(THD *thd, set_var *var)
{
  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);
  }
  return 0;
}

1414

1415
sys_var_long_ptr::
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1416
sys_var_long_ptr(sys_var_chain *chain, const char *name_arg, ulong *value_ptr_arg,
1417
                 sys_after_update_func after_update_arg)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
1418
  :sys_var_long_ptr_global(chain, name_arg, value_ptr_arg,
1419 1420 1421 1422 1423
                           &LOCK_global_system_variables, after_update_arg)
{}


bool sys_var_long_ptr_global::check(THD *thd, set_var *var)
svoj@mysql.com's avatar
svoj@mysql.com committed
1424
{
1425
  return get_unsigned(thd, var);
svoj@mysql.com's avatar
svoj@mysql.com committed
1426
}
1427

1428
bool sys_var_long_ptr_global::update(THD *thd, set_var *var)
1429
{
1430
  ulonglong tmp= var->save_result.ulonglong_value;
1431
  pthread_mutex_lock(guard);
1432
  if (option_limits)
1433
    *value= (ulong) fix_unsigned(thd, tmp, option_limits);
1434
  else
1435 1436 1437
  {
#if SIZEOF_LONG < SIZEOF_LONG_LONG
    /* Avoid overflows on 32 bit systems */
1438
    if (tmp > ULONG_MAX)
1439
    {
1440
      tmp= ULONG_MAX;
1441 1442
      throw_bounds_warning(thd, TRUE, TRUE, name,
                           (longlong) var->save_result.ulonglong_value);
1443 1444
    }
#endif
1445
    *value= (ulong) tmp;
1446 1447
  }

1448
  pthread_mutex_unlock(guard);
1449 1450 1451 1452
  return 0;
}


1453
void sys_var_long_ptr_global::set_default(THD *thd, enum_var_type type)
1454
{
1455
  my_bool not_used;
1456
  pthread_mutex_lock(guard);
1457 1458
  *value= (ulong) getopt_ull_limit_value((ulong) option_limits->def_value,
                                         option_limits, &not_used);
1459
  pthread_mutex_unlock(guard);
1460 1461 1462
}


1463 1464
bool sys_var_ulonglong_ptr::update(THD *thd, set_var *var)
{
1465
  ulonglong tmp= var->save_result.ulonglong_value;
1466
  pthread_mutex_lock(&LOCK_global_system_variables);
1467
  if (option_limits)
1468
    *value= (ulonglong) fix_unsigned(thd, tmp, option_limits);
1469 1470
  else
    *value= (ulonglong) tmp;
1471
  pthread_mutex_unlock(&LOCK_global_system_variables);
1472 1473 1474 1475 1476 1477
  return 0;
}


void sys_var_ulonglong_ptr::set_default(THD *thd, enum_var_type type)
{
1478
  my_bool not_used;
1479
  pthread_mutex_lock(&LOCK_global_system_variables);
1480 1481
  *value= getopt_ull_limit_value((ulonglong) option_limits->def_value,
                                 option_limits, &not_used);
1482
  pthread_mutex_unlock(&LOCK_global_system_variables);
1483 1484 1485
}


1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
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;
}


1499 1500 1501 1502 1503 1504 1505
bool sys_var_enum::update(THD *thd, set_var *var)
{
  *value= (uint) var->save_result.ulong_value;
  return 0;
}


1506
uchar *sys_var_enum::value_ptr(THD *thd, enum_var_type type, LEX_STRING *base)
1507
{
1508
  return (uchar*) enum_names->type_names[*value];
1509 1510
}

1511 1512 1513 1514 1515 1516 1517

uchar *sys_var_enum_const::value_ptr(THD *thd, enum_var_type type,
                                     LEX_STRING *base)
{
  return (uchar*) enum_names->type_names[global_system_variables.*offset];
}

1518 1519
bool sys_var_thd_ulong::check(THD *thd, set_var *var)
{
1520
  return (get_unsigned(thd, var) ||
1521 1522
          (check_func && (*check_func)(thd, var)));
}
1523

1524 1525
bool sys_var_thd_ulong::update(THD *thd, set_var *var)
{
1526
  ulonglong tmp= var->save_result.ulonglong_value;
1527 1528 1529

  /* Don't use bigger value than given with --maximum-variable-name=.. */
  if ((ulong) tmp > max_system_variables.*offset)
1530
  {
1531
    throw_bounds_warning(thd, TRUE, TRUE, name, (longlong) tmp);
1532
    tmp= max_system_variables.*offset;
1533
  }
1534

1535 1536 1537
  if (option_limits)
    tmp= (ulong) fix_unsigned(thd, tmp, option_limits);
#if SIZEOF_LONG < SIZEOF_LONG_LONG
1538
  else if (tmp > ULONG_MAX)
1539
  {
1540
    tmp= ULONG_MAX;
1541
    throw_bounds_warning(thd, TRUE, TRUE, name, (longlong) var->save_result.ulonglong_value);
1542
  }
1543 1544
#endif

1545
  if (var->type == OPT_GLOBAL)
1546
    global_system_variables.*offset= (ulong) tmp;
1547 1548
  else
    thd->variables.*offset= (ulong) tmp;
1549

1550 1551 1552 1553 1554 1555 1556 1557
  return 0;
}


void sys_var_thd_ulong::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
  {
1558
    my_bool not_used;
1559
    /* We will not come here if option_limits is not set */
1560
    global_system_variables.*offset=
serg@janus.mylan's avatar
serg@janus.mylan committed
1561 1562
      (ulong) getopt_ull_limit_value((ulong) option_limits->def_value,
                                     option_limits, &not_used);
1563 1564 1565 1566 1567 1568
  }
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1569
uchar *sys_var_thd_ulong::value_ptr(THD *thd, enum_var_type type,
1570
				   LEX_STRING *base)
1571 1572
{
  if (type == OPT_GLOBAL)
1573 1574
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
1575 1576 1577
}


1578 1579
bool sys_var_thd_ha_rows::update(THD *thd, set_var *var)
{
1580
  ulonglong tmp= var->save_result.ulonglong_value;
1581 1582 1583 1584 1585 1586

  /* Don't use bigger value than given with --maximum-variable-name=.. */
  if ((ha_rows) tmp > max_system_variables.*offset)
    tmp= max_system_variables.*offset;

  if (option_limits)
1587
    tmp= (ha_rows) fix_unsigned(thd, tmp, option_limits);
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
  if (var->type == OPT_GLOBAL)
  {
    /* Lock is needed to make things safe on 32 bit systems */
    pthread_mutex_lock(&LOCK_global_system_variables);    
    global_system_variables.*offset= (ha_rows) tmp;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.*offset= (ha_rows) tmp;
  return 0;
}


void sys_var_thd_ha_rows::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
  {
1605
    my_bool not_used;
1606 1607
    /* We will not come here if option_limits is not set */
    pthread_mutex_lock(&LOCK_global_system_variables);
1608 1609 1610
    global_system_variables.*offset=
      (ha_rows) getopt_ull_limit_value((ha_rows) option_limits->def_value,
                                       option_limits, &not_used);
1611 1612 1613 1614 1615 1616 1617
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1618
uchar *sys_var_thd_ha_rows::value_ptr(THD *thd, enum_var_type type,
1619
				     LEX_STRING *base)
1620 1621
{
  if (type == OPT_GLOBAL)
1622 1623
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
1624 1625
}

1626 1627 1628 1629 1630
bool sys_var_thd_ulonglong::check(THD *thd, set_var *var)
{
  return get_unsigned(thd, var);
}

1631 1632
bool sys_var_thd_ulonglong::update(THD *thd,  set_var *var)
{
1633
  ulonglong tmp= var->save_result.ulonglong_value;
1634

1635
  if (tmp > max_system_variables.*offset)
1636 1637 1638
    tmp= max_system_variables.*offset;

  if (option_limits)
1639
    tmp= fix_unsigned(thd, tmp, option_limits);
1640
  if (var->type == OPT_GLOBAL)
1641 1642 1643
  {
    /* Lock is needed to make things safe on 32 bit systems */
    pthread_mutex_lock(&LOCK_global_system_variables);
1644
    global_system_variables.*offset= (ulonglong) tmp;
1645 1646
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
1647
  else
1648
    thd->variables.*offset= (ulonglong) tmp;
1649 1650 1651 1652 1653 1654 1655
  return 0;
}


void sys_var_thd_ulonglong::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
1656
  {
1657
    my_bool not_used;
1658
    pthread_mutex_lock(&LOCK_global_system_variables);
1659 1660 1661
    global_system_variables.*offset=
      getopt_ull_limit_value((ulonglong) option_limits->def_value,
                             option_limits, &not_used);
1662 1663
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
1664 1665 1666 1667 1668
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1669
uchar *sys_var_thd_ulonglong::value_ptr(THD *thd, enum_var_type type,
1670
				       LEX_STRING *base)
1671 1672
{
  if (type == OPT_GLOBAL)
1673 1674
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
}


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


1697
uchar *sys_var_thd_bool::value_ptr(THD *thd, enum_var_type type,
1698
				  LEX_STRING *base)
1699 1700
{
  if (type == OPT_GLOBAL)
1701 1702
    return (uchar*) &(global_system_variables.*offset);
  return (uchar*) &(thd->variables.*offset);
1703 1704 1705
}


1706
bool sys_var::check_enum(THD *thd, set_var *var, const TYPELIB *enum_names)
1707
{
1708
  char buff[STRING_BUFFER_USUAL_SIZE];
1709
  const char *value;
1710
  String str(buff, sizeof(buff), system_charset_info), *res;
1711 1712 1713 1714 1715

  if (var->value->result_type() == STRING_RESULT)
  {
    if (!(res=var->value->val_str(&str)) ||
	((long) (var->save_result.ulong_value=
1716 1717
		 (ulong) find_type(enum_names, res->ptr(),
				   res->length(),1)-1)) < 0)
1718
    {
1719
      value= res ? res->c_ptr() : "NULL";
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
      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:
1737
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, value);
1738 1739 1740
  return 1;
}

1741 1742 1743

bool sys_var::check_set(THD *thd, set_var *var, TYPELIB *enum_names)
{
1744
  bool not_used;
1745
  char buff[STRING_BUFFER_USUAL_SIZE], *error= 0;
1746 1747 1748 1749 1750 1751
  uint error_len= 0;
  String str(buff, sizeof(buff), system_charset_info), *res;

  if (var->value->result_type() == STRING_RESULT)
  {
    if (!(res= var->value->val_str(&str)))
1752
    {
1753
      strmov(buff, "NULL");
1754
      goto err;
1755
    }
1756 1757 1758 1759 1760 1761 1762 1763

    if (!m_allow_empty_value &&
        res->length() == 0)
    {
      buff[0]= 0;
      goto err;
    }

monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1764 1765
    var->save_result.ulong_value= ((ulong)
				   find_set(enum_names, res->c_ptr(),
1766 1767 1768
					    res->length(),
                                            NULL,
                                            &error, &error_len,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1769
					    &not_used));
1770 1771
    if (error_len)
    {
1772
      strmake(buff, error, min(sizeof(buff) - 1, error_len));
1773 1774 1775 1776 1777 1778
      goto err;
    }
  }
  else
  {
    ulonglong tmp= var->value->val_int();
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791

    if (!m_allow_empty_value &&
        tmp == 0)
    {
      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.
    */
1792 1793
    if (unlikely((tmp >= ((ULL(1)) << enum_names->count)) &&
                 (enum_names->count < 64)))
1794 1795 1796 1797 1798 1799 1800 1801 1802
    {
      llstr(tmp, buff);
      goto err;
    }
    var->save_result.ulong_value= (ulong) tmp;  // Save for update
  }
  return 0;

err:
1803
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, buff);
1804 1805 1806 1807
  return 1;
}


1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
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;
}


1827
uchar *sys_var_thd_enum::value_ptr(THD *thd, enum_var_type type,
1828
				  LEX_STRING *base)
1829 1830 1831 1832
{
  ulong tmp= ((type == OPT_GLOBAL) ?
	      global_system_variables.*offset :
	      thd->variables.*offset);
1833
  return (uchar*) enum_names->type_names[tmp];
1834 1835
}

1836 1837 1838 1839 1840
bool sys_var_thd_bit::check(THD *thd, set_var *var)
{
  return (check_enum(thd, var, &bool_typelib) ||
          (check_func && (*check_func)(thd, var)));
}
1841 1842 1843

bool sys_var_thd_bit::update(THD *thd, set_var *var)
{
1844
  int res= (*update_func)(thd, var);
1845 1846 1847 1848
  return res;
}


1849
uchar *sys_var_thd_bit::value_ptr(THD *thd, enum_var_type type,
1850
				 LEX_STRING *base)
1851 1852 1853 1854 1855 1856 1857
{
  /*
    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);
1858
  return (uchar*) &thd->sys_var_tmp.my_bool_value;
1859 1860 1861
}


1862
/** Update a date_time format variable based on given value. */
1863 1864 1865 1866 1867 1868

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");
1869
  DBUG_DUMP("positions", (uchar*) new_value->positions,
1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
	    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)
{
1904
  char buff[STRING_BUFFER_USUAL_SIZE];
1905 1906 1907 1908 1909 1910 1911 1912 1913
  String str(buff,sizeof(buff), system_charset_info), *res;
  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())))
  {
1914
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, res->c_ptr());
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
    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);
}


1949
uchar *sys_var_thd_date_time_format::value_ptr(THD *thd, enum_var_type type,
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
					      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);
1962
    return (uchar*) res;
1963
  }
1964
  return (uchar*) (thd->variables.*offset)->format.str;
1965 1966 1967
}


1968 1969
typedef struct old_names_map_st
{
1970 1971 1972
  const char *old_name;
  const char *new_name;
} my_old_conv;
1973

1974 1975
static my_old_conv old_conv[]= 
{
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
  {	"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		}
1987
};
1988

1989
CHARSET_INFO *get_old_charset_by_name(const char *name)
1990
{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1991
  my_old_conv *conv;
1992
 
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1993
  for (conv= old_conv; conv->old_name; conv++)
1994
  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1995 1996
    if (!my_strcasecmp(&my_charset_latin1, name, conv->old_name))
      return get_charset_by_csname(conv->new_name, MY_CS_PRIMARY, MYF(0));
1997
  }
1998
  return NULL;
1999 2000
}

monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2001

2002
bool sys_var_collation::check(THD *thd, set_var *var)
2003 2004
{
  CHARSET_INFO *tmp;
2005
  LINT_INIT(tmp);
2006

2007
  if (var->value->result_type() == STRING_RESULT)
2008
  {
2009
    char buff[STRING_BUFFER_USUAL_SIZE];
2010 2011 2012
    String str(buff,sizeof(buff), system_charset_info), *res;
    if (!(res=var->value->val_str(&str)))
    {
2013
      my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
2014 2015 2016 2017
      return 1;
    }
    if (!(tmp=get_charset_by_name(res->c_ptr(),MYF(0))))
    {
2018
      my_error(ER_UNKNOWN_COLLATION, MYF(0), res->c_ptr());
2019 2020
      return 1;
    }
2021
  }
2022
  else // INT_RESULT
2023
  {
2024
    if (!(tmp=get_charset((int) var->value->val_int(),MYF(0))))
2025 2026
    {
      char buf[20];
2027
      int10_to_str((int) var->value->val_int(), buf, -10);
2028
      my_error(ER_UNKNOWN_COLLATION, MYF(0), buf);
2029 2030
      return 1;
    }
2031 2032 2033 2034 2035
  }
  var->save_result.charset= tmp;	// Save for update
  return 0;
}

2036

2037 2038 2039
bool sys_var_character_set::check(THD *thd, set_var *var)
{
  CHARSET_INFO *tmp;
2040
  LINT_INIT(tmp);
2041

2042 2043
  if (var->value->result_type() == STRING_RESULT)
  {
2044
    char buff[STRING_BUFFER_USUAL_SIZE];
2045 2046
    String str(buff,sizeof(buff), system_charset_info), *res;
    if (!(res=var->value->val_str(&str)))
2047
    {
2048 2049
      if (!nullable)
      {
2050
        my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
2051 2052 2053 2054 2055 2056 2057
        return 1;
      }
      tmp= NULL;
    }
    else if (!(tmp=get_charset_by_csname(res->c_ptr(),MY_CS_PRIMARY,MYF(0))) &&
             !(tmp=get_old_charset_by_name(res->c_ptr())))
    {
2058
      my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), res->c_ptr());
2059 2060 2061
      return 1;
    }
  }
2062
  else // INT_RESULT
2063
  {
2064
    if (!(tmp=get_charset((int) var->value->val_int(),MYF(0))))
2065 2066
    {
      char buf[20];
2067
      int10_to_str((int) var->value->val_int(), buf, -10);
2068 2069 2070
      my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), buf);
      return 1;
    }
2071 2072 2073 2074 2075
  }
  var->save_result.charset= tmp;	// Save for update
  return 0;
}

2076

2077
bool sys_var_character_set::update(THD *thd, set_var *var)
2078
{
2079
  ci_ptr(thd,var->type)[0]= var->save_result.charset;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2080
  thd->update_charset();
2081 2082 2083
  return 0;
}

2084

2085
uchar *sys_var_character_set::value_ptr(THD *thd, enum_var_type type,
2086
				       LEX_STRING *base)
2087
{
2088
  CHARSET_INFO *cs= ci_ptr(thd,type)[0];
2089
  return cs ? (uchar*) cs->csname : (uchar*) NULL;
2090 2091 2092
}


antony@ppcg5.local's avatar
antony@ppcg5.local committed
2093
void sys_var_character_set_sv::set_default(THD *thd, enum_var_type type)
2094
{
2095
  if (type == OPT_GLOBAL)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2096
    global_system_variables.*offset= *global_default;
2097
  else
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2098 2099 2100 2101
  {
    thd->variables.*offset= global_system_variables.*offset;
    thd->update_charset();
  }
2102
}
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2103
CHARSET_INFO **sys_var_character_set_sv::ci_ptr(THD *thd, enum_var_type type)
2104 2105
{
  if (type == OPT_GLOBAL)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2106
    return &(global_system_variables.*offset);
2107
  else
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2108
    return &(thd->variables.*offset);
2109 2110
}

2111

2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
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;
}


2127 2128
CHARSET_INFO ** sys_var_character_set_database::ci_ptr(THD *thd,
						       enum_var_type type)
2129 2130
{
  if (type == OPT_GLOBAL)
2131
    return &global_system_variables.collation_database;
2132
  else
2133
    return &thd->variables.collation_database;
2134 2135
}

2136

2137 2138 2139
void sys_var_character_set_database::set_default(THD *thd, enum_var_type type)
{
 if (type == OPT_GLOBAL)
2140
    global_system_variables.collation_database= default_charset_info;
2141
  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2142
  {
2143
    thd->variables.collation_database= thd->db_charset;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2144 2145
    thd->update_charset();
  }
2146 2147
}

2148

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2149
bool sys_var_collation_sv::update(THD *thd, set_var *var)
2150 2151
{
  if (var->type == OPT_GLOBAL)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2152
    global_system_variables.*offset= var->save_result.charset;
2153
  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2154
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2155
    thd->variables.*offset= var->save_result.charset;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2156 2157
    thd->update_charset();
  }
2158 2159 2160
  return 0;
}

2161

antony@ppcg5.local's avatar
antony@ppcg5.local committed
2162
void sys_var_collation_sv::set_default(THD *thd, enum_var_type type)
2163
{
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2164 2165
  if (type == OPT_GLOBAL)
    global_system_variables.*offset= *global_default;
2166 2167
  else
  {
antony@ppcg5.local's avatar
antony@ppcg5.local committed
2168
    thd->variables.*offset= global_system_variables.*offset;
2169 2170 2171 2172 2173
    thd->update_charset();
  }
}


2174
uchar *sys_var_collation_sv::value_ptr(THD *thd, enum_var_type type,
2175
                                       LEX_STRING *base)
2176 2177
{
  CHARSET_INFO *cs= ((type == OPT_GLOBAL) ?
2178
		     global_system_variables.*offset : thd->variables.*offset);
2179
  return cs ? (uchar*) cs->name : (uchar*) "NULL";
2180 2181
}

2182

2183
LEX_STRING default_key_cache_base= {(char *) "default", 7 };
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2184

2185
static KEY_CACHE zero_key_cache;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2186

2187
KEY_CACHE *get_key_cache(LEX_STRING *cache_name)
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2188
{
2189 2190
  safe_mutex_assert_owner(&LOCK_global_system_variables);
  if (!cache_name || ! cache_name->length)
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2191
    cache_name= &default_key_cache_base;
2192
  return ((KEY_CACHE*) find_named(&key_caches,
2193
                                      cache_name->str, cache_name->length, 0));
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2194 2195
}

2196

2197
uchar *sys_var_key_cache_param::value_ptr(THD *thd, enum_var_type type,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2198
					 LEX_STRING *base)
2199
{
2200
  KEY_CACHE *key_cache= get_key_cache(base);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2201 2202
  if (!key_cache)
    key_cache= &zero_key_cache;
2203
  return (uchar*) key_cache + offset ;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2204
}
2205

2206

2207 2208
bool sys_var_key_buffer_size::update(THD *thd, set_var *var)
{
2209
  ulonglong tmp= var->save_result.ulonglong_value;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2210
  LEX_STRING *base_name= &var->base;
2211
  KEY_CACHE *key_cache;
2212 2213 2214
  bool error= 0;

  /* If no basename, assume it's for the key cache named 'default' */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2215
  if (!base_name->length)
2216
    base_name= &default_key_cache_base;
2217 2218 2219

  pthread_mutex_lock(&LOCK_global_system_variables);
  key_cache= get_key_cache(base_name);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2220
                            
2221 2222
  if (!key_cache)
  {
2223
    /* Key cache didn't exists */
2224
    if (!tmp)					// Tried to delete cache
2225 2226 2227 2228 2229 2230
      goto end;					// Ok, nothing to do
    if (!(key_cache= create_key_cache(base_name->str, base_name->length)))
    {
      error= 1;
      goto end;
    }
2231
  }
2232 2233 2234 2235 2236 2237 2238 2239 2240

  /*
    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;

monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2241
  if (!tmp)					// Zero size means delete
2242
  {
timour@mysql.com's avatar
timour@mysql.com committed
2243
    if (key_cache == dflt_key_cache)
2244 2245 2246 2247
    {
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                          ER_WARN_CANT_DROP_DEFAULT_KEYCACHE,
                          ER(ER_WARN_CANT_DROP_DEFAULT_KEYCACHE));
2248
      goto end;					// Ignore default key cache
2249
    }
2250

2251
    if (key_cache->key_cache_inited)		// If initied
2252 2253
    {
      /*
2254 2255
	Move tables using this key cache to the default key cache
	and clear the old key cache.
2256
      */
2257
      NAMED_LIST *list; 
2258
      key_cache= (KEY_CACHE *) find_named(&key_caches, base_name->str,
2259
					      base_name->length, &list);
2260 2261
      key_cache->in_init= 1;
      pthread_mutex_unlock(&LOCK_global_system_variables);
timour@mysql.com's avatar
timour@mysql.com committed
2262
      error= reassign_keycache_tables(thd, key_cache, dflt_key_cache);
2263 2264
      pthread_mutex_lock(&LOCK_global_system_variables);
      key_cache->in_init= 0;
2265
    }
2266 2267 2268 2269 2270
    /*
      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;
2271 2272
  }

2273
  key_cache->param_buff_size=
2274
    (ulonglong) fix_unsigned(thd, tmp, option_limits);
2275

2276 2277 2278 2279
  /* If key cache didn't existed initialize it, else resize it */
  key_cache->in_init= 1;
  pthread_mutex_unlock(&LOCK_global_system_variables);

2280
  if (!key_cache->key_cache_inited)
2281
    error= (bool) (ha_init_key_cache("", key_cache));
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2282
  else
2283
    error= (bool)(ha_resize_key_cache(key_cache));
2284

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

2288 2289 2290
end:
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return error;
2291 2292
}

2293

2294 2295 2296 2297 2298 2299
/**
  @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
*/
2300
bool sys_var_key_cache_long::update(THD *thd, set_var *var)
2301
{
monty@mysql.com's avatar
monty@mysql.com committed
2302
  ulong tmp= (ulong) var->value->val_int();
2303
  LEX_STRING *base_name= &var->base;
2304 2305
  bool error= 0;

2306
  if (!base_name->length)
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2307
    base_name= &default_key_cache_base;
2308 2309

  pthread_mutex_lock(&LOCK_global_system_variables);
2310
  KEY_CACHE *key_cache= get_key_cache(base_name);
2311

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2312 2313
  if (!key_cache && !(key_cache= create_key_cache(base_name->str,
				                  base_name->length)))
2314 2315 2316 2317
  {
    error= 1;
    goto end;
  }
2318

2319 2320 2321 2322 2323 2324 2325 2326 2327
  /*
    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;

  *((ulong*) (((char*) key_cache) + offset))=
2328
    (ulong) fix_unsigned(thd, tmp, option_limits);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2329

2330 2331 2332 2333 2334
  /*
    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;
2335

2336
  pthread_mutex_unlock(&LOCK_global_system_variables);
2337

2338 2339 2340 2341 2342 2343 2344 2345
  error= (bool) (ha_resize_key_cache(key_cache));

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

end:
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return error;
2346 2347
}

2348

2349 2350
bool sys_var_log_state::update(THD *thd, set_var *var)
{
2351
  bool res;
2352 2353 2354 2355 2356 2357

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

2358 2359 2360
  pthread_mutex_lock(&LOCK_global_system_variables);
  if (!var->save_result.ulong_value)
  {
2361 2362
    logger.deactivate_log_handler(thd, log_type);
    res= false;
2363
  }
2364 2365
  else
    res= logger.activate_log_handler(thd, log_type);
2366 2367 2368 2369 2370 2371
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return res;
}

void sys_var_log_state::set_default(THD *thd, enum_var_type type)
{
2372 2373 2374 2375 2376
  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'");

2377 2378 2379 2380 2381 2382 2383 2384
  pthread_mutex_lock(&LOCK_global_system_variables);
  logger.deactivate_log_handler(thd, log_type);
  pthread_mutex_unlock(&LOCK_global_system_variables);
}


static int  sys_check_log_path(THD *thd,  set_var *var)
{
2385
  char path[FN_REFLEN], buff[FN_REFLEN];
2386
  MY_STAT f_stat;
2387 2388
  String str(buff, sizeof(buff), system_charset_info), *res;
  const char *log_file_str;
2389 2390
  size_t path_length;

2391 2392 2393 2394
  if (!(res= var->value->val_str(&str)))
    goto err;

  log_file_str= res->c_ptr();
2395 2396
  bzero(&f_stat, sizeof(MY_STAT));

2397 2398 2399
  path_length= unpack_filename(path, log_file_str);

  if (!path_length)
2400
  {
2401 2402 2403
    /* File name is empty. */

    goto err;
2404
  }
2405 2406

  if (my_stat(path, &f_stat, MYF(0)))
2407 2408
  {
    /*
2409 2410
      A file system object exists. Check if argument is a file and we have
      'write' permission.
2411
    */
2412 2413 2414

    if (!MY_S_ISREG(f_stat.st_mode) ||
        !(f_stat.st_mode & MY_S_IWRITE))
2415
      goto err;
2416 2417

    return 0;
2418
  }
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433

  /* 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;

2434
  return 0;
2435 2436 2437 2438 2439

err:
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->var->name, 
           res ? log_file_str : "NULL");
  return 1;
2440 2441 2442 2443 2444 2445 2446
}


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)
{
2447
  MYSQL_QUERY_LOG *file_log;
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460
  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:
2461
    assert(0);                                  // Impossible
2462 2463 2464 2465 2466 2467 2468
  }

  if (!old_value)
  {
    old_value= make_default_log_name(buff, log_ext);
    str_length= strlen(old_value);
  }
2469
  if (!(res= my_strndup(old_value, str_length, MYF(MY_FAE+MY_WME))))
2470 2471 2472 2473 2474 2475
  {
    result= 1;
    goto err;
  }

  pthread_mutex_lock(&LOCK_global_system_variables);
2476
  logger.lock_exclusive();
2477 2478 2479 2480 2481 2482 2483 2484

  if (file_log && log_state)
    file_log->close(0);
  old_value= var_str->value;
  var_str->value= res;
  var_str->value_length= str_length;
  my_free(old_value, MYF(MY_ALLOW_ZERO_PTR));
  if (file_log && log_state)
2485 2486 2487
  {
    switch (log_type) {
    case QUERY_LOG_SLOW:
2488
      file_log->open_slow_log(sys_var_slow_log_path.value);
2489 2490 2491 2492 2493 2494 2495 2496
      break;
    case QUERY_LOG_GENERAL:
      file_log->open_query_log(sys_var_general_log_path.value);
      break;
    default:
      DBUG_ASSERT(0);
    }
  }
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538

  logger.unlock();
  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)
{
  pthread_mutex_lock(&LOCK_global_system_variables);
2539
  logger.lock_exclusive();
2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551
  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();
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return 0;
}


void sys_var_log_output::set_default(THD *thd, enum_var_type type)
{
  pthread_mutex_lock(&LOCK_global_system_variables);
2552 2553 2554 2555
  logger.lock_exclusive();
  logger.init_slow_log(LOG_FILE);
  logger.init_general_log(LOG_FILE);
  *value= LOG_FILE;
2556 2557 2558 2559 2560
  logger.unlock();
  pthread_mutex_unlock(&LOCK_global_system_variables);
}


2561
uchar *sys_var_log_output::value_ptr(THD *thd, enum_var_type type,
monty@mysql.com's avatar
monty@mysql.com committed
2562
                                    LEX_STRING *base)
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
{
  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--;
2582
  return (uchar*) thd->strmake(tmp.ptr(), length);
2583 2584 2585
}


2586 2587 2588 2589
/*****************************************************************************
  Functions to handle SET NAMES and SET CHARACTER SET
*****************************************************************************/

2590
int set_var_collation_client::check(THD *thd)
2591
{
2592 2593 2594 2595 2596 2597 2598
  /* 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;
  }
2599 2600 2601
  return 0;
}

2602
int set_var_collation_client::update(THD *thd)
2603
{
2604 2605
  thd->variables.character_set_client= character_set_client;
  thd->variables.character_set_results= character_set_results;
2606
  thd->variables.collation_connection= collation_connection;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2607
  thd->update_charset();
2608 2609
  thd->protocol_text.init(thd);
  thd->protocol_binary.init(thd);
2610 2611 2612 2613 2614
  return 0;
}

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

2615 2616
bool sys_var_timestamp::update(THD *thd,  set_var *var)
{
2617
  thd->set_time((time_t) var->save_result.ulonglong_value);
2618 2619 2620 2621 2622 2623 2624 2625 2626 2627
  return 0;
}


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


2628
uchar *sys_var_timestamp::value_ptr(THD *thd, enum_var_type type,
2629
				   LEX_STRING *base)
2630 2631
{
  thd->sys_var_tmp.long_value= (long) thd->start_time;
2632
  return (uchar*) &thd->sys_var_tmp.long_value;
2633 2634 2635 2636 2637
}


bool sys_var_last_insert_id::update(THD *thd, set_var *var)
{
2638 2639
  thd->first_successful_insert_id_in_prev_stmt= 
    var->save_result.ulonglong_value;
2640 2641 2642 2643
  return 0;
}


2644
uchar *sys_var_last_insert_id::value_ptr(THD *thd, enum_var_type type,
2645
					LEX_STRING *base)
2646
{
2647 2648 2649 2650 2651 2652
  /*
    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();
2653
  return (uchar*) &thd->sys_var_tmp.ulonglong_value;
2654 2655 2656 2657 2658
}


bool sys_var_insert_id::update(THD *thd, set_var *var)
{
2659
  thd->force_one_auto_inc_interval(var->save_result.ulonglong_value);
2660 2661 2662 2663
  return 0;
}


2664
uchar *sys_var_insert_id::value_ptr(THD *thd, enum_var_type type,
2665
				   LEX_STRING *base)
2666
{
2667 2668
  thd->sys_var_tmp.ulonglong_value= 
    thd->auto_inc_intervals_forced.minimum();
2669
  return (uchar*) &thd->sys_var_tmp.ulonglong_value;
2670
}
2671 2672


2673 2674
bool sys_var_rand_seed1::update(THD *thd, set_var *var)
{
2675
  thd->rand.seed1= (ulong) var->save_result.ulonglong_value;
2676 2677 2678 2679 2680
  return 0;
}

bool sys_var_rand_seed2::update(THD *thd, set_var *var)
{
2681
  thd->rand.seed2= (ulong) var->save_result.ulonglong_value;
2682 2683 2684 2685
  return 0;
}


2686 2687
bool sys_var_thd_time_zone::check(THD *thd, set_var *var)
{
2688
  char buff[MAX_TIME_ZONE_NAME_LENGTH];
2689 2690 2691
  String str(buff, sizeof(buff), &my_charset_latin1);
  String *res= var->value->val_str(&str);

2692
  if (!(var->save_result.time_zone= my_tz_find(thd, res)))
2693
  {
2694
    my_error(ER_UNKNOWN_TIME_ZONE, MYF(0), res ? res->c_ptr() : "NULL");
2695 2696 2697 2698 2699 2700 2701 2702
    return 1;
  }
  return 0;
}


bool sys_var_thd_time_zone::update(THD *thd, set_var *var)
{
2703 2704 2705 2706 2707 2708 2709 2710 2711
  /* 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;
2712 2713 2714 2715
  return 0;
}


2716
uchar *sys_var_thd_time_zone::value_ptr(THD *thd, enum_var_type type,
2717 2718 2719 2720 2721 2722 2723
				       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)
2724
    return (uchar *)(global_system_variables.time_zone->get_name()->ptr());
2725
  else
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
  {
    /*
      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;
2736
    return (uchar *)(thd->variables.time_zone->get_name()->ptr());
2737
  }
2738 2739 2740 2741 2742
}


void sys_var_thd_time_zone::set_default(THD *thd, enum_var_type type)
{
2743
 pthread_mutex_lock(&LOCK_global_system_variables);
2744 2745 2746 2747 2748
 if (type == OPT_GLOBAL)
 {
   if (default_tz_name)
   {
     String str(default_tz_name, &my_charset_latin1);
2749 2750 2751 2752
     /*
       We are guaranteed to find this time zone since its existence
       is checked during start-up.
     */
2753
     global_system_variables.time_zone= my_tz_find(thd, &str);
2754 2755 2756 2757 2758 2759
   }
   else
     global_system_variables.time_zone= my_tz_SYSTEM;
 }
 else
   thd->variables.time_zone= global_system_variables.time_zone;
2760
 pthread_mutex_unlock(&LOCK_global_system_variables);
2761 2762
}

2763 2764 2765 2766 2767 2768 2769 2770 2771

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.
2772
      May be we should have a separate error message for this?
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
    */
    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);
2783
  max_user_connections= (uint)var->save_result.ulonglong_value;
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
  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);
}


2798
uchar *sys_var_max_user_conn::value_ptr(THD *thd, enum_var_type type,
2799 2800 2801 2802
                                       LEX_STRING *base)
{
  if (type != OPT_GLOBAL &&
      thd->user_connect && thd->user_connect->user_resources.user_conn)
2803 2804
    return (uchar*) &(thd->user_connect->user_resources.user_conn);
  return (uchar*) &(max_user_connections);
2805 2806
}

2807

2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819
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);
}


bar@mysql.com's avatar
bar@mysql.com committed
2820 2821
bool sys_var_thd_lc_time_names::check(THD *thd, set_var *var)
{
2822
  MY_LOCALE *locale_match;
bar@mysql.com's avatar
bar@mysql.com committed
2823

2824
  if (var->value->result_type() == INT_RESULT)
bar@mysql.com's avatar
bar@mysql.com committed
2825
  {
2826 2827 2828 2829 2830 2831 2832
    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;
    }
bar@mysql.com's avatar
bar@mysql.com committed
2833
  }
2834
  else // STRING_RESULT
bar@mysql.com's avatar
bar@mysql.com committed
2835
  {
2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
    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;
    }
    const char *locale_str= res->c_ptr();
    if (!(locale_match= my_locale_by_name(locale_str)))
    {
      my_printf_error(ER_UNKNOWN_ERROR,
                      "Unknown locale: '%s'", MYF(0), locale_str);
      return 1;
    }
bar@mysql.com's avatar
bar@mysql.com committed
2850
  }
2851

2852 2853
  var->save_result.locale_value= locale_match;
  return 0;
bar@mysql.com's avatar
bar@mysql.com committed
2854 2855 2856 2857 2858
}


bool sys_var_thd_lc_time_names::update(THD *thd, set_var *var)
{
2859 2860 2861 2862
  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;
bar@mysql.com's avatar
bar@mysql.com committed
2863 2864 2865 2866
  return 0;
}


2867
uchar *sys_var_thd_lc_time_names::value_ptr(THD *thd, enum_var_type type,
bar@mysql.com's avatar
bar@mysql.com committed
2868 2869
					  LEX_STRING *base)
{
2870
  return type == OPT_GLOBAL ?
2871 2872
                 (uchar *) global_system_variables.lc_time_names->name :
                 (uchar *) thd->variables.lc_time_names->name;
bar@mysql.com's avatar
bar@mysql.com committed
2873 2874 2875 2876 2877
}


void sys_var_thd_lc_time_names::set_default(THD *thd, enum_var_type type)
{
2878 2879 2880 2881
  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;
bar@mysql.com's avatar
bar@mysql.com committed
2882
}
2883

2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937
/*
  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;
}


2938 2939 2940 2941 2942 2943
/*
  Functions to update thd->options bits
*/

static bool set_option_bit(THD *thd, set_var *var)
{
2944 2945 2946
  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;
2947
  else
2948
    thd->options|= sys_var->bit_flag;
2949 2950 2951 2952 2953 2954 2955 2956
  return 0;
}


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

2957
  ulonglong org_options= thd->options;
2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968

  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 */
2969
      thd->options&= ~(ulonglong) (OPTION_BEGIN | OPTION_KEEP_LOG);
2970
      thd->transaction.all.modified_non_trans_table= FALSE;
2971 2972 2973 2974 2975 2976
      thd->server_status|= SERVER_STATUS_AUTOCOMMIT;
      if (ha_commit(thd))
	return 1;
    }
    else
    {
2977
      thd->transaction.all.modified_non_trans_table= FALSE;
2978 2979 2980 2981 2982 2983
      thd->server_status&= ~SERVER_STATUS_AUTOCOMMIT;
    }
  }
  return 0;
}

2984 2985 2986
static int check_log_update(THD *thd, set_var *var)
{
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2987
  if (!(thd->security_ctx->master_access & SUPER_ACL))
2988 2989 2990 2991 2992 2993 2994
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
#endif
  return 0;
}
2995 2996 2997

static bool set_log_update(THD *thd, set_var *var)
{
2998 2999 3000 3001 3002
  /*
    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
  */
3003

3004
  if (opt_sql_bin_update)
3005 3006 3007 3008 3009 3010 3011 3012 3013
  {
    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));
3014 3015 3016 3017 3018
  set_option_bit(thd, var);
  return 0;
}


3019 3020 3021 3022
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
3023
  if (thd->security_ctx->master_access & SUPER_ACL)
3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
    return 0;
  else
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
#else
  return 0;
#endif
}
3034

3035
static uchar *get_warning_count(THD *thd)
3036 3037 3038
{
  thd->sys_var_tmp.long_value=
    (thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_NOTE] +
3039
     thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_ERROR] +
3040
     thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_WARN]);
3041
  return (uchar*) &thd->sys_var_tmp.long_value;
3042 3043
}

3044
static uchar *get_error_count(THD *thd)
3045 3046 3047
{
  thd->sys_var_tmp.long_value= 
    thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_ERROR];
3048
  return (uchar*) &thd->sys_var_tmp.long_value;
3049 3050
}

3051

3052 3053
/**
  Get the tmpdir that was specified or chosen by default.
3054

3055 3056 3057 3058 3059
  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.
3060

3061
  @param thd		thread handle
3062

3063
  @retval
3064
    ptr		pointer to NUL-terminated string
3065
*/
3066
static uchar *get_tmpdir(THD *thd)
3067 3068
{
  if (opt_mysql_tmpdir)
3069 3070
    return (uchar *)opt_mysql_tmpdir;
  return (uchar*)mysql_tmpdir;
3071 3072
}

3073 3074 3075 3076 3077 3078 3079
/****************************************************************************
  Main handling of variables:
  - Initialisation
  - Searching during parsing
  - Update loop
****************************************************************************/

3080 3081 3082
/**
  Find variable name in option my_getopt structure used for
  command line args.
3083

3084 3085
  @param opt	option structure array to search in
  @param name	variable name
3086

3087
  @retval
3088
    0		Error
3089
  @retval
3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111
    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;
}


3112 3113
/**
  Return variable name and length for hashing of variables.
3114 3115
*/

3116 3117
static uchar *get_sys_var_length(const sys_var *var, size_t *length,
                                 my_bool first)
3118 3119
{
  *length= var->name_length;
3120
  return (uchar*) var->name;
3121 3122 3123 3124
}


/*
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3125 3126 3127 3128 3129 3130 3131 3132 3133 3134
  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
3135 3136 3137
*/


antony@ppcg5.local's avatar
antony@ppcg5.local committed
3138
int mysql_add_sys_var_chain(sys_var *first, struct my_option *long_options)
3139
{
3140
  sys_var *var;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3141 3142 3143 3144
  
  /* A write lock should be held on LOCK_system_variables_hash */
  
  for (var= first; var; var= var->next)
3145
  {
3146
    var->name_length= strlen(var->name);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3147
    /* this fails if there is a conflicting variable name. see HASH_UNIQUE */
3148
    if (my_hash_insert(&system_variable_hash, (uchar*) var))
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3149 3150 3151
      goto error;
    if (long_options)
      var->option_limits= find_option(long_options, var->name);
3152
  }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3153 3154 3155 3156
  return 0;

error:
  for (; first != var; first= first->next)
3157
    hash_delete(&system_variable_hash, (uchar*) first);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3158 3159
  return 1;
}
3160 3161
 
 
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3162 3163
/*
  Remove variables to the dynamic hash of system variables
3164
   
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3165 3166 3167
  SYNOPSIS
    mysql_del_sys_var_chain()
    first       Pointer to first system variable to remove
3168
   
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3169 3170 3171 3172
  RETURN VALUES
    0           SUCCESS
    otherwise   FAILURE
*/
3173
 
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3174 3175 3176
int mysql_del_sys_var_chain(sys_var *first)
{
  int result= 0;
3177
 
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3178
  /* A write lock should be held on LOCK_system_variables_hash */
3179
   
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3180
  for (sys_var *var= first; var; var= var->next)
3181
    result|= hash_delete(&system_variable_hash, (uchar*) var);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3182 3183 3184

  return result;
}
3185 3186
 
 
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3187 3188 3189 3190
static int show_cmp(SHOW_VAR *a, SHOW_VAR *b)
{
  return strcmp(a->name, b->name);
}
3191 3192
 
 
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
/*
  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;
3209
  int size= sizeof(SHOW_VAR) * (count + 1);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3210 3211 3212 3213
  SHOW_VAR *result= (SHOW_VAR*) thd->alloc(size);

  if (result)
  {
3214
    SHOW_VAR *show= result;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226

    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)
3227
      my_qsort(result, count, sizeof(SHOW_VAR),
3228
               (qsort_cmp) show_cmp);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3229 3230 3231
    
    /* make last element empty */
    bzero(show, sizeof(SHOW_VAR));
3232
  }
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262
  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");
  
  for (sys_var *var=vars.first; var; var= var->next, count++);

  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;

3263 3264 3265 3266 3267 3268 3269
  /*
    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;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3270 3271 3272 3273 3274 3275

  DBUG_RETURN(0);

error:
  fprintf(stderr, "failed to initialize system variables");
  DBUG_RETURN(1);
3276 3277 3278 3279 3280 3281 3282 3283 3284
}


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


3285 3286
/**
  Find a user set-table variable.
3287

3288 3289 3290 3291
  @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.
3292

3293
  @retval
3294
    pointer	pointer to variable definitions
3295
  @retval
3296 3297 3298
    0		Unknown variable (error message is given)
*/

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3299
sys_var *intern_find_sys_var(const char *str, uint length, bool no_error)
3300
{
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3301 3302 3303 3304 3305 3306 3307
  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,
3308
			      (uchar*) str, length ? length : strlen(str));
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3309
  if (!(var || no_error))
3310
    my_error(ER_UNKNOWN_SYSTEM_VARIABLE, MYF(0), (char*) str);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3311

3312 3313 3314 3315
  return var;
}


3316 3317
/**
  Execute update of all variables.
3318

3319 3320
  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.
3321

3322 3323
  This should ensure that in all normal cases none all or variables are
  updated.
3324

3325 3326
  @param THD		Thread id
  @param var_list       List of variables to update
3327

3328
  @retval
3329
    0	ok
3330
  @retval
3331
    1	ERROR, message sent (normally no variables was updated)
3332
  @retval
3333
    -1  ERROR, message not sent
3334 3335
*/

3336
int sql_set_variables(THD *thd, List<set_var_base> *var_list)
3337
{
3338
  int error;
3339
  List_iterator_fast<set_var_base> it(*var_list);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3340
  DBUG_ENTER("sql_set_variables");
3341 3342 3343 3344

  set_var_base *var;
  while ((var=it++))
  {
3345
    if ((error= var->check(thd)))
3346
      goto err;
3347
  }
3348
  if (!(error= test(thd->is_error())))
3349 3350 3351 3352 3353
  {
    it.rewind();
    while ((var= it++))
      error|= var->update(thd);         // Returns 0, -1 or 1
  }
3354

3355 3356
err:
  free_underlaid_joins(thd, &thd->lex->select_lex);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3357
  DBUG_RETURN(error);
3358 3359 3360
}


3361 3362 3363 3364
/**
  Say if all variables set by a SET support the ONE_SHOT keyword
  (currently, only character set and collation do; later timezones
  will).
3365

3366
  @param var_list	List of variables to update
3367

3368
  @note
3369 3370
    It has a "not_" because it makes faster tests (no need to "!")

3371
  @retval
3372
    0	all variables of the list support ONE_SHOT
3373
  @retval
3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
    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;
}


3390 3391 3392 3393
/*****************************************************************************
  Functions to handle SET mysql_internal_variable=const_expr
*****************************************************************************/

3394
int set_var::check(THD *thd)
3395
{
3396 3397 3398 3399 3400
  if (var->is_readonly())
  {
    my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0), var->name, "read only");
    return -1;
  }
3401 3402
  if (var->check_type(type))
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3403
    int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
3404
    my_error(err, MYF(0), var->name);
3405
    return -1;
3406 3407 3408 3409 3410 3411 3412 3413
  }
  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))
    {
3414
      my_error(ER_NO_DEFAULT, MYF(0), var->name);
3415
      return -1;
3416 3417 3418 3419
    }
    return 0;
  }

3420
  if ((!value->fixed &&
3421
       value->fix_fields(thd, &value)) || value->check_cols(1))
3422
    return -1;
3423 3424
  if (var->check_update_type(value->result_type()))
  {
3425
    my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var->name);
3426
    return -1;
3427
  }
3428
  return var->check(thd, this) ? -1 : 0;
3429 3430 3431
}


3432 3433
/**
  Check variable, but without assigning value (used by PS).
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3434

3435
  @param thd		thread handler
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3436

3437
  @retval
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3438
    0	ok
3439
  @retval
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3440
    1	ERROR, message sent (normally no variables was updated)
3441 3442
  @retval
    -1   ERROR, message not sent
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3443
*/
3444 3445 3446 3447
int set_var::light_check(THD *thd)
{
  if (var->check_type(type))
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3448
    int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
3449
    my_error(err, MYF(0), var->name);
3450 3451
    return -1;
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3452
  if (type == OPT_GLOBAL && check_global_access(thd, SUPER_ACL))
3453 3454
    return 1;

3455
  if (value && ((!value->fixed && value->fix_fields(thd, &value)) ||
3456
                value->check_cols(1)))
3457 3458 3459 3460
    return -1;
  return 0;
}

3461 3462
/**
  Update variable
3463

3464 3465 3466 3467 3468 3469 3470 3471 3472
  @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.
*/
3473
int set_var::update(THD *thd)
3474 3475 3476 3477
{
  if (!value)
    var->set_default(thd, type);
  else if (var->update(thd, this))
3478
    return -1;				// should never happen
3479 3480 3481 3482 3483 3484 3485 3486 3487 3488
  if (var->after_update)
    (*var->after_update)(thd, type);
  return 0;
}


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

3489
int set_var_user::check(THD *thd)
3490
{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3491 3492
  /*
    Item_func_set_user_var can't substitute something else on its place =>
3493
    0 can be passed as last argument (reference on item)
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3494
  */
3495
  return (user_var_item->fix_fields(thd, (Item**) 0) ||
3496
	  user_var_item->check(0)) ? -1 : 0;
3497 3498 3499
}


3500 3501
/**
  Check variable, but without assigning value (used by PS).
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3502

3503
  @param thd		thread handler
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3504

3505
  @retval
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3506
    0	ok
3507
  @retval
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3508
    1	ERROR, message sent (normally no variables was updated)
3509 3510
  @retval
    -1   ERROR, message not sent
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3511
*/
3512 3513 3514 3515 3516 3517
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)
  */
3518
  return (user_var_item->fix_fields(thd, (Item**) 0));
3519 3520 3521
}


3522
int set_var_user::update(THD *thd)
3523 3524 3525 3526
{
  if (user_var_item->update())
  {
    /* Give an error if it's not given already */
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3527
    my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY), MYF(0));
3528
    return -1;
3529 3530 3531 3532 3533 3534 3535 3536 3537
  }
  return 0;
}


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

3538
int set_var_password::check(THD *thd)
3539
{
hf@deer.(none)'s avatar
hf@deer.(none) committed
3540
#ifndef NO_EMBEDDED_ACCESS_CHECKS
3541
  if (!user->host.str)
3542
  {
3543
    if (*thd->security_ctx->priv_host != 0)
3544
    {
3545 3546
      user->host.str= (char *) thd->security_ctx->priv_host;
      user->host.length= strlen(thd->security_ctx->priv_host);
3547 3548 3549 3550 3551 3552 3553
    }
    else
    {
      user->host.str= (char *)"%";
      user->host.length= 1;
    }
  }
3554
  /* Returns 1 as the function sends error to client */
3555 3556
  return check_change_password(thd, user->host.str, user->user.str,
                               password, strlen(password)) ? 1 : 0;
3557
#else
hf@deer.(none)'s avatar
hf@deer.(none) committed
3558 3559
  return 0;
#endif
3560 3561
}

3562
int set_var_password::update(THD *thd)
3563
{
hf@deer.(none)'s avatar
hf@deer.(none) committed
3564
#ifndef NO_EMBEDDED_ACCESS_CHECKS
3565
  /* Returns 1 as the function sends error to client */
3566 3567
  return change_password(thd, user->host.str, user->user.str, password) ?
	  1 : 0;
hf@deer.(none)'s avatar
hf@deer.(none) committed
3568 3569 3570
#else
  return 0;
#endif
3571 3572
}

3573 3574 3575 3576
/****************************************************************************
 Functions to handle table_type
****************************************************************************/

3577 3578
/* Based upon sys_var::check_enum() */

3579
bool sys_var_thd_storage_engine::check(THD *thd, set_var *var)
3580
{
3581
  char buff[STRING_BUFFER_USUAL_SIZE];
3582 3583 3584
  const char *value;
  String str(buff, sizeof(buff), &my_charset_latin1), *res;

antony@ppcg5.local's avatar
antony@ppcg5.local committed
3585
  var->save_result.plugin= NULL;
3586 3587
  if (var->value->result_type() == STRING_RESULT)
  {
3588
    LEX_STRING engine_name;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3589
    handlerton *hton;
3590
    if (!(res=var->value->val_str(&str)) ||
3591 3592 3593
        !(engine_name.str= (char *)res->ptr()) ||
        !(engine_name.length= res->length()) ||
	!(var->save_result.plugin= ha_resolve_by_name(thd, &engine_name)) ||
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3594 3595
        !(hton= plugin_data(var->save_result.plugin, handlerton *)) ||
        ha_checktype(thd, ha_legacy_type(hton), 1, 0) != hton)
3596 3597 3598 3599 3600 3601
    {
      value= res ? res->c_ptr() : "NULL";
      goto err;
    }
    return 0;
  }
monty@mysql.com's avatar
monty@mysql.com committed
3602
  value= "unknown";
3603 3604

err:
3605
  my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), value);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3606
  return 1;
3607 3608
}

3609

3610
uchar *sys_var_thd_storage_engine::value_ptr(THD *thd, enum_var_type type,
3611
					    LEX_STRING *base)
3612
{
3613
  uchar* result;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3614
  handlerton *hton;
3615
  LEX_STRING *engine_name;
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3616 3617 3618 3619
  plugin_ref plugin= thd->variables.*offset;
  if (type == OPT_GLOBAL)
    plugin= my_plugin_lock(thd, &(global_system_variables.*offset));
  hton= plugin_data(plugin, handlerton*);
3620 3621
  engine_name= &hton2plugin[hton->slot]->name;
  result= (uchar *) thd->strmake(engine_name->str, engine_name->length);
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3622 3623 3624
  if (type == OPT_GLOBAL)
    plugin_unlock(thd, plugin);
  return result;
3625 3626
}

3627

3628
void sys_var_thd_storage_engine::set_default(THD *thd, enum_var_type type)
3629
{
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3630
  plugin_ref old_value, new_value, *value;
3631
  if (type == OPT_GLOBAL)
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3632 3633 3634 3635
  {
    value= &(global_system_variables.*offset);
    new_value= ha_lock_engine(NULL, myisam_hton);
  }
3636
  else
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3637 3638 3639 3640 3641 3642 3643 3644
  {
    value= &(thd->variables.*offset);
    new_value= my_plugin_lock(NULL, &(global_system_variables.*offset));
  }
  DBUG_ASSERT(new_value);
  old_value= *value;
  *value= new_value;
  plugin_unlock(NULL, old_value);
3645 3646
}

3647

3648
bool sys_var_thd_storage_engine::update(THD *thd, set_var *var)
3649
{
antony@ppcg5.local's avatar
antony@ppcg5.local committed
3650 3651 3652 3653 3654 3655 3656 3657 3658
  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)
  {
    *value= my_plugin_lock(NULL, &var->save_result.plugin);
    plugin_unlock(NULL, old_value);
  }
3659 3660 3661
  return 0;
}

3662 3663
void sys_var_thd_table_type::warn_deprecated(THD *thd)
{
3664
  WARN_DEPRECATED(thd, "5.2", "@@table_type", "'@@storage_engine'");
3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678
}

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

3679

3680 3681 3682
/****************************************************************************
 Functions to handle sql_mode
****************************************************************************/
3683

3684 3685
/**
  Make string representation of mode.
3686

3687 3688 3689 3690 3691 3692
  @param[in]  thd    thread handler
  @param[in]  val    sql_mode value
  @param[out] len    pointer on length of string

  @return
    pointer to string with sql_mode representation
3693 3694
*/

3695 3696 3697
bool
sys_var_thd_sql_mode::
symbolic_mode_representation(THD *thd, ulonglong val, LEX_STRING *rep)
3698
{
3699
  char buff[STRING_BUFFER_USUAL_SIZE*8];
3700 3701 3702
  String tmp(buff, sizeof(buff), &my_charset_latin1);

  tmp.length(0);
3703

3704 3705 3706 3707
  for (uint i= 0; val; val>>= 1, i++)
  {
    if (val & 1)
    {
3708 3709
      tmp.append(sql_mode_typelib.type_names[i],
                 sql_mode_typelib.type_lengths[i]);
3710 3711 3712
      tmp.append(',');
    }
  }
monty@mishka.local's avatar
monty@mishka.local committed
3713

3714 3715 3716 3717 3718 3719 3720 3721
  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();
3722 3723
}

monty@mishka.local's avatar
monty@mishka.local committed
3724

3725
uchar *sys_var_thd_sql_mode::value_ptr(THD *thd, enum_var_type type,
3726 3727
				      LEX_STRING *base)
{
3728
  LEX_STRING sql_mode;
3729 3730
  ulonglong val= ((type == OPT_GLOBAL) ? global_system_variables.*offset :
                  thd->variables.*offset);
3731
  (void) symbolic_mode_representation(thd, val, &sql_mode);
3732
  return (uchar *) sql_mode.str;
3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743
}


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

monty@mishka.local's avatar
monty@mishka.local committed
3744

3745 3746 3747 3748 3749 3750
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
3751
  {
3752
    thd->variables.sql_mode= fix_sql_mode(thd->variables.sql_mode);
3753 3754 3755 3756 3757 3758 3759 3760
    /*
      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;
  }
3761 3762
}

3763
/** Map database specific bits to function bits. */
3764

3765 3766 3767 3768 3769 3770 3771 3772 3773
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)
3774
  {
3775
    sql_mode|= (MODE_REAL_AS_FLOAT | MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
3776 3777 3778 3779 3780 3781
		MODE_IGNORE_SPACE);
    /* 
      MODE_ONLY_FULL_GROUP_BY removed from ANSI mode because it is currently
      overly restrictive (see BUG#8510).
    */
  }
3782 3783 3784 3785
  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 |
3786
		MODE_NO_FIELD_OPTIONS | MODE_NO_AUTO_CREATE_USER);
3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801
  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);
3802
  if (sql_mode & MODE_MAXDB)
3803 3804 3805
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
3806
		MODE_NO_FIELD_OPTIONS | MODE_NO_AUTO_CREATE_USER);
3807
  if (sql_mode & MODE_MYSQL40)
3808
    sql_mode|= MODE_HIGH_NOT_PRECEDENCE;
3809
  if (sql_mode & MODE_MYSQL323)
3810
    sql_mode|= MODE_HIGH_NOT_PRECEDENCE;
3811 3812 3813
  if (sql_mode & MODE_TRADITIONAL)
    sql_mode|= (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES |
                MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
3814
                MODE_ERROR_FOR_DIVISION_BY_ZERO | MODE_NO_AUTO_CREATE_USER);
3815 3816
  return sql_mode;
}
3817 3818


3819 3820 3821 3822
/****************************************************************************
  Named list handling
****************************************************************************/

3823
uchar* find_named(I_List<NAMED_LIST> *list, const char *name, uint length,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3824
		NAMED_LIST **found)
3825 3826 3827 3828 3829 3830
{
  I_List_iterator<NAMED_LIST> it(*list);
  NAMED_LIST *element;
  while ((element= it++))
  {
    if (element->cmp(name, length))
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3831
    {
3832 3833
      if (found)
        *found= element;
3834
      return element->data;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3835
    }
3836 3837 3838 3839 3840
  }
  return 0;
}


3841
void delete_elements(I_List<NAMED_LIST> *list,
3842
		     void (*free_element)(const char *name, uchar*))
3843 3844
{
  NAMED_LIST *element;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3845
  DBUG_ENTER("delete_elements");
3846 3847
  while ((element= list->get()))
  {
3848
    (*free_element)(element->name, element->data);
3849 3850
    delete element;
  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3851
  DBUG_VOID_RETURN;
3852 3853 3854 3855 3856
}


/* Key cache functions */

3857
static KEY_CACHE *create_key_cache(const char *name, uint length)
3858
{
3859
  KEY_CACHE *key_cache;
3860 3861 3862
  DBUG_ENTER("create_key_cache");
  DBUG_PRINT("enter",("name: %.*s", length, name));
  
3863
  if ((key_cache= (KEY_CACHE*) my_malloc(sizeof(KEY_CACHE),
3864
					     MYF(MY_ZEROFILL | MY_WME))))
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3865
  {
3866
    if (!new NAMED_LIST(&key_caches, name, length, (uchar*) key_cache))
3867 3868
    {
      my_free((char*) key_cache, MYF(0));
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3869
      key_cache= 0;
3870 3871 3872 3873 3874 3875 3876 3877 3878
    }
    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
      */
3879 3880 3881
      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;
3882
    }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3883
  }
3884
  DBUG_RETURN(key_cache);
3885 3886 3887
}


3888
KEY_CACHE *get_or_create_key_cache(const char *name, uint length)
3889
{
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3890
  LEX_STRING key_cache_name;
3891
  KEY_CACHE *key_cache;
3892

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3893 3894
  key_cache_name.str= (char *) name;
  key_cache_name.length= length;
3895 3896
  pthread_mutex_lock(&LOCK_global_system_variables);
  if (!(key_cache= get_key_cache(&key_cache_name)))
3897
    key_cache= create_key_cache(name, length);
3898
  pthread_mutex_unlock(&LOCK_global_system_variables);
3899 3900 3901 3902
  return key_cache;
}


3903
void free_key_cache(const char *name, KEY_CACHE *key_cache)
3904
{
3905 3906
  ha_end_key_cache(key_cache);
  my_free((char*) key_cache, MYF(0));
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3907 3908 3909
}


3910
bool process_key_caches(process_key_cache_t func)
3911
{
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3912 3913
  I_List_iterator<NAMED_LIST> it(key_caches);
  NAMED_LIST *element;
3914

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3915 3916
  while ((element= it++))
  {
3917
    KEY_CACHE *key_cache= (KEY_CACHE *) element->data;
3918
    func(element->name, key_cache);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3919 3920
  }
  return 0;
3921 3922
}

3923

3924 3925
void sys_var_trust_routine_creators::warn_deprecated(THD *thd)
{
3926 3927
  WARN_DEPRECATED(thd, "5.2", "@@log_bin_trust_routine_creators",
                      "'@@log_bin_trust_function_creators'");
3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941
}

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

3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989
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.
  */
3990
  if (result= close_cached_tables(thd, NULL, FALSE, TRUE, TRUE))
3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005
    goto end_with_read_lock;

  if (result= make_global_read_lock_block_commit(thd))
    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);
}


serg@serg.mylan's avatar
serg@serg.mylan committed
4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016
/* 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)
{
  if (var->type == OPT_GLOBAL)
    DBUG_SET_INITIAL(var ? var->value->str_value.c_ptr() : "");
  else
4017 4018
    DBUG_SET(var ? var->value->str_value.c_ptr() : "");

serg@serg.mylan's avatar
serg@serg.mylan committed
4019 4020 4021
  return 0;
}

4022

4023
uchar *sys_var_thd_dbug::value_ptr(THD *thd, enum_var_type type, LEX_STRING *b)
serg@serg.mylan's avatar
serg@serg.mylan committed
4024 4025 4026 4027 4028 4029
{
  char buf[256];
  if (type == OPT_GLOBAL)
    DBUG_EXPLAIN_INITIAL(buf, sizeof(buf));
  else
    DBUG_EXPLAIN(buf, sizeof(buf));
4030
  return (uchar*) thd->strdup(buf);
serg@serg.mylan's avatar
serg@serg.mylan committed
4031
}
andrey@lmy004's avatar
andrey@lmy004 committed
4032

4033
#ifdef HAVE_EVENT_SCHEDULER
4034 4035 4036 4037 4038
bool sys_var_event_scheduler::check(THD *thd, set_var *var)
{
  return check_enum(thd, var, &Events::var_typelib);
}

4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056
/*
   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)
{
4057
  int res;
4058 4059
  /* here start the thread if not running. */
  DBUG_ENTER("sys_var_event_scheduler::update");
4060
  DBUG_PRINT("info", ("new_value: %d", (int) var->save_result.ulong_value));
4061

4062 4063 4064 4065
  enum Events::enum_opt_event_scheduler
    new_state=
    (enum Events::enum_opt_event_scheduler) var->save_result.ulong_value;

4066
  res= Events::switch_event_scheduler_state(new_state);
4067 4068 4069 4070 4071

  DBUG_RETURN((bool) res);
}


4072
uchar *sys_var_event_scheduler::value_ptr(THD *thd, enum_var_type type,
4073 4074
                                         LEX_STRING *base)
{
4075
  return (uchar *) Events::get_opt_event_scheduler_str();
4076
}
4077
#endif
4078

4079 4080 4081 4082
/****************************************************************************
  Used templates
****************************************************************************/

4083
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
4084
template class List<set_var_base>;
4085
template class List_iterator_fast<set_var_base>;
4086
template class I_List_iterator<NAMED_LIST>;
4087
#endif