00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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);
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;
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;
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
00077
00078
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:
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
00145 if (this == item)
00146 return 1;
00147
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 }