item_timefunc.h 24.2 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4 5 6
   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
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
unknown's avatar
unknown committed
7

unknown's avatar
unknown committed
8 9 10 11
   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
12

unknown's avatar
unknown committed
13 14 15 16 17 18 19
   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 */

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

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

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

unknown's avatar
unknown committed
32 33 34 35 36 37
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"; }
38 39
  void fix_length_and_dec() 
  { 
40
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
41
  }
unknown's avatar
unknown committed
42 43 44 45 46 47 48 49 50
};


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"; }
51 52 53
  void fix_length_and_dec()
  { 
    decimals=0;
54
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
55
  }
unknown's avatar
unknown committed
56 57 58 59 60 61 62 63 64
};


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"; }
65 66 67
  void fix_length_and_dec() 
  { 
    decimals=0; 
68
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
69 70
    maybe_null=1; 
  }
unknown's avatar
unknown committed
71
  enum_monotonicity_info get_monotonicity_info() const;
unknown's avatar
unknown committed
72 73 74 75 76 77 78 79 80
};


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"; }
81 82 83
  void fix_length_and_dec() 
  { 
    decimals=0; 
84
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
85 86
    maybe_null=1; 
  }
unknown's avatar
unknown committed
87 88 89 90 91 92 93 94
};


class Item_func_month :public Item_func
{
public:
  Item_func_month(Item *a) :Item_func(a) {}
  longlong val_int();
95
  double val_real()
96
  { DBUG_ASSERT(fixed == 1); return (double) Item_func_month::val_int(); }
97 98
  String *val_str(String *str) 
  {
99
    str->set(val_int(), &my_charset_bin);
100 101
    return null_value ? 0 : str;
  }
unknown's avatar
unknown committed
102 103
  const char *func_name() const { return "month"; }
  enum Item_result result_type () const { return INT_RESULT; }
104 105
  void fix_length_and_dec() 
  { 
106
    collation.set(&my_charset_bin);
107
    decimals=0;
108
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
109 110
    maybe_null=1; 
  }
unknown's avatar
unknown committed
111 112
};

113

unknown's avatar
unknown committed
114 115 116 117 118 119 120
class Item_func_monthname :public Item_func_month
{
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; }
121
  void fix_length_and_dec() 
122
  {
123
    collation.set(&my_charset_bin);
124
    decimals=0;
125
    max_length=10*my_charset_bin.mbmaxlen;
126 127
    maybe_null=1; 
  }
unknown's avatar
unknown committed
128 129 130 131 132 133 134 135 136
};


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"; }
137 138 139
  void fix_length_and_dec() 
  { 
    decimals=0;
140
    max_length=3*MY_CHARSET_BIN_MB_MAXLEN;
141 142
    maybe_null=1; 
  }
unknown's avatar
unknown committed
143 144 145 146 147 148 149 150 151
};


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"; }
152 153 154
  void fix_length_and_dec()
  {
    decimals=0;
155
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
156 157
    maybe_null=1;
  }
unknown's avatar
unknown committed
158 159 160 161 162 163 164 165 166
};


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"; }
167 168 169
  void fix_length_and_dec()
  {
    decimals=0;
170
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
171 172
    maybe_null=1;
  }
unknown's avatar
unknown committed
173 174 175 176 177 178 179 180 181
};


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"; }
182 183 184
  void fix_length_and_dec()
  { 
     decimals=0;
185
     max_length=1*MY_CHARSET_BIN_MB_MAXLEN;
186 187
     maybe_null=1;
  }
unknown's avatar
unknown committed
188 189 190 191 192 193 194 195 196
};


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"; }
197 198 199
  void fix_length_and_dec() 
  { 
    decimals=0;
200
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
201 202
    maybe_null=1;
  }
unknown's avatar
unknown committed
203 204 205 206 207 208 209 210 211
};


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"; }
212 213 214
  void fix_length_and_dec()
  { 
    decimals=0;
215
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
216 217
    maybe_null=1;
  }
unknown's avatar
unknown committed
218 219 220 221 222 223 224 225
};

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"; }
226 227 228
  void fix_length_and_dec()
  { 
    decimals=0;
229
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
230 231
    maybe_null=1;
  }
unknown's avatar
unknown committed
232 233 234 235 236 237 238 239 240
};


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
241
  enum_monotonicity_info get_monotonicity_info() const;
242 243 244
  void fix_length_and_dec()
  { 
    decimals=0;
245
    max_length=4*MY_CHARSET_BIN_MB_MAXLEN;
246 247
    maybe_null=1;
  }
unknown's avatar
unknown committed
248 249 250 251 252 253 254 255 256 257
};


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();
258
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
259
  String *val_str(String *str)
260 261
  {
    DBUG_ASSERT(fixed == 1);
262
    str->set(val_int(), &my_charset_bin);
263 264
    return null_value ? 0 : str;
  }
265 266 267 268
  const char *func_name() const
  {
     return (odbc_type ? "dayofweek" : "weekday");
  }
unknown's avatar
unknown committed
269
  enum Item_result result_type () const { return INT_RESULT; }
270 271
  void fix_length_and_dec()
  {
272
    collation.set(&my_charset_bin);
273
    decimals=0;
274
    max_length=1*MY_CHARSET_BIN_MB_MAXLEN;
275 276
    maybe_null=1;
  }
unknown's avatar
unknown committed
277 278 279 280 281 282 283 284 285
};

class Item_func_dayname :public Item_func_weekday
{
 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; }
286 287
  void fix_length_and_dec() 
  { 
288
    collation.set(&my_charset_bin);
289
    decimals=0; 
290
    max_length=9*MY_CHARSET_BIN_MB_MAXLEN;
291 292
    maybe_null=1; 
  }
unknown's avatar
unknown committed
293 294 295 296 297 298 299 300 301 302
};


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();
303
  const char *func_name() const { return "unix_timestamp"; }
unknown's avatar
unknown committed
304 305
  void fix_length_and_dec()
  {
306
    decimals=0;
307
    max_length=10*MY_CHARSET_BIN_MB_MAXLEN;
unknown's avatar
unknown committed
308 309 310 311 312 313 314 315 316 317 318 319
  }
};


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()
  {
320
    decimals=0;
321
    max_length=10*MY_CHARSET_BIN_MB_MAXLEN;
unknown's avatar
unknown committed
322 323 324 325
  }
};


unknown's avatar
unknown committed
326 327 328
/*
  This can't be a Item_str_func, because the val_real() functions are special
*/
unknown's avatar
unknown committed
329 330 331 332 333 334 335

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; }
336
  enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
unknown's avatar
unknown committed
337
  String *val_str(String *str);
338
  longlong val_int();
339
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
unknown's avatar
unknown committed
340
  const char *func_name() const { return "date"; }
341 342
  void fix_length_and_dec()
  { 
343
    collation.set(&my_charset_bin);
344
    decimals=0;
345
    max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
346
  }
unknown's avatar
unknown committed
347
  int save_in_field(Field *to, bool no_conversions);
unknown's avatar
unknown committed
348
  Field *tmp_table_field(TABLE *table)
349
  {
unknown's avatar
unknown committed
350 351
    return tmp_table_field_from_field_type(table, 0);
  }
352 353 354 355 356 357 358 359 360
};


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) {}
361
  Item_date_func(Item *a,Item *b, Item *c) :Item_str_func(a,b,c) {}
362
  enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
unknown's avatar
unknown committed
363
  Field *tmp_table_field(TABLE *table)
364
  {
unknown's avatar
unknown committed
365
    return tmp_table_field_from_field_type(table, 0);
366
  }
unknown's avatar
unknown committed
367 368 369
};


370
/* Abstract CURTIME function. Children should define what time zone is used */
371

unknown's avatar
unknown committed
372 373 374
class Item_func_curtime :public Item_func
{
  longlong value;
375
  char buff[9*2+32];
unknown's avatar
unknown committed
376 377 378 379 380
  uint buff_length;
public:
  Item_func_curtime() :Item_func() {}
  Item_func_curtime(Item *a) :Item_func(a) {}
  enum Item_result result_type () const { return STRING_RESULT; }
381
  enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
382
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) value; }
383
  longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
384
  String *val_str(String *str);
unknown's avatar
unknown committed
385
  void fix_length_and_dec();
unknown's avatar
unknown committed
386
  Field *tmp_table_field(TABLE *table)
387
  {
unknown's avatar
unknown committed
388
    return tmp_table_field_from_field_type(table, 0);
389 390 391
  }
  /* 
    Abstract method that defines which time zone is used for conversion.
392 393
    Converts time current time in my_time_t representation to broken-down
    TIME representation using UTC-SYSTEM or per-thread time zone.
394
  */
395
  virtual void store_now_in_TIME(TIME *now_time)=0;
unknown's avatar
unknown committed
396 397 398
};


399 400 401 402 403 404
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"; }
405
  virtual void store_now_in_TIME(TIME *now_time);
406 407 408 409 410 411 412 413 414
};


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"; }
415
  virtual void store_now_in_TIME(TIME *now_time);
416 417 418 419 420
};


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

unknown's avatar
unknown committed
421 422 423 424 425 426
class Item_func_curdate :public Item_date
{
  longlong value;
  TIME ltime;
public:
  Item_func_curdate() :Item_date() {}
427
  longlong val_int() { DBUG_ASSERT(fixed == 1); return (value) ; }
428
  String *val_str(String *str);
429
  void fix_length_and_dec();
430
  bool get_date(TIME *res, uint fuzzy_date);
431
  virtual void store_now_in_TIME(TIME *now_time)=0;
432 433 434 435 436 437 438 439
};


class Item_func_curdate_local :public Item_func_curdate
{
public:
  Item_func_curdate_local() :Item_func_curdate() {}
  const char *func_name() const { return "curdate"; }
440
  void store_now_in_TIME(TIME *now_time);
unknown's avatar
unknown committed
441 442 443
};


444 445 446 447 448
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"; }
449
  void store_now_in_TIME(TIME *now_time);
450 451 452 453 454
};


/* Abstract CURRENT_TIMESTAMP function. See also Item_func_curtime */

455
class Item_func_now :public Item_date_func
unknown's avatar
unknown committed
456
{
457
protected:
unknown's avatar
unknown committed
458
  longlong value;
459
  char buff[20*2+32];	// +32 to make my_snprintf_{8bit|ucs2} happy
unknown's avatar
unknown committed
460 461 462
  uint buff_length;
  TIME ltime;
public:
463 464
  Item_func_now() :Item_date_func() {}
  Item_func_now(Item *a) :Item_date_func(a) {}
unknown's avatar
unknown committed
465
  enum Item_result result_type () const { return STRING_RESULT; }
466
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) value; }
467
  longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
unknown's avatar
unknown committed
468
  int save_in_field(Field *to, bool no_conversions);
469
  String *val_str(String *str);
unknown's avatar
unknown committed
470
  void fix_length_and_dec();
471
  bool get_date(TIME *res, uint fuzzy_date);
472
  virtual void store_now_in_TIME(TIME *now_time)=0;
473 474 475 476 477 478 479 480 481
};


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"; }
482
  virtual void store_now_in_TIME(TIME *now_time);
483
  virtual enum Functype functype() const { return NOW_FUNC; }
484 485 486 487 488 489 490 491 492
};


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"; }
493
  virtual void store_now_in_TIME(TIME *now_time);
unknown's avatar
unknown committed
494 495 496
};


497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
/*
  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"; }
  void store_now_in_TIME(TIME *now_time);
  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();
  bool get_date(TIME *res, uint fuzzy_date);
  void update_used_tables()
  {
    Item_func_now::update_used_tables();
    used_tables_cache|= RAND_TABLE_BIT;
  }
};


unknown's avatar
unknown committed
523 524 525 526 527
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"; }
528
  bool get_date(TIME *res, uint fuzzy_date);
unknown's avatar
unknown committed
529 530 531 532 533 534
};


class Item_func_date_format :public Item_str_func
{
  int fixed_length;
535
  const bool is_time_format;
536
  String value;
unknown's avatar
unknown committed
537
public:
538 539
  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
540
  String *val_str(String *str);
541 542
  const char *func_name() const
    { return is_time_format ? "time_format" : "date_format"; }
unknown's avatar
unknown committed
543 544
  void fix_length_and_dec();
  uint format_length(const String *format);
unknown's avatar
unknown committed
545
  bool eq(const Item *item, bool binary_cmp) const;
unknown's avatar
unknown committed
546 547 548
};


549
class Item_func_from_unixtime :public Item_date_func
unknown's avatar
unknown committed
550
{
551
  THD *thd;
unknown's avatar
unknown committed
552
 public:
553
  Item_func_from_unixtime(Item *a) :Item_date_func(a) {}
554
  double val_real()
555 556 557 558
  {
    DBUG_ASSERT(fixed == 1);
    return (double) Item_func_from_unixtime::val_int();
  }
unknown's avatar
unknown committed
559 560 561
  longlong val_int();
  String *val_str(String *str);
  const char *func_name() const { return "from_unixtime"; }
562 563 564 565 566 567 568 569 570 571 572
  void fix_length_and_dec();
  bool get_date(TIME *res, uint fuzzy_date);
};


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

573 574 575 576 577 578 579 580
/*
  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.
*/
581 582
class Item_func_convert_tz :public Item_date_func
{
583 584 585 586
  /* Cached pointer to list of pre-opened time zone tables. */
  TABLE_LIST *tz_tables;
  /*
    If time zone parameters are constants we are caching objects that
587 588 589
    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.
590
  */
591
  bool from_tz_cached, to_tz_cached;
592 593 594
  Time_zone *from_tz, *to_tz;
 public:
  Item_func_convert_tz(Item *a, Item *b, Item *c):
595
    Item_date_func(a, b, c), from_tz_cached(0), to_tz_cached(0) {}
596
  longlong val_int();
597
  double val_real() { return (double) val_int(); }
598 599
  String *val_str(String *str);
  const char *func_name() const { return "convert_tz"; }
600
  bool fix_fields(THD *, Item **);
601
  void fix_length_and_dec();
602
  bool get_date(TIME *res, uint fuzzy_date);
603
  void cleanup();
unknown's avatar
unknown committed
604 605 606 607 608 609 610
};


class Item_func_sec_to_time :public Item_str_func
{
public:
  Item_func_sec_to_time(Item *item) :Item_str_func(item) {}
611
  double val_real()
612 613 614 615
  {
    DBUG_ASSERT(fixed == 1);
    return (double) Item_func_sec_to_time::val_int();
  }
unknown's avatar
unknown committed
616 617
  longlong val_int();
  String *val_str(String *);
618 619
  void fix_length_and_dec()
  { 
620
    collation.set(&my_charset_bin);
621
    maybe_null=1;
622
    max_length=MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
623
  }
624
  enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
unknown's avatar
unknown committed
625
  const char *func_name() const { return "sec_to_time"; }
unknown's avatar
unknown committed
626
  Field *tmp_table_field(TABLE *table)
627
  {
unknown's avatar
unknown committed
628
    return tmp_table_field_from_field_type(table, 0);
629
  }
unknown's avatar
unknown committed
630 631
};

632

633
class Item_date_add_interval :public Item_date_func
unknown's avatar
unknown committed
634 635
{
  String value;
636
  enum_field_types cached_field_type;
unknown's avatar
unknown committed
637 638

public:
639 640
  const interval_type int_type; // keep it public
  const bool date_sub_interval; // keep it public
unknown's avatar
unknown committed
641
  Item_date_add_interval(Item *a,Item *b,interval_type type_arg,bool neg_arg)
642
    :Item_date_func(a,b),int_type(type_arg), date_sub_interval(neg_arg) {}
unknown's avatar
unknown committed
643 644
  String *val_str(String *);
  const char *func_name() const { return "date_add_interval"; }
645 646
  void fix_length_and_dec();
  enum_field_types field_type() const { return cached_field_type; }
647
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
unknown's avatar
unknown committed
648
  longlong val_int();
649
  bool get_date(TIME *res, uint fuzzy_date);
650
  void print(String *str);
unknown's avatar
unknown committed
651 652
};

653

unknown's avatar
unknown committed
654 655 656 657 658
class Item_extract :public Item_int_func
{
  String value;
  bool date_value;
 public:
659
  const interval_type int_type; // keep it public
unknown's avatar
unknown committed
660 661 662
  Item_extract(interval_type type_arg, Item *a)
    :Item_int_func(a), int_type(type_arg) {}
  longlong val_int();
663
  enum Functype functype() const { return EXTRACT_FUNC; }
unknown's avatar
unknown committed
664 665
  const char *func_name() const { return "extract"; }
  void fix_length_and_dec();
unknown's avatar
unknown committed
666
  bool eq(const Item *item, bool binary_cmp) const;
667
  void print(String *str);
unknown's avatar
unknown committed
668
};
669

670

unknown's avatar
unknown committed
671
class Item_typecast :public Item_str_func
672 673
{
public:
unknown's avatar
unknown committed
674
  Item_typecast(Item *a) :Item_str_func(a) {}
675
  String *val_str(String *a)
unknown's avatar
unknown committed
676
  {
677
    DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
678 679
    String *tmp=args[0]->val_str(a);
    null_value=args[0]->null_value;
unknown's avatar
unknown committed
680
    if (tmp)
681
      tmp->set_charset(collation.collation);
unknown's avatar
unknown committed
682 683 684 685
    return tmp;
  }
  void fix_length_and_dec()
  {
686
    collation.set(&my_charset_bin);
unknown's avatar
unknown committed
687 688
    max_length=args[0]->max_length;
  }
689
  virtual const char* cast_type() const= 0;
unknown's avatar
unknown committed
690 691 692 693
  void print(String *str);
};


694 695 696 697 698 699 700 701 702 703 704 705 706
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;
  }
};


707 708
class Item_char_typecast :public Item_typecast
{
709 710 711 712 713 714 715
  int cast_length;
  CHARSET_INFO *cast_cs;
  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) {}
716
  enum Functype functype() const { return CHAR_TYPECAST_FUNC; }
717 718
  bool eq(const Item *item, bool binary_cmp) const;
  const char *func_name() const { return "cast_as_char"; }
719
  const char* cast_type() const { return "char"; };
720 721
  String *val_str(String *a);
  void fix_length_and_dec();
722
  void print(String *str);
723 724 725
};


726
class Item_date_typecast :public Item_typecast_maybe_null
unknown's avatar
unknown committed
727 728
{
public:
729
  Item_date_typecast(Item *a) :Item_typecast_maybe_null(a) {}
730
  const char *func_name() const { return "cast_as_date"; }
unknown's avatar
unknown committed
731
  String *val_str(String *str);
732
  bool get_date(TIME *ltime, uint fuzzy_date);
733
  const char *cast_type() const { return "date"; }
734
  enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
unknown's avatar
unknown committed
735
  Field *tmp_table_field(TABLE *table)
736
  {
unknown's avatar
unknown committed
737
    return tmp_table_field_from_field_type(table, 0);
738
  }  
739 740 741 742 743 744
  void fix_length_and_dec()
  {
    collation.set(&my_charset_bin);
    max_length= 10;
    maybe_null= 1;
  }
745 746
};

747

748
class Item_time_typecast :public Item_typecast_maybe_null
749 750
{
public:
751
  Item_time_typecast(Item *a) :Item_typecast_maybe_null(a) {}
752
  const char *func_name() const { return "cast_as_time"; }
unknown's avatar
unknown committed
753 754
  String *val_str(String *str);
  bool get_time(TIME *ltime);
755
  const char *cast_type() const { return "time"; }
756
  enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
unknown's avatar
unknown committed
757
  Field *tmp_table_field(TABLE *table)
758
  {
unknown's avatar
unknown committed
759
    return tmp_table_field_from_field_type(table, 0);
760
  }
761 762
};

763

764
class Item_datetime_typecast :public Item_typecast_maybe_null
765 766
{
public:
767
  Item_datetime_typecast(Item *a) :Item_typecast_maybe_null(a) {}
768
  const char *func_name() const { return "cast_as_datetime"; }
unknown's avatar
unknown committed
769
  String *val_str(String *str);
770
  const char *cast_type() const { return "datetime"; }
771
  enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
unknown's avatar
unknown committed
772
  Field *tmp_table_field(TABLE *table)
773
  {
unknown's avatar
unknown committed
774
    return tmp_table_field_from_field_type(table, 0);
775
  }
776
};
unknown's avatar
unknown committed
777 778 779 780 781 782 783 784 785 786 787

class Item_func_makedate :public Item_str_func
{
public:
  Item_func_makedate(Item *a,Item *b) :Item_str_func(a,b) {}
  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;
788
    max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
unknown's avatar
unknown committed
789
  }
unknown's avatar
unknown committed
790
  Field *tmp_table_field(TABLE *table)
unknown's avatar
unknown committed
791
  {
unknown's avatar
unknown committed
792
    return tmp_table_field_from_field_type(table, 0);
unknown's avatar
unknown committed
793 794 795
  }
};

unknown's avatar
unknown committed
796

unknown's avatar
unknown committed
797 798
class Item_func_add_time :public Item_str_func
{
unknown's avatar
unknown committed
799
  const bool is_date;
unknown's avatar
unknown committed
800 801 802 803
  int sign;
  enum_field_types cached_field_type;

public:
unknown's avatar
unknown committed
804 805
  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
806 807 808
  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
809

unknown's avatar
unknown committed
810
  Field *tmp_table_field(TABLE *table)
unknown's avatar
unknown committed
811
  {
unknown's avatar
unknown committed
812
    return tmp_table_field_from_field_type(table, 0);
unknown's avatar
unknown committed
813
  }
814
  void print(String *str);
815
  const char *func_name() const { return "add_time"; }
unknown's avatar
unknown committed
816 817 818 819 820 821 822 823 824 825 826 827 828
};

class Item_func_timediff :public Item_str_func
{
public:
  Item_func_timediff(Item *a, Item *b)
    :Item_str_func(a, b) {}
  String *val_str(String *str);
  const char *func_name() const { return "timediff"; }
  enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
  void fix_length_and_dec()
  {
    decimals=0;
829
    max_length=MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
830
    maybe_null= 1;
unknown's avatar
unknown committed
831
  }
unknown's avatar
unknown committed
832
  Field *tmp_table_field(TABLE *table)
unknown's avatar
unknown committed
833
  {
unknown's avatar
unknown committed
834
    return tmp_table_field_from_field_type(table, 0);
unknown's avatar
unknown committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848
  }
};

class Item_func_maketime :public Item_str_func
{
public:
  Item_func_maketime(Item *a, Item *b, Item *c)
    :Item_str_func(a, b ,c) {}
  String *val_str(String *str);
  const char *func_name() const { return "maketime"; }
  enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
  void fix_length_and_dec()
  {
    decimals=0;
849
    max_length=MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
unknown's avatar
unknown committed
850
  }
unknown's avatar
unknown committed
851
  Field *tmp_table_field(TABLE *table)
unknown's avatar
unknown committed
852
  {
unknown's avatar
unknown committed
853
    return tmp_table_field_from_field_type(table, 0);
unknown's avatar
unknown committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
  }
};

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;
  }
};
869 870


871 872 873 874 875 876
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) {}
877
  const char *func_name() const { return "timestampdiff"; }
878 879 880 881 882 883 884 885
  longlong val_int();
  void fix_length_and_dec()
  {
    decimals=0;
    maybe_null=1;
  }
  void print(String *str);
};
unknown's avatar
unknown committed
886 887


888
enum date_time_format
889 890 891 892 893 894 895
{
  USA_FORMAT, JIS_FORMAT, ISO_FORMAT, EUR_FORMAT, INTERNAL_FORMAT
};

class Item_func_get_format :public Item_str_func
{
public:
896
  const timestamp_type type; // keep it public
897 898 899
  Item_func_get_format(timestamp_type type_arg, Item *a)
    :Item_str_func(a), type(type_arg)
  {}
900 901 902 903
  String *val_str(String *str);
  const char *func_name() const { return "get_format"; }
  void fix_length_and_dec()
  {
904
    maybe_null= 1;
905 906 907
    decimals=0;
    max_length=17*MY_CHARSET_BIN_MB_MAXLEN;
  }
908
  void print(String *str);
909 910 911
};


912
class Item_func_str_to_date :public Item_str_func
913
{
914 915 916 917
  enum_field_types cached_field_type;
  date_time_format_types cached_format_type;
  timestamp_type cached_timestamp_type;
  bool const_item;
918 919
public:
  Item_func_str_to_date(Item *a, Item *b)
920
    :Item_str_func(a, b)
921
  {}
922
  String *val_str(String *str);
923
  bool get_date(TIME *ltime, uint fuzzy_date);
924
  const char *func_name() const { return "str_to_date"; }
925 926
  enum_field_types field_type() const { return cached_field_type; }
  void fix_length_and_dec();
unknown's avatar
unknown committed
927 928 929 930
  Field *tmp_table_field(TABLE *table)
  {
    return tmp_table_field_from_field_type(table, 1);
  }
931
};
unknown's avatar
unknown committed
932

933 934

class Item_func_last_day :public Item_date
unknown's avatar
unknown committed
935 936
{
public:
937
  Item_func_last_day(Item *a) :Item_date(a) {}
unknown's avatar
unknown committed
938
  const char *func_name() const { return "last_day"; }
939
  bool get_date(TIME *res, uint fuzzy_date);
unknown's avatar
unknown committed
940
};