~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/get_user_var.cc

Merge Monty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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
 
 
29
String *
 
30
Item_func_get_user_var::val_str(String *str)
 
31
{
 
32
  assert(fixed == 1);
 
33
  if (!var_entry)
 
34
    return((String*) 0);                        // No such variable
 
35
  return(var_entry->val_str(&null_value, str, decimals));
 
36
}
 
37
 
 
38
 
 
39
double Item_func_get_user_var::val_real()
 
40
{
 
41
  assert(fixed == 1);
 
42
  if (!var_entry)
 
43
    return 0.0;                                 // No such variable
 
44
  return (var_entry->val_real(&null_value));
 
45
}
 
46
 
 
47
 
 
48
my_decimal *Item_func_get_user_var::val_decimal(my_decimal *dec)
 
49
{
 
50
  assert(fixed == 1);
 
51
  if (!var_entry)
 
52
    return 0;
 
53
  return var_entry->val_decimal(&null_value, dec);
 
54
}
 
55
 
 
56
int64_t Item_func_get_user_var::val_int()
 
57
{
 
58
  assert(fixed == 1);
 
59
  if (!var_entry)
 
60
    return 0L;                          // No such variable
 
61
  return (var_entry->val_int(&null_value));
 
62
}
 
63
 
 
64
void Item_func_get_user_var::fix_length_and_dec()
 
65
{
 
66
  Session *session=current_session;
 
67
  maybe_null=1;
 
68
  decimals=NOT_FIXED_DEC;
 
69
  max_length=MAX_BLOB_WIDTH;
 
70
 
 
71
  var_entry= session->getVariable(name, false);
 
72
 
 
73
  /*
 
74
    If the variable didn't exist it has been created as a STRING-type.
 
75
    'var_entry' is NULL only if there occured an error during the call to
 
76
    get_var_with_binlog.
 
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
    case INT_RESULT:
 
91
      max_length= MAX_BIGINT_WIDTH;
 
92
      decimals=0;
 
93
      break;
 
94
    case STRING_RESULT:
 
95
      max_length= MAX_BLOB_WIDTH;
 
96
      break;
 
97
    case DECIMAL_RESULT:
 
98
      max_length= DECIMAL_MAX_STR_LENGTH;
 
99
      decimals= DECIMAL_MAX_SCALE;
 
100
      break;
 
101
    case ROW_RESULT:                            // Keep compiler happy
 
102
    default:
 
103
      assert(0);
 
104
      break;
 
105
    }
 
106
  }
 
107
  else
 
108
  {
 
109
    collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
 
110
    null_value= 1;
 
111
    m_cached_result_type= STRING_RESULT;
 
112
    max_length= MAX_BLOB_WIDTH;
 
113
  }
 
114
}
 
115
 
 
116
 
 
117
bool Item_func_get_user_var::const_item() const
 
118
{
 
119
  return (!var_entry || current_session->query_id != var_entry->update_query_id);
 
120
}
 
121
 
 
122
 
 
123
enum Item_result Item_func_get_user_var::result_type() const
 
124
{
 
125
  return m_cached_result_type;
 
126
}
 
127
 
 
128
 
 
129
void Item_func_get_user_var::print(String *str,
 
130
                                   enum_query_type )
 
131
{
 
132
  str->append(STRING_WITH_LEN("(@"));
 
133
  str->append(name.str,name.length);
 
134
  str->append(')');
 
135
}
 
136
 
 
137
 
 
138
bool Item_func_get_user_var::eq(const Item *item,
 
139
                                bool ) const
 
140
{
 
141
  /* Assume we don't have rtti */
 
142
  if (this == item)
 
143
    return 1;                                   // Same item is same.
 
144
  /* Check if other type is also a get_user_var() object */
 
145
  if (item->type() != FUNC_ITEM ||
 
146
      ((Item_func*) item)->functype() != functype())
 
147
    return 0;
 
148
  Item_func_get_user_var *other=(Item_func_get_user_var*) item;
 
149
  return (name.length == other->name.length &&
 
150
          !memcmp(name.str, other->name.str, name.length));
 
151
}