item_timefunc.h 14.4 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 20 21 22 23 24 25 26 27 28 29
   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 */

#ifdef __GNUC__
#pragma interface			/* gcc class implementation */
#endif

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"; }
30 31 32 33
  void fix_length_and_dec() 
  { 
    max_length=6*thd_charset()->mbmaxlen;
  }
unknown's avatar
unknown committed
34 35 36 37 38 39 40 41 42
};


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"; }
43 44 45 46 47
  void fix_length_and_dec()
  { 
    decimals=0;
    max_length=6*thd_charset()->mbmaxlen;
  }
unknown's avatar
unknown committed
48 49 50 51 52 53 54 55 56
};


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"; }
57 58 59 60 61 62
  void fix_length_and_dec() 
  { 
    decimals=0; 
    max_length=6*thd_charset()->mbmaxlen;
    maybe_null=1; 
  }
unknown's avatar
unknown committed
63 64 65 66 67 68 69 70 71
};


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"; }
72 73 74 75 76 77
  void fix_length_and_dec() 
  { 
    decimals=0; 
    max_length=2*thd_charset()->mbmaxlen;
    maybe_null=1; 
  }
unknown's avatar
unknown committed
78 79 80 81 82 83 84 85 86
};


class Item_func_month :public Item_func
{
public:
  Item_func_month(Item *a) :Item_func(a) {}
  longlong val_int();
  double val() { return (double) Item_func_month::val_int(); }
87 88
  String *val_str(String *str) 
  {
89
    str->set(val_int(), thd_charset());
90 91
    return null_value ? 0 : str;
  }
unknown's avatar
unknown committed
92 93
  const char *func_name() const { return "month"; }
  enum Item_result result_type () const { return INT_RESULT; }
94 95 96 97 98 99 100
  void fix_length_and_dec() 
  { 
    set_charset(thd_charset());
    decimals=0;
    max_length=2*thd_charset()->mbmaxlen;
    maybe_null=1; 
  }
unknown's avatar
unknown committed
101 102
};

103

unknown's avatar
unknown committed
104 105 106 107 108 109 110
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; }
111
  void fix_length_and_dec() 
112 113 114
  {
    set_charset(thd_charset());
    decimals=0;
115 116 117
    max_length=10*thd_charset()->mbmaxlen;
    maybe_null=1; 
  }
unknown's avatar
unknown committed
118 119 120 121 122 123 124 125 126
};


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"; }
127 128 129 130 131 132
  void fix_length_and_dec() 
  { 
    decimals=0;
    max_length=3*thd_charset()->mbmaxlen;
    maybe_null=1; 
  }
unknown's avatar
unknown committed
133 134 135 136 137 138 139 140 141
};


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"; }
142 143 144 145 146 147
  void fix_length_and_dec()
  {
    decimals=0;
    max_length=2*thd_charset()->mbmaxlen;
    maybe_null=1;
  }
unknown's avatar
unknown committed
148 149 150 151 152 153 154 155 156
};


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"; }
157 158 159 160 161 162
  void fix_length_and_dec()
  {
    decimals=0;
    max_length=2*thd_charset()->mbmaxlen;
    maybe_null=1;
  }
unknown's avatar
unknown committed
163 164 165 166 167 168 169 170 171
};


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"; }
172 173 174 175 176 177
  void fix_length_and_dec()
  { 
     decimals=0;
     max_length=1*thd_charset()->mbmaxlen;
     maybe_null=1;
  }
unknown's avatar
unknown committed
178 179 180 181 182 183 184 185 186
};


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"; }
187 188 189 190 191 192
  void fix_length_and_dec() 
  { 
    decimals=0;
    max_length=2*thd_charset()->mbmaxlen;
    maybe_null=1;
  }
unknown's avatar
unknown committed
193 194 195 196 197 198 199 200 201
};


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"; }
202 203 204 205 206 207
  void fix_length_and_dec()
  { 
    decimals=0;
    max_length=2*thd_charset()->mbmaxlen;
    maybe_null=1;
  }
unknown's avatar
unknown committed
208 209 210 211 212 213 214 215
};

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"; }
216 217 218 219 220 221
  void fix_length_and_dec()
  { 
    decimals=0;
    max_length=6*thd_charset()->mbmaxlen;
    maybe_null=1;
  }
unknown's avatar
unknown committed
222 223 224 225 226 227 228 229 230
};


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"; }
231 232 233 234 235 236
  void fix_length_and_dec()
  { 
    decimals=0;
    max_length=4*thd_charset()->mbmaxlen;
    maybe_null=1;
  }
unknown's avatar
unknown committed
237 238 239 240 241 242 243 244 245 246 247
};


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();
  double val() { return (double) val_int(); }
248 249
  String *val_str(String *str)
  { 
250
    str->set(val_int(), thd_charset());
251 252
    return null_value ? 0 : str;
  }
unknown's avatar
unknown committed
253 254
  const char *func_name() const { return "weekday"; }
  enum Item_result result_type () const { return INT_RESULT; }
255 256 257 258 259 260 261
  void fix_length_and_dec()
  {
    set_charset(thd_charset());
    decimals=0;
    max_length=1*thd_charset()->mbmaxlen;
    maybe_null=1;
  }
unknown's avatar
unknown committed
262 263 264 265 266 267 268 269 270
};

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; }
271 272
  void fix_length_and_dec() 
  { 
273
    set_charset(thd_charset());
274 275 276 277
    decimals=0; 
    max_length=9*thd_charset()->mbmaxlen;
    maybe_null=1; 
  }
unknown's avatar
unknown committed
278 279 280 281 282 283 284 285 286 287 288 289 290
};


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();
  const char *func_name() const { return "timestamp"; }
  void fix_length_and_dec()
  {
291 292
    decimals=0;
    max_length=10*thd_charset()->mbmaxlen;
unknown's avatar
unknown committed
293 294 295 296 297 298 299 300 301 302 303 304
  }
};


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()
  {
305 306
    decimals=0;
    max_length=10*thd_charset()->mbmaxlen;
unknown's avatar
unknown committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
  }
};


/* This can't be a Item_str_func, because the val() functions are special */

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; }
  String *val_str(String *str);
  double val() { return (double) val_int(); }
  const char *func_name() const { return "date"; }
322 323 324 325 326 327
  void fix_length_and_dec()
  { 
    set_charset(thd_charset());
    decimals=0;
    max_length=10*thd_charset()->mbmaxlen;
  }
328
  int  save_in_field(Field *to);
329 330 331 332
  void make_field(Send_field *tmp_field)
  {
    init_make_field(tmp_field,FIELD_TYPE_DATE);
  }
333 334
  Field *tmp_table_field(TABLE *t_arg)
  {
335
    return (!t_arg) ? result_field : new Field_date(maybe_null, name, t_arg, thd_charset());
336
  }  
337 338 339 340 341 342 343 344 345 346 347 348 349
};


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) {}
  void make_field(Send_field *tmp_field)
  {
    init_make_field(tmp_field,FIELD_TYPE_DATETIME);
  }
350 351
  Field *tmp_table_field(TABLE *t_arg)
  {
352
    return  (!t_arg) ? result_field : new Field_datetime(maybe_null, name,
353
							 t_arg, thd_charset());
354
  }
unknown's avatar
unknown committed
355 356 357 358 359 360
};


class Item_func_curtime :public Item_func
{
  longlong value;
361
  char buff[9*2+32];
unknown's avatar
unknown committed
362 363 364 365 366 367 368
  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; }
  double val() { return (double) value; }
  longlong val_int() { return value; }
369
  String *val_str(String *str);
unknown's avatar
unknown committed
370 371
  const char *func_name() const { return "curtime"; }
  void fix_length_and_dec();
372 373 374 375
  void make_field(Send_field *tmp_field)
  {
    init_make_field(tmp_field,FIELD_TYPE_TIME);
  }
376 377
  Field *tmp_table_field(TABLE *t_arg)
  {
378
    return (!t_arg) ? result_field : 
379
    		      new Field_time(maybe_null, name, t_arg, thd_charset());
380
  }  
unknown's avatar
unknown committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
};


class Item_func_curdate :public Item_date
{
  longlong value;
  TIME ltime;
public:
  Item_func_curdate() :Item_date() {}
  longlong val_int() { return (value) ; }
  const char *func_name() const { return "curdate"; }
  void fix_length_and_dec();			/* Retrieves curtime */
  bool get_date(TIME *res,bool fuzzy_date);
};


397
class Item_func_now :public Item_date_func
unknown's avatar
unknown committed
398 399
{
  longlong value;
400
  char buff[20*2+32];	// +32 to make my_snprintf_{8bit|ucs2} happy
unknown's avatar
unknown committed
401 402 403
  uint buff_length;
  TIME ltime;
public:
404 405
  Item_func_now() :Item_date_func() {}
  Item_func_now(Item *a) :Item_date_func(a) {}
unknown's avatar
unknown committed
406 407 408
  enum Item_result result_type () const { return STRING_RESULT; }
  double val()	     { return (double) value; }
  longlong val_int() { return value; }
409
  int  save_in_field(Field *to);
410
  String *val_str(String *str);
unknown's avatar
unknown committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
  const char *func_name() const { return "now"; }
  void fix_length_and_dec();
  bool get_date(TIME *res,bool fuzzy_date);
};


class Item_func_from_days :public Item_date
{
public:
  Item_func_from_days(Item *a) :Item_date(a) {}
  longlong val_int();
  const char *func_name() const { return "from_days"; }
};


class Item_func_date_format :public Item_str_func
{
  int fixed_length;
  const bool date_or_time;
430
  String value;
unknown's avatar
unknown committed
431 432 433 434 435 436 437 438 439 440
public:
  Item_func_date_format(Item *a,Item *b,bool date_or_time_arg)
    :Item_str_func(a,b),date_or_time(date_or_time_arg) {}
  String *val_str(String *str);
  const char *func_name() const { return "date_format"; }
  void fix_length_and_dec();
  uint format_length(const String *format);
};


441
class Item_func_from_unixtime :public Item_date_func
unknown's avatar
unknown committed
442 443
{
 public:
444
  Item_func_from_unixtime(Item *a) :Item_date_func(a) {}
unknown's avatar
unknown committed
445 446 447 448
  double val() { return (double) Item_func_from_unixtime::val_int(); }
  longlong val_int();
  String *val_str(String *str);
  const char *func_name() const { return "from_unixtime"; }
449 450 451 452 453 454
  void fix_length_and_dec()
  { 
    set_charset(thd_charset());
    decimals=0;
    max_length=19*thd_charset()->mbmaxlen;
  }
455
//  enum Item_result result_type () const { return STRING_RESULT; }
unknown's avatar
unknown committed
456 457 458 459 460 461 462 463 464 465 466
  bool get_date(TIME *res,bool fuzzy_date);
};


class Item_func_sec_to_time :public Item_str_func
{
public:
  Item_func_sec_to_time(Item *item) :Item_str_func(item) {}
  double val() { return (double) Item_func_sec_to_time::val_int(); }
  longlong val_int();
  String *val_str(String *);
467 468 469 470 471 472
  void fix_length_and_dec()
  { 
    set_charset(thd_charset());
    maybe_null=1;
    max_length=13*thd_charset()->mbmaxlen;
  }
unknown's avatar
unknown committed
473
  const char *func_name() const { return "sec_to_time"; }
474 475 476 477
  void make_field(Send_field *tmp_field)
  {
    init_make_field(tmp_field,FIELD_TYPE_TIME);
  }
478 479
  Field *tmp_table_field(TABLE *t_arg)
  {
480
    return (!t_arg) ? result_field : 
481
    		      new Field_time(maybe_null, name, t_arg, thd_charset());
482
  }
unknown's avatar
unknown committed
483 484
};

485

unknown's avatar
unknown committed
486 487 488 489 490 491 492
enum interval_type { INTERVAL_YEAR, INTERVAL_MONTH, INTERVAL_DAY,
		     INTERVAL_HOUR, INTERVAL_MINUTE, INTERVAL_SECOND,
		     INTERVAL_YEAR_MONTH, INTERVAL_DAY_HOUR,
		     INTERVAL_DAY_MINUTE, INTERVAL_DAY_SECOND,
		     INTERVAL_HOUR_MINUTE, INTERVAL_HOUR_SECOND,
		     INTERVAL_MINUTE_SECOND};

493
class Item_date_add_interval :public Item_date_func
unknown's avatar
unknown committed
494 495 496 497 498 499 500
{
  const interval_type int_type;
  String value;
  const bool date_sub_interval;

public:
  Item_date_add_interval(Item *a,Item *b,interval_type type_arg,bool neg_arg)
501
    :Item_date_func(a,b),int_type(type_arg), date_sub_interval(neg_arg) {}
unknown's avatar
unknown committed
502 503
  String *val_str(String *);
  const char *func_name() const { return "date_add_interval"; }
504 505 506 507 508 509 510
  void fix_length_and_dec()
  {
    set_charset(thd_charset());
    maybe_null=1;
    max_length=19*thd_charset()->mbmaxlen;
    value.alloc(32);
  }
unknown's avatar
unknown committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
  double val() { return (double) val_int(); }
  longlong val_int();
  bool get_date(TIME *res,bool fuzzy_date);
};

class Item_extract :public Item_int_func
{
  const interval_type int_type;
  String value;
  bool date_value;
 public:
  Item_extract(interval_type type_arg, Item *a)
    :Item_int_func(a), int_type(type_arg) {}
  longlong val_int();
  const char *func_name() const { return "extract"; }
  void fix_length_and_dec();
};
528

unknown's avatar
unknown committed
529
class Item_typecast :public Item_str_func
530 531
{
public:
unknown's avatar
unknown committed
532
  Item_typecast(Item *a) :Item_str_func(a) {}
533 534
  String *val_str(String *a)
  { a=args[0]->val_str(a); null_value=args[0]->null_value; return a; }
535
  void fix_length_and_dec() { max_length=args[0]->max_length; }
unknown's avatar
unknown committed
536 537 538 539 540 541 542 543 544
  void print(String *str);
};


class Item_date_typecast :public Item_typecast
{
public:
  Item_date_typecast(Item *a) :Item_typecast(a) {}
  const char *func_name() const { return "date"; }
545 546 547 548 549 550
  void make_field(Send_field *tmp_field)
  {
    init_make_field(tmp_field,FIELD_TYPE_DATE);
  }
  Field *tmp_table_field(TABLE *t_arg)
  {
551
    return (!t_arg) ? result_field : 
552
    		      new Field_date(maybe_null, name, t_arg, thd_charset());
553 554 555
  }  
};

unknown's avatar
unknown committed
556
class Item_time_typecast :public Item_typecast
557 558
{
public:
unknown's avatar
unknown committed
559 560
  Item_time_typecast(Item *a) :Item_typecast(a) {}
  const char *func_name() const { return "time"; }
561 562 563 564 565 566
  void make_field(Send_field *tmp_field)
  {
    init_make_field(tmp_field,FIELD_TYPE_TIME);
  }
  Field *tmp_table_field(TABLE *t_arg)
  {
567
    return (!t_arg) ? result_field : 
568
    		      new Field_time(maybe_null, name, t_arg, thd_charset());
569
  }
570 571
};

unknown's avatar
unknown committed
572
class Item_datetime_typecast :public Item_typecast
573 574
{
public:
unknown's avatar
unknown committed
575 576
  Item_datetime_typecast(Item *a) :Item_typecast(a) {}
  const char *func_name() const { return "datetime"; }
577 578 579 580 581 582
  void make_field(Send_field *tmp_field)
  {
    init_make_field(tmp_field,FIELD_TYPE_DATETIME);
  }
  Field *tmp_table_field(TABLE *t_arg)
  {
583
    return (!t_arg) ? result_field : new Field_datetime(maybe_null, name,
584
							t_arg, thd_charset());
585
  }
586
};