1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
20
#include <drizzled/server_includes.h>
22
#include <drizzled/function/time/week_mode.h>
23
#include <drizzled/function/time/extract.h>
24
#include <drizzled/session.h>
27
'interval_names' reflects the order of the enumeration interval_type.
31
extern const char *interval_names[];
33
static const char *interval_names[]=
35
"year", "quarter", "month", "week", "day",
36
"hour", "minute", "second", "microsecond",
37
"year_month", "day_hour", "day_minute",
38
"day_second", "hour_minute", "hour_second",
39
"minute_second", "day_microsecond",
40
"hour_microsecond", "minute_microsecond",
45
void Item_extract::print(String *str, enum_query_type query_type)
47
str->append(STRING_WITH_LEN("extract("));
48
str->append(interval_names[int_type]);
49
str->append(STRING_WITH_LEN(" from "));
50
args[0]->print(str, query_type);
54
void Item_extract::fix_length_and_dec()
56
value.alloc(32); // alloc buffer
58
maybe_null=1; // If wrong date
60
case INTERVAL_YEAR: max_length=4; date_value=1; break;
61
case INTERVAL_YEAR_MONTH: max_length=6; date_value=1; break;
62
case INTERVAL_QUARTER: max_length=2; date_value=1; break;
63
case INTERVAL_MONTH: max_length=2; date_value=1; break;
64
case INTERVAL_WEEK: max_length=2; date_value=1; break;
65
case INTERVAL_DAY: max_length=2; date_value=1; break;
66
case INTERVAL_DAY_HOUR: max_length=9; date_value=0; break;
67
case INTERVAL_DAY_MINUTE: max_length=11; date_value=0; break;
68
case INTERVAL_DAY_SECOND: max_length=13; date_value=0; break;
69
case INTERVAL_HOUR: max_length=2; date_value=0; break;
70
case INTERVAL_HOUR_MINUTE: max_length=4; date_value=0; break;
71
case INTERVAL_HOUR_SECOND: max_length=6; date_value=0; break;
72
case INTERVAL_MINUTE: max_length=2; date_value=0; break;
73
case INTERVAL_MINUTE_SECOND: max_length=4; date_value=0; break;
74
case INTERVAL_SECOND: max_length=2; date_value=0; break;
75
case INTERVAL_MICROSECOND: max_length=2; date_value=0; break;
76
case INTERVAL_DAY_MICROSECOND: max_length=20; date_value=0; break;
77
case INTERVAL_HOUR_MICROSECOND: max_length=13; date_value=0; break;
78
case INTERVAL_MINUTE_MICROSECOND: max_length=11; date_value=0; break;
79
case INTERVAL_SECOND_MICROSECOND: max_length=9; date_value=0; break;
80
case INTERVAL_LAST: assert(0); break; /* purecov: deadcode */
85
int64_t Item_extract::val_int()
94
if (get_arg0_date(<ime, TIME_FUZZY_DATE))
100
String *res= args[0]->val_str(&value);
101
if (!res || str_to_time_with_warn(res->ptr(), res->length(), <ime))
106
neg= ltime.neg ? -1 : 1;
110
case INTERVAL_YEAR: return ltime.year;
111
case INTERVAL_YEAR_MONTH: return ltime.year*100L+ltime.month;
112
case INTERVAL_QUARTER: return (ltime.month+2)/3;
113
case INTERVAL_MONTH: return ltime.month;
116
week_format= current_session->variables.default_week_format;
117
return calc_week(<ime, week_mode(week_format), &year);
119
case INTERVAL_DAY: return ltime.day;
120
case INTERVAL_DAY_HOUR: return (long) (ltime.day*100L+ltime.hour)*neg;
121
case INTERVAL_DAY_MINUTE: return (long) (ltime.day*10000L+
124
case INTERVAL_DAY_SECOND: return ((int64_t) ltime.day*1000000L+
125
(int64_t) (ltime.hour*10000L+
128
case INTERVAL_HOUR: return (long) ltime.hour*neg;
129
case INTERVAL_HOUR_MINUTE: return (long) (ltime.hour*100+ltime.minute)*neg;
130
case INTERVAL_HOUR_SECOND: return (long) (ltime.hour*10000+ltime.minute*100+
132
case INTERVAL_MINUTE: return (long) ltime.minute*neg;
133
case INTERVAL_MINUTE_SECOND: return (long) (ltime.minute*100+ltime.second)*neg;
134
case INTERVAL_SECOND: return (long) ltime.second*neg;
135
case INTERVAL_MICROSECOND: return (long) ltime.second_part*neg;
136
case INTERVAL_DAY_MICROSECOND: return (((int64_t)ltime.day*1000000L +
137
(int64_t)ltime.hour*10000L +
139
ltime.second)*1000000L +
140
ltime.second_part)*neg;
141
case INTERVAL_HOUR_MICROSECOND: return (((int64_t)ltime.hour*10000L +
143
ltime.second)*1000000L +
144
ltime.second_part)*neg;
145
case INTERVAL_MINUTE_MICROSECOND: return (((int64_t)(ltime.minute*100+
146
ltime.second))*1000000L+
147
ltime.second_part)*neg;
148
case INTERVAL_SECOND_MICROSECOND: return ((int64_t)ltime.second*1000000L+
149
ltime.second_part)*neg;
150
case INTERVAL_LAST: assert(0); break; /* purecov: deadcode */
152
return 0; // Impossible
155
bool Item_extract::eq(const Item *item, bool binary_cmp) const
159
if (item->type() != FUNC_ITEM ||
160
functype() != ((Item_func*)item)->functype())
163
Item_extract* ie= (Item_extract*)item;
164
if (ie->int_type != int_type)
167
if (!args[0]->eq(ie->args[0], binary_cmp))