~ubuntu-branches/ubuntu/saucy/drizzle/saucy-proposed

« back to all changes in this revision

Viewing changes to drizzled/function/get_user_var.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

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
namespace drizzled
 
30
{
 
31
 
 
32
String *Item_func_get_user_var::val_str(String *str)
 
33
{
 
34
  assert(fixed == 1);
 
35
  if (!var_entry)
 
36
    return((String*) 0);                        // No such variable
 
37
  return(var_entry->val_str(&null_value, str, decimals));
 
38
}
 
39
 
 
40
 
 
41
double Item_func_get_user_var::val_real()
 
42
{
 
43
  assert(fixed == 1);
 
44
  if (!var_entry)
 
45
    return 0.0;                                 // No such variable
 
46
  return (var_entry->val_real(&null_value));
 
47
}
 
48
 
 
49
 
 
50
my_decimal *Item_func_get_user_var::val_decimal(my_decimal *dec)
 
51
{
 
52
  assert(fixed == 1);
 
53
  if (!var_entry)
 
54
    return 0;
 
55
  return var_entry->val_decimal(&null_value, dec);
 
56
}
 
57
 
 
58
int64_t Item_func_get_user_var::val_int()
 
59
{
 
60
  assert(fixed == 1);
 
61
  if (!var_entry)
 
62
    return 0L;                          // No such variable
 
63
  return (var_entry->val_int(&null_value));
 
64
}
 
65
 
 
66
void Item_func_get_user_var::fix_length_and_dec()
 
67
{
 
68
  Session *session=current_session;
 
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
    'var_entry' is NULL only if there occured an error during the call to
 
78
    get_var_with_binlog.
 
79
  */
 
80
  if (var_entry)
 
81
  {
 
82
    m_cached_result_type= var_entry->type;
 
83
    unsigned_flag= var_entry->unsigned_flag;
 
84
    max_length= var_entry->length;
 
85
 
 
86
    collation.set(var_entry->collation);
 
87
    switch(m_cached_result_type) 
 
88
    {
 
89
    case REAL_RESULT:
 
90
      max_length= DBL_DIG + 8;
 
91
      break;
 
92
    case INT_RESULT:
 
93
      max_length= MAX_BIGINT_WIDTH;
 
94
      decimals=0;
 
95
      break;
 
96
    case STRING_RESULT:
 
97
      max_length= MAX_BLOB_WIDTH;
 
98
      break;
 
99
    case DECIMAL_RESULT:
 
100
      max_length= DECIMAL_MAX_STR_LENGTH;
 
101
      decimals= DECIMAL_MAX_SCALE;
 
102
      break;
 
103
    case ROW_RESULT:                            // Keep compiler happy
 
104
    default:
 
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
 
 
119
bool Item_func_get_user_var::const_item() const
 
120
{
 
121
  return (!var_entry || current_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
 
 
131
void Item_func_get_user_var::print(String *str,
 
132
                                   enum_query_type )
 
133
{
 
134
  str->append(STRING_WITH_LEN("(@"));
 
135
  str->append(name.str,name.length);
 
136
  str->append(')');
 
137
}
 
138
 
 
139
 
 
140
bool Item_func_get_user_var::eq(const Item *item,
 
141
                                bool ) const
 
142
{
 
143
  /* Assume we don't have rtti */
 
144
  if (this == item)
 
145
    return 1;                                   // Same item is same.
 
146
  /* Check if other type is also a get_user_var() object */
 
147
  if (item->type() != FUNC_ITEM ||
 
148
      ((Item_func*) item)->functype() != functype())
 
149
    return 0;
 
150
  Item_func_get_user_var *other=(Item_func_get_user_var*) item;
 
151
  return (name.length == other->name.length &&
 
152
          !memcmp(name.str, other->name.str, name.length));
 
153
}
 
154
 
 
155
} /* namespace drizzled */