~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/user_var_entry.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

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