~drizzle-trunk/drizzle/development

629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
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
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
2154.2.24 by Brian Aker
Merge in all changes for current_session, etc.
21
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
22
#include <drizzled/session.h>
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
23
#include <drizzled/internal/m_string.h>
2154.2.24 by Brian Aker
Merge in all changes for current_session, etc.
24
#include <drizzled/user_var_entry.h>
2241.2.14 by Olaf van der Spek
Refactor
25
#include <drizzled/type/decimal.h>
2281.5.1 by Muhammad Umair
Merged charset declarations of global_charset_info.h and charset_info.h into charset.h header file.
26
#include <drizzled/charset.h>
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
27
2241.2.14 by Olaf van der Spek
Refactor
28
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
29
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
30
/** Get the value of a variable as a double. */
31
32
double user_var_entry::val_real(bool *null_value)
33
{
34
  if ((*null_value= (value == 0)))
35
    return 0.0;
36
37
  switch (type) {
38
  case REAL_RESULT:
39
    return *(double*) value;
2008 by Brian Aker
Formatting + remove default from switch/case.
40
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
41
  case INT_RESULT:
42
    return (double) *(int64_t*) value;
2008 by Brian Aker
Formatting + remove default from switch/case.
43
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
44
  case DECIMAL_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
45
    {
46
      double result;
2030.1.4 by Brian Aker
Change my_decimal to Decimal
47
      class_decimal2double(E_DEC_FATAL_ERROR, (type::Decimal *)value, &result);
2008 by Brian Aker
Formatting + remove default from switch/case.
48
      return result;
49
    }
50
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
51
  case STRING_RESULT:
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
52
    return internal::my_atof(value);                      // This is null terminated
2008 by Brian Aker
Formatting + remove default from switch/case.
53
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
54
  case ROW_RESULT:
2318.7.22 by Olaf van der Spek
Use assert(false) instead of assert(1)
55
    assert(false);				// Impossible
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
56
    break;
57
  }
58
  return 0.0;					// Impossible
59
}
60
61
62
/** Get the value of a variable as an integer. */
63
64
int64_t user_var_entry::val_int(bool *null_value) const
65
{
66
  if ((*null_value= (value == 0)))
67
    return 0L;
68
69
  switch (type) {
70
  case REAL_RESULT:
71
    return (int64_t) *(double*) value;
2008 by Brian Aker
Formatting + remove default from switch/case.
72
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
73
  case INT_RESULT:
74
    return *(int64_t*) value;
2008 by Brian Aker
Formatting + remove default from switch/case.
75
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
76
  case DECIMAL_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
77
    {
78
      int64_t result;
2034.2.1 by Brian Aker
Move class_decimal2int to a method on Decimal.
79
      ((type::Decimal *)(value))->val_int32(E_DEC_FATAL_ERROR, 0, &result);
2008 by Brian Aker
Formatting + remove default from switch/case.
80
      return result;
81
    }
82
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
83
  case STRING_RESULT:
2008 by Brian Aker
Formatting + remove default from switch/case.
84
    {
85
      int error;
86
      return internal::my_strtoll10(value, (char**) 0, &error);// String is null terminated
87
    }
88
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
89
  case ROW_RESULT:
2318.7.22 by Olaf van der Spek
Use assert(false) instead of assert(1)
90
    assert(false);				// Impossible
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
91
    break;
92
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
93
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
94
  return 0L;					// Impossible
95
}
96
97
98
/** Get the value of a variable as a string. */
99
100
String *user_var_entry::val_str(bool *null_value, String *str,
1927.2.1 by Brian Aker
Merge up the tree.
101
                                uint32_t decimals)
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
102
{
2318.7.22 by Olaf van der Spek
Use assert(false) instead of assert(1)
103
  if ((*null_value= not value))
104
    return NULL;
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
105
2318.7.22 by Olaf van der Spek
Use assert(false) instead of assert(1)
106
  switch (type) 
107
  {
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
108
  case REAL_RESULT:
109
    str->set_real(*(double*) value, decimals, &my_charset_bin);
110
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
111
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
112
  case INT_RESULT:
113
    if (!unsigned_flag)
114
      str->set(*(int64_t*) value, &my_charset_bin);
115
    else
116
      str->set(*(uint64_t*) value, &my_charset_bin);
117
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
118
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
119
  case DECIMAL_RESULT:
2137.1.8 by Brian Aker
Remove error type from str convert since we always want an error.
120
    class_decimal2string((type::Decimal *)value, 0, str);
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
121
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
122
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
123
  case STRING_RESULT:
2281.4.6 by Olaf van der Spek
Return void
124
    str->copy(value, length, collation.collation);
2318.7.22 by Olaf van der Spek
Use assert(false) instead of assert(1)
125
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
126
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
127
  case ROW_RESULT:
2318.7.22 by Olaf van der Spek
Use assert(false) instead of assert(1)
128
    assert(false);				// Impossible
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
129
    break;
130
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
131
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
132
  return(str);
133
}
134
135
/** Get the value of a variable as a decimal. */
136
2030.1.4 by Brian Aker
Change my_decimal to Decimal
137
type::Decimal *user_var_entry::val_decimal(bool *null_value, type::Decimal *val)
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
138
{
139
  if ((*null_value= (value == 0)))
140
    return 0;
141
142
  switch (type) {
143
  case REAL_RESULT:
2030.1.3 by Brian Aker
Second pass through function names.
144
    double2_class_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
145
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
146
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
147
  case INT_RESULT:
2030.1.3 by Brian Aker
Second pass through function names.
148
    int2_class_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
149
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
150
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
151
  case DECIMAL_RESULT:
2030.1.4 by Brian Aker
Change my_decimal to Decimal
152
    val= (type::Decimal *)value;
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
153
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
154
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
155
  case STRING_RESULT:
2034.2.5 by Brian Aker
Improvement for decimal in encapsulation.
156
    val->store(E_DEC_FATAL_ERROR, value, length, collation.collation);
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
157
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
158
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
159
  case ROW_RESULT:
2318.7.22 by Olaf van der Spek
Use assert(false) instead of assert(1)
160
    assert(false);				// Impossible
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
161
    break;
162
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
163
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
164
  return(val);
165
}
1089.1.5 by Brian Aker
Cleanup of user_var
166
167
/**
168
  Set value to user variable.
169
170
  @param entry          pointer to structure representing variable
171
  @param set_null       should we set NULL value ?
172
  @param ptr            pointer to buffer with new value
173
  @param length         length of new value
174
  @param type           type of new value
175
  @param cs             charset info for new value
176
  @param dv             derivation for new value
177
  @param unsigned_arg   indiates if a value of type INT_RESULT is unsigned
178
179
  @note Sets error and fatal error if allocation fails.
180
181
  @retval
182
    false   success
183
  @retval
184
    true    failure
185
*/
186
2246.3.4 by Olaf van der Spek
Propogate return void
187
void user_var_entry::update_hash(bool set_null, void *ptr, uint32_t arg_length,
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
188
                                 Item_result arg_type, const charset_info_st * const cs, Derivation dv,
1089.1.5 by Brian Aker
Cleanup of user_var
189
                                 bool unsigned_arg)
190
{
191
  if (set_null)
192
  {
193
    if (value)
194
    {
195
      assert(length && size);
196
      free(value);
197
      value= NULL;
198
      length= 0;
199
      size= 0;
200
    }
201
  }
202
  else
203
  {
204
    size_t needed_size= arg_length + ((arg_type == STRING_RESULT) ? 1 : 0);
205
206
    if (needed_size > size)
207
    {
2246.3.4 by Olaf van der Spek
Propogate return void
208
			value= (char *)realloc(value, needed_size);
1089.1.5 by Brian Aker
Cleanup of user_var
209
      size= needed_size;
210
    }
211
212
    if (arg_type == STRING_RESULT)
213
      value[arg_length]= 0;			// Store end \0
214
215
    memcpy(value, ptr, arg_length);
216
    if (arg_type == DECIMAL_RESULT)
2030.1.4 by Brian Aker
Change my_decimal to Decimal
217
      ((type::Decimal*)value)->fix_buffer_pointer();
1089.1.5 by Brian Aker
Cleanup of user_var
218
    length= arg_length;
219
    collation.set(cs, dv);
220
    unsigned_flag= unsigned_arg;
221
  }
222
  type= arg_type;
223
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
224
225
} /* namespace drizzled */