~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/user_var_entry.cc

  • Committer: Mark Atwood
  • Date: 2008-10-03 01:39:40 UTC
  • mto: This revision was merged to the branch mainline in revision 437.
  • Revision ID: mark@fallenpegasus.com-20081003013940-mvefjo725dltz41h
rename logging_noop to logging_query

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