17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include "drizzled/function/time/to_days.h"
23
#include "drizzled/error.h"
24
#include "drizzled/temporal.h"
30
* We intepret the first argument as a DateTime and then convert
31
* it to a Julian Day Number and return it.
20
#include <drizzled/server_includes.h>
22
#include <drizzled/functions/time/to_days.h>
33
24
int64_t Item_func_to_days::val_int()
37
/* We return NULL from FROM_DAYS() only when supplied a NULL argument */
38
if (args[0]->null_value)
45
* We attempt to convert the first argument into a
46
* temporal value. If the conversion is successful,
47
* we know that a conversion to a Julian Day Number
48
* is always possible. Upon successful conversion,
49
* we return the Julian Day Number. If no conversion
50
* was possible into a temporal value, we throw an
51
* error and return 0, setting the null_value flag to true.
53
/* Grab the first argument as a DateTime object */
55
Item_result arg0_result_type= args[0]->result_type();
28
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
30
return (int64_t) calc_daynr(ltime.year,ltime.month,ltime.day);
35
Get information about this Item tree monotonicity
57
switch (arg0_result_type)
38
Item_func_to_days::get_monotonicity_info()
41
Get information about monotonicity of the function represented by this item
45
See enum_monotonicity_info.
48
enum_monotonicity_info Item_func_to_days::get_monotonicity_info() const
50
if (args[0]->type() == Item::FIELD_ITEM)
62
* For doubles supplied, interpret the arg as a string,
63
* so intentionally fall-through here...
64
* This allows us to accept double parameters like
65
* 19971231235959.01 and interpret it the way MySQL does:
66
* as a TIMESTAMP-like thing with a microsecond component.
67
* Ugh, but need to keep backwards-compat.
71
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
72
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
73
String *res= args[0]->val_str(&tmp);
78
* Likely a nested function issue where the nested
79
* function had bad input. We rely on the nested
80
* function my_error() and simply return false here.
85
if (! temporal.from_string(res->c_ptr(), res->length()))
88
* Could not interpret the function argument as a temporal value,
89
* so throw an error and return 0
91
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
97
if (temporal.from_int64_t(args[0]->val_int()))
99
/* Intentionally fall-through on invalid conversion from integer */
103
* Could not interpret the function argument as a temporal value,
104
* so throw an error and return 0
107
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
108
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
111
res= args[0]->val_str(&tmp);
116
* Likely a nested function issue where the nested
117
* function had bad input. We rely on the nested
118
* function my_error() and simply return false here.
123
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
52
if (args[0]->field_type() == DRIZZLE_TYPE_NEWDATE)
53
return MONOTONIC_STRICT_INCREASING;
54
if (args[0]->field_type() == DRIZZLE_TYPE_DATETIME)
55
return MONOTONIC_INCREASING;
127
int64_t julian_day_number;
128
temporal.to_julian_day_number(&julian_day_number);
129
return julian_day_number;
132
60
int64_t Item_func_to_days::val_int_endpoint(bool left_endp, bool *incl_endp)
137
* We attempt to convert the first argument into a
138
* temporal value. If the conversion is successful,
139
* we know that a conversion to a Julian Day Number
140
* is always possible. Depending on whether the
141
* first argument is a Date, or a DateTime with no
142
* time-portion, we return the Julian Day Number or
143
* the appropriate end-point integer.
145
/* Grab the first argument as a DateTime object */
147
Item_result arg0_result_type= args[0]->result_type();
149
switch (arg0_result_type)
154
* For doubles supplied, interpret the arg as a string,
155
* so intentionally fall-through here...
156
* This allows us to accept double parameters like
157
* 19971231235959.01 and interpret it the way MySQL does:
158
* as a TIMESTAMP-like thing with a microsecond component.
159
* Ugh, but need to keep backwards-compat.
163
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
164
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
165
String *res= args[0]->val_str(&tmp);
170
* Likely a nested function issue where the nested
171
* function had bad input. We rely on the nested
172
* function my_error() and simply return false here.
177
if (! temporal.from_string(res->c_ptr(), res->length()))
180
* Could not interpret the function argument as a temporal value,
181
* so throw an error and return 0
183
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
189
if (temporal.from_int64_t(args[0]->val_int()))
191
/* Intentionally fall-through on invalid conversion from integer */
195
* Could not interpret the function argument as a temporal value,
196
* so throw an error and return 0
199
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
200
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
203
res= args[0]->val_str(&tmp);
208
* Likely a nested function issue where the nested
209
* function had bad input. We rely on the nested
210
* function my_error() and simply return false here.
215
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
220
if (null_value == true)
65
if (get_arg0_date(<ime, TIME_NO_ZERO_DATE))
222
67
/* got NULL, leave the incl_endp intact */
226
int64_t julian_day_number;
227
temporal.to_julian_day_number(&julian_day_number);
229
if (args[0]->field_type() == DRIZZLE_TYPE_DATE)
70
res=(int64_t) calc_daynr(ltime.year,ltime.month,ltime.day);
72
if (args[0]->field_type() == DRIZZLE_TYPE_NEWDATE)
231
/* TO_DAYS() is strictly monotonic for dates, leave incl_endp intact */
232
return julian_day_number;
74
// TO_DAYS() is strictly monotonic for dates, leave incl_endp intact