~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>
26
#include <drizzled/charset_info.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:
55
    assert(1);				// Impossible
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:
90
    assert(1);				// Impossible
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
{
103
  if ((*null_value= (value == 0)))
104
    return (String*) 0;
105
106
  switch (type) {
107
  case REAL_RESULT:
108
    str->set_real(*(double*) value, decimals, &my_charset_bin);
109
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
110
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
111
  case INT_RESULT:
112
    if (!unsigned_flag)
113
      str->set(*(int64_t*) value, &my_charset_bin);
114
    else
115
      str->set(*(uint64_t*) value, &my_charset_bin);
116
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
117
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
118
  case DECIMAL_RESULT:
2137.1.8 by Brian Aker
Remove error type from str convert since we always want an error.
119
    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
120
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
121
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
122
  case STRING_RESULT:
123
    if (str->copy(value, length, collation.collation))
124
      str= 0;					// EOM error
2008 by Brian Aker
Formatting + remove default from switch/case.
125
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
126
  case ROW_RESULT:
127
    assert(1);				// Impossible
128
    break;
129
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
130
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
131
  return(str);
132
}
133
134
/** Get the value of a variable as a decimal. */
135
2030.1.4 by Brian Aker
Change my_decimal to Decimal
136
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
137
{
138
  if ((*null_value= (value == 0)))
139
    return 0;
140
141
  switch (type) {
142
  case REAL_RESULT:
2030.1.3 by Brian Aker
Second pass through function names.
143
    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
144
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
145
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
146
  case INT_RESULT:
2030.1.3 by Brian Aker
Second pass through function names.
147
    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
148
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
149
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
150
  case DECIMAL_RESULT:
2030.1.4 by Brian Aker
Change my_decimal to Decimal
151
    val= (type::Decimal *)value;
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
152
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
153
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
154
  case STRING_RESULT:
2034.2.5 by Brian Aker
Improvement for decimal in encapsulation.
155
    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
156
    break;
2008 by Brian Aker
Formatting + remove default from switch/case.
157
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
158
  case ROW_RESULT:
159
    assert(1);				// Impossible
160
    break;
161
  }
2008 by Brian Aker
Formatting + remove default from switch/case.
162
629.6.7 by Lee
final clean up of item/func functions moving them to the functions directory
163
  return(val);
164
}
1089.1.5 by Brian Aker
Cleanup of user_var
165
166
/**
167
  Set value to user variable.
168
169
  @param entry          pointer to structure representing variable
170
  @param set_null       should we set NULL value ?
171
  @param ptr            pointer to buffer with new value
172
  @param length         length of new value
173
  @param type           type of new value
174
  @param cs             charset info for new value
175
  @param dv             derivation for new value
176
  @param unsigned_arg   indiates if a value of type INT_RESULT is unsigned
177
178
  @note Sets error and fatal error if allocation fails.
179
180
  @retval
181
    false   success
182
  @retval
183
    true    failure
184
*/
185
2246.3.4 by Olaf van der Spek
Propogate return void
186
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
187
                                 Item_result arg_type, const charset_info_st * const cs, Derivation dv,
1089.1.5 by Brian Aker
Cleanup of user_var
188
                                 bool unsigned_arg)
189
{
190
  if (set_null)
191
  {
192
    if (value)
193
    {
194
      assert(length && size);
195
      free(value);
196
      value= NULL;
197
      length= 0;
198
      size= 0;
199
    }
200
  }
201
  else
202
  {
203
    size_t needed_size= arg_length + ((arg_type == STRING_RESULT) ? 1 : 0);
204
205
    if (needed_size > size)
206
    {
2246.3.4 by Olaf van der Spek
Propogate return void
207
			value= (char *)realloc(value, needed_size);
1089.1.5 by Brian Aker
Cleanup of user_var
208
      size= needed_size;
209
    }
210
211
    if (arg_type == STRING_RESULT)
212
      value[arg_length]= 0;			// Store end \0
213
214
    memcpy(value, ptr, arg_length);
215
    if (arg_type == DECIMAL_RESULT)
2030.1.4 by Brian Aker
Change my_decimal to Decimal
216
      ((type::Decimal*)value)->fix_buffer_pointer();
1089.1.5 by Brian Aker
Cleanup of user_var
217
    length= arg_length;
218
    collation.set(cs, dv);
219
    unsigned_flag= unsigned_arg;
220
  }
221
  type= arg_type;
222
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
223
224
} /* namespace drizzled */