item_timefunc.h 30.8 KB
Newer Older
1
/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6

unknown's avatar
unknown committed
7 8 9 10
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
unknown's avatar
unknown committed
11

unknown's avatar
unknown committed
12 13 14 15 16 17 18
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/* Function items used by mysql */

19
#ifdef USE_PRAGMA_INTERFACE
unknown's avatar
unknown committed
20 21 22
#pragma interface			/* gcc class implementation */
#endif

23 24 25 26 27
enum date_time_format_types 
{ 
  TIME_ONLY= 0, TIME_MICROSECOND, DATE_ONLY, DATE_TIME, DATE_TIME_MICROSECOND
};

28 29 30
bool get_interval_value(Item *args,interval_type int_type,
			       String *str_value, INTERVAL *interval);

unknown's avatar
unknown committed
31 32 33 34 35 36
class Item_func_period_add :public Item_int_func
{
public:
  Item_func_period_add(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return "period_add"; }
37 38
  void fix_length_and_dec() 
  { 
39
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
40
  }
unknown's avatar
unknown committed
41 42 43 44 45 46 47 48 49
};


class Item_func_period_diff :public Item_int_func
{
public:
  Item_func_period_diff(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return "period_diff"; }
50 51 52
  void fix_length_and_dec()
  { 
    decimals=0;
53
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
54
  }
unknown's avatar
unknown committed
55 56 57 58 59 60 61 62 63
};


class Item_func_to_days :public Item_int_func
{
public:
  Item_func_to_days(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "to_days"; }
64 65 66
  void fix_length_and_dec() 
  { 
    decimals=0; 
67
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
68 69
    maybe_null=1; 
  }
unknown's avatar
unknown committed
70
  enum_monotonicity_info get_monotonicity_info() const;
71
  longlong val_int_endpoint(bool left_endp, bool *incl_endp);
72
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
73
  bool check_valid_arguments_processor(uchar *int_arg)
74 75 76
  {
    return !has_date_args();
  }
unknown's avatar
unknown committed
77 78 79 80 81 82 83 84 85
};


class Item_func_dayofmonth :public Item_int_func
{
public:
  Item_func_dayofmonth(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "dayofmonth"; }
86 87 88
  void fix_length_and_dec() 
  { 
    decimals=0; 
89
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
90 91
    maybe_null=1; 
  }
92
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
93
  bool check_valid_arguments_processor(uchar *int_arg)
94 95 96
  {
    return !has_date_args();
  }
unknown's avatar
unknown committed
97 98 99 100 101 102 103 104
};


class Item_func_month :public Item_func
{
public:
  Item_func_month(Item *a) :Item_func(a) {}
  longlong val_int();
105
  double val_real()
106
  { DBUG_ASSERT(fixed == 1); return (double) Item_func_month::val_int(); }
107 108
  String *val_str(String *str) 
  {
109 110 111 112 113
    longlong nr= val_int();
    if (null_value)
      return 0;
    str->set(nr, &my_charset_bin);
    return str;
114
  }
unknown's avatar
unknown committed
115 116
  const char *func_name() const { return "month"; }
  enum Item_result result_type () const { return INT_RESULT; }
117 118
  void fix_length_and_dec() 
  { 
119
    collation.set(&my_charset_bin);
120
    decimals=0;
121
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
122 123
    maybe_null=1; 
  }
124
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
125
  bool check_valid_arguments_processor(uchar *int_arg)
126 127 128
  {
    return !has_date_args();
  }
unknown's avatar
unknown committed
129 130
};

131

unknown's avatar
unknown committed
132 133
class Item_func_monthname :public Item_func_month
{
134
  MY_LOCALE *locale;
unknown's avatar
unknown committed
135 136 137 138 139
public:
  Item_func_monthname(Item *a) :Item_func_month(a) {}
  const char *func_name() const { return "monthname"; }
  String *val_str(String *str);
  enum Item_result result_type () const { return STRING_RESULT; }
140
  void fix_length_and_dec();
unknown's avatar
unknown committed
141
  bool check_partition_func_processor(uchar *int_arg) {return TRUE;}
unknown's avatar
unknown committed
142 143 144 145 146 147 148 149 150
};


class Item_func_dayofyear :public Item_int_func
{
public:
  Item_func_dayofyear(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "dayofyear"; }
151 152 153
  void fix_length_and_dec() 
  { 
    decimals=0;
154
    max_length=3*MY_CHARSET_BIN_MB_MAXLEN;
155 156
    maybe_null=1; 
  }
157
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
158
  bool check_valid_arguments_processor(uchar *int_arg)
159 160 161
  {
    return !has_date_args();
  }
unknown's avatar
unknown committed
162 163 164 165 166 167 168 169 170
};


class Item_func_hour :public Item_int_func
{
public:
  Item_func_hour(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "hour"; }
171 172 173
  void fix_length_and_dec()
  {
    decimals=0;
174
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
175 176
    maybe_null=1;
  }
177
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
178
  bool check_valid_arguments_processor(uchar *int_arg)
179 180 181
  {
    return !has_time_args();
  }
unknown's avatar
unknown committed
182 183 184 185 186 187 188 189 190
};


class Item_func_minute :public Item_int_func
{
public:
  Item_func_minute(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "minute"; }
191 192 193
  void fix_length_and_dec()
  {
    decimals=0;
194
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
195 196
    maybe_null=1;
  }
197
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
198
  bool check_valid_arguments_processor(uchar *int_arg)
199 200 201
  {
    return !has_time_args();
  }
unknown's avatar
unknown committed
202 203 204 205 206 207 208 209 210
};


class Item_func_quarter :public Item_int_func
{
public:
  Item_func_quarter(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "quarter"; }
211 212 213
  void fix_length_and_dec()
  { 
     decimals=0;
214
     max_length=1*MY_CHARSET_BIN_MB_MAXLEN;
215 216
     maybe_null=1;
  }
217
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
218
  bool check_valid_arguments_processor(uchar *int_arg)
219 220 221
  {
    return !has_date_args();
  }
unknown's avatar
unknown committed
222 223 224 225 226 227 228 229 230
};


class Item_func_second :public Item_int_func
{
public:
  Item_func_second(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "second"; }
231 232 233
  void fix_length_and_dec() 
  { 
    decimals=0;
234
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
235 236
    maybe_null=1;
  }
237
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
238
  bool check_valid_arguments_processor(uchar *int_arg)
239 240 241
  {
    return !has_time_args();
  }
unknown's avatar
unknown committed
242 243 244 245 246 247 248 249 250
};


class Item_func_week :public Item_int_func
{
public:
  Item_func_week(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return "week"; }
251 252 253
  void fix_length_and_dec()
  { 
    decimals=0;
254
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
255 256
    maybe_null=1;
  }
unknown's avatar
unknown committed
257 258 259 260 261 262 263 264
};

class Item_func_yearweek :public Item_int_func
{
public:
  Item_func_yearweek(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return "yearweek"; }
265 266 267
  void fix_length_and_dec()
  { 
    decimals=0;
268
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
269 270
    maybe_null=1;
  }
271
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
272
  bool check_valid_arguments_processor(uchar *int_arg)
273 274 275
  {
    return !has_date_args();
  }
unknown's avatar
unknown committed
276 277 278 279 280 281 282 283 284
};


class Item_func_year :public Item_int_func
{
public:
  Item_func_year(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "year"; }
unknown's avatar
unknown committed
285
  enum_monotonicity_info get_monotonicity_info() const;
286
  longlong val_int_endpoint(bool left_endp, bool *incl_endp);
287 288 289
  void fix_length_and_dec()
  { 
    decimals=0;
290
    max_length=4*MY_CHARSET_BIN_MB_MAXLEN;
291 292
    maybe_null=1;
  }
293
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
294
  bool check_valid_arguments_processor(uchar *int_arg)
295 296 297
  {
    return !has_date_args();
  }
unknown's avatar
unknown committed
298 299 300 301 302 303 304 305 306 307
};


class Item_func_weekday :public Item_func
{
  bool odbc_type;
public:
  Item_func_weekday(Item *a,bool type_arg)
    :Item_func(a), odbc_type(type_arg) {}
  longlong val_int();
308
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
309
  String *val_str(String *str)
310 311
  {
    DBUG_ASSERT(fixed == 1);
312
    str->set(val_int(), &my_charset_bin);
313 314
    return null_value ? 0 : str;
  }
315 316 317 318
  const char *func_name() const
  {
     return (odbc_type ? "dayofweek" : "weekday");
  }
unknown's avatar
unknown committed
319
  enum Item_result result_type () const { return INT_RESULT; }
320 321
  void fix_length_and_dec()
  {
322
    collation.set(&my_charset_bin);
323
    decimals=0;
324
    max_length=1*MY_CHARSET_BIN_MB_MAXLEN;
325 326
    maybe_null=1;
  }
327
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
328
  bool check_valid_arguments_processor(uchar *int_arg)
329 330 331
  {
    return !has_date_args();
  }
unknown's avatar
unknown committed
332 333 334 335
};

class Item_func_dayname :public Item_func_weekday
{
336
  MY_LOCALE *locale;
unknown's avatar
unknown committed
337 338 339 340 341
 public:
  Item_func_dayname(Item *a) :Item_func_weekday(a,0) {}
  const char *func_name() const { return "dayname"; }
  String *val_str(String *str);
  enum Item_result result_type () const { return STRING_RESULT; }
342
  void fix_length_and_dec();
unknown's avatar
unknown committed
343
  bool check_partition_func_processor(uchar *int_arg) {return TRUE;}
unknown's avatar
unknown committed
344 345 346 347 348 349 350 351 352 353
};


class Item_func_unix_timestamp :public Item_int_func
{
  String value;
public:
  Item_func_unix_timestamp() :Item_int_func() {}
  Item_func_unix_timestamp(Item *a) :Item_int_func(a) {}
  longlong val_int();
354
  const char *func_name() const { return "unix_timestamp"; }
355 356 357 358 359 360
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
  /*
    UNIX_TIMESTAMP() depends on the current timezone
    (and thus may not be used as a partitioning function)
    when its argument is NOT of the TIMESTAMP type.
  */
361
  bool check_valid_arguments_processor(uchar *int_arg)
362 363 364
  {
    return !has_timestamp_args();
  }
unknown's avatar
unknown committed
365 366
  void fix_length_and_dec()
  {
367
    decimals=0;
368
    max_length=10*MY_CHARSET_BIN_MB_MAXLEN;
unknown's avatar
unknown committed
369 370 371 372 373 374 375 376 377 378 379 380
  }
};


class Item_func_time_to_sec :public Item_int_func
{
public:
  Item_func_time_to_sec(Item *item) :Item_int_func(item) {}
  longlong val_int();
  const char *func_name() const { return "time_to_sec"; }
  void fix_length_and_dec()
  {
381
    maybe_null= TRUE;
382
    decimals=0;
383
    max_length=10*MY_CHARSET_BIN_MB_MAXLEN;
unknown's avatar
unknown committed
384
  }
385
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
386
  bool check_valid_arguments_processor(uchar *int_arg)
387 388 389
  {
    return !has_time_args();
  }
unknown's avatar
unknown committed
390 391 392
};


unknown's avatar
unknown committed
393 394 395
/*
  This can't be a Item_str_func, because the val_real() functions are special
*/
unknown's avatar
unknown committed
396 397 398 399 400 401 402

class Item_date :public Item_func
{
public:
  Item_date() :Item_func() {}
  Item_date(Item *a) :Item_func(a) {}
  enum Item_result result_type () const { return STRING_RESULT; }
403
  enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
unknown's avatar
unknown committed
404
  String *val_str(String *str);
405
  longlong val_int();
406
  double val_real() { return val_real_from_decimal(); }
unknown's avatar
unknown committed
407
  const char *func_name() const { return "date"; }
408 409
  void fix_length_and_dec()
  { 
410
    collation.set(&my_charset_bin);
411
    decimals=0;
412
    max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
413
  }
unknown's avatar
unknown committed
414
  Field *tmp_table_field(TABLE *table)
415
  {
unknown's avatar
unknown committed
416 417
    return tmp_table_field_from_field_type(table, 0);
  }
418
  bool result_as_longlong() { return TRUE; }
419 420 421 422 423 424 425 426 427
  my_decimal *val_decimal(my_decimal *decimal_value)
  {
    DBUG_ASSERT(fixed == 1);
    return  val_decimal_from_date(decimal_value);
  }
  int save_in_field(Field *field, bool no_conversions)
  {
    return save_date_in_field(field);
  }
428 429 430 431 432 433 434 435 436
};


class Item_date_func :public Item_str_func
{
public:
  Item_date_func() :Item_str_func() {}
  Item_date_func(Item *a) :Item_str_func(a) {}
  Item_date_func(Item *a,Item *b) :Item_str_func(a,b) {}
437
  Item_date_func(Item *a,Item *b, Item *c) :Item_str_func(a,b,c) {}
438
  enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
unknown's avatar
unknown committed
439
  Field *tmp_table_field(TABLE *table)
440
  {
unknown's avatar
unknown committed
441
    return tmp_table_field_from_field_type(table, 0);
442
  }
443
  bool result_as_longlong() { return TRUE; }
444
  double val_real() { return (double) val_int(); }
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
  my_decimal *val_decimal(my_decimal *decimal_value)
  {
    DBUG_ASSERT(fixed == 1);
    return  val_decimal_from_date(decimal_value);
  }
  int save_in_field(Field *field, bool no_conversions)
  {
    return save_date_in_field(field);
  }
};


class Item_str_timefunc :public Item_str_func
{
public:
  Item_str_timefunc() :Item_str_func() {}
  Item_str_timefunc(Item *a) :Item_str_func(a) {}
  Item_str_timefunc(Item *a,Item *b) :Item_str_func(a,b) {}
  Item_str_timefunc(Item *a, Item *b, Item *c) :Item_str_func(a, b ,c) {}
  enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
  void fix_length_and_dec()
  {
467
    decimals= DATETIME_DEC;
468 469
    max_length=MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
  }
470
  Field *tmp_table_field(TABLE *table)
471
  {
472
    return tmp_table_field_from_field_type(table, 0);
473
  }
474
  double val_real() { return val_real_from_decimal(); }
475 476 477 478 479 480 481 482 483
  my_decimal *val_decimal(my_decimal *decimal_value)
  {
    DBUG_ASSERT(fixed == 1);
    return  val_decimal_from_time(decimal_value);
  }
  int save_in_field(Field *field, bool no_conversions)
  {
    return save_time_in_field(field);
  }
484
  longlong val_int() { return val_int_from_decimal(); }
485
  bool result_as_longlong() { return TRUE; }
unknown's avatar
unknown committed
486 487 488
};


489
/* Abstract CURTIME function. Children should define what time zone is used */
490

491
class Item_func_curtime :public Item_str_timefunc
unknown's avatar
unknown committed
492 493
{
  longlong value;
494
  char buff[9*2+32];
unknown's avatar
unknown committed
495 496
  uint buff_length;
public:
497 498
  Item_func_curtime() :Item_str_timefunc() {}
  Item_func_curtime(Item *a) :Item_str_timefunc(a) {}
499
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) value; }
500
  longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
501
  String *val_str(String *str);
unknown's avatar
unknown committed
502
  void fix_length_and_dec();
503 504
  /* 
    Abstract method that defines which time zone is used for conversion.
505
    Converts time current time in my_time_t representation to broken-down
506
    MYSQL_TIME representation using UTC-SYSTEM or per-thread time zone.
507
  */
508
  virtual void store_now_in_TIME(MYSQL_TIME *now_time)=0;
509
  bool result_as_longlong() { return TRUE; }
unknown's avatar
unknown committed
510 511 512
};


513 514 515 516 517 518
class Item_func_curtime_local :public Item_func_curtime
{
public:
  Item_func_curtime_local() :Item_func_curtime() {}
  Item_func_curtime_local(Item *a) :Item_func_curtime(a) {}
  const char *func_name() const { return "curtime"; }
519
  virtual void store_now_in_TIME(MYSQL_TIME *now_time);
520 521 522 523 524 525 526 527 528
};


class Item_func_curtime_utc :public Item_func_curtime
{
public:
  Item_func_curtime_utc() :Item_func_curtime() {}
  Item_func_curtime_utc(Item *a) :Item_func_curtime(a) {}
  const char *func_name() const { return "utc_time"; }
529
  virtual void store_now_in_TIME(MYSQL_TIME *now_time);
530 531 532 533 534
};


/* Abstract CURDATE function. See also Item_func_curtime. */

unknown's avatar
unknown committed
535 536 537
class Item_func_curdate :public Item_date
{
  longlong value;
538
  MYSQL_TIME ltime;
unknown's avatar
unknown committed
539 540
public:
  Item_func_curdate() :Item_date() {}
541
  longlong val_int() { DBUG_ASSERT(fixed == 1); return (value) ; }
542
  String *val_str(String *str);
543
  void fix_length_and_dec();
544 545
  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
  virtual void store_now_in_TIME(MYSQL_TIME *now_time)=0;
546 547 548 549 550 551 552 553
};


class Item_func_curdate_local :public Item_func_curdate
{
public:
  Item_func_curdate_local() :Item_func_curdate() {}
  const char *func_name() const { return "curdate"; }
554
  void store_now_in_TIME(MYSQL_TIME *now_time);
unknown's avatar
unknown committed
555 556 557
};


558 559 560 561 562
class Item_func_curdate_utc :public Item_func_curdate
{
public:
  Item_func_curdate_utc() :Item_func_curdate() {}
  const char *func_name() const { return "utc_date"; }
563
  void store_now_in_TIME(MYSQL_TIME *now_time);
564 565 566 567 568
};


/* Abstract CURRENT_TIMESTAMP function. See also Item_func_curtime */

569
class Item_func_now :public Item_date_func
unknown's avatar
unknown committed
570
{
571
protected:
unknown's avatar
unknown committed
572
  longlong value;
573
  char buff[20*2+32];	// +32 to make my_snprintf_{8bit|ucs2} happy
unknown's avatar
unknown committed
574
  uint buff_length;
575
  MYSQL_TIME ltime;
unknown's avatar
unknown committed
576
public:
577 578
  Item_func_now() :Item_date_func() {}
  Item_func_now(Item *a) :Item_date_func(a) {}
unknown's avatar
unknown committed
579
  enum Item_result result_type () const { return STRING_RESULT; }
580
  longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
unknown's avatar
unknown committed
581
  int save_in_field(Field *to, bool no_conversions);
582
  String *val_str(String *str);
unknown's avatar
unknown committed
583
  void fix_length_and_dec();
584 585
  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
  virtual void store_now_in_TIME(MYSQL_TIME *now_time)=0;
586 587 588 589 590 591 592 593 594
};


class Item_func_now_local :public Item_func_now
{
public:
  Item_func_now_local() :Item_func_now() {}
  Item_func_now_local(Item *a) :Item_func_now(a) {}
  const char *func_name() const { return "now"; }
595
  virtual void store_now_in_TIME(MYSQL_TIME *now_time);
596
  virtual enum Functype functype() const { return NOW_FUNC; }
597 598 599 600 601 602 603 604 605
};


class Item_func_now_utc :public Item_func_now
{
public:
  Item_func_now_utc() :Item_func_now() {}
  Item_func_now_utc(Item *a) :Item_func_now(a) {}
  const char *func_name() const { return "utc_timestamp"; }
606
  virtual void store_now_in_TIME(MYSQL_TIME *now_time);
unknown's avatar
unknown committed
607 608 609
};


610 611 612 613 614 615 616 617 618 619 620
/*
  This is like NOW(), but always uses the real current time, not the
  query_start(). This matches the Oracle behavior.
*/
class Item_func_sysdate_local :public Item_func_now
{
public:
  Item_func_sysdate_local() :Item_func_now() {}
  Item_func_sysdate_local(Item *a) :Item_func_now(a) {}
  bool const_item() const { return 0; }
  const char *func_name() const { return "sysdate"; }
621
  void store_now_in_TIME(MYSQL_TIME *now_time);
622 623 624 625 626
  double val_real();
  longlong val_int();
  int save_in_field(Field *to, bool no_conversions);
  String *val_str(String *str);
  void fix_length_and_dec();
627
  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
628 629 630 631 632 633 634 635
  void update_used_tables()
  {
    Item_func_now::update_used_tables();
    used_tables_cache|= RAND_TABLE_BIT;
  }
};


unknown's avatar
unknown committed
636 637 638 639 640
class Item_func_from_days :public Item_date
{
public:
  Item_func_from_days(Item *a) :Item_date(a) {}
  const char *func_name() const { return "from_days"; }
641
  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
642
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
643
  bool check_valid_arguments_processor(uchar *int_arg)
644 645 646
  {
    return has_date_args() || has_time_args();
  }
unknown's avatar
unknown committed
647 648 649 650 651 652
};


class Item_func_date_format :public Item_str_func
{
  int fixed_length;
653
  const bool is_time_format;
654
  String value;
unknown's avatar
unknown committed
655
public:
656 657
  Item_func_date_format(Item *a,Item *b,bool is_time_format_arg)
    :Item_str_func(a,b),is_time_format(is_time_format_arg) {}
unknown's avatar
unknown committed
658
  String *val_str(String *str);
659 660
  const char *func_name() const
    { return is_time_format ? "time_format" : "date_format"; }
unknown's avatar
unknown committed
661 662
  void fix_length_and_dec();
  uint format_length(const String *format);
unknown's avatar
unknown committed
663
  bool eq(const Item *item, bool binary_cmp) const;
unknown's avatar
unknown committed
664 665 666
};


667
class Item_func_from_unixtime :public Item_date_func
unknown's avatar
unknown committed
668
{
669
  THD *thd;
unknown's avatar
unknown committed
670
 public:
671
  Item_func_from_unixtime(Item *a) :Item_date_func(a) {}
unknown's avatar
unknown committed
672 673 674
  longlong val_int();
  String *val_str(String *str);
  const char *func_name() const { return "from_unixtime"; }
675
  void fix_length_and_dec();
676
  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
677 678 679 680 681 682 683 684 685
};


/* 
  We need Time_zone class declaration for storing pointers in
  Item_func_convert_tz.
*/
class Time_zone;

686 687 688 689 690 691 692 693
/*
  This class represents CONVERT_TZ() function.
  The important fact about this function that it is handled in special way.
  When such function is met in expression time_zone system tables are added
  to global list of tables to open, so later those already opened and locked
  tables can be used during this function calculation for loading time zone
  descriptions.
*/
694 695
class Item_func_convert_tz :public Item_date_func
{
696 697
  /*
    If time zone parameters are constants we are caching objects that
698 699 700
    represent them (we use separate from_tz_cached/to_tz_cached members
    to indicate this fact, since NULL is legal value for from_tz/to_tz
    members.
701
  */
702
  bool from_tz_cached, to_tz_cached;
703 704 705
  Time_zone *from_tz, *to_tz;
 public:
  Item_func_convert_tz(Item *a, Item *b, Item *c):
706
    Item_date_func(a, b, c), from_tz_cached(0), to_tz_cached(0) {}
707 708 709 710
  longlong val_int();
  String *val_str(String *str);
  const char *func_name() const { return "convert_tz"; }
  void fix_length_and_dec();
711
  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
712
  void cleanup();
unknown's avatar
unknown committed
713 714 715
};


716
class Item_func_sec_to_time :public Item_str_timefunc
unknown's avatar
unknown committed
717 718
{
public:
719
  Item_func_sec_to_time(Item *item) :Item_str_timefunc(item) {}
720
  double val_real()
721 722 723 724
  {
    DBUG_ASSERT(fixed == 1);
    return (double) Item_func_sec_to_time::val_int();
  }
unknown's avatar
unknown committed
725 726
  longlong val_int();
  String *val_str(String *);
727 728
  void fix_length_and_dec()
  { 
729
    Item_str_timefunc::fix_length_and_dec();
730
    collation.set(&my_charset_bin);
731 732
    maybe_null=1;
  }
unknown's avatar
unknown committed
733
  const char *func_name() const { return "sec_to_time"; }
734
  bool result_as_longlong() { return TRUE; }
unknown's avatar
unknown committed
735 736
};

737

738
class Item_date_add_interval :public Item_date_func
unknown's avatar
unknown committed
739 740
{
  String value;
741
  enum_field_types cached_field_type;
unknown's avatar
unknown committed
742 743

public:
744 745
  const interval_type int_type; // keep it public
  const bool date_sub_interval; // keep it public
unknown's avatar
unknown committed
746
  Item_date_add_interval(Item *a,Item *b,interval_type type_arg,bool neg_arg)
747
    :Item_date_func(a,b),int_type(type_arg), date_sub_interval(neg_arg) {}
unknown's avatar
unknown committed
748 749
  String *val_str(String *);
  const char *func_name() const { return "date_add_interval"; }
750 751
  void fix_length_and_dec();
  enum_field_types field_type() const { return cached_field_type; }
unknown's avatar
unknown committed
752
  longlong val_int();
753
  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
754
  bool eq(const Item *item, bool binary_cmp) const;
755
  virtual void print(String *str, enum_query_type query_type);
unknown's avatar
unknown committed
756 757
};

758

unknown's avatar
unknown committed
759 760 761 762
class Item_extract :public Item_int_func
{
  bool date_value;
 public:
763
  const interval_type int_type; // keep it public
unknown's avatar
unknown committed
764 765 766
  Item_extract(interval_type type_arg, Item *a)
    :Item_int_func(a), int_type(type_arg) {}
  longlong val_int();
767
  enum Functype functype() const { return EXTRACT_FUNC; }
unknown's avatar
unknown committed
768 769
  const char *func_name() const { return "extract"; }
  void fix_length_and_dec();
unknown's avatar
unknown committed
770
  bool eq(const Item *item, bool binary_cmp) const;
771
  virtual void print(String *str, enum_query_type query_type);
772
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
773
  bool check_valid_arguments_processor(uchar *int_arg)
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
  {
    switch (int_type) {
    case INTERVAL_YEAR:
    case INTERVAL_YEAR_MONTH:
    case INTERVAL_QUARTER:
    case INTERVAL_MONTH:
    /* case INTERVAL_WEEK: Not allowed as partitioning function, bug#57071 */
    case INTERVAL_DAY:
      return !has_date_args();
    case INTERVAL_DAY_HOUR:
    case INTERVAL_DAY_MINUTE:
    case INTERVAL_DAY_SECOND:
    case INTERVAL_DAY_MICROSECOND:
      return !has_datetime_args();
    case INTERVAL_HOUR:
    case INTERVAL_HOUR_MINUTE:
    case INTERVAL_HOUR_SECOND:
    case INTERVAL_MINUTE:
    case INTERVAL_MINUTE_SECOND:
    case INTERVAL_SECOND:
    case INTERVAL_MICROSECOND:
    case INTERVAL_HOUR_MICROSECOND:
    case INTERVAL_MINUTE_MICROSECOND:
    case INTERVAL_SECOND_MICROSECOND:
      return !has_time_args();
    default:
      /*
        INTERVAL_LAST is only an end marker,
        INTERVAL_WEEK depends on default_week_format which is a session
        variable and cannot be used for partitioning. See bug#57071.
      */
      break;
    }
    return true;
  }
unknown's avatar
unknown committed
809
};
810

811

unknown's avatar
unknown committed
812
class Item_typecast :public Item_str_func
813 814
{
public:
unknown's avatar
unknown committed
815
  Item_typecast(Item *a) :Item_str_func(a) {}
816
  String *val_str(String *a)
unknown's avatar
unknown committed
817
  {
818
    DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
819 820
    String *tmp=args[0]->val_str(a);
    null_value=args[0]->null_value;
unknown's avatar
unknown committed
821
    if (tmp)
822
      tmp->set_charset(collation.collation);
unknown's avatar
unknown committed
823 824 825 826
    return tmp;
  }
  void fix_length_and_dec()
  {
827
    collation.set(&my_charset_bin);
unknown's avatar
unknown committed
828 829
    max_length=args[0]->max_length;
  }
830
  virtual const char* cast_type() const= 0;
831
  virtual void print(String *str, enum_query_type query_type);
unknown's avatar
unknown committed
832 833 834
};


835 836 837 838 839 840 841 842 843 844 845 846 847
class Item_typecast_maybe_null :public Item_typecast
{
public:
  Item_typecast_maybe_null(Item *a) :Item_typecast(a) {}
  void fix_length_and_dec()
  {
    collation.set(&my_charset_bin);
    max_length=args[0]->max_length;
    maybe_null= 1;
  }
};


848 849
class Item_char_typecast :public Item_typecast
{
850
  int cast_length;
851
  CHARSET_INFO *cast_cs, *from_cs;
852 853 854 855 856
  bool charset_conversion;
  String tmp_value;
public:
  Item_char_typecast(Item *a, int length_arg, CHARSET_INFO *cs_arg)
    :Item_typecast(a), cast_length(length_arg), cast_cs(cs_arg) {}
857
  enum Functype functype() const { return CHAR_TYPECAST_FUNC; }
858 859
  bool eq(const Item *item, bool binary_cmp) const;
  const char *func_name() const { return "cast_as_char"; }
860
  const char* cast_type() const { return "char"; };
861 862
  String *val_str(String *a);
  void fix_length_and_dec();
863
  virtual void print(String *str, enum_query_type query_type);
864 865 866
};


867
class Item_date_typecast :public Item_typecast_maybe_null
unknown's avatar
unknown committed
868 869
{
public:
870
  Item_date_typecast(Item *a) :Item_typecast_maybe_null(a) {}
871
  const char *func_name() const { return "cast_as_date"; }
unknown's avatar
unknown committed
872
  String *val_str(String *str);
873
  bool get_date(MYSQL_TIME *ltime, uint fuzzy_date);
874
  bool get_time(MYSQL_TIME *ltime);
875
  const char *cast_type() const { return "date"; }
876
  enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
unknown's avatar
unknown committed
877
  Field *tmp_table_field(TABLE *table)
878
  {
unknown's avatar
unknown committed
879
    return tmp_table_field_from_field_type(table, 0);
880
  }  
881 882 883 884 885 886
  void fix_length_and_dec()
  {
    collation.set(&my_charset_bin);
    max_length= 10;
    maybe_null= 1;
  }
887 888
  bool result_as_longlong() { return TRUE; }
  longlong val_int();
889
  double val_real() { return (double) val_int(); }
890 891 892 893 894 895 896 897 898
  my_decimal *val_decimal(my_decimal *decimal_value)
  {
    DBUG_ASSERT(fixed == 1);
    return  val_decimal_from_date(decimal_value);
  }
  int save_in_field(Field *field, bool no_conversions)
  {
    return save_date_in_field(field);
  }
899 900
};

901

902
class Item_time_typecast :public Item_typecast_maybe_null
903 904
{
public:
905
  Item_time_typecast(Item *a) :Item_typecast_maybe_null(a) {}
906
  const char *func_name() const { return "cast_as_time"; }
unknown's avatar
unknown committed
907
  String *val_str(String *str);
908
  bool get_time(MYSQL_TIME *ltime);
909
  const char *cast_type() const { return "time"; }
910
  enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
unknown's avatar
unknown committed
911
  Field *tmp_table_field(TABLE *table)
912
  {
unknown's avatar
unknown committed
913
    return tmp_table_field_from_field_type(table, 0);
914
  }
915 916
  bool result_as_longlong() { return TRUE; }
  longlong val_int();
917
  double val_real() { return val_real_from_decimal(); }
918 919 920 921 922 923 924 925 926
  my_decimal *val_decimal(my_decimal *decimal_value)
  {
    DBUG_ASSERT(fixed == 1);
    return  val_decimal_from_time(decimal_value);
  }
  int save_in_field(Field *field, bool no_conversions)
  {
    return save_time_in_field(field);
  }
927 928
};

929

930
class Item_datetime_typecast :public Item_typecast_maybe_null
931 932
{
public:
933
  Item_datetime_typecast(Item *a) :Item_typecast_maybe_null(a) {}
934
  const char *func_name() const { return "cast_as_datetime"; }
unknown's avatar
unknown committed
935
  String *val_str(String *str);
936
  const char *cast_type() const { return "datetime"; }
937
  enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
unknown's avatar
unknown committed
938
  Field *tmp_table_field(TABLE *table)
939
  {
unknown's avatar
unknown committed
940
    return tmp_table_field_from_field_type(table, 0);
941
  }
942 943
  void fix_length_and_dec()
  {
944 945 946
    collation.set(&my_charset_bin);
    maybe_null= 1;
    max_length= MAX_DATETIME_FULL_WIDTH * MY_CHARSET_BIN_MB_MAXLEN;
947 948
    decimals= DATETIME_DEC;
  }
949 950
  bool result_as_longlong() { return TRUE; }
  longlong val_int();
951
  double val_real() { return val_real_from_decimal(); }
952
  double val() { return (double) val_int(); }
953 954 955 956 957 958 959 960 961
  my_decimal *val_decimal(my_decimal *decimal_value)
  {
    DBUG_ASSERT(fixed == 1);
    return  val_decimal_from_date(decimal_value);
  }
  int save_in_field(Field *field, bool no_conversions)
  {
    return save_date_in_field(field);
  }
962
};
unknown's avatar
unknown committed
963

964
class Item_func_makedate :public Item_date_func
unknown's avatar
unknown committed
965 966
{
public:
967
  Item_func_makedate(Item *a,Item *b) :Item_date_func(a,b) {}
unknown's avatar
unknown committed
968 969 970 971 972 973
  String *val_str(String *str);
  const char *func_name() const { return "makedate"; }
  enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
  void fix_length_and_dec()
  { 
    decimals=0;
974
    max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
975 976
    /* It returns NULL when the second argument is less or equal to 0 */
    maybe_null= 1;
unknown's avatar
unknown committed
977
  }
978
  longlong val_int();
unknown's avatar
unknown committed
979 980
};

unknown's avatar
unknown committed
981

unknown's avatar
unknown committed
982 983
class Item_func_add_time :public Item_str_func
{
unknown's avatar
unknown committed
984
  const bool is_date;
unknown's avatar
unknown committed
985 986 987 988
  int sign;
  enum_field_types cached_field_type;

public:
unknown's avatar
unknown committed
989 990
  Item_func_add_time(Item *a, Item *b, bool type_arg, bool neg_arg)
    :Item_str_func(a, b), is_date(type_arg) { sign= neg_arg ? -1 : 1; }
unknown's avatar
unknown committed
991 992 993
  String *val_str(String *str);
  enum_field_types field_type() const { return cached_field_type; }
  void fix_length_and_dec();
unknown's avatar
unknown committed
994

unknown's avatar
unknown committed
995
  Field *tmp_table_field(TABLE *table)
unknown's avatar
unknown committed
996
  {
unknown's avatar
unknown committed
997
    return tmp_table_field_from_field_type(table, 0);
unknown's avatar
unknown committed
998
  }
999
  virtual void print(String *str, enum_query_type query_type);
1000
  const char *func_name() const { return "add_time"; }
1001
  double val_real() { return val_real_from_decimal(); }
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
  my_decimal *val_decimal(my_decimal *decimal_value)
  {
    DBUG_ASSERT(fixed == 1);
    if (cached_field_type == MYSQL_TYPE_TIME)
      return  val_decimal_from_time(decimal_value);
    if (cached_field_type == MYSQL_TYPE_DATETIME)
      return  val_decimal_from_date(decimal_value);
    return Item_str_func::val_decimal(decimal_value);
  }
  int save_in_field(Field *field, bool no_conversions)
  {
    if (cached_field_type == MYSQL_TYPE_TIME)
      return save_time_in_field(field);
    if (cached_field_type == MYSQL_TYPE_DATETIME)
      return save_date_in_field(field);
    return Item_str_func::save_in_field(field, no_conversions);
  }
unknown's avatar
unknown committed
1019 1020
};

1021
class Item_func_timediff :public Item_str_timefunc
unknown's avatar
unknown committed
1022 1023 1024
{
public:
  Item_func_timediff(Item *a, Item *b)
1025
    :Item_str_timefunc(a, b) {}
unknown's avatar
unknown committed
1026 1027 1028 1029
  String *val_str(String *str);
  const char *func_name() const { return "timediff"; }
  void fix_length_and_dec()
  {
1030
    Item_str_timefunc::fix_length_and_dec();
1031
    maybe_null= 1;
unknown's avatar
unknown committed
1032 1033 1034
  }
};

1035
class Item_func_maketime :public Item_str_timefunc
unknown's avatar
unknown committed
1036 1037 1038
{
public:
  Item_func_maketime(Item *a, Item *b, Item *c)
1039 1040 1041 1042
    :Item_str_timefunc(a, b, c) 
  {
    maybe_null= TRUE;
  }
unknown's avatar
unknown committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
  String *val_str(String *str);
  const char *func_name() const { return "maketime"; }
};

class Item_func_microsecond :public Item_int_func
{
public:
  Item_func_microsecond(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "microsecond"; }
  void fix_length_and_dec() 
  { 
    decimals=0;
    maybe_null=1;
  }
1058
  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
1059
  bool check_valid_arguments_processor(uchar *int_arg)
1060 1061 1062
  {
    return !has_time_args();
  }
unknown's avatar
unknown committed
1063
};
1064 1065


1066 1067 1068 1069 1070 1071
class Item_func_timestamp_diff :public Item_int_func
{
  const interval_type int_type;
public:
  Item_func_timestamp_diff(Item *a,Item *b,interval_type type_arg)
    :Item_int_func(a,b), int_type(type_arg) {}
1072
  const char *func_name() const { return "timestampdiff"; }
1073 1074 1075 1076 1077 1078
  longlong val_int();
  void fix_length_and_dec()
  {
    decimals=0;
    maybe_null=1;
  }
1079
  virtual void print(String *str, enum_query_type query_type);
1080
};
unknown's avatar
unknown committed
1081 1082


1083
enum date_time_format
1084 1085 1086 1087 1088 1089 1090
{
  USA_FORMAT, JIS_FORMAT, ISO_FORMAT, EUR_FORMAT, INTERNAL_FORMAT
};

class Item_func_get_format :public Item_str_func
{
public:
1091
  const timestamp_type type; // keep it public
1092 1093 1094
  Item_func_get_format(timestamp_type type_arg, Item *a)
    :Item_str_func(a), type(type_arg)
  {}
1095 1096 1097 1098
  String *val_str(String *str);
  const char *func_name() const { return "get_format"; }
  void fix_length_and_dec()
  {
1099
    maybe_null= 1;
1100 1101 1102
    decimals=0;
    max_length=17*MY_CHARSET_BIN_MB_MAXLEN;
  }
1103
  virtual void print(String *str, enum_query_type query_type);
1104 1105 1106
};


1107
class Item_func_str_to_date :public Item_str_func
1108
{
1109 1110 1111 1112
  enum_field_types cached_field_type;
  date_time_format_types cached_format_type;
  timestamp_type cached_timestamp_type;
  bool const_item;
1113 1114
public:
  Item_func_str_to_date(Item *a, Item *b)
1115
    :Item_str_func(a, b), const_item(false)
1116
  {}
1117
  String *val_str(String *str);
1118
  bool get_date(MYSQL_TIME *ltime, uint fuzzy_date);
1119
  const char *func_name() const { return "str_to_date"; }
1120 1121
  enum_field_types field_type() const { return cached_field_type; }
  void fix_length_and_dec();
unknown's avatar
unknown committed
1122 1123 1124 1125
  Field *tmp_table_field(TABLE *table)
  {
    return tmp_table_field_from_field_type(table, 1);
  }
1126
};
unknown's avatar
unknown committed
1127

1128 1129

class Item_func_last_day :public Item_date
unknown's avatar
unknown committed
1130 1131
{
public:
1132
  Item_func_last_day(Item *a) :Item_date(a) {}
unknown's avatar
unknown committed
1133
  const char *func_name() const { return "last_day"; }
1134
  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
unknown's avatar
unknown committed
1135
};