~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_timefunc.h

  • Committer: Monty Taylor
  • Date: 2008-10-14 16:33:44 UTC
  • mfrom: (512 drizzle)
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 515.
  • Revision ID: monty@inaugust.com-20081014163344-9xprk712n9hmbrzr
MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 */
 
19
 
 
20
 
 
21
/* Function items used by mysql */
 
22
 
 
23
enum date_time_format_types 
 
24
 
25
  TIME_ONLY= 0, TIME_MICROSECOND, DATE_ONLY, DATE_TIME, DATE_TIME_MICROSECOND
 
26
};
 
27
 
 
28
bool get_interval_value(Item *args,interval_type int_type,
 
29
                               String *str_value, INTERVAL *interval);
 
30
 
 
31
class Item_func_period_add :public Item_int_func
 
32
{
 
33
public:
 
34
  Item_func_period_add(Item *a,Item *b) :Item_int_func(a,b) {}
 
35
  int64_t val_int();
 
36
  const char *func_name() const { return "period_add"; }
 
37
  void fix_length_and_dec() 
 
38
  { 
 
39
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
 
40
  }
 
41
};
 
42
 
 
43
 
 
44
class Item_func_period_diff :public Item_int_func
 
45
{
 
46
public:
 
47
  Item_func_period_diff(Item *a,Item *b) :Item_int_func(a,b) {}
 
48
  int64_t val_int();
 
49
  const char *func_name() const { return "period_diff"; }
 
50
  void fix_length_and_dec()
 
51
  { 
 
52
    decimals=0;
 
53
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
 
54
  }
 
55
};
 
56
 
 
57
 
 
58
class Item_func_to_days :public Item_int_func
 
59
{
 
60
public:
 
61
  Item_func_to_days(Item *a) :Item_int_func(a) {}
 
62
  int64_t val_int();
 
63
  const char *func_name() const { return "to_days"; }
 
64
  void fix_length_and_dec() 
 
65
  { 
 
66
    decimals=0; 
 
67
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
 
68
    maybe_null=1; 
 
69
  }
 
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)))
 
73
  { return false; }
 
74
};
 
75
 
 
76
 
 
77
class Item_func_dayofmonth :public Item_int_func
 
78
{
 
79
public:
 
80
  Item_func_dayofmonth(Item *a) :Item_int_func(a) {}
 
81
  int64_t val_int();
 
82
  const char *func_name() const { return "dayofmonth"; }
 
83
  void fix_length_and_dec() 
 
84
  { 
 
85
    decimals=0; 
 
86
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
 
87
    maybe_null=1; 
 
88
  }
 
89
  bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
 
90
  { return false; }
 
91
};
 
92
 
 
93
 
 
94
class Item_func_month :public Item_func
 
95
{
 
96
public:
 
97
  Item_func_month(Item *a) :Item_func(a) {}
 
98
  int64_t val_int();
 
99
  double val_real()
 
100
  { assert(fixed == 1); return (double) Item_func_month::val_int(); }
 
101
  String *val_str(String *str) 
 
102
  {
 
103
    str->set(val_int(), &my_charset_bin);
 
104
    return null_value ? 0 : str;
 
105
  }
 
106
  const char *func_name() const { return "month"; }
 
107
  enum Item_result result_type () const { return INT_RESULT; }
 
108
  void fix_length_and_dec() 
 
109
  { 
 
110
    collation.set(&my_charset_bin);
 
111
    decimals=0;
 
112
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
 
113
    maybe_null=1; 
 
114
  }
 
115
  bool check_vcol_func_processor(unsigned char *int_arg __attribute__((unused)))
 
116
  { return false; }
 
117
};
 
118
 
 
119
 
 
120
class Item_func_monthname :public Item_func_month
 
121
{
 
122
public:
 
123
  Item_func_monthname(Item *a) :Item_func_month(a) {}
 
124
  const char *func_name() const { return "monthname"; }
 
125
  String *val_str(String *str);
 
126
  enum Item_result result_type () const { return STRING_RESULT; }
 
127
  void fix_length_and_dec() 
 
128
  {
 
129
    collation.set(&my_charset_bin);
 
130
    decimals=0;
 
131
    max_length=10*my_charset_bin.mbmaxlen;
 
132
    maybe_null=1; 
 
133
  }
 
134
};
 
135
 
 
136
 
 
137
class Item_func_dayofyear :public Item_int_func
 
138
{
 
139
public:
 
140
  Item_func_dayofyear(Item *a) :Item_int_func(a) {}
 
141
  int64_t val_int();
 
142
  const char *func_name() const { return "dayofyear"; }
 
143
  void fix_length_and_dec() 
 
144
  { 
 
145
    decimals=0;
 
146
    max_length=3*MY_CHARSET_BIN_MB_MAXLEN;
 
147
    maybe_null=1; 
 
148
  }
 
149
};
 
150
 
 
151
 
 
152
class Item_func_hour :public Item_int_func
 
153
{
 
154
public:
 
155
  Item_func_hour(Item *a) :Item_int_func(a) {}
 
156
  int64_t val_int();
 
157
  const char *func_name() const { return "hour"; }
 
158
  void fix_length_and_dec()
 
159
  {
 
160
    decimals=0;
 
161
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
 
162
    maybe_null=1;
 
163
  }
 
164
};
 
165
 
 
166
 
 
167
class Item_func_minute :public Item_int_func
 
168
{
 
169
public:
 
170
  Item_func_minute(Item *a) :Item_int_func(a) {}
 
171
  int64_t val_int();
 
172
  const char *func_name() const { return "minute"; }
 
173
  void fix_length_and_dec()
 
174
  {
 
175
    decimals=0;
 
176
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
 
177
    maybe_null=1;
 
178
  }
 
179
};
 
180
 
 
181
 
 
182
class Item_func_quarter :public Item_int_func
 
183
{
 
184
public:
 
185
  Item_func_quarter(Item *a) :Item_int_func(a) {}
 
186
  int64_t val_int();
 
187
  const char *func_name() const { return "quarter"; }
 
188
  void fix_length_and_dec()
 
189
  { 
 
190
     decimals=0;
 
191
     max_length=1*MY_CHARSET_BIN_MB_MAXLEN;
 
192
     maybe_null=1;
 
193
  }
 
194
};
 
195
 
 
196
 
 
197
class Item_func_second :public Item_int_func
 
198
{
 
199
public:
 
200
  Item_func_second(Item *a) :Item_int_func(a) {}
 
201
  int64_t val_int();
 
202
  const char *func_name() const { return "second"; }
 
203
  void fix_length_and_dec() 
 
204
  { 
 
205
    decimals=0;
 
206
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
 
207
    maybe_null=1;
 
208
  }
 
209
};
 
210
 
 
211
 
 
212
class Item_func_week :public Item_int_func
 
213
{
 
214
public:
 
215
  Item_func_week(Item *a,Item *b) :Item_int_func(a,b) {}
 
216
  int64_t val_int();
 
217
  const char *func_name() const { return "week"; }
 
218
  void fix_length_and_dec()
 
219
  { 
 
220
    decimals=0;
 
221
    max_length=2*MY_CHARSET_BIN_MB_MAXLEN;
 
222
    maybe_null=1;
 
223
  }
 
224
};
 
225
 
 
226
class Item_func_yearweek :public Item_int_func
 
227
{
 
228
public:
 
229
  Item_func_yearweek(Item *a,Item *b) :Item_int_func(a,b) {}
 
230
  int64_t val_int();
 
231
  const char *func_name() const { return "yearweek"; }
 
232
  void fix_length_and_dec()
 
233
  { 
 
234
    decimals=0;
 
235
    max_length=6*MY_CHARSET_BIN_MB_MAXLEN;
 
236
    maybe_null=1;
 
237
  }
 
238
};
 
239
 
 
240
 
 
241
class Item_func_year :public Item_int_func
 
242
{
 
243
public:
 
244
  Item_func_year(Item *a) :Item_int_func(a) {}
 
245
  int64_t val_int();
 
246
  const char *func_name() const { return "year"; }
 
247
  enum_monotonicity_info get_monotonicity_info() const;
 
248
  int64_t val_int_endpoint(bool left_endp, bool *incl_endp);
 
249
  void fix_length_and_dec()
 
250
  { 
 
251
    decimals=0;
 
252
    max_length=4*MY_CHARSET_BIN_MB_MAXLEN;
 
253
    maybe_null=1;
 
254
  }
 
255
};
 
256
 
 
257
 
 
258
class Item_func_weekday :public Item_func
 
259
{
 
260
  bool odbc_type;
 
261
public:
 
262
  Item_func_weekday(Item *a,bool type_arg)
 
263
    :Item_func(a), odbc_type(type_arg) {}
 
264
  int64_t val_int();
 
265
  double val_real() { assert(fixed == 1); return (double) val_int(); }
 
266
  String *val_str(String *str)
 
267
  {
 
268
    assert(fixed == 1);
 
269
    str->set(val_int(), &my_charset_bin);
 
270
    return null_value ? 0 : str;
 
271
  }
 
272
  const char *func_name() const
 
273
  {
 
274
     return (odbc_type ? "dayofweek" : "weekday");
 
275
  }
 
276
  enum Item_result result_type () const { return INT_RESULT; }
 
277
  void fix_length_and_dec()
 
278
  {
 
279
    collation.set(&my_charset_bin);
 
280
    decimals=0;
 
281
    max_length=1*MY_CHARSET_BIN_MB_MAXLEN;
 
282
    maybe_null=1;
 
283
  }
 
284
};
 
285
 
 
286
class Item_func_dayname :public Item_func_weekday
 
287
{
 
288
 public:
 
289
  Item_func_dayname(Item *a) :Item_func_weekday(a,0) {}
 
290
  const char *func_name() const { return "dayname"; }
 
291
  String *val_str(String *str);
 
292
  enum Item_result result_type () const { return STRING_RESULT; }
 
293
  void fix_length_and_dec() 
 
294
  { 
 
295
    collation.set(&my_charset_bin);
 
296
    decimals=0; 
 
297
    max_length=9*MY_CHARSET_BIN_MB_MAXLEN;
 
298
    maybe_null=1; 
 
299
  }
 
300
};
 
301
 
 
302
 
 
303
class Item_func_unix_timestamp :public Item_int_func
 
304
{
 
305
  String value;
 
306
public:
 
307
  Item_func_unix_timestamp() :Item_int_func() {}
 
308
  Item_func_unix_timestamp(Item *a) :Item_int_func(a) {}
 
309
  int64_t val_int();
 
310
  const char *func_name() const { return "unix_timestamp"; }
 
311
  void fix_length_and_dec()
 
312
  {
 
313
    decimals=0;
 
314
    max_length=10*MY_CHARSET_BIN_MB_MAXLEN;
 
315
  }
 
316
  bool check_vcol_func_processor(unsigned char *int_arg  __attribute__((unused)))
 
317
  { return true; }
 
318
};
 
319
 
 
320
 
 
321
class Item_func_time_to_sec :public Item_int_func
 
322
{
 
323
public:
 
324
  Item_func_time_to_sec(Item *item) :Item_int_func(item) {}
 
325
  int64_t val_int();
 
326
  const char *func_name() const { return "time_to_sec"; }
 
327
  void fix_length_and_dec()
 
328
  {
 
329
    decimals=0;
 
330
    max_length=10*MY_CHARSET_BIN_MB_MAXLEN;
 
331
  }
 
332
};
 
333
 
 
334
 
 
335
/*
 
336
  This can't be a Item_str_func, because the val_real() functions are special
 
337
*/
 
338
 
 
339
class Item_date :public Item_func
 
340
{
 
341
public:
 
342
  Item_date() :Item_func() {}
 
343
  Item_date(Item *a) :Item_func(a) {}
 
344
  enum Item_result result_type () const { return STRING_RESULT; }
 
345
  enum_field_types field_type() const { return DRIZZLE_TYPE_NEWDATE; }
 
346
  String *val_str(String *str);
 
347
  int64_t val_int();
 
348
  double val_real() { return val_real_from_decimal(); }
 
349
  const char *func_name() const { return "date"; }
 
350
  void fix_length_and_dec()
 
351
  { 
 
352
    collation.set(&my_charset_bin);
 
353
    decimals=0;
 
354
    max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
 
355
  }
 
356
  Field *tmp_table_field(Table *table)
 
357
  {
 
358
    return tmp_table_field_from_field_type(table, 0);
 
359
  }
 
360
  bool result_as_int64_t() { return true; }
 
361
  my_decimal *val_decimal(my_decimal *decimal_value)
 
362
  {
 
363
    assert(fixed == 1);
 
364
    return  val_decimal_from_date(decimal_value);
 
365
  }
 
366
  int save_in_field(Field *field,
 
367
                    bool no_conversions __attribute__((unused)))
 
368
  {
 
369
    return save_date_in_field(field);
 
370
  }
 
371
};
 
372
 
 
373
 
 
374
class Item_date_func :public Item_str_func
 
375
{
 
376
public:
 
377
  Item_date_func() :Item_str_func() {}
 
378
  Item_date_func(Item *a) :Item_str_func(a) {}
 
379
  Item_date_func(Item *a,Item *b) :Item_str_func(a,b) {}
 
380
  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)
 
383
  {
 
384
    return tmp_table_field_from_field_type(table, 0);
 
385
  }
 
386
  bool result_as_int64_t() { return true; }
 
387
  double val_real() { return (double) val_int(); }
 
388
  my_decimal *val_decimal(my_decimal *decimal_value)
 
389
  {
 
390
    assert(fixed == 1);
 
391
    return  val_decimal_from_date(decimal_value);
 
392
  }
 
393
  int save_in_field(Field *field,
 
394
                    bool no_conversions __attribute__((unused)))
 
395
  {
 
396
    return save_date_in_field(field);
 
397
  }
 
398
};
 
399
 
 
400
 
 
401
class Item_str_timefunc :public Item_str_func
 
402
{
 
403
public:
 
404
  Item_str_timefunc() :Item_str_func() {}
 
405
  Item_str_timefunc(Item *a) :Item_str_func(a) {}
 
406
  Item_str_timefunc(Item *a,Item *b) :Item_str_func(a,b) {}
 
407
  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; }
 
409
  void fix_length_and_dec()
 
410
  {
 
411
    decimals= DATETIME_DEC;
 
412
    max_length=MAX_TIME_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
 
413
  }
 
414
  Field *tmp_table_field(Table *table)
 
415
  {
 
416
    return tmp_table_field_from_field_type(table, 0);
 
417
  }
 
418
  double val_real() { return val_real_from_decimal(); }
 
419
  my_decimal *val_decimal(my_decimal *decimal_value)
 
420
  {
 
421
    assert(fixed == 1);
 
422
    return  val_decimal_from_time(decimal_value);
 
423
  }
 
424
  int save_in_field(Field *field,
 
425
                    bool no_conversions __attribute__((unused)))
 
426
  {
 
427
    return save_time_in_field(field);
 
428
  }
 
429
};
 
430
 
 
431
 
 
432
/* Abstract CURTIME function. Children should define what time zone is used */
 
433
 
 
434
class Item_func_curtime :public Item_str_timefunc
 
435
{
 
436
  int64_t value;
 
437
  char buff[9*2+32];
 
438
  uint32_t buff_length;
 
439
public:
 
440
  Item_func_curtime() :Item_str_timefunc() {}
 
441
  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; }
 
444
  String *val_str(String *str);
 
445
  void fix_length_and_dec();
 
446
  /* 
 
447
    Abstract method that defines which time zone is used for conversion.
 
448
    Converts time current time in my_time_t representation to broken-down
 
449
    DRIZZLE_TIME representation using UTC-SYSTEM or per-thread time zone.
 
450
  */
 
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)))
 
454
  { return true; }
 
455
};
 
456
 
 
457
 
 
458
class Item_func_curtime_local :public Item_func_curtime
 
459
{
 
460
public:
 
461
  Item_func_curtime_local() :Item_func_curtime() {}
 
462
  Item_func_curtime_local(Item *a) :Item_func_curtime(a) {}
 
463
  const char *func_name() const { return "curtime"; }
 
464
  virtual void store_now_in_TIME(DRIZZLE_TIME *now_time);
 
465
};
 
466
 
 
467
 
 
468
class Item_func_curtime_utc :public Item_func_curtime
 
469
{
 
470
public:
 
471
  Item_func_curtime_utc() :Item_func_curtime() {}
 
472
  Item_func_curtime_utc(Item *a) :Item_func_curtime(a) {}
 
473
  const char *func_name() const { return "utc_time"; }
 
474
  virtual void store_now_in_TIME(DRIZZLE_TIME *now_time);
 
475
};
 
476
 
 
477
 
 
478
/* Abstract CURDATE function. See also Item_func_curtime. */
 
479
 
 
480
class Item_func_curdate :public Item_date
 
481
{
 
482
  int64_t value;
 
483
  DRIZZLE_TIME ltime;
 
484
public:
 
485
  Item_func_curdate() :Item_date() {}
 
486
  int64_t val_int() { assert(fixed == 1); return (value) ; }
 
487
  String *val_str(String *str);
 
488
  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)))
 
492
  { return true; }
 
493
};
 
494
 
 
495
 
 
496
class Item_func_curdate_local :public Item_func_curdate
 
497
{
 
498
public:
 
499
  Item_func_curdate_local() :Item_func_curdate() {}
 
500
  const char *func_name() const { return "curdate"; }
 
501
  void store_now_in_TIME(DRIZZLE_TIME *now_time);
 
502
};
 
503
 
 
504
 
 
505
class Item_func_curdate_utc :public Item_func_curdate
 
506
{
 
507
public:
 
508
  Item_func_curdate_utc() :Item_func_curdate() {}
 
509
  const char *func_name() const { return "utc_date"; }
 
510
  void store_now_in_TIME(DRIZZLE_TIME *now_time);
 
511
};
 
512
 
 
513
 
 
514
/* Abstract CURRENT_TIMESTAMP function. See also Item_func_curtime */
 
515
 
 
516
class Item_func_now :public Item_date_func
 
517
{
 
518
protected:
 
519
  int64_t value;
 
520
  char buff[20*2+32];   // +32 to make my_snprintf_{8bit|ucs2} happy
 
521
  uint32_t buff_length;
 
522
  DRIZZLE_TIME ltime;
 
523
public:
 
524
  Item_func_now() :Item_date_func() {}
 
525
  Item_func_now(Item *a) :Item_date_func(a) {}
 
526
  enum Item_result result_type () const { return STRING_RESULT; }
 
527
  int64_t val_int() { assert(fixed == 1); return value; }
 
528
  int save_in_field(Field *to, bool no_conversions);
 
529
  String *val_str(String *str);
 
530
  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)))
 
534
  { return true; }
 
535
};
 
536
 
 
537
 
 
538
class Item_func_now_local :public Item_func_now
 
539
{
 
540
public:
 
541
  Item_func_now_local() :Item_func_now() {}
 
542
  Item_func_now_local(Item *a) :Item_func_now(a) {}
 
543
  const char *func_name() const { return "now"; }
 
544
  virtual void store_now_in_TIME(DRIZZLE_TIME *now_time);
 
545
  virtual enum Functype functype() const { return NOW_FUNC; }
 
546
};
 
547
 
 
548
 
 
549
class Item_func_now_utc :public Item_func_now
 
550
{
 
551
public:
 
552
  Item_func_now_utc() :Item_func_now() {}
 
553
  Item_func_now_utc(Item *a) :Item_func_now(a) {}
 
554
  const char *func_name() const { return "utc_timestamp"; }
 
555
  virtual void store_now_in_TIME(DRIZZLE_TIME *now_time);
 
556
};
 
557
 
 
558
 
 
559
/*
 
560
  This is like NOW(), but always uses the real current time, not the
 
561
  query_start(). This matches the Oracle behavior.
 
562
*/
 
563
class Item_func_sysdate_local :public Item_func_now
 
564
{
 
565
public:
 
566
  Item_func_sysdate_local() :Item_func_now() {}
 
567
  Item_func_sysdate_local(Item *a) :Item_func_now(a) {}
 
568
  bool const_item() const { return 0; }
 
569
  const char *func_name() const { return "sysdate"; }
 
570
  void store_now_in_TIME(DRIZZLE_TIME *now_time);
 
571
  double val_real();
 
572
  int64_t val_int();
 
573
  int save_in_field(Field *to, bool no_conversions);
 
574
  String *val_str(String *str);
 
575
  void fix_length_and_dec();
 
576
  bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
 
577
  void update_used_tables()
 
578
  {
 
579
    Item_func_now::update_used_tables();
 
580
    used_tables_cache|= RAND_TABLE_BIT;
 
581
  }
 
582
};
 
583
 
 
584
 
 
585
class Item_func_from_days :public Item_date
 
586
{
 
587
public:
 
588
  Item_func_from_days(Item *a) :Item_date(a) {}
 
589
  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)))
 
592
  { return false; }
 
593
};
 
594
 
 
595
 
 
596
class Item_func_date_format :public Item_str_func
 
597
{
 
598
  int fixed_length;
 
599
  const bool is_time_format;
 
600
  String value;
 
601
public:
 
602
  Item_func_date_format(Item *a,Item *b,bool is_time_format_arg)
 
603
    :Item_str_func(a,b),is_time_format(is_time_format_arg) {}
 
604
  String *val_str(String *str);
 
605
  const char *func_name() const
 
606
    { return is_time_format ? "time_format" : "date_format"; }
 
607
  void fix_length_and_dec();
 
608
  uint32_t format_length(const String *format);
 
609
  bool eq(const Item *item, bool binary_cmp) const;
 
610
};
 
611
 
 
612
 
 
613
class Item_func_from_unixtime :public Item_date_func
 
614
{
 
615
  THD *thd;
 
616
 public:
 
617
  Item_func_from_unixtime(Item *a) :Item_date_func(a) {}
 
618
  int64_t val_int();
 
619
  String *val_str(String *str);
 
620
  const char *func_name() const { return "from_unixtime"; }
 
621
  void fix_length_and_dec();
 
622
  bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
 
623
};
 
624
 
 
625
 
 
626
/* 
 
627
  We need Time_zone class declaration for storing pointers in
 
628
  Item_func_convert_tz.
 
629
*/
 
630
class Time_zone;
 
631
 
 
632
/*
 
633
  This class represents CONVERT_TZ() function.
 
634
  The important fact about this function that it is handled in special way.
 
635
  When such function is met in expression time_zone system tables are added
 
636
  to global list of tables to open, so later those already opened and locked
 
637
  tables can be used during this function calculation for loading time zone
 
638
  descriptions.
 
639
*/
 
640
class Item_func_convert_tz :public Item_date_func
 
641
{
 
642
  /*
 
643
    If time zone parameters are constants we are caching objects that
 
644
    represent them (we use separate from_tz_cached/to_tz_cached members
 
645
    to indicate this fact, since NULL is legal value for from_tz/to_tz
 
646
    members.
 
647
  */
 
648
  bool from_tz_cached, to_tz_cached;
 
649
  Time_zone *from_tz, *to_tz;
 
650
 public:
 
651
  Item_func_convert_tz(Item *a, Item *b, Item *c):
 
652
    Item_date_func(a, b, c), from_tz_cached(0), to_tz_cached(0) {}
 
653
  int64_t val_int();
 
654
  String *val_str(String *str);
 
655
  const char *func_name() const { return "convert_tz"; }
 
656
  void fix_length_and_dec();
 
657
  bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
 
658
  void cleanup();
 
659
};
 
660
 
 
661
 
 
662
class Item_func_sec_to_time :public Item_str_timefunc
 
663
{
 
664
public:
 
665
  Item_func_sec_to_time(Item *item) :Item_str_timefunc(item) {}
 
666
  double val_real()
 
667
  {
 
668
    assert(fixed == 1);
 
669
    return (double) Item_func_sec_to_time::val_int();
 
670
  }
 
671
  int64_t val_int();
 
672
  String *val_str(String *);
 
673
  void fix_length_and_dec()
 
674
  { 
 
675
    Item_str_timefunc::fix_length_and_dec();
 
676
    collation.set(&my_charset_bin);
 
677
    maybe_null=1;
 
678
  }
 
679
  const char *func_name() const { return "sec_to_time"; }
 
680
  bool result_as_int64_t() { return true; }
 
681
};
 
682
 
 
683
 
 
684
class Item_date_add_interval :public Item_date_func
 
685
{
 
686
  String value;
 
687
  enum_field_types cached_field_type;
 
688
 
 
689
public:
 
690
  const interval_type int_type; // keep it public
 
691
  const bool date_sub_interval; // keep it public
 
692
  Item_date_add_interval(Item *a,Item *b,interval_type type_arg,bool neg_arg)
 
693
    :Item_date_func(a,b),int_type(type_arg), date_sub_interval(neg_arg) {}
 
694
  String *val_str(String *);
 
695
  const char *func_name() const { return "date_add_interval"; }
 
696
  void fix_length_and_dec();
 
697
  enum_field_types field_type() const { return cached_field_type; }
 
698
  int64_t val_int();
 
699
  bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
 
700
  bool eq(const Item *item, bool binary_cmp) const;
 
701
  virtual void print(String *str, enum_query_type query_type);
 
702
};
 
703
 
 
704
 
 
705
class Item_extract :public Item_int_func
 
706
{
 
707
  String value;
 
708
  bool date_value;
 
709
 public:
 
710
  const interval_type int_type; // keep it public
 
711
  Item_extract(interval_type type_arg, Item *a)
 
712
    :Item_int_func(a), int_type(type_arg) {}
 
713
  int64_t val_int();
 
714
  enum Functype functype() const { return EXTRACT_FUNC; }
 
715
  const char *func_name() const { return "extract"; }
 
716
  void fix_length_and_dec();
 
717
  bool eq(const Item *item, bool binary_cmp) const;
 
718
  virtual void print(String *str, enum_query_type query_type);
 
719
};
 
720
 
 
721
 
 
722
class Item_typecast :public Item_str_func
 
723
{
 
724
public:
 
725
  Item_typecast(Item *a) :Item_str_func(a) {}
 
726
  String *val_str(String *a)
 
727
  {
 
728
    assert(fixed == 1);
 
729
    String *tmp=args[0]->val_str(a);
 
730
    null_value=args[0]->null_value;
 
731
    if (tmp)
 
732
      tmp->set_charset(collation.collation);
 
733
    return tmp;
 
734
  }
 
735
  void fix_length_and_dec()
 
736
  {
 
737
    collation.set(&my_charset_bin);
 
738
    max_length=args[0]->max_length;
 
739
  }
 
740
  virtual const char* cast_type() const= 0;
 
741
  virtual void print(String *str, enum_query_type query_type);
 
742
};
 
743
 
 
744
 
 
745
class Item_typecast_maybe_null :public Item_typecast
 
746
{
 
747
public:
 
748
  Item_typecast_maybe_null(Item *a) :Item_typecast(a) {}
 
749
  void fix_length_and_dec()
 
750
  {
 
751
    collation.set(&my_charset_bin);
 
752
    max_length=args[0]->max_length;
 
753
    maybe_null= 1;
 
754
  }
 
755
};
 
756
 
 
757
 
 
758
class Item_char_typecast :public Item_typecast
 
759
{
 
760
  int cast_length;
 
761
  const CHARSET_INFO *cast_cs, *from_cs;
 
762
  bool charset_conversion;
 
763
  String tmp_value;
 
764
public:
 
765
  Item_char_typecast(Item *a, int length_arg, const CHARSET_INFO * const cs_arg)
 
766
    :Item_typecast(a), cast_length(length_arg), cast_cs(cs_arg) {}
 
767
  enum Functype functype() const { return CHAR_TYPECAST_FUNC; }
 
768
  bool eq(const Item *item, bool binary_cmp) const;
 
769
  const char *func_name() const { return "cast_as_char"; }
 
770
  const char* cast_type() const { return "char"; };
 
771
  String *val_str(String *a);
 
772
  void fix_length_and_dec();
 
773
  virtual void print(String *str, enum_query_type query_type);
 
774
};
 
775
 
 
776
 
 
777
class Item_date_typecast :public Item_typecast_maybe_null
 
778
{
 
779
public:
 
780
  Item_date_typecast(Item *a) :Item_typecast_maybe_null(a) {}
 
781
  const char *func_name() const { return "cast_as_date"; }
 
782
  String *val_str(String *str);
 
783
  bool get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date);
 
784
  bool get_time(DRIZZLE_TIME *ltime);
 
785
  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)
 
788
  {
 
789
    return tmp_table_field_from_field_type(table, 0);
 
790
  }  
 
791
  void fix_length_and_dec()
 
792
  {
 
793
    collation.set(&my_charset_bin);
 
794
    max_length= 10;
 
795
    maybe_null= 1;
 
796
  }
 
797
  bool result_as_int64_t() { return true; }
 
798
  int64_t val_int();
 
799
  double val_real() { return (double) val_int(); }
 
800
  my_decimal *val_decimal(my_decimal *decimal_value)
 
801
  {
 
802
    assert(fixed == 1);
 
803
    return  val_decimal_from_date(decimal_value);
 
804
  }
 
805
  int save_in_field(Field *field,
 
806
                    bool no_conversions __attribute__((unused)))
 
807
  {
 
808
    return save_date_in_field(field);
 
809
  }
 
810
};
 
811
 
 
812
 
 
813
class Item_time_typecast :public Item_typecast_maybe_null
 
814
{
 
815
public:
 
816
  Item_time_typecast(Item *a) :Item_typecast_maybe_null(a) {}
 
817
  const char *func_name() const { return "cast_as_time"; }
 
818
  String *val_str(String *str);
 
819
  bool get_time(DRIZZLE_TIME *ltime);
 
820
  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)
 
823
  {
 
824
    return tmp_table_field_from_field_type(table, 0);
 
825
  }
 
826
  bool result_as_int64_t() { return true; }
 
827
  int64_t val_int();
 
828
  double val_real() { return val_real_from_decimal(); }
 
829
  my_decimal *val_decimal(my_decimal *decimal_value)
 
830
  {
 
831
    assert(fixed == 1);
 
832
    return  val_decimal_from_time(decimal_value);
 
833
  }
 
834
  int save_in_field(Field *field,
 
835
                    bool no_conversions __attribute__((unused)))
 
836
  {
 
837
    return save_time_in_field(field);
 
838
  }
 
839
};
 
840
 
 
841
 
 
842
class Item_datetime_typecast :public Item_typecast_maybe_null
 
843
{
 
844
public:
 
845
  Item_datetime_typecast(Item *a) :Item_typecast_maybe_null(a) {}
 
846
  const char *func_name() const { return "cast_as_datetime"; }
 
847
  String *val_str(String *str);
 
848
  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)
 
851
  {
 
852
    return tmp_table_field_from_field_type(table, 0);
 
853
  }
 
854
  void fix_length_and_dec()
 
855
  {
 
856
    collation.set(&my_charset_bin);
 
857
    maybe_null= 1;
 
858
    max_length= MAX_DATETIME_FULL_WIDTH * MY_CHARSET_BIN_MB_MAXLEN;
 
859
    decimals= DATETIME_DEC;
 
860
  }
 
861
  bool result_as_int64_t() { return true; }
 
862
  int64_t val_int();
 
863
  double val_real() { return val_real_from_decimal(); }
 
864
  double val() { return (double) val_int(); }
 
865
  my_decimal *val_decimal(my_decimal *decimal_value)
 
866
  {
 
867
    assert(fixed == 1);
 
868
    return  val_decimal_from_date(decimal_value);
 
869
  }
 
870
  int save_in_field(Field *field,
 
871
                    bool no_conversions __attribute__((unused)))
 
872
  {
 
873
    return save_date_in_field(field);
 
874
  }
 
875
};
 
876
 
 
877
class Item_func_makedate :public Item_date_func
 
878
{
 
879
public:
 
880
  Item_func_makedate(Item *a,Item *b) :Item_date_func(a,b) {}
 
881
  String *val_str(String *str);
 
882
  const char *func_name() const { return "makedate"; }
 
883
  enum_field_types field_type() const { return DRIZZLE_TYPE_NEWDATE; }
 
884
  void fix_length_and_dec()
 
885
  { 
 
886
    decimals=0;
 
887
    max_length=MAX_DATE_WIDTH*MY_CHARSET_BIN_MB_MAXLEN;
 
888
  }
 
889
  int64_t val_int();
 
890
};
 
891
 
 
892
 
 
893
class Item_func_add_time :public Item_str_func
 
894
{
 
895
  const bool is_date;
 
896
  int sign;
 
897
  enum_field_types cached_field_type;
 
898
 
 
899
public:
 
900
  Item_func_add_time(Item *a, Item *b, bool type_arg, bool neg_arg)
 
901
    :Item_str_func(a, b), is_date(type_arg) { sign= neg_arg ? -1 : 1; }
 
902
  String *val_str(String *str);
 
903
  enum_field_types field_type() const { return cached_field_type; }
 
904
  void fix_length_and_dec();
 
905
 
 
906
  Field *tmp_table_field(Table *table)
 
907
  {
 
908
    return tmp_table_field_from_field_type(table, 0);
 
909
  }
 
910
  virtual void print(String *str, enum_query_type query_type);
 
911
  const char *func_name() const { return "add_time"; }
 
912
  double val_real() { return val_real_from_decimal(); }
 
913
  my_decimal *val_decimal(my_decimal *decimal_value)
 
914
  {
 
915
    assert(fixed == 1);
 
916
    if (cached_field_type == DRIZZLE_TYPE_TIME)
 
917
      return  val_decimal_from_time(decimal_value);
 
918
    if (cached_field_type == DRIZZLE_TYPE_DATETIME)
 
919
      return  val_decimal_from_date(decimal_value);
 
920
    return Item_str_func::val_decimal(decimal_value);
 
921
  }
 
922
  int save_in_field(Field *field, bool no_conversions)
 
923
  {
 
924
    if (cached_field_type == DRIZZLE_TYPE_TIME)
 
925
      return save_time_in_field(field);
 
926
    if (cached_field_type == DRIZZLE_TYPE_DATETIME)
 
927
      return save_date_in_field(field);
 
928
    return Item_str_func::save_in_field(field, no_conversions);
 
929
  }
 
930
};
 
931
 
 
932
class Item_func_timediff :public Item_str_timefunc
 
933
{
 
934
public:
 
935
  Item_func_timediff(Item *a, Item *b)
 
936
    :Item_str_timefunc(a, b) {}
 
937
  String *val_str(String *str);
 
938
  const char *func_name() const { return "timediff"; }
 
939
  void fix_length_and_dec()
 
940
  {
 
941
    Item_str_timefunc::fix_length_and_dec();
 
942
    maybe_null= 1;
 
943
  }
 
944
};
 
945
 
 
946
class Item_func_maketime :public Item_str_timefunc
 
947
{
 
948
public:
 
949
  Item_func_maketime(Item *a, Item *b, Item *c)
 
950
    :Item_str_timefunc(a, b, c) 
 
951
  {
 
952
    maybe_null= true;
 
953
  }
 
954
  String *val_str(String *str);
 
955
  const char *func_name() const { return "maketime"; }
 
956
};
 
957
 
 
958
class Item_func_microsecond :public Item_int_func
 
959
{
 
960
public:
 
961
  Item_func_microsecond(Item *a) :Item_int_func(a) {}
 
962
  int64_t val_int();
 
963
  const char *func_name() const { return "microsecond"; }
 
964
  void fix_length_and_dec() 
 
965
  { 
 
966
    decimals=0;
 
967
    maybe_null=1;
 
968
  }
 
969
};
 
970
 
 
971
 
 
972
class Item_func_timestamp_diff :public Item_int_func
 
973
{
 
974
  const interval_type int_type;
 
975
public:
 
976
  Item_func_timestamp_diff(Item *a,Item *b,interval_type type_arg)
 
977
    :Item_int_func(a,b), int_type(type_arg) {}
 
978
  const char *func_name() const { return "timestampdiff"; }
 
979
  int64_t val_int();
 
980
  void fix_length_and_dec()
 
981
  {
 
982
    decimals=0;
 
983
    maybe_null=1;
 
984
  }
 
985
  virtual void print(String *str, enum_query_type query_type);
 
986
};
 
987
 
 
988
 
 
989
enum date_time_format
 
990
{
 
991
  USA_FORMAT, JIS_FORMAT, ISO_FORMAT, EUR_FORMAT, INTERNAL_FORMAT
 
992
};
 
993
 
 
994
class Item_func_get_format :public Item_str_func
 
995
{
 
996
public:
 
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)
 
999
    :Item_str_func(a), type(type_arg)
 
1000
  {}
 
1001
  String *val_str(String *str);
 
1002
  const char *func_name() const { return "get_format"; }
 
1003
  void fix_length_and_dec()
 
1004
  {
 
1005
    maybe_null= 1;
 
1006
    decimals=0;
 
1007
    max_length=17*MY_CHARSET_BIN_MB_MAXLEN;
 
1008
  }
 
1009
  virtual void print(String *str, enum_query_type query_type);
 
1010
};
 
1011
 
 
1012
 
 
1013
class Item_func_str_to_date :public Item_str_func
 
1014
{
 
1015
  enum_field_types cached_field_type;
 
1016
  date_time_format_types cached_format_type;
 
1017
  enum enum_drizzle_timestamp_type cached_timestamp_type;
 
1018
  bool const_item;
 
1019
public:
 
1020
  Item_func_str_to_date(Item *a, Item *b)
 
1021
    :Item_str_func(a, b), const_item(false)
 
1022
  {}
 
1023
  String *val_str(String *str);
 
1024
  bool get_date(DRIZZLE_TIME *ltime, uint32_t fuzzy_date);
 
1025
  const char *func_name() const { return "str_to_date"; }
 
1026
  enum_field_types field_type() const { return cached_field_type; }
 
1027
  void fix_length_and_dec();
 
1028
  Field *tmp_table_field(Table *table)
 
1029
  {
 
1030
    return tmp_table_field_from_field_type(table, 1);
 
1031
  }
 
1032
};
 
1033
 
 
1034
 
 
1035
class Item_func_last_day :public Item_date
 
1036
{
 
1037
public:
 
1038
  Item_func_last_day(Item *a) :Item_date(a) {}
 
1039
  const char *func_name() const { return "last_day"; }
 
1040
  bool get_date(DRIZZLE_TIME *res, uint32_t fuzzy_date);
 
1041
};