~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/get_user_var.cc

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

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