18 #include "my_global.h"
19 #include "sql_class.h"
25 #include "transaction.h"
26 #include "sql_locale.h"
29 #include "sql_parse.h"
30 #include "sql_table.h"
43 static HASH system_variable_hash;
44 static PolyLock_mutex PLock_global_system_variables(&LOCK_global_system_variables);
50 static uchar *get_sys_var_length(
const sys_var *var,
size_t *length,
53 *length= var->name.length;
54 return (uchar*) var->name.str;
61 DBUG_ENTER(
"sys_var_init");
64 DBUG_ASSERT(system_charset_info != NULL);
66 if (my_hash_init(&system_variable_hash, system_charset_info, 100, 0,
67 0, (my_hash_get_key) get_sys_var_length, 0, HASH_UNIQUE))
70 if (mysql_add_sys_var_chain(all_sys_vars.first))
76 fprintf(stderr,
"failed to initialize System variables");
80 int sys_var_add_options(std::vector<my_option> *long_options,
int parse_flags)
82 DBUG_ENTER(
"sys_var_add_options");
84 for (
sys_var *var=all_sys_vars.first; var; var= var->next)
86 if (var->register_option(long_options, parse_flags))
93 fprintf(stderr,
"failed to initialize System variables");
99 DBUG_ENTER(
"sys_var_end");
101 my_hash_free(&system_variable_hash);
103 for (
sys_var *var=all_sys_vars.first; var; var= var->next)
136 const char *comment,
int flags_arg, ptrdiff_t off,
137 int getopt_id,
enum get_opt_arg_type getopt_arg_type,
138 SHOW_TYPE show_val_type_arg, longlong def_val,
140 on_check_function on_check_func,
141 on_update_function on_update_func,
142 const char *substitute,
int parse_flag) :
144 binlog_status(binlog_status_arg),
145 flags(flags_arg), m_parse_flag(parse_flag), show_val_type(show_val_type_arg),
146 guard(lock),
offset(off), on_check(on_check_func), on_update(on_update_func),
147 deprecation_substitute(substitute),
159 DBUG_ASSERT(parse_flag == PARSE_NORMAL || getopt_id <= 0 || getopt_id >= 255);
162 name.length= strlen(name_arg);
163 DBUG_ASSERT(name.length <= NAME_CHAR_LEN);
169 option.arg_type= getopt_arg_type;
170 option.value= (uchar **)global_var_ptr();
171 option.def_value= def_val;
174 chain->last->next=
this;
180 bool sys_var::update(THD *thd,
set_var *var)
182 enum_var_type
type= var->type;
183 if (type == OPT_GLOBAL || scope() == GLOBAL)
191 AutoWLock lock1(&PLock_global_system_variables);
193 return global_update(thd, var) ||
194 (on_update && on_update(
this, thd, OPT_GLOBAL));
197 return session_update(thd, var) ||
198 (on_update && on_update(
this, thd, OPT_SESSION));
206 uchar *sys_var::global_value_ptr(THD *thd,
LEX_STRING *base)
208 return global_var_ptr();
211 bool sys_var::check(THD *thd,
set_var *var)
213 if ((var->
value && do_check(thd, var))
214 || (on_check && on_check(
this, thd, var)))
216 if (!thd->is_error())
218 char buff[STRING_BUFFER_USUAL_SIZE];
219 String str(buff,
sizeof(buff), system_charset_info), *res;
223 str.set(STRING_WITH_LEN(
"DEFAULT"), &my_charset_latin1);
226 else if (!(res=var->
value->val_str(&str)))
228 str.set(STRING_WITH_LEN(
"NULL"), &my_charset_latin1);
232 my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name.str, err.ptr());
239 uchar *sys_var::value_ptr(THD *thd, enum_var_type type,
LEX_STRING *base)
241 if (type == OPT_GLOBAL || scope() == GLOBAL)
245 return global_value_ptr(thd, base);
253 if (var->type == OPT_GLOBAL || scope() == GLOBAL)
254 global_save_default(thd, var);
256 session_save_default(thd, var);
258 return check(thd, var) || update(thd, var);
261 void sys_var::do_deprecated_warning(THD *thd)
263 if (deprecation_substitute != NULL)
265 char buf1[NAME_CHAR_LEN + 3];
266 strxnmov(buf1,
sizeof(buf1)-1,
"@@", name.str, 0);
272 uint errmsg= deprecation_substitute[0] ==
'\0'
273 ? ER_WARN_DEPRECATED_SYNTAX_NO_REPLACEMENT
274 : ER_WARN_DEPRECATED_SYNTAX;
276 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
277 ER_WARN_DEPRECATED_SYNTAX, ER(errmsg),
278 buf1, deprecation_substitute);
280 sql_print_warning(ER_DEFAULT(errmsg), buf1, deprecation_substitute);
296 bool throw_bounds_warning(THD *thd,
const char *
name,
297 bool fixed,
bool is_unsigned, longlong v)
304 ullstr((ulonglong) v, buf);
308 if (thd->variables.sql_mode & MODE_STRICT_ALL_TABLES)
310 my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, buf);
313 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
314 ER_TRUNCATED_WRONG_VALUE,
315 ER(ER_TRUNCATED_WRONG_VALUE), name, buf);
320 bool throw_bounds_warning(THD *thd,
const char *
name,
bool fixed,
double v)
326 my_gcvt(v, MY_GCVT_ARG_DOUBLE,
sizeof(buf) - 1, buf, NULL);
328 if (thd->variables.sql_mode & MODE_STRICT_ALL_TABLES)
330 my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), name, buf);
333 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
334 ER_TRUNCATED_WRONG_VALUE,
335 ER(ER_TRUNCATED_WRONG_VALUE), name, buf);
342 return is_os_charset ? thd->variables.character_set_filesystem :
348 const char *old_name;
349 const char *new_name;
354 {
"cp1251_koi8" ,
"cp1251" },
355 {
"cp1250_latin2" ,
"cp1250" },
356 {
"kam_latin2" ,
"keybcs2" },
357 {
"mac_latin2" ,
"MacRoman" },
358 {
"macce_latin2" ,
"MacCE" },
359 {
"pc2_latin2" ,
"pclatin2" },
360 {
"vga_latin2" ,
"pclatin1" },
361 {
"koi8_cp1251" ,
"koi8r" },
362 {
"win1251ukr_koi8_ukr" ,
"win1251ukr" },
363 {
"koi8_ukr_win1251ukr" ,
"koi8u" },
367 const CHARSET_INFO *get_old_charset_by_name(
const char *name)
371 for (conv= old_conv; conv->old_name; conv++)
373 if (!my_strcasecmp(&my_charset_latin1, name, conv->old_name))
374 return get_charset_by_csname(conv->new_name, MY_CS_PRIMARY, MYF(0));
404 for (var= first; var; var= var->next)
407 if (my_hash_insert(&system_variable_hash, (uchar*) var))
409 fprintf(stderr,
"*** duplicate variable name '%s' ?\n", var->name.str);
416 for (; first != var; first= first->next)
417 my_hash_delete(&system_variable_hash, (uchar*) first);
434 int mysql_del_sys_var_chain(
sys_var *first)
440 for (
sys_var *var= first; var; var= var->next)
441 result|= my_hash_delete(&system_variable_hash, (uchar*) var);
449 return strcmp(a->name, b->name);
466 SHOW_VAR* enumerate_sys_vars(THD *thd,
bool sorted,
enum enum_var_type type)
468 int count= system_variable_hash.records,
i;
476 for (
i= 0;
i < count;
i++)
481 if (type == OPT_GLOBAL && var->check_type(type))
485 if (var->not_visible())
488 show->name= var->name.str;
489 show->value= (
char*) var;
490 show->type= SHOW_SYS;
496 my_qsort(result, show-result,
sizeof(
SHOW_VAR),
497 (qsort_cmp) show_cmp);
518 sys_var *intern_find_sys_var(
const char *str, uint length)
526 var= (
sys_var*) my_hash_search(&system_variable_hash,
527 (uchar*) str, length ? length : strlen(str));
530 if (var && var->not_visible())
561 DBUG_ENTER(
"sql_set_variables");
566 if ((error= var->check(thd)))
569 if (!(error=
test(thd->is_error())))
573 error|= var->update(thd);
597 var->do_deprecated_warning(thd);
598 if (var->is_readonly())
600 my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0), var->name.str,
"read only");
603 if (var->check_type(type))
605 int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
606 my_error(err, MYF(0), var->name.str);
615 if ((!
value->fixed &&
618 if (var->check_update_type(
value->result_type()))
620 my_error(ER_WRONG_TYPE_FOR_VAR, MYF(0), var->name.str);
623 return var->
check(thd,
this) ? -1 : 0;
641 if (var->check_type(type))
643 int err= type == OPT_GLOBAL ? ER_LOCAL_VARIABLE : ER_GLOBAL_VARIABLE;
644 my_error(err, MYF(0), var->name);
651 value->check_cols(1)))
670 return value ? var->
update(thd,
this) : var->set_default(thd,
this);
680 str->append(type == OPT_GLOBAL ?
"GLOBAL " :
"SESSION ");
683 str->append(base.str, base.length);
684 str->append(STRING_WITH_LEN(
"."));
686 str->append(var->name.str,var->name.length);
687 str->append(STRING_WITH_LEN(
"="));
691 str->append(STRING_WITH_LEN(
"DEFAULT"));
699 int set_var_user::check(THD *thd)
705 return (user_var_item->fix_fields(thd, (
Item**) 0) ||
706 user_var_item->
check(0)) ? -1 : 0;
728 return (user_var_item->fix_fields(thd, (
Item**) 0));
732 int set_var_user::update(THD *thd)
734 if (user_var_item->
update())
737 my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY), MYF(0));
744 void set_var_user::print(THD *thd,
String *str)
746 user_var_item->print_assignment(str, QT_ORDINARY);
767 #ifndef NO_EMBEDDED_ACCESS_CHECKS
770 DBUG_ASSERT(thd->security_ctx->priv_host);
771 user->host.str= (
char *) thd->security_ctx->priv_host;
772 user->host.length= strlen(thd->security_ctx->priv_host);
782 DBUG_ASSERT(thd->security_ctx->user);
783 user->user.str= (
char *) thd->security_ctx->user;
784 user->user.length= strlen(thd->security_ctx->user);
787 return check_change_password(thd, user->host.str, user->user.str,
788 password, strlen(password)) ? 1 : 0;
794 int set_var_password::update(THD *thd)
796 #ifndef NO_EMBEDDED_ACCESS_CHECKS
798 return change_password(thd, user->host.str, user->user.str, password) ?
805 void set_var_password::print(THD *thd,
String *str)
807 if (user->user.str != NULL && user->user.length > 0)
809 str->append(STRING_WITH_LEN(
"PASSWORD FOR "));
810 append_identifier(thd, str, user->user.str, user->user.length);
811 if (user->host.str != NULL && user->host.length > 0)
813 str->append(STRING_WITH_LEN(
"@"));
814 append_identifier(thd, str, user->host.str, user->host.length);
816 str->append(STRING_WITH_LEN(
"="));
819 str->append(STRING_WITH_LEN(
"PASSWORD FOR CURRENT_USER()="));
820 str->append(STRING_WITH_LEN(
"<secret>"));
827 int set_var_collation_client::check(THD *thd)
830 if (!is_supported_parser_charset(character_set_client))
832 my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0),
"character_set_client",
833 character_set_client->csname);
839 int set_var_collation_client::update(THD *thd)
841 thd->variables.character_set_client= character_set_client;
842 thd->variables.character_set_results= character_set_results;
843 thd->variables.collation_connection= collation_connection;
844 thd->update_charset();
845 thd->protocol_text.init(thd);
846 thd->protocol_binary.init(thd);
850 void set_var_collation_client::print(THD *thd,
String *str)
852 str->append((set_cs_flags & SET_CS_NAMES) ?
"NAMES " :
"CHARACTER SET ");
853 if (set_cs_flags & SET_CS_DEFAULT)
854 str->append(
"DEFAULT");
858 str->append(character_set_client->csname);
860 if (set_cs_flags & SET_CS_COLLATE)
862 str->append(
" COLLATE '");
863 str->append(collation_connection->name);