drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
extract.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/temporal.h>
23#include <drizzled/error.h>
24#include <drizzled/session.h>
25#include <drizzled/calendar.h>
26#include <drizzled/function/time/extract.h>
27
28namespace drizzled
29{
30
31/*
32 'interval_names' reflects the order of the enumeration interval_type.
33 See item/time.h
34 */
35
36extern const char *interval_names[];
37
39{
40 str->append(STRING_WITH_LEN("extract("));
41 str->append(interval_names[int_type], strlen(interval_names[int_type]));
42 str->append(STRING_WITH_LEN(" from "));
43 args[0]->print(str);
44 str->append(')');
45}
46
47void Item_extract::fix_length_and_dec()
48{
49 value.alloc(DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING);
50
51 maybe_null= 1; /* If NULL supplied only */
52 switch (int_type) {
53 case INTERVAL_YEAR: max_length=4; date_value=1; break;
54 case INTERVAL_YEAR_MONTH: max_length=6; date_value=1; break;
55 case INTERVAL_QUARTER: max_length=2; date_value=1; break;
56 case INTERVAL_MONTH: max_length=2; date_value=1; break;
57 case INTERVAL_WEEK: max_length=2; date_value=1; break;
58 case INTERVAL_DAY: max_length=2; date_value=1; break;
59 case INTERVAL_DAY_HOUR: max_length=9; date_value=0; break;
60 case INTERVAL_DAY_MINUTE: max_length=11; date_value=0; break;
61 case INTERVAL_DAY_SECOND: max_length=13; date_value=0; break;
62 case INTERVAL_HOUR: max_length=2; date_value=0; break;
63 case INTERVAL_HOUR_MINUTE: max_length=4; date_value=0; break;
64 case INTERVAL_HOUR_SECOND: max_length=6; date_value=0; break;
65 case INTERVAL_MINUTE: max_length=2; date_value=0; break;
66 case INTERVAL_MINUTE_SECOND: max_length=4; date_value=0; break;
67 case INTERVAL_SECOND: max_length=2; date_value=0; break;
68 case INTERVAL_MICROSECOND: max_length=2; date_value=0; break;
69 case INTERVAL_DAY_MICROSECOND: max_length=20; date_value=0; break;
70 case INTERVAL_HOUR_MICROSECOND: max_length=13; date_value=0; break;
71 case INTERVAL_MINUTE_MICROSECOND: max_length=11; date_value=0; break;
72 case INTERVAL_SECOND_MICROSECOND: max_length=9; date_value=0; break;
73 case INTERVAL_LAST: assert(0); break;
74 }
75}
76
78{
79 assert(fixed);
80
81 if (args[0]->is_null())
82 {
83 /* For NULL argument, we return a NULL result */
84 null_value= true;
85 return 0;
86 }
87
88 /* We could have either a datetime or a time.. */
89 DateTime datetime_temporal;
90 Time time_temporal;
91
92 /* Abstract pointer type we'll use in the final switch */
93 Temporal *temporal;
94
95 if (date_value)
96 {
97 /* Grab the first argument as a DateTime object */
98 Item_result arg0_result_type= args[0]->result_type();
99
100 switch (arg0_result_type)
101 {
102 case DECIMAL_RESULT:
103 /*
104 * For doubles supplied, interpret the arg as a string,
105 * so intentionally fall-through here...
106 * This allows us to accept double parameters like
107 * 19971231235959.01 and interpret it the way MySQL does:
108 * as a TIMESTAMP-like thing with a microsecond component.
109 * Ugh, but need to keep backwards-compat.
110 */
111 case STRING_RESULT:
112 {
113 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
114 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
115 String *res= args[0]->val_str(&tmp);
116
117 if (res && (res != &tmp))
118 {
119 tmp.copy(*res);
120 }
121
122 if (! datetime_temporal.from_string(tmp.c_ptr(), tmp.length()))
123 {
124 /*
125 * Could not interpret the function argument as a temporal value,
126 * so throw an error and return 0
127 */
128 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
129 return 0;
130 }
131 }
132 break;
133 case INT_RESULT:
134 if (datetime_temporal.from_int64_t(args[0]->val_int()))
135 break;
136 /* Intentionally fall-through on invalid conversion from integer */
137 /* fallthrough */
138 default:
139 {
140 /*
141 * Could not interpret the function argument as a temporal value,
142 * so throw an error and return 0
143 */
144 null_value= true;
145 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
146 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
147 String *res;
148
149 res= args[0]->val_str(&tmp);
150
151 if (res && (res != &tmp))
152 {
153 tmp.copy(*res);
154 }
155
156 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
157 return 0;
158 }
159 }
160 /*
161 * If we got here, we have a successfully converted DateTime temporal.
162 * Point our working temporal to this.
163 */
164 temporal= &datetime_temporal;
165 }
166 else
167 {
168 /*
169 * Because of the ridiculous way in which MySQL handles
170 * TIME values (it does implicit integer -> string conversions
171 * but only for DATETIME, not TIME values) we must first
172 * try a conversion into a TIME from a string. If this
173 * fails, we fall back on a DATETIME conversion. This is
174 * necessary because of the fact that DateTime::from_string()
175 * looks first for DATETIME, then DATE regex matches. 6 consecutive
176 * numbers, say 231130, will match the DATE regex YYMMDD
177 * with no TIME part, but MySQL actually implicitly treats
178 * parameters to SECOND(), HOUR(), and MINUTE() as TIME-only
179 * values and matches 231130 as HHmmSS!
180 *
181 * Oh, and Brian Aker MADE me do this. :) --JRP
182 */
183
184 char time_buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
185 String tmp_time(time_buff,sizeof(time_buff), &my_charset_utf8_bin);
186 String *time_res= args[0]->val_str(&tmp_time);
187
188 if (time_res && (time_res != &tmp_time))
189 {
190 tmp_time.copy(*time_res);
191 }
192
193 if (! time_temporal.from_string(tmp_time.c_ptr(), tmp_time.length()))
194 {
195 /*
196 * OK, we failed to match the first argument as a string
197 * representing a time value, so we grab the first argument
198 * as a DateTime object and try that for a match...
199 */
200 Item_result arg0_result_type= args[0]->result_type();
201
202 switch (arg0_result_type)
203 {
204 case DECIMAL_RESULT:
205 /*
206 * For doubles supplied, interpret the arg as a string,
207 * so intentionally fall-through here...
208 * This allows us to accept double parameters like
209 * 19971231235959.01 and interpret it the way MySQL does:
210 * as a TIMESTAMP-like thing with a microsecond component.
211 * Ugh, but need to keep backwards-compat.
212 */
213 case STRING_RESULT:
214 {
215 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
216 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
217 String *res= args[0]->val_str(&tmp);
218
219 if (res && (res != &tmp))
220 {
221 tmp.copy(*res);
222 }
223
224 if (! datetime_temporal.from_string(tmp.c_ptr(), tmp.length()))
225 {
226 /*
227 * Could not interpret the function argument as a temporal value,
228 * so throw an error and return 0
229 */
230 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), tmp.c_ptr());
231 return 0;
232 }
233 }
234 break;
235 case INT_RESULT:
236 if (datetime_temporal.from_int64_t(args[0]->val_int()))
237 break;
238 /* Intentionally fall-through on invalid conversion from integer */
239 /* fallthrough */
240 default:
241 {
242 /*
243 * Could not interpret the function argument as a temporal value,
244 * so throw an error and return 0
245 */
246 null_value= true;
247 char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
248 String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
249 String *res;
250
251 res= args[0]->val_str(&tmp);
252
253 my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
254 return 0;
255 }
256 }
257 /* If we're here, our time failed, but our datetime succeeded... */
258 temporal= &datetime_temporal;
259 }
260 else
261 {
262 /* If we're here, our time succeeded... */
263 temporal= &time_temporal;
264 }
265 }
266
267 /* Return the requested datetime component */
268 switch (int_type) {
269 case INTERVAL_YEAR:
270 return (int64_t) temporal->years();
271 case INTERVAL_YEAR_MONTH:
272 return (int64_t) ((temporal->years() * 100L) + temporal->months());
273 case INTERVAL_QUARTER:
274 return (int64_t) (temporal->months() + 2) / 3;
275 case INTERVAL_MONTH:
276 return (int64_t) temporal->months();
277 case INTERVAL_WEEK:
279 , temporal->months()
280 , temporal->days());
281 case INTERVAL_DAY:
282 return (int64_t) temporal->days();
283 case INTERVAL_DAY_HOUR:
284 return (int64_t) ((temporal->days() * 100L) + temporal->hours());
285 case INTERVAL_DAY_MINUTE:
286 return (int64_t) ((temporal->days() * 10000L)
287 + (temporal->hours() * 100L)
288 + temporal->minutes());
289 case INTERVAL_DAY_SECOND:
290 return (int64_t) (
291 (int64_t) (temporal->days() * 1000000L)
292 + (int64_t) (temporal->hours() * 10000L)
293 + (temporal->minutes() * 100L)
294 + temporal->seconds());
295 case INTERVAL_HOUR:
296 return (int64_t) temporal->hours();
297 case INTERVAL_HOUR_MINUTE:
298 return (int64_t) (temporal->hours() * 100L)
299 + temporal->minutes();
300 case INTERVAL_HOUR_SECOND:
301 return (int64_t) (temporal->hours() * 10000L)
302 + (temporal->minutes() * 100L)
303 + temporal->seconds();
304 case INTERVAL_MINUTE:
305 return (int64_t) temporal->minutes();
306 case INTERVAL_MINUTE_SECOND:
307 return (int64_t) (temporal->minutes() * 100L) + temporal->seconds();
308 case INTERVAL_SECOND:
309 return (int64_t) temporal->seconds();
310 case INTERVAL_MICROSECOND:
311 return (int64_t) temporal->useconds();
312 case INTERVAL_DAY_MICROSECOND:
313 return (int64_t)
314 (
315 (
316 (int64_t) (temporal->days() * 1000000L)
317 + (int64_t) (temporal->hours() * 10000L)
318 + (temporal->minutes() * 100L)
319 + temporal->seconds()
320 )
321 * 1000000L
322 )
323 + temporal->useconds();
324 case INTERVAL_HOUR_MICROSECOND:
325 return (int64_t)
326 (
327 (
328 (int64_t) (temporal->hours() * 10000L)
329 + (temporal->minutes() * 100L)
330 + temporal->seconds()
331 )
332 * 1000000L
333 )
334 + temporal->useconds();
335 case INTERVAL_MINUTE_MICROSECOND:
336 return (int64_t)
337 (
338 (
339 (temporal->minutes() * 100L)
340 + temporal->seconds()
341 )
342 * 1000000L
343 )
344 + temporal->useconds();
345 case INTERVAL_SECOND_MICROSECOND:
346 return (int64_t) (temporal->seconds() * 1000000L)
347 + temporal->useconds();
348 case INTERVAL_LAST:
349 default:
350 assert(0);
351 return 0;
352 }
353}
354
355bool Item_extract::eq(const Item *item, bool binary_cmp) const
356{
357 if (this == item)
358 return 1;
359 if (item->type() != FUNC_ITEM ||
360 functype() != ((Item_func*)item)->functype())
361 return 0;
362
363 Item_extract* ie= (Item_extract*)item;
364 if (ie->int_type != int_type)
365 return 0;
366
367 if (!args[0]->eq(ie->args[0], binary_cmp))
368 return 0;
369 return 1;
370}
371
372} /* 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
bool eq(const Item *item, bool binary_cmp) const
Definition extract.cc:355
virtual void print(String *str)
Definition extract.cc:38
bool fixed
Definition item.h:122
bool null_value
Definition item.h:124
bool maybe_null
Definition item.h:123
uint32_t useconds() const
Definition temporal.h:116
uint32_t seconds() const
Definition temporal.h:130
uint32_t months() const
Definition temporal.h:146
uint32_t hours() const
Definition temporal.h:138
uint32_t minutes() const
Definition temporal.h:134
uint32_t years() const
Definition temporal.h:150
uint32_t days() const
Definition temporal.h:142
bool from_string(const char *from, size_t from_len)
Definition temporal.cc:940
TODO: Rename this file - func.h is stupid.
uint32_t iso_week_number_from_gregorian_date(uint32_t year, uint32_t month, uint32_t day)
Definition calendar.cc:429