Drizzled Public API Documentation

get_system_var.cc
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008 Sun Microsystems, Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <config.h>
21 
22 #include <drizzled/error.h>
23 #include <drizzled/function/get_system_var.h>
24 #include <drizzled/session.h>
25 #include <drizzled/sys_var.h>
26 #include <drizzled/sql_lex.h>
27 
28 namespace drizzled {
29 
30 Item_func_get_system_var::Item_func_get_system_var(sys_var *var_arg, sql_var_t var_type_arg,
31  str_ref component_arg, const char *name_arg, size_t name_len_arg)
32  : var(var_arg), var_type(var_type_arg), component(component_arg)
33 {
34  /* set_name() will allocate the name */
35  set_name(name_arg, name_len_arg);
36 }
37 
38 
39 bool Item_func_get_system_var::fix_fields(Session *session, Item **ref)
40 {
41 
42  /*
43  Evaluate the system variable and substitute the result (a basic constant)
44  instead of this item. If the variable can not be evaluated,
45  the error is reported in sys_var::item().
46  */
47  Item *item= var->item(session, var_type);
48  if (not item)
49  return 1; // Impossible
50 
51  item->set_name(name, 0); // don't allocate a new name
52  *ref= item;
53 
54  return 0;
55 }
56 
57 Item *get_system_var(Session *session, sql_var_t var_type, str_ref name, str_ref component)
58 {
59  str_ref *base_name, *component_name;
60 
61  if (component.empty())
62  {
63  base_name= &name;
64  component_name= &component; // Empty string
65  }
66  else
67  {
68  base_name= &component;
69  component_name= &name;
70  }
71 
72  sys_var *var= find_sys_var(*base_name);
73  if (not var)
74  return NULL;
75  if (not component.empty())
76  {
77  my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->data());
78  return NULL;
79  }
80  session->lex().setCacheable(false);
81 
82  if (component_name->size() > MAX_SYS_VAR_LENGTH)
83  component_name->assign(component_name->data(), MAX_SYS_VAR_LENGTH);
84 
85  return new Item_func_get_system_var(var, var_type, *component_name, NULL, 0);
86 }
87 
88 
89 } /* namespace drizzled */
const char * name
Definition: item.h:110
sys_var * find_sys_var(const std::string &name)
Definition: sys_var.cc:1411
Item * item(Session *, sql_var_t)
Definition: sys_var.cc:798