~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/user_var_entry.cc

Renamed more stuff to drizzle.

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