drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
to_days.cc
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/function/time/to_days.h>
23#include <drizzled/error.h>
24#include <drizzled/temporal.h>
25
26namespace drizzled
27{
28
29/*
30 * We intepret the first argument as a DateTime and then convert
31 * it to a Julian Day Number and return it.
32 */
34{
35 assert(fixed);
36
37 /* We return NULL from FROM_DAYS() only when supplied a NULL argument */
38 if (args[0]->null_value)
39 {
40 null_value= true;
41 return false;
42 }
43
44 /*
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.
52 */
53 /* Grab the first argument as a DateTime object */
54 DateTime temporal;
55 Item_result arg0_result_type= args[0]->result_type();
56
57 switch (arg0_result_type)
58 {
59 case REAL_RESULT:
60 case DECIMAL_RESULT:
61 /*
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.
68 */
69 case STRING_RESULT:
70 {
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);
74
75 if (! res)
76 {
77 /*
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.
81 */
82 return false;
83 }
84
85 if (res != &tmp)
86 {
87 tmp.copy(*res);
88 }
89
90 if (! temporal.from_string(tmp.c_ptr(), tmp.length()))
91 {
92 /*
93 * Could not interpret the function argument as a temporal value,
94 * so throw an error and return 0
95 */
96 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
97 return 0;
98 }
99 }
100 break;
101 case INT_RESULT:
102 if (temporal.from_int64_t(args[0]->val_int()))
103 break;
104 /* Intentionally fall-through on invalid conversion from integer */
105 /* fallthrough */
106 default:
107 {
108 /*
109 * Could not interpret the function argument as a temporal value,
110 * so throw an error and return 0
111 */
112 null_value= true;
113 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
114 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
115 String *res;
116
117 res= args[0]->val_str(&tmp);
118
119 if (! res)
120 {
121 /*
122 * Likely a nested function issue where the nested
123 * function had bad input. We rely on the nested
124 * function my_error() and simply return false here.
125 */
126 return false;
127 }
128
129 if (res != &tmp)
130 {
131 tmp.copy(*res);
132 }
133
134 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
135 return 0;
136 }
137 }
138 int64_t julian_day_number;
139 temporal.to_julian_day_number(&julian_day_number);
140 return julian_day_number;
141}
142
143int64_t Item_func_to_days::val_int_endpoint(bool left_endp, bool *incl_endp)
144{
145 assert(fixed);
146
147 /*
148 * We attempt to convert the first argument into a
149 * temporal value. If the conversion is successful,
150 * we know that a conversion to a Julian Day Number
151 * is always possible. Depending on whether the
152 * first argument is a Date, or a DateTime with no
153 * time-portion, we return the Julian Day Number or
154 * the appropriate end-point integer.
155 */
156 /* Grab the first argument as a DateTime object */
157 DateTime temporal;
158 Item_result arg0_result_type= args[0]->result_type();
159
160 switch (arg0_result_type)
161 {
162 case REAL_RESULT:
163 case DECIMAL_RESULT:
164 /*
165 * For doubles supplied, interpret the arg as a string,
166 * so intentionally fall-through here...
167 * This allows us to accept double parameters like
168 * 19971231235959.01 and interpret it the way MySQL does:
169 * as a TIMESTAMP-like thing with a microsecond component.
170 * Ugh, but need to keep backwards-compat.
171 */
172 case STRING_RESULT:
173 {
174 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
175 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
176 String *res= args[0]->val_str(&tmp);
177
178 if (! res)
179 {
180 /*
181 * Likely a nested function issue where the nested
182 * function had bad input. We rely on the nested
183 * function my_error() and simply return false here.
184 */
185 return 0;
186 }
187
188 if (res != &tmp)
189 {
190 tmp.copy(*res);
191 }
192
193 if (! temporal.from_string(tmp.c_ptr(), tmp.length()))
194 {
195 /*
196 * Could not interpret the function argument as a temporal value,
197 * so throw an error and return 0
198 */
199 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
200 return 0;
201 }
202 }
203 break;
204 case INT_RESULT:
205 if (temporal.from_int64_t(args[0]->val_int()))
206 break;
207 /* Intentionally fall-through on invalid conversion from integer */
208 /* fallthrough */
209 default:
210 {
211 /*
212 * Could not interpret the function argument as a temporal value,
213 * so throw an error and return 0
214 */
215 null_value= true;
216 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
217 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
218 String *res;
219
220 res= args[0]->val_str(&tmp);
221
222 if (! res)
223 {
224 /*
225 * Likely a nested function issue where the nested
226 * function had bad input. We rely on the nested
227 * function my_error() and simply return false here.
228 */
229 return 0;
230 }
231
232 if (res != &tmp)
233 {
234 tmp.copy(*res);
235 }
236
237 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
238 return 0;
239 }
240 }
241
242 if (null_value == true)
243 {
244 /* got NULL, leave the incl_endp intact */
245 return INT64_MIN;
246 }
247
248 int64_t julian_day_number;
249 temporal.to_julian_day_number(&julian_day_number);
250
251 if (args[0]->field_type() == DRIZZLE_TYPE_DATE)
252 {
253 /* TO_DAYS() is strictly monotonic for dates, leave incl_endp intact */
254 return julian_day_number;
255 }
256
257 /*
258 Handle the special but practically useful case of datetime values that
259 point to day bound ("strictly less" comparison stays intact):
260
261 col < '2007-09-15 00:00:00' -> TO_DAYS(col) < TO_DAYS('2007-09-15')
262
263 which is different from the general case ("strictly less" changes to
264 "less or equal"):
265
266 col < '2007-09-15 12:34:56' -> TO_DAYS(col) <= TO_DAYS('2007-09-15')
267 */
268 if (!left_endp && ! (
269 temporal.hours()
270 || temporal.minutes()
271 || temporal.seconds()
272 || temporal.useconds()
273 || temporal.nseconds()
274 )
275 )
276 ; /* do nothing */
277 else
278 *incl_endp= true;
279 return julian_day_number;
280}
281
282} /* namespace drizzled */
bool from_string(const char *from, size_t from_len)
Definition temporal.cc:122
bool from_int64_t(const int64_t from, bool convert)
Definition temporal.cc:1106
void to_julian_day_number(int64_t *to) const
Definition temporal.cc:1075
int64_t val_int_endpoint(bool left_endp, bool *incl_endp)
Definition to_days.cc:143
bool fixed
Definition item.h:122
bool null_value
Definition item.h:124
uint32_t useconds() const
Definition temporal.h:116
uint32_t nseconds() const
Definition temporal.h:112
uint32_t seconds() const
Definition temporal.h:130
uint32_t hours() const
Definition temporal.h:138
uint32_t minutes() const
Definition temporal.h:134
TODO: Rename this file - func.h is stupid.