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 |
*
|
|
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 <drizzled/server_includes.h> |
|
21 |
#include <drizzled/session.h> |
|
22 |
#include CSTDINT_H
|
|
23 |
||
24 |
/** Get the value of a variable as a double. */
|
|
25 |
||
26 |
double user_var_entry::val_real(bool *null_value) |
|
27 |
{
|
|
28 |
if ((*null_value= (value == 0))) |
|
29 |
return 0.0; |
|
30 |
||
31 |
switch (type) { |
|
32 |
case REAL_RESULT: |
|
33 |
return *(double*) value; |
|
34 |
case INT_RESULT: |
|
35 |
return (double) *(int64_t*) value; |
|
36 |
case DECIMAL_RESULT: |
|
37 |
{
|
|
38 |
double result; |
|
39 |
my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result); |
|
40 |
return result; |
|
41 |
}
|
|
42 |
case STRING_RESULT: |
|
43 |
return my_atof(value); // This is null terminated |
|
44 |
case ROW_RESULT: |
|
45 |
assert(1); // Impossible |
|
46 |
break; |
|
47 |
}
|
|
48 |
return 0.0; // Impossible |
|
49 |
}
|
|
50 |
||
51 |
||
52 |
/** Get the value of a variable as an integer. */
|
|
53 |
||
54 |
int64_t user_var_entry::val_int(bool *null_value) const |
|
55 |
{
|
|
56 |
if ((*null_value= (value == 0))) |
|
57 |
return 0L; |
|
58 |
||
59 |
switch (type) { |
|
60 |
case REAL_RESULT: |
|
61 |
return (int64_t) *(double*) value; |
|
62 |
case INT_RESULT: |
|
63 |
return *(int64_t*) value; |
|
64 |
case DECIMAL_RESULT: |
|
65 |
{
|
|
66 |
int64_t result; |
|
67 |
my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result); |
|
68 |
return result; |
|
69 |
}
|
|
70 |
case STRING_RESULT: |
|
71 |
{
|
|
72 |
int error; |
|
73 |
return my_strtoll10(value, (char**) 0, &error);// String is null terminated |
|
74 |
}
|
|
75 |
case ROW_RESULT: |
|
76 |
assert(1); // Impossible |
|
77 |
break; |
|
78 |
}
|
|
79 |
return 0L; // Impossible |
|
80 |
}
|
|
81 |
||
82 |
||
83 |
/** Get the value of a variable as a string. */
|
|
84 |
||
85 |
String *user_var_entry::val_str(bool *null_value, String *str, |
|
86 |
uint32_t decimals) |
|
87 |
{
|
|
88 |
if ((*null_value= (value == 0))) |
|
89 |
return (String*) 0; |
|
90 |
||
91 |
switch (type) { |
|
92 |
case REAL_RESULT: |
|
93 |
str->set_real(*(double*) value, decimals, &my_charset_bin); |
|
94 |
break; |
|
95 |
case INT_RESULT: |
|
96 |
if (!unsigned_flag) |
|
97 |
str->set(*(int64_t*) value, &my_charset_bin); |
|
98 |
else
|
|
99 |
str->set(*(uint64_t*) value, &my_charset_bin); |
|
100 |
break; |
|
101 |
case DECIMAL_RESULT: |
|
102 |
my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str); |
|
103 |
break; |
|
104 |
case STRING_RESULT: |
|
105 |
if (str->copy(value, length, collation.collation)) |
|
106 |
str= 0; // EOM error |
|
107 |
case ROW_RESULT: |
|
108 |
assert(1); // Impossible |
|
109 |
break; |
|
110 |
}
|
|
111 |
return(str); |
|
112 |
}
|
|
113 |
||
114 |
/** Get the value of a variable as a decimal. */
|
|
115 |
||
116 |
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val) |
|
117 |
{
|
|
118 |
if ((*null_value= (value == 0))) |
|
119 |
return 0; |
|
120 |
||
121 |
switch (type) { |
|
122 |
case REAL_RESULT: |
|
123 |
double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val); |
|
124 |
break; |
|
125 |
case INT_RESULT: |
|
126 |
int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val); |
|
127 |
break; |
|
128 |
case DECIMAL_RESULT: |
|
129 |
val= (my_decimal *)value; |
|
130 |
break; |
|
131 |
case STRING_RESULT: |
|
132 |
str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val); |
|
133 |
break; |
|
134 |
case ROW_RESULT: |
|
135 |
assert(1); // Impossible |
|
136 |
break; |
|
137 |
}
|
|
138 |
return(val); |
|
139 |
}
|