~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_timefunc.h

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

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