1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21
17
/* Function items used by mysql */
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface /* gcc class implementation */
23
23
enum date_time_format_types
25
25
TIME_ONLY= 0, TIME_MICROSECOND, DATE_ONLY, DATE_TIME, DATE_TIME_MICROSECOND
70
70
enum_monotonicity_info get_monotonicity_info() const;
71
int64_t val_int_endpoint(bool left_endp, bool *incl_endp);
72
bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
71
longlong val_int_endpoint(bool left_endp, bool *incl_endp);
97
93
Item_func_month(Item *a) :Item_func(a) {}
100
{ assert(fixed == 1); return (double) Item_func_month::val_int(); }
96
{ DBUG_ASSERT(fixed == 1); return (double) Item_func_month::val_int(); }
101
97
String *val_str(String *str)
103
99
str->set(val_int(), &my_charset_bin);
244
238
Item_func_year(Item *a) :Item_int_func(a) {}
246
240
const char *func_name() const { return "year"; }
247
241
enum_monotonicity_info get_monotonicity_info() const;
248
int64_t val_int_endpoint(bool left_endp, bool *incl_endp);
242
longlong val_int_endpoint(bool left_endp, bool *incl_endp);
249
243
void fix_length_and_dec()
262
256
Item_func_weekday(Item *a,bool type_arg)
263
257
:Item_func(a), odbc_type(type_arg) {}
265
double val_real() { assert(fixed == 1); return (double) val_int(); }
259
double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
266
260
String *val_str(String *str)
262
DBUG_ASSERT(fixed == 1);
269
263
str->set(val_int(), &my_charset_bin);
270
264
return null_value ? 0 : str;
307
301
Item_func_unix_timestamp() :Item_int_func() {}
308
302
Item_func_unix_timestamp(Item *a) :Item_int_func(a) {}
310
304
const char *func_name() const { return "unix_timestamp"; }
311
305
void fix_length_and_dec()
314
308
max_length=10*MY_CHARSET_BIN_MB_MAXLEN;
316
bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
342
334
Item_date() :Item_func() {}
343
335
Item_date(Item *a) :Item_func(a) {}
344
336
enum Item_result result_type () const { return STRING_RESULT; }
345
enum_field_types field_type() const { return DRIZZLE_TYPE_NEWDATE; }
337
enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
346
338
String *val_str(String *str);
348
340
double val_real() { return val_real_from_decimal(); }
349
341
const char *func_name() const { return "date"; }
350
342
void fix_length_and_dec()
354
346
max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
356
Field *tmp_table_field(Table *table)
348
Field *tmp_table_field(TABLE *table)
358
350
return tmp_table_field_from_field_type(table, 0);
360
bool result_as_int64_t() { return true; }
352
bool result_as_longlong() { return TRUE; }
361
353
my_decimal *val_decimal(my_decimal *decimal_value)
355
DBUG_ASSERT(fixed == 1);
364
356
return val_decimal_from_date(decimal_value);
366
int save_in_field(Field *field,
367
bool no_conversions __attribute__((unused)))
358
int save_in_field(Field *field, bool no_conversions)
369
360
return save_date_in_field(field);
378
369
Item_date_func(Item *a) :Item_str_func(a) {}
379
370
Item_date_func(Item *a,Item *b) :Item_str_func(a,b) {}
380
371
Item_date_func(Item *a,Item *b, Item *c) :Item_str_func(a,b,c) {}
381
enum_field_types field_type() const { return DRIZZLE_TYPE_DATETIME; }
382
Field *tmp_table_field(Table *table)
372
enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
373
Field *tmp_table_field(TABLE *table)
384
375
return tmp_table_field_from_field_type(table, 0);
386
bool result_as_int64_t() { return true; }
377
bool result_as_longlong() { return TRUE; }
387
378
double val_real() { return (double) val_int(); }
388
379
my_decimal *val_decimal(my_decimal *decimal_value)
381
DBUG_ASSERT(fixed == 1);
391
382
return val_decimal_from_date(decimal_value);
393
int save_in_field(Field *field,
394
bool no_conversions __attribute__((unused)))
384
int save_in_field(Field *field, bool no_conversions)
396
386
return save_date_in_field(field);
405
395
Item_str_timefunc(Item *a) :Item_str_func(a) {}
406
396
Item_str_timefunc(Item *a,Item *b) :Item_str_func(a,b) {}
407
397
Item_str_timefunc(Item *a, Item *b, Item *c) :Item_str_func(a, b ,c) {}
408
enum_field_types field_type() const { return DRIZZLE_TYPE_TIME; }
398
enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
409
399
void fix_length_and_dec()
411
401
decimals= DATETIME_DEC;
412
402
max_length=MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
414
Field *tmp_table_field(Table *table)
404
Field *tmp_table_field(TABLE *table)
416
406
return tmp_table_field_from_field_type(table, 0);
418
408
double val_real() { return val_real_from_decimal(); }
419
409
my_decimal *val_decimal(my_decimal *decimal_value)
411
DBUG_ASSERT(fixed == 1);
422
412
return val_decimal_from_time(decimal_value);
424
int save_in_field(Field *field,
425
bool no_conversions __attribute__((unused)))
414
int save_in_field(Field *field, bool no_conversions)
427
416
return save_time_in_field(field);
434
423
class Item_func_curtime :public Item_str_timefunc
437
426
char buff[9*2+32];
438
uint32_t buff_length;
440
429
Item_func_curtime() :Item_str_timefunc() {}
441
430
Item_func_curtime(Item *a) :Item_str_timefunc(a) {}
442
double val_real() { assert(fixed == 1); return (double) value; }
443
int64_t val_int() { assert(fixed == 1); return value; }
431
double val_real() { DBUG_ASSERT(fixed == 1); return (double) value; }
432
longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
444
433
String *val_str(String *str);
445
434
void fix_length_and_dec();
447
436
Abstract method that defines which time zone is used for conversion.
448
437
Converts time current time in my_time_t representation to broken-down
449
DRIZZLE_TIME representation using UTC-SYSTEM or per-thread time zone.
438
MYSQL_TIME representation using UTC-SYSTEM or per-thread time zone.
451
virtual void store_now_in_TIME(DRIZZLE_TIME *now_time)=0;
452
bool result_as_int64_t() { return true; }
453
bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
440
virtual void store_now_in_TIME(MYSQL_TIME *now_time)=0;
441
bool result_as_longlong() { return TRUE; }
461
448
Item_func_curtime_local() :Item_func_curtime() {}
462
449
Item_func_curtime_local(Item *a) :Item_func_curtime(a) {}
463
450
const char *func_name() const { return "curtime"; }
464
virtual void store_now_in_TIME(DRIZZLE_TIME *now_time);
451
virtual void store_now_in_TIME(MYSQL_TIME *now_time);
471
458
Item_func_curtime_utc() :Item_func_curtime() {}
472
459
Item_func_curtime_utc(Item *a) :Item_func_curtime(a) {}
473
460
const char *func_name() const { return "utc_time"; }
474
virtual void store_now_in_TIME(DRIZZLE_TIME *now_time);
461
virtual void store_now_in_TIME(MYSQL_TIME *now_time);
480
467
class Item_func_curdate :public Item_date
485
472
Item_func_curdate() :Item_date() {}
486
int64_t val_int() { assert(fixed == 1); return (value) ; }
473
longlong val_int() { DBUG_ASSERT(fixed == 1); return (value) ; }
487
474
String *val_str(String *str);
488
475
void fix_length_and_dec();
489
bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
490
virtual void store_now_in_TIME(DRIZZLE_TIME *now_time)=0;
491
bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
476
bool get_date(MYSQL_TIME *res, uint fuzzy_date);
477
virtual void store_now_in_TIME(MYSQL_TIME *now_time)=0;
516
501
class Item_func_now :public Item_date_func
520
505
char buff[20*2+32]; // +32 to make my_snprintf_{8bit|ucs2} happy
521
uint32_t buff_length;
524
509
Item_func_now() :Item_date_func() {}
525
510
Item_func_now(Item *a) :Item_date_func(a) {}
526
511
enum Item_result result_type () const { return STRING_RESULT; }
527
int64_t val_int() { assert(fixed == 1); return value; }
512
longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
528
513
int save_in_field(Field *to, bool no_conversions);
529
514
String *val_str(String *str);
530
515
void fix_length_and_dec();
531
bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
532
virtual void store_now_in_TIME(DRIZZLE_TIME *now_time)=0;
533
bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
516
bool get_date(MYSQL_TIME *res, uint fuzzy_date);
517
virtual void store_now_in_TIME(MYSQL_TIME *now_time)=0;
541
524
Item_func_now_local() :Item_func_now() {}
542
525
Item_func_now_local(Item *a) :Item_func_now(a) {}
543
526
const char *func_name() const { return "now"; }
544
virtual void store_now_in_TIME(DRIZZLE_TIME *now_time);
527
virtual void store_now_in_TIME(MYSQL_TIME *now_time);
545
528
virtual enum Functype functype() const { return NOW_FUNC; }
552
535
Item_func_now_utc() :Item_func_now() {}
553
536
Item_func_now_utc(Item *a) :Item_func_now(a) {}
554
537
const char *func_name() const { return "utc_timestamp"; }
555
virtual void store_now_in_TIME(DRIZZLE_TIME *now_time);
538
virtual void store_now_in_TIME(MYSQL_TIME *now_time);
567
550
Item_func_sysdate_local(Item *a) :Item_func_now(a) {}
568
551
bool const_item() const { return 0; }
569
552
const char *func_name() const { return "sysdate"; }
570
void store_now_in_TIME(DRIZZLE_TIME *now_time);
553
void store_now_in_TIME(MYSQL_TIME *now_time);
571
554
double val_real();
573
556
int save_in_field(Field *to, bool no_conversions);
574
557
String *val_str(String *str);
575
558
void fix_length_and_dec();
576
bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
559
bool get_date(MYSQL_TIME *res, uint fuzzy_date);
577
560
void update_used_tables()
579
562
Item_func_now::update_used_tables();
588
571
Item_func_from_days(Item *a) :Item_date(a) {}
589
572
const char *func_name() const { return "from_days"; }
590
bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
591
bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
573
bool get_date(MYSQL_TIME *res, uint fuzzy_date);
605
586
const char *func_name() const
606
587
{ return is_time_format ? "time_format" : "date_format"; }
607
588
void fix_length_and_dec();
608
uint32_t format_length(const String *format);
589
uint format_length(const String *format);
609
590
bool eq(const Item *item, bool binary_cmp) const;
617
598
Item_func_from_unixtime(Item *a) :Item_date_func(a) {}
619
600
String *val_str(String *str);
620
601
const char *func_name() const { return "from_unixtime"; }
621
602
void fix_length_and_dec();
622
bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
603
bool get_date(MYSQL_TIME *res, uint fuzzy_date);
651
632
Item_func_convert_tz(Item *a, Item *b, Item *c):
652
633
Item_date_func(a, b, c), from_tz_cached(0), to_tz_cached(0) {}
654
635
String *val_str(String *str);
655
636
const char *func_name() const { return "convert_tz"; }
656
637
void fix_length_and_dec();
657
bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
638
bool get_date(MYSQL_TIME *res, uint fuzzy_date);
695
676
const char *func_name() const { return "date_add_interval"; }
696
677
void fix_length_and_dec();
697
678
enum_field_types field_type() const { return cached_field_type; }
699
bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
680
bool get_date(MYSQL_TIME *res, uint fuzzy_date);
700
681
bool eq(const Item *item, bool binary_cmp) const;
701
682
virtual void print(String *str, enum_query_type query_type);
710
691
const interval_type int_type; // keep it public
711
692
Item_extract(interval_type type_arg, Item *a)
712
693
:Item_int_func(a), int_type(type_arg) {}
714
695
enum Functype functype() const { return EXTRACT_FUNC; }
715
696
const char *func_name() const { return "extract"; }
716
697
void fix_length_and_dec();
758
739
class Item_char_typecast :public Item_typecast
761
const CHARSET_INFO *cast_cs, *from_cs;
742
CHARSET_INFO *cast_cs, *from_cs;
762
743
bool charset_conversion;
763
744
String tmp_value;
765
Item_char_typecast(Item *a, int length_arg, const CHARSET_INFO * const cs_arg)
746
Item_char_typecast(Item *a, int length_arg, CHARSET_INFO *cs_arg)
766
747
:Item_typecast(a), cast_length(length_arg), cast_cs(cs_arg) {}
767
748
enum Functype functype() const { return CHAR_TYPECAST_FUNC; }
768
749
bool eq(const Item *item, bool binary_cmp) const;
780
761
Item_date_typecast(Item *a) :Item_typecast_maybe_null(a) {}
781
762
const char *func_name() const { return "cast_as_date"; }
782
763
String *val_str(String *str);
783
bool get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date);
784
bool get_time(DRIZZLE_TIME *ltime);
764
bool get_date(MYSQL_TIME *ltime, uint fuzzy_date);
765
bool get_time(MYSQL_TIME *ltime);
785
766
const char *cast_type() const { return "date"; }
786
enum_field_types field_type() const { return DRIZZLE_TYPE_NEWDATE; }
787
Field *tmp_table_field(Table *table)
767
enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
768
Field *tmp_table_field(TABLE *table)
789
770
return tmp_table_field_from_field_type(table, 0);
797
bool result_as_int64_t() { return true; }
778
bool result_as_longlong() { return TRUE; }
799
780
double val_real() { return (double) val_int(); }
800
781
my_decimal *val_decimal(my_decimal *decimal_value)
783
DBUG_ASSERT(fixed == 1);
803
784
return val_decimal_from_date(decimal_value);
805
int save_in_field(Field *field,
806
bool no_conversions __attribute__((unused)))
786
int save_in_field(Field *field, bool no_conversions)
808
788
return save_date_in_field(field);
816
796
Item_time_typecast(Item *a) :Item_typecast_maybe_null(a) {}
817
797
const char *func_name() const { return "cast_as_time"; }
818
798
String *val_str(String *str);
819
bool get_time(DRIZZLE_TIME *ltime);
799
bool get_time(MYSQL_TIME *ltime);
820
800
const char *cast_type() const { return "time"; }
821
enum_field_types field_type() const { return DRIZZLE_TYPE_TIME; }
822
Field *tmp_table_field(Table *table)
801
enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
802
Field *tmp_table_field(TABLE *table)
824
804
return tmp_table_field_from_field_type(table, 0);
826
bool result_as_int64_t() { return true; }
806
bool result_as_longlong() { return TRUE; }
828
808
double val_real() { return val_real_from_decimal(); }
829
809
my_decimal *val_decimal(my_decimal *decimal_value)
811
DBUG_ASSERT(fixed == 1);
832
812
return val_decimal_from_time(decimal_value);
834
int save_in_field(Field *field,
835
bool no_conversions __attribute__((unused)))
814
int save_in_field(Field *field, bool no_conversions)
837
816
return save_time_in_field(field);
846
825
const char *func_name() const { return "cast_as_datetime"; }
847
826
String *val_str(String *str);
848
827
const char *cast_type() const { return "datetime"; }
849
enum_field_types field_type() const { return DRIZZLE_TYPE_DATETIME; }
850
Field *tmp_table_field(Table *table)
828
enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
829
Field *tmp_table_field(TABLE *table)
852
831
return tmp_table_field_from_field_type(table, 0);
858
837
max_length= MAX_DATETIME_FULL_WIDTH * MY_CHARSET_BIN_MB_MAXLEN;
859
838
decimals= DATETIME_DEC;
861
bool result_as_int64_t() { return true; }
840
bool result_as_longlong() { return TRUE; }
863
842
double val_real() { return val_real_from_decimal(); }
864
843
double val() { return (double) val_int(); }
865
844
my_decimal *val_decimal(my_decimal *decimal_value)
846
DBUG_ASSERT(fixed == 1);
868
847
return val_decimal_from_date(decimal_value);
870
int save_in_field(Field *field,
871
bool no_conversions __attribute__((unused)))
849
int save_in_field(Field *field, bool no_conversions)
873
851
return save_date_in_field(field);
880
858
Item_func_makedate(Item *a,Item *b) :Item_date_func(a,b) {}
881
859
String *val_str(String *str);
882
860
const char *func_name() const { return "makedate"; }
883
enum_field_types field_type() const { return DRIZZLE_TYPE_NEWDATE; }
861
enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
884
862
void fix_length_and_dec()
887
865
max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
903
881
enum_field_types field_type() const { return cached_field_type; }
904
882
void fix_length_and_dec();
906
Field *tmp_table_field(Table *table)
884
Field *tmp_table_field(TABLE *table)
908
886
return tmp_table_field_from_field_type(table, 0);
912
890
double val_real() { return val_real_from_decimal(); }
913
891
my_decimal *val_decimal(my_decimal *decimal_value)
916
if (cached_field_type == DRIZZLE_TYPE_TIME)
893
DBUG_ASSERT(fixed == 1);
894
if (cached_field_type == MYSQL_TYPE_TIME)
917
895
return val_decimal_from_time(decimal_value);
918
if (cached_field_type == DRIZZLE_TYPE_DATETIME)
896
if (cached_field_type == MYSQL_TYPE_DATETIME)
919
897
return val_decimal_from_date(decimal_value);
920
898
return Item_str_func::val_decimal(decimal_value);
922
900
int save_in_field(Field *field, bool no_conversions)
924
if (cached_field_type == DRIZZLE_TYPE_TIME)
902
if (cached_field_type == MYSQL_TYPE_TIME)
925
903
return save_time_in_field(field);
926
if (cached_field_type == DRIZZLE_TYPE_DATETIME)
904
if (cached_field_type == MYSQL_TYPE_DATETIME)
927
905
return save_date_in_field(field);
928
906
return Item_str_func::save_in_field(field, no_conversions);
949
927
Item_func_maketime(Item *a, Item *b, Item *c)
950
928
:Item_str_timefunc(a, b, c)
954
932
String *val_str(String *str);
955
933
const char *func_name() const { return "maketime"; }
976
954
Item_func_timestamp_diff(Item *a,Item *b,interval_type type_arg)
977
955
:Item_int_func(a,b), int_type(type_arg) {}
978
956
const char *func_name() const { return "timestampdiff"; }
980
958
void fix_length_and_dec()
994
972
class Item_func_get_format :public Item_str_func
997
const enum enum_drizzle_timestamp_type type; // keep it public
998
Item_func_get_format(enum enum_drizzle_timestamp_type type_arg, Item *a)
975
const timestamp_type type; // keep it public
976
Item_func_get_format(timestamp_type type_arg, Item *a)
999
977
:Item_str_func(a), type(type_arg)
1001
979
String *val_str(String *str);
1015
993
enum_field_types cached_field_type;
1016
994
date_time_format_types cached_format_type;
1017
enum enum_drizzle_timestamp_type cached_timestamp_type;
995
timestamp_type cached_timestamp_type;
1018
996
bool const_item;
1020
998
Item_func_str_to_date(Item *a, Item *b)
1021
999
:Item_str_func(a, b), const_item(false)
1023
1001
String *val_str(String *str);
1024
bool get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date);
1002
bool get_date(MYSQL_TIME *ltime, uint fuzzy_date);
1025
1003
const char *func_name() const { return "str_to_date"; }
1026
1004
enum_field_types field_type() const { return cached_field_type; }
1027
1005
void fix_length_and_dec();
1028
Field *tmp_table_field(Table *table)
1006
Field *tmp_table_field(TABLE *table)
1030
1008
return tmp_table_field_from_field_type(table, 1);
1038
1016
Item_func_last_day(Item *a) :Item_date(a) {}
1039
1017
const char *func_name() const { return "last_day"; }
1040
bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
1018
bool get_date(MYSQL_TIME *res, uint fuzzy_date);