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::Item_func_get_system_var(sys_var *var_arg, sql_var_t var_type_arg,
00031                        str_ref component_arg, const char *name_arg, size_t name_len_arg)
00032   : var(var_arg), var_type(var_type_arg), component(component_arg)
00033 {
00034   /* set_name() will allocate the name */
00035   set_name(name_arg, name_len_arg);
00036 }
00037 
00038 
00039 bool Item_func_get_system_var::fix_fields(Session *session, Item **ref)
00040 {
00041 
00042   /*
00043     Evaluate the system variable and substitute the result (a basic constant)
00044     instead of this item. If the variable can not be evaluated,
00045     the error is reported in sys_var::item().
00046   */
00047   Item *item= var->item(session, var_type);
00048   if (not item)
00049     return 1;                             // Impossible
00050 
00051   item->set_name(name, 0); // don't allocate a new name
00052   *ref= item;
00053 
00054   return 0;
00055 }
00056 
00057 Item *get_system_var(Session *session, sql_var_t var_type, str_ref name, str_ref component)
00058 {
00059   str_ref *base_name, *component_name;
00060 
00061   if (component.empty())
00062   {
00063     base_name= &name;
00064     component_name= &component;                 // Empty string
00065   }
00066   else
00067   {
00068     base_name= &component;
00069     component_name= &name;
00070   }
00071 
00072   sys_var *var= find_sys_var(*base_name);
00073   if (not var)
00074     return NULL;
00075   if (not component.empty())
00076   {
00077     my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->data());
00078     return NULL;
00079   }
00080   session->lex().setCacheable(false);
00081 
00082   if (component_name->size() > MAX_SYS_VAR_LENGTH)
00083     component_name->assign(component_name->data(), MAX_SYS_VAR_LENGTH);
00084 
00085   return new Item_func_get_system_var(var, var_type, *component_name, NULL, 0);
00086 }
00087 
00088 
00089 } /* namespace drizzled */