85
74
int64_t Item_extract::val_int()
78
if (args[0]->is_null())
80
/* For NULL argument, we return a NULL result */
85
/* We could have either a datetime or a time.. */
86
drizzled::DateTime datetime_temporal;
87
drizzled::Time time_temporal;
89
/* Abstract pointer type we'll use in the final switch */
90
drizzled::Temporal *temporal;
94
if (get_arg0_date(<ime, TIME_FUZZY_DATE))
94
/* Grab the first argument as a DateTime object */
95
Item_result arg0_result_type= args[0]->result_type();
97
switch (arg0_result_type)
101
* For doubles supplied, interpret the arg as a string,
102
* so intentionally fall-through here...
103
* This allows us to accept double parameters like
104
* 19971231235959.01 and interpret it the way MySQL does:
105
* as a TIMESTAMP-like thing with a microsecond component.
106
* Ugh, but need to keep backwards-compat.
110
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
111
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
112
String *res= args[0]->val_str(&tmp);
113
if (! datetime_temporal.from_string(res->c_ptr(), res->length()))
116
* Could not interpret the function argument as a temporal value,
117
* so throw an error and return 0
119
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
125
if (datetime_temporal.from_int64_t(args[0]->val_int()))
127
/* Intentionally fall-through on invalid conversion from integer */
131
* Could not interpret the function argument as a temporal value,
132
* so throw an error and return 0
135
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
136
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
139
res= args[0]->val_str(&tmp);
141
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
146
* If we got here, we have a successfully converted DateTime temporal.
147
* Point our working temporal to this.
149
temporal= &datetime_temporal;
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;
154
* Because of the ridiculous way in which MySQL handles
155
* TIME values (it does implicit integer -> string conversions
156
* but only for DATETIME, not TIME values) we must first
157
* try a conversion into a TIME from a string. If this
158
* fails, we fall back on a DATETIME conversion. This is
159
* necessary because of the fact that DateTime::from_string()
160
* looks first for DATETIME, then DATE regex matches. 6 consecutive
161
* numbers, say 231130, will match the DATE regex YYMMDD
162
* with no TIME part, but MySQL actually implicitly treats
163
* parameters to SECOND(), HOUR(), and MINUTE() as TIME-only
164
* values and matches 231130 as HHmmSS!
166
* Oh, and Brian Aker MADE me do this. :) --JRP
169
char time_buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
170
String tmp_time(time_buff,sizeof(time_buff), &my_charset_utf8_bin);
171
String *time_res= args[0]->val_str(&tmp_time);
172
if (! time_temporal.from_string(time_res->c_ptr(), time_res->length()))
175
* OK, we failed to match the first argument as a string
176
* representing a time value, so we grab the first argument
177
* as a DateTime object and try that for a match...
179
Item_result arg0_result_type= args[0]->result_type();
181
switch (arg0_result_type)
185
* For doubles supplied, interpret the arg as a string,
186
* so intentionally fall-through here...
187
* This allows us to accept double parameters like
188
* 19971231235959.01 and interpret it the way MySQL does:
189
* as a TIMESTAMP-like thing with a microsecond component.
190
* Ugh, but need to keep backwards-compat.
194
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
195
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
196
String *res= args[0]->val_str(&tmp);
197
if (! datetime_temporal.from_string(res->c_ptr(), res->length()))
200
* Could not interpret the function argument as a temporal value,
201
* so throw an error and return 0
203
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
209
if (datetime_temporal.from_int64_t(args[0]->val_int()))
211
/* Intentionally fall-through on invalid conversion from integer */
215
* Could not interpret the function argument as a temporal value,
216
* so throw an error and return 0
219
char buff[DRIZZLE_MAX_LENGTH_DATETIME_AS_STRING];
220
String tmp(buff,sizeof(buff), &my_charset_utf8_bin);
223
res= args[0]->val_str(&tmp);
225
my_error(ER_INVALID_DATETIME_VALUE, MYF(0), res->c_ptr());
229
/* If we're here, our time failed, but our datetime succeeded... */
230
temporal= &datetime_temporal;
234
/* If we're here, our time succeeded... */
235
temporal= &time_temporal;
239
/* Return the requested datetime component */
109
240
switch (int_type) {
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
242
return (int64_t) temporal->years();
243
case INTERVAL_YEAR_MONTH:
244
return (int64_t) ((temporal->years() * 100L) + temporal->months());
245
case INTERVAL_QUARTER:
246
return (int64_t) (temporal->months() + 2) / 3;
248
return (int64_t) temporal->months();
250
return iso_week_number_from_gregorian_date(temporal->years()
253
, NULL); /* NULL is year_out parameter, which is not needed */
255
return (int64_t) temporal->days();
256
case INTERVAL_DAY_HOUR:
257
return (int64_t) ((temporal->days() * 100L) + temporal->hours());
258
case INTERVAL_DAY_MINUTE:
259
return (int64_t) ((temporal->days() * 10000L)
260
+ (temporal->hours() * 100L)
261
+ temporal->minutes());
262
case INTERVAL_DAY_SECOND:
264
(int64_t) (temporal->days() * 1000000L)
265
+ (int64_t) (temporal->hours() * 10000L)
266
+ (temporal->minutes() * 100L)
267
+ temporal->seconds());
269
return (int64_t) temporal->hours();
270
case INTERVAL_HOUR_MINUTE:
271
return (int64_t) (temporal->hours() * 100L)
272
+ temporal->minutes();
273
case INTERVAL_HOUR_SECOND:
274
return (int64_t) (temporal->hours() * 10000L)
275
+ (temporal->minutes() * 100L)
276
+ temporal->seconds();
277
case INTERVAL_MINUTE:
278
return (int64_t) temporal->minutes();
279
case INTERVAL_MINUTE_SECOND:
280
return (int64_t) (temporal->minutes() * 100L) + temporal->seconds();
281
case INTERVAL_SECOND:
282
return (int64_t) temporal->seconds();
283
case INTERVAL_MICROSECOND:
284
return (int64_t) temporal->useconds();
285
case INTERVAL_DAY_MICROSECOND:
289
(int64_t) (temporal->days() * 1000000L)
290
+ (int64_t) (temporal->hours() * 10000L)
291
+ (temporal->minutes() * 100L)
292
+ temporal->seconds()
296
+ temporal->useconds();
297
case INTERVAL_HOUR_MICROSECOND:
301
(int64_t) (temporal->hours() * 10000L)
302
+ (temporal->minutes() * 100L)
303
+ temporal->seconds()
307
+ temporal->useconds();
308
case INTERVAL_MINUTE_MICROSECOND:
312
(temporal->minutes() * 100L)
313
+ temporal->seconds()
317
+ temporal->useconds();
318
case INTERVAL_SECOND_MICROSECOND:
319
return (int64_t) (temporal->seconds() * 1000000L)
320
+ temporal->useconds();
324
return 0; /* purecov: deadcode */
155
328
bool Item_extract::eq(const Item *item, bool binary_cmp) const