~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/user_var_entry.cc

  • Committer: Brian Aker
  • Date: 2009-09-26 04:00:11 UTC
  • mfrom: (1126.12.1 trunk-nodebug)
  • Revision ID: brian@gaz-20090926040011-2qzxdcbpm1ibpkhl
Merge Lee

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 <drizzled/session.h>
 
22
#include CSTDINT_H
 
23
 
 
24
/** Get the value of a variable as a double. */
 
25
 
 
26
double user_var_entry::val_real(bool *null_value)
 
27
{
 
28
  if ((*null_value= (value == 0)))
 
29
    return 0.0;
 
30
 
 
31
  switch (type) {
 
32
  case REAL_RESULT:
 
33
    return *(double*) value;
 
34
  case INT_RESULT:
 
35
    return (double) *(int64_t*) value;
 
36
  case DECIMAL_RESULT:
 
37
  {
 
38
    double result;
 
39
    my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
 
40
    return result;
 
41
  }
 
42
  case STRING_RESULT:
 
43
    return my_atof(value);                      // This is null terminated
 
44
  case ROW_RESULT:
 
45
    assert(1);                          // Impossible
 
46
    break;
 
47
  }
 
48
  return 0.0;                                   // Impossible
 
49
}
 
50
 
 
51
 
 
52
/** Get the value of a variable as an integer. */
 
53
 
 
54
int64_t user_var_entry::val_int(bool *null_value) const
 
55
{
 
56
  if ((*null_value= (value == 0)))
 
57
    return 0L;
 
58
 
 
59
  switch (type) {
 
60
  case REAL_RESULT:
 
61
    return (int64_t) *(double*) value;
 
62
  case INT_RESULT:
 
63
    return *(int64_t*) value;
 
64
  case DECIMAL_RESULT:
 
65
  {
 
66
    int64_t result;
 
67
    my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
 
68
    return result;
 
69
  }
 
70
  case STRING_RESULT:
 
71
  {
 
72
    int error;
 
73
    return my_strtoll10(value, (char**) 0, &error);// String is null terminated
 
74
  }
 
75
  case ROW_RESULT:
 
76
    assert(1);                          // Impossible
 
77
    break;
 
78
  }
 
79
  return 0L;                                    // Impossible
 
80
}
 
81
 
 
82
 
 
83
/** Get the value of a variable as a string. */
 
84
 
 
85
String *user_var_entry::val_str(bool *null_value, String *str,
 
86
                                uint32_t decimals)
 
87
{
 
88
  if ((*null_value= (value == 0)))
 
89
    return (String*) 0;
 
90
 
 
91
  switch (type) {
 
92
  case REAL_RESULT:
 
93
    str->set_real(*(double*) value, decimals, &my_charset_bin);
 
94
    break;
 
95
  case INT_RESULT:
 
96
    if (!unsigned_flag)
 
97
      str->set(*(int64_t*) value, &my_charset_bin);
 
98
    else
 
99
      str->set(*(uint64_t*) value, &my_charset_bin);
 
100
    break;
 
101
  case DECIMAL_RESULT:
 
102
    my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
 
103
    break;
 
104
  case STRING_RESULT:
 
105
    if (str->copy(value, length, collation.collation))
 
106
      str= 0;                                   // EOM error
 
107
  case ROW_RESULT:
 
108
    assert(1);                          // Impossible
 
109
    break;
 
110
  }
 
111
  return(str);
 
112
}
 
113
 
 
114
/** Get the value of a variable as a decimal. */
 
115
 
 
116
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
 
117
{
 
118
  if ((*null_value= (value == 0)))
 
119
    return 0;
 
120
 
 
121
  switch (type) {
 
122
  case REAL_RESULT:
 
123
    double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
 
124
    break;
 
125
  case INT_RESULT:
 
126
    int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
 
127
    break;
 
128
  case DECIMAL_RESULT:
 
129
    val= (my_decimal *)value;
 
130
    break;
 
131
  case STRING_RESULT:
 
132
    str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
 
133
    break;
 
134
  case ROW_RESULT:
 
135
    assert(1);                          // Impossible
 
136
    break;
 
137
  }
 
138
  return(val);
 
139
}
 
140
 
 
141
/**
 
142
  Set value to user variable.
 
143
 
 
144
  @param entry          pointer to structure representing variable
 
145
  @param set_null       should we set NULL value ?
 
146
  @param ptr            pointer to buffer with new value
 
147
  @param length         length of new value
 
148
  @param type           type of new value
 
149
  @param cs             charset info for new value
 
150
  @param dv             derivation for new value
 
151
  @param unsigned_arg   indiates if a value of type INT_RESULT is unsigned
 
152
 
 
153
  @note Sets error and fatal error if allocation fails.
 
154
 
 
155
  @retval
 
156
    false   success
 
157
  @retval
 
158
    true    failure
 
159
*/
 
160
 
 
161
#define extra_size sizeof(double)
 
162
 
 
163
bool user_var_entry::update_hash(bool set_null, void *ptr, uint32_t arg_length,
 
164
                                 Item_result arg_type, const CHARSET_INFO * const cs, Derivation dv,
 
165
                                 bool unsigned_arg)
 
166
{
 
167
  if (set_null)
 
168
  {
 
169
    if (value)
 
170
    {
 
171
      assert(length && size);
 
172
      free(value);
 
173
      value= NULL;
 
174
      length= 0;
 
175
      size= 0;
 
176
    }
 
177
  }
 
178
  else
 
179
  {
 
180
    size_t needed_size= arg_length + ((arg_type == STRING_RESULT) ? 1 : 0);
 
181
 
 
182
    if (needed_size > size)
 
183
    {
 
184
      char *new_ptr;
 
185
 
 
186
      new_ptr= (char *)realloc(value, needed_size);
 
187
 
 
188
      if (new_ptr == NULL)
 
189
        return true;
 
190
 
 
191
      value= new_ptr;
 
192
      size= needed_size;
 
193
    }
 
194
 
 
195
    if (arg_type == STRING_RESULT)
 
196
      value[arg_length]= 0;                     // Store end \0
 
197
 
 
198
    memcpy(value, ptr, arg_length);
 
199
    if (arg_type == DECIMAL_RESULT)
 
200
      ((my_decimal*)value)->fix_buffer_pointer();
 
201
    length= arg_length;
 
202
    collation.set(cs, dv);
 
203
    unsigned_flag= unsigned_arg;
 
204
  }
 
205
  type= arg_type;
 
206
 
 
207
  return false;
 
208
}