Drizzled Public API Documentation

get_system_var.cc

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include <drizzled/error.h>
00023 #include <drizzled/function/get_system_var.h>
00024 #include <drizzled/session.h>
00025 #include <drizzled/sys_var.h>
00026 #include <drizzled/sql_lex.h>
00027 
00028 namespace drizzled {
00029 
00030 Item_func_get_system_var::
00031 Item_func_get_system_var(sys_var *var_arg, sql_var_t var_type_arg,
00032                        LEX_STRING *component_arg, const char *name_arg,
00033                        size_t name_len_arg)
00034   :var(var_arg), var_type(var_type_arg), component(*component_arg)
00035 {
00036   /* set_name() will allocate the name */
00037   set_name(name_arg, name_len_arg, system_charset_info);
00038 }
00039 
00040 
00041 bool
00042 Item_func_get_system_var::fix_fields(Session *session, Item **ref)
00043 {
00044   Item *item;
00045 
00046   /*
00047     Evaluate the system variable and substitute the result (a basic constant)
00048     instead of this item. If the variable can not be evaluated,
00049     the error is reported in sys_var::item().
00050   */
00051   if (!(item= var->item(session, var_type, &component)))
00052     return(1);                             // Impossible
00053 
00054   item->set_name(name, 0, system_charset_info); // don't allocate a new name
00055   *ref= item;
00056 
00057   return(0);
00058 }
00059 
00060 Item *get_system_var(Session *session, sql_var_t var_type, LEX_STRING name,
00061                      LEX_STRING component)
00062 {
00063   sys_var *var;
00064   LEX_STRING *base_name, *component_name;
00065 
00066   if (component.str)
00067   {
00068     base_name= &component;
00069     component_name= &name;
00070   }
00071   else
00072   {
00073     base_name= &name;
00074     component_name= &component;                 // Empty string
00075   }
00076 
00077   if (!(var= find_sys_var(base_name->str)))
00078     return 0;
00079   if (component.str)
00080   {
00081     my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->str);
00082     return 0;
00083   }
00084   session->lex().setCacheable(false);
00085 
00086   set_if_smaller(component_name->length, (size_t)MAX_SYS_VAR_LENGTH);
00087 
00088   return new Item_func_get_system_var(var, var_type, component_name,
00089                                       NULL, 0);
00090 }
00091 
00092 
00093 } /* namespace drizzled */