Drizzled Public API Documentation

get_user_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 <float.h>
00023 
00024 #include <drizzled/function/get_user_var.h>
00025 #include <drizzled/item/null.h>
00026 #include <drizzled/sql_parse.h>
00027 #include <drizzled/session.h>
00028 #include <drizzled/user_var_entry.h>
00029 
00030 namespace drizzled
00031 {
00032 
00033 String *Item_func_get_user_var::val_str(String *str)
00034 {
00035   assert(fixed == 1);
00036   if (!var_entry)
00037     return((String*) 0);                        // No such variable
00038   return(var_entry->val_str(&null_value, str, decimals));
00039 }
00040 
00041 
00042 double Item_func_get_user_var::val_real()
00043 {
00044   assert(fixed == 1);
00045   if (!var_entry)
00046     return 0.0;                                 // No such variable
00047   return (var_entry->val_real(&null_value));
00048 }
00049 
00050 
00051 type::Decimal *Item_func_get_user_var::val_decimal(type::Decimal *dec)
00052 {
00053   assert(fixed == 1);
00054   if (!var_entry)
00055     return 0;
00056   return var_entry->val_decimal(&null_value, dec);
00057 }
00058 
00059 int64_t Item_func_get_user_var::val_int()
00060 {
00061   assert(fixed == 1);
00062   if (!var_entry)
00063     return 0L;                          // No such variable
00064   return (var_entry->val_int(&null_value));
00065 }
00066 
00067 void Item_func_get_user_var::fix_length_and_dec()
00068 {
00069   maybe_null=1;
00070   decimals=NOT_FIXED_DEC;
00071   max_length=MAX_BLOB_WIDTH;
00072 
00073   var_entry= session.getVariable(name, false);
00074 
00075   /*
00076     If the variable didn't exist it has been created as a STRING-type.
00077     'var_entry' is NULL only if there occured an error during the call to
00078     get_var_with_binlog.
00079   */
00080   if (var_entry)
00081   {
00082     m_cached_result_type= var_entry->type;
00083     unsigned_flag= var_entry->unsigned_flag;
00084     max_length= var_entry->length;
00085 
00086     collation.set(var_entry->collation);
00087     switch(m_cached_result_type) 
00088     {
00089     case REAL_RESULT:
00090       max_length= DBL_DIG + 8;
00091       break;
00092 
00093     case INT_RESULT:
00094       max_length= MAX_BIGINT_WIDTH;
00095       decimals=0;
00096       break;
00097     case STRING_RESULT:
00098       max_length= MAX_BLOB_WIDTH;
00099       break;
00100 
00101     case DECIMAL_RESULT:
00102       max_length= DECIMAL_MAX_STR_LENGTH;
00103       decimals= DECIMAL_MAX_SCALE;
00104       break;
00105 
00106     case ROW_RESULT:                            // Keep compiler happy
00107       assert(0);
00108       break;
00109     }
00110   }
00111   else
00112   {
00113     collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
00114     null_value= 1;
00115     m_cached_result_type= STRING_RESULT;
00116     max_length= MAX_BLOB_WIDTH;
00117   }
00118 }
00119 
00120 
00121 bool Item_func_get_user_var::const_item() const
00122 {
00123   return (!var_entry || session.getQueryId() != var_entry->update_query_id);
00124 }
00125 
00126 
00127 enum Item_result Item_func_get_user_var::result_type() const
00128 {
00129   return m_cached_result_type;
00130 }
00131 
00132 
00133 void Item_func_get_user_var::print(String *str)
00134 {
00135   str->append(STRING_WITH_LEN("(@"));
00136   str->append(name.str,name.length);
00137   str->append(')');
00138 }
00139 
00140 
00141 bool Item_func_get_user_var::eq(const Item *item,
00142                                 bool ) const
00143 {
00144   /* Assume we don't have rtti */
00145   if (this == item)
00146     return 1;         // Same item is same.
00147   /* Check if other type is also a get_user_var() object */
00148   if (item->type() != FUNC_ITEM ||
00149       ((Item_func*) item)->functype() != functype())
00150     return 0;
00151   Item_func_get_user_var *other=(Item_func_get_user_var*) item;
00152   return (name.length == other->name.length &&
00153     !memcmp(name.str, other->name.str, name.length));
00154 }
00155 
00156 } /* namespace drizzled */