set_var.cc 43.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

   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:

  - 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'
  - 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.
27 28 29
  - If the variable should be changeable or one should be able to access it
    with @@variable_name, it should be added to the 'list of all variables'
    list in this file.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  - If the variable should be changed from the command line, add a definition
    of it in the my_option structure list in mysqld.dcc
  - If the variable should show up in 'show variables' add it to the
    init_vars[] struct in this file

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

#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
#include "slave.h"
#include "sql_acl.h"
#include <my_getopt.h>
#include <myisam.h>
#ifdef HAVE_BERKELEY_DB
#include "ha_berkeley.h"
#endif
#ifdef HAVE_INNOBASE_DB
#include "ha_innodb.h"
#endif

static HASH system_variable_hash;
const char *bool_type_names[]= { "OFF", "ON", NullS };
TYPELIB bool_typelib=
{
  array_elements(bool_type_names)-1, "", bool_type_names
};

66 67 68 69 70 71
const char *delay_key_write_type_names[]= { "OFF", "ON", "ALL", NullS };
TYPELIB delay_key_write_typelib=
{
  array_elements(delay_key_write_type_names)-1, "", delay_key_write_type_names
};

72 73 74 75 76 77 78 79 80 81
static bool sys_check_charset(THD *thd, set_var *var);
static bool sys_update_charset(THD *thd, set_var *var);
static void sys_set_default_charset(THD *thd, enum_var_type type);
static bool set_option_bit(THD *thd, set_var *var);
static bool set_option_autocommit(THD *thd, set_var *var);
static bool set_log_update(THD *thd, set_var *var);
static void fix_low_priority_updates(THD *thd, enum_var_type type);
static void fix_tx_isolation(THD *thd, enum_var_type type);
static void fix_net_read_timeout(THD *thd, enum_var_type type);
static void fix_net_write_timeout(THD *thd, enum_var_type type);
82
static void fix_net_retry_count(THD *thd, enum_var_type type);
83 84 85
static void fix_max_join_size(THD *thd, enum_var_type type);
static void fix_query_cache_size(THD *thd, enum_var_type type);
static void fix_key_buffer_size(THD *thd, enum_var_type type);
86 87
static byte *get_error_count(THD *thd);
static byte *get_warning_count(THD *thd);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

/*
  Variable definition list

  These are variables that can be set from the command line, in
  alphabetic order
*/

sys_var_long_ptr	sys_binlog_cache_size("binlog_cache_size",
					      &binlog_cache_size);
sys_var_thd_ulong	sys_bulk_insert_buff_size("bulk_insert_buffer_size",
						  &SV::bulk_insert_buff_size);
sys_var_str		sys_charset("character_set",
				    sys_check_charset,
				    sys_update_charset,
				    sys_set_default_charset);
sys_var_thd_conv_charset sys_convert_charset("convert_character_set");
sys_var_bool_ptr	sys_concurrent_insert("concurrent_insert",
					      &myisam_concurrent_insert);
sys_var_long_ptr	sys_connect_timeout("connect_timeout",
					    &connect_timeout);
109 110 111 112
sys_var_enum		sys_delay_key_write("delay_key_write",
					    &delay_key_write_options,
					    &delay_key_write_typelib,
					    fix_delay_key_write);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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);
sys_var_bool_ptr	sys_flush("flush", &myisam_flush);
sys_var_long_ptr	sys_flush_time("flush_time", &flush_time);
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);
sys_var_long_ptr	sys_key_buffer_size("key_buffer_size",
					    &keybuff_size,
					    fix_key_buffer_size);
sys_var_bool_ptr	sys_local_infile("local_infile",
					 &opt_local_infile);
sys_var_thd_bool	sys_log_warnings("log_warnings", &SV::log_warnings);
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",
					    &max_binlog_size);
sys_var_long_ptr	sys_max_connections("max_connections",
					    &max_connections);
sys_var_long_ptr	sys_max_connect_errors("max_connect_errors",
					       &max_connect_errors);
sys_var_long_ptr	sys_max_delayed_threads("max_delayed_threads",
						&max_insert_delayed_threads);
153 154
sys_var_thd_ulong	sys_max_error_count("max_error_count",
					    &SV::max_error_count);
155 156
sys_var_thd_ulong	sys_max_heap_table_size("max_heap_table_size",
						&SV::max_heap_table_size);
157
sys_var_thd_ulonglong	sys_max_join_size("max_join_size",
158 159 160
					  &SV::max_join_size,
					  fix_max_join_size);
#ifndef TO_BE_DELETED	/* Alias for max_join_size */
161
sys_var_thd_ulonglong	sys_sql_max_join_size("sql_max_join_size",
162 163 164
					      &SV::max_join_size,
					      fix_max_join_size);
#endif
165 166
sys_var_thd_ulong	sys_max_prep_stmt_count("max_prepared_statements",
						&SV::max_prep_stmt_count);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
sys_var_thd_ulong	sys_max_sort_length("max_sort_length",
					    &SV::max_sort_length);
sys_var_long_ptr	sys_max_user_connections("max_user_connections",
						 &max_user_connections);
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);
sys_var_thd_ulonglong	sys_myisam_max_extra_sort_file_size("myisam_max_extra_sort_file_size", &SV::myisam_max_extra_sort_file_size);
sys_var_thd_ulonglong	sys_myisam_max_sort_file_size("myisam_max_sort_file_size", &SV::myisam_max_sort_file_size);
sys_var_thd_ulong	sys_myisam_sort_buffer_size("myisam_sort_buffer_size", &SV::myisam_sort_buff_size);
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,
					     fix_net_read_timeout);
sys_var_thd_ulong	sys_net_write_timeout("net_write_timeout",
					      &SV::net_write_timeout,
					      fix_net_write_timeout);
186 187 188
sys_var_thd_ulong	sys_net_retry_count("net_retry_count",
					    &SV::net_retry_count,
					    fix_net_retry_count);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
sys_var_thd_ulong	sys_read_buff_size("read_buffer_size",
					   &SV::read_buff_size);
sys_var_thd_ulong	sys_read_rnd_buff_size("read_rnd_buffer_size",
					       &SV::read_rnd_buff_size);
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);
#ifdef HAVE_QUERY_CACHE
sys_var_long_ptr	sys_query_cache_limit("query_cache_limit",
					      &query_cache.query_cache_limit);
sys_var_thd_enum	sys_query_cache_type("query_cache_type",
					     &SV::query_cache_type,
					     &query_cache_type_typelib);
#endif /* HAVE_QUERY_CACHE */
sys_var_bool_ptr	sys_safe_show_db("safe_show_database",
					 &opt_safe_show_db);
sys_var_long_ptr	sys_server_id("server_id",&server_id);
208 209
sys_var_bool_ptr	sys_slave_compressed_protocol("slave_compressed_protocol",
						      &opt_slave_compressed_protocol);
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
sys_var_long_ptr	sys_slave_net_timeout("slave_net_timeout",
					      &slave_net_timeout);
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);
sys_var_thd_enum	sys_table_type("table_type", &SV::table_type,
				       &ha_table_typelib);
sys_var_long_ptr	sys_table_cache_size("table_cache",
					     &table_cache_size);
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,
					 fix_tx_isolation);
sys_var_thd_ulong	sys_tmp_table_size("tmp_table_size",
					   &SV::tmp_table_size);
sys_var_thd_ulong	sys_net_wait_timeout("wait_timeout",
					     &SV::net_wait_timeout);
/*
  Variables that are bits in THD
*/

static sys_var_thd_bit	sys_autocommit("autocommit",
				       set_option_autocommit,
				       OPTION_NOT_AUTOCOMMIT,
				       1);
static sys_var_thd_bit	sys_big_tables("big_tables",
				       set_option_bit,
				       OPTION_BIG_TABLES);
#ifndef TO_BE_DELETED	/* Alias for big_tables */
static sys_var_thd_bit	sys_sql_big_tables("sql_big_tables",
					   set_option_bit,
					   OPTION_BIG_TABLES);
#endif
static sys_var_thd_bit	sys_big_selects("sql_big_selects",
					set_option_bit,
					OPTION_BIG_TABLES);
static sys_var_thd_bit	sys_log_off("sql_log_off",
				    set_option_bit,
				    OPTION_LOG_OFF);
static sys_var_thd_bit	sys_log_update("sql_log_update",
				       set_log_update,
				       OPTION_UPDATE_LOG);
static sys_var_thd_bit	sys_log_binlog("sql_log_bin",
					set_log_update,
					OPTION_BIN_LOG);
static sys_var_thd_bit	sys_sql_warnings("sql_warnings",
					 set_option_bit,
					 OPTION_WARNINGS);
static sys_var_thd_bit	sys_auto_is_null("sql_auto_is_null",
					 set_option_bit,
					 OPTION_AUTO_IS_NULL);
static sys_var_thd_bit	sys_safe_updates("sql_safe_updates",
					 set_option_bit,
					 OPTION_SAFE_UPDATES);
static sys_var_thd_bit	sys_buffer_results("sql_buffer_result",
					   set_option_bit,
					   OPTION_BUFFER_RESULT);
static sys_var_thd_bit	sys_quote_show_create("sql_quote_show_create",
					      set_option_bit,
					      OPTION_QUOTE_SHOW_CREATE);
273 274 275 276 277 278 279 280 281
static sys_var_thd_bit	sys_foreign_key_checks("foreign_key_checks",
					       set_option_bit,
					       OPTION_NO_FOREIGN_KEY_CHECKS,
					       1);
static sys_var_thd_bit	sys_unique_checks("unique_checks",
					  set_option_bit,
					  OPTION_RELAXED_UNIQUE_CHECKS,
					  1);

282 283 284

/* Local state variables */

285
static sys_var_thd_ulonglong	sys_select_limit("sql_select_limit",
286 287 288 289 290
						 &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");
static sys_var_insert_id	sys_insert_id("insert_id");
291 292 293 294 295 296 297 298 299
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);

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
/* alias for last_insert_id() to be compatible with Sybase */
static sys_var_slave_skip_counter sys_slave_skip_counter("sql_slave_skip_counter");


/*
  List of all variables for initialisation and storage in hash
  This is sorted in alphabetical order to make it easy to add new variables

  If the variable is not in this list, it can't be changed with
  SET variable_name=
*/

sys_var *sys_variables[]=
{
  &sys_auto_is_null,
  &sys_autocommit,
  &sys_big_tables,
  &sys_big_selects,
  &sys_binlog_cache_size,
  &sys_buffer_results,
  &sys_bulk_insert_buff_size,
  &sys_concurrent_insert,
  &sys_connect_timeout,
  &sys_convert_charset,
  &sys_delay_key_write,
  &sys_delayed_insert_limit,
  &sys_delayed_insert_timeout,
  &sys_delayed_queue_size,
328
  &sys_error_count,
329 330
  &sys_flush,
  &sys_flush_time,
331
  &sys_foreign_key_checks,
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  &sys_identity,
  &sys_insert_id,
  &sys_interactive_timeout,
  &sys_join_buffer_size,
  &sys_key_buffer_size,
  &sys_last_insert_id,
  &sys_local_infile,
  &sys_log_binlog,
  &sys_log_off,
  &sys_log_update,
  &sys_log_warnings,
  &sys_long_query_time,
  &sys_low_priority_updates,
  &sys_max_allowed_packet,
  &sys_max_binlog_cache_size,
  &sys_max_binlog_size,
  &sys_max_connect_errors,
  &sys_max_connections,
  &sys_max_delayed_threads,
351
  &sys_max_error_count,
352 353
  &sys_max_heap_table_size,
  &sys_max_join_size,
354
  &sys_max_prep_stmt_count,
355 356 357 358 359 360 361 362 363
  &sys_max_sort_length,
  &sys_max_tmp_tables,
  &sys_max_user_connections,
  &sys_max_write_lock_count,
  &sys_myisam_max_extra_sort_file_size,
  &sys_myisam_max_sort_file_size,
  &sys_myisam_sort_buffer_size,
  &sys_net_buffer_length,
  &sys_net_read_timeout,
364
  &sys_net_retry_count,
365 366 367 368 369 370
  &sys_net_wait_timeout,
  &sys_net_write_timeout,
  &sys_query_cache_size,
#ifdef HAVE_QUERY_CACHE
  &sys_query_cache_limit,
  &sys_query_cache_type,
371
#endif /* HAVE_QUERY_CACHE */
372 373 374 375 376 377 378 379
  &sys_quote_show_create,
  &sys_read_buff_size,
  &sys_read_rnd_buff_size,
  &sys_rpl_recovery_rank,
  &sys_safe_show_db,
  &sys_safe_updates,
  &sys_select_limit,
  &sys_server_id,
380
  &sys_slave_compressed_protocol,
381 382 383 384 385 386 387 388 389 390 391 392 393 394
  &sys_slave_net_timeout,
  &sys_slave_skip_counter,
  &sys_slow_launch_time,
  &sys_sort_buffer,
  &sys_sql_big_tables,
  &sys_sql_low_priority_updates,
  &sys_sql_max_join_size,
  &sys_sql_warnings,
  &sys_table_cache_size,
  &sys_table_type,
  &sys_thread_cache_size,
  &sys_timestamp,
  &sys_tmp_table_size,
  &sys_tx_isolation,
395 396
  &sys_unique_checks,
  &sys_warning_count
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
};


/*
  Variables shown by SHOW variables in alphabetical order
*/

struct show_var_st init_vars[]= {
  {"back_log",                (char*) &back_log,                    SHOW_LONG},
  {"basedir",                 mysql_home,                           SHOW_CHAR},
#ifdef HAVE_BERKELEY_DB
  {"bdb_cache_size",          (char*) &berkeley_cache_size,         SHOW_LONG},
  {"bdb_log_buffer_size",     (char*) &berkeley_log_buffer_size,    SHOW_LONG},
  {"bdb_home",                (char*) &berkeley_home,               SHOW_CHAR_PTR},
  {"bdb_max_lock",            (char*) &berkeley_max_lock,	    SHOW_LONG},
  {"bdb_logdir",              (char*) &berkeley_logdir,             SHOW_CHAR_PTR},
  {"bdb_shared_data",	      (char*) &berkeley_shared_data,	    SHOW_BOOL},
  {"bdb_tmpdir",              (char*) &berkeley_tmpdir,             SHOW_CHAR_PTR},
  {"bdb_version",             (char*) DB_VERSION_STRING,            SHOW_CHAR},
#endif
  {sys_binlog_cache_size.name,(char*) &sys_binlog_cache_size,	    SHOW_SYS},
  {sys_bulk_insert_buff_size.name,(char*) &sys_bulk_insert_buff_size,SHOW_SYS},
  {sys_charset.name, 	      (char*) &sys_charset,		     SHOW_SYS},
  {"character_sets",          (char*) &charsets_list,               SHOW_CHAR_PTR},
  {sys_concurrent_insert.name,(char*) &sys_concurrent_insert,       SHOW_SYS},
  {sys_connect_timeout.name,  (char*) &sys_connect_timeout,         SHOW_SYS},
  {sys_convert_charset.name,  (char*) &sys_convert_charset,	    SHOW_SYS},
  {"datadir",                 mysql_real_data_home,                 SHOW_CHAR},
  {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},
  {sys_flush.name,             (char*) &sys_flush,                  SHOW_SYS},
  {sys_flush_time.name,        (char*) &sys_flush_time,             SHOW_SYS},
  {"ft_min_word_len",         (char*) &ft_min_word_len,             SHOW_LONG},
  {"ft_max_word_len",         (char*) &ft_max_word_len,             SHOW_LONG},
  {"ft_max_word_len_for_sort",(char*) &ft_max_word_len_for_sort,    SHOW_LONG},
  {"ft_boolean_syntax",       (char*) ft_boolean_syntax,	    SHOW_CHAR},
  {"have_bdb",		      (char*) &have_berkeley_db,	    SHOW_HAVE},
  {"have_innodb",	      (char*) &have_innodb,		    SHOW_HAVE},
  {"have_isam",	      	      (char*) &have_isam,		    SHOW_HAVE},
  {"have_raid",		      (char*) &have_raid,		    SHOW_HAVE},
  {"have_symlink",            (char*) &have_symlink,         	    SHOW_HAVE},
  {"have_openssl",	      (char*) &have_openssl,		    SHOW_HAVE},
  {"have_query_cache",        (char*) &have_query_cache,            SHOW_HAVE},
  {"init_file",               (char*) &opt_init_file,               SHOW_CHAR_PTR},
#ifdef HAVE_INNOBASE_DB
  {"innodb_additional_mem_pool_size", (char*) &innobase_additional_mem_pool_size, SHOW_LONG },
  {"innodb_buffer_pool_size", (char*) &innobase_buffer_pool_size, SHOW_LONG },
  {"innodb_data_file_path", (char*) &innobase_data_file_path,	    SHOW_CHAR_PTR},
  {"innodb_data_home_dir",  (char*) &innobase_data_home_dir,	    SHOW_CHAR_PTR},
  {"innodb_file_io_threads", (char*) &innobase_file_io_threads, SHOW_LONG },
  {"innodb_force_recovery", (char*) &innobase_force_recovery, SHOW_LONG },
  {"innodb_thread_concurrency", (char*) &innobase_thread_concurrency, SHOW_LONG },
451
  {"innodb_flush_log_at_trx_commit", (char*) &innobase_flush_log_at_trx_commit, SHOW_LONG},
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
  {"innodb_fast_shutdown", (char*) &innobase_fast_shutdown, SHOW_MY_BOOL},
  {"innodb_flush_method",    (char*) &innobase_unix_file_flush_method, SHOW_CHAR_PTR},
  {"innodb_lock_wait_timeout", (char*) &innobase_lock_wait_timeout, SHOW_LONG },
  {"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 },
  {"innodb_log_file_size", (char*) &innobase_log_file_size, SHOW_LONG},
  {"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},
  {"innodb_mirrored_log_groups", (char*) &innobase_mirrored_log_groups, SHOW_LONG},
#endif
  {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},
  {"language",                language,                             SHOW_CHAR},
  {"large_files_support",     (char*) &opt_large_files,             SHOW_BOOL},	
  {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_update",              (char*) &opt_update_log,              SHOW_BOOL},
  {"log_bin",                 (char*) &opt_bin_log,                 SHOW_BOOL},
475
  {"log_slave_updates",       (char*) &opt_log_slave_updates,       SHOW_MY_BOOL},
476 477 478 479 480 481 482 483 484 485
  {"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},
  {"lower_case_table_names",  (char*) &lower_case_table_names,      SHOW_MY_BOOL},
  {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_connections.name,    (char*) &sys_max_connections,	    SHOW_SYS},
  {sys_max_connect_errors.name, (char*) &sys_max_connect_errors,    SHOW_SYS},
486
  {sys_max_error_count.name,	(char*) &sys_max_error_count,	    SHOW_SYS},
487 488 489
  {sys_max_delayed_threads.name,(char*) &sys_max_delayed_threads,   SHOW_SYS},
  {sys_max_heap_table_size.name,(char*) &sys_max_heap_table_size,   SHOW_SYS},
  {sys_max_join_size.name,	(char*) &sys_max_join_size,	    SHOW_SYS},
490
  {sys_max_prep_stmt_count.name,(char*) &sys_max_prep_stmt_count,   SHOW_SYS},
491 492 493 494 495 496 497 498 499 500 501 502 503
  {sys_max_sort_length.name,	(char*) &sys_max_sort_length,	    SHOW_SYS},
  {sys_max_user_connections.name,(char*) &sys_max_user_connections, SHOW_SYS},
  {sys_max_tmp_tables.name,	(char*) &sys_max_tmp_tables,   	    SHOW_SYS},
  {sys_max_write_lock_count.name, (char*) &sys_max_write_lock_count,SHOW_SYS},
  {sys_myisam_max_extra_sort_file_size.name,
   (char*) &sys_myisam_max_extra_sort_file_size,
   SHOW_SYS},
  {sys_myisam_max_sort_file_size.name,
   (char*) &sys_myisam_max_sort_file_size,
   SHOW_SYS},
  {"myisam_recover_options",  (char*) &myisam_recover_options_str,  SHOW_CHAR_PTR},
  {sys_myisam_sort_buffer_size.name, (char*) &sys_myisam_sort_buffer_size, SHOW_SYS},
#ifdef __NT__
504
  {"named_pipe",	      (char*) &opt_enable_named_pipe,       SHOW_MY_BOOL},
505 506 507
#endif
  {sys_net_buffer_length.name,(char*) &sys_net_buffer_length,       SHOW_SYS},
  {sys_net_read_timeout.name, (char*) &sys_net_read_timeout,        SHOW_SYS},
508
  {sys_net_retry_count.name,  (char*) &sys_net_retry_count,	    SHOW_SYS},
509 510 511 512 513 514 515 516
  {sys_net_write_timeout.name,(char*) &sys_net_write_timeout,       SHOW_SYS},
  {"open_files_limit",	      (char*) &open_files_limit,	    SHOW_LONG},
  {"pid_file",                (char*) pidfile_name,                 SHOW_CHAR},
  {"port",                    (char*) &mysql_port,                  SHOW_INT},
  {"protocol_version",        (char*) &protocol_version,            SHOW_INT},
  {sys_read_buff_size.name,   (char*) &sys_read_buff_size,	    SHOW_SYS},
  {sys_read_rnd_buff_size.name,(char*) &sys_read_rnd_buff_size,	    SHOW_SYS},
  {sys_rpl_recovery_rank.name,(char*) &sys_rpl_recovery_rank,       SHOW_SYS},
517
#ifdef HAVE_QUERY_CACHE
518 519 520 521 522 523 524
  {sys_query_cache_limit.name,(char*) &sys_query_cache_limit,	    SHOW_SYS},
  {sys_query_cache_size.name, (char*) &sys_query_cache_size,	    SHOW_SYS},
  {sys_query_cache_type.name, (char*) &sys_query_cache_type,        SHOW_SYS},
#endif /* HAVE_QUERY_CACHE */
  {sys_safe_show_db.name,     (char*) &sys_safe_show_db,            SHOW_SYS},
  {sys_server_id.name,	      (char*) &sys_server_id,		    SHOW_SYS},
  {sys_slave_net_timeout.name,(char*) &sys_slave_net_timeout,	    SHOW_SYS},
525
  {"skip_external_locking",   (char*) &my_disable_locking,          SHOW_MY_BOOL},
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
  {"skip_networking",         (char*) &opt_disable_networking,      SHOW_BOOL},
  {"skip_show_database",      (char*) &opt_skip_show_db,            SHOW_BOOL},
  {sys_slow_launch_time.name, (char*) &sys_slow_launch_time,        SHOW_SYS},
  {"socket",                  (char*) &mysql_unix_port,             SHOW_CHAR_PTR},
  {sys_sort_buffer.name,      (char*) &sys_sort_buffer, 	    SHOW_SYS},
  {"sql_mode",                (char*) &opt_sql_mode,                SHOW_LONG},
  {"table_cache",             (char*) &table_cache_size,            SHOW_LONG},
  {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},
  {sys_tx_isolation.name,     (char*) &sys_tx_isolation,	    SHOW_SYS},
#ifdef HAVE_TZNAME
  {"timezone",                time_zone,                            SHOW_CHAR},
#endif
  {sys_tmp_table_size.name,   (char*) &sys_tmp_table_size,	    SHOW_SYS},
544
  {"tmpdir",                  (char*) &opt_mysql_tmpdir,            SHOW_CHAR_PTR},
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
  {"version",                 server_version,                       SHOW_CHAR},
  {sys_net_wait_timeout.name, (char*) &sys_net_wait_timeout,	    SHOW_SYS},
  {NullS, NullS, SHOW_LONG}
};

/*
  Functions to check and update variables
*/

/*
  The following 3 functions need to be changed in 4.1 when we allow
  one to change character sets
*/

static bool sys_check_charset(THD *thd, set_var *var)
{
  return 0;
}


static bool sys_update_charset(THD *thd, set_var *var)
{
  return 0;
}


static void sys_set_default_charset(THD *thd, enum_var_type type)
{
}


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


/*
  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)
  {
597
    if (thd->variables.max_join_size == (ulonglong) HA_POS_ERROR)
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
      thd->options|= OPTION_BIG_SELECTS;
    else
      thd->options&= ~OPTION_BIG_SELECTS;
  }
}
  

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


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

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

635 636 637 638 639 640
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;
}

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655

static void fix_query_cache_size(THD *thd, enum_var_type type)
{
#ifdef HAVE_QUERY_CACHE
  query_cache.resize(query_cache_size);
#endif
}


static void fix_key_buffer_size(THD *thd, enum_var_type type)
{
  ha_resize_key_cache();
}


656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
void fix_delay_key_write(THD *thd, enum_var_type type)
{
  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;
  }
}


673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
bool sys_var_long_ptr::update(THD *thd, set_var *var)
{
  ulonglong tmp= var->value->val_int();
  if (option_limits)
    *value= (ulong) getopt_ull_limit_value(tmp, option_limits);
  else
    *value= (ulong) tmp;
  return 0;
}


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


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


703 704 705 706 707 708 709 710 711 712 713 714 715
bool sys_var_enum::update(THD *thd, set_var *var)
{
  *value= (uint) var->save_result.ulong_value;
  return 0;
}


byte *sys_var_enum::value_ptr(THD *thd, enum_var_type type)
{
  return (byte*) enum_names->type_names[*value];
}


716 717 718 719 720 721 722 723 724 725 726 727 728 729
bool sys_var_thd_ulong::update(THD *thd, set_var *var)
{
  ulonglong tmp= var->value->val_int();

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

  if (option_limits)
    tmp= (ulong) 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);    
730
    global_system_variables.*offset= (ulong) tmp;
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
  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;
}


byte *sys_var_thd_ulong::value_ptr(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
    return (byte*) &(global_system_variables.*offset);
  return (byte*) &(thd->variables.*offset);
}


bool sys_var_thd_ulonglong::update(THD *thd,  set_var *var)
{
  if (var->type == OPT_GLOBAL)
    global_system_variables.*offset= var->value->val_int();
  else
    thd->variables.*offset= var->value->val_int();
  return 0;
}


void sys_var_thd_ulonglong::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
772
    global_system_variables.*offset= (ulonglong) option_limits->def_value;
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
  else
    thd->variables.*offset= global_system_variables.*offset;
}


byte *sys_var_thd_ulonglong::value_ptr(THD *thd, enum_var_type type)
{
  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;
}


byte *sys_var_thd_bool::value_ptr(THD *thd, enum_var_type type)
{
  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)
{
  char buff[80], *value;
816
  String str(buff, sizeof(buff), system_charset_info), *res;
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853

  if (var->value->result_type() == STRING_RESULT)
  {
    if (!(res=var->value->val_str(&str)) ||
	((long) (var->save_result.ulong_value=
		 (ulong) find_type(res->c_ptr(), enum_names, 3)-1))
	< 0)
    {
      value=res->c_ptr();
      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:
  my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, value);
  return 1;
}

/*
  Return an Item for a variable.  Used with @@[global.]variable_name

  If type is not given, return local value if exists, else global

  We have to use netprintf() instead of my_error() here as this is
  called on the parsing stage.
854 855 856 857

  TODO:
    With prepared statements/stored procedures this has to be fixed
    to create an item that gets the current value at fix_fields() stage.
858 859 860 861 862 863 864 865
*/

Item *sys_var::item(THD *thd, enum_var_type var_type)
{
  if (check_type(var_type))
  {
    if (var_type != OPT_DEFAULT)
    {
866
      net_printf(thd,
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
		 var_type == OPT_GLOBAL ? ER_LOCAL_VARIABLE :
		 ER_GLOBAL_VARIABLE, name);
      return 0;
    }
    /* As there was no local variable, return the global value */
    var_type= OPT_GLOBAL;
  }
  switch (type()) {
  case SHOW_LONG:
    return new Item_uint((int32) *(ulong*) value_ptr(thd, var_type));
  case SHOW_LONGLONG:
    return new Item_int(*(longlong*) value_ptr(thd, var_type));
  case SHOW_MY_BOOL:
    return new Item_int((int32) *(my_bool*) value_ptr(thd, var_type),1);
  case SHOW_CHAR:
  {
    char *str= (char*) value_ptr(thd, var_type);
884
    return new Item_string(str, strlen(str), system_charset_info);
885 886
  }
  default:
887
    net_printf(thd, ER_VAR_CANT_BE_READ, name);
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
  }
  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;
}


byte *sys_var_thd_enum::value_ptr(THD *thd, enum_var_type type)
{
  ulong tmp= ((type == OPT_GLOBAL) ?
	      global_system_variables.*offset :
	      thd->variables.*offset);
  return (byte*) enum_names->type_names[tmp];
}


bool sys_var_thd_bit::update(THD *thd, set_var *var)
{
923
  int res= (*update_func)(thd, var);
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
  thd->lex.select_lex.options=thd->options;
  return res;
}


byte *sys_var_thd_bit::value_ptr(THD *thd, enum_var_type type)
{
  /*
    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;
}


bool sys_var_thd_conv_charset::check(THD *thd, set_var *var)
{
  CONVERT *tmp;
  char buff[80];
945
  String str(buff,sizeof(buff), system_charset_info), *res;
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036

  if (!var->value)					// Default value
  {
    var->save_result.convert= (var->type != OPT_GLOBAL ?
			       global_system_variables.convert_set
			       : (CONVERT*) 0);
    return 0;
  }
  if (!(res=var->value->val_str(&str)))
    res= &empty_string;

  if (!(tmp=get_convert_set(res->c_ptr())))
  {
    my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), res->c_ptr());
    return 1;
  }
  var->save_result.convert=tmp;			// Save for update
  return 0;
}


bool sys_var_thd_conv_charset::update(THD *thd, set_var *var)
{
  if (var->type == OPT_GLOBAL)
    global_system_variables.convert_set= var->save_result.convert;
  else
    thd->lex.convert_set= thd->variables.convert_set=
      var->save_result.convert;
  return 0;
}


byte *sys_var_thd_conv_charset::value_ptr(THD *thd, enum_var_type type)
{
  CONVERT *conv= ((type == OPT_GLOBAL) ?
		  global_system_variables.convert_set :
		  thd->variables.convert_set);
  return conv ? (byte*) conv->name : (byte*) "";
}



bool sys_var_timestamp::update(THD *thd,  set_var *var)
{
  thd->set_time((time_t) var->value->val_int());
  return 0;
}


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


byte *sys_var_timestamp::value_ptr(THD *thd, enum_var_type type)
{
  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)
{
  thd->insert_id(var->value->val_int());
  return 0;
}


byte *sys_var_last_insert_id::value_ptr(THD *thd, enum_var_type type)
{
  thd->sys_var_tmp.long_value= (long) thd->insert_id();
  return (byte*) &thd->last_insert_id;
}


bool sys_var_insert_id::update(THD *thd, set_var *var)
{
  thd->next_insert_id=var->value->val_int();
  return 0;
}


byte *sys_var_insert_id::value_ptr(THD *thd, enum_var_type type)
{
  return (byte*) &thd->current_insert_id;
}


bool sys_var_slave_skip_counter::check(THD *thd, set_var *var)
{
1037
  int result= 0;
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
  LOCK_ACTIVE_MI;
  pthread_mutex_lock(&active_mi->rli.run_lock);
  if (active_mi->rli.slave_running)
  {
    my_error(ER_SLAVE_MUST_STOP, MYF(0));
    result=1;
  }
  pthread_mutex_unlock(&active_mi->rli.run_lock);
  UNLOCK_ACTIVE_MI;
  return result;
}


bool sys_var_slave_skip_counter::update(THD *thd, set_var *var)
{
  LOCK_ACTIVE_MI;
  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);
1063
    active_mi->rli.slave_skip_counter= (ulong) var->value->val_int();
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
    pthread_mutex_unlock(&active_mi->rli.data_lock);
  }
  pthread_mutex_unlock(&active_mi->rli.run_lock);
  UNLOCK_ACTIVE_MI;
  return 0;
}


/*
  Functions to update thd->options bits
*/

static bool set_option_bit(THD *thd, set_var *var)
{
1078 1079 1080
  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;
1081
  else
1082
    thd->options|= sys_var->bit_flag;
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
  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 */
      thd->options&= ~(ulong) (OPTION_BEGIN | OPTION_STATUS_NO_TRANS_UPDATE);
      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;
}


static bool set_log_update(THD *thd, set_var *var)
{
  if (opt_sql_bin_update)
    ((sys_var_thd_bit*) var->var)->bit_flag|= (OPTION_BIN_LOG |
					       OPTION_UPDATE_LOG);
  set_option_bit(thd, var);
  return 0;
}

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
static byte *get_warning_count(THD *thd)
{
  thd->sys_var_tmp.long_value=
    (thd->warn_count[(uint) MYSQL_ERROR::WARN_LEVEL_NOTE] +
     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;
}

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

/****************************************************************************
  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()
{
  extern struct my_option my_long_options[];	// From mysqld

1203 1204
  hash_init(&system_variable_hash, system_charset_info,
	    array_elements(sys_variables),0,0,
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	    (hash_get_key) get_sys_var_length,0, HASH_CASE_INSENSITIVE);
  sys_var **var, **end;
  for (var= sys_variables, end= sys_variables+array_elements(sys_variables) ;
       var < end;
       var++)
  {
    (*var)->name_length= strlen((*var)->name);
    (*var)->option_limits= find_option(my_long_options, (*var)->name);
    hash_insert(&system_variable_hash, (byte*) *var);
  }

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

  NOTE
    We have to use net_printf() as this is called during the parsing stage

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

1249
sys_var *find_sys_var(const char *str, uint length)
1250
{
1251 1252
  sys_var *var= (sys_var*) hash_search(&system_variable_hash,
				       (byte*) str,
1253 1254 1255
				       length ? length :
				       strlen(str));
  if (!var)
1256
    net_printf(current_thd, ER_UNKNOWN_SYSTEM_VARIABLE, (char*) str);
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
  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
1279 1280
    1	ERROR, message sent (normally no variables was updated)
    -1  ERROR, message not sent
1281 1282
*/

1283
int sql_set_variables(THD *thd, List<set_var_base> *var_list)
1284
{
1285
  int error= 0;
1286 1287 1288 1289 1290
  List_iterator<set_var_base> it(*var_list);

  set_var_base *var;
  while ((var=it++))
  {
1291 1292
    if ((error=var->check(thd)))
      return error;
1293 1294 1295
  }
  it.rewind();
  while ((var=it++))
1296
    error|= var->update(thd);			// Returns 0, -1 or 1
1297 1298 1299 1300 1301 1302 1303 1304
  return error;
}


/*****************************************************************************
  Functions to handle SET mysql_internal_variable=const_expr
*****************************************************************************/

1305
int set_var::check(THD *thd)
1306 1307 1308 1309 1310 1311
{
  if (var->check_type(type))
  {
    my_error(type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE,
	     MYF(0),
	     var->name);
1312
    return -1;
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
  }
  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))
    {
      my_error(ER_NO_DEFAULT, MYF(0), var->name);
1323
      return -1;
1324 1325 1326 1327
    }
    return 0;
  }

1328
  if (value->fix_fields(thd, 0, &value))
1329
    return -1;
1330 1331 1332
  if (var->check_update_type(value->result_type()))
  {
    my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var->name);
1333
    return -1;
1334
  }    
1335
  return var->check(thd, this) ? -1 : 0;
1336 1337 1338
}


1339
int set_var::update(THD *thd)
1340 1341 1342 1343
{
  if (!value)
    var->set_default(thd, type);
  else if (var->update(thd, this))
1344
    return -1;				// should never happen
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
  if (var->after_update)
    (*var->after_update)(thd, type);
  return 0;
}


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

1355
int set_var_user::check(THD *thd)
1356
{
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1357
  return user_var_item->fix_fields(thd,0, (Item**) 0) ? -1 : 0;
1358 1359 1360
}


1361
int set_var_user::update(THD *thd)
1362 1363 1364 1365
{
  if (user_var_item->update())
  {
    /* Give an error if it's not given already */
1366 1367
    my_error(ER_SET_CONSTANTS_ONLY, MYF(0));
    return -1;
1368 1369 1370 1371 1372 1373 1374 1375 1376
  }
  return 0;
}


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

1377
int set_var_password::check(THD *thd)
1378 1379 1380
{
  if (!user->host.str)
    user->host.str= (char*) thd->host_or_ip;
1381 1382
  /* Returns 1 as the function sends error to client */
  return check_change_password(thd, user->host.str, user->user.str) ? 1 : 0;
1383 1384
}

1385
int set_var_password::update(THD *thd)
1386
{
1387 1388 1389
  /* Returns 1 as the function sends error to client */
  return (change_password(thd, user->host.str, user->user.str, password) ?
	  1 : 0);
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
}

/****************************************************************************
  Used templates
****************************************************************************/

#ifdef __GNUC__
template class List<set_var_base>;
template class List_iterator<set_var_base>;
#endif