set_var.cc 98.3 KB
Newer Older
1
/* Copyright (C) 2000-2003 MySQL AB
unknown's avatar
unknown committed
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 should be changeable or one should be able to access it
    with @@variable_name, it should be added to the 'list of all variables'
unknown's avatar
unknown committed
27 28 29
    list (sys_variables) in this file.
  - If the variable is thread specific, add it to 'system_variables' struct.
    If not, add it to mysqld.cc and an declaration in 'mysql_priv.h'
unknown's avatar
unknown committed
30 31
  - If the variable should be changed from the command line, add a definition
    of it in the my_option structure list in mysqld.dcc
unknown's avatar
unknown committed
32 33
  - Don't forget to initialize new fields in global_system_variables and
    max_system_variables!
unknown's avatar
unknown committed
34 35 36
  - If the variable should show up in 'show variables' add it to the
    init_vars[] struct in this file

37 38 39 40 41 42
  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).

unknown's avatar
unknown committed
43 44 45 46 47 48 49 50 51 52 53 54 55
  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"
56
#include <mysql.h>
unknown's avatar
unknown committed
57 58
#include "slave.h"
#include <my_getopt.h>
59
#include <thr_alarm.h>
unknown's avatar
unknown committed
60 61 62 63 64 65 66
#include <myisam.h>
#ifdef HAVE_BERKELEY_DB
#include "ha_berkeley.h"
#endif
#ifdef HAVE_INNOBASE_DB
#include "ha_innodb.h"
#endif
unknown's avatar
unknown committed
67 68 69
#ifdef HAVE_NDBCLUSTER_DB
#include "ha_ndbcluster.h"
#endif
unknown's avatar
unknown committed
70 71 72 73 74

static HASH system_variable_hash;
const char *bool_type_names[]= { "OFF", "ON", NullS };
TYPELIB bool_typelib=
{
75
  array_elements(bool_type_names)-1, "", bool_type_names, NULL
unknown's avatar
unknown committed
76 77
};

78 79 80
const char *delay_key_write_type_names[]= { "OFF", "ON", "ALL", NullS };
TYPELIB delay_key_write_typelib=
{
81 82
  array_elements(delay_key_write_type_names)-1, "",
  delay_key_write_type_names, NULL
83 84
};

85
static int  sys_check_charset(THD *thd, set_var *var);
unknown's avatar
unknown committed
86 87
static bool sys_update_charset(THD *thd, set_var *var);
static void sys_set_default_charset(THD *thd, enum_var_type type);
88
static int  sys_check_ftb_syntax(THD *thd,  set_var *var);
unknown's avatar
unknown committed
89 90
static bool sys_update_ftb_syntax(THD *thd, set_var * var);
static void sys_default_ftb_syntax(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
91 92 93 94
static bool sys_update_init_connect(THD*, set_var*);
static void sys_default_init_connect(THD*, enum_var_type type);
static bool sys_update_init_slave(THD*, set_var*);
static void sys_default_init_slave(THD*, enum_var_type type);
unknown's avatar
unknown committed
95 96
static bool set_option_bit(THD *thd, set_var *var);
static bool set_option_autocommit(THD *thd, set_var *var);
97
static int  check_log_update(THD *thd, set_var *var);
unknown's avatar
unknown committed
98
static bool set_log_update(THD *thd, set_var *var);
99
static int  check_pseudo_thread_id(THD *thd, set_var *var);
unknown's avatar
unknown committed
100 101 102 103
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);
104
static void fix_net_retry_count(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
105 106
static void fix_max_join_size(THD *thd, enum_var_type type);
static void fix_query_cache_size(THD *thd, enum_var_type type);
107
static void fix_query_cache_min_res_unit(THD *thd, enum_var_type type);
108 109
static void fix_myisam_max_extra_sort_file_size(THD *thd, enum_var_type type);
static void fix_myisam_max_sort_file_size(THD *thd, enum_var_type type);
110 111
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);
112
static void fix_max_connections(THD *thd, enum_var_type type);
113
static int check_max_delayed_threads(THD *thd, set_var *var);
114 115
static void fix_thd_mem_root(THD *thd, enum_var_type type);
static void fix_trans_mem_root(THD *thd, enum_var_type type);
116
static void fix_server_id(THD *thd, enum_var_type type);
unknown's avatar
unknown committed
117
static KEY_CACHE *create_key_cache(const char *name, uint length);
118
void fix_sql_mode_var(THD *thd, enum_var_type type);
119 120
static byte *get_error_count(THD *thd);
static byte *get_warning_count(THD *thd);
unknown's avatar
unknown committed
121 122 123 124 125 126 127 128 129 130 131 132

/*
  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);
133 134
sys_var_character_set_server	sys_character_set_server("character_set_server");
sys_var_str			sys_charset_system("character_set_system",
135 136
				    sys_check_charset,
				    sys_update_charset,
137 138
				    sys_set_default_charset,
                                    (char *)my_charset_utf8_general_ci.name);
139
sys_var_character_set_database	sys_character_set_database("character_set_database");
140
sys_var_character_set_client  sys_character_set_client("character_set_client");
unknown's avatar
unknown committed
141
sys_var_character_set_connection  sys_character_set_connection("character_set_connection");
142
sys_var_character_set_results sys_character_set_results("character_set_results");
unknown's avatar
unknown committed
143
sys_var_collation_connection sys_collation_connection("collation_connection");
144 145
sys_var_collation_database sys_collation_database("collation_database");
sys_var_collation_server sys_collation_server("collation_server");
unknown's avatar
unknown committed
146 147 148 149
sys_var_bool_ptr	sys_concurrent_insert("concurrent_insert",
					      &myisam_concurrent_insert);
sys_var_long_ptr	sys_connect_timeout("connect_timeout",
					    &connect_timeout);
150 151 152 153
sys_var_enum		sys_delay_key_write("delay_key_write",
					    &delay_key_write_options,
					    &delay_key_write_typelib,
					    fix_delay_key_write);
unknown's avatar
unknown committed
154 155 156 157 158 159
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);
unknown's avatar
unknown committed
160 161
sys_var_long_ptr	sys_expire_logs_days("expire_logs_days",
					     &expire_logs_days);
unknown's avatar
unknown committed
162 163
sys_var_bool_ptr	sys_flush("flush", &myisam_flush);
sys_var_long_ptr	sys_flush_time("flush_time", &flush_time);
unknown's avatar
unknown committed
164 165 166
sys_var_str             sys_ft_boolean_syntax("ft_boolean_syntax",
                                         sys_check_ftb_syntax,
                                         sys_update_ftb_syntax,
167 168
                                         sys_default_ftb_syntax,
                                         ft_boolean_syntax);
unknown's avatar
unknown committed
169 170
sys_var_str             sys_init_connect("init_connect", 0,
                                         sys_update_init_connect,
171
                                         sys_default_init_connect,0);
unknown's avatar
unknown committed
172 173
sys_var_str             sys_init_slave("init_slave", 0,
                                       sys_update_init_slave,
174
                                       sys_default_init_slave,0);
unknown's avatar
unknown committed
175 176 177 178
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);
179
sys_var_key_buffer_size	sys_key_buffer_size("key_buffer_size");
180
sys_var_key_cache_long  sys_key_cache_block_size("key_cache_block_size",
unknown's avatar
unknown committed
181 182
						 offsetof(KEY_CACHE,
							  param_block_size));
183
sys_var_key_cache_long	sys_key_cache_division_limit("key_cache_division_limit",
unknown's avatar
unknown committed
184 185
						     offsetof(KEY_CACHE,
							      param_division_limit));
186
sys_var_key_cache_long  sys_key_cache_age_threshold("key_cache_age_threshold",
unknown's avatar
unknown committed
187 188
						     offsetof(KEY_CACHE,
							      param_age_threshold));
unknown's avatar
unknown committed
189 190
sys_var_bool_ptr	sys_local_infile("local_infile",
					 &opt_local_infile);
191
sys_var_thd_ulong	sys_log_warnings("log_warnings", &SV::log_warnings);
unknown's avatar
unknown committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
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",
207 208
					    &max_binlog_size,
                                            fix_max_binlog_size);
unknown's avatar
unknown committed
209
sys_var_long_ptr	sys_max_connections("max_connections",
210 211
					    &max_connections,
                                            fix_max_connections);
unknown's avatar
unknown committed
212 213
sys_var_long_ptr	sys_max_connect_errors("max_connect_errors",
					       &max_connect_errors);
214
sys_var_thd_ulong       sys_max_insert_delayed_threads("max_insert_delayed_threads",
215 216 217
						       &SV::max_insert_delayed_threads,
                                                       check_max_delayed_threads,
                                                       fix_max_connections);
218 219
sys_var_thd_ulong	sys_max_delayed_threads("max_delayed_threads",
						&SV::max_insert_delayed_threads,
220 221
                                                check_max_delayed_threads,
                                                fix_max_connections);
222 223
sys_var_thd_ulong	sys_max_error_count("max_error_count",
					    &SV::max_error_count);
unknown's avatar
unknown committed
224 225
sys_var_thd_ulong	sys_max_heap_table_size("max_heap_table_size",
						&SV::max_heap_table_size);
226 227 228
sys_var_thd_ulong       sys_pseudo_thread_id("pseudo_thread_id",
					     &SV::pseudo_thread_id,
                                             check_pseudo_thread_id, 0);
229
sys_var_thd_ha_rows	sys_max_join_size("max_join_size",
unknown's avatar
unknown committed
230 231
					  &SV::max_join_size,
					  fix_max_join_size);
232 233
sys_var_thd_ulong	sys_max_seeks_for_key("max_seeks_for_key",
					      &SV::max_seeks_for_key);
unknown's avatar
unknown committed
234
sys_var_thd_ulong   sys_max_length_for_sort_data("max_length_for_sort_data",
235
                                                 &SV::max_length_for_sort_data);
unknown's avatar
unknown committed
236
#ifndef TO_BE_DELETED	/* Alias for max_join_size */
237
sys_var_thd_ha_rows	sys_sql_max_join_size("sql_max_join_size",
unknown's avatar
unknown committed
238 239 240
					      &SV::max_join_size,
					      fix_max_join_size);
#endif
241 242 243
sys_var_long_ptr	sys_max_relay_log_size("max_relay_log_size",
                                               &max_relay_log_size,
                                               fix_max_relay_log_size);
unknown's avatar
unknown committed
244 245 246 247 248 249 250 251
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);
unknown's avatar
unknown committed
252 253
sys_var_long_ptr	sys_myisam_data_pointer_size("myisam_data_pointer_size",
                                                    &myisam_data_pointer_size);
254 255
sys_var_thd_ulonglong	sys_myisam_max_extra_sort_file_size("myisam_max_extra_sort_file_size", &SV::myisam_max_extra_sort_file_size, fix_myisam_max_extra_sort_file_size, 1);
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);
256
sys_var_thd_ulong       sys_myisam_repair_threads("myisam_repair_threads", &SV::myisam_repair_threads);
unknown's avatar
unknown committed
257 258 259 260 261
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,
262
					     0, fix_net_read_timeout);
unknown's avatar
unknown committed
263 264
sys_var_thd_ulong	sys_net_write_timeout("net_write_timeout",
					      &SV::net_write_timeout,
265
					      0, fix_net_write_timeout);
266 267
sys_var_thd_ulong	sys_net_retry_count("net_retry_count",
					    &SV::net_retry_count,
268
					    0, fix_net_retry_count);
269
sys_var_thd_bool	sys_new_mode("new", &SV::new_mode);
270
sys_var_thd_bool	sys_old_passwords("old_passwords", &SV::old_passwords);
unknown's avatar
unknown committed
271 272
sys_var_thd_ulong       sys_preload_buff_size("preload_buffer_size",
                                              &SV::preload_buff_size);
unknown's avatar
unknown committed
273 274
sys_var_thd_ulong	sys_read_buff_size("read_buffer_size",
					   &SV::read_buff_size);
275
sys_var_bool_ptr	sys_readonly("read_only", &opt_readonly);
unknown's avatar
unknown committed
276 277
sys_var_thd_ulong	sys_read_rnd_buff_size("read_rnd_buffer_size",
					       &SV::read_rnd_buff_size);
278 279 280 281
#ifdef HAVE_REPLICATION
sys_var_bool_ptr	sys_relay_log_purge("relay_log_purge",
                                            &relay_log_purge);
#endif
unknown's avatar
unknown committed
282 283 284 285 286
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);
287 288 289 290

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",
291
						   &SV::query_alloc_block_size,
292
						   0, fix_thd_mem_root);
293
sys_var_thd_ulong	sys_query_prealloc_size("query_prealloc_size",
294
						&SV::query_prealloc_size,
295
						0, fix_thd_mem_root);
296
sys_var_thd_ulong	sys_trans_alloc_block_size("transaction_alloc_block_size",
297
						   &SV::trans_alloc_block_size,
298
						   0, fix_trans_mem_root);
299
sys_var_thd_ulong	sys_trans_prealloc_size("transaction_prealloc_size",
300
						&SV::trans_prealloc_size,
301
						0, fix_trans_mem_root);
302

unknown's avatar
unknown committed
303 304 305
#ifdef HAVE_QUERY_CACHE
sys_var_long_ptr	sys_query_cache_limit("query_cache_limit",
					      &query_cache.query_cache_limit);
306 307 308
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);
unknown's avatar
unknown committed
309 310 311
sys_var_thd_enum	sys_query_cache_type("query_cache_type",
					     &SV::query_cache_type,
					     &query_cache_type_typelib);
312 313 314
sys_var_thd_bool
sys_query_cache_wlock_invalidate("query_cache_wlock_invalidate",
				 &SV::query_cache_wlock_invalidate);
unknown's avatar
unknown committed
315
#endif /* HAVE_QUERY_CACHE */
316
sys_var_bool_ptr	sys_secure_auth("secure_auth", &opt_secure_auth);
317
sys_var_long_ptr	sys_server_id("server_id", &server_id, fix_server_id);
318 319
sys_var_bool_ptr	sys_slave_compressed_protocol("slave_compressed_protocol",
						      &opt_slave_compressed_protocol);
unknown's avatar
SCRUM  
unknown committed
320
#ifdef HAVE_REPLICATION
unknown's avatar
unknown committed
321 322
sys_var_long_ptr	sys_slave_net_timeout("slave_net_timeout",
					      &slave_net_timeout);
323
#endif
unknown's avatar
unknown committed
324 325 326 327
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);
328 329
sys_var_thd_sql_mode    sys_sql_mode("sql_mode",
                                     &SV::sql_mode);
330 331
sys_var_thd_table_type  sys_table_type("table_type",
				       &SV::table_type);
unknown's avatar
unknown committed
332 333
sys_var_thd_storage_engine sys_storage_engine("storage_engine",
				       &SV::table_type);
334 335 336 337
#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);
unknown's avatar
unknown committed
338 339 340 341 342 343 344 345 346 347 348 349
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);
350

351 352 353
#ifdef HAVE_INNOBASE_DB
sys_var_long_ptr        sys_innodb_max_dirty_pages_pct("innodb_max_dirty_pages_pct",
                                                        &srv_max_buf_pool_modified_pct);
354 355
sys_var_long_ptr	sys_innodb_max_purge_lag("innodb_max_purge_lag",
							&srv_max_purge_lag);
356 357
sys_var_thd_bool	sys_innodb_table_locks("innodb_table_locks",
                                               &SV::innodb_table_locks);
358 359
sys_var_long_ptr	sys_innodb_autoextend_increment("innodb_autoextend_increment",
							&srv_auto_extend_increment);
unknown's avatar
unknown committed
360
#endif
361

362
#ifdef HAVE_NDBCLUSTER_DB
363
/* ndb thread specific variable settings */
364 365 366 367
sys_var_thd_ulong 
sys_ndb_autoincrement_prefetch_sz("ndb_autoincrement_prefetch_sz",
				  &SV::ndb_autoincrement_prefetch_sz);
sys_var_thd_bool
368
sys_ndb_force_send("ndb_force_send", &SV::ndb_force_send);
369
sys_var_thd_bool
370
sys_ndb_use_exact_count("ndb_use_exact_count", &SV::ndb_use_exact_count);
371
sys_var_thd_bool
372
sys_ndb_use_transactions("ndb_use_transactions", &SV::ndb_use_transactions);
373
#endif
374 375 376 377 378

/* Time/date/datetime formats */

sys_var_thd_date_time_format sys_time_format("time_format",
					     &SV::time_format,
379
					     MYSQL_TIMESTAMP_TIME);
380 381
sys_var_thd_date_time_format sys_date_format("date_format",
					     &SV::date_format,
382
					     MYSQL_TIMESTAMP_DATE);
383 384
sys_var_thd_date_time_format sys_datetime_format("datetime_format",
						 &SV::datetime_format,
385
						 MYSQL_TIMESTAMP_DATETIME);
386 387

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

389
static sys_var_thd_bit	sys_autocommit("autocommit", 0,
unknown's avatar
unknown committed
390 391 392
				       set_option_autocommit,
				       OPTION_NOT_AUTOCOMMIT,
				       1);
393
static sys_var_thd_bit	sys_big_tables("big_tables", 0,
unknown's avatar
unknown committed
394 395 396
				       set_option_bit,
				       OPTION_BIG_TABLES);
#ifndef TO_BE_DELETED	/* Alias for big_tables */
397
static sys_var_thd_bit	sys_sql_big_tables("sql_big_tables", 0,
unknown's avatar
unknown committed
398 399 400
					   set_option_bit,
					   OPTION_BIG_TABLES);
#endif
401
static sys_var_thd_bit	sys_big_selects("sql_big_selects", 0,
unknown's avatar
unknown committed
402
					set_option_bit,
403
					OPTION_BIG_SELECTS);
404
static sys_var_thd_bit	sys_log_off("sql_log_off", 0,
unknown's avatar
unknown committed
405 406 407
				    set_option_bit,
				    OPTION_LOG_OFF);
static sys_var_thd_bit	sys_log_update("sql_log_update",
408
                                       check_log_update,
unknown's avatar
unknown committed
409 410 411
				       set_log_update,
				       OPTION_UPDATE_LOG);
static sys_var_thd_bit	sys_log_binlog("sql_log_bin",
412 413 414 415
                                       check_log_update,
                                       set_log_update,
                                       OPTION_BIN_LOG);
static sys_var_thd_bit	sys_sql_warnings("sql_warnings", 0,
unknown's avatar
unknown committed
416 417
					 set_option_bit,
					 OPTION_WARNINGS);
418
static sys_var_thd_bit	sys_auto_is_null("sql_auto_is_null", 0,
unknown's avatar
unknown committed
419 420
					 set_option_bit,
					 OPTION_AUTO_IS_NULL);
421
static sys_var_thd_bit	sys_safe_updates("sql_safe_updates", 0,
unknown's avatar
unknown committed
422 423
					 set_option_bit,
					 OPTION_SAFE_UPDATES);
424
static sys_var_thd_bit	sys_buffer_results("sql_buffer_result", 0,
unknown's avatar
unknown committed
425 426
					   set_option_bit,
					   OPTION_BUFFER_RESULT);
427
static sys_var_thd_bit	sys_quote_show_create("sql_quote_show_create", 0,
unknown's avatar
unknown committed
428 429
					      set_option_bit,
					      OPTION_QUOTE_SHOW_CREATE);
430
static sys_var_thd_bit	sys_foreign_key_checks("foreign_key_checks", 0,
431 432 433
					       set_option_bit,
					       OPTION_NO_FOREIGN_KEY_CHECKS,
					       1);
434
static sys_var_thd_bit	sys_unique_checks("unique_checks", 0,
435 436 437 438
					  set_option_bit,
					  OPTION_RELAXED_UNIQUE_CHECKS,
					  1);

unknown's avatar
unknown committed
439 440
/* Local state variables */

441
static sys_var_thd_ha_rows	sys_select_limit("sql_select_limit",
unknown's avatar
unknown committed
442 443 444 445 446
						 &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");
447 448 449 450 451 452 453 454 455
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);

unknown's avatar
unknown committed
456
/* alias for last_insert_id() to be compatible with Sybase */
unknown's avatar
SCRUM  
unknown committed
457
#ifdef HAVE_REPLICATION
unknown's avatar
unknown committed
458
static sys_var_slave_skip_counter sys_slave_skip_counter("sql_slave_skip_counter");
459
#endif
460 461
static sys_var_rand_seed1	sys_rand_seed1("rand_seed1");
static sys_var_rand_seed2	sys_rand_seed2("rand_seed2");
unknown's avatar
unknown committed
462

463 464
static sys_var_thd_ulong        sys_default_week_format("default_week_format",
					                &SV::default_week_format);
unknown's avatar
unknown committed
465

unknown's avatar
unknown committed
466 467 468
sys_var_thd_ulong               sys_group_concat_max_len("group_concat_max_len",
                                                         &SV::group_concat_max_len);

469
sys_var_thd_time_zone            sys_time_zone("time_zone");
470 471 472 473

/* Read only variables */

sys_var_const_str		sys_os("version_compile_os", SYSTEM_TYPE);
474
/* Global read-only variable describing server license */
475
sys_var_const_str		sys_license("license", STRINGIFY_ARG(LICENSE));
476

477

unknown's avatar
unknown committed
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
/*
  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,
495 496
  &sys_character_set_server,
  &sys_character_set_database,
497
  &sys_character_set_client,
unknown's avatar
unknown committed
498
  &sys_character_set_connection,
499
  &sys_character_set_results,
unknown's avatar
unknown committed
500
  &sys_collation_connection,
501 502
  &sys_collation_database,
  &sys_collation_server,
unknown's avatar
unknown committed
503 504
  &sys_concurrent_insert,
  &sys_connect_timeout,
505 506
  &sys_date_format,
  &sys_datetime_format,
507
  &sys_default_week_format,
unknown's avatar
unknown committed
508 509 510 511
  &sys_delay_key_write,
  &sys_delayed_insert_limit,
  &sys_delayed_insert_timeout,
  &sys_delayed_queue_size,
512
  &sys_error_count,
unknown's avatar
unknown committed
513
  &sys_expire_logs_days,
unknown's avatar
unknown committed
514 515
  &sys_flush,
  &sys_flush_time,
unknown's avatar
unknown committed
516
  &sys_ft_boolean_syntax,
517
  &sys_foreign_key_checks,
unknown's avatar
unknown committed
518
  &sys_group_concat_max_len,
unknown's avatar
unknown committed
519
  &sys_identity,
unknown's avatar
unknown committed
520 521
  &sys_init_connect,
  &sys_init_slave,
unknown's avatar
unknown committed
522 523 524 525
  &sys_insert_id,
  &sys_interactive_timeout,
  &sys_join_buffer_size,
  &sys_key_buffer_size,
unknown's avatar
unknown committed
526
  &sys_key_cache_block_size,
527 528
  &sys_key_cache_division_limit,
  &sys_key_cache_age_threshold,
unknown's avatar
unknown committed
529
  &sys_last_insert_id,
530
  &sys_license,
unknown's avatar
unknown committed
531 532 533 534 535 536 537 538 539 540 541 542 543
  &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,
544
  &sys_max_error_count,
545
  &sys_max_insert_delayed_threads,
unknown's avatar
unknown committed
546 547
  &sys_max_heap_table_size,
  &sys_max_join_size,
unknown's avatar
unknown committed
548
  &sys_max_length_for_sort_data,
549
  &sys_max_relay_log_size,
550
  &sys_max_seeks_for_key,
unknown's avatar
unknown committed
551 552 553 554
  &sys_max_sort_length,
  &sys_max_tmp_tables,
  &sys_max_user_connections,
  &sys_max_write_lock_count,
unknown's avatar
unknown committed
555
  &sys_myisam_data_pointer_size,
unknown's avatar
unknown committed
556 557
  &sys_myisam_max_extra_sort_file_size,
  &sys_myisam_max_sort_file_size,
558
  &sys_myisam_repair_threads,
unknown's avatar
unknown committed
559 560 561
  &sys_myisam_sort_buffer_size,
  &sys_net_buffer_length,
  &sys_net_read_timeout,
562
  &sys_net_retry_count,
unknown's avatar
unknown committed
563 564
  &sys_net_wait_timeout,
  &sys_net_write_timeout,
565
  &sys_new_mode,
566
  &sys_old_passwords,
unknown's avatar
unknown committed
567
  &sys_preload_buff_size,
unknown's avatar
unknown committed
568
  &sys_pseudo_thread_id,
569
  &sys_query_alloc_block_size,
unknown's avatar
unknown committed
570
  &sys_query_cache_size,
571
  &sys_query_prealloc_size,
unknown's avatar
unknown committed
572 573
#ifdef HAVE_QUERY_CACHE
  &sys_query_cache_limit,
574
  &sys_query_cache_min_res_unit,
unknown's avatar
unknown committed
575
  &sys_query_cache_type,
576
  &sys_query_cache_wlock_invalidate,
577
#endif /* HAVE_QUERY_CACHE */
unknown's avatar
unknown committed
578
  &sys_quote_show_create,
579 580
  &sys_rand_seed1,
  &sys_rand_seed2,
581
  &sys_range_alloc_block_size,
582
  &sys_readonly,
unknown's avatar
unknown committed
583 584
  &sys_read_buff_size,
  &sys_read_rnd_buff_size,
585 586 587
#ifdef HAVE_REPLICATION
  &sys_relay_log_purge,
#endif
unknown's avatar
unknown committed
588 589
  &sys_rpl_recovery_rank,
  &sys_safe_updates,
590
  &sys_secure_auth,
unknown's avatar
unknown committed
591 592
  &sys_select_limit,
  &sys_server_id,
unknown's avatar
SCRUM  
unknown committed
593
#ifdef HAVE_REPLICATION
594
  &sys_slave_compressed_protocol,
unknown's avatar
unknown committed
595 596
  &sys_slave_net_timeout,
  &sys_slave_skip_counter,
597
#endif
unknown's avatar
unknown committed
598 599 600 601 602
  &sys_slow_launch_time,
  &sys_sort_buffer,
  &sys_sql_big_tables,
  &sys_sql_low_priority_updates,
  &sys_sql_max_join_size,
603
  &sys_sql_mode,
unknown's avatar
unknown committed
604
  &sys_sql_warnings,
unknown's avatar
unknown committed
605
  &sys_storage_engine,
606 607 608 609
#ifdef HAVE_REPLICATION
  &sys_sync_binlog_period,
#endif
  &sys_sync_frm,
unknown's avatar
unknown committed
610 611 612
  &sys_table_cache_size,
  &sys_table_type,
  &sys_thread_cache_size,
613
  &sys_time_format,
unknown's avatar
unknown committed
614
  &sys_timestamp,
615
  &sys_time_zone,
unknown's avatar
unknown committed
616
  &sys_tmp_table_size,
617 618
  &sys_trans_alloc_block_size,
  &sys_trans_prealloc_size,
unknown's avatar
unknown committed
619
  &sys_tx_isolation,
620
  &sys_os,
621 622
#ifdef HAVE_INNOBASE_DB
  &sys_innodb_max_dirty_pages_pct,
623
  &sys_innodb_max_purge_lag,
624
  &sys_innodb_table_locks,
625
  &sys_innodb_max_purge_lag,
626
  &sys_innodb_autoextend_increment,
627 628 629 630 631 632 633
#endif  
#ifdef HAVE_NDBCLUSTER_DB
  &sys_ndb_autoincrement_prefetch_sz,
  &sys_ndb_force_send,
  &sys_ndb_use_exact_count,
  &sys_ndb_use_transactions,
#endif
634 635
  &sys_unique_checks,
  &sys_warning_count
unknown's avatar
unknown committed
636 637 638 639 640 641 642 643 644 645 646 647 648
};


/*
  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_home",                (char*) &berkeley_home,               SHOW_CHAR_PTR},
649
  {"bdb_log_buffer_size",     (char*) &berkeley_log_buffer_size,    SHOW_LONG},
unknown's avatar
unknown committed
650
  {"bdb_logdir",              (char*) &berkeley_logdir,             SHOW_CHAR_PTR},
651
  {"bdb_max_lock",            (char*) &berkeley_max_lock,	    SHOW_LONG},
unknown's avatar
unknown committed
652 653 654 655 656
  {"bdb_shared_data",	      (char*) &berkeley_shared_data,	    SHOW_BOOL},
  {"bdb_tmpdir",              (char*) &berkeley_tmpdir,             SHOW_CHAR_PTR},
#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},
657
  {sys_character_set_client.name,(char*) &sys_character_set_client, SHOW_SYS},
unknown's avatar
unknown committed
658
  {sys_character_set_connection.name,(char*) &sys_character_set_connection,SHOW_SYS},
659
  {sys_character_set_database.name, (char*) &sys_character_set_database,SHOW_SYS},
660
  {sys_character_set_results.name,(char*) &sys_character_set_results, SHOW_SYS},
661 662
  {sys_character_set_server.name, (char*) &sys_character_set_server,SHOW_SYS},
  {sys_charset_system.name,   (char*) &sys_charset_system,          SHOW_SYS},
663
  {"character_sets_dir",      mysql_charsets_dir,                   SHOW_CHAR},
unknown's avatar
unknown committed
664
  {sys_collation_connection.name,(char*) &sys_collation_connection, SHOW_SYS},
665 666
  {sys_collation_database.name,(char*) &sys_collation_database,     SHOW_SYS},
  {sys_collation_server.name,(char*) &sys_collation_server,         SHOW_SYS},
unknown's avatar
unknown committed
667 668 669
  {sys_concurrent_insert.name,(char*) &sys_concurrent_insert,       SHOW_SYS},
  {sys_connect_timeout.name,  (char*) &sys_connect_timeout,         SHOW_SYS},
  {"datadir",                 mysql_real_data_home,                 SHOW_CHAR},
670 671 672
  {sys_date_format.name,      (char*) &sys_date_format,		    SHOW_SYS},
  {sys_datetime_format.name,  (char*) &sys_datetime_format,	    SHOW_SYS},
  {sys_default_week_format.name, (char*) &sys_default_week_format,  SHOW_SYS},
unknown's avatar
unknown committed
673 674 675 676
  {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},
unknown's avatar
unknown committed
677
  {sys_expire_logs_days.name, (char*) &sys_expire_logs_days,        SHOW_SYS},
unknown's avatar
unknown committed
678 679
  {sys_flush.name,             (char*) &sys_flush,                  SHOW_SYS},
  {sys_flush_time.name,        (char*) &sys_flush_time,             SHOW_SYS},
unknown's avatar
unknown committed
680
  {sys_ft_boolean_syntax.name,(char*) &ft_boolean_syntax,	    SHOW_CHAR},
unknown's avatar
unknown committed
681
  {"ft_max_word_len",         (char*) &ft_max_word_len,             SHOW_LONG},
682
  {"ft_min_word_len",         (char*) &ft_min_word_len,             SHOW_LONG},
683
  {"ft_query_expansion_limit",(char*) &ft_query_expansion_limit,    SHOW_LONG},
684
  {"ft_stopword_file",        (char*) &ft_stopword_file,            SHOW_CHAR_PTR},
unknown's avatar
unknown committed
685
  {sys_group_concat_max_len.name, (char*) &sys_group_concat_max_len,  SHOW_SYS},
686
  {"have_archive",	      (char*) &have_archive_db,	            SHOW_HAVE},
unknown's avatar
unknown committed
687
  {"have_bdb",		      (char*) &have_berkeley_db,	    SHOW_HAVE},
unknown's avatar
unknown committed
688
  {"have_compress",	      (char*) &have_compress,		    SHOW_HAVE},
689
  {"have_crypt",	      (char*) &have_crypt,		    SHOW_HAVE},
690
  {"have_csv",	              (char*) &have_csv_db,	            SHOW_HAVE},
691
  {"have_example_engine",      (char*) &have_example_db,	            SHOW_HAVE},
unknown's avatar
unknown committed
692
  {"have_geometry",           (char*) &have_geometry,               SHOW_HAVE},
unknown's avatar
unknown committed
693
  {"have_innodb",	      (char*) &have_innodb,		    SHOW_HAVE},
694
  {"have_isam",		      (char*) &have_isam,		    SHOW_HAVE},
unknown's avatar
unknown committed
695
  {"have_ndbcluster",         (char*) &have_ndbcluster,             SHOW_HAVE},
unknown's avatar
unknown committed
696 697
  {"have_openssl",	      (char*) &have_openssl,		    SHOW_HAVE},
  {"have_query_cache",        (char*) &have_query_cache,            SHOW_HAVE},
698
  {"have_raid",		      (char*) &have_raid,		    SHOW_HAVE},
699
  {"have_rtree_keys",         (char*) &have_rtree_keys,             SHOW_HAVE},
700
  {"have_symlink",            (char*) &have_symlink,                SHOW_HAVE},
unknown's avatar
unknown committed
701
  {"init_connect",            (char*) &sys_init_connect,            SHOW_SYS},
702
  {"init_file",               (char*) &opt_init_file,               SHOW_CHAR_PTR},
unknown's avatar
unknown committed
703
  {"init_slave",              (char*) &sys_init_slave,              SHOW_SYS},
unknown's avatar
unknown committed
704 705
#ifdef HAVE_INNOBASE_DB
  {"innodb_additional_mem_pool_size", (char*) &innobase_additional_mem_pool_size, SHOW_LONG },
706
  {sys_innodb_autoextend_increment.name, (char*) &sys_innodb_autoextend_increment, SHOW_SYS},
unknown's avatar
unknown committed
707
  {"innodb_buffer_pool_awe_mem_mb", (char*) &innobase_buffer_pool_awe_mem_mb, SHOW_LONG },
708
  {"innodb_buffer_pool_size", (char*) &innobase_buffer_pool_size, SHOW_LONG },
unknown's avatar
unknown committed
709 710 711
  {"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_fast_shutdown", (char*) &innobase_fast_shutdown, SHOW_MY_BOOL},
712
  {"innodb_file_io_threads", (char*) &innobase_file_io_threads, SHOW_LONG },
unknown's avatar
unknown committed
713
  {"innodb_file_per_table", (char*) &innobase_file_per_table, SHOW_MY_BOOL},
714
  {"innodb_flush_log_at_trx_commit", (char*) &innobase_flush_log_at_trx_commit, SHOW_INT},
unknown's avatar
unknown committed
715
  {"innodb_flush_method",    (char*) &innobase_unix_file_flush_method, SHOW_CHAR_PTR},
716
  {"innodb_force_recovery", (char*) &innobase_force_recovery, SHOW_LONG },
unknown's avatar
unknown committed
717
  {"innodb_lock_wait_timeout", (char*) &innobase_lock_wait_timeout, SHOW_LONG },
unknown's avatar
unknown committed
718
  {"innodb_locks_unsafe_for_binlog", (char*) &innobase_locks_unsafe_for_binlog, SHOW_MY_BOOL},
unknown's avatar
unknown committed
719 720 721 722 723 724
  {"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},
725
  {sys_innodb_max_dirty_pages_pct.name, (char*) &sys_innodb_max_dirty_pages_pct, SHOW_SYS},
726
  {sys_innodb_max_purge_lag.name, (char*) &sys_innodb_max_purge_lag, SHOW_SYS},
727 728
  {"innodb_mirrored_log_groups", (char*) &innobase_mirrored_log_groups, SHOW_LONG},
  {"innodb_open_files", (char*) &innobase_open_files, SHOW_LONG },
unknown's avatar
unknown committed
729
  {sys_innodb_table_locks.name, (char*) &sys_innodb_table_locks, SHOW_SYS},
730
  {"innodb_thread_concurrency", (char*) &innobase_thread_concurrency, SHOW_LONG },
unknown's avatar
unknown committed
731 732 733 734
#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},
735
  {sys_key_cache_age_threshold.name,   (char*) &sys_key_cache_age_threshold,
736
                                                                    SHOW_SYS},
unknown's avatar
unknown committed
737
  {sys_key_cache_block_size.name,   (char*) &sys_key_cache_block_size,
738
                                                                    SHOW_SYS},
739
  {sys_key_cache_division_limit.name,   (char*) &sys_key_cache_division_limit,
740
                                                                    SHOW_SYS},
unknown's avatar
unknown committed
741
  {"language",                language,                             SHOW_CHAR},
742 743
  {"large_files_support",     (char*) &opt_large_files,             SHOW_BOOL},
  {sys_license.name,	      (char*) &sys_license,                 SHOW_SYS},
unknown's avatar
unknown committed
744 745 746 747 748 749
  {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},
750
  {"log_error",               (char*) log_error_file,               SHOW_CHAR},
unknown's avatar
SCRUM  
unknown committed
751
#ifdef HAVE_REPLICATION
752
  {"log_slave_updates",       (char*) &opt_log_slave_updates,       SHOW_MY_BOOL},
753
#endif
unknown's avatar
unknown committed
754
  {"log_slow_queries",        (char*) &opt_slow_log,                SHOW_BOOL},
755
  {"log_update",              (char*) &opt_update_log,              SHOW_BOOL},
unknown's avatar
unknown committed
756 757 758
  {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},
759
  {"lower_case_file_system",  (char*) &lower_case_file_system,      SHOW_MY_BOOL},
unknown's avatar
unknown committed
760
  {"lower_case_table_names",  (char*) &lower_case_table_names,      SHOW_INT},
unknown's avatar
unknown committed
761 762 763 764
  {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},
765
  {sys_max_connections.name,    (char*) &sys_max_connections,	    SHOW_SYS},
unknown's avatar
unknown committed
766
  {sys_max_delayed_threads.name,(char*) &sys_max_delayed_threads,   SHOW_SYS},
767
  {sys_max_error_count.name,	(char*) &sys_max_error_count,	    SHOW_SYS},
unknown's avatar
unknown committed
768
  {sys_max_heap_table_size.name,(char*) &sys_max_heap_table_size,   SHOW_SYS},
769 770
  {sys_max_insert_delayed_threads.name,
   (char*) &sys_max_insert_delayed_threads,   SHOW_SYS},
unknown's avatar
unknown committed
771
  {sys_max_join_size.name,	(char*) &sys_max_join_size,	    SHOW_SYS},
unknown's avatar
unknown committed
772
  {sys_max_length_for_sort_data.name, (char*) &sys_max_length_for_sort_data,
unknown's avatar
unknown committed
773
   SHOW_SYS},
774 775
  {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},
unknown's avatar
unknown committed
776
  {sys_max_sort_length.name,	(char*) &sys_max_sort_length,	    SHOW_SYS},
777
  {sys_max_tmp_tables.name,	(char*) &sys_max_tmp_tables,	    SHOW_SYS},
778
  {sys_max_user_connections.name,(char*) &sys_max_user_connections, SHOW_SYS},
unknown's avatar
unknown committed
779
  {sys_max_write_lock_count.name, (char*) &sys_max_write_lock_count,SHOW_SYS},
unknown's avatar
unknown committed
780
  {sys_myisam_data_pointer_size.name, (char*) &sys_myisam_data_pointer_size, SHOW_SYS},
unknown's avatar
unknown committed
781 782 783
  {sys_myisam_max_extra_sort_file_size.name,
   (char*) &sys_myisam_max_extra_sort_file_size,
   SHOW_SYS},
784 785
  {sys_myisam_max_sort_file_size.name, (char*) &sys_myisam_max_sort_file_size,
   SHOW_SYS},
786
  {"myisam_recover_options",  (char*) &myisam_recover_options_str,  SHOW_CHAR_PTR},
787
  {sys_myisam_repair_threads.name, (char*) &sys_myisam_repair_threads,
unknown's avatar
unknown committed
788 789 790
   SHOW_SYS},
  {sys_myisam_sort_buffer_size.name, (char*) &sys_myisam_sort_buffer_size, SHOW_SYS},
#ifdef __NT__
791
  {"named_pipe",	      (char*) &opt_enable_named_pipe,       SHOW_MY_BOOL},
792 793 794 795 796 797 798
#endif
#ifdef HAVE_NDBCLUSTER_DB
  {sys_ndb_autoincrement_prefetch_sz.name,
   (char*) &sys_ndb_autoincrement_prefetch_sz,                      SHOW_SYS},
  {sys_ndb_force_send.name,   (char*) &sys_ndb_force_send,          SHOW_SYS},
  {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},
unknown's avatar
unknown committed
799 800 801
#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},
802
  {sys_net_retry_count.name,  (char*) &sys_net_retry_count,	    SHOW_SYS},
unknown's avatar
unknown committed
803
  {sys_net_write_timeout.name,(char*) &sys_net_write_timeout,       SHOW_SYS},
804
  {sys_new_mode.name,         (char*) &sys_new_mode,                SHOW_SYS},
805
  {sys_old_passwords.name,    (char*) &sys_old_passwords,           SHOW_SYS},
unknown's avatar
unknown committed
806 807
  {"open_files_limit",	      (char*) &open_files_limit,	    SHOW_LONG},
  {"pid_file",                (char*) pidfile_name,                 SHOW_CHAR},
808
  {"port",                    (char*) &mysqld_port,                  SHOW_INT},
unknown's avatar
unknown committed
809
  {sys_preload_buff_size.name, (char*) &sys_preload_buff_size,      SHOW_SYS},
810
  {"protocol_version",        (char*) &protocol_version,            SHOW_INT},
811 812
  {sys_query_alloc_block_size.name, (char*) &sys_query_alloc_block_size,
   SHOW_SYS},
unknown's avatar
unknown committed
813
#ifdef HAVE_QUERY_CACHE
unknown's avatar
unknown committed
814
  {sys_query_cache_limit.name,(char*) &sys_query_cache_limit,	    SHOW_SYS},
815 816
  {sys_query_cache_min_res_unit.name, (char*) &sys_query_cache_min_res_unit,
   SHOW_SYS},
unknown's avatar
unknown committed
817 818
  {sys_query_cache_size.name, (char*) &sys_query_cache_size,	    SHOW_SYS},
  {sys_query_cache_type.name, (char*) &sys_query_cache_type,        SHOW_SYS},
unknown's avatar
unknown committed
819 820
  {sys_query_cache_wlock_invalidate.name,
   (char *) &sys_query_cache_wlock_invalidate, SHOW_SYS},
unknown's avatar
unknown committed
821
#endif /* HAVE_QUERY_CACHE */
822
  {sys_query_prealloc_size.name, (char*) &sys_query_prealloc_size,  SHOW_SYS},
823 824
  {sys_range_alloc_block_size.name, (char*) &sys_range_alloc_block_size,
   SHOW_SYS},
unknown's avatar
unknown committed
825 826 827
  {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},
unknown's avatar
unknown committed
828 829 830
#ifdef HAVE_REPLICATION
  {sys_relay_log_purge.name,  (char*) &sys_relay_log_purge,         SHOW_SYS},
#endif
unknown's avatar
unknown committed
831
  {sys_rpl_recovery_rank.name,(char*) &sys_rpl_recovery_rank,       SHOW_SYS},
832
  {"secure_auth",             (char*) &sys_secure_auth,             SHOW_SYS},
833 834 835 836
#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
unknown's avatar
unknown committed
837
  {sys_server_id.name,	      (char*) &sys_server_id,		    SHOW_SYS},
838
  {"skip_external_locking",   (char*) &my_disable_locking,          SHOW_MY_BOOL},
unknown's avatar
unknown committed
839 840
  {"skip_networking",         (char*) &opt_disable_networking,      SHOW_BOOL},
  {"skip_show_database",      (char*) &opt_skip_show_db,            SHOW_BOOL},
841 842 843
#ifdef HAVE_REPLICATION
  {sys_slave_net_timeout.name,(char*) &sys_slave_net_timeout,	    SHOW_SYS},
#endif
unknown's avatar
unknown committed
844
  {sys_slow_launch_time.name, (char*) &sys_slow_launch_time,        SHOW_SYS},
845
#ifdef HAVE_SYS_UN_H
846
  {"socket",                  (char*) &mysqld_unix_port,             SHOW_CHAR_PTR},
847
#endif
unknown's avatar
unknown committed
848
  {sys_sort_buffer.name,      (char*) &sys_sort_buffer, 	    SHOW_SYS},
849
  {sys_sql_mode.name,         (char*) &sys_sql_mode,                SHOW_SYS},
unknown's avatar
unknown committed
850
  {sys_storage_engine.name,   (char*) &sys_storage_engine,          SHOW_SYS},
851 852 853 854
#ifdef HAVE_REPLICATION
  {sys_sync_binlog_period.name,(char*) &sys_sync_binlog_period,     SHOW_SYS},
#endif
  {sys_sync_frm.name,         (char*) &sys_sync_frm,               SHOW_SYS},
855 856 857
#ifdef HAVE_TZNAME
  {"system_time_zone",        system_time_zone,                     SHOW_CHAR},
#endif
unknown's avatar
unknown committed
858 859 860 861 862 863 864
  {"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},
865
  {sys_time_format.name,      (char*) &sys_time_format,		    SHOW_SYS},
866
  {"time_zone",               (char*) &sys_time_zone,               SHOW_SYS},
unknown's avatar
unknown committed
867
  {sys_tmp_table_size.name,   (char*) &sys_tmp_table_size,	    SHOW_SYS},
unknown's avatar
unknown committed
868
  {"tmpdir",                  (char*) &opt_mysql_tmpdir,            SHOW_CHAR_PTR},
869 870 871
  {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},
872
  {sys_tx_isolation.name,     (char*) &sys_tx_isolation,	    SHOW_SYS},
unknown's avatar
unknown committed
873
  {"version",                 server_version,                       SHOW_CHAR},
874 875 876
#ifdef HAVE_BERKELEY_DB
  {"version_bdb",             (char*) DB_VERSION_STRING,            SHOW_CHAR},
#endif
877
  {"version_comment",         (char*) MYSQL_COMPILATION_COMMENT,    SHOW_CHAR},
878
  {"version_compile_machine", (char*) MACHINE_TYPE,		    SHOW_CHAR},
879
  {sys_os.name,		      (char*) &sys_os,			    SHOW_SYS},
unknown's avatar
unknown committed
880 881 882 883
  {sys_net_wait_timeout.name, (char*) &sys_net_wait_timeout,	    SHOW_SYS},
  {NullS, NullS, SHOW_LONG}
};

884

885 886 887 888 889 890
bool sys_var::check(THD *thd, set_var *var)
{
  var->save_result.ulonglong_value= var->value->val_int();
  return 0;
}

unknown's avatar
unknown committed
891 892 893 894 895 896 897 898 899 900 901 902
bool sys_var_str::check(THD *thd, set_var *var)
{
  int res;
  if (!check_func)
    return 0;

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

unknown's avatar
unknown committed
903 904 905 906
/*
  Functions to check and update variables
*/

unknown's avatar
unknown committed
907

908 909 910 911 912 913 914 915 916 917
/*
  Update variables 'init_connect, init_slave'.

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

bool update_sys_var_str(sys_var_str *var_str, rw_lock_t *var_mutex,
			set_var *var)
unknown's avatar
unknown committed
918
{
unknown's avatar
unknown committed
919 920
  char *res= 0, *old_value=(char *)(var ? var->value->str_value.ptr() : 0);
  uint new_length= (var ? var->value->str_value.length() : 0);
unknown's avatar
unknown committed
921 922
  if (!old_value)
    old_value= (char*) "";
923
  if (!(res= my_strdup_with_length((byte*)old_value, new_length, MYF(0))))
unknown's avatar
unknown committed
924
    return 1;
unknown's avatar
unknown committed
925 926 927 928
  /*
    Replace the old value in such a way that the any thread using
    the value will work.
  */
929 930 931 932 933
  rw_wrlock(var_mutex);
  old_value= var_str->value;
  var_str->value= res;
  var_str->value_length= new_length;
  rw_unlock(var_mutex);
unknown's avatar
unknown committed
934 935 936 937 938
  my_free(old_value, MYF(MY_ALLOW_ZERO_PTR));
  return 0;
}


939 940 941 942 943 944
static bool sys_update_init_connect(THD *thd, set_var *var)
{
  return update_sys_var_str(&sys_init_connect, &LOCK_sys_init_connect, var);
}


unknown's avatar
unknown committed
945 946
static void sys_default_init_connect(THD* thd, enum_var_type type)
{
947
  update_sys_var_str(&sys_init_connect, &LOCK_sys_init_connect, 0);
unknown's avatar
unknown committed
948 949 950 951 952
}


static bool sys_update_init_slave(THD *thd, set_var *var)
{
953
  return update_sys_var_str(&sys_init_slave, &LOCK_sys_init_slave, var);
unknown's avatar
unknown committed
954 955 956 957 958
}


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

unknown's avatar
unknown committed
962 963 964
static int sys_check_ftb_syntax(THD *thd,  set_var *var)
{
  if (thd->master_access & SUPER_ACL)
965
    return ft_boolean_check_syntax_string((byte*) var->value->str_value.c_ptr()) ?
unknown's avatar
unknown committed
966
      -1 : 0;
unknown's avatar
unknown committed
967 968 969 970 971 972 973 974 975
  else
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
}

static bool sys_update_ftb_syntax(THD *thd, set_var * var)
{
unknown's avatar
unknown committed
976 977
  strmake(ft_boolean_syntax, var->value->str_value.c_ptr(),
	  sizeof(ft_boolean_syntax)-1);
unknown's avatar
unknown committed
978 979 980 981 982
  return 0;
}

static void sys_default_ftb_syntax(THD *thd, enum_var_type type)
{
983
  strmake(ft_boolean_syntax, def_ft_boolean_syntax,
unknown's avatar
unknown committed
984
	  sizeof(ft_boolean_syntax)-1);
unknown's avatar
unknown committed
985
}
unknown's avatar
unknown committed
986

unknown's avatar
unknown committed
987 988 989 990 991
/*
  The following 3 functions need to be changed in 4.1 when we allow
  one to change character sets
*/

unknown's avatar
unknown committed
992
static int sys_check_charset(THD *thd, set_var *var)
unknown's avatar
unknown committed
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
{
  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);
}


1022 1023 1024 1025
static void
fix_myisam_max_extra_sort_file_size(THD *thd, enum_var_type type)
{
  myisam_max_extra_temp_length=
1026
    (my_off_t) global_system_variables.myisam_max_extra_sort_file_size;
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
}


static void
fix_myisam_max_sort_file_size(THD *thd, enum_var_type type)
{
  myisam_max_temp_length=
    (my_off_t) global_system_variables.myisam_max_sort_file_size;
}

unknown's avatar
unknown committed
1037 1038 1039 1040 1041 1042 1043 1044
/*
  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)
  {
1045
    if (thd->variables.max_join_size == HA_POS_ERROR)
unknown's avatar
unknown committed
1046 1047 1048 1049 1050
      thd->options|= OPTION_BIG_SELECTS;
    else
      thd->options&= ~OPTION_BIG_SELECTS;
  }
}
1051

unknown's avatar
unknown committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

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

unknown's avatar
SCRUM  
unknown committed
1070
#ifdef HAVE_REPLICATION
unknown's avatar
unknown committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
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;
}

1084 1085 1086 1087 1088
static void fix_net_retry_count(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
    thd->net.retry_count=thd->variables.net_retry_count;
}
unknown's avatar
SCRUM  
unknown committed
1089 1090 1091
#else /* HAVE_REPLICATION */
static void fix_net_read_timeout(THD *thd __attribute__(unused),
				 enum_var_type type __attribute__(unused))
1092
{}
unknown's avatar
SCRUM  
unknown committed
1093 1094
static void fix_net_write_timeout(THD *thd __attribute__(unused),
				  enum_var_type type __attribute__(unused))
1095
{}
unknown's avatar
SCRUM  
unknown committed
1096 1097
static void fix_net_retry_count(THD *thd __attribute__(unused),
				enum_var_type type __attribute__(unused))
1098
{}
unknown's avatar
SCRUM  
unknown committed
1099
#endif /* HAVE_REPLICATION */
1100

unknown's avatar
unknown committed
1101 1102 1103 1104

static void fix_query_cache_size(THD *thd, enum_var_type type)
{
#ifdef HAVE_QUERY_CACHE
1105
  ulong requested= query_cache_size;
unknown's avatar
unknown committed
1106
  query_cache.resize(query_cache_size);
1107 1108 1109 1110
  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);
unknown's avatar
unknown committed
1111 1112 1113 1114
#endif
}


1115 1116 1117 1118 1119 1120 1121 1122 1123
#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


1124
extern void fix_delay_key_write(THD *thd, enum_var_type type)
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
{
  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;
  }
}

1140
static void fix_max_binlog_size(THD *thd, enum_var_type type)
1141 1142 1143 1144 1145
{
  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);
1146
#ifdef HAVE_REPLICATION
1147 1148
  if (!max_relay_log_size)
    active_mi->rli.relay_log.set_max_size(max_binlog_size);
unknown's avatar
unknown committed
1149
#endif
1150 1151 1152
  DBUG_VOID_RETURN;
}

1153
static void fix_max_relay_log_size(THD *thd, enum_var_type type)
1154 1155 1156 1157
{
  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));
1158
#ifdef HAVE_REPLICATION
1159 1160
  active_mi->rli.relay_log.set_max_size(max_relay_log_size ?
                                        max_relay_log_size: max_binlog_size);
1161
#endif
1162 1163
  DBUG_VOID_RETURN;
}
1164

1165

1166 1167
static int check_max_delayed_threads(THD *thd, set_var *var)
{
unknown's avatar
unknown committed
1168
  longlong val= var->value->val_int();
1169
  if (var->type != OPT_GLOBAL && val != 0 &&
unknown's avatar
unknown committed
1170
      val != (longlong) global_system_variables.max_insert_delayed_threads)
1171 1172 1173 1174 1175 1176 1177 1178
  {
    char buf[64];
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), var->var->name, llstr(val, buf));
    return 1;
  }
  return 0;
}

unknown's avatar
unknown committed
1179

1180
static void fix_max_connections(THD *thd, enum_var_type type)
1181
{
1182
#ifndef EMBEDDED_LIBRARY
1183 1184
  resize_thr_alarm(max_connections + 
		   global_system_variables.max_insert_delayed_threads + 10);
1185
#endif
1186 1187
}

1188

1189 1190 1191
static void fix_thd_mem_root(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
unknown's avatar
unknown committed
1192
    reset_root_defaults(thd->mem_root,
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
                        thd->variables.query_alloc_block_size,
                        thd->variables.query_prealloc_size);
}


static void fix_trans_mem_root(THD *thd, enum_var_type type)
{
  if (type != OPT_GLOBAL)
    reset_root_defaults(&thd->transaction.mem_root,
                        thd->variables.trans_alloc_block_size,
                        thd->variables.trans_prealloc_size);
}

1206

1207 1208 1209 1210
static void fix_server_id(THD *thd, enum_var_type type)
{
  server_id_supplied = 1;
}
1211

1212

unknown's avatar
unknown committed
1213 1214
bool sys_var_long_ptr::update(THD *thd, set_var *var)
{
1215
  ulonglong tmp= var->save_result.ulonglong_value;
1216
  pthread_mutex_lock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1217 1218 1219 1220
  if (option_limits)
    *value= (ulong) getopt_ull_limit_value(tmp, option_limits);
  else
    *value= (ulong) tmp;
1221
  pthread_mutex_unlock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
  return 0;
}


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


1232 1233
bool sys_var_ulonglong_ptr::update(THD *thd, set_var *var)
{
1234
  ulonglong tmp= var->save_result.ulonglong_value;
1235
  pthread_mutex_lock(&LOCK_global_system_variables);
1236 1237 1238 1239
  if (option_limits)
    *value= (ulonglong) getopt_ull_limit_value(tmp, option_limits);
  else
    *value= (ulonglong) tmp;
1240
  pthread_mutex_unlock(&LOCK_global_system_variables);
1241 1242 1243 1244 1245 1246
  return 0;
}


void sys_var_ulonglong_ptr::set_default(THD *thd, enum_var_type type)
{
1247
  pthread_mutex_lock(&LOCK_global_system_variables);
1248
  *value= (ulonglong) option_limits->def_value;
1249
  pthread_mutex_unlock(&LOCK_global_system_variables);
1250 1251 1252
}


unknown's avatar
unknown committed
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
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;
}


1266 1267 1268 1269 1270 1271 1272
bool sys_var_enum::update(THD *thd, set_var *var)
{
  *value= (uint) var->save_result.ulong_value;
  return 0;
}


1273
byte *sys_var_enum::value_ptr(THD *thd, enum_var_type type, LEX_STRING *base)
1274 1275 1276 1277
{
  return (byte*) enum_names->type_names[*value];
}

1278 1279 1280 1281 1282
bool sys_var_thd_ulong::check(THD *thd, set_var *var)
{
  return (sys_var_thd::check(thd, var) ||
          (check_func && (*check_func)(thd, var)));
}
1283

unknown's avatar
unknown committed
1284 1285
bool sys_var_thd_ulong::update(THD *thd, set_var *var)
{
1286
  ulonglong tmp= var->save_result.ulonglong_value;
unknown's avatar
unknown committed
1287 1288 1289 1290 1291 1292 1293 1294

  /* 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)
1295
    global_system_variables.*offset= (ulong) tmp;
unknown's avatar
unknown committed
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
  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;
}


1314 1315
byte *sys_var_thd_ulong::value_ptr(THD *thd, enum_var_type type,
				   LEX_STRING *base)
unknown's avatar
unknown committed
1316 1317 1318 1319 1320 1321 1322
{
  if (type == OPT_GLOBAL)
    return (byte*) &(global_system_variables.*offset);
  return (byte*) &(thd->variables.*offset);
}


1323 1324
bool sys_var_thd_ha_rows::update(THD *thd, set_var *var)
{
1325
  ulonglong tmp= var->save_result.ulonglong_value;
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359

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


1360 1361
byte *sys_var_thd_ha_rows::value_ptr(THD *thd, enum_var_type type,
				     LEX_STRING *base)
1362 1363 1364 1365 1366 1367
{
  if (type == OPT_GLOBAL)
    return (byte*) &(global_system_variables.*offset);
  return (byte*) &(thd->variables.*offset);
}

unknown's avatar
unknown committed
1368 1369
bool sys_var_thd_ulonglong::update(THD *thd,  set_var *var)
{
1370
  ulonglong tmp= var->save_result.ulonglong_value;
1371

1372
  if (tmp > max_system_variables.*offset)
1373 1374 1375
    tmp= max_system_variables.*offset;

  if (option_limits)
1376
    tmp= getopt_ull_limit_value(tmp, option_limits);
unknown's avatar
unknown committed
1377
  if (var->type == OPT_GLOBAL)
1378 1379 1380
  {
    /* Lock is needed to make things safe on 32 bit systems */
    pthread_mutex_lock(&LOCK_global_system_variables);
1381
    global_system_variables.*offset= (ulonglong) tmp;
1382 1383
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
unknown's avatar
unknown committed
1384
  else
1385
    thd->variables.*offset= (ulonglong) tmp;
unknown's avatar
unknown committed
1386 1387 1388 1389 1390 1391 1392
  return 0;
}


void sys_var_thd_ulonglong::set_default(THD *thd, enum_var_type type)
{
  if (type == OPT_GLOBAL)
1393 1394
  {
    pthread_mutex_lock(&LOCK_global_system_variables);
1395
    global_system_variables.*offset= (ulonglong) option_limits->def_value;
1396 1397
    pthread_mutex_unlock(&LOCK_global_system_variables);
  }
unknown's avatar
unknown committed
1398 1399 1400 1401 1402
  else
    thd->variables.*offset= global_system_variables.*offset;
}


1403 1404
byte *sys_var_thd_ulonglong::value_ptr(THD *thd, enum_var_type type,
				       LEX_STRING *base)
unknown's avatar
unknown committed
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
{
  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;
}


1431 1432
byte *sys_var_thd_bool::value_ptr(THD *thd, enum_var_type type,
				  LEX_STRING *base)
unknown's avatar
unknown committed
1433 1434 1435 1436 1437 1438 1439 1440 1441
{
  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)
{
1442 1443
  char buff[80];
  const char *value;
1444
  String str(buff, sizeof(buff), system_charset_info), *res;
unknown's avatar
unknown committed
1445 1446 1447 1448 1449

  if (var->value->result_type() == STRING_RESULT)
  {
    if (!(res=var->value->val_str(&str)) ||
	((long) (var->save_result.ulong_value=
1450 1451
		 (ulong) find_type(enum_names, res->ptr(),
				   res->length(),1)-1)) < 0)
unknown's avatar
unknown committed
1452
    {
1453
      value= res ? res->c_ptr() : "NULL";
unknown's avatar
unknown committed
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
      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;
}

1475 1476 1477

bool sys_var::check_set(THD *thd, set_var *var, TYPELIB *enum_names)
{
1478
  bool not_used;
unknown's avatar
unknown committed
1479
  char buff[80], *error= 0;
1480 1481 1482 1483 1484 1485 1486
  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)))
      goto err;
unknown's avatar
unknown committed
1487 1488
    var->save_result.ulong_value= ((ulong)
				   find_set(enum_names, res->c_ptr(),
1489 1490 1491
					    res->length(),
                                            NULL,
                                            &error, &error_len,
unknown's avatar
unknown committed
1492
					    &not_used));
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
    if (error_len)
    {
      strmake(buff, error, min(sizeof(buff), error_len));
      goto err;
    }
  }
  else
  {
    ulonglong tmp= var->value->val_int();
    if (tmp >= enum_names->count)
    {
      llstr(tmp, buff);
      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, buff);
  return 1;
}


unknown's avatar
unknown committed
1517 1518 1519 1520 1521 1522 1523
/*
  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.
1524 1525 1526 1527

  TODO:
    With prepared statements/stored procedures this has to be fixed
    to create an item that gets the current value at fix_fields() stage.
unknown's avatar
unknown committed
1528 1529
*/

1530
Item *sys_var::item(THD *thd, enum_var_type var_type, LEX_STRING *base)
unknown's avatar
unknown committed
1531 1532 1533 1534 1535
{
  if (check_type(var_type))
  {
    if (var_type != OPT_DEFAULT)
    {
unknown's avatar
unknown committed
1536
      net_printf(thd, ER_INCORRECT_GLOBAL_LOCAL_VAR,
1537
		 name, var_type == OPT_GLOBAL ? "SESSION" : "GLOBAL");
unknown's avatar
unknown committed
1538 1539 1540 1541 1542 1543 1544
      return 0;
    }
    /* As there was no local variable, return the global value */
    var_type= OPT_GLOBAL;
  }
  switch (type()) {
  case SHOW_LONG:
1545 1546 1547 1548 1549 1550 1551
  {
    ulong value;
    pthread_mutex_lock(&LOCK_global_system_variables);
    value= *(ulong*) value_ptr(thd, var_type, base);
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return new Item_uint((int32) value);
  }
unknown's avatar
unknown committed
1552
  case SHOW_LONGLONG:
1553 1554 1555
  {
    longlong value;
    pthread_mutex_lock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1556
    value= *(longlong*) value_ptr(thd, var_type, base);
1557 1558 1559
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return new Item_int(value);
  }
1560
  case SHOW_HA_ROWS:
1561 1562 1563
  {
    ha_rows value;
    pthread_mutex_lock(&LOCK_global_system_variables);
unknown's avatar
unknown committed
1564
    value= *(ha_rows*) value_ptr(thd, var_type, base);
1565 1566 1567
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return new Item_int((longlong) value);
  }
unknown's avatar
unknown committed
1568
  case SHOW_MY_BOOL:
1569
    return new Item_int((int32) *(my_bool*) value_ptr(thd, var_type, base),1);
unknown's avatar
unknown committed
1570 1571
  case SHOW_CHAR:
  {
1572 1573
    Item_string *tmp;
    pthread_mutex_lock(&LOCK_global_system_variables);
1574
    char *str= (char*) value_ptr(thd, var_type, base);
1575 1576 1577
    tmp= new Item_string(str, strlen(str), system_charset_info);
    pthread_mutex_unlock(&LOCK_global_system_variables);
    return tmp;
unknown's avatar
unknown committed
1578 1579
  }
  default:
1580
    net_printf(thd, ER_VAR_CANT_BE_READ, name);
unknown's avatar
unknown committed
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
  }
  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;
}


1605 1606
byte *sys_var_thd_enum::value_ptr(THD *thd, enum_var_type type,
				  LEX_STRING *base)
unknown's avatar
unknown committed
1607 1608 1609 1610 1611 1612 1613
{
  ulong tmp= ((type == OPT_GLOBAL) ?
	      global_system_variables.*offset :
	      thd->variables.*offset);
  return (byte*) enum_names->type_names[tmp];
}

1614 1615 1616 1617 1618
bool sys_var_thd_bit::check(THD *thd, set_var *var)
{
  return (check_enum(thd, var, &bool_typelib) ||
          (check_func && (*check_func)(thd, var)));
}
unknown's avatar
unknown committed
1619 1620 1621

bool sys_var_thd_bit::update(THD *thd, set_var *var)
{
1622
  int res= (*update_func)(thd, var);
unknown's avatar
unknown committed
1623 1624 1625 1626
  return res;
}


1627 1628
byte *sys_var_thd_bit::value_ptr(THD *thd, enum_var_type type,
				 LEX_STRING *base)
unknown's avatar
unknown committed
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
{
  /*
    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;
}


1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 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 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
/* 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)
{
  char buff[80];
  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())))
  {
    my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, res->c_ptr());
    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;
}


1746 1747
typedef struct old_names_map_st
{
unknown's avatar
unknown committed
1748 1749 1750
  const char *old_name;
  const char *new_name;
} my_old_conv;
unknown's avatar
unknown committed
1751

unknown's avatar
unknown committed
1752 1753
static my_old_conv old_conv[]= 
{
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
  {	"cp1251_koi8"		,	"cp1251"	},
  {	"cp1250_latin2"		,	"cp1250"	},
  {	"kam_latin2"		,	"keybcs2"	},
  {	"mac_latin2"		,	"MacRoman"	},
  {	"macce_latin2"		,	"MacCE"		},
  {	"pc2_latin2"		,	"pclatin2"	},
  {	"vga_latin2"		,	"pclatin1"	},
  {	"koi8_cp1251"		,	"koi8r"		},
  {	"win1251ukr_koi8_ukr"	,	"win1251ukr"	},
  {	"koi8_ukr_win1251ukr"	,	"koi8u"		},
  {	NULL			,	NULL		}
unknown's avatar
unknown committed
1765
};
unknown's avatar
unknown committed
1766

unknown's avatar
unknown committed
1767
CHARSET_INFO *get_old_charset_by_name(const char *name)
unknown's avatar
unknown committed
1768
{
unknown's avatar
unknown committed
1769
  my_old_conv *conv;
unknown's avatar
unknown committed
1770
 
unknown's avatar
unknown committed
1771
  for (conv= old_conv; conv->old_name; conv++)
1772
  {
unknown's avatar
unknown committed
1773 1774
    if (!my_strcasecmp(&my_charset_latin1, name, conv->old_name))
      return get_charset_by_csname(conv->new_name, MY_CS_PRIMARY, MYF(0));
1775
  }
unknown's avatar
unknown committed
1776
  return NULL;
unknown's avatar
unknown committed
1777 1778
}

unknown's avatar
unknown committed
1779

1780
bool sys_var_collation::check(THD *thd, set_var *var)
unknown's avatar
unknown committed
1781 1782 1783
{
  CHARSET_INFO *tmp;

1784
  if (var->value->result_type() == STRING_RESULT)
1785
  {
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
    char buff[80];
    String str(buff,sizeof(buff), system_charset_info), *res;
    if (!(res=var->value->val_str(&str)))
    {
      my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
      return 1;
    }
    if (!(tmp=get_charset_by_name(res->c_ptr(),MYF(0))))
    {
      my_error(ER_UNKNOWN_COLLATION, MYF(0), res->c_ptr());
      return 1;
    }
1798
  }
1799
  else // INT_RESULT
1800
  {
1801
    if (!(tmp=get_charset((int) var->value->val_int(),MYF(0))))
1802 1803
    {
      char buf[20];
1804
      int10_to_str((int) var->value->val_int(), buf, -10);
1805 1806 1807
      my_error(ER_UNKNOWN_COLLATION, MYF(0), buf);
      return 1;
    }
1808 1809 1810 1811 1812
  }
  var->save_result.charset= tmp;	// Save for update
  return 0;
}

1813

1814 1815 1816 1817
bool sys_var_character_set::check(THD *thd, set_var *var)
{
  CHARSET_INFO *tmp;

1818 1819 1820 1821 1822
  if (var->value->result_type() == STRING_RESULT)
  {
    char buff[80];
    String str(buff,sizeof(buff), system_charset_info), *res;
    if (!(res=var->value->val_str(&str)))
1823
    {
1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
      if (!nullable)
      {
        my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, "NULL");
        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())))
    {
      my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), res->c_ptr());
1835 1836 1837
      return 1;
    }
  }
1838
  else // INT_RESULT
unknown's avatar
unknown committed
1839
  {
1840
    if (!(tmp=get_charset((int) var->value->val_int(),MYF(0))))
1841 1842
    {
      char buf[20];
1843
      int10_to_str((int) var->value->val_int(), buf, -10);
1844 1845 1846
      my_error(ER_UNKNOWN_CHARACTER_SET, MYF(0), buf);
      return 1;
    }
unknown's avatar
unknown committed
1847 1848 1849 1850 1851
  }
  var->save_result.charset= tmp;	// Save for update
  return 0;
}

1852

1853
bool sys_var_character_set::update(THD *thd, set_var *var)
unknown's avatar
unknown committed
1854
{
1855
  ci_ptr(thd,var->type)[0]= var->save_result.charset;
unknown's avatar
unknown committed
1856
  thd->update_charset();
unknown's avatar
unknown committed
1857 1858 1859
  return 0;
}

1860 1861 1862

byte *sys_var_character_set::value_ptr(THD *thd, enum_var_type type,
				       LEX_STRING *base)
unknown's avatar
unknown committed
1863
{
1864
  CHARSET_INFO *cs= ci_ptr(thd,type)[0];
1865
  return cs ? (byte*) cs->csname : (byte*) "NULL";
unknown's avatar
unknown committed
1866 1867
}

1868

1869 1870
CHARSET_INFO ** sys_var_character_set_connection::ci_ptr(THD *thd,
							 enum_var_type type)
unknown's avatar
unknown committed
1871 1872 1873 1874 1875 1876 1877
{
  if (type == OPT_GLOBAL)
    return &global_system_variables.collation_connection;
  else
    return &thd->variables.collation_connection;
}

1878 1879 1880

void sys_var_character_set_connection::set_default(THD *thd,
						   enum_var_type type)
unknown's avatar
unknown committed
1881 1882 1883 1884
{
 if (type == OPT_GLOBAL)
   global_system_variables.collation_connection= default_charset_info;
 else
unknown's avatar
unknown committed
1885
 {
unknown's avatar
unknown committed
1886
   thd->variables.collation_connection= global_system_variables.collation_connection;
unknown's avatar
unknown committed
1887 1888
   thd->update_charset();
 }
unknown's avatar
unknown committed
1889 1890
}

1891

1892 1893
CHARSET_INFO ** sys_var_character_set_client::ci_ptr(THD *thd,
						     enum_var_type type)
1894 1895 1896 1897 1898 1899 1900
{
  if (type == OPT_GLOBAL)
    return &global_system_variables.character_set_client;
  else
    return &thd->variables.character_set_client;
}

1901

1902
void sys_var_character_set_client::set_default(THD *thd, enum_var_type type)
1903 1904
{
 if (type == OPT_GLOBAL)
1905
   global_system_variables.character_set_client= default_charset_info;
1906
 else
unknown's avatar
unknown committed
1907 1908 1909 1910 1911
 {
   thd->variables.character_set_client= (global_system_variables.
					 character_set_client);
   thd->update_charset();
 }
1912
}
unknown's avatar
unknown committed
1913

1914

unknown's avatar
unknown committed
1915 1916
CHARSET_INFO **
sys_var_character_set_results::ci_ptr(THD *thd, enum_var_type type)
1917
{
1918 1919
  if (type == OPT_GLOBAL)
    return &global_system_variables.character_set_results;
1920
  else
1921
    return &thd->variables.character_set_results;
1922 1923
}

1924

1925
void sys_var_character_set_results::set_default(THD *thd, enum_var_type type)
1926
{
1927
 if (type == OPT_GLOBAL)
1928
   global_system_variables.character_set_results= default_charset_info;
1929
 else
unknown's avatar
unknown committed
1930 1931 1932 1933 1934
 {
   thd->variables.character_set_results= (global_system_variables.
					  character_set_results);
   thd->update_charset();
 }
1935 1936
}

1937

unknown's avatar
unknown committed
1938 1939
CHARSET_INFO **
sys_var_character_set_server::ci_ptr(THD *thd, enum_var_type type)
1940 1941
{
  if (type == OPT_GLOBAL)
1942
    return &global_system_variables.collation_server;
1943
  else
1944
    return &thd->variables.collation_server;
1945 1946
}

1947

1948
void sys_var_character_set_server::set_default(THD *thd, enum_var_type type)
1949 1950
{
 if (type == OPT_GLOBAL)
1951
   global_system_variables.collation_server= default_charset_info;
1952
 else
unknown's avatar
unknown committed
1953
 {
1954
   thd->variables.collation_server= global_system_variables.collation_server;
unknown's avatar
unknown committed
1955 1956
   thd->update_charset();
 }
1957 1958
}

1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972
#if defined(HAVE_REPLICATION) && (MYSQL_VERSION_ID < 50000)
bool sys_var_character_set_server::check(THD *thd, set_var *var)
{
  if ((var->type == OPT_GLOBAL) &&
      (mysql_bin_log.is_open() ||
       active_mi->slave_running || active_mi->rli.slave_running))
  {
    my_printf_error(0, "Binary logging and replication forbid changing \
the global server character set or collation", MYF(0));
    return 1;
  }
  return sys_var_character_set::check(thd,var);
}
#endif
1973 1974 1975

CHARSET_INFO ** sys_var_character_set_database::ci_ptr(THD *thd,
						       enum_var_type type)
1976 1977
{
  if (type == OPT_GLOBAL)
1978
    return &global_system_variables.collation_database;
1979
  else
1980
    return &thd->variables.collation_database;
1981 1982
}

1983

1984 1985 1986
void sys_var_character_set_database::set_default(THD *thd, enum_var_type type)
{
 if (type == OPT_GLOBAL)
1987
    global_system_variables.collation_database= default_charset_info;
1988
  else
unknown's avatar
unknown committed
1989
  {
1990
    thd->variables.collation_database= thd->db_charset;
unknown's avatar
unknown committed
1991 1992
    thd->update_charset();
  }
1993 1994
}

1995

1996
bool sys_var_collation_connection::update(THD *thd, set_var *var)
1997 1998
{
  if (var->type == OPT_GLOBAL)
1999
    global_system_variables.collation_connection= var->save_result.charset;
2000
  else
unknown's avatar
unknown committed
2001
  {
2002
    thd->variables.collation_connection= var->save_result.charset;
unknown's avatar
unknown committed
2003 2004
    thd->update_charset();
  }
2005 2006 2007
  return 0;
}

2008 2009 2010

byte *sys_var_collation_connection::value_ptr(THD *thd, enum_var_type type,
					      LEX_STRING *base)
2011 2012
{
  CHARSET_INFO *cs= ((type == OPT_GLOBAL) ?
2013 2014 2015
		  global_system_variables.collation_connection :
		  thd->variables.collation_connection);
  return cs ? (byte*) cs->name : (byte*) "NULL";
2016 2017
}

2018

2019
void sys_var_collation_connection::set_default(THD *thd, enum_var_type type)
2020 2021
{
 if (type == OPT_GLOBAL)
2022
   global_system_variables.collation_connection= default_charset_info;
2023
 else
unknown's avatar
unknown committed
2024 2025 2026 2027 2028
 {
   thd->variables.collation_connection= (global_system_variables.
					 collation_connection);
   thd->update_charset();
 }
2029 2030
}

2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
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();
 }
}

2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
#if defined(HAVE_REPLICATION) && (MYSQL_VERSION_ID < 50000)
bool sys_var_collation_server::check(THD *thd, set_var *var)
{
  if ((var->type == OPT_GLOBAL) &&
      (mysql_bin_log.is_open() ||
       active_mi->slave_running || active_mi->rli.slave_running))
  {
    my_printf_error(0, "Binary logging and replication forbid changing \
the global server character set or collation", MYF(0));
    return 1;
  }
  return sys_var_collation::check(thd,var);
}
#endif
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115

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

2116

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

unknown's avatar
unknown committed
2119
static KEY_CACHE zero_key_cache;
unknown's avatar
unknown committed
2120

unknown's avatar
unknown committed
2121
KEY_CACHE *get_key_cache(LEX_STRING *cache_name)
unknown's avatar
unknown committed
2122
{
2123 2124
  safe_mutex_assert_owner(&LOCK_global_system_variables);
  if (!cache_name || ! cache_name->length)
unknown's avatar
unknown committed
2125
    cache_name= &default_key_cache_base;
unknown's avatar
unknown committed
2126
  return ((KEY_CACHE*) find_named(&key_caches,
2127
                                      cache_name->str, cache_name->length, 0));
unknown's avatar
unknown committed
2128 2129
}

2130

unknown's avatar
unknown committed
2131 2132
byte *sys_var_key_cache_param::value_ptr(THD *thd, enum_var_type type,
					 LEX_STRING *base)
2133
{
unknown's avatar
unknown committed
2134
  KEY_CACHE *key_cache= get_key_cache(base);
unknown's avatar
unknown committed
2135 2136 2137 2138
  if (!key_cache)
    key_cache= &zero_key_cache;
  return (byte*) key_cache + offset ;
}
unknown's avatar
unknown committed
2139

2140

2141 2142
bool sys_var_key_buffer_size::update(THD *thd, set_var *var)
{
2143
  ulonglong tmp= var->save_result.ulonglong_value;
unknown's avatar
unknown committed
2144
  LEX_STRING *base_name= &var->base;
unknown's avatar
unknown committed
2145
  KEY_CACHE *key_cache;
2146 2147 2148
  bool error= 0;

  /* If no basename, assume it's for the key cache named 'default' */
unknown's avatar
unknown committed
2149
  if (!base_name->length)
2150
    base_name= &default_key_cache_base;
2151 2152 2153

  pthread_mutex_lock(&LOCK_global_system_variables);
  key_cache= get_key_cache(base_name);
unknown's avatar
unknown committed
2154
                            
2155 2156
  if (!key_cache)
  {
2157
    /* Key cache didn't exists */
2158
    if (!tmp)					// Tried to delete cache
2159 2160 2161 2162 2163 2164
      goto end;					// Ok, nothing to do
    if (!(key_cache= create_key_cache(base_name->str, base_name->length)))
    {
      error= 1;
      goto end;
    }
2165
  }
2166 2167 2168 2169 2170 2171 2172 2173 2174

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

unknown's avatar
unknown committed
2175
  if (!tmp)					// Zero size means delete
2176
  {
2177 2178 2179
    if (key_cache == sql_key_cache)
      goto end;					// Ignore default key cache

unknown's avatar
unknown committed
2180
    if (key_cache->key_cache_inited)		// If initied
2181 2182
    {
      /*
2183 2184
	Move tables using this key cache to the default key cache
	and clear the old key cache.
2185
      */
2186
      NAMED_LIST *list; 
unknown's avatar
unknown committed
2187
      key_cache= (KEY_CACHE *) find_named(&key_caches, base_name->str,
2188
					      base_name->length, &list);
2189 2190 2191 2192 2193
      key_cache->in_init= 1;
      pthread_mutex_unlock(&LOCK_global_system_variables);
      error= reassign_keycache_tables(thd, key_cache, sql_key_cache);
      pthread_mutex_lock(&LOCK_global_system_variables);
      key_cache->in_init= 0;
2194
    }
2195 2196 2197 2198 2199
    /*
      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;
2200 2201
  }

unknown's avatar
unknown committed
2202 2203
  key_cache->param_buff_size=
    (ulonglong) getopt_ull_limit_value(tmp, option_limits);
2204

2205 2206 2207 2208
  /* If key cache didn't existed initialize it, else resize it */
  key_cache->in_init= 1;
  pthread_mutex_unlock(&LOCK_global_system_variables);

unknown's avatar
unknown committed
2209
  if (!key_cache->key_cache_inited)
2210
    error= (bool) (ha_init_key_cache("", key_cache));
unknown's avatar
unknown committed
2211
  else
2212
    error= (bool)(ha_resize_key_cache(key_cache));
2213

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

2217 2218 2219
end:
  pthread_mutex_unlock(&LOCK_global_system_variables);
  return error;
2220 2221
}

2222 2223

bool sys_var_key_cache_long::update(THD *thd, set_var *var)
2224
{
unknown's avatar
unknown committed
2225
  ulong tmp= (ulong) var->value->val_int();
2226
  LEX_STRING *base_name= &var->base;
2227 2228
  bool error= 0;

2229
  if (!base_name->length)
unknown's avatar
unknown committed
2230
    base_name= &default_key_cache_base;
2231 2232

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

unknown's avatar
unknown committed
2235 2236
  if (!key_cache && !(key_cache= create_key_cache(base_name->str,
				                  base_name->length)))
2237 2238 2239 2240
  {
    error= 1;
    goto end;
  }
unknown's avatar
unknown committed
2241

2242 2243 2244 2245 2246 2247 2248 2249 2250
  /*
    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))=
2251
    (ulong) getopt_ull_limit_value(tmp, option_limits);
unknown's avatar
unknown committed
2252

2253 2254 2255 2256 2257
  /*
    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;
2258

2259
  pthread_mutex_unlock(&LOCK_global_system_variables);
2260

2261 2262 2263 2264 2265 2266 2267 2268
  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;
2269 2270
}

2271

2272 2273 2274 2275
/*****************************************************************************
  Functions to handle SET NAMES and SET CHARACTER SET
*****************************************************************************/

unknown's avatar
unknown committed
2276
int set_var_collation_client::check(THD *thd)
2277 2278 2279 2280
{
  return 0;
}

unknown's avatar
unknown committed
2281
int set_var_collation_client::update(THD *thd)
2282
{
2283 2284
  thd->variables.character_set_client= character_set_client;
  thd->variables.character_set_results= character_set_results;
unknown's avatar
unknown committed
2285
  thd->variables.collation_connection= collation_connection;
unknown's avatar
unknown committed
2286
  thd->update_charset();
2287 2288 2289 2290 2291 2292 2293
  thd->protocol_simple.init(thd);
  thd->protocol_prep.init(thd);
  return 0;
}

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

unknown's avatar
unknown committed
2294 2295
bool sys_var_timestamp::update(THD *thd,  set_var *var)
{
2296
  thd->set_time((time_t) var->save_result.ulonglong_value);
unknown's avatar
unknown committed
2297 2298 2299 2300 2301 2302 2303 2304 2305 2306
  return 0;
}


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


2307 2308
byte *sys_var_timestamp::value_ptr(THD *thd, enum_var_type type,
				   LEX_STRING *base)
unknown's avatar
unknown committed
2309 2310 2311 2312 2313 2314 2315 2316
{
  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)
{
2317
  thd->insert_id(var->save_result.ulonglong_value);
unknown's avatar
unknown committed
2318 2319 2320 2321
  return 0;
}


2322 2323
byte *sys_var_last_insert_id::value_ptr(THD *thd, enum_var_type type,
					LEX_STRING *base)
unknown's avatar
unknown committed
2324 2325 2326 2327 2328 2329 2330 2331
{
  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)
{
2332
  thd->next_insert_id= var->save_result.ulonglong_value;
unknown's avatar
unknown committed
2333 2334 2335 2336
  return 0;
}


2337 2338
byte *sys_var_insert_id::value_ptr(THD *thd, enum_var_type type,
				   LEX_STRING *base)
unknown's avatar
unknown committed
2339 2340 2341 2342 2343
{
  return (byte*) &thd->current_insert_id;
}


unknown's avatar
SCRUM  
unknown committed
2344
#ifdef HAVE_REPLICATION
unknown's avatar
unknown committed
2345 2346
bool sys_var_slave_skip_counter::check(THD *thd, set_var *var)
{
2347
  int result= 0;
unknown's avatar
unknown committed
2348
  pthread_mutex_lock(&LOCK_active_mi);
unknown's avatar
unknown committed
2349 2350 2351 2352 2353 2354 2355
  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);
unknown's avatar
unknown committed
2356
  pthread_mutex_unlock(&LOCK_active_mi);
2357
  var->save_result.ulong_value= (ulong) var->value->val_int();
unknown's avatar
unknown committed
2358 2359 2360 2361 2362 2363
  return result;
}


bool sys_var_slave_skip_counter::update(THD *thd, set_var *var)
{
unknown's avatar
unknown committed
2364
  pthread_mutex_lock(&LOCK_active_mi);
unknown's avatar
unknown committed
2365 2366 2367 2368 2369 2370 2371 2372 2373
  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);
2374
    active_mi->rli.slave_skip_counter= var->save_result.ulong_value;
unknown's avatar
unknown committed
2375 2376 2377
    pthread_mutex_unlock(&active_mi->rli.data_lock);
  }
  pthread_mutex_unlock(&active_mi->rli.run_lock);
unknown's avatar
unknown committed
2378
  pthread_mutex_unlock(&LOCK_active_mi);
unknown's avatar
unknown committed
2379
  return 0;
2380 2381 2382 2383 2384 2385
}


bool sys_var_sync_binlog_period::update(THD *thd, set_var *var)
{
  pthread_mutex_t *lock_log= mysql_bin_log.get_log_lock();
2386
  sync_binlog_period= (ulong) var->save_result.ulonglong_value;
2387 2388 2389 2390 2391 2392 2393 2394 2395
  /*
    Must reset the counter otherwise it may already be beyond the new period
    and so the new period will not be taken into account. Need mutex otherwise
    might be cancelled by a simultanate ++ in MYSQL_LOG::write().
  */
  pthread_mutex_lock(lock_log);
  sync_binlog_counter= 0;
  pthread_mutex_unlock(lock_log);
  return 0;
unknown's avatar
unknown committed
2396
}
unknown's avatar
SCRUM  
unknown committed
2397
#endif /* HAVE_REPLICATION */
unknown's avatar
unknown committed
2398

2399 2400
bool sys_var_rand_seed1::update(THD *thd, set_var *var)
{
2401
  thd->rand.seed1= (ulong) var->save_result.ulonglong_value;
2402 2403 2404 2405 2406
  return 0;
}

bool sys_var_rand_seed2::update(THD *thd, set_var *var)
{
2407
  thd->rand.seed2= (ulong) var->save_result.ulonglong_value;
2408 2409 2410 2411
  return 0;
}


2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
bool sys_var_thd_time_zone::check(THD *thd, set_var *var)
{
  char buff[MAX_TIME_ZONE_NAME_LENGTH]; 
  String str(buff, sizeof(buff), &my_charset_latin1);
  String *res= var->value->val_str(&str);

#if defined(HAVE_REPLICATION) && (MYSQL_VERSION_ID < 50000)
  if ((var->type == OPT_GLOBAL) &&
      (mysql_bin_log.is_open() ||
       active_mi->slave_running || active_mi->rli.slave_running))
  {
    my_printf_error(0, "Binary logging and replication forbid changing "
                       "of the global server time zone", MYF(0));
    return 1;
  }
#endif
2428 2429 2430

  if (!(var->save_result.time_zone=
          my_tz_find(res, thd->lex->time_zone_tables_used)))
2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474
  {
    my_error(ER_UNKNOWN_TIME_ZONE, MYF(0), res ? res->c_ptr() : "NULL");
    return 1;
  }
  return 0;
}


bool sys_var_thd_time_zone::update(THD *thd, set_var *var)
{
  /* We are using Time_zone object found during check() phase */ 
  *get_tz_ptr(thd,var->type)= var->save_result.time_zone;
  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.
  */
  return (byte *)((*get_tz_ptr(thd,type))->get_name()->ptr());
}


Time_zone** sys_var_thd_time_zone::get_tz_ptr(THD *thd, 
                                              enum_var_type type)
{
  if (type == OPT_GLOBAL)
    return &global_system_variables.time_zone;
  else
    return &thd->variables.time_zone;
}


void sys_var_thd_time_zone::set_default(THD *thd, enum_var_type type)
{
 if (type == OPT_GLOBAL)
 {
   if (default_tz_name)
   {
     String str(default_tz_name, &my_charset_latin1);
2475 2476
     global_system_variables.time_zone=
       my_tz_find(&str, thd->lex->time_zone_tables_used);
2477 2478 2479 2480 2481 2482 2483 2484
   }
   else
     global_system_variables.time_zone= my_tz_SYSTEM;
 }
 else
   thd->variables.time_zone= global_system_variables.time_zone;
}

unknown's avatar
unknown committed
2485 2486 2487 2488 2489 2490
/*
  Functions to update thd->options bits
*/

static bool set_option_bit(THD *thd, set_var *var)
{
2491 2492 2493
  sys_var_thd_bit *sys_var= ((sys_var_thd_bit*) var->var);
  if ((var->save_result.ulong_value != 0) == sys_var->reverse)
    thd->options&= ~sys_var->bit_flag;
unknown's avatar
unknown committed
2494
  else
2495
    thd->options|= sys_var->bit_flag;
unknown's avatar
unknown committed
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529
  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;
}

2530
static int check_log_update(THD *thd, set_var *var)
unknown's avatar
unknown committed
2531
{
2532 2533 2534 2535 2536 2537 2538
#ifndef NO_EMBEDDED_ACCESS_CHECKS
  if (!(thd->master_access & SUPER_ACL))
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
#endif
2539 2540 2541 2542 2543
  return 0;
}

static bool set_log_update(THD *thd, set_var *var)
{
unknown's avatar
unknown committed
2544 2545 2546 2547 2548 2549 2550
  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;
}

2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
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
  if (thd->master_access & SUPER_ACL)
    return 0;
  else
  {
    my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
    return 1;
  }
#else
  return 0;
#endif
}

2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581
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;
}

unknown's avatar
unknown committed
2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640

/****************************************************************************
  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()
{
2641 2642
  hash_init(&system_variable_hash, system_charset_info,
	    array_elements(sys_variables),0,0,
unknown's avatar
unknown committed
2643
	    (hash_get_key) get_sys_var_length,0,0);
unknown's avatar
unknown committed
2644 2645 2646 2647 2648 2649 2650
  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);
unknown's avatar
SCRUM  
unknown committed
2651
    my_hash_insert(&system_variable_hash, (byte*) *var);
unknown's avatar
unknown committed
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
  }
  /*
    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)
*/

2686
sys_var *find_sys_var(const char *str, uint length)
unknown's avatar
unknown committed
2687
{
unknown's avatar
unknown committed
2688 2689
  sys_var *var= (sys_var*) hash_search(&system_variable_hash,
				       (byte*) str,
unknown's avatar
unknown committed
2690 2691 2692
				       length ? length :
				       strlen(str));
  if (!var)
2693
    net_printf(current_thd, ER_UNKNOWN_SYSTEM_VARIABLE, (char*) str);
unknown's avatar
unknown committed
2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
  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
2716 2717
    1	ERROR, message sent (normally no variables was updated)
    -1  ERROR, message not sent
unknown's avatar
unknown committed
2718 2719
*/

2720
int sql_set_variables(THD *thd, List<set_var_base> *var_list)
unknown's avatar
unknown committed
2721
{
unknown's avatar
unknown committed
2722
  int error;
unknown's avatar
unknown committed
2723
  List_iterator_fast<set_var_base> it(*var_list);
unknown's avatar
unknown committed
2724
  DBUG_ENTER("sql_set_variables");
unknown's avatar
unknown committed
2725 2726 2727 2728

  set_var_base *var;
  while ((var=it++))
  {
unknown's avatar
unknown committed
2729
    if ((error= var->check(thd)))
2730
      goto err;
unknown's avatar
unknown committed
2731
  }
unknown's avatar
unknown committed
2732
  if (!(error= test(thd->net.report_error)))
2733 2734 2735 2736 2737
  {
    it.rewind();
    while ((var= it++))
      error|= var->update(thd);         // Returns 0, -1 or 1
  }
unknown's avatar
unknown committed
2738

2739 2740
err:
  free_underlaid_joins(thd, &thd->lex->select_lex);
unknown's avatar
unknown committed
2741
  DBUG_RETURN(error);
unknown's avatar
unknown committed
2742 2743 2744
}


2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774
/*
  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;
}


unknown's avatar
unknown committed
2775 2776 2777 2778
/*****************************************************************************
  Functions to handle SET mysql_internal_variable=const_expr
*****************************************************************************/

2779
int set_var::check(THD *thd)
unknown's avatar
unknown committed
2780 2781 2782 2783 2784 2785
{
  if (var->check_type(type))
  {
    my_error(type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE,
	     MYF(0),
	     var->name);
2786
    return -1;
unknown's avatar
unknown committed
2787 2788 2789 2790 2791 2792 2793 2794 2795
  }
  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);
2796
      return -1;
unknown's avatar
unknown committed
2797 2798 2799 2800
    }
    return 0;
  }

unknown's avatar
unknown committed
2801 2802
  if ((!value->fixed && 
       value->fix_fields(thd, 0, &value)) || value->check_cols(1))
2803
    return -1;
unknown's avatar
unknown committed
2804 2805 2806
  if (var->check_update_type(value->result_type()))
  {
    my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var->name);
2807
    return -1;
2808
  }
2809
  return var->check(thd, this) ? -1 : 0;
unknown's avatar
unknown committed
2810 2811 2812
}


unknown's avatar
unknown committed
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824
/*
  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
*/
2825 2826 2827 2828 2829 2830 2831 2832 2833
int set_var::light_check(THD *thd)
{
  if (var->check_type(type))
  {
    my_error(type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE,
	     MYF(0),
	     var->name);
    return -1;
  }
unknown's avatar
unknown committed
2834
  if (type == OPT_GLOBAL && check_global_access(thd, SUPER_ACL))
2835 2836
    return 1;

unknown's avatar
unknown committed
2837 2838
  if (value && ((!value->fixed && value->fix_fields(thd, 0, &value)) ||
                value->check_cols(1)))
2839 2840 2841 2842 2843
    return -1;
  return 0;
}


2844
int set_var::update(THD *thd)
unknown's avatar
unknown committed
2845 2846 2847 2848
{
  if (!value)
    var->set_default(thd, type);
  else if (var->update(thd, this))
2849
    return -1;				// should never happen
unknown's avatar
unknown committed
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859
  if (var->after_update)
    (*var->after_update)(thd, type);
  return 0;
}


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

2860
int set_var_user::check(THD *thd)
unknown's avatar
unknown committed
2861
{
unknown's avatar
unknown committed
2862 2863
  /*
    Item_func_set_user_var can't substitute something else on its place =>
2864
    0 can be passed as last argument (reference on item)
unknown's avatar
unknown committed
2865 2866
  */
  return (user_var_item->fix_fields(thd, 0, (Item**) 0) ||
2867
	  user_var_item->check()) ? -1 : 0;
unknown's avatar
unknown committed
2868 2869 2870
}


unknown's avatar
unknown committed
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882
/*
  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
*/
2883 2884 2885 2886 2887 2888 2889 2890 2891 2892
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)
  */
  return (user_var_item->fix_fields(thd, 0, (Item**) 0));
}


2893
int set_var_user::update(THD *thd)
unknown's avatar
unknown committed
2894 2895 2896 2897
{
  if (user_var_item->update())
  {
    /* Give an error if it's not given already */
2898 2899
    my_error(ER_SET_CONSTANTS_ONLY, MYF(0));
    return -1;
unknown's avatar
unknown committed
2900 2901 2902 2903 2904 2905 2906 2907 2908
  }
  return 0;
}


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

2909
int set_var_password::check(THD *thd)
unknown's avatar
unknown committed
2910
{
unknown's avatar
unknown committed
2911
#ifndef NO_EMBEDDED_ACCESS_CHECKS
unknown's avatar
unknown committed
2912 2913
  if (!user->host.str)
    user->host.str= (char*) thd->host_or_ip;
2914
  /* Returns 1 as the function sends error to client */
2915 2916 2917
  return check_change_password(thd, user->host.str, user->user.str, password) ?
         1 : 0;
#else
unknown's avatar
unknown committed
2918 2919
  return 0;
#endif
unknown's avatar
unknown committed
2920 2921
}

2922
int set_var_password::update(THD *thd)
unknown's avatar
unknown committed
2923
{
unknown's avatar
unknown committed
2924
#ifndef NO_EMBEDDED_ACCESS_CHECKS
2925
  /* Returns 1 as the function sends error to client */
2926 2927
  return change_password(thd, user->host.str, user->user.str, password) ?
	  1 : 0;
unknown's avatar
unknown committed
2928 2929 2930
#else
  return 0;
#endif
unknown's avatar
unknown committed
2931 2932
}

2933 2934 2935 2936
/****************************************************************************
 Functions to handle table_type
****************************************************************************/

unknown's avatar
unknown committed
2937 2938
/* Based upon sys_var::check_enum() */

unknown's avatar
unknown committed
2939
bool sys_var_thd_storage_engine::check(THD *thd, set_var *var)
2940 2941 2942 2943 2944 2945 2946
{
  char buff[80];
  const char *value;
  String str(buff, sizeof(buff), &my_charset_latin1), *res;

  if (var->value->result_type() == STRING_RESULT)
  {
2947
    enum db_type db_type;
2948 2949
    if (!(res=var->value->val_str(&str)) ||
	!(var->save_result.ulong_value=
unknown's avatar
unknown committed
2950
	 (ulong) (db_type= ha_resolve_by_name(res->ptr(), res->length()))) ||
2951
	ha_checktype(db_type) != db_type) 
2952 2953 2954 2955 2956 2957
    {
      value= res ? res->c_ptr() : "NULL";
      goto err;
    }
    return 0;
  }
unknown's avatar
unknown committed
2958
  value= "unknown";
2959 2960

err:
unknown's avatar
unknown committed
2961
  my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), value);
2962 2963 2964
  return 1;    
}

unknown's avatar
unknown committed
2965

unknown's avatar
unknown committed
2966 2967
byte *sys_var_thd_storage_engine::value_ptr(THD *thd, enum_var_type type,
					    LEX_STRING *base)
2968 2969 2970 2971
{
  ulong val;
  val= ((type == OPT_GLOBAL) ? global_system_variables.*offset :
        thd->variables.*offset);
unknown's avatar
unknown committed
2972
  const char *table_type= ha_get_storage_engine((enum db_type)val);
unknown's avatar
unknown committed
2973
  return (byte *) table_type;
2974 2975
}

unknown's avatar
unknown committed
2976

unknown's avatar
unknown committed
2977
void sys_var_thd_storage_engine::set_default(THD *thd, enum_var_type type)
2978 2979 2980 2981 2982 2983 2984
{
  if (type == OPT_GLOBAL)
    global_system_variables.*offset= (ulong) DB_TYPE_MYISAM;
  else
    thd->variables.*offset= (ulong) (global_system_variables.*offset);
}

unknown's avatar
unknown committed
2985

unknown's avatar
unknown committed
2986
bool sys_var_thd_storage_engine::update(THD *thd, set_var *var)
2987 2988 2989 2990 2991 2992 2993 2994
{
  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;
}

unknown's avatar
unknown committed
2995 2996 2997 2998
void sys_var_thd_table_type::warn_deprecated(THD *thd)
{
  push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
		      ER_WARN_DEPRECATED_SYNTAX,
2999 3000
		      ER(ER_WARN_DEPRECATED_SYNTAX), "table_type",
                      "storage_engine"); 
unknown's avatar
unknown committed
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014
}

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

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

unknown's avatar
unknown committed
3015

3016 3017 3018
/****************************************************************************
 Functions to handle sql_mode
****************************************************************************/
3019

3020 3021
byte *sys_var_thd_sql_mode::value_ptr(THD *thd, enum_var_type type,
				      LEX_STRING *base)
3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061
{
  ulong val;
  char buff[256];
  String tmp(buff, sizeof(buff), &my_charset_latin1);

  tmp.length(0);
  val= ((type == OPT_GLOBAL) ? global_system_variables.*offset :
        thd->variables.*offset);
  for (uint i= 0; val; val>>= 1, i++)
  {
    if (val & 1)
    {
      tmp.append(enum_names->type_names[i]);
      tmp.append(',');
    }
  }
  if (tmp.length())
    tmp.length(tmp.length() - 1);
  return (byte*) thd->strmake(tmp.ptr(), tmp.length());
}


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

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
    thd->variables.sql_mode= fix_sql_mode(thd->variables.sql_mode);
}

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

3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093
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)
    sql_mode|= (MODE_REAL_AS_FLOAT | MODE_PIPES_AS_CONCAT | MODE_ANSI_QUOTES |
		MODE_IGNORE_SPACE | MODE_ONLY_FULL_GROUP_BY);
  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 |
		MODE_NO_FIELD_OPTIONS);
  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);
3094
  if (sql_mode & MODE_MAXDB)
3095 3096 3097 3098 3099 3100 3101 3102 3103 3104
    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_MYSQL40)
    sql_mode|= MODE_NO_FIELD_OPTIONS;
  if (sql_mode & MODE_MYSQL323)
    sql_mode|= MODE_NO_FIELD_OPTIONS;
  return sql_mode;
}
3105 3106


3107 3108 3109 3110
/****************************************************************************
  Named list handling
****************************************************************************/

unknown's avatar
unknown committed
3111 3112
gptr find_named(I_List<NAMED_LIST> *list, const char *name, uint length,
		NAMED_LIST **found)
3113 3114 3115 3116 3117 3118
{
  I_List_iterator<NAMED_LIST> it(*list);
  NAMED_LIST *element;
  while ((element= it++))
  {
    if (element->cmp(name, length))
unknown's avatar
unknown committed
3119
    {
3120 3121
      if (found)
        *found= element;
3122
      return element->data;
unknown's avatar
unknown committed
3123
    }
3124 3125 3126 3127 3128
  }
  return 0;
}


3129 3130
void delete_elements(I_List<NAMED_LIST> *list,
		     void (*free_element)(const char *name, gptr))
3131 3132
{
  NAMED_LIST *element;
unknown's avatar
unknown committed
3133
  DBUG_ENTER("delete_elements");
3134 3135
  while ((element= list->get()))
  {
3136
    (*free_element)(element->name, element->data);
3137 3138
    delete element;
  }
unknown's avatar
unknown committed
3139
  DBUG_VOID_RETURN;
3140 3141 3142 3143 3144
}


/* Key cache functions */

unknown's avatar
unknown committed
3145
static KEY_CACHE *create_key_cache(const char *name, uint length)
3146
{
unknown's avatar
unknown committed
3147
  KEY_CACHE *key_cache;
3148 3149 3150
  DBUG_ENTER("create_key_cache");
  DBUG_PRINT("enter",("name: %.*s", length, name));
  
unknown's avatar
unknown committed
3151
  if ((key_cache= (KEY_CACHE*) my_malloc(sizeof(KEY_CACHE),
3152
					     MYF(MY_ZEROFILL | MY_WME))))
unknown's avatar
unknown committed
3153 3154
  {
    if (!new NAMED_LIST(&key_caches, name, length, (gptr) key_cache))
3155 3156
    {
      my_free((char*) key_cache, MYF(0));
unknown's avatar
unknown committed
3157
      key_cache= 0;
3158 3159 3160 3161 3162 3163 3164 3165 3166
    }
    else
    {
      /*
	Set default values for a key cache
	The values in dflt_key_cache_var is set by my_getopt() at startup

	We don't set 'buff_size' as this is used to enable the key cache
      */
unknown's avatar
unknown committed
3167 3168 3169
      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;
3170
    }
unknown's avatar
unknown committed
3171
  }
3172
  DBUG_RETURN(key_cache);
3173 3174 3175
}


unknown's avatar
unknown committed
3176
KEY_CACHE *get_or_create_key_cache(const char *name, uint length)
3177
{
unknown's avatar
unknown committed
3178
  LEX_STRING key_cache_name;
unknown's avatar
unknown committed
3179
  KEY_CACHE *key_cache;
3180

unknown's avatar
unknown committed
3181 3182
  key_cache_name.str= (char *) name;
  key_cache_name.length= length;
3183 3184
  pthread_mutex_lock(&LOCK_global_system_variables);
  if (!(key_cache= get_key_cache(&key_cache_name)))
3185
    key_cache= create_key_cache(name, length);
3186
  pthread_mutex_unlock(&LOCK_global_system_variables);
3187 3188 3189 3190
  return key_cache;
}


unknown's avatar
unknown committed
3191
void free_key_cache(const char *name, KEY_CACHE *key_cache)
3192
{
3193 3194
  ha_end_key_cache(key_cache);
  my_free((char*) key_cache, MYF(0));
unknown's avatar
unknown committed
3195 3196 3197
}


unknown's avatar
unknown committed
3198
bool process_key_caches(int (* func) (const char *name, KEY_CACHE *))
3199
{
unknown's avatar
unknown committed
3200 3201
  I_List_iterator<NAMED_LIST> it(key_caches);
  NAMED_LIST *element;
3202

unknown's avatar
unknown committed
3203 3204
  while ((element= it++))
  {
unknown's avatar
unknown committed
3205
    KEY_CACHE *key_cache= (KEY_CACHE *) element->data;
3206
    func(element->name, key_cache);
unknown's avatar
unknown committed
3207 3208
  }
  return 0;
3209 3210
}

3211

unknown's avatar
unknown committed
3212 3213 3214 3215 3216 3217
/****************************************************************************
  Used templates
****************************************************************************/

#ifdef __GNUC__
template class List<set_var_base>;
unknown's avatar
unknown committed
3218
template class List_iterator_fast<set_var_base>;
3219
template class I_List_iterator<NAMED_LIST>;
unknown's avatar
unknown committed
3220
#endif