1 #ifndef SET_VAR_INCLUDED
2 #define SET_VAR_INCLUDED
3 /* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; version 2 of the License.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
17 
18 /**
19   @file
20   "public" interface to sys_var - server configuration variables.
21 */
22 
23 #ifdef USE_PRAGMA_INTERFACE
24 #pragma interface                       /* gcc class implementation */
25 #endif
26 
27 #include <my_getopt.h>
28 
29 class sys_var;
30 class set_var;
31 class sys_var_pluginvar;
32 class PolyLock;
33 class Item_func_set_user_var;
34 
35 // This include needs to be here since item.h requires enum_var_type :-P
36 #include "item.h"                          /* Item */
37 #include "sql_class.h"                     /* THD  */
38 
39 extern TYPELIB bool_typelib;
40 
41 struct sys_var_chain
42 {
43   sys_var *first;
44   sys_var *last;
45 };
46 
47 int mysql_add_sys_var_chain(sys_var *chain);
48 int mysql_del_sys_var_chain(sys_var *chain);
49 
50 /**
51   A class representing one system variable - that is something
52   that can be accessed as @@global.variable_name or @@session.variable_name,
53   visible in SHOW xxx VARIABLES and in INFORMATION_SCHEMA.xxx_VARIABLES,
54   optionally it can be assigned to, optionally it can have a command-line
55   counterpart with the same name.
56 */
57 class sys_var
58 {
59 public:
60   sys_var *next;
61   LEX_CSTRING name;
62   enum flag_enum { GLOBAL, SESSION, ONLY_SESSION, SCOPE_MASK=1023,
63                    READONLY=1024, ALLOCATED=2048 };
64   static const int PARSE_EARLY= 1;
65   static const int PARSE_NORMAL= 2;
66   /**
67     Enumeration type to indicate for a system variable whether
68     it will be written to the binlog or not.
69   */
70   enum binlog_status_enum { VARIABLE_NOT_IN_BINLOG,
71                             SESSION_VARIABLE_IN_BINLOG } binlog_status;
72 
73 protected:
74   typedef bool (*on_check_function)(sys_var *self, THD *thd, set_var *var);
75   typedef bool (*on_update_function)(sys_var *self, THD *thd, enum_var_type type);
76 
77   int flags;            ///< or'ed flag_enum values
78   int m_parse_flag;     ///< either PARSE_EARLY or PARSE_NORMAL.
79   const SHOW_TYPE show_val_type; ///< what value_ptr() returns for sql_show.cc
80   my_option option;     ///< min, max, default values are stored here
81   PolyLock *guard;      ///< *second* lock that protects the variable
82   ptrdiff_t offset;     ///< offset to the value from global_system_variables
83   on_check_function on_check;
84   on_update_function on_update;
85   const char *const deprecation_substitute;
86   bool is_os_charset; ///< true if the value is in character_set_filesystem
87 
88 public:
89   sys_var(sys_var_chain *chain, const char *name_arg, const char *comment,
90           int flag_args, ptrdiff_t off, int getopt_id,
91           enum get_opt_arg_type getopt_arg_type, SHOW_TYPE show_val_type_arg,
92           longlong def_val, PolyLock *lock, enum binlog_status_enum binlog_status_arg,
93           on_check_function on_check_func, on_update_function on_update_func,
94           const char *substitute, int parse_flag);
95 
~sys_var()96   virtual ~sys_var() {}
97 
98   /**
99     All the cleanup procedures should be performed here
100   */
cleanup()101   virtual void cleanup() {}
102   /**
103     downcast for sys_var_pluginvar. Returns this if it's an instance
104     of sys_var_pluginvar, and 0 otherwise.
105   */
cast_pluginvar()106   virtual sys_var_pluginvar *cast_pluginvar() { return 0; }
107 
108   bool check(THD *thd, set_var *var);
109   uchar *value_ptr(THD *thd, enum_var_type type, LEX_STRING *base);
110 
111   /**
112      Update the system variable with the default value from either
113      session or global scope.  The default value is stored in the
114      'var' argument. Return false when successful.
115   */
116   bool set_default(THD *thd, set_var *var);
117   bool update(THD *thd, set_var *var);
118 
show_type()119   SHOW_TYPE show_type() { return show_val_type; }
scope()120   int scope() const { return flags & SCOPE_MASK; }
121   CHARSET_INFO *charset(THD *thd);
is_readonly()122   bool is_readonly() const { return flags & READONLY; }
123   /**
124     the following is only true for keycache variables,
125     that support the syntax @@keycache_name.variable_name
126   */
is_struct()127   bool is_struct() { return option.var_type & GET_ASK_ADDR; }
is_written_to_binlog(enum_var_type type)128   bool is_written_to_binlog(enum_var_type type)
129   { return type != OPT_GLOBAL && binlog_status == SESSION_VARIABLE_IN_BINLOG; }
130   virtual bool check_update_type(Item_result type) = 0;
check_type(enum_var_type type)131   bool check_type(enum_var_type type)
132   {
133     switch (scope())
134     {
135     case GLOBAL:       return type != OPT_GLOBAL;
136     case SESSION:      return false; // always ok
137     case ONLY_SESSION: return type == OPT_GLOBAL;
138     }
139     return true; // keep gcc happy
140   }
register_option(DYNAMIC_ARRAY * array,int parse_flags)141   bool register_option(DYNAMIC_ARRAY *array, int parse_flags)
142   {
143     return (option.id != -1) && (m_parse_flag & parse_flags) &&
144            insert_dynamic(array, (uchar*)&option);
145   }
146   void do_deprecated_warning(THD *thd);
147 
148 private:
149   virtual bool do_check(THD *thd, set_var *var) = 0;
150   /**
151     save the session default value of the variable in var
152   */
153   virtual void session_save_default(THD *thd, set_var *var) = 0;
154   /**
155     save the global default value of the variable in var
156   */
157   virtual void global_save_default(THD *thd, set_var *var) = 0;
158   virtual bool session_update(THD *thd, set_var *var) = 0;
159   virtual bool global_update(THD *thd, set_var *var) = 0;
160 
161 protected:
162   /**
163     A pointer to a value of the variable for SHOW.
164     It must be of show_val_type type (bool for SHOW_BOOL, int for SHOW_INT,
165     longlong for SHOW_LONGLONG, etc).
166   */
167   virtual uchar *session_value_ptr(THD *thd, LEX_STRING *base);
168   virtual uchar *global_value_ptr(THD *thd, LEX_STRING *base);
169 
170   /**
171     A pointer to a storage area of the variable, to the raw data.
172     Typically it's the same as session_value_ptr(), but it's different,
173     for example, for ENUM, that is printed as a string, but stored as a number.
174   */
session_var_ptr(THD * thd)175   uchar *session_var_ptr(THD *thd)
176   { return ((uchar*)&(thd->variables)) + offset; }
177 
global_var_ptr()178   uchar *global_var_ptr()
179   { return ((uchar*)&global_system_variables) + offset; }
180 };
181 
182 #include "sql_plugin.h"                    /* SHOW_HA_ROWS, SHOW_MY_BOOL */
183 
184 /****************************************************************************
185   Classes for parsing of the SET command
186 ****************************************************************************/
187 
188 /**
189   A base class for everything that can be set with SET command.
190   It's similar to Items, an instance of this is created by the parser
191   for every assigmnent in SET (or elsewhere, e.g. in SELECT).
192 */
193 class set_var_base :public Sql_alloc
194 {
195 public:
set_var_base()196   set_var_base() {}
~set_var_base()197   virtual ~set_var_base() {}
198   virtual int check(THD *thd)=0;           /* To check privileges etc. */
199   virtual int update(THD *thd)=0;                  /* To set the value */
light_check(THD * thd)200   virtual int light_check(THD *thd) { return check(thd); }   /* for PS */
201 };
202 
203 
204 /**
205   set_var_base descendant for assignments to the system variables.
206 */
207 class set_var :public set_var_base
208 {
209 public:
210   sys_var *var; ///< system variable to be updated
211   Item *value;  ///< the expression that provides the new value of the variable
212   enum_var_type type;
213   union ///< temp storage to hold a value between sys_var::check and ::update
214   {
215     ulonglong ulonglong_value;          ///< for all integer, set, enum sysvars
216     double double_value;                ///< for Sys_var_double
217     plugin_ref plugin;                  ///< for Sys_var_plugin
218     Time_zone *time_zone;               ///< for Sys_var_tz
219     LEX_STRING string_value;            ///< for Sys_var_charptr and others
220     void *ptr;                          ///< for Sys_var_struct
221   } save_result;
222   LEX_STRING base; /**< for structured variables, like keycache_name.variable_name */
223 
set_var(enum_var_type type_arg,sys_var * var_arg,const LEX_STRING * base_name_arg,Item * value_arg)224   set_var(enum_var_type type_arg, sys_var *var_arg,
225           const LEX_STRING *base_name_arg, Item *value_arg)
226     :var(var_arg), type(type_arg), base(*base_name_arg)
227   {
228     /*
229       If the set value is a field, change it to a string to allow things like
230       SET table_type=MYISAM;
231     */
232     if (value_arg && value_arg->type() == Item::FIELD_ITEM)
233     {
234       Item_field *item= (Item_field*) value_arg;
235       if (item->field_name)
236       {
237         if (!(value= new Item_string(item->field_name,
238                                      (uint) strlen(item->field_name),
239                                      system_charset_info))) // names are utf8
240 	  value= value_arg;			/* Give error message later */
241       }
242       else
243       {
244         /* Both Item_field and Item_insert_value will return the type as
245         Item::FIELD_ITEM. If the item->field_name is NULL, we assume the
246         object to be Item_insert_value. */
247         value= value_arg;
248       }
249     }
250     else
251       value= value_arg;
252   }
253   int check(THD *thd);
254   int update(THD *thd);
255   int light_check(THD *thd);
256 };
257 
258 
259 /* User variables like @my_own_variable */
260 class set_var_user: public set_var_base
261 {
262   Item_func_set_user_var *user_var_item;
263 public:
set_var_user(Item_func_set_user_var * item)264   set_var_user(Item_func_set_user_var *item)
265     :user_var_item(item)
266   {}
267   int check(THD *thd);
268   int update(THD *thd);
269   int light_check(THD *thd);
270 };
271 
272 /* For SET PASSWORD */
273 
274 class set_var_password: public set_var_base
275 {
276   LEX_USER *user;
277   char *password;
278 public:
set_var_password(LEX_USER * user_arg,char * password_arg)279   set_var_password(LEX_USER *user_arg,char *password_arg)
280     :user(user_arg), password(password_arg)
281   {}
282   int check(THD *thd);
283   int update(THD *thd);
284 };
285 
286 
287 /* For SET NAMES and SET CHARACTER SET */
288 
289 class set_var_collation_client: public set_var_base
290 {
291   CHARSET_INFO *character_set_client;
292   CHARSET_INFO *character_set_results;
293   CHARSET_INFO *collation_connection;
294 public:
set_var_collation_client(CHARSET_INFO * client_coll_arg,CHARSET_INFO * connection_coll_arg,CHARSET_INFO * result_coll_arg)295   set_var_collation_client(CHARSET_INFO *client_coll_arg,
296                            CHARSET_INFO *connection_coll_arg,
297                            CHARSET_INFO *result_coll_arg)
298     :character_set_client(client_coll_arg),
299      character_set_results(result_coll_arg),
300      collation_connection(connection_coll_arg)
301   {}
302   int check(THD *thd);
303   int update(THD *thd);
304 };
305 
306 
307 /* optional things, have_* variables */
308 extern SHOW_COMP_OPTION have_csv, have_innodb;
309 extern SHOW_COMP_OPTION have_ndbcluster, have_partitioning;
310 extern SHOW_COMP_OPTION have_profiling;
311 
312 extern SHOW_COMP_OPTION have_ssl, have_symlink, have_dlopen;
313 extern SHOW_COMP_OPTION have_query_cache;
314 extern SHOW_COMP_OPTION have_geometry, have_rtree_keys;
315 extern SHOW_COMP_OPTION have_crypt;
316 extern SHOW_COMP_OPTION have_compress;
317 
318 /*
319   Prototypes for helper functions
320 */
321 
322 SHOW_VAR* enumerate_sys_vars(THD *thd, bool sorted, enum enum_var_type type);
323 
324 sys_var *find_sys_var(THD *thd, const char *str, uint length=0);
325 int sql_set_variables(THD *thd, List<set_var_base> *var_list);
326 
327 bool fix_delay_key_write(sys_var *self, THD *thd, enum_var_type type);
328 
329 ulong expand_sql_mode(ulonglong sql_mode);
330 bool sql_mode_string_representation(THD *thd, ulong sql_mode, LEX_STRING *ls);
331 
332 extern sys_var *Sys_autocommit_ptr;
333 
334 CHARSET_INFO *get_old_charset_by_name(const char *old_name);
335 
336 int sys_var_init();
337 int sys_var_add_options(DYNAMIC_ARRAY *long_options, int parse_flags);
338 void sys_var_end(void);
339 
340 #endif
341 
342