item_timefunc.h 24.3 KB
Newer Older
bk@work.mysql.com's avatar
bk@work.mysql.com committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
2

bk@work.mysql.com's avatar
bk@work.mysql.com 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.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
7

bk@work.mysql.com's avatar
bk@work.mysql.com 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.
monty@hundin.mysql.fi's avatar
monty@hundin.mysql.fi committed
12

bk@work.mysql.com's avatar
bk@work.mysql.com 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
bk@work.mysql.com's avatar
bk@work.mysql.com 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);

bk@work.mysql.com's avatar
bk@work.mysql.com 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
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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; 
  }
71
  enum_monotonicity_info get_monotonicity_info() const;
bk@work.mysql.com's avatar
bk@work.mysql.com 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; 
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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; 
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
111 112
};

113

bk@work.mysql.com's avatar
bk@work.mysql.com 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; 
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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; 
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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"; }
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;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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");
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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; 
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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"; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
304 305
  void fix_length_and_dec()
  {
306
    decimals=0;
307
    max_length=10*MY_CHARSET_BIN_MB_MAXLEN;
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
322 323 324 325
  }
};


326 327 328
/*
  This can't be a Item_str_func, because the val_real() functions are special
*/
bk@work.mysql.com's avatar
bk@work.mysql.com 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; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
337
  String *val_str(String *str);
338
  longlong val_int();
339
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
bk@work.mysql.com's avatar
bk@work.mysql.com 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
  }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
347
  int save_in_field(Field *to, bool no_conversions);
348
  Field *tmp_table_field(TABLE *table)
349
  {
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; }
363
  Field *tmp_table_field(TABLE *table)
364
  {
365
    return tmp_table_field_from_field_type(table, 0);
366
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
367 368 369
};


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

bk@work.mysql.com's avatar
bk@work.mysql.com committed
372 373 374
class Item_func_curtime :public Item_func
{
  longlong value;
375
  char buff[9*2+32];
bk@work.mysql.com's avatar
bk@work.mysql.com 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);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
385
  void fix_length_and_dec();
386
  Field *tmp_table_field(TABLE *table)
387
  {
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;
bk@work.mysql.com's avatar
bk@work.mysql.com 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. */

bk@work.mysql.com's avatar
bk@work.mysql.com 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);
bk@work.mysql.com's avatar
bk@work.mysql.com 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
bk@work.mysql.com's avatar
bk@work.mysql.com committed
456
{
457
protected:
bk@work.mysql.com's avatar
bk@work.mysql.com committed
458
  longlong value;
459
  char buff[20*2+32];	// +32 to make my_snprintf_{8bit|ucs2} happy
bk@work.mysql.com's avatar
bk@work.mysql.com 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) {}
bk@work.mysql.com's avatar
bk@work.mysql.com 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; }
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
468
  int save_in_field(Field *to, bool no_conversions);
469
  String *val_str(String *str);
bk@work.mysql.com's avatar
bk@work.mysql.com 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);
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
  }
};


bk@work.mysql.com's avatar
bk@work.mysql.com 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);
bk@work.mysql.com's avatar
bk@work.mysql.com 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;
bk@work.mysql.com's avatar
bk@work.mysql.com 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) {}
bk@work.mysql.com's avatar
bk@work.mysql.com committed
540
  String *val_str(String *str);
541 542
  const char *func_name() const
    { return is_time_format ? "time_format" : "date_format"; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
543 544
  void fix_length_and_dec();
  uint format_length(const String *format);
545
  bool eq(const Item *item, bool binary_cmp) const;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
546 547 548
};


549
class Item_func_from_unixtime :public Item_date_func
bk@work.mysql.com's avatar
bk@work.mysql.com committed
550
{
551
  THD *thd;
bk@work.mysql.com's avatar
bk@work.mysql.com 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();
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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();
bk@work.mysql.com's avatar
bk@work.mysql.com 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();
  }
bk@work.mysql.com's avatar
bk@work.mysql.com 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; }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
625
  const char *func_name() const { return "sec_to_time"; }
626
  Field *tmp_table_field(TABLE *table)
627
  {
628
    return tmp_table_field_from_field_type(table, 0);
629
  }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
630 631
};

632

633
class Item_date_add_interval :public Item_date_func
bk@work.mysql.com's avatar
bk@work.mysql.com committed
634 635
{
  String value;
636
  enum_field_types cached_field_type;
bk@work.mysql.com's avatar
bk@work.mysql.com committed
637 638

public:
639 640
  const interval_type int_type; // keep it public
  const bool date_sub_interval; // keep it public
bk@work.mysql.com's avatar
bk@work.mysql.com 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) {}
bk@work.mysql.com's avatar
bk@work.mysql.com 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(); }
bk@work.mysql.com's avatar
bk@work.mysql.com committed
648
  longlong val_int();
649
  bool get_date(TIME *res, uint fuzzy_date);
650
  bool eq(const Item *item, bool binary_cmp) const;
651
  void print(String *str);
bk@work.mysql.com's avatar
bk@work.mysql.com committed
652 653
};

654

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

671

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


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


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


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

748

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

764

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

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;
789
    max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
790
  }
791
  Field *tmp_table_field(TABLE *table)
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
792
  {
793
    return tmp_table_field_from_field_type(table, 0);
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
794 795 796
  }
};

797

gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
798 799
class Item_func_add_time :public Item_str_func
{
800
  const bool is_date;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
801 802 803 804
  int sign;
  enum_field_types cached_field_type;

public:
805 806
  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; }
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
807 808 809
  String *val_str(String *str);
  enum_field_types field_type() const { return cached_field_type; }
  void fix_length_and_dec();
810

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

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;
830
    max_length=MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
831
    maybe_null= 1;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
832
  }
833
  Field *tmp_table_field(TABLE *table)
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
834
  {
835
    return tmp_table_field_from_field_type(table, 0);
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
836 837 838 839 840 841 842 843 844 845 846 847 848 849
  }
};

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;
850
    max_length=MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
851
  }
852
  Field *tmp_table_field(TABLE *table)
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
853
  {
854
    return tmp_table_field_from_field_type(table, 0);
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
  }
};

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


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


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

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


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

934 935

class Item_func_last_day :public Item_date
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
936 937
{
public:
938
  Item_func_last_day(Item *a) :Item_date(a) {}
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
939
  const char *func_name() const { return "last_day"; }
940
  bool get_date(TIME *res, uint fuzzy_date);
gluh@gluh.mysql.r18.ru's avatar
gluh@gluh.mysql.r18.ru committed
941
};