set_var.cc 129 KB
Newer Older
1
/* Copyright (C) 2000-2003 MySQL AB
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

   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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

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

/*
  Handling of MySQL SQL variables

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

34 35 36 37 38 39
  NOTES:
    - Be careful with var->save_result: sys_var::check() only updates
    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).

40 41 42 43 44 45 46 47
  TODO:
    - Add full support for the variable character_set (for 4.1)

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

48
#ifdef USE_PRAGMA_IMPLEMENTATION
49 50 51 52
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
53
#include <mysql.h>
54 55
#include "slave.h"
#include <my_getopt.h>
56
#include <thr_alarm.h>
57
#include <myisam.h>
58
#include <my_dir.h>
59

60
#include "events.h"
61

62 63 64 65
/* WITH_INNOBASE_STORAGE_ENGINE */
extern uint innobase_flush_log_at_trx_commit;
extern ulong innobase_fast_shutdown;
extern long innobase_mirrored_log_groups, innobase_log_files_in_group;
kent@mysql.com's avatar
kent@mysql.com committed
66 67 68 69
extern longlong innobase_log_file_size;
extern long innobase_log_buffer_size;
extern longlong innobase_buffer_pool_size;
extern long innobase_additional_mem_pool_size;
70 71 72 73 74 75 76 77 78 79 80 81 82
extern long innobase_file_io_threads, innobase_lock_wait_timeout;
extern long innobase_force_recovery;
extern long innobase_open_files;
extern char *innobase_data_home_dir, *innobase_data_file_path;
extern char *innobase_log_group_home_dir, *innobase_log_arch_dir;
extern char *innobase_unix_file_flush_method;
/* The following variables have to be my_bool for SHOW VARIABLES to work */
extern my_bool innobase_log_archive,
               innobase_use_doublewrite,
               innobase_use_checksums,
               innobase_file_per_table,
               innobase_locks_unsafe_for_binlog;

83
extern "C" {
84 85 86 87 88 89 90 91
extern ulong srv_max_buf_pool_modified_pct;
extern ulong srv_max_purge_lag;
extern ulong srv_auto_extend_increment;
extern ulong srv_n_spin_wait_rounds;
extern ulong srv_n_free_tickets_to_enter;
extern ulong srv_thread_sleep_delay;
extern ulong srv_thread_concurrency;
extern ulong srv_commit_concurrency;
92
extern ulong srv_flush_log_at_trx_commit;
93
}
94 95 96

/* WITH_NDBCLUSTER_STORAGE_ENGINE */
extern ulong ndb_cache_check_time;
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
97 98 99 100 101
extern ulong ndb_extra_logging;
#ifdef HAVE_NDB_BINLOG
extern ulong ndb_report_thresh_binlog_epoch_slip;
extern ulong ndb_report_thresh_binlog_mem_usage;
#endif
102 103 104



105 106 107 108
static HASH system_variable_hash;
const char *bool_type_names[]= { "OFF", "ON", NullS };
TYPELIB bool_typelib=
{
109
  array_elements(bool_type_names)-1, "", bool_type_names, NULL
110 111
};

112 113 114
const char *delay_key_write_type_names[]= { "OFF", "ON", "ALL", NullS };
TYPELIB delay_key_write_typelib=
{
115 116
  array_elements(delay_key_write_type_names)-1, "",
  delay_key_write_type_names, NULL
117 118
};

119
static int sys_check_charset(THD *thd, set_var *var);
120 121
static bool sys_update_charset(THD *thd, set_var *var);
static void sys_set_default_charset(THD *thd, enum_var_type type);
122 123 124
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
125 126 127 128
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);
129 130
static bool set_option_bit(THD *thd, set_var *var);
static bool set_option_autocommit(THD *thd, set_var *var);
131
static int  check_log_update(THD *thd, set_var *var);
132
static bool set_log_update(THD *thd, set_var *var);
133
static int  check_pseudo_thread_id(THD *thd, set_var *var);
134
void fix_binlog_format_after_update(THD *thd, enum_var_type type);
135
static void fix_low_priority_updates(THD *thd, enum_var_type type);
136
static int check_tx_isolation(THD *thd, set_var *var);
137
static void fix_tx_isolation(THD *thd, enum_var_type type);
138 139
static int check_completion_type(THD *thd, set_var *var);
static void fix_completion_type(THD *thd, enum_var_type type);
140 141
static void fix_net_read_timeout(THD *thd, enum_var_type type);
static void fix_net_write_timeout(THD *thd, enum_var_type type);
142
static void fix_net_retry_count(THD *thd, enum_var_type type);
143 144
static void fix_max_join_size(THD *thd, enum_var_type type);
static void fix_query_cache_size(THD *thd, enum_var_type type);
145
static void fix_query_cache_min_res_unit(THD *thd, enum_var_type type);
146
static void fix_myisam_max_sort_file_size(THD *thd, enum_var_type type);
147 148
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);
149
static void fix_max_connections(THD *thd, enum_var_type type);
150
static int check_max_delayed_threads(THD *thd, set_var *var);
151 152
static void fix_thd_mem_root(THD *thd, enum_var_type type);
static void fix_trans_mem_root(THD *thd, enum_var_type type);
153
static void fix_server_id(THD *thd, enum_var_type type);
154
static KEY_CACHE *create_key_cache(const char *name, uint length);
155
void fix_sql_mode_var(THD *thd, enum_var_type type);
156 157
static byte *get_error_count(THD *thd);
static byte *get_warning_count(THD *thd);
158
static byte *get_tmpdir(THD *thd);
159 160 161 162 163
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);
164 165 166 167 168

/*
  Variable definition list

  These are variables that can be set from the command line, in
169 170 171 172
  alphabetic order.

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

175 176 177
sys_var *sys_var::first= NULL;
uint sys_var::sys_vars= 0;

178 179 180 181 182
sys_var_thd_ulong	sys_auto_increment_increment("auto_increment_increment",
                                                     &SV::auto_increment_increment);
sys_var_thd_ulong	sys_auto_increment_offset("auto_increment_offset",
                                                  &SV::auto_increment_offset);

183 184 185
sys_var_bool_ptr	sys_automatic_sp_privileges("automatic_sp_privileges",
					      &sp_automatic_privileges);

186
sys_var_const_str       sys_basedir("basedir", mysql_home);
187 188
sys_var_long_ptr	sys_binlog_cache_size("binlog_cache_size",
					      &binlog_cache_size);
189 190
sys_var_thd_binlog_format sys_binlog_format("binlog_format",
                                            &SV::binlog_format);
191 192
sys_var_thd_ulong	sys_bulk_insert_buff_size("bulk_insert_buffer_size",
						  &SV::bulk_insert_buff_size);
193
sys_var_character_set_server	sys_character_set_server("character_set_server");
194 195
sys_var_const_str       sys_charset_system("character_set_system",
                                           (char *)my_charset_utf8_general_ci.name);
196
sys_var_character_set_database	sys_character_set_database("character_set_database");
197
sys_var_character_set_client  sys_character_set_client("character_set_client");
198
sys_var_character_set_connection  sys_character_set_connection("character_set_connection");
199
sys_var_character_set_results sys_character_set_results("character_set_results");
200
sys_var_character_set_filesystem  sys_character_set_filesystem("character_set_filesystem");
201 202 203 204
sys_var_thd_ulong	sys_completion_type("completion_type",
					 &SV::completion_type,
					 check_completion_type,
					 fix_completion_type);
205
sys_var_collation_connection sys_collation_connection("collation_connection");
206 207
sys_var_collation_database sys_collation_database("collation_database");
sys_var_collation_server sys_collation_server("collation_server");
208 209
sys_var_long_ptr	sys_concurrent_insert("concurrent_insert",
                                              &myisam_concurrent_insert);
210 211
sys_var_long_ptr	sys_connect_timeout("connect_timeout",
					    &connect_timeout);
212
sys_var_const_str       sys_datadir("datadir", mysql_real_data_home);
serg@serg.mylan's avatar
serg@serg.mylan committed
213 214 215
#ifndef DBUG_OFF
sys_var_thd_dbug        sys_dbug("debug");
#endif
216 217 218 219
sys_var_enum		sys_delay_key_write("delay_key_write",
					    &delay_key_write_options,
					    &delay_key_write_typelib,
					    fix_delay_key_write);
220 221 222 223 224 225
sys_var_long_ptr	sys_delayed_insert_limit("delayed_insert_limit",
						 &delayed_insert_limit);
sys_var_long_ptr	sys_delayed_insert_timeout("delayed_insert_timeout",
						   &delayed_insert_timeout);
sys_var_long_ptr	sys_delayed_queue_size("delayed_queue_size",
					       &delayed_queue_size);
226 227

sys_var_event_scheduler sys_event_scheduler("event_scheduler");
228 229
sys_var_long_ptr	sys_expire_logs_days("expire_logs_days",
					     &expire_logs_days);
230 231
sys_var_bool_ptr	sys_flush("flush", &myisam_flush);
sys_var_long_ptr	sys_flush_time("flush_time", &flush_time);
232 233 234 235 236 237 238 239 240 241 242
sys_var_str             sys_ft_boolean_syntax("ft_boolean_syntax",
                                         sys_check_ftb_syntax,
                                         sys_update_ftb_syntax,
                                         sys_default_ftb_syntax,
                                         ft_boolean_syntax);
sys_var_str             sys_init_connect("init_connect", 0,
                                         sys_update_init_connect,
                                         sys_default_init_connect,0);
sys_var_str             sys_init_slave("init_slave", 0,
                                       sys_update_init_slave,
                                       sys_default_init_slave,0);
243 244 245 246
sys_var_thd_ulong	sys_interactive_timeout("interactive_timeout",
						&SV::net_interactive_timeout);
sys_var_thd_ulong	sys_join_buffer_size("join_buffer_size",
					     &SV::join_buff_size);
247
sys_var_key_buffer_size	sys_key_buffer_size("key_buffer_size");
248
sys_var_key_cache_long  sys_key_cache_block_size("key_cache_block_size",
249 250
						 offsetof(KEY_CACHE,
							  param_block_size));
251
sys_var_key_cache_long	sys_key_cache_division_limit("key_cache_division_limit",
252 253
						     offsetof(KEY_CACHE,
							      param_division_limit));
254
sys_var_key_cache_long  sys_key_cache_age_threshold("key_cache_age_threshold",
255 256
						     offsetof(KEY_CACHE,
							      param_age_threshold));
257 258
sys_var_bool_ptr	sys_local_infile("local_infile",
					 &opt_local_infile);
259
sys_var_trust_routine_creators
260
sys_trust_routine_creators("log_bin_trust_routine_creators",
261 262 263 264
                           &trust_function_creators);
sys_var_bool_ptr       
sys_trust_function_creators("log_bin_trust_function_creators",
                            &trust_function_creators);
265 266 267
sys_var_bool_ptr
  sys_log_queries_not_using_indexes("log_queries_not_using_indexes",
                                    &opt_log_queries_not_using_indexes);
268
sys_var_thd_ulong	sys_log_warnings("log_warnings", &SV::log_warnings);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
sys_var_thd_ulong	sys_long_query_time("long_query_time",
					     &SV::long_query_time);
sys_var_thd_bool	sys_low_priority_updates("low_priority_updates",
						 &SV::low_priority_updates,
						 fix_low_priority_updates);
#ifndef TO_BE_DELETED	/* Alias for the low_priority_updates */
sys_var_thd_bool	sys_sql_low_priority_updates("sql_low_priority_updates",
						     &SV::low_priority_updates,
						     fix_low_priority_updates);
#endif
sys_var_thd_ulong	sys_max_allowed_packet("max_allowed_packet",
					       &SV::max_allowed_packet);
sys_var_long_ptr	sys_max_binlog_cache_size("max_binlog_cache_size",
						  &max_binlog_cache_size);
sys_var_long_ptr	sys_max_binlog_size("max_binlog_size",
284 285
					    &max_binlog_size,
                                            fix_max_binlog_size);
286
sys_var_long_ptr	sys_max_connections("max_connections",
287 288
					    &max_connections,
                                            fix_max_connections);
289 290
sys_var_long_ptr	sys_max_connect_errors("max_connect_errors",
					       &max_connect_errors);
291
sys_var_thd_ulong       sys_max_insert_delayed_threads("max_insert_delayed_threads",
292 293 294
						       &SV::max_insert_delayed_threads,
                                                       check_max_delayed_threads,
                                                       fix_max_connections);
295 296
sys_var_thd_ulong	sys_max_delayed_threads("max_delayed_threads",
						&SV::max_insert_delayed_threads,
297 298
                                                check_max_delayed_threads,
                                                fix_max_connections);
299 300
sys_var_thd_ulong	sys_max_error_count("max_error_count",
					    &SV::max_error_count);
301 302
sys_var_thd_ulong	sys_max_heap_table_size("max_heap_table_size",
						&SV::max_heap_table_size);
303 304 305
sys_var_thd_ulong       sys_pseudo_thread_id("pseudo_thread_id",
					     &SV::pseudo_thread_id,
                                             check_pseudo_thread_id, 0);
306
sys_var_thd_ha_rows	sys_max_join_size("max_join_size",
307 308
					  &SV::max_join_size,
					  fix_max_join_size);
309 310
sys_var_thd_ulong	sys_max_seeks_for_key("max_seeks_for_key",
					      &SV::max_seeks_for_key);
igor@hundin.mysql.fi's avatar
igor@hundin.mysql.fi committed
311
sys_var_thd_ulong   sys_max_length_for_sort_data("max_length_for_sort_data",
312
                                                 &SV::max_length_for_sort_data);
313
#ifndef TO_BE_DELETED	/* Alias for max_join_size */
314
sys_var_thd_ha_rows	sys_sql_max_join_size("sql_max_join_size",
315 316 317
					      &SV::max_join_size,
					      fix_max_join_size);
#endif
318 319 320 321
static sys_var_long_ptr_global
sys_max_prepared_stmt_count("max_prepared_stmt_count",
                            &max_prepared_stmt_count,
                            &LOCK_prepared_stmt_count);
322 323 324
sys_var_long_ptr	sys_max_relay_log_size("max_relay_log_size",
                                               &max_relay_log_size,
                                               fix_max_relay_log_size);
325 326
sys_var_thd_ulong	sys_max_sort_length("max_sort_length",
					    &SV::max_sort_length);
327 328
sys_var_thd_ulong	sys_max_sp_recursion_depth("max_sp_recursion_depth",
                                                   &SV::max_sp_recursion_depth);
329
sys_var_max_user_conn   sys_max_user_connections("max_user_connections");
330 331 332 333
sys_var_thd_ulong	sys_max_tmp_tables("max_tmp_tables",
					   &SV::max_tmp_tables);
sys_var_long_ptr	sys_max_write_lock_count("max_write_lock_count",
						 &max_write_lock_count);
ingo@mysql.com's avatar
ingo@mysql.com committed
334 335
sys_var_thd_ulong       sys_multi_range_count("multi_range_count",
                                              &SV::multi_range_count);
serg@serg.mylan's avatar
serg@serg.mylan committed
336 337
sys_var_long_ptr	sys_myisam_data_pointer_size("myisam_data_pointer_size",
                                                    &myisam_data_pointer_size);
338
sys_var_thd_ulonglong	sys_myisam_max_sort_file_size("myisam_max_sort_file_size", &SV::myisam_max_sort_file_size, fix_myisam_max_sort_file_size, 1);
339
sys_var_thd_ulong       sys_myisam_repair_threads("myisam_repair_threads", &SV::myisam_repair_threads);
340
sys_var_thd_ulong	sys_myisam_sort_buffer_size("myisam_sort_buffer_size", &SV::myisam_sort_buff_size);
vtkachenko@quadxeon.mysql.com's avatar
vtkachenko@quadxeon.mysql.com committed
341 342
sys_var_bool_ptr	sys_myisam_use_mmap("myisam_use_mmap", 
                                            &opt_myisam_use_mmap);
343 344 345 346 347 348

sys_var_thd_enum        sys_myisam_stats_method("myisam_stats_method",
                                                &SV::myisam_stats_method,
                                                &myisam_stats_method_typelib,
                                                NULL);

349 350 351 352
sys_var_thd_ulong	sys_net_buffer_length("net_buffer_length",
					      &SV::net_buffer_length);
sys_var_thd_ulong	sys_net_read_timeout("net_read_timeout",
					     &SV::net_read_timeout,
353
					     0, fix_net_read_timeout);
354 355
sys_var_thd_ulong	sys_net_write_timeout("net_write_timeout",
					      &SV::net_write_timeout,
356
					      0, fix_net_write_timeout);
357 358
sys_var_thd_ulong	sys_net_retry_count("net_retry_count",
					    &SV::net_retry_count,
359
					    0, fix_net_retry_count);
360
sys_var_thd_bool	sys_new_mode("new", &SV::new_mode);
361 362
sys_var_thd_bool	sys_old_alter_table("old_alter_table",
					    &SV::old_alter_table);
363
sys_var_thd_bool	sys_old_passwords("old_passwords", &SV::old_passwords);
364 365 366 367
sys_var_thd_ulong       sys_optimizer_prune_level("optimizer_prune_level",
                                                  &SV::optimizer_prune_level);
sys_var_thd_ulong       sys_optimizer_search_depth("optimizer_search_depth",
                                                   &SV::optimizer_search_depth);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
368 369
sys_var_thd_ulong       sys_preload_buff_size("preload_buffer_size",
                                              &SV::preload_buff_size);
370 371
sys_var_thd_ulong	sys_read_buff_size("read_buffer_size",
					   &SV::read_buff_size);
372
sys_var_opt_readonly    sys_readonly("read_only", &opt_readonly);
373 374
sys_var_thd_ulong	sys_read_rnd_buff_size("read_rnd_buffer_size",
					       &SV::read_rnd_buff_size);
375 376
sys_var_thd_ulong	sys_div_precincrement("div_precision_increment",
                                              &SV::div_precincrement);
377 378 379 380
#ifdef HAVE_REPLICATION
sys_var_bool_ptr	sys_relay_log_purge("relay_log_purge",
                                            &relay_log_purge);
#endif
381 382 383 384 385
sys_var_long_ptr	sys_rpl_recovery_rank("rpl_recovery_rank",
					      &rpl_recovery_rank);
sys_var_long_ptr	sys_query_cache_size("query_cache_size",
					     &query_cache_size,
					     fix_query_cache_size);
386 387 388 389

sys_var_thd_ulong	sys_range_alloc_block_size("range_alloc_block_size",
						   &SV::range_alloc_block_size);
sys_var_thd_ulong	sys_query_alloc_block_size("query_alloc_block_size",
390 391
						   &SV::query_alloc_block_size,
						   0, fix_thd_mem_root);
392
sys_var_thd_ulong	sys_query_prealloc_size("query_prealloc_size",
393 394
						&SV::query_prealloc_size,
						0, fix_thd_mem_root);
395
sys_var_readonly        sys_tmpdir("tmpdir", OPT_GLOBAL, SHOW_CHAR, get_tmpdir);
396
sys_var_thd_ulong	sys_trans_alloc_block_size("transaction_alloc_block_size",
397 398
						   &SV::trans_alloc_block_size,
						   0, fix_trans_mem_root);
399
sys_var_thd_ulong	sys_trans_prealloc_size("transaction_prealloc_size",
400 401
						&SV::trans_prealloc_size,
						0, fix_trans_mem_root);
402

403 404 405
#ifdef HAVE_QUERY_CACHE
sys_var_long_ptr	sys_query_cache_limit("query_cache_limit",
					      &query_cache.query_cache_limit);
406 407 408
sys_var_long_ptr        sys_query_cache_min_res_unit("query_cache_min_res_unit",
						     &query_cache_min_res_unit,
						     fix_query_cache_min_res_unit);
409 410 411
sys_var_thd_enum	sys_query_cache_type("query_cache_type",
					     &SV::query_cache_type,
					     &query_cache_type_typelib);
412 413 414
sys_var_thd_bool
sys_query_cache_wlock_invalidate("query_cache_wlock_invalidate",
				 &SV::query_cache_wlock_invalidate);
415
#endif /* HAVE_QUERY_CACHE */
416
sys_var_bool_ptr	sys_secure_auth("secure_auth", &opt_secure_auth);
417
sys_var_long_ptr	sys_server_id("server_id", &server_id, fix_server_id);
418 419
sys_var_bool_ptr	sys_slave_compressed_protocol("slave_compressed_protocol",
						      &opt_slave_compressed_protocol);
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
420
#ifdef HAVE_REPLICATION
421 422
sys_var_long_ptr	sys_slave_net_timeout("slave_net_timeout",
					      &slave_net_timeout);
423 424
sys_var_long_ptr	sys_slave_trans_retries("slave_transaction_retries",
                                                &slave_trans_retries);
425
#endif
426 427 428 429
sys_var_long_ptr	sys_slow_launch_time("slow_launch_time",
					     &slow_launch_time);
sys_var_thd_ulong	sys_sort_buffer("sort_buffer_size",
					&SV::sortbuff_size);
430 431
sys_var_thd_sql_mode    sys_sql_mode("sql_mode",
                                     &SV::sql_mode);
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
#ifdef HAVE_OPENSSL
extern char *opt_ssl_ca, *opt_ssl_capath, *opt_ssl_cert, *opt_ssl_cipher,
            *opt_ssl_key;
sys_var_const_str_ptr	sys_ssl_ca("ssl_ca", &opt_ssl_ca);
sys_var_const_str_ptr	sys_ssl_capath("ssl_capath", &opt_ssl_capath);
sys_var_const_str_ptr	sys_ssl_cert("ssl_cert", &opt_ssl_cert);
sys_var_const_str_ptr	sys_ssl_cipher("ssl_cipher", &opt_ssl_cipher);
sys_var_const_str_ptr	sys_ssl_key("ssl_key", &opt_ssl_key);
#else
sys_var_const_str	sys_ssl_ca("ssl_ca", NULL);
sys_var_const_str	sys_ssl_capath("ssl_capath", NULL);
sys_var_const_str	sys_ssl_cert("ssl_cert", NULL);
sys_var_const_str	sys_ssl_cipher("ssl_cipher", NULL);
sys_var_const_str	sys_ssl_key("ssl_key", NULL);
#endif
447 448 449 450
sys_var_thd_enum
sys_updatable_views_with_limit("updatable_views_with_limit",
                               &SV::updatable_views_with_limit,
                               &updatable_views_with_limit_typelib);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
451

452 453
sys_var_thd_table_type  sys_table_type("table_type",
				       &SV::table_type);
454 455
sys_var_thd_storage_engine sys_storage_engine("storage_engine",
				       &SV::table_type);
456 457 458 459
#ifdef HAVE_REPLICATION
sys_var_sync_binlog_period sys_sync_binlog_period("sync_binlog", &sync_binlog_period);
#endif
sys_var_bool_ptr	sys_sync_frm("sync_frm", &opt_sync_frm);
460 461
sys_var_const_str	sys_system_time_zone("system_time_zone",
                                             system_time_zone);
462 463 464
sys_var_long_ptr	sys_table_def_size("table_definition_cache",
                                           &table_def_size);
sys_var_long_ptr	sys_table_cache_size("table_open_cache",
465
					     &table_cache_size);
466 467
sys_var_long_ptr	sys_table_lock_wait_timeout("table_lock_wait_timeout",
                                                    &table_lock_wait_timeout);
468 469 470 471 472
sys_var_long_ptr	sys_thread_cache_size("thread_cache_size",
					      &thread_cache_size);
sys_var_thd_enum	sys_tx_isolation("tx_isolation",
					 &SV::tx_isolation,
					 &tx_isolation_typelib,
473 474
					 fix_tx_isolation,
					 check_tx_isolation);
475 476
sys_var_thd_ulong	sys_tmp_table_size("tmp_table_size",
					   &SV::tmp_table_size);
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
477 478
sys_var_bool_ptr  sys_timed_mutexes("timed_mutexes",
                                    &timed_mutexes);
479 480 481 482 483 484 485
sys_var_const_str	sys_version("version", server_version);
sys_var_const_str	sys_version_comment("version_comment",
                                            MYSQL_COMPILATION_COMMENT);
sys_var_const_str	sys_version_compile_machine("version_compile_machine",
                                                    MACHINE_TYPE);
sys_var_const_str	sys_version_compile_os("version_compile_os",
                                               SYSTEM_TYPE);
486 487
sys_var_thd_ulong	sys_net_wait_timeout("wait_timeout",
					     &SV::net_wait_timeout);
488
#ifdef WITH_INNOBASE_STORAGE_ENGINE
489 490
sys_var_long_ptr	sys_innodb_fast_shutdown("innodb_fast_shutdown",
						 &innobase_fast_shutdown);
491 492
sys_var_long_ptr        sys_innodb_max_dirty_pages_pct("innodb_max_dirty_pages_pct",
                                                        &srv_max_buf_pool_modified_pct);
493 494
sys_var_long_ptr	sys_innodb_max_purge_lag("innodb_max_purge_lag",
							&srv_max_purge_lag);
495 496
sys_var_thd_bool	sys_innodb_table_locks("innodb_table_locks",
                                               &SV::innodb_table_locks);
497 498
sys_var_thd_bool	sys_innodb_support_xa("innodb_support_xa",
                                               &SV::innodb_support_xa);
499 500
sys_var_long_ptr	sys_innodb_autoextend_increment("innodb_autoextend_increment",
							&srv_auto_extend_increment);
501 502
sys_var_long_ptr	sys_innodb_sync_spin_loops("innodb_sync_spin_loops",
                                             &srv_n_spin_wait_rounds);
vtkachenko@mail.mysql.com's avatar
vtkachenko@mail.mysql.com committed
503
sys_var_long_ptr  sys_innodb_concurrency_tickets("innodb_concurrency_tickets",
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
504 505 506 507 508
                                             &srv_n_free_tickets_to_enter);
sys_var_long_ptr  sys_innodb_thread_sleep_delay("innodb_thread_sleep_delay",
                                                &srv_thread_sleep_delay);
sys_var_long_ptr  sys_innodb_thread_concurrency("innodb_thread_concurrency",
                                                &srv_thread_concurrency);
509 510
sys_var_long_ptr  sys_innodb_commit_concurrency("innodb_commit_concurrency",
                                                &srv_commit_concurrency);
511 512 513
sys_var_long_ptr  sys_innodb_flush_log_at_trx_commit(
                                        "innodb_flush_log_at_trx_commit",
                                        &srv_flush_log_at_trx_commit);
514
#endif
mskold@mysql.com's avatar
mskold@mysql.com committed
515 516 517 518 519
/* Condition pushdown to storage engine */
sys_var_thd_bool
sys_engine_condition_pushdown("engine_condition_pushdown",
			      &SV::engine_condition_pushdown);

520
/* ndb thread specific variable settings */
521
sys_var_thd_ulong
522 523 524
sys_ndb_autoincrement_prefetch_sz("ndb_autoincrement_prefetch_sz",
				  &SV::ndb_autoincrement_prefetch_sz);
sys_var_thd_bool
525
sys_ndb_force_send("ndb_force_send", &SV::ndb_force_send);
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
526 527 528 529 530 531 532 533
#ifdef HAVE_NDB_BINLOG
sys_var_long_ptr
sys_ndb_report_thresh_binlog_epoch_slip("ndb_report_thresh_binlog_epoch_slip",
                                        &ndb_report_thresh_binlog_epoch_slip);
sys_var_long_ptr
sys_ndb_report_thresh_binlog_mem_usage("ndb_report_thresh_binlog_mem_usage",
                                       &ndb_report_thresh_binlog_mem_usage);
#endif
534
sys_var_thd_bool
535
sys_ndb_use_exact_count("ndb_use_exact_count", &SV::ndb_use_exact_count);
536
sys_var_thd_bool
537
sys_ndb_use_transactions("ndb_use_transactions", &SV::ndb_use_transactions);
538 539
sys_var_long_ptr
sys_ndb_cache_check_time("ndb_cache_check_time", &ndb_cache_check_time);
540 541 542 543 544 545 546 547 548
sys_var_thd_bool
sys_ndb_index_stat_enable("ndb_index_stat_enable",
                          &SV::ndb_index_stat_enable);
sys_var_thd_ulong
sys_ndb_index_stat_cache_entries("ndb_index_stat_cache_entries",
                                 &SV::ndb_index_stat_cache_entries);
sys_var_thd_ulong
sys_ndb_index_stat_update_freq("ndb_index_stat_update_freq",
                               &SV::ndb_index_stat_update_freq);
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
549 550
sys_var_long_ptr
sys_ndb_extra_logging("ndb_extra_logging", &ndb_extra_logging);
551 552
sys_var_thd_bool
sys_ndb_use_copying_alter_table("ndb_use_copying_alter_table", &SV::ndb_use_copying_alter_table);
553 554 555 556 557

/* Time/date/datetime formats */

sys_var_thd_date_time_format sys_time_format("time_format",
					     &SV::time_format,
558
					     MYSQL_TIMESTAMP_TIME);
559 560
sys_var_thd_date_time_format sys_date_format("date_format",
					     &SV::date_format,
561
					     MYSQL_TIMESTAMP_DATE);
562 563
sys_var_thd_date_time_format sys_datetime_format("datetime_format",
						 &SV::datetime_format,
564
						 MYSQL_TIMESTAMP_DATETIME);
565 566

/* Variables that are bits in THD */
567

568 569 570 571
sys_var_thd_bit sys_autocommit("autocommit", 0,
                               set_option_autocommit,
                               OPTION_NOT_AUTOCOMMIT,
                               1);
572
static sys_var_thd_bit	sys_big_tables("big_tables", 0,
573 574 575
				       set_option_bit,
				       OPTION_BIG_TABLES);
#ifndef TO_BE_DELETED	/* Alias for big_tables */
576
static sys_var_thd_bit	sys_sql_big_tables("sql_big_tables", 0,
577 578 579
					   set_option_bit,
					   OPTION_BIG_TABLES);
#endif
580
static sys_var_thd_bit	sys_big_selects("sql_big_selects", 0,
581
					set_option_bit,
582
					OPTION_BIG_SELECTS);
583 584
static sys_var_thd_bit	sys_log_off("sql_log_off",
				    check_log_update,
585 586 587
				    set_option_bit,
				    OPTION_LOG_OFF);
static sys_var_thd_bit	sys_log_update("sql_log_update",
588
                                       check_log_update,
589
				       set_log_update,
590
				       OPTION_BIN_LOG);
591
static sys_var_thd_bit	sys_log_binlog("sql_log_bin",
592
                                       check_log_update,
593
				       set_option_bit,
594 595
				       OPTION_BIN_LOG);
static sys_var_thd_bit	sys_sql_warnings("sql_warnings", 0,
596 597
					 set_option_bit,
					 OPTION_WARNINGS);
598 599
static sys_var_thd_bit	sys_sql_notes("sql_notes", 0,
					 set_option_bit,
600
					 OPTION_SQL_NOTES);
601
static sys_var_thd_bit	sys_auto_is_null("sql_auto_is_null", 0,
602 603
					 set_option_bit,
					 OPTION_AUTO_IS_NULL);
604
static sys_var_thd_bit	sys_safe_updates("sql_safe_updates", 0,
605 606
					 set_option_bit,
					 OPTION_SAFE_UPDATES);
607
static sys_var_thd_bit	sys_buffer_results("sql_buffer_result", 0,
608 609
					   set_option_bit,
					   OPTION_BUFFER_RESULT);
610
static sys_var_thd_bit	sys_quote_show_create("sql_quote_show_create", 0,
611 612
					      set_option_bit,
					      OPTION_QUOTE_SHOW_CREATE);
613
static sys_var_thd_bit	sys_foreign_key_checks("foreign_key_checks", 0,
614 615 616
					       set_option_bit,
					       OPTION_NO_FOREIGN_KEY_CHECKS,
					       1);
617
static sys_var_thd_bit	sys_unique_checks("unique_checks", 0,
618 619 620 621
					  set_option_bit,
					  OPTION_RELAXED_UNIQUE_CHECKS,
					  1);

622 623
/* Local state variables */

624
static sys_var_thd_ha_rows	sys_select_limit("sql_select_limit",
625 626 627 628
						 &SV::select_limit);
static sys_var_timestamp	sys_timestamp("timestamp");
static sys_var_last_insert_id	sys_last_insert_id("last_insert_id");
static sys_var_last_insert_id	sys_identity("identity");
bar@mysql.com's avatar
bar@mysql.com committed
629 630 631

static sys_var_thd_lc_time_names       sys_lc_time_names("lc_time_names");

632
static sys_var_insert_id	sys_insert_id("insert_id");
633 634 635 636 637 638 639 640 641
static sys_var_readonly		sys_error_count("error_count",
						OPT_SESSION,
						SHOW_LONG,
						get_error_count);
static sys_var_readonly		sys_warning_count("warning_count",
						  OPT_SESSION,
						  SHOW_LONG,
						  get_warning_count);

642
/* alias for last_insert_id() to be compatible with Sybase */
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
643
#ifdef HAVE_REPLICATION
644
static sys_var_slave_skip_counter sys_slave_skip_counter("sql_slave_skip_counter");
645
#endif
646 647
static sys_var_rand_seed1	sys_rand_seed1("rand_seed1");
static sys_var_rand_seed2	sys_rand_seed2("rand_seed2");
648

649 650
static sys_var_thd_ulong        sys_default_week_format("default_week_format",
					                &SV::default_week_format);
651

652 653 654
sys_var_thd_ulong               sys_group_concat_max_len("group_concat_max_len",
                                                         &SV::group_concat_max_len);

655
sys_var_thd_time_zone            sys_time_zone("time_zone");
656 657 658

/* Read only variables */

elliot@mysql.com's avatar
elliot@mysql.com committed
659 660 661 662 663 664
sys_var_have_variable sys_have_archive_db("have_archive", &have_archive_db);
sys_var_have_variable sys_have_blackhole_db("have_blackhole_engine",
                                            &have_blackhole_db);
sys_var_have_variable sys_have_compress("have_compress", &have_compress);
sys_var_have_variable sys_have_crypt("have_crypt", &have_crypt);
sys_var_have_variable sys_have_csv_db("have_csv", &have_csv_db);
665
sys_var_have_variable sys_have_dlopen("have_dynamic_loading", &have_dlopen);
elliot@mysql.com's avatar
elliot@mysql.com committed
666 667 668 669 670 671
sys_var_have_variable sys_have_example_db("have_example_engine",
                                          &have_example_db);
sys_var_have_variable sys_have_federated_db("have_federated_engine",
                                            &have_federated_db);
sys_var_have_variable sys_have_geometry("have_geometry", &have_geometry);
sys_var_have_variable sys_have_innodb("have_innodb", &have_innodb);
672
sys_var_have_variable sys_have_merge_db("have_merge", &have_merge_db);
elliot@mysql.com's avatar
elliot@mysql.com committed
673 674 675 676 677 678 679 680 681
sys_var_have_variable sys_have_ndbcluster("have_ndbcluster", &have_ndbcluster);
sys_var_have_variable sys_have_openssl("have_openssl", &have_openssl);
sys_var_have_variable sys_have_partition_db("have_partitioning",
                                            &have_partition_db);
sys_var_have_variable sys_have_query_cache("have_query_cache",
                                           &have_query_cache);
sys_var_have_variable sys_have_rtree_keys("have_rtree_keys", &have_rtree_keys);
sys_var_have_variable sys_have_symlink("have_symlink", &have_symlink);
sys_var_have_variable sys_have_row_based_replication("have_row_based_replication",&have_row_based_replication);
682 683
/* Global read-only variable describing server license */
sys_var_const_str		sys_license("license", STRINGIFY_ARG(LICENSE));
684

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
/* Global variables which enable|disable logging */
sys_var_log_state sys_var_general_log("general_log", &opt_log,
                                      QUERY_LOG_GENERAL);
sys_var_log_state sys_var_slow_query_log("slow_query_log", &opt_slow_log,
                                         QUERY_LOG_SLOW);
sys_var_str sys_var_general_log_path("general_log_file", sys_check_log_path,
				     sys_update_general_log_path,
				     sys_default_general_log_path,
				     opt_logname);
sys_var_str sys_var_slow_log_path("slow_query_log_file", sys_check_log_path,
				  sys_update_slow_log_path, 
				  sys_default_slow_log_path,
				  opt_slow_logname);
sys_var_log_output sys_var_log_output_state("log_output", &log_output_options,
					    &log_output_typelib, 0);

701
#ifdef HAVE_REPLICATION
elliot@mysql.com's avatar
elliot@mysql.com committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
static int show_slave_skip_errors(THD *thd, SHOW_VAR *var, char *buff)
{
  var->type=SHOW_CHAR;
  var->value= buff;
  if (!use_slave_mask || bitmap_is_clear_all(&slave_error_mask))
  {
    var->value= const_cast<char *>("OFF");
  }
  else if (bitmap_is_set_all(&slave_error_mask))
  {
    var->value= const_cast<char *>("ALL");
  }
  else
  {
    /* 10 is enough assuming errors are max 4 digits */
    int i;
    var->value= buff;
    for (i= 1;
         i < MAX_SLAVE_ERROR &&
         (buff - var->value) < SHOW_VAR_FUNC_BUFF_SIZE;
         i++)
    {
      if (bitmap_is_set(&slave_error_mask, i))
      {
        buff= int10_to_str(i, buff, 10);
        *buff++= ',';
      }
    }
    if (var->value != buff)
      buff--;				// Remove last ','
    if (i < MAX_SLAVE_ERROR)
      buff= strmov(buff, "...");  // Couldn't show all errors
    *buff=0;
  }
  return 0;
}
#endif /* HAVE_REPLICATION */
739

740
/*
741
  Variables shown by SHOW VARIABLES in alphabetical order
742 743
*/

744
SHOW_VAR init_vars[]= {
745
  {"auto_increment_increment", (char*) &sys_auto_increment_increment, SHOW_SYS},
746
  {"auto_increment_offset",   (char*) &sys_auto_increment_offset, SHOW_SYS},
747
  {sys_automatic_sp_privileges.name,(char*) &sys_automatic_sp_privileges,       SHOW_SYS},
748
  {"back_log",                (char*) &back_log,                    SHOW_LONG},
749
  {sys_basedir.name,          (char*) &sys_basedir,                 SHOW_SYS},
750
  {sys_binlog_cache_size.name,(char*) &sys_binlog_cache_size,	    SHOW_SYS},
751
  {sys_binlog_format.name,    (char*) &sys_binlog_format,	    SHOW_SYS},
752
  {sys_bulk_insert_buff_size.name,(char*) &sys_bulk_insert_buff_size,SHOW_SYS},
753
  {sys_character_set_client.name,(char*) &sys_character_set_client, SHOW_SYS},
754
  {sys_character_set_connection.name,(char*) &sys_character_set_connection,SHOW_SYS},
755
  {sys_character_set_database.name, (char*) &sys_character_set_database,SHOW_SYS},
756
  {sys_character_set_filesystem.name,(char*) &sys_character_set_filesystem, SHOW_SYS},
757
  {sys_character_set_results.name,(char*) &sys_character_set_results, SHOW_SYS},
758 759 760
  {sys_character_set_server.name, (char*) &sys_character_set_server,SHOW_SYS},
  {sys_charset_system.name,   (char*) &sys_charset_system,          SHOW_SYS},
  {"character_sets_dir",      mysql_charsets_dir,                   SHOW_CHAR},
761
  {sys_collation_connection.name,(char*) &sys_collation_connection, SHOW_SYS},
762 763
  {sys_collation_database.name,(char*) &sys_collation_database,     SHOW_SYS},
  {sys_collation_server.name,(char*) &sys_collation_server,         SHOW_SYS},
764
  {sys_completion_type.name,  (char*) &sys_completion_type,	    SHOW_SYS},
765 766
  {sys_concurrent_insert.name,(char*) &sys_concurrent_insert,       SHOW_SYS},
  {sys_connect_timeout.name,  (char*) &sys_connect_timeout,         SHOW_SYS},
767
  {sys_datadir.name,          (char*) &sys_datadir,                 SHOW_SYS},
768 769
  {sys_date_format.name,      (char*) &sys_date_format,		    SHOW_SYS},
  {sys_datetime_format.name,  (char*) &sys_datetime_format,	    SHOW_SYS},
serg@serg.mylan's avatar
serg@serg.mylan committed
770 771 772
#ifndef DBUG_OFF
  {sys_dbug.name,             (char*) &sys_dbug,                    SHOW_SYS},
#endif
773
  {sys_default_week_format.name, (char*) &sys_default_week_format,  SHOW_SYS},
774 775 776 777
  {sys_delay_key_write.name,  (char*) &sys_delay_key_write,         SHOW_SYS},
  {sys_delayed_insert_limit.name, (char*) &sys_delayed_insert_limit,SHOW_SYS},
  {sys_delayed_insert_timeout.name, (char*) &sys_delayed_insert_timeout, SHOW_SYS},
  {sys_delayed_queue_size.name,(char*) &sys_delayed_queue_size,     SHOW_SYS},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
778
  {sys_div_precincrement.name,(char*) &sys_div_precincrement,SHOW_SYS},
serg@serg.mylan's avatar
serg@serg.mylan committed
779
  {sys_engine_condition_pushdown.name,
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
780
   (char*) &sys_engine_condition_pushdown,                          SHOW_SYS},
781
  {sys_event_scheduler.name,  (char*) &sys_event_scheduler,         SHOW_SYS},
782
  {sys_expire_logs_days.name, (char*) &sys_expire_logs_days,        SHOW_SYS},
783 784
  {sys_flush.name,             (char*) &sys_flush,                  SHOW_SYS},
  {sys_flush_time.name,        (char*) &sys_flush_time,             SHOW_SYS},
785
  {sys_ft_boolean_syntax.name,(char*) &ft_boolean_syntax,	    SHOW_CHAR},
786
  {"ft_max_word_len",         (char*) &ft_max_word_len,             SHOW_LONG},
787
  {"ft_min_word_len",         (char*) &ft_min_word_len,             SHOW_LONG},
788
  {"ft_query_expansion_limit",(char*) &ft_query_expansion_limit,    SHOW_LONG},
789
  {"ft_stopword_file",        (char*) &ft_stopword_file,            SHOW_CHAR_PTR},
790 791
  {sys_var_general_log.name, (char*) &opt_log,                      SHOW_MY_BOOL},
  {sys_var_general_log_path.name, (char*) &sys_var_general_log_path,  SHOW_SYS},
792
  {sys_group_concat_max_len.name, (char*) &sys_group_concat_max_len,  SHOW_SYS},
793 794 795 796 797
  {sys_have_archive_db.name,  (char*) &have_archive_db,             SHOW_HAVE},
  {sys_have_blackhole_db.name,(char*) &have_blackhole_db,           SHOW_HAVE},
  {sys_have_compress.name,    (char*) &have_compress,               SHOW_HAVE},
  {sys_have_crypt.name,       (char*) &have_crypt,                  SHOW_HAVE},
  {sys_have_csv_db.name,      (char*) &have_csv_db,                 SHOW_HAVE},
798
  {sys_have_dlopen.name,      (char*) &have_dlopen,                 SHOW_HAVE},
799 800 801 802
  {sys_have_example_db.name,  (char*) &have_example_db,             SHOW_HAVE},
  {sys_have_federated_db.name,(char*) &have_federated_db,           SHOW_HAVE},
  {sys_have_geometry.name,    (char*) &have_geometry,               SHOW_HAVE},
  {sys_have_innodb.name,      (char*) &have_innodb,                 SHOW_HAVE},
803
  {sys_have_merge_db.name,    (char*) &have_merge_db,               SHOW_HAVE},
804 805 806 807
  {sys_have_ndbcluster.name,  (char*) &have_ndbcluster,             SHOW_HAVE},
  {sys_have_openssl.name,     (char*) &have_openssl,                SHOW_HAVE},
  {sys_have_partition_db.name,(char*) &have_partition_db,           SHOW_HAVE},
  {sys_have_query_cache.name, (char*) &have_query_cache,            SHOW_HAVE},
paul@snake-hub.snake.net's avatar
paul@snake-hub.snake.net committed
808
  {sys_have_row_based_replication.name, (char*) &have_row_based_replication, SHOW_HAVE},
809 810
  {sys_have_rtree_keys.name,  (char*) &have_rtree_keys,             SHOW_HAVE},
  {sys_have_symlink.name,     (char*) &have_symlink,                SHOW_HAVE},
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
811
  {"init_connect",            (char*) &sys_init_connect,            SHOW_SYS},
812
  {"init_file",               (char*) &opt_init_file,               SHOW_CHAR_PTR},
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
813
  {"init_slave",              (char*) &sys_init_slave,              SHOW_SYS},
814
#ifdef WITH_INNOBASE_STORAGE_ENGINE
815
  {"innodb_additional_mem_pool_size", (char*) &innobase_additional_mem_pool_size, SHOW_LONG },
816
  {sys_innodb_autoextend_increment.name, (char*) &sys_innodb_autoextend_increment, SHOW_SYS},
817
  {"innodb_buffer_pool_size", (char*) &innobase_buffer_pool_size, SHOW_LONGLONG },
818
  {"innodb_checksums", (char*) &innobase_use_checksums, SHOW_MY_BOOL},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
819
  {sys_innodb_commit_concurrency.name, (char*) &sys_innodb_commit_concurrency, SHOW_SYS},
820
  {sys_innodb_concurrency_tickets.name, (char*) &sys_innodb_concurrency_tickets, SHOW_SYS},
821 822
  {"innodb_data_file_path", (char*) &innobase_data_file_path,	    SHOW_CHAR_PTR},
  {"innodb_data_home_dir",  (char*) &innobase_data_home_dir,	    SHOW_CHAR_PTR},
823
  {"innodb_doublewrite", (char*) &innobase_use_doublewrite, SHOW_MY_BOOL},
824
  {sys_innodb_fast_shutdown.name,(char*) &sys_innodb_fast_shutdown, SHOW_SYS},
825
  {"innodb_file_io_threads", (char*) &innobase_file_io_threads, SHOW_LONG },
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
826
  {"innodb_file_per_table", (char*) &innobase_file_per_table, SHOW_MY_BOOL},
827
  {"innodb_flush_method",    (char*) &innobase_unix_file_flush_method, SHOW_CHAR_PTR},
828
  {"innodb_force_recovery", (char*) &innobase_force_recovery, SHOW_LONG },
829
  {"innodb_lock_wait_timeout", (char*) &innobase_lock_wait_timeout, SHOW_LONG },
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
830
  {"innodb_locks_unsafe_for_binlog", (char*) &innobase_locks_unsafe_for_binlog, SHOW_MY_BOOL},
831 832 833
  {"innodb_log_arch_dir",   (char*) &innobase_log_arch_dir, 	    SHOW_CHAR_PTR},
  {"innodb_log_archive",    (char*) &innobase_log_archive, 	    SHOW_MY_BOOL},
  {"innodb_log_buffer_size", (char*) &innobase_log_buffer_size, SHOW_LONG },
834
  {"innodb_log_file_size", (char*) &innobase_log_file_size, SHOW_LONGLONG},
835 836
  {"innodb_log_files_in_group", (char*) &innobase_log_files_in_group,	SHOW_LONG},
  {"innodb_log_group_home_dir", (char*) &innobase_log_group_home_dir, SHOW_CHAR_PTR},
837
  {sys_innodb_max_dirty_pages_pct.name, (char*) &sys_innodb_max_dirty_pages_pct, SHOW_SYS},
838
  {sys_innodb_max_purge_lag.name, (char*) &sys_innodb_max_purge_lag, SHOW_SYS},
839 840
  {"innodb_mirrored_log_groups", (char*) &innobase_mirrored_log_groups, SHOW_LONG},
  {"innodb_open_files", (char*) &innobase_open_files, SHOW_LONG },
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
841
  {sys_innodb_support_xa.name, (char*) &sys_innodb_support_xa, SHOW_SYS},
842
  {sys_innodb_sync_spin_loops.name, (char*) &sys_innodb_sync_spin_loops, SHOW_SYS},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
843
  {sys_innodb_table_locks.name, (char*) &sys_innodb_table_locks, SHOW_SYS},
844 845
  {sys_innodb_thread_concurrency.name, (char*) &sys_innodb_thread_concurrency, SHOW_SYS},
  {sys_innodb_thread_sleep_delay.name, (char*) &sys_innodb_thread_sleep_delay, SHOW_SYS},
846
  {sys_innodb_flush_log_at_trx_commit.name, (char*) &sys_innodb_flush_log_at_trx_commit, SHOW_SYS},
847
#endif
848 849 850
  {sys_interactive_timeout.name,(char*) &sys_interactive_timeout,   SHOW_SYS},
  {sys_join_buffer_size.name,   (char*) &sys_join_buffer_size,	    SHOW_SYS},
  {sys_key_buffer_size.name,	(char*) &sys_key_buffer_size,	    SHOW_SYS},
851 852
  {sys_key_cache_age_threshold.name,   (char*) &sys_key_cache_age_threshold,
                                                                    SHOW_SYS},
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
853
  {sys_key_cache_block_size.name,   (char*) &sys_key_cache_block_size,
854
                                                                    SHOW_SYS},
855
  {sys_key_cache_division_limit.name,   (char*) &sys_key_cache_division_limit,
856
                                                                    SHOW_SYS},
857
  {"language",                language,                             SHOW_CHAR},
858
  {"large_files_support",     (char*) &opt_large_files,             SHOW_BOOL},
859
  {"large_page_size",         (char*) &opt_large_page_size,         SHOW_INT},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
860
  {"large_pages",             (char*) &opt_large_pages,             SHOW_MY_BOOL},
bar@mysql.com's avatar
bar@mysql.com committed
861
  {sys_lc_time_names.name,    (char*) &sys_lc_time_names,           SHOW_SYS},
862
  {sys_license.name,	      (char*) &sys_license,                 SHOW_SYS},
863 864 865 866 867 868
  {sys_local_infile.name,     (char*) &sys_local_infile,	    SHOW_SYS},
#ifdef HAVE_MLOCKALL
  {"locked_in_memory",	      (char*) &locked_in_memory,	    SHOW_BOOL},
#endif
  {"log",                     (char*) &opt_log,                     SHOW_BOOL},
  {"log_bin",                 (char*) &opt_bin_log,                 SHOW_BOOL},
869
  {sys_trust_function_creators.name,(char*) &sys_trust_function_creators, SHOW_SYS},
870
  {"log_error",               (char*) log_error_file,               SHOW_CHAR},
871
  {sys_var_log_output_state.name, (char*) &sys_var_log_output_state, SHOW_SYS},
872 873
  {sys_log_queries_not_using_indexes.name,
    (char*) &sys_log_queries_not_using_indexes, SHOW_SYS},
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
874
#ifdef HAVE_REPLICATION
875
  {"log_slave_updates",       (char*) &opt_log_slave_updates,       SHOW_MY_BOOL},
876
#endif
877 878 879 880
  {"log_slow_queries",        (char*) &opt_slow_log,                SHOW_BOOL},
  {sys_log_warnings.name,     (char*) &sys_log_warnings,	    SHOW_SYS},
  {sys_long_query_time.name,  (char*) &sys_long_query_time, 	    SHOW_SYS},
  {sys_low_priority_updates.name, (char*) &sys_low_priority_updates, SHOW_SYS},
881
  {"lower_case_file_system",  (char*) &lower_case_file_system,      SHOW_MY_BOOL},
882
  {"lower_case_table_names",  (char*) &lower_case_table_names,      SHOW_INT},
883 884 885 886
  {sys_max_allowed_packet.name,(char*) &sys_max_allowed_packet,	    SHOW_SYS},
  {sys_max_binlog_cache_size.name,(char*) &sys_max_binlog_cache_size, SHOW_SYS},
  {sys_max_binlog_size.name,    (char*) &sys_max_binlog_size,	    SHOW_SYS},
  {sys_max_connect_errors.name, (char*) &sys_max_connect_errors,    SHOW_SYS},
887
  {sys_max_connections.name,    (char*) &sys_max_connections,	    SHOW_SYS},
888
  {sys_max_delayed_threads.name,(char*) &sys_max_delayed_threads,   SHOW_SYS},
889
  {sys_max_error_count.name,	(char*) &sys_max_error_count,	    SHOW_SYS},
890
  {sys_max_heap_table_size.name,(char*) &sys_max_heap_table_size,   SHOW_SYS},
891 892
  {sys_max_insert_delayed_threads.name,
   (char*) &sys_max_insert_delayed_threads,   SHOW_SYS},
893
  {sys_max_join_size.name,	(char*) &sys_max_join_size,	    SHOW_SYS},
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
894
  {sys_max_length_for_sort_data.name, (char*) &sys_max_length_for_sort_data,
igor@hundin.mysql.fi's avatar
igor@hundin.mysql.fi committed
895
   SHOW_SYS},
896 897
  {sys_max_prepared_stmt_count.name, (char*) &sys_max_prepared_stmt_count,
    SHOW_SYS},
898 899
  {sys_max_relay_log_size.name, (char*) &sys_max_relay_log_size,    SHOW_SYS},
  {sys_max_seeks_for_key.name,  (char*) &sys_max_seeks_for_key,	    SHOW_SYS},
900
  {sys_max_sort_length.name,	(char*) &sys_max_sort_length,	    SHOW_SYS},
901 902
  {sys_max_sp_recursion_depth.name,
    (char*) &sys_max_sp_recursion_depth, SHOW_SYS},
903
  {sys_max_tmp_tables.name,	(char*) &sys_max_tmp_tables,	    SHOW_SYS},
904
  {sys_max_user_connections.name,(char*) &sys_max_user_connections, SHOW_SYS},
905
  {sys_max_write_lock_count.name, (char*) &sys_max_write_lock_count,SHOW_SYS},
ingo@mysql.com's avatar
ingo@mysql.com committed
906
  {sys_multi_range_count.name,  (char*) &sys_multi_range_count,     SHOW_SYS},
serg@serg.mylan's avatar
serg@serg.mylan committed
907
  {sys_myisam_data_pointer_size.name, (char*) &sys_myisam_data_pointer_size, SHOW_SYS},
908 909
  {sys_myisam_max_sort_file_size.name, (char*) &sys_myisam_max_sort_file_size,
   SHOW_SYS},
910
  {"myisam_recover_options",  (char*) &myisam_recover_options_str,  SHOW_CHAR_PTR},
911
  {sys_myisam_repair_threads.name, (char*) &sys_myisam_repair_threads,
912 913
   SHOW_SYS},
  {sys_myisam_sort_buffer_size.name, (char*) &sys_myisam_sort_buffer_size, SHOW_SYS},
914 915
  
  {sys_myisam_stats_method.name, (char*) &sys_myisam_stats_method, SHOW_SYS},
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
916
  {sys_myisam_use_mmap.name, (char*) &sys_myisam_use_mmap, SHOW_SYS},
917
  
918
#ifdef __NT__
919
  {"named_pipe",	      (char*) &opt_enable_named_pipe,       SHOW_MY_BOOL},
920 921 922
#endif
  {sys_ndb_autoincrement_prefetch_sz.name,
   (char*) &sys_ndb_autoincrement_prefetch_sz,                      SHOW_SYS},
923
  {sys_ndb_cache_check_time.name,(char*) &sys_ndb_cache_check_time, SHOW_SYS},
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
924
  {sys_ndb_extra_logging.name,(char*) &sys_ndb_extra_logging,       SHOW_SYS},
925
  {sys_ndb_force_send.name,   (char*) &sys_ndb_force_send,          SHOW_SYS},
926 927 928
  {sys_ndb_index_stat_cache_entries.name, (char*) &sys_ndb_index_stat_cache_entries, SHOW_SYS},
  {sys_ndb_index_stat_enable.name, (char*) &sys_ndb_index_stat_enable, SHOW_SYS},
  {sys_ndb_index_stat_update_freq.name, (char*) &sys_ndb_index_stat_update_freq, SHOW_SYS},
tomas@poseidon.ndb.mysql.com's avatar
tomas@poseidon.ndb.mysql.com committed
929 930 931 932 933 934
#ifdef HAVE_NDB_BINLOG
  {sys_ndb_report_thresh_binlog_epoch_slip.name,
   (char*) &sys_ndb_report_thresh_binlog_epoch_slip,                SHOW_SYS},
  {sys_ndb_report_thresh_binlog_mem_usage.name,
   (char*) &sys_ndb_report_thresh_binlog_mem_usage,                 SHOW_SYS},
#endif
935 936
  {sys_ndb_use_copying_alter_table.name,
   (char*) &sys_ndb_use_copying_alter_table,                        SHOW_SYS},
937 938
  {sys_ndb_use_exact_count.name,(char*) &sys_ndb_use_exact_count,   SHOW_SYS},
  {sys_ndb_use_transactions.name,(char*) &sys_ndb_use_transactions, SHOW_SYS},
939 940
  {sys_net_buffer_length.name,(char*) &sys_net_buffer_length,       SHOW_SYS},
  {sys_net_read_timeout.name, (char*) &sys_net_read_timeout,        SHOW_SYS},
941
  {sys_net_retry_count.name,  (char*) &sys_net_retry_count,	    SHOW_SYS},
942
  {sys_net_write_timeout.name,(char*) &sys_net_write_timeout,       SHOW_SYS},
943
  {sys_new_mode.name,         (char*) &sys_new_mode,                SHOW_SYS},
944
  {sys_old_alter_table.name,  (char*) &sys_old_alter_table,         SHOW_SYS},
945
  {sys_old_passwords.name,    (char*) &sys_old_passwords,           SHOW_SYS},
946
  {"open_files_limit",	      (char*) &open_files_limit,	    SHOW_LONG},
947 948 949 950
  {sys_optimizer_prune_level.name, (char*) &sys_optimizer_prune_level,
   SHOW_SYS},
  {sys_optimizer_search_depth.name,(char*) &sys_optimizer_search_depth,
   SHOW_SYS},
951
  {"pid_file",                (char*) pidfile_name,                 SHOW_CHAR},
952
  {"plugin_dir",              (char*) opt_plugin_dir,               SHOW_CHAR},
953
  {"port",                    (char*) &mysqld_port,                  SHOW_INT},
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
954
  {sys_preload_buff_size.name, (char*) &sys_preload_buff_size,      SHOW_SYS},
955
  {"protocol_version",        (char*) &protocol_version,            SHOW_INT},
956 957
  {sys_query_alloc_block_size.name, (char*) &sys_query_alloc_block_size,
   SHOW_SYS},
958
#ifdef HAVE_QUERY_CACHE
959
  {sys_query_cache_limit.name,(char*) &sys_query_cache_limit,	    SHOW_SYS},
960 961
  {sys_query_cache_min_res_unit.name, (char*) &sys_query_cache_min_res_unit,
   SHOW_SYS},
962 963
  {sys_query_cache_size.name, (char*) &sys_query_cache_size,	    SHOW_SYS},
  {sys_query_cache_type.name, (char*) &sys_query_cache_type,        SHOW_SYS},
paul@ice.snake.net's avatar
paul@ice.snake.net committed
964 965
  {sys_query_cache_wlock_invalidate.name,
   (char *) &sys_query_cache_wlock_invalidate, SHOW_SYS},
966
#endif /* HAVE_QUERY_CACHE */
967
  {sys_query_prealloc_size.name, (char*) &sys_query_prealloc_size,  SHOW_SYS},
968 969
  {sys_range_alloc_block_size.name, (char*) &sys_range_alloc_block_size,
   SHOW_SYS},
serg@serg.mylan's avatar
serg@serg.mylan committed
970 971 972
  {sys_read_buff_size.name,   (char*) &sys_read_buff_size,	    SHOW_SYS},
  {sys_readonly.name,         (char*) &sys_readonly,                SHOW_SYS},
  {sys_read_rnd_buff_size.name,(char*) &sys_read_rnd_buff_size,	    SHOW_SYS},
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
973 974
#ifdef HAVE_REPLICATION
  {sys_relay_log_purge.name,  (char*) &sys_relay_log_purge,         SHOW_SYS},
975
  {"relay_log_space_limit",  (char*) &relay_log_space_limit,        SHOW_LONGLONG},
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
976
#endif
serg@serg.mylan's avatar
serg@serg.mylan committed
977
  {sys_rpl_recovery_rank.name,(char*) &sys_rpl_recovery_rank,       SHOW_SYS},
978
  {"secure_auth",             (char*) &sys_secure_auth,             SHOW_SYS},
979 980 981 982
#ifdef HAVE_SMEM
  {"shared_memory",           (char*) &opt_enable_shared_memory,    SHOW_MY_BOOL},
  {"shared_memory_base_name", (char*) &shared_memory_base_name,     SHOW_CHAR_PTR},
#endif
983
  {sys_server_id.name,	      (char*) &sys_server_id,		    SHOW_SYS},
984
  {"skip_external_locking",   (char*) &my_disable_locking,          SHOW_MY_BOOL},
985 986
  {"skip_networking",         (char*) &opt_disable_networking,      SHOW_BOOL},
  {"skip_show_database",      (char*) &opt_skip_show_db,            SHOW_BOOL},
987
#ifdef HAVE_REPLICATION
988 989 990
  {sys_slave_compressed_protocol.name,
    (char*) &sys_slave_compressed_protocol,           SHOW_SYS},
  {"slave_load_tmpdir",       (char*) &slave_load_tmpdir,           SHOW_CHAR_PTR},
991
  {sys_slave_net_timeout.name,(char*) &sys_slave_net_timeout,	    SHOW_SYS},
992
  {"slave_skip_errors",       (char*) &show_slave_skip_errors,      SHOW_FUNC},
993
  {sys_slave_trans_retries.name,(char*) &sys_slave_trans_retries,   SHOW_SYS},
994
#endif
995
  {sys_slow_launch_time.name, (char*) &sys_slow_launch_time,        SHOW_SYS},
996 997
  {sys_var_slow_query_log.name, (char*) &opt_slow_log,              SHOW_MY_BOOL},
  {sys_var_slow_log_path.name, (char*) &sys_var_slow_log_path,      SHOW_SYS},
998
#ifdef HAVE_SYS_UN_H
999
  {"socket",                  (char*) &mysqld_unix_port,             SHOW_CHAR_PTR},
1000
#endif
1001 1002
  {sys_sort_buffer.name,      (char*) &sys_sort_buffer,             SHOW_SYS},
  {sys_big_selects.name,      (char*) &sys_big_selects,             SHOW_SYS},
1003
  {sys_sql_mode.name,         (char*) &sys_sql_mode,                SHOW_SYS},
1004 1005
  {"sql_notes",               (char*) &sys_sql_notes,               SHOW_SYS},
  {"sql_warnings",            (char*) &sys_sql_warnings,            SHOW_SYS},
1006 1007 1008 1009 1010
  {sys_ssl_ca.name,           (char*) &sys_ssl_ca,                  SHOW_SYS},
  {sys_ssl_capath.name,       (char*) &sys_ssl_capath,              SHOW_SYS},
  {sys_ssl_cert.name,         (char*) &sys_ssl_cert,                SHOW_SYS},
  {sys_ssl_cipher.name,       (char*) &sys_ssl_cipher,              SHOW_SYS},
  {sys_ssl_key.name,          (char*) &sys_ssl_key,                 SHOW_SYS},
paul@frost.snake.net's avatar
paul@frost.snake.net committed
1011
  {sys_storage_engine.name,   (char*) &sys_storage_engine,          SHOW_SYS},
1012 1013
#ifdef HAVE_REPLICATION
  {sys_sync_binlog_period.name,(char*) &sys_sync_binlog_period,     SHOW_SYS},
paul@frost.snake.net's avatar
paul@frost.snake.net committed
1014 1015
#endif
  {sys_sync_frm.name,         (char*) &sys_sync_frm,               SHOW_SYS},
1016 1017 1018
#ifdef HAVE_TZNAME
  {"system_time_zone",        system_time_zone,                     SHOW_CHAR},
#endif
1019
  {"table_definition_cache",  (char*) &table_def_size,              SHOW_LONG},
1020
  {"table_lock_wait_timeout", (char*) &table_lock_wait_timeout,     SHOW_LONG },
paul@kite-hub.kitebird.com's avatar
paul@kite-hub.kitebird.com committed
1021
  {"table_open_cache",        (char*) &table_cache_size,            SHOW_LONG},
1022 1023 1024 1025 1026 1027
  {sys_table_type.name,	      (char*) &sys_table_type,	            SHOW_SYS},
  {sys_thread_cache_size.name,(char*) &sys_thread_cache_size,       SHOW_SYS},
#ifdef HAVE_THR_SETCONCURRENCY
  {"thread_concurrency",      (char*) &concurrency,                 SHOW_LONG},
#endif
  {"thread_stack",            (char*) &thread_stack,                SHOW_LONG},
1028
  {sys_time_format.name,      (char*) &sys_time_format,		    SHOW_SYS},
1029
  {"time_zone",               (char*) &sys_time_zone,               SHOW_SYS},
vtkachenko@intelp4d.mysql.com's avatar
vtkachenko@intelp4d.mysql.com committed
1030
  {sys_timed_mutexes.name,    (char*) &sys_timed_mutexes,       SHOW_SYS},
1031
  {sys_tmp_table_size.name,   (char*) &sys_tmp_table_size,	    SHOW_SYS},
1032
  {sys_tmpdir.name,           (char*) &sys_tmpdir,	            SHOW_SYS},
1033 1034 1035
  {sys_trans_alloc_block_size.name, (char*) &sys_trans_alloc_block_size,
   SHOW_SYS},
  {sys_trans_prealloc_size.name, (char*) &sys_trans_prealloc_size,  SHOW_SYS},
1036
  {sys_tx_isolation.name,     (char*) &sys_tx_isolation,	    SHOW_SYS},
1037 1038
  {sys_updatable_views_with_limit.name,
                              (char*) &sys_updatable_views_with_limit,SHOW_SYS},
1039 1040 1041 1042 1043
  {sys_version.name,          (char*) &sys_version,                 SHOW_SYS},
  {sys_version_comment.name,  (char*) &sys_version_comment,         SHOW_SYS},
  {sys_version_compile_machine.name, (char*) &sys_version_compile_machine,
   SHOW_SYS},
  {sys_version_compile_os.name,	(char*) &sys_version_compile_os,    SHOW_SYS},
1044 1045 1046 1047
  {sys_net_wait_timeout.name, (char*) &sys_net_wait_timeout,	    SHOW_SYS},
  {NullS, NullS, SHOW_LONG}
};

1048

1049 1050 1051 1052 1053 1054
bool sys_var::check(THD *thd, set_var *var)
{
  var->save_result.ulonglong_value= var->value->val_int();
  return 0;
}

1055 1056 1057 1058 1059 1060 1061
bool sys_var_str::check(THD *thd, set_var *var)
{
  int res;
  if (!check_func)
    return 0;

  if ((res=(*check_func)(thd, var)) < 0)
1062 1063
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0),
             name, var->value->str_value.ptr());
1064 1065 1066
  return res;
}

1067 1068 1069 1070
/*
  Functions to check and update variables
*/

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

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
/*
  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
1082
{
1083 1084 1085 1086
  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*) "";
1087
  if (!(res= my_strndup(old_value, new_length, MYF(0))))
1088
    return 1;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1089 1090 1091 1092
  /*
    Replace the old value in such a way that the any thread using
    the value will work.
  */
1093 1094 1095 1096 1097
  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
1098 1099 1100 1101 1102
  my_free(old_value, MYF(MY_ALLOW_ZERO_PTR));
  return 0;
}


1103 1104 1105 1106 1107 1108
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
1109 1110
static void sys_default_init_connect(THD* thd, enum_var_type type)
{
1111
  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
1112 1113 1114 1115 1116
}


static bool sys_update_init_slave(THD *thd, set_var *var)
{
1117
  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
1118 1119 1120 1121 1122
}


static void sys_default_init_slave(THD* thd, enum_var_type type)
{
1123
  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
1124 1125
}

1126 1127
static int sys_check_ftb_syntax(THD *thd,  set_var *var)
{
1128 1129 1130 1131
  if (thd->security_ctx->master_access & SUPER_ACL)
    return (ft_boolean_check_syntax_string((byte*)
                                           var->value->str_value.c_ptr()) ?
            -1 : 0);
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
  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);
  return 0;
}

static void sys_default_ftb_syntax(THD *thd, enum_var_type type)
{
1148
  strmake(ft_boolean_syntax, def_ft_boolean_syntax,
1149 1150
	  sizeof(ft_boolean_syntax)-1);
}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
1151

1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

/*
  If one sets the LOW_PRIORIY UPDATES flag, we also must change the
  used lock type
*/

static void fix_low_priority_updates(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
    thd->update_lock_default= (thd->variables.low_priority_updates ?
			       TL_WRITE_LOW_PRIORITY : TL_WRITE);
}


1166 1167 1168 1169 1170 1171 1172
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;
}

1173 1174 1175 1176 1177 1178 1179 1180
/*
  Set the OPTION_BIG_SELECTS flag if max_join_size == HA_POS_ERROR
*/

static void fix_max_join_size(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
  {
1181
    if (thd->variables.max_join_size == HA_POS_ERROR)
1182 1183 1184 1185 1186
      thd->options|= OPTION_BIG_SELECTS;
    else
      thd->options&= ~OPTION_BIG_SELECTS;
  }
}
1187

1188

1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
/*
  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;
}

1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
/*
  If one doesn't use the SESSION modifier, the isolation level
  is only active for the next command
*/
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);
}

1214 1215
static void fix_completion_type(THD *thd __attribute__((unused)),
				enum_var_type type __attribute__((unused))) {}
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

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

1229 1230 1231 1232 1233

/*
  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
1234
#ifdef HAVE_REPLICATION
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
static void fix_net_read_timeout(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
    thd->net.read_timeout=thd->variables.net_read_timeout;
}


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

1248 1249 1250 1251 1252
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
1253
#else /* HAVE_REPLICATION */
1254 1255
static void fix_net_read_timeout(THD *thd __attribute__((unused)),
				 enum_var_type type __attribute__((unused)))
1256
{}
1257 1258
static void fix_net_write_timeout(THD *thd __attribute__((unused)),
				  enum_var_type type __attribute__((unused)))
1259
{}
1260 1261
static void fix_net_retry_count(THD *thd __attribute__((unused)),
				enum_var_type type __attribute__((unused)))
1262
{}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
1263
#endif /* HAVE_REPLICATION */
1264

1265 1266 1267 1268

static void fix_query_cache_size(THD *thd, enum_var_type type)
{
#ifdef HAVE_QUERY_CACHE
1269
  ulong requested= query_cache_size;
1270
  query_cache.resize(query_cache_size);
1271 1272 1273 1274
  if (requested != query_cache_size)
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
			ER_WARN_QC_RESIZE, ER(ER_WARN_QC_RESIZE),
			requested, query_cache_size);
1275 1276 1277 1278
#endif
}


1279 1280 1281 1282 1283 1284 1285 1286 1287
#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


1288
extern void fix_delay_key_write(THD *thd, enum_var_type type)
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
{
  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;
  }
}

1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317

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).
1318 1319 1320

    If we don't have row-based replication compiled in, the variable
    is always read-only.
1321
  */
1322 1323 1324 1325
#ifndef HAVE_ROW_BASED_REPLICATION
  my_error(ER_RBR_NOT_AVAILABLE, MYF(0));
  return 1;
#else
1326 1327 1328 1329 1330 1331 1332
  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;
  }
  /*
1333
    if in a stored function/trigger, it's too late to change mode
1334
  */
1335
  if (thd->in_sub_stmt)
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
  {
    my_error(ER_STORED_FUNCTION_PREVENTS_SWITCH_BINLOG_FORMAT, MYF(0));
    return 1;    
  }
#ifdef HAVE_NDB_BINLOG
  /*
    Cluster does not support changing the binlog format on the fly yet.
  */
  if (opt_bin_log && (have_ndbcluster == SHOW_OPTION_YES))
  {
    my_error(ER_NDB_CANT_SWITCH_BINLOG_FORMAT, MYF(0));
    return 1;
  }
lars@mysql.com's avatar
lars@mysql.com committed
1349 1350
#endif /* HAVE_NDB_BINLOG */
#endif /* HAVE_ROW_BASED_REPLICATION */
1351 1352 1353
  return sys_var_thd_enum::is_readonly();
}

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

1355 1356
void fix_binlog_format_after_update(THD *thd, enum_var_type type)
{
1357
#ifdef HAVE_ROW_BASED_REPLICATION
1358
  thd->reset_current_stmt_binlog_row_based();
1359
#endif /*HAVE_ROW_BASED_REPLICATION*/
1360 1361 1362
}


1363
static void fix_max_binlog_size(THD *thd, enum_var_type type)
1364 1365 1366 1367 1368
{
  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);
1369
#ifdef HAVE_REPLICATION
1370 1371
  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
1372
#endif
1373 1374 1375
  DBUG_VOID_RETURN;
}

1376
static void fix_max_relay_log_size(THD *thd, enum_var_type type)
1377 1378 1379 1380
{
  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));
1381
#ifdef HAVE_REPLICATION
1382 1383
  active_mi->rli.relay_log.set_max_size(max_relay_log_size ?
                                        max_relay_log_size: max_binlog_size);
1384
#endif
1385 1386
  DBUG_VOID_RETURN;
}
1387

1388

1389 1390
static int check_max_delayed_threads(THD *thd, set_var *var)
{
monty@mishka.local's avatar
monty@mishka.local committed
1391
  longlong val= var->value->val_int();
1392
  if (var->type != OPT_GLOBAL && val != 0 &&
monty@mishka.local's avatar
monty@mishka.local committed
1393
      val != (longlong) global_system_variables.max_insert_delayed_threads)
1394 1395
  {
    char buf[64];
1396
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->var->name, llstr(val, buf));
1397 1398 1399 1400 1401
    return 1;
  }
  return 0;
}

1402
static void fix_max_connections(THD *thd, enum_var_type type)
1403
{
1404
#ifndef EMBEDDED_LIBRARY
1405 1406
  resize_thr_alarm(max_connections + 
		   global_system_variables.max_insert_delayed_threads + 10);
1407
#endif
1408 1409 1410 1411 1412 1413
}


static void fix_thd_mem_root(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
1414
    reset_root_defaults(thd->mem_root,
1415 1416 1417 1418 1419 1420 1421
                        thd->variables.query_alloc_block_size,
                        thd->variables.query_prealloc_size);
}


static void fix_trans_mem_root(THD *thd, enum_var_type type)
{
1422
#ifdef USING_TRANSACTIONS
1423 1424 1425 1426
  if (type != OPT_GLOBAL)
    reset_root_defaults(&thd->transaction.mem_root,
                        thd->variables.trans_alloc_block_size,
                        thd->variables.trans_prealloc_size);
1427
#endif
1428 1429
}

1430

1431 1432 1433 1434
static void fix_server_id(THD *thd, enum_var_type type)
{
  server_id_supplied = 1;
}
1435

1436 1437 1438 1439 1440 1441 1442 1443 1444 1445

sys_var_long_ptr::
sys_var_long_ptr(const char *name_arg, ulong *value_ptr,
                 sys_after_update_func after_update_arg)
  :sys_var_long_ptr_global(name_arg, value_ptr,
                           &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
1446 1447 1448 1449 1450
{
  longlong v= var->value->val_int();
  var->save_result.ulonglong_value= v < 0 ? 0 : v;
  return 0;
}
1451

1452
bool sys_var_long_ptr_global::update(THD *thd, set_var *var)
1453
{
1454
  ulonglong tmp= var->save_result.ulonglong_value;
1455
  pthread_mutex_lock(guard);
1456 1457 1458 1459
  if (option_limits)
    *value= (ulong) getopt_ull_limit_value(tmp, option_limits);
  else
    *value= (ulong) tmp;
1460
  pthread_mutex_unlock(guard);
1461 1462 1463 1464
  return 0;
}


1465
void sys_var_long_ptr_global::set_default(THD *thd, enum_var_type type)
1466
{
1467
  pthread_mutex_lock(guard);
1468
  *value= (ulong) option_limits->def_value;
1469
  pthread_mutex_unlock(guard);
1470 1471 1472
}


1473 1474
bool sys_var_ulonglong_ptr::update(THD *thd, set_var *var)
{
1475
  ulonglong tmp= var->save_result.ulonglong_value;
1476
  pthread_mutex_lock(&LOCK_global_system_variables);
1477 1478 1479 1480
  if (option_limits)
    *value= (ulonglong) getopt_ull_limit_value(tmp, option_limits);
  else
    *value= (ulonglong) tmp;
1481
  pthread_mutex_unlock(&LOCK_global_system_variables);
1482 1483 1484 1485 1486 1487
  return 0;
}


void sys_var_ulonglong_ptr::set_default(THD *thd, enum_var_type type)
{
1488
  pthread_mutex_lock(&LOCK_global_system_variables);
1489
  *value= (ulonglong) option_limits->def_value;
1490
  pthread_mutex_unlock(&LOCK_global_system_variables);
1491 1492 1493
}


1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
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;
}


1507 1508 1509 1510 1511 1512 1513
bool sys_var_enum::update(THD *thd, set_var *var)
{
  *value= (uint) var->save_result.ulong_value;
  return 0;
}


1514
byte *sys_var_enum::value_ptr(THD *thd, enum_var_type type, LEX_STRING *base)
1515 1516 1517 1518
{
  return (byte*) enum_names->type_names[*value];
}

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

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

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

1533 1534 1535 1536 1537 1538
#if SIZEOF_LONG == 4
  /* Avoid overflows on 32 bit systems */
  if (tmp > (ulonglong) ~(ulong) 0)
    tmp= ((ulonglong) ~(ulong) 0);
#endif

1539 1540 1541
  if (option_limits)
    tmp= (ulong) getopt_ull_limit_value(tmp, option_limits);
  if (var->type == OPT_GLOBAL)
1542
    global_system_variables.*offset= (ulong) tmp;
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
  else
    thd->variables.*offset= (ulong) tmp;
  return 0;
}


void sys_var_thd_ulong::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
  {
    /* We will not come here if option_limits is not set */
    global_system_variables.*offset= (ulong) option_limits->def_value;
  }
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1561 1562
byte *sys_var_thd_ulong::value_ptr(THD *thd, enum_var_type type,
				   LEX_STRING *base)
1563 1564 1565 1566 1567 1568 1569
{
  if (type == OPT_GLOBAL)
    return (byte*) &(global_system_variables.*offset);
  return (byte*) &(thd->variables.*offset);
}


1570 1571
bool sys_var_thd_ha_rows::update(THD *thd, set_var *var)
{
1572
  ulonglong tmp= var->save_result.ulonglong_value;
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606

  /* 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)
    tmp= (ha_rows) getopt_ull_limit_value(tmp, option_limits);
  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)
  {
    /* We will not come here if option_limits is not set */
    pthread_mutex_lock(&LOCK_global_system_variables);
    global_system_variables.*offset= (ha_rows) option_limits->def_value;
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1607 1608
byte *sys_var_thd_ha_rows::value_ptr(THD *thd, enum_var_type type,
				     LEX_STRING *base)
1609 1610 1611 1612 1613 1614
{
  if (type == OPT_GLOBAL)
    return (byte*) &(global_system_variables.*offset);
  return (byte*) &(thd->variables.*offset);
}

1615 1616
bool sys_var_thd_ulonglong::update(THD *thd,  set_var *var)
{
1617
  ulonglong tmp= var->save_result.ulonglong_value;
1618

1619
  if (tmp > max_system_variables.*offset)
1620 1621 1622
    tmp= max_system_variables.*offset;

  if (option_limits)
1623
    tmp= getopt_ull_limit_value(tmp, option_limits);
1624
  if (var->type == OPT_GLOBAL)
1625 1626 1627
  {
    /* Lock is needed to make things safe on 32 bit systems */
    pthread_mutex_lock(&LOCK_global_system_variables);
1628
    global_system_variables.*offset= (ulonglong) tmp;
1629 1630
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
1631
  else
1632
    thd->variables.*offset= (ulonglong) tmp;
1633 1634 1635 1636 1637 1638 1639
  return 0;
}


void sys_var_thd_ulonglong::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
1640 1641
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
1642
    global_system_variables.*offset= (ulonglong) option_limits->def_value;
1643 1644
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
1645 1646 1647 1648 1649
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1650 1651
byte *sys_var_thd_ulonglong::value_ptr(THD *thd, enum_var_type type,
				       LEX_STRING *base)
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
{
  if (type == OPT_GLOBAL)
    return (byte*) &(global_system_variables.*offset);
  return (byte*) &(thd->variables.*offset);
}


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


1678 1679
byte *sys_var_thd_bool::value_ptr(THD *thd, enum_var_type type,
				  LEX_STRING *base)
1680 1681 1682 1683 1684 1685 1686 1687 1688
{
  if (type == OPT_GLOBAL)
    return (byte*) &(global_system_variables.*offset);
  return (byte*) &(thd->variables.*offset);
}


bool sys_var::check_enum(THD *thd, set_var *var, TYPELIB *enum_names)
{
1689
  char buff[STRING_BUFFER_USUAL_SIZE];
1690
  const char *value;
1691
  String str(buff, sizeof(buff), system_charset_info), *res;
1692 1693 1694 1695 1696

  if (var->value->result_type() == STRING_RESULT)
  {
    if (!(res=var->value->val_str(&str)) ||
	((long) (var->save_result.ulong_value=
1697 1698
		 (ulong) find_type(enum_names, res->ptr(),
				   res->length(),1)-1)) < 0)
1699
    {
1700
      value= res ? res->c_ptr() : "NULL";
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
      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:
1718
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, value);
1719 1720 1721
  return 1;
}

1722 1723 1724

bool sys_var::check_set(THD *thd, set_var *var, TYPELIB *enum_names)
{
1725
  bool not_used;
1726
  char buff[STRING_BUFFER_USUAL_SIZE], *error= 0;
1727 1728 1729 1730 1731 1732
  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)))
1733
    {
1734
      strmov(buff, "NULL");
1735
      goto err;
1736
    }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1737 1738
    var->save_result.ulong_value= ((ulong)
				   find_set(enum_names, res->c_ptr(),
1739 1740 1741
					    res->length(),
                                            NULL,
                                            &error, &error_len,
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1742
					    &not_used));
1743 1744 1745 1746 1747 1748 1749 1750 1751
    if (error_len)
    {
      strmake(buff, error, min(sizeof(buff), error_len));
      goto err;
    }
  }
  else
  {
    ulonglong tmp= var->value->val_int();
1752 1753 1754 1755 1756 1757
   /*
     For when the enum is made to contain 64 elements, as 1ULL<<64 is
     undefined, we guard with a "count<64" test.
   */
    if (unlikely((tmp >= ((ULL(1)) << enum_names->count)) &&
                 (enum_names->count < 64)))
1758 1759 1760 1761 1762 1763 1764 1765 1766
    {
      llstr(tmp, buff);
      goto err;
    }
    var->save_result.ulong_value= (ulong) tmp;  // Save for update
  }
  return 0;

err:
1767
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, buff);
1768 1769 1770 1771
  return 1;
}


1772 1773 1774 1775 1776
/*
  Return an Item for a variable.  Used with @@[global.]variable_name
  If type is not given, return local value if exists, else global
*/

1777
Item *sys_var::item(THD *thd, enum_var_type var_type, LEX_STRING *base)
1778 1779 1780 1781 1782
{
  if (check_type(var_type))
  {
    if (var_type != OPT_DEFAULT)
    {
1783 1784
      my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0),
               name, var_type == OPT_GLOBAL ? "SESSION" : "GLOBAL");
1785 1786 1787 1788 1789 1790
      return 0;
    }
    /* As there was no local variable, return the global value */
    var_type= OPT_GLOBAL;
  }
  switch (type()) {
1791 1792 1793 1794 1795 1796
  case SHOW_INT:
  {
    uint value;
    pthread_mutex_lock(&LOCK_global_system_variables);
    value= *(uint*) value_ptr(thd, var_type, base);
    pthread_mutex_unlock(&LOCK_global_system_variables);
1797
    return new Item_uint((ulonglong) value);
1798
  }
1799
  case SHOW_LONG:
1800 1801 1802 1803 1804
  {
    ulong value;
    pthread_mutex_lock(&LOCK_global_system_variables);
    value= *(ulong*) value_ptr(thd, var_type, base);
    pthread_mutex_unlock(&LOCK_global_system_variables);
1805
    return new Item_uint((ulonglong) value);
1806
  }
1807
  case SHOW_LONGLONG:
1808 1809 1810
  {
    longlong value;
    pthread_mutex_lock(&LOCK_global_system_variables);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1811
    value= *(longlong*) value_ptr(thd, var_type, base);
1812 1813 1814
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return new Item_int(value);
  }
1815
  case SHOW_HA_ROWS:
1816 1817 1818
  {
    ha_rows value;
    pthread_mutex_lock(&LOCK_global_system_variables);
monty@narttu.mysql.fi's avatar
monty@narttu.mysql.fi committed
1819
    value= *(ha_rows*) value_ptr(thd, var_type, base);
1820 1821 1822
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return new Item_int((longlong) value);
  }
1823
  case SHOW_MY_BOOL:
1824
    return new Item_int((int32) *(my_bool*) value_ptr(thd, var_type, base),1);
1825 1826
  case SHOW_CHAR:
  {
bar@mysql.com's avatar
bar@mysql.com committed
1827
    Item *tmp;
1828
    pthread_mutex_lock(&LOCK_global_system_variables);
1829
    char *str= (char*) value_ptr(thd, var_type, base);
bar@mysql.com's avatar
bar@mysql.com committed
1830 1831 1832 1833 1834 1835 1836 1837
    if (str)
      tmp= new Item_string(str, strlen(str),
                           system_charset_info, DERIVATION_SYSCONST);
    else
    {
      tmp= new Item_null();
      tmp->collation.set(system_charset_info, DERIVATION_SYSCONST);
    }
1838 1839
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return tmp;
1840 1841
  }
  default:
1842
    my_error(ER_VAR_CANT_BE_READ, MYF(0), name);
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866
  }
  return 0;
}


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


1867 1868
byte *sys_var_thd_enum::value_ptr(THD *thd, enum_var_type type,
				  LEX_STRING *base)
1869 1870 1871 1872 1873 1874 1875
{
  ulong tmp= ((type == OPT_GLOBAL) ?
	      global_system_variables.*offset :
	      thd->variables.*offset);
  return (byte*) enum_names->type_names[tmp];
}

1876 1877 1878 1879 1880
bool sys_var_thd_bit::check(THD *thd, set_var *var)
{
  return (check_enum(thd, var, &bool_typelib) ||
          (check_func && (*check_func)(thd, var)));
}
1881 1882 1883

bool sys_var_thd_bit::update(THD *thd, set_var *var)
{
1884
  int res= (*update_func)(thd, var);
1885 1886 1887 1888
  return res;
}


1889 1890
byte *sys_var_thd_bit::value_ptr(THD *thd, enum_var_type type,
				 LEX_STRING *base)
1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
{
  /*
    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);
  return (byte*) &thd->sys_var_tmp.my_bool_value;
}


1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 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
/* Update a date_time format variable based on given value */

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");
  DBUG_DUMP("positions",(char*) new_value->positions,
	    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)
{
1944
  char buff[STRING_BUFFER_USUAL_SIZE];
1945 1946 1947 1948 1949 1950 1951 1952 1953
  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())))
  {
1954
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, res->c_ptr());
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007
    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);
}


byte *sys_var_thd_date_time_format::value_ptr(THD *thd, enum_var_type type,
					      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);
    return (byte*) res;
  }
  return (byte*) (thd->variables.*offset)->format.str;
}


2008 2009
typedef struct old_names_map_st
{
2010 2011 2012
  const char *old_name;
  const char *new_name;
} my_old_conv;
2013

2014 2015
static my_old_conv old_conv[]= 
{
2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
  {	"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		}
2027
};
2028

2029
CHARSET_INFO *get_old_charset_by_name(const char *name)
2030
{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2031
  my_old_conv *conv;
2032
 
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2033
  for (conv= old_conv; conv->old_name; conv++)
2034
  {
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2035 2036
    if (!my_strcasecmp(&my_charset_latin1, name, conv->old_name))
      return get_charset_by_csname(conv->new_name, MY_CS_PRIMARY, MYF(0));
2037
  }
2038
  return NULL;
2039 2040
}

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

2042
bool sys_var_collation::check(THD *thd, set_var *var)
2043 2044
{
  CHARSET_INFO *tmp;
2045
  LINT_INIT(tmp);
2046

2047
  if (var->value->result_type() == STRING_RESULT)
2048
  {
2049
    char buff[STRING_BUFFER_USUAL_SIZE];
2050 2051 2052
    String str(buff,sizeof(buff), system_charset_info), *res;
    if (!(res=var->value->val_str(&str)))
    {
2053
      my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
2054 2055 2056 2057
      return 1;
    }
    if (!(tmp=get_charset_by_name(res->c_ptr(),MYF(0))))
    {
2058
      my_error(ER_UNKNOWN_COLLATION, MYF(0), res->c_ptr());
2059 2060
      return 1;
    }
2061
  }
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
      my_error(ER_UNKNOWN_COLLATION, MYF(0), buf);
2069 2070
      return 1;
    }
2071 2072 2073 2074 2075
  }
  var->save_result.charset= tmp;	// Save for update
  return 0;
}

2076

2077 2078 2079
bool sys_var_character_set::check(THD *thd, set_var *var)
{
  CHARSET_INFO *tmp;
2080
  LINT_INIT(tmp);
2081

2082 2083
  if (var->value->result_type() == STRING_RESULT)
  {
2084
    char buff[STRING_BUFFER_USUAL_SIZE];
2085 2086
    String str(buff,sizeof(buff), system_charset_info), *res;
    if (!(res=var->value->val_str(&str)))
2087
    {
2088 2089
      if (!nullable)
      {
2090
        my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
2091 2092 2093 2094 2095 2096 2097
        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())))
    {
2098
      my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), res->c_ptr());
2099 2100 2101
      return 1;
    }
  }
2102
  else // INT_RESULT
2103
  {
2104
    if (!(tmp=get_charset((int) var->value->val_int(),MYF(0))))
2105 2106
    {
      char buf[20];
2107
      int10_to_str((int) var->value->val_int(), buf, -10);
2108 2109 2110
      my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), buf);
      return 1;
    }
2111 2112 2113 2114 2115
  }
  var->save_result.charset= tmp;	// Save for update
  return 0;
}

2116

2117
bool sys_var_character_set::update(THD *thd, set_var *var)
2118
{
2119
  ci_ptr(thd,var->type)[0]= var->save_result.charset;
bar@bar.mysql.r18.ru's avatar
bar@bar.mysql.r18.ru committed
2120
  thd->update_charset();
2121 2122 2123
  return 0;
}

2124 2125 2126

byte *sys_var_character_set::value_ptr(THD *thd, enum_var_type type,
				       LEX_STRING *base)
2127
{
2128
  CHARSET_INFO *cs= ci_ptr(thd,type)[0];
bar@mysql.com's avatar
bar@mysql.com committed
2129
  return cs ? (byte*) cs->csname : (byte*) NULL;
2130 2131
}

2132

2133 2134
CHARSET_INFO ** sys_var_character_set_connection::ci_ptr(THD *thd,
							 enum_var_type type)
2135 2136 2137 2138 2139 2140 2141
{
  if (type == OPT_GLOBAL)
    return &global_system_variables.collation_connection;
  else
    return &thd->variables.collation_connection;
}

2142 2143 2144

void sys_var_character_set_connection::set_default(THD *thd,
						   enum_var_type type)
2145 2146 2147 2148
{
 if (type == OPT_GLOBAL)
   global_system_variables.collation_connection= default_charset_info;
 else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2149
 {
2150
   thd->variables.collation_connection= global_system_variables.collation_connection;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2151 2152
   thd->update_charset();
 }
2153 2154
}

2155

2156 2157
CHARSET_INFO ** sys_var_character_set_client::ci_ptr(THD *thd,
						     enum_var_type type)
2158 2159 2160 2161 2162 2163 2164
{
  if (type == OPT_GLOBAL)
    return &global_system_variables.character_set_client;
  else
    return &thd->variables.character_set_client;
}

2165

2166
void sys_var_character_set_client::set_default(THD *thd, enum_var_type type)
2167 2168
{
 if (type == OPT_GLOBAL)
2169
   global_system_variables.character_set_client= default_charset_info;
2170
 else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2171 2172 2173 2174 2175
 {
   thd->variables.character_set_client= (global_system_variables.
					 character_set_client);
   thd->update_charset();
 }
2176
}
2177

2178

2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
CHARSET_INFO **
sys_var_character_set_filesystem::ci_ptr(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
    return &global_system_variables.character_set_filesystem;
  else
    return &thd->variables.character_set_filesystem;
}


extern CHARSET_INFO *character_set_filesystem;

void
sys_var_character_set_filesystem::set_default(THD *thd, enum_var_type type)
{
 if (type == OPT_GLOBAL)
   global_system_variables.character_set_filesystem= character_set_filesystem;
 else
 {
   thd->variables.character_set_filesystem= (global_system_variables.
					     character_set_filesystem);
   thd->update_charset();
 }
}


monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2205 2206
CHARSET_INFO **
sys_var_character_set_results::ci_ptr(THD *thd, enum_var_type type)
2207
{
2208 2209
  if (type == OPT_GLOBAL)
    return &global_system_variables.character_set_results;
2210
  else
2211
    return &thd->variables.character_set_results;
2212 2213
}

2214

2215
void sys_var_character_set_results::set_default(THD *thd, enum_var_type type)
2216
{
2217
 if (type == OPT_GLOBAL)
2218
   global_system_variables.character_set_results= default_charset_info;
2219
 else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2220 2221 2222 2223 2224
 {
   thd->variables.character_set_results= (global_system_variables.
					  character_set_results);
   thd->update_charset();
 }
2225 2226
}

2227

monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2228 2229
CHARSET_INFO **
sys_var_character_set_server::ci_ptr(THD *thd, enum_var_type type)
2230 2231
{
  if (type == OPT_GLOBAL)
2232
    return &global_system_variables.collation_server;
2233
  else
2234
    return &thd->variables.collation_server;
2235 2236
}

2237

2238
void sys_var_character_set_server::set_default(THD *thd, enum_var_type type)
2239 2240
{
 if (type == OPT_GLOBAL)
2241
   global_system_variables.collation_server= default_charset_info;
2242
 else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2243
 {
2244
   thd->variables.collation_server= global_system_variables.collation_server;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2245 2246
   thd->update_charset();
 }
2247 2248
}

2249 2250
CHARSET_INFO ** sys_var_character_set_database::ci_ptr(THD *thd,
						       enum_var_type type)
2251 2252
{
  if (type == OPT_GLOBAL)
2253
    return &global_system_variables.collation_database;
2254
  else
2255
    return &thd->variables.collation_database;
2256 2257
}

2258

2259 2260 2261
void sys_var_character_set_database::set_default(THD *thd, enum_var_type type)
{
 if (type == OPT_GLOBAL)
2262
    global_system_variables.collation_database= default_charset_info;
2263
  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2264
  {
2265
    thd->variables.collation_database= thd->db_charset;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2266 2267
    thd->update_charset();
  }
2268 2269
}

2270

2271
bool sys_var_collation_connection::update(THD *thd, set_var *var)
2272 2273
{
  if (var->type == OPT_GLOBAL)
2274
    global_system_variables.collation_connection= var->save_result.charset;
2275
  else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2276
  {
2277
    thd->variables.collation_connection= var->save_result.charset;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2278 2279
    thd->update_charset();
  }
2280 2281 2282
  return 0;
}

2283 2284 2285

byte *sys_var_collation_connection::value_ptr(THD *thd, enum_var_type type,
					      LEX_STRING *base)
2286 2287
{
  CHARSET_INFO *cs= ((type == OPT_GLOBAL) ?
2288 2289 2290
		  global_system_variables.collation_connection :
		  thd->variables.collation_connection);
  return cs ? (byte*) cs->name : (byte*) "NULL";
2291 2292
}

2293

2294
void sys_var_collation_connection::set_default(THD *thd, enum_var_type type)
2295 2296
{
 if (type == OPT_GLOBAL)
2297
   global_system_variables.collation_connection= default_charset_info;
2298
 else
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2299 2300 2301 2302 2303
 {
   thd->variables.collation_connection= (global_system_variables.
					 collation_connection);
   thd->update_charset();
 }
2304 2305
}

2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376
bool sys_var_collation_database::update(THD *thd, set_var *var)
{
  if (var->type == OPT_GLOBAL)
    global_system_variables.collation_database= var->save_result.charset;
  else
  {
    thd->variables.collation_database= var->save_result.charset;
    thd->update_charset();
  }
  return 0;
}


byte *sys_var_collation_database::value_ptr(THD *thd, enum_var_type type,
					      LEX_STRING *base)
{
  CHARSET_INFO *cs= ((type == OPT_GLOBAL) ?
		  global_system_variables.collation_database :
		  thd->variables.collation_database);
  return cs ? (byte*) cs->name : (byte*) "NULL";
}


void sys_var_collation_database::set_default(THD *thd, enum_var_type type)
{
 if (type == OPT_GLOBAL)
   global_system_variables.collation_database= default_charset_info;
 else
 {
   thd->variables.collation_database= (global_system_variables.
					 collation_database);
   thd->update_charset();
 }
}


bool sys_var_collation_server::update(THD *thd, set_var *var)
{
  if (var->type == OPT_GLOBAL)
    global_system_variables.collation_server= var->save_result.charset;
  else
  {
    thd->variables.collation_server= var->save_result.charset;
    thd->update_charset();
  }
  return 0;
}


byte *sys_var_collation_server::value_ptr(THD *thd, enum_var_type type,
					      LEX_STRING *base)
{
  CHARSET_INFO *cs= ((type == OPT_GLOBAL) ?
		  global_system_variables.collation_server :
		  thd->variables.collation_server);
  return cs ? (byte*) cs->name : (byte*) "NULL";
}


void sys_var_collation_server::set_default(THD *thd, enum_var_type type)
{
 if (type == OPT_GLOBAL)
   global_system_variables.collation_server= default_charset_info;
 else
 {
   thd->variables.collation_server= (global_system_variables.
					 collation_server);
   thd->update_charset();
 }
}

2377

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

2380
static KEY_CACHE zero_key_cache;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2381

2382
KEY_CACHE *get_key_cache(LEX_STRING *cache_name)
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2383
{
2384 2385
  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
2386
    cache_name= &default_key_cache_base;
2387
  return ((KEY_CACHE*) find_named(&key_caches,
2388
                                      cache_name->str, cache_name->length, 0));
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2389 2390
}

2391

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2392 2393
byte *sys_var_key_cache_param::value_ptr(THD *thd, enum_var_type type,
					 LEX_STRING *base)
2394
{
2395
  KEY_CACHE *key_cache= get_key_cache(base);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2396 2397 2398 2399
  if (!key_cache)
    key_cache= &zero_key_cache;
  return (byte*) key_cache + offset ;
}
2400

2401

2402 2403
bool sys_var_key_buffer_size::update(THD *thd, set_var *var)
{
2404
  ulonglong tmp= var->save_result.ulonglong_value;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2405
  LEX_STRING *base_name= &var->base;
2406
  KEY_CACHE *key_cache;
2407 2408 2409
  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
2410
  if (!base_name->length)
2411
    base_name= &default_key_cache_base;
2412 2413 2414

  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
2415
                            
2416 2417
  if (!key_cache)
  {
2418
    /* Key cache didn't exists */
2419
    if (!tmp)					// Tried to delete cache
2420 2421 2422 2423 2424 2425
      goto end;					// Ok, nothing to do
    if (!(key_cache= create_key_cache(base_name->str, base_name->length)))
    {
      error= 1;
      goto end;
    }
2426
  }
2427 2428 2429 2430 2431 2432 2433 2434 2435

  /*
    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
2436
  if (!tmp)					// Zero size means delete
2437
  {
timour@mysql.com's avatar
timour@mysql.com committed
2438
    if (key_cache == dflt_key_cache)
2439 2440 2441 2442
    {
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
                          ER_WARN_CANT_DROP_DEFAULT_KEYCACHE,
                          ER(ER_WARN_CANT_DROP_DEFAULT_KEYCACHE));
2443
      goto end;					// Ignore default key cache
2444
    }
2445

2446
    if (key_cache->key_cache_inited)		// If initied
2447 2448
    {
      /*
2449 2450
	Move tables using this key cache to the default key cache
	and clear the old key cache.
2451
      */
2452
      NAMED_LIST *list; 
2453
      key_cache= (KEY_CACHE *) find_named(&key_caches, base_name->str,
2454
					      base_name->length, &list);
2455 2456
      key_cache->in_init= 1;
      pthread_mutex_unlock(&LOCK_global_system_variables);
timour@mysql.com's avatar
timour@mysql.com committed
2457
      error= reassign_keycache_tables(thd, key_cache, dflt_key_cache);
2458 2459
      pthread_mutex_lock(&LOCK_global_system_variables);
      key_cache->in_init= 0;
2460
    }
2461 2462 2463 2464 2465
    /*
      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;
2466 2467
  }

2468 2469
  key_cache->param_buff_size=
    (ulonglong) getopt_ull_limit_value(tmp, option_limits);
2470

2471 2472 2473 2474
  /* If key cache didn't existed initialize it, else resize it */
  key_cache->in_init= 1;
  pthread_mutex_unlock(&LOCK_global_system_variables);

2475
  if (!key_cache->key_cache_inited)
2476
    error= (bool) (ha_init_key_cache("", key_cache));
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2477
  else
2478
    error= (bool)(ha_resize_key_cache(key_cache));
2479

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

2483 2484 2485
end:
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return error;
2486 2487
}

2488 2489

bool sys_var_key_cache_long::update(THD *thd, set_var *var)
2490
{
monty@mysql.com's avatar
monty@mysql.com committed
2491
  ulong tmp= (ulong) var->value->val_int();
2492
  LEX_STRING *base_name= &var->base;
2493 2494
  bool error= 0;

2495
  if (!base_name->length)
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2496
    base_name= &default_key_cache_base;
2497 2498

  pthread_mutex_lock(&LOCK_global_system_variables);
2499
  KEY_CACHE *key_cache= get_key_cache(base_name);
2500

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2501 2502
  if (!key_cache && !(key_cache= create_key_cache(base_name->str,
				                  base_name->length)))
2503 2504 2505 2506
  {
    error= 1;
    goto end;
  }
2507

2508 2509 2510 2511 2512 2513 2514 2515 2516
  /*
    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))=
2517
    (ulong) getopt_ull_limit_value(tmp, option_limits);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2518

2519 2520 2521 2522 2523
  /*
    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;
2524

2525
  pthread_mutex_unlock(&LOCK_global_system_variables);
2526

2527 2528 2529 2530 2531 2532 2533 2534
  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;
2535 2536
}

2537

2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599
bool sys_var_log_state::update(THD *thd, set_var *var)
{
  bool res= 0;
  pthread_mutex_lock(&LOCK_global_system_variables);
  if (!var->save_result.ulong_value)
    logger.deactivate_log_handler(thd, log_type);
  else
  {
    if ((res= logger.activate_log_handler(thd, log_type)))
    {
      my_error(ER_CANT_ACTIVATE_LOG, MYF(0),
               log_type == QUERY_LOG_GENERAL ? "general" :
               "slow query");
      goto err;
    }
  }
err:
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return res;
}

void sys_var_log_state::set_default(THD *thd, enum_var_type type)
{
  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)
{
  char path[FN_REFLEN];
  MY_STAT f_stat;
  const char *var_path= var->value->str_value.ptr();
  bzero(&f_stat, sizeof(MY_STAT));

  (void) unpack_filename(path, var_path);
  if (my_stat(path, &f_stat, MYF(0)))
  {
    /* Check if argument is a file and we have 'write' permission */
    if (!MY_S_ISREG(f_stat.st_mode) ||
        !(f_stat.st_mode & MY_S_IWRITE))
      return -1;
  }
  else
  {
    /*
      Check if directory exists and 
      we have permission to create file & write to file
    */
    (void) dirname_part(path, var_path);
    if (my_access(path, (F_OK|W_OK)))
      return -1;
  }
  return 0;
}


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)
{
2600
  MYSQL_QUERY_LOG *file_log;
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621
  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:
    DBUG_ASSERT(0);
  }

  if (!old_value)
  {
    old_value= make_default_log_name(buff, log_ext);
    str_length= strlen(old_value);
  }
2622
  if (!(res= my_strndup(old_value, str_length, MYF(MY_FAE+MY_WME))))
2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
  {
    result= 1;
    goto err;
  }

  pthread_mutex_lock(&LOCK_global_system_variables);
  logger.lock();

  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)
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
  {
    switch (log_type) {
    case QUERY_LOG_SLOW:
      file_log->open_slow_log(sys_var_general_log_path.value);
      break;
    case QUERY_LOG_GENERAL:
      file_log->open_query_log(sys_var_general_log_path.value);
      break;
    default:
      DBUG_ASSERT(0);
    }
  }
2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713

  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);
  logger.lock();
  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);
  logger.lock();
  logger.init_slow_log(LOG_TABLE);
  logger.init_general_log(LOG_TABLE);
  *value= LOG_TABLE;
  logger.unlock();
  pthread_mutex_unlock(&LOCK_global_system_variables);
}


monty@mysql.com's avatar
monty@mysql.com committed
2714 2715
byte *sys_var_log_output::value_ptr(THD *thd, enum_var_type type,
                                    LEX_STRING *base)
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738
{
  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--;
  return (byte*) thd->strmake(tmp.ptr(), length);
}


2739 2740 2741 2742
/*****************************************************************************
  Functions to handle SET NAMES and SET CHARACTER SET
*****************************************************************************/

2743
int set_var_collation_client::check(THD *thd)
2744 2745 2746 2747
{
  return 0;
}

2748
int set_var_collation_client::update(THD *thd)
2749
{
2750 2751
  thd->variables.character_set_client= character_set_client;
  thd->variables.character_set_results= character_set_results;
2752
  thd->variables.collation_connection= collation_connection;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
2753
  thd->update_charset();
2754 2755 2756 2757 2758 2759 2760
  thd->protocol_simple.init(thd);
  thd->protocol_prep.init(thd);
  return 0;
}

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

2761 2762
bool sys_var_timestamp::update(THD *thd,  set_var *var)
{
2763
  thd->set_time((time_t) var->save_result.ulonglong_value);
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773
  return 0;
}


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


2774 2775
byte *sys_var_timestamp::value_ptr(THD *thd, enum_var_type type,
				   LEX_STRING *base)
2776 2777 2778 2779 2780 2781 2782 2783
{
  thd->sys_var_tmp.long_value= (long) thd->start_time;
  return (byte*) &thd->sys_var_tmp.long_value;
}


bool sys_var_last_insert_id::update(THD *thd, set_var *var)
{
2784 2785
  thd->first_successful_insert_id_in_prev_stmt= 
    var->save_result.ulonglong_value;
2786 2787 2788 2789
  return 0;
}


2790 2791
byte *sys_var_last_insert_id::value_ptr(THD *thd, enum_var_type type,
					LEX_STRING *base)
2792
{
2793 2794 2795 2796 2797 2798 2799
  /*
    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();
  return (byte*) &thd->sys_var_tmp.ulonglong_value;
2800 2801 2802 2803 2804
}


bool sys_var_insert_id::update(THD *thd, set_var *var)
{
2805
  thd->force_one_auto_inc_interval(var->save_result.ulonglong_value);
2806 2807 2808 2809
  return 0;
}


2810 2811
byte *sys_var_insert_id::value_ptr(THD *thd, enum_var_type type,
				   LEX_STRING *base)
2812
{
2813 2814 2815
  thd->sys_var_tmp.ulonglong_value= 
    thd->auto_inc_intervals_forced.minimum();
  return (byte*) &thd->sys_var_tmp.ulonglong_value;
2816 2817 2818
}


hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
2819
#ifdef HAVE_REPLICATION
2820 2821
bool sys_var_slave_skip_counter::check(THD *thd, set_var *var)
{
2822
  int result= 0;
2823
  pthread_mutex_lock(&LOCK_active_mi);
2824 2825 2826
  pthread_mutex_lock(&active_mi->rli.run_lock);
  if (active_mi->rli.slave_running)
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
2827
    my_message(ER_SLAVE_MUST_STOP, ER(ER_SLAVE_MUST_STOP), MYF(0));
2828 2829 2830
    result=1;
  }
  pthread_mutex_unlock(&active_mi->rli.run_lock);
2831
  pthread_mutex_unlock(&LOCK_active_mi);
2832
  var->save_result.ulong_value= (ulong) var->value->val_int();
2833 2834 2835 2836 2837 2838
  return result;
}


bool sys_var_slave_skip_counter::update(THD *thd, set_var *var)
{
2839
  pthread_mutex_lock(&LOCK_active_mi);
2840 2841 2842 2843 2844 2845 2846 2847 2848
  pthread_mutex_lock(&active_mi->rli.run_lock);
  /*
    The following test should normally never be true as we test this
    in the check function;  To be safe against multiple
    SQL_SLAVE_SKIP_COUNTER request, we do the check anyway
  */
  if (!active_mi->rli.slave_running)
  {
    pthread_mutex_lock(&active_mi->rli.data_lock);
2849
    active_mi->rli.slave_skip_counter= var->save_result.ulong_value;
2850 2851 2852
    pthread_mutex_unlock(&active_mi->rli.data_lock);
  }
  pthread_mutex_unlock(&active_mi->rli.run_lock);
2853
  pthread_mutex_unlock(&LOCK_active_mi);
2854 2855
  return 0;
}
2856 2857 2858 2859


bool sys_var_sync_binlog_period::update(THD *thd, set_var *var)
{
2860
  sync_binlog_period= (ulong) var->save_result.ulonglong_value;
2861
  return 0;
2862
}
hf@deer.mysql.r18.ru's avatar
SCRUM  
hf@deer.mysql.r18.ru committed
2863
#endif /* HAVE_REPLICATION */
2864

2865 2866
bool sys_var_rand_seed1::update(THD *thd, set_var *var)
{
2867
  thd->rand.seed1= (ulong) var->save_result.ulonglong_value;
2868 2869 2870 2871 2872
  return 0;
}

bool sys_var_rand_seed2::update(THD *thd, set_var *var)
{
2873
  thd->rand.seed2= (ulong) var->save_result.ulonglong_value;
2874 2875 2876 2877
  return 0;
}


2878 2879
bool sys_var_thd_time_zone::check(THD *thd, set_var *var)
{
2880
  char buff[MAX_TIME_ZONE_NAME_LENGTH];
2881 2882 2883
  String str(buff, sizeof(buff), &my_charset_latin1);
  String *res= var->value->val_str(&str);

2884
  if (!(var->save_result.time_zone=
2885
        my_tz_find(res, thd->lex->time_zone_tables_used)))
2886
  {
2887
    my_error(ER_UNKNOWN_TIME_ZONE, MYF(0), res ? res->c_ptr() : "NULL");
2888 2889 2890 2891 2892 2893 2894 2895
    return 1;
  }
  return 0;
}


bool sys_var_thd_time_zone::update(THD *thd, set_var *var)
{
2896 2897 2898 2899 2900 2901 2902 2903 2904
  /* 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;
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
  return 0;
}


byte *sys_var_thd_time_zone::value_ptr(THD *thd, enum_var_type type,
				       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)
2917
    return (byte *)(global_system_variables.time_zone->get_name()->ptr());
2918
  else
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
  {
    /*
      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;
2929
    return (byte *)(thd->variables.time_zone->get_name()->ptr());
2930
  }
2931 2932 2933 2934 2935
}


void sys_var_thd_time_zone::set_default(THD *thd, enum_var_type type)
{
2936
 pthread_mutex_lock(&LOCK_global_system_variables);
2937 2938 2939 2940 2941
 if (type == OPT_GLOBAL)
 {
   if (default_tz_name)
   {
     String str(default_tz_name, &my_charset_latin1);
2942 2943 2944 2945
     /*
       We are guaranteed to find this time zone since its existence
       is checked during start-up.
     */
2946 2947
     global_system_variables.time_zone=
       my_tz_find(&str, thd->lex->time_zone_tables_used);
2948 2949 2950 2951 2952 2953
   }
   else
     global_system_variables.time_zone= my_tz_SYSTEM;
 }
 else
   thd->variables.time_zone= global_system_variables.time_zone;
2954
 pthread_mutex_unlock(&LOCK_global_system_variables);
2955 2956
}

2957 2958 2959 2960 2961 2962 2963 2964 2965

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.
2966
      May be we should have a separate error message for this?
2967 2968 2969 2970 2971 2972 2973 2974 2975 2976
    */
    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);
2977
  max_user_connections= (uint)var->save_result.ulonglong_value;
2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000
  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);
}


byte *sys_var_max_user_conn::value_ptr(THD *thd, enum_var_type type,
                                       LEX_STRING *base)
{
  if (type != OPT_GLOBAL &&
      thd->user_connect && thd->user_connect->user_resources.user_conn)
    return (byte*) &(thd->user_connect->user_resources.user_conn);
  return (byte*) &(max_user_connections);
}

bar@mysql.com's avatar
bar@mysql.com committed
3001 3002 3003 3004 3005
bool sys_var_thd_lc_time_names::check(THD *thd, set_var *var)
{
  char *locale_str =var->value->str_value.c_ptr();
  MY_LOCALE *locale_match=  my_locale_by_name(locale_str);

3006
  if (locale_match == NULL)
bar@mysql.com's avatar
bar@mysql.com committed
3007
  {
3008 3009
    my_printf_error(ER_UNKNOWN_ERROR,
                    "Unknown locale: '%s'", MYF(0), locale_str);
bar@mysql.com's avatar
bar@mysql.com committed
3010 3011
    return 1;
  }
3012 3013
  var->save_result.locale_value= locale_match;
  return 0;
bar@mysql.com's avatar
bar@mysql.com committed
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034
}


bool sys_var_thd_lc_time_names::update(THD *thd, set_var *var)
{
  thd->variables.lc_time_names= var->save_result.locale_value;
  return 0;
}


byte *sys_var_thd_lc_time_names::value_ptr(THD *thd, enum_var_type type,
					  LEX_STRING *base)
{
  return (byte *)(thd->variables.lc_time_names->name);
}


void sys_var_thd_lc_time_names::set_default(THD *thd, enum_var_type type)
{
  thd->variables.lc_time_names = &my_locale_en_US;
}
3035

3036 3037 3038 3039 3040 3041
/*
  Functions to update thd->options bits
*/

static bool set_option_bit(THD *thd, set_var *var)
{
3042 3043 3044
  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;
3045
  else
3046
    thd->options|= sys_var->bit_flag;
3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066
  return 0;
}


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

  ulong org_options=thd->options;

  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 */
3067 3068
      thd->options&= ~(ulong) (OPTION_BEGIN | OPTION_STATUS_NO_TRANS_UPDATE |
                               OPTION_KEEP_LOG);
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081
      thd->server_status|= SERVER_STATUS_AUTOCOMMIT;
      if (ha_commit(thd))
	return 1;
    }
    else
    {
      thd->options&= ~(ulong) (OPTION_STATUS_NO_TRANS_UPDATE);
      thd->server_status&= ~SERVER_STATUS_AUTOCOMMIT;
    }
  }
  return 0;
}

3082 3083 3084
static int check_log_update(THD *thd, set_var *var)
{
#ifndef NO_EMBEDDED_ACCESS_CHECKS
3085
  if (!(thd->security_ctx->master_access & SUPER_ACL))
3086 3087 3088 3089 3090 3091 3092
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
#endif
  return 0;
}
3093 3094 3095

static bool set_log_update(THD *thd, set_var *var)
{
3096 3097 3098 3099 3100
  /*
    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
  */
3101

3102
  if (opt_sql_bin_update)
3103 3104 3105 3106 3107 3108 3109 3110 3111
  {
    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));
3112 3113 3114 3115 3116
  set_option_bit(thd, var);
  return 0;
}


3117 3118 3119 3120
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
3121
  if (thd->security_ctx->master_access & SUPER_ACL)
3122 3123 3124 3125 3126 3127 3128 3129 3130 3131
    return 0;
  else
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
#else
  return 0;
#endif
}
3132

3133 3134 3135 3136
static byte *get_warning_count(THD *thd)
{
  thd->sys_var_tmp.long_value=
    (thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_NOTE] +
3137
     thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_ERROR] +
3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148
     thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_WARN]);
  return (byte*) &thd->sys_var_tmp.long_value;
}

static byte *get_error_count(THD *thd)
{
  thd->sys_var_tmp.long_value= 
    thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_ERROR];
  return (byte*) &thd->sys_var_tmp.long_value;
}

3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173

/*
  Get the tmpdir that was specified or chosen by default

  SYNOPSIS
    get_tmpdir()
    thd		thread handle

  DESCRIPTION
    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.

  RETURN VALUES
    ptr		pointer to NUL-terminated string
 */
static byte *get_tmpdir(THD *thd)
{
  if (opt_mysql_tmpdir)
    return (byte *)opt_mysql_tmpdir;
  return (byte*)mysql_tmpdir;
}

3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231
/****************************************************************************
  Main handling of variables:
  - Initialisation
  - Searching during parsing
  - Update loop
****************************************************************************/

/*
  Find variable name in option my_getopt structure used for command line args

  SYNOPSIS
    find_option()
    opt		option structure array to search in
    name	variable name

  RETURN VALUES
    0		Error
    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;
}


/*
  Return variable name and length for hashing of variables
*/

static byte *get_sys_var_length(const sys_var *var, uint *length,
				my_bool first)
{
  *length= var->name_length;
  return (byte*) var->name;
}


/*
  Initialises sys variables and put them in system_variable_hash
*/


void set_var_init()
{
3232 3233 3234 3235 3236
  sys_var *var;

  hash_init(&system_variable_hash, system_charset_info, sys_var::sys_vars, 0,
	    0, (hash_get_key) get_sys_var_length, 0, 0);
  for (var= sys_var::first; var; var= var->next)
3237
  {
3238 3239 3240
    var->name_length= strlen(var->name);
    var->option_limits= find_option(my_long_options, var->name);
    my_hash_insert(&system_variable_hash, (byte*) var);
3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271
  }
  /*
    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;
}


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


/*
  Find a user set-table variable

  SYNOPSIS
    find_sys_var()
    str		Name of system variable to find
    length	Length of variable.  zero means that we should use strlen()
		on the variable

  RETURN VALUES
    pointer	pointer to variable definitions
    0		Unknown variable (error message is given)
*/

3272
sys_var *find_sys_var(const char *str, uint length)
3273
{
3274 3275
  sys_var *var= (sys_var*) hash_search(&system_variable_hash,
				       (byte*) str,
3276 3277 3278
				       length ? length :
				       strlen(str));
  if (!var)
3279
    my_error(ER_UNKNOWN_SYSTEM_VARIABLE, MYF(0), (char*) str);
3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
  return var;
}


/*
  Execute update of all variables

  SYNOPSIS

  sql_set
    THD		Thread id
    set_var	List of variables to update

  DESCRIPTION
    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.

    This should ensure that in all normal cases none all or variables are
    updated

    RETURN VALUE
    0	ok
3302 3303
    1	ERROR, message sent (normally no variables was updated)
    -1  ERROR, message not sent
3304 3305
*/

3306
int sql_set_variables(THD *thd, List<set_var_base> *var_list)
3307
{
3308
  int error;
3309
  List_iterator_fast<set_var_base> it(*var_list);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3310
  DBUG_ENTER("sql_set_variables");
3311 3312 3313 3314

  set_var_base *var;
  while ((var=it++))
  {
3315
    if ((error= var->check(thd)))
3316
      goto err;
3317
  }
3318
  if (!(error= test(thd->net.report_error)))
3319 3320 3321 3322 3323
  {
    it.rewind();
    while ((var= it++))
      error|= var->update(thd);         // Returns 0, -1 or 1
  }
3324

3325 3326
err:
  free_underlaid_joins(thd, &thd->lex->select_lex);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3327
  DBUG_RETURN(error);
3328 3329 3330
}


3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360
/*
  Say if all variables set by a SET support the ONE_SHOT keyword (currently,
  only character set and collation do; later timezones will).

  SYNOPSIS

  not_all_support_one_shot
    set_var	List of variables to update

  NOTES
    It has a "not_" because it makes faster tests (no need to "!")

    RETURN VALUE
    0	all variables of the list support ONE_SHOT
    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;
}


3361 3362 3363 3364
/*****************************************************************************
  Functions to handle SET mysql_internal_variable=const_expr
*****************************************************************************/

3365
int set_var::check(THD *thd)
3366
{
3367 3368 3369 3370 3371
  if (var->is_readonly())
  {
    my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0), var->name, "read only");
    return -1;
  }
3372 3373
  if (var->check_type(type))
  {
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3374
    int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
3375
    my_error(err, MYF(0), var->name);
3376
    return -1;
3377 3378 3379 3380 3381 3382 3383 3384
  }
  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))
    {
3385
      my_error(ER_NO_DEFAULT, MYF(0), var->name);
3386
      return -1;
3387 3388 3389 3390
    }
    return 0;
  }

3391
  if ((!value->fixed &&
3392
       value->fix_fields(thd, &value)) || value->check_cols(1))
3393
    return -1;
3394 3395
  if (var->check_update_type(value->result_type()))
  {
3396
    my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var->name);
3397
    return -1;
3398
  }
3399
  return var->check(thd, this) ? -1 : 0;
3400 3401 3402
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414
/*
  Check variable, but without assigning value (used by PS)

  SYNOPSIS
    set_var::light_check()
    thd		thread handler

  RETURN VALUE
    0	ok
    1	ERROR, message sent (normally no variables was updated)
    -1  ERROR, message not sent
*/
3415 3416 3417 3418
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
3419
    int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
3420
    my_error(err, MYF(0), var->name);
3421 3422
    return -1;
  }
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3423
  if (type == OPT_GLOBAL && check_global_access(thd, SUPER_ACL))
3424 3425
    return 1;

3426
  if (value && ((!value->fixed && value->fix_fields(thd, &value)) ||
3427
                value->check_cols(1)))
3428 3429 3430 3431 3432
    return -1;
  return 0;
}


3433
int set_var::update(THD *thd)
3434 3435 3436 3437
{
  if (!value)
    var->set_default(thd, type);
  else if (var->update(thd, this))
3438
    return -1;				// should never happen
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448
  if (var->after_update)
    (*var->after_update)(thd, type);
  return 0;
}


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

3449
int set_var_user::check(THD *thd)
3450
{
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3451 3452
  /*
    Item_func_set_user_var can't substitute something else on its place =>
3453
    0 can be passed as last argument (reference on item)
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3454
  */
3455
  return (user_var_item->fix_fields(thd, (Item**) 0) ||
3456
	  user_var_item->check(0)) ? -1 : 0;
3457 3458 3459
}


bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
/*
  Check variable, but without assigning value (used by PS)

  SYNOPSIS
    set_var_user::light_check()
    thd		thread handler

  RETURN VALUE
    0	ok
    1	ERROR, message sent (normally no variables was updated)
    -1  ERROR, message not sent
*/
3472 3473 3474 3475 3476 3477
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)
  */
3478
  return (user_var_item->fix_fields(thd, (Item**) 0));
3479 3480 3481
}


3482
int set_var_user::update(THD *thd)
3483 3484 3485 3486
{
  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
3487
    my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY), MYF(0));
3488
    return -1;
3489 3490 3491 3492 3493 3494 3495 3496 3497
  }
  return 0;
}


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

3498
int set_var_password::check(THD *thd)
3499
{
hf@deer.(none)'s avatar
hf@deer.(none) committed
3500
#ifndef NO_EMBEDDED_ACCESS_CHECKS
3501
  if (!user->host.str)
3502
  {
3503
    if (*thd->security_ctx->priv_host != 0)
3504
    {
3505 3506
      user->host.str= (char *) thd->security_ctx->priv_host;
      user->host.length= strlen(thd->security_ctx->priv_host);
3507 3508 3509 3510 3511 3512 3513
    }
    else
    {
      user->host.str= (char *)"%";
      user->host.length= 1;
    }
  }
3514
  /* Returns 1 as the function sends error to client */
3515 3516
  return check_change_password(thd, user->host.str, user->user.str,
                               password, strlen(password)) ? 1 : 0;
3517
#else
hf@deer.(none)'s avatar
hf@deer.(none) committed
3518 3519
  return 0;
#endif
3520 3521
}

3522
int set_var_password::update(THD *thd)
3523
{
hf@deer.(none)'s avatar
hf@deer.(none) committed
3524
#ifndef NO_EMBEDDED_ACCESS_CHECKS
3525
  /* Returns 1 as the function sends error to client */
3526 3527
  return change_password(thd, user->host.str, user->user.str, password) ?
	  1 : 0;
hf@deer.(none)'s avatar
hf@deer.(none) committed
3528 3529 3530
#else
  return 0;
#endif
3531 3532
}

3533 3534 3535 3536
/****************************************************************************
 Functions to handle table_type
****************************************************************************/

3537 3538
/* Based upon sys_var::check_enum() */

3539
bool sys_var_thd_storage_engine::check(THD *thd, set_var *var)
3540
{
3541
  char buff[STRING_BUFFER_USUAL_SIZE];
3542 3543 3544 3545 3546
  const char *value;
  String str(buff, sizeof(buff), &my_charset_latin1), *res;

  if (var->value->result_type() == STRING_RESULT)
  {
3547 3548
    LEX_STRING name;
    handlerton *db_type;
3549
    if (!(res=var->value->val_str(&str)) ||
3550 3551 3552
        !(name.str= (char *)res->ptr()) || !(name.length= res->length()) ||
	!(var->save_result.hton= db_type= ha_resolve_by_name(thd, &name)) ||
        ha_checktype(thd, ha_legacy_type(db_type), 1, 0) != db_type)
3553 3554 3555 3556 3557 3558
    {
      value= res ? res->c_ptr() : "NULL";
      goto err;
    }
    return 0;
  }
monty@mysql.com's avatar
monty@mysql.com committed
3559
  value= "unknown";
3560 3561

err:
3562
  my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), value);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
3563
  return 1;
3564 3565
}

3566

3567 3568
byte *sys_var_thd_storage_engine::value_ptr(THD *thd, enum_var_type type,
					    LEX_STRING *base)
3569
{
3570 3571 3572
  handlerton *val;
  val= (type == OPT_GLOBAL) ? global_system_variables.*offset :
        thd->variables.*offset;
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
3573
  return (byte *) hton2plugin[val->slot]->name.str;
3574 3575
}

3576

3577
void sys_var_thd_storage_engine::set_default(THD *thd, enum_var_type type)
3578 3579
{
  if (type == OPT_GLOBAL)
3580
    global_system_variables.*offset= myisam_hton;
3581
  else
3582
    thd->variables.*offset= global_system_variables.*offset;
3583 3584
}

3585

3586
bool sys_var_thd_storage_engine::update(THD *thd, set_var *var)
3587
{
3588 3589 3590 3591
  handlerton **value= &(global_system_variables.*offset);
  if (var->type != OPT_GLOBAL)
    value= &(thd->variables.*offset);
  *value= var->save_result.hton;
3592 3593 3594
  return 0;
}

3595 3596
void sys_var_thd_table_type::warn_deprecated(THD *thd)
{
3597
  WARN_DEPRECATED(thd, "5.2", "table_type", "'storage_engine'");
3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
}

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

3612

3613 3614 3615
/****************************************************************************
 Functions to handle sql_mode
****************************************************************************/
3616

3617 3618 3619
/*
  Make string representation of mode

3620 3621 3622 3623 3624 3625 3626
  SYNOPSIS
    thd   in  thread handler
    val   in  sql_mode value
    len   out pointer on length of string

  RETURN
    pointer to string with sql_mode representation
3627 3628 3629 3630
*/

byte *sys_var_thd_sql_mode::symbolic_mode_representation(THD *thd, ulong val,
                                                         ulong *len)
3631 3632 3633
{
  char buff[256];
  String tmp(buff, sizeof(buff), &my_charset_latin1);
monty@mishka.local's avatar
monty@mishka.local committed
3634
  ulong length;
3635 3636 3637 3638 3639 3640

  tmp.length(0);
  for (uint i= 0; val; val>>= 1, i++)
  {
    if (val & 1)
    {
3641 3642
      tmp.append(sql_mode_typelib.type_names[i],
                 sql_mode_typelib.type_lengths[i]);
3643 3644 3645
      tmp.append(',');
    }
  }
monty@mishka.local's avatar
monty@mishka.local committed
3646 3647 3648 3649 3650

  if ((length= tmp.length()))
    length--;
  *len= length;
  return (byte*) thd->strmake(tmp.ptr(), length);
3651 3652
}

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

3654 3655 3656 3657 3658 3659 3660
byte *sys_var_thd_sql_mode::value_ptr(THD *thd, enum_var_type type,
				      LEX_STRING *base)
{
  ulong val= ((type == OPT_GLOBAL) ? global_system_variables.*offset :
              thd->variables.*offset);
  ulong length_unused;
  return symbolic_mode_representation(thd, val, &length_unused);
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671
}


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
3672

3673 3674 3675 3676 3677 3678
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
3679
  {
3680
    thd->variables.sql_mode= fix_sql_mode(thd->variables.sql_mode);
3681 3682 3683 3684 3685 3686 3687 3688
    /*
      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;
  }
3689 3690 3691
}

/* Map database specific bits to function bits */
3692

3693 3694 3695 3696 3697 3698 3699 3700 3701
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)
3702
  {
3703
    sql_mode|= (MODE_REAL_AS_FLOAT | MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
3704 3705 3706 3707 3708 3709
		MODE_IGNORE_SPACE);
    /* 
      MODE_ONLY_FULL_GROUP_BY removed from ANSI mode because it is currently
      overly restrictive (see BUG#8510).
    */
  }
3710 3711 3712 3713
  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 |
3714
		MODE_NO_FIELD_OPTIONS | MODE_NO_AUTO_CREATE_USER);
3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729
  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);
3730
  if (sql_mode & MODE_MAXDB)
3731 3732 3733
    sql_mode|= (MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE |
		MODE_NO_KEY_OPTIONS | MODE_NO_TABLE_OPTIONS |
3734
		MODE_NO_FIELD_OPTIONS | MODE_NO_AUTO_CREATE_USER);
3735
  if (sql_mode & MODE_MYSQL40)
3736
    sql_mode|= MODE_HIGH_NOT_PRECEDENCE;
3737
  if (sql_mode & MODE_MYSQL323)
3738
    sql_mode|= MODE_HIGH_NOT_PRECEDENCE;
3739 3740 3741
  if (sql_mode & MODE_TRADITIONAL)
    sql_mode|= (MODE_STRICT_TRANS_TABLES | MODE_STRICT_ALL_TABLES |
                MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE |
3742
                MODE_ERROR_FOR_DIVISION_BY_ZERO | MODE_NO_AUTO_CREATE_USER);
3743 3744
  return sql_mode;
}
3745 3746


3747 3748 3749 3750
/****************************************************************************
  Named list handling
****************************************************************************/

monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3751 3752
gptr find_named(I_List<NAMED_LIST> *list, const char *name, uint length,
		NAMED_LIST **found)
3753 3754 3755 3756 3757 3758
{
  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
3759
    {
3760 3761
      if (found)
        *found= element;
3762
      return element->data;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3763
    }
3764 3765 3766 3767 3768
  }
  return 0;
}


3769 3770
void delete_elements(I_List<NAMED_LIST> *list,
		     void (*free_element)(const char *name, gptr))
3771 3772
{
  NAMED_LIST *element;
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3773
  DBUG_ENTER("delete_elements");
3774 3775
  while ((element= list->get()))
  {
3776
    (*free_element)(element->name, element->data);
3777 3778
    delete element;
  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
3779
  DBUG_VOID_RETURN;
3780 3781 3782 3783 3784
}


/* Key cache functions */

3785
static KEY_CACHE *create_key_cache(const char *name, uint length)
3786
{
3787
  KEY_CACHE *key_cache;
3788 3789 3790
  DBUG_ENTER("create_key_cache");
  DBUG_PRINT("enter",("name: %.*s", length, name));
  
3791
  if ((key_cache= (KEY_CACHE*) my_malloc(sizeof(KEY_CACHE),
3792
					     MYF(MY_ZEROFILL | MY_WME))))
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3793 3794
  {
    if (!new NAMED_LIST(&key_caches, name, length, (gptr) key_cache))
3795 3796
    {
      my_free((char*) key_cache, MYF(0));
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3797
      key_cache= 0;
3798 3799 3800 3801 3802 3803 3804 3805 3806
    }
    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
      */
3807 3808 3809
      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;
3810
    }
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3811
  }
3812
  DBUG_RETURN(key_cache);
3813 3814 3815
}


3816
KEY_CACHE *get_or_create_key_cache(const char *name, uint length)
3817
{
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3818
  LEX_STRING key_cache_name;
3819
  KEY_CACHE *key_cache;
3820

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3821 3822
  key_cache_name.str= (char *) name;
  key_cache_name.length= length;
3823 3824
  pthread_mutex_lock(&LOCK_global_system_variables);
  if (!(key_cache= get_key_cache(&key_cache_name)))
3825
    key_cache= create_key_cache(name, length);
3826
  pthread_mutex_unlock(&LOCK_global_system_variables);
3827 3828 3829 3830
  return key_cache;
}


3831
void free_key_cache(const char *name, KEY_CACHE *key_cache)
3832
{
3833 3834
  ha_end_key_cache(key_cache);
  my_free((char*) key_cache, MYF(0));
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3835 3836 3837
}


3838
bool process_key_caches(int (* func) (const char *name, KEY_CACHE *))
3839
{
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3840 3841
  I_List_iterator<NAMED_LIST> it(key_caches);
  NAMED_LIST *element;
3842

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3843 3844
  while ((element= it++))
  {
3845
    KEY_CACHE *key_cache= (KEY_CACHE *) element->data;
3846
    func(element->name, key_cache);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
3847 3848
  }
  return 0;
3849 3850
}

3851

3852 3853
void sys_var_trust_routine_creators::warn_deprecated(THD *thd)
{
3854 3855
  WARN_DEPRECATED(thd, "5.2", "log_bin_trust_routine_creators",
                      "'log_bin_trust_function_creators'");
3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869
}

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

3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933
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.
  */
  if (close_cached_tables(thd, true, NULL, false))
    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
3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951
/* 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
  {
    DBUG_POP();
    DBUG_PUSH(var ? var->value->str_value.c_ptr() : "");
  }
  return 0;
}

3952

serg@serg.mylan's avatar
serg@serg.mylan committed
3953 3954 3955 3956 3957 3958 3959
byte *sys_var_thd_dbug::value_ptr(THD *thd, enum_var_type type, LEX_STRING *b)
{
  char buf[256];
  if (type == OPT_GLOBAL)
    DBUG_EXPLAIN_INITIAL(buf, sizeof(buf));
  else
    DBUG_EXPLAIN(buf, sizeof(buf));
andrey@lmy004's avatar
andrey@lmy004 committed
3960
  return (byte*) thd->strdup(buf);
serg@serg.mylan's avatar
serg@serg.mylan committed
3961
}
andrey@lmy004's avatar
andrey@lmy004 committed
3962

3963

3964 3965 3966 3967 3968 3969
bool sys_var_event_scheduler::check(THD *thd, set_var *var)
{
  return check_enum(thd, var, &Events::var_typelib);
}


3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987
/*
   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)
{
3988
  int res;
3989 3990
  /* here start the thread if not running. */
  DBUG_ENTER("sys_var_event_scheduler::update");
3991
  if (Events::opt_event_scheduler == Events::EVENTS_DISABLED)
3992
  {
3993
    my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--event-scheduler=DISABLED");
3994
    DBUG_RETURN(TRUE);
3995 3996 3997
  }

  DBUG_PRINT("new_value", ("%lu", (bool)var->save_result.ulong_value));
3998

3999 4000 4001
  Item_result var_type= var->value->result_type();

  if (var->save_result.ulong_value == Events::EVENTS_ON)
4002
    res= Events::get_instance()->start_execution_of_events();
4003
  else if (var->save_result.ulong_value == Events::EVENTS_OFF)
4004
    res= Events::get_instance()->stop_execution_of_events();
4005
  else
4006
  {
4007
    DBUG_ASSERT(0);
4008
  }
4009 4010
  if (res)
    my_error(ER_EVENT_SET_VAR_ERROR, MYF(0));
4011 4012 4013 4014 4015 4016 4017 4018

  DBUG_RETURN((bool) res);
}


byte *sys_var_event_scheduler::value_ptr(THD *thd, enum_var_type type,
                                         LEX_STRING *base)
{
4019 4020 4021
  int state;
  if (Events::opt_event_scheduler == Events::EVENTS_DISABLED)
    state= Events::EVENTS_DISABLED;              // This should be DISABLED
4022
  else if (Events::get_instance()->is_execution_of_events_started())
4023
    state= Events::EVENTS_ON;                    // This should be ON
4024
  else
4025
    state= Events::EVENTS_OFF;                   // This should be OFF
4026

4027
  return (byte*) Events::opt_typelib.type_names[state];
4028 4029 4030
}


4031 4032 4033 4034
/****************************************************************************
  Used templates
****************************************************************************/

4035
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
4036
template class List<set_var_base>;
4037
template class List_iterator_fast<set_var_base>;
4038
template class I_List_iterator<NAMED_LIST>;
4039
#endif