Drizzled Public API Documentation

get_user_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 <float.h>
23 
24 #include <drizzled/function/get_user_var.h>
25 #include <drizzled/item/null.h>
26 #include <drizzled/sql_parse.h>
27 #include <drizzled/session.h>
28 #include <drizzled/user_var_entry.h>
29 
30 namespace drizzled
31 {
32 
34 {
35  assert(fixed == 1);
36  if (!var_entry)
37  return((String*) 0); // No such variable
38  return(var_entry->val_str(&null_value, str, decimals));
39 }
40 
41 
43 {
44  assert(fixed == 1);
45  if (!var_entry)
46  return 0.0; // No such variable
47  return (var_entry->val_real(&null_value));
48 }
49 
50 
52 {
53  assert(fixed == 1);
54  if (!var_entry)
55  return 0;
56  return var_entry->val_decimal(&null_value, dec);
57 }
58 
60 {
61  assert(fixed == 1);
62  if (!var_entry)
63  return 0L; // No such variable
64  return (var_entry->val_int(&null_value));
65 }
66 
67 void Item_func_get_user_var::fix_length_and_dec()
68 {
69  maybe_null=1;
70  decimals=NOT_FIXED_DEC;
71  max_length=MAX_BLOB_WIDTH;
72 
73  var_entry= session.getVariable(name, false);
74 
75  /*
76  If the variable didn't exist it has been created as a STRING-type.
77  */
78  if (var_entry)
79  {
80  m_cached_result_type= var_entry->type;
81  unsigned_flag= var_entry->unsigned_flag;
82  max_length= var_entry->length;
83 
84  collation.set(var_entry->collation);
85  switch(m_cached_result_type)
86  {
87  case REAL_RESULT:
88  max_length= DBL_DIG + 8;
89  break;
90 
91  case INT_RESULT:
92  max_length= MAX_BIGINT_WIDTH;
93  decimals=0;
94  break;
95  case STRING_RESULT:
96  max_length= MAX_BLOB_WIDTH;
97  break;
98 
99  case DECIMAL_RESULT:
100  max_length= DECIMAL_MAX_STR_LENGTH;
101  decimals= DECIMAL_MAX_SCALE;
102  break;
103 
104  case ROW_RESULT: // Keep compiler happy
105  assert(0);
106  break;
107  }
108  }
109  else
110  {
111  collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
112  null_value= 1;
113  m_cached_result_type= STRING_RESULT;
114  max_length= MAX_BLOB_WIDTH;
115  }
116 }
117 
118 
120 {
121  return (!var_entry || session.getQueryId() != var_entry->update_query_id);
122 }
123 
124 
125 enum Item_result Item_func_get_user_var::result_type() const
126 {
127  return m_cached_result_type;
128 }
129 
130 
132 {
133  str->append(STRING_WITH_LEN("(@"));
134  str->append(name);
135  str->append(')');
136 }
137 
138 
140  bool ) const
141 {
142  /* Assume we don't have rtti */
143  if (this == item)
144  return 1; // Same item is same.
145  /* Check if other type is also a get_user_var() object */
146  if (item->type() != FUNC_ITEM ||
147  ((Item_func*) item)->functype() != functype())
148  return 0;
150  return (name.size() == other->name.size() &&
151  !memcmp(name.data(), other->name.data(), name.size()));
152 }
153 
154 } /* namespace drizzled */
double val_real(bool *null_value) const
virtual void print(String *str)
bool fixed
Definition: item.h:120
bool null_value
Definition: item.h:122
bool maybe_null
Definition: item.h:121
query_id_t getQueryId() const
Definition: session.h:625
String * val_str(bool *null_value, String *, uint32_t decimals) const
String * val_str(String *str)
Definition: get_user_var.cc:33
type::Decimal * val_decimal(bool *null_value, type::Decimal *result) const
type::Decimal * val_decimal(type::Decimal *)
Definition: get_user_var.cc:51
int64_t val_int(bool *null_value) const
bool eq(const Item *item, bool binary_cmp) const