~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
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.
6
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.
11
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 */
15
16
17
/* Function items used by mysql */
18
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface			/* gcc class implementation */
21
#endif
22
23
#ifdef HAVE_IEEEFP_H
24
extern "C"				/* Bug in BSDI include file */
25
{
26
#include <ieeefp.h>
27
}
28
#endif
29
30
class Item_func :public Item_result_field
31
{
32
protected:
33
  Item **args, *tmp_arg[2];
34
  /*
35
    Allowed numbers of columns in result (usually 1, which means scalar value)
36
    0 means get this number from first argument
37
  */
38
  uint allowed_arg_cols;
39
public:
40
  uint arg_count;
41
  table_map used_tables_cache, not_null_tables_cache;
42
  bool const_item_cache;
43
  enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
160.1.2 by mark
remove FTPARSER and last remains of full text search
44
		  GE_FUNC,GT_FUNC,
1 by brian
clean slate
45
		  LIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC,
46
		  COND_AND_FUNC, COND_OR_FUNC, COND_XOR_FUNC,
47
                  BETWEEN, IN_FUNC, MULT_EQUAL_FUNC,
48
		  INTERVAL_FUNC, ISNOTNULLTEST_FUNC,
49
                  NOT_FUNC, NOT_ALL_FUNC,
50
                  NOW_FUNC, TRIG_COND_FUNC,
51
                  SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC,
52
                  EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP, UDF_FUNC,
53
                  NEG_FUNC };
54
  enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL,
55
                       OPTIMIZE_EQUAL };
56
  enum Type type() const { return FUNC_ITEM; }
57
  virtual enum Functype functype() const   { return UNKNOWN_FUNC; }
58
  Item_func(void):
59
    allowed_arg_cols(1), arg_count(0)
60
  {
61
    with_sum_func= 0;
62
  }
63
  Item_func(Item *a):
64
    allowed_arg_cols(1), arg_count(1)
65
  {
66
    args= tmp_arg;
67
    args[0]= a;
68
    with_sum_func= a->with_sum_func;
69
  }
70
  Item_func(Item *a,Item *b):
71
    allowed_arg_cols(1), arg_count(2)
72
  {
73
    args= tmp_arg;
74
    args[0]= a; args[1]= b;
75
    with_sum_func= a->with_sum_func || b->with_sum_func;
76
  }
77
  Item_func(Item *a,Item *b,Item *c):
78
    allowed_arg_cols(1)
79
  {
80
    arg_count= 0;
81
    if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
82
    {
83
      arg_count= 3;
84
      args[0]= a; args[1]= b; args[2]= c;
85
      with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
86
    }
87
  }
88
  Item_func(Item *a,Item *b,Item *c,Item *d):
89
    allowed_arg_cols(1)
90
  {
91
    arg_count= 0;
92
    if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
93
    {
94
      arg_count= 4;
95
      args[0]= a; args[1]= b; args[2]= c; args[3]= d;
96
      with_sum_func= a->with_sum_func || b->with_sum_func ||
97
	c->with_sum_func || d->with_sum_func;
98
    }
99
  }
100
  Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
101
    allowed_arg_cols(1)
102
  {
103
    arg_count= 5;
104
    if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
105
    {
106
      args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
107
      with_sum_func= a->with_sum_func || b->with_sum_func ||
108
	c->with_sum_func || d->with_sum_func || e->with_sum_func ;
109
    }
110
  }
111
  Item_func(List<Item> &list);
112
  // Constructor used for Item_cond_and/or (see Item comment)
113
  Item_func(THD *thd, Item_func *item);
114
  bool fix_fields(THD *, Item **ref);
115
  void fix_after_pullout(st_select_lex *new_parent, Item **ref);
116
  table_map used_tables() const;
117
  table_map not_null_tables() const;
118
  void update_used_tables();
119
  bool eq(const Item *item, bool binary_cmp) const;
120
  virtual optimize_type select_optimize() const { return OPTIMIZE_NONE; }
121
  virtual bool have_rev_func() const { return 0; }
122
  virtual Item *key_item() const { return args[0]; }
123
  /*
124
    This method is used for debug purposes to print the name of an
125
    item to the debug log. The second use of this method is as
126
    a helper function of print(), where it is applicable.
127
    To suit both goals it should return a meaningful,
128
    distinguishable and sintactically correct string.  This method
129
    should not be used for runtime type identification, use enum
130
    {Sum}Functype and Item_func::functype()/Item_sum::sum_func()
131
    instead.
132
  */
133
  virtual const char *func_name() const= 0;
134
  virtual bool const_item() const { return const_item_cache; }
135
  inline Item **arguments() const { return args; }
136
  void set_arguments(List<Item> &list);
137
  inline uint argument_count() const { return arg_count; }
138
  inline void remove_arguments() { arg_count=0; }
139
  void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
140
  virtual void print(String *str, enum_query_type query_type);
141
  void print_op(String *str, enum_query_type query_type);
142
  void print_args(String *str, uint from, enum_query_type query_type);
143
  virtual void fix_num_length_and_dec();
144
  void count_only_length();
145
  void count_real_length();
146
  void count_decimal_length();
147
  inline bool get_arg0_date(MYSQL_TIME *ltime, uint fuzzy_date)
148
  {
149
    return (null_value=args[0]->get_date(ltime, fuzzy_date));
150
  }
151
  inline bool get_arg0_time(MYSQL_TIME *ltime)
152
  {
153
    return (null_value=args[0]->get_time(ltime));
154
  }
155
  bool is_null() { 
156
    update_null_value();
157
    return null_value; 
158
  }
159
  void signal_divide_by_null();
160
  friend class udf_handler;
161
  Field *tmp_table_field() { return result_field; }
162
  Field *tmp_table_field(TABLE *t_arg);
163
  Item *get_tmp_table_item(THD *thd);
164
165
  my_decimal *val_decimal(my_decimal *);
166
167
  bool agg_arg_collations(DTCollation &c, Item **items, uint nitems,
168
                          uint flags)
169
  {
170
    return agg_item_collations(c, func_name(), items, nitems, flags, 1);
171
  }
172
  bool agg_arg_collations_for_comparison(DTCollation &c,
173
                                         Item **items, uint nitems,
174
                                         uint flags)
175
  {
176
    return agg_item_collations_for_comparison(c, func_name(),
177
                                              items, nitems, flags);
178
  }
179
  bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems,
180
                        uint flags, int item_sep)
181
  {
182
    return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
183
  }
184
  bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
185
  Item *transform(Item_transformer transformer, uchar *arg);
186
  Item* compile(Item_analyzer analyzer, uchar **arg_p,
187
                Item_transformer transformer, uchar *arg_t);
188
  void traverse_cond(Cond_traverser traverser,
189
                     void * arg, traverse_order order);
190
  inline double fix_result(double value)
191
  {
192
    if (isfinite(value))
193
      return value;
194
    null_value=1;
195
    return 0.0;
196
  }
197
};
198
199
200
class Item_real_func :public Item_func
201
{
202
public:
203
  Item_real_func() :Item_func() {}
204
  Item_real_func(Item *a) :Item_func(a) {}
205
  Item_real_func(Item *a,Item *b) :Item_func(a,b) {}
206
  Item_real_func(List<Item> &list) :Item_func(list) {}
207
  String *val_str(String*str);
208
  my_decimal *val_decimal(my_decimal *decimal_value);
152 by Brian Aker
longlong replacement
209
  int64_t val_int()
210
    { assert(fixed == 1); return (int64_t) rint(val_real()); }
1 by brian
clean slate
211
  enum Item_result result_type () const { return REAL_RESULT; }
212
  void fix_length_and_dec()
213
  { decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
214
};
215
216
217
class Item_func_numhybrid: public Item_func
218
{
219
protected:
220
  Item_result hybrid_type;
221
public:
222
  Item_func_numhybrid(Item *a) :Item_func(a), hybrid_type(REAL_RESULT)
223
  {}
224
  Item_func_numhybrid(Item *a,Item *b)
225
    :Item_func(a,b), hybrid_type(REAL_RESULT)
226
  {}
227
  Item_func_numhybrid(List<Item> &list)
228
    :Item_func(list), hybrid_type(REAL_RESULT)
229
  {}
230
231
  enum Item_result result_type () const { return hybrid_type; }
232
  void fix_length_and_dec();
233
  void fix_num_length_and_dec();
234
  virtual void find_num_type()= 0; /* To be called from fix_length_and_dec */
235
236
  double val_real();
152 by Brian Aker
longlong replacement
237
  int64_t val_int();
1 by brian
clean slate
238
  my_decimal *val_decimal(my_decimal *);
239
  String *val_str(String*str);
240
241
  /**
242
     @brief Performs the operation that this functions implements when the
243
     result type is INT.
244
245
     @return The result of the operation.
246
  */
152 by Brian Aker
longlong replacement
247
  virtual int64_t int_op()= 0;
1 by brian
clean slate
248
249
  /**
250
     @brief Performs the operation that this functions implements when the
251
     result type is REAL.
252
253
     @return The result of the operation.
254
  */
255
  virtual double real_op()= 0;
256
257
  /**
258
     @brief Performs the operation that this functions implements when the
259
     result type is DECIMAL.
260
261
     @param A pointer where the DECIMAL value will be allocated.
262
     @return 
263
       - 0 If the result is NULL
264
       - The same pointer it was given, with the area initialized to the
265
         result of the operation.
266
  */
267
  virtual my_decimal *decimal_op(my_decimal *)= 0;
268
269
  /**
270
     @brief Performs the operation that this functions implements when the
271
     result type is a string type.
272
273
     @return The result of the operation.
274
  */
275
  virtual String *str_op(String *)= 0;
276
  bool is_null() { update_null_value(); return null_value; }
277
};
278
279
/* function where type of result detected by first argument */
280
class Item_func_num1: public Item_func_numhybrid
281
{
282
public:
283
  Item_func_num1(Item *a) :Item_func_numhybrid(a) {}
284
  Item_func_num1(Item *a, Item *b) :Item_func_numhybrid(a, b) {}
285
286
  void fix_num_length_and_dec();
287
  void find_num_type();
77.1.7 by Monty Taylor
Heap builds clean.
288
  String *str_op(String *str __attribute__((__unused__)))
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
289
  { assert(0); return 0; }
1 by brian
clean slate
290
};
291
292
293
/* Base class for operations like '+', '-', '*' */
294
class Item_num_op :public Item_func_numhybrid
295
{
296
 public:
297
  Item_num_op(Item *a,Item *b) :Item_func_numhybrid(a, b) {}
298
  virtual void result_precision()= 0;
299
300
  virtual inline void print(String *str, enum_query_type query_type)
301
  {
302
    print_op(str, query_type);
303
  }
304
305
  void find_num_type();
77.1.7 by Monty Taylor
Heap builds clean.
306
  String *str_op(String *str __attribute__((__unused__)))
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
307
  { assert(0); return 0; }
1 by brian
clean slate
308
};
309
310
311
class Item_int_func :public Item_func
312
{
313
public:
314
  Item_int_func() :Item_func() { max_length= 21; }
315
  Item_int_func(Item *a) :Item_func(a) { max_length= 21; }
316
  Item_int_func(Item *a,Item *b) :Item_func(a,b) { max_length= 21; }
317
  Item_int_func(Item *a,Item *b,Item *c) :Item_func(a,b,c)
318
  { max_length= 21; }
319
  Item_int_func(List<Item> &list) :Item_func(list) { max_length= 21; }
320
  Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item) {}
321
  double val_real();
322
  String *val_str(String*str);
323
  enum Item_result result_type () const { return INT_RESULT; }
324
  void fix_length_and_dec() {}
325
};
326
327
328
class Item_func_connection_id :public Item_int_func
329
{
152 by Brian Aker
longlong replacement
330
  int64_t value;
1 by brian
clean slate
331
332
public:
333
  Item_func_connection_id() {}
334
  const char *func_name() const { return "connection_id"; }
335
  void fix_length_and_dec();
336
  bool fix_fields(THD *thd, Item **ref);
152 by Brian Aker
longlong replacement
337
  int64_t val_int() { assert(fixed == 1); return value; }
1 by brian
clean slate
338
};
339
340
341
class Item_func_signed :public Item_int_func
342
{
343
public:
344
  Item_func_signed(Item *a) :Item_int_func(a) {}
345
  const char *func_name() const { return "cast_as_signed"; }
152 by Brian Aker
longlong replacement
346
  int64_t val_int();
347
  int64_t val_int_from_str(int *error);
1 by brian
clean slate
348
  void fix_length_and_dec()
349
  { max_length=args[0]->max_length; unsigned_flag=0; }
350
  virtual void print(String *str, enum_query_type query_type);
351
  uint decimal_precision() const { return args[0]->decimal_precision(); }
352
};
353
354
355
class Item_func_unsigned :public Item_func_signed
356
{
357
public:
358
  Item_func_unsigned(Item *a) :Item_func_signed(a) {}
359
  const char *func_name() const { return "cast_as_unsigned"; }
360
  void fix_length_and_dec()
361
  { max_length=args[0]->max_length; unsigned_flag=1; }
152 by Brian Aker
longlong replacement
362
  int64_t val_int();
1 by brian
clean slate
363
  virtual void print(String *str, enum_query_type query_type);
364
};
365
366
367
class Item_decimal_typecast :public Item_func
368
{
369
  my_decimal decimal_value;
370
public:
371
  Item_decimal_typecast(Item *a, int len, int dec) :Item_func(a)
372
  {
373
    decimals= dec;
374
    max_length= my_decimal_precision_to_length(len, dec, unsigned_flag);
375
  }
376
  String *val_str(String *str);
377
  double val_real();
152 by Brian Aker
longlong replacement
378
  int64_t val_int();
1 by brian
clean slate
379
  my_decimal *val_decimal(my_decimal*);
380
  enum Item_result result_type () const { return DECIMAL_RESULT; }
381
  enum_field_types field_type() const { return MYSQL_TYPE_NEWDECIMAL; }
382
  void fix_length_and_dec() {};
383
  const char *func_name() const { return "decimal_typecast"; }
384
  virtual void print(String *str, enum_query_type query_type);
385
};
386
387
388
class Item_func_additive_op :public Item_num_op
389
{
390
public:
391
  Item_func_additive_op(Item *a,Item *b) :Item_num_op(a,b) {}
392
  void result_precision();
393
};
394
395
396
class Item_func_plus :public Item_func_additive_op
397
{
398
public:
399
  Item_func_plus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
400
  const char *func_name() const { return "+"; }
152 by Brian Aker
longlong replacement
401
  int64_t int_op();
1 by brian
clean slate
402
  double real_op();
403
  my_decimal *decimal_op(my_decimal *);
404
};
405
406
class Item_func_minus :public Item_func_additive_op
407
{
408
public:
409
  Item_func_minus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
410
  const char *func_name() const { return "-"; }
152 by Brian Aker
longlong replacement
411
  int64_t int_op();
1 by brian
clean slate
412
  double real_op();
413
  my_decimal *decimal_op(my_decimal *);
414
  void fix_length_and_dec();
415
};
416
417
418
class Item_func_mul :public Item_num_op
419
{
420
public:
421
  Item_func_mul(Item *a,Item *b) :Item_num_op(a,b) {}
422
  const char *func_name() const { return "*"; }
152 by Brian Aker
longlong replacement
423
  int64_t int_op();
1 by brian
clean slate
424
  double real_op();
425
  my_decimal *decimal_op(my_decimal *);
426
  void result_precision();
427
};
428
429
430
class Item_func_div :public Item_num_op
431
{
432
public:
433
  uint prec_increment;
434
  Item_func_div(Item *a,Item *b) :Item_num_op(a,b) {}
152 by Brian Aker
longlong replacement
435
  int64_t int_op() { assert(0); return 0; }
1 by brian
clean slate
436
  double real_op();
437
  my_decimal *decimal_op(my_decimal *);
438
  const char *func_name() const { return "/"; }
439
  void fix_length_and_dec();
440
  void result_precision();
441
};
442
443
444
class Item_func_int_div :public Item_int_func
445
{
446
public:
447
  Item_func_int_div(Item *a,Item *b) :Item_int_func(a,b)
448
  {}
152 by Brian Aker
longlong replacement
449
  int64_t val_int();
1 by brian
clean slate
450
  const char *func_name() const { return "DIV"; }
451
  void fix_length_and_dec();
452
453
  virtual inline void print(String *str, enum_query_type query_type)
454
  {
455
    print_op(str, query_type);
456
  }
457
458
};
459
460
461
class Item_func_mod :public Item_num_op
462
{
463
public:
464
  Item_func_mod(Item *a,Item *b) :Item_num_op(a,b) {}
152 by Brian Aker
longlong replacement
465
  int64_t int_op();
1 by brian
clean slate
466
  double real_op();
467
  my_decimal *decimal_op(my_decimal *);
468
  const char *func_name() const { return "%"; }
469
  void result_precision();
470
  void fix_length_and_dec();
471
};
472
473
474
class Item_func_neg :public Item_func_num1
475
{
476
public:
477
  Item_func_neg(Item *a) :Item_func_num1(a) {}
478
  double real_op();
152 by Brian Aker
longlong replacement
479
  int64_t int_op();
1 by brian
clean slate
480
  my_decimal *decimal_op(my_decimal *);
481
  const char *func_name() const { return "-"; }
482
  enum Functype functype() const   { return NEG_FUNC; }
483
  void fix_length_and_dec();
484
  void fix_num_length_and_dec();
485
  uint decimal_precision() const { return args[0]->decimal_precision(); }
486
};
487
488
489
class Item_func_abs :public Item_func_num1
490
{
491
public:
492
  Item_func_abs(Item *a) :Item_func_num1(a) {}
493
  double real_op();
152 by Brian Aker
longlong replacement
494
  int64_t int_op();
1 by brian
clean slate
495
  my_decimal *decimal_op(my_decimal *);
496
  const char *func_name() const { return "abs"; }
497
  void fix_length_and_dec();
498
};
499
500
// A class to handle logarithmic and trigonometric functions
501
502
class Item_dec_func :public Item_real_func
503
{
504
 public:
505
  Item_dec_func(Item *a) :Item_real_func(a) {}
506
  Item_dec_func(Item *a,Item *b) :Item_real_func(a,b) {}
507
  void fix_length_and_dec()
508
  {
509
    decimals=NOT_FIXED_DEC; max_length=float_length(decimals);
510
    maybe_null=1;
511
  }
512
};
513
514
class Item_func_exp :public Item_dec_func
515
{
516
public:
517
  Item_func_exp(Item *a) :Item_dec_func(a) {}
518
  double val_real();
519
  const char *func_name() const { return "exp"; }
520
};
521
522
523
class Item_func_ln :public Item_dec_func
524
{
525
public:
526
  Item_func_ln(Item *a) :Item_dec_func(a) {}
527
  double val_real();
528
  const char *func_name() const { return "ln"; }
529
};
530
531
532
class Item_func_log :public Item_dec_func
533
{
534
public:
535
  Item_func_log(Item *a) :Item_dec_func(a) {}
536
  Item_func_log(Item *a,Item *b) :Item_dec_func(a,b) {}
537
  double val_real();
538
  const char *func_name() const { return "log"; }
539
};
540
541
542
class Item_func_log2 :public Item_dec_func
543
{
544
public:
545
  Item_func_log2(Item *a) :Item_dec_func(a) {}
546
  double val_real();
547
  const char *func_name() const { return "log2"; }
548
};
549
550
551
class Item_func_log10 :public Item_dec_func
552
{
553
public:
554
  Item_func_log10(Item *a) :Item_dec_func(a) {}
555
  double val_real();
556
  const char *func_name() const { return "log10"; }
557
};
558
559
560
class Item_func_sqrt :public Item_dec_func
561
{
562
public:
563
  Item_func_sqrt(Item *a) :Item_dec_func(a) {}
564
  double val_real();
565
  const char *func_name() const { return "sqrt"; }
566
};
567
568
569
class Item_func_pow :public Item_dec_func
570
{
571
public:
572
  Item_func_pow(Item *a,Item *b) :Item_dec_func(a,b) {}
573
  double val_real();
574
  const char *func_name() const { return "pow"; }
575
};
576
577
578
class Item_func_acos :public Item_dec_func
579
{
580
public:
581
  Item_func_acos(Item *a) :Item_dec_func(a) {}
582
  double val_real();
583
  const char *func_name() const { return "acos"; }
584
};
585
586
class Item_func_asin :public Item_dec_func
587
{
588
public:
589
  Item_func_asin(Item *a) :Item_dec_func(a) {}
590
  double val_real();
591
  const char *func_name() const { return "asin"; }
592
};
593
594
class Item_func_atan :public Item_dec_func
595
{
596
public:
597
  Item_func_atan(Item *a) :Item_dec_func(a) {}
598
  Item_func_atan(Item *a,Item *b) :Item_dec_func(a,b) {}
599
  double val_real();
600
  const char *func_name() const { return "atan"; }
601
};
602
603
class Item_func_cos :public Item_dec_func
604
{
605
public:
606
  Item_func_cos(Item *a) :Item_dec_func(a) {}
607
  double val_real();
608
  const char *func_name() const { return "cos"; }
609
};
610
611
class Item_func_sin :public Item_dec_func
612
{
613
public:
614
  Item_func_sin(Item *a) :Item_dec_func(a) {}
615
  double val_real();
616
  const char *func_name() const { return "sin"; }
617
};
618
619
class Item_func_tan :public Item_dec_func
620
{
621
public:
622
  Item_func_tan(Item *a) :Item_dec_func(a) {}
623
  double val_real();
624
  const char *func_name() const { return "tan"; }
625
};
626
627
class Item_func_integer :public Item_int_func
628
{
629
public:
630
  inline Item_func_integer(Item *a) :Item_int_func(a) {}
631
  void fix_length_and_dec();
632
};
633
634
635
class Item_func_int_val :public Item_func_num1
636
{
637
public:
638
  Item_func_int_val(Item *a) :Item_func_num1(a) {}
639
  void fix_num_length_and_dec();
640
  void find_num_type();
641
};
642
643
644
class Item_func_ceiling :public Item_func_int_val
645
{
646
public:
647
  Item_func_ceiling(Item *a) :Item_func_int_val(a) {}
648
  const char *func_name() const { return "ceiling"; }
152 by Brian Aker
longlong replacement
649
  int64_t int_op();
1 by brian
clean slate
650
  double real_op();
651
  my_decimal *decimal_op(my_decimal *);
652
};
653
654
655
class Item_func_floor :public Item_func_int_val
656
{
657
public:
658
  Item_func_floor(Item *a) :Item_func_int_val(a) {}
659
  const char *func_name() const { return "floor"; }
152 by Brian Aker
longlong replacement
660
  int64_t int_op();
1 by brian
clean slate
661
  double real_op();
662
  my_decimal *decimal_op(my_decimal *);
663
};
664
665
/* This handles round and truncate */
666
667
class Item_func_round :public Item_func_num1
668
{
669
  bool truncate;
670
public:
671
  Item_func_round(Item *a, Item *b, bool trunc_arg)
672
    :Item_func_num1(a,b), truncate(trunc_arg) {}
673
  const char *func_name() const { return truncate ? "truncate" : "round"; }
674
  double real_op();
152 by Brian Aker
longlong replacement
675
  int64_t int_op();
1 by brian
clean slate
676
  my_decimal *decimal_op(my_decimal *);
677
  void fix_length_and_dec();
678
};
679
680
681
class Item_func_rand :public Item_real_func
682
{
683
  struct rand_struct *rand;
684
public:
685
  Item_func_rand(Item *a) :Item_real_func(a), rand(0) {}
686
  Item_func_rand()	  :Item_real_func() {}
687
  double val_real();
688
  const char *func_name() const { return "rand"; }
689
  bool const_item() const { return 0; }
690
  void update_used_tables();
691
  bool fix_fields(THD *thd, Item **ref);
692
private:
693
  void seed_random (Item * val);  
694
};
695
696
697
class Item_func_sign :public Item_int_func
698
{
699
public:
700
  Item_func_sign(Item *a) :Item_int_func(a) {}
701
  const char *func_name() const { return "sign"; }
152 by Brian Aker
longlong replacement
702
  int64_t val_int();
1 by brian
clean slate
703
};
704
705
706
class Item_func_units :public Item_real_func
707
{
708
  char *name;
709
  double mul,add;
710
public:
711
  Item_func_units(char *name_arg,Item *a,double mul_arg,double add_arg)
712
    :Item_real_func(a),name(name_arg),mul(mul_arg),add(add_arg) {}
713
  double val_real();
714
  const char *func_name() const { return name; }
715
  void fix_length_and_dec()
716
  { decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
717
};
718
719
720
class Item_func_min_max :public Item_func
721
{
722
  Item_result cmp_type;
723
  String tmp_value;
724
  int cmp_sign;
725
  /* TRUE <=> arguments should be compared in the DATETIME context. */
726
  bool compare_as_dates;
727
  /* An item used for issuing warnings while string to DATETIME conversion. */
728
  Item *datetime_item;
729
  THD *thd;
730
protected:
731
  enum_field_types cached_field_type;
732
public:
733
  Item_func_min_max(List<Item> &list,int cmp_sign_arg) :Item_func(list),
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
734
    cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg), compare_as_dates(false),
1 by brian
clean slate
735
    datetime_item(0) {}
736
  double val_real();
152 by Brian Aker
longlong replacement
737
  int64_t val_int();
1 by brian
clean slate
738
  String *val_str(String *);
739
  my_decimal *val_decimal(my_decimal *);
740
  void fix_length_and_dec();
741
  enum Item_result result_type () const { return cmp_type; }
152 by Brian Aker
longlong replacement
742
  bool result_as_int64_t() { return compare_as_dates; };
151 by Brian Aker
Ulonglong to uint64_t
743
  uint cmp_datetimes(uint64_t *value);
1 by brian
clean slate
744
  enum_field_types field_type() const { return cached_field_type; }
745
};
746
747
class Item_func_min :public Item_func_min_max
748
{
749
public:
750
  Item_func_min(List<Item> &list) :Item_func_min_max(list,1) {}
751
  const char *func_name() const { return "least"; }
752
};
753
754
class Item_func_max :public Item_func_min_max
755
{
756
public:
757
  Item_func_max(List<Item> &list) :Item_func_min_max(list,-1) {}
758
  const char *func_name() const { return "greatest"; }
759
};
760
761
762
/* 
763
  Objects of this class are used for ROLLUP queries to wrap up 
764
  each constant item referred to in GROUP BY list. 
765
*/
766
767
class Item_func_rollup_const :public Item_func
768
{
769
public:
770
  Item_func_rollup_const(Item *a) :Item_func(a)
771
  {
772
    name= a->name;
773
    name_length= a->name_length;
774
  }
775
  double val_real() { return args[0]->val_real(); }
152 by Brian Aker
longlong replacement
776
  int64_t val_int() { return args[0]->val_int(); }
1 by brian
clean slate
777
  String *val_str(String *str) { return args[0]->val_str(str); }
778
  my_decimal *val_decimal(my_decimal *dec) { return args[0]->val_decimal(dec); }
779
  const char *func_name() const { return "rollup_const"; }
780
  bool const_item() const { return 0; }
781
  Item_result result_type() const { return args[0]->result_type(); }
782
  void fix_length_and_dec()
783
  {
784
    collation= args[0]->collation;
785
    max_length= args[0]->max_length;
786
    decimals=args[0]->decimals; 
787
    /* The item could be a NULL constant. */
788
    null_value= args[0]->is_null();
789
  }
790
};
791
792
793
class Item_func_length :public Item_int_func
794
{
795
  String value;
796
public:
797
  Item_func_length(Item *a) :Item_int_func(a) {}
152 by Brian Aker
longlong replacement
798
  int64_t val_int();
1 by brian
clean slate
799
  const char *func_name() const { return "length"; }
800
  void fix_length_and_dec() { max_length=10; }
801
};
802
803
class Item_func_bit_length :public Item_func_length
804
{
805
public:
806
  Item_func_bit_length(Item *a) :Item_func_length(a) {}
152 by Brian Aker
longlong replacement
807
  int64_t val_int()
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
808
    { assert(fixed == 1); return Item_func_length::val_int()*8; }
1 by brian
clean slate
809
  const char *func_name() const { return "bit_length"; }
810
};
811
812
class Item_func_char_length :public Item_int_func
813
{
814
  String value;
815
public:
816
  Item_func_char_length(Item *a) :Item_int_func(a) {}
152 by Brian Aker
longlong replacement
817
  int64_t val_int();
1 by brian
clean slate
818
  const char *func_name() const { return "char_length"; }
819
  void fix_length_and_dec() { max_length=10; }
820
};
821
822
class Item_func_coercibility :public Item_int_func
823
{
824
public:
825
  Item_func_coercibility(Item *a) :Item_int_func(a) {}
152 by Brian Aker
longlong replacement
826
  int64_t val_int();
1 by brian
clean slate
827
  const char *func_name() const { return "coercibility"; }
828
  void fix_length_and_dec() { max_length=10; maybe_null= 0; }
829
  table_map not_null_tables() const { return 0; }
830
};
831
832
class Item_func_locate :public Item_int_func
833
{
834
  String value1,value2;
835
  DTCollation cmp_collation;
836
public:
837
  Item_func_locate(Item *a,Item *b) :Item_int_func(a,b) {}
838
  Item_func_locate(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
839
  const char *func_name() const { return "locate"; }
152 by Brian Aker
longlong replacement
840
  int64_t val_int();
1 by brian
clean slate
841
  void fix_length_and_dec();
842
  virtual void print(String *str, enum_query_type query_type);
843
};
844
845
846
class Item_func_field :public Item_int_func
847
{
848
  String value,tmp;
849
  Item_result cmp_type;
850
  DTCollation cmp_collation;
851
public:
852
  Item_func_field(List<Item> &list) :Item_int_func(list) {}
152 by Brian Aker
longlong replacement
853
  int64_t val_int();
1 by brian
clean slate
854
  const char *func_name() const { return "field"; }
855
  void fix_length_and_dec();
856
};
857
858
859
class Item_func_ascii :public Item_int_func
860
{
861
  String value;
862
public:
863
  Item_func_ascii(Item *a) :Item_int_func(a) {}
152 by Brian Aker
longlong replacement
864
  int64_t val_int();
1 by brian
clean slate
865
  const char *func_name() const { return "ascii"; }
866
  void fix_length_and_dec() { max_length=3; }
867
};
868
869
class Item_func_ord :public Item_int_func
870
{
871
  String value;
872
public:
873
  Item_func_ord(Item *a) :Item_int_func(a) {}
152 by Brian Aker
longlong replacement
874
  int64_t val_int();
1 by brian
clean slate
875
  const char *func_name() const { return "ord"; }
876
};
877
878
class Item_func_find_in_set :public Item_int_func
879
{
880
  String value,value2;
881
  uint enum_value;
151 by Brian Aker
Ulonglong to uint64_t
882
  uint64_t enum_bit;
1 by brian
clean slate
883
  DTCollation cmp_collation;
884
public:
885
  Item_func_find_in_set(Item *a,Item *b) :Item_int_func(a,b),enum_value(0) {}
152 by Brian Aker
longlong replacement
886
  int64_t val_int();
1 by brian
clean slate
887
  const char *func_name() const { return "find_in_set"; }
888
  void fix_length_and_dec();
889
};
890
891
/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
892
893
class Item_func_bit: public Item_int_func
894
{
895
public:
896
  Item_func_bit(Item *a, Item *b) :Item_int_func(a, b) {}
897
  Item_func_bit(Item *a) :Item_int_func(a) {}
898
  void fix_length_and_dec() { unsigned_flag= 1; }
899
900
  virtual inline void print(String *str, enum_query_type query_type)
901
  {
902
    print_op(str, query_type);
903
  }
904
};
905
906
class Item_func_bit_or :public Item_func_bit
907
{
908
public:
909
  Item_func_bit_or(Item *a, Item *b) :Item_func_bit(a, b) {}
152 by Brian Aker
longlong replacement
910
  int64_t val_int();
1 by brian
clean slate
911
  const char *func_name() const { return "|"; }
912
};
913
914
class Item_func_bit_and :public Item_func_bit
915
{
916
public:
917
  Item_func_bit_and(Item *a, Item *b) :Item_func_bit(a, b) {}
152 by Brian Aker
longlong replacement
918
  int64_t val_int();
1 by brian
clean slate
919
  const char *func_name() const { return "&"; }
920
};
921
922
class Item_func_bit_count :public Item_int_func
923
{
924
public:
925
  Item_func_bit_count(Item *a) :Item_int_func(a) {}
152 by Brian Aker
longlong replacement
926
  int64_t val_int();
1 by brian
clean slate
927
  const char *func_name() const { return "bit_count"; }
928
  void fix_length_and_dec() { max_length=2; }
929
};
930
931
class Item_func_shift_left :public Item_func_bit
932
{
933
public:
934
  Item_func_shift_left(Item *a, Item *b) :Item_func_bit(a, b) {}
152 by Brian Aker
longlong replacement
935
  int64_t val_int();
1 by brian
clean slate
936
  const char *func_name() const { return "<<"; }
937
};
938
939
class Item_func_shift_right :public Item_func_bit
940
{
941
public:
942
  Item_func_shift_right(Item *a, Item *b) :Item_func_bit(a, b) {}
152 by Brian Aker
longlong replacement
943
  int64_t val_int();
1 by brian
clean slate
944
  const char *func_name() const { return ">>"; }
945
};
946
947
class Item_func_bit_neg :public Item_func_bit
948
{
949
public:
950
  Item_func_bit_neg(Item *a) :Item_func_bit(a) {}
152 by Brian Aker
longlong replacement
951
  int64_t val_int();
1 by brian
clean slate
952
  const char *func_name() const { return "~"; }
953
954
  virtual inline void print(String *str, enum_query_type query_type)
955
  {
956
    Item_func::print(str, query_type);
957
  }
958
};
959
960
961
class Item_func_last_insert_id :public Item_int_func
962
{
963
public:
964
  Item_func_last_insert_id() :Item_int_func() {}
965
  Item_func_last_insert_id(Item *a) :Item_int_func(a) {}
152 by Brian Aker
longlong replacement
966
  int64_t val_int();
1 by brian
clean slate
967
  const char *func_name() const { return "last_insert_id"; }
968
  void fix_length_and_dec()
969
  {
970
    if (arg_count)
971
      max_length= args[0]->max_length;
972
  }
973
  bool fix_fields(THD *thd, Item **ref);
974
};
975
976
977
class Item_func_benchmark :public Item_int_func
978
{
979
public:
980
  Item_func_benchmark(Item *count_expr, Item *expr)
981
    :Item_int_func(count_expr, expr)
982
  {}
152 by Brian Aker
longlong replacement
983
  int64_t val_int();
1 by brian
clean slate
984
  const char *func_name() const { return "benchmark"; }
985
  void fix_length_and_dec() { max_length=1; maybe_null=0; }
986
  virtual void print(String *str, enum_query_type query_type);
987
};
988
989
990
class Item_udf_func :public Item_func
991
{
992
protected:
993
  udf_handler udf;
77.1.7 by Monty Taylor
Heap builds clean.
994
  bool is_expensive_processor(uchar *arg __attribute__((__unused__)))
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
995
  { return true; }
1 by brian
clean slate
996
997
public:
998
  Item_udf_func(udf_func *udf_arg)
999
    :Item_func(), udf(udf_arg) {}
1000
  Item_udf_func(udf_func *udf_arg, List<Item> &list)
1001
    :Item_func(list), udf(udf_arg) {}
1002
  const char *func_name() const { return udf.name(); }
1003
  enum Functype functype() const   { return UDF_FUNC; }
77.1.7 by Monty Taylor
Heap builds clean.
1004
  bool fix_fields(THD *thd, Item **ref __attribute__((__unused__)))
1 by brian
clean slate
1005
  {
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
1006
    assert(fixed == 0);
1 by brian
clean slate
1007
    bool res= udf.fix_fields(thd, this, arg_count, args);
1008
    used_tables_cache= udf.used_tables_cache;
1009
    const_item_cache= udf.const_item_cache;
1010
    fixed= 1;
1011
    return res;
1012
  }
1013
  void update_used_tables() 
1014
  {
1015
    /*
1016
      TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
1017
      not.
1018
      Currently UDF_INIT has a member (const_item) that is an in/out 
1019
      parameter to the init() call.
1020
      The code in udf_handler::fix_fields also duplicates the arguments 
1021
      handling code in Item_func::fix_fields().
1022
      
1023
      The lack of information if a UDF is deterministic makes writing
1024
      a correct update_used_tables() for UDFs impossible.
1025
      One solution to this would be :
1026
       - Add a is_deterministic member of UDF_INIT
1027
       - (optionally) deprecate the const_item member of UDF_INIT
1028
       - Take away the duplicate code from udf_handler::fix_fields() and
1029
         make Item_udf_func call Item_func::fix_fields() to process its 
1030
         arguments as for any other function.
1031
       - Store the deterministic flag returned by <udf>_init into the 
1032
       udf_handler. 
1033
       - Don't implement Item_udf_func::fix_fields, implement
1034
       Item_udf_func::fix_length_and_dec() instead (similar to non-UDF
1035
       functions).
1036
       - Override Item_func::update_used_tables to call 
1037
       Item_func::update_used_tables() and add a RAND_TABLE_BIT to the 
1038
       result of Item_func::update_used_tables() if the UDF is 
1039
       non-deterministic.
1040
       - (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
1041
       better describe its usage.
1042
       
1043
      The above would require a change of the UDF API.
1044
      Until that change is done here's how the current code works:
1045
      We call Item_func::update_used_tables() only when we know that
1046
      the function depends on real non-const tables and is deterministic.
1047
      This can be done only because we know that the optimizer will
1048
      call update_used_tables() only when there's possibly a new const
1049
      table. So update_used_tables() can only make a Item_func more
1050
      constant than it is currently.
1051
      That's why we don't need to do anything if a function is guaranteed
1052
      to return non-constant (it's non-deterministic) or is already a
1053
      const.
1054
    */  
1055
    if ((used_tables_cache & ~PSEUDO_TABLE_BITS) && 
1056
        !(used_tables_cache & RAND_TABLE_BIT))
1057
    {
1058
      Item_func::update_used_tables();
1059
      if (!const_item_cache && !used_tables_cache)
1060
        used_tables_cache= RAND_TABLE_BIT;
1061
    }
1062
  }
1063
  void cleanup();
1064
  Item_result result_type () const { return udf.result_type(); }
1065
  table_map not_null_tables() const { return 0; }
1066
  virtual void print(String *str, enum_query_type query_type);
1067
};
1068
1069
1070
class Item_func_udf_float :public Item_udf_func
1071
{
1072
 public:
1073
  Item_func_udf_float(udf_func *udf_arg)
1074
    :Item_udf_func(udf_arg) {}
1075
  Item_func_udf_float(udf_func *udf_arg,
1076
                      List<Item> &list)
1077
    :Item_udf_func(udf_arg, list) {}
152 by Brian Aker
longlong replacement
1078
  int64_t val_int()
1 by brian
clean slate
1079
  {
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
1080
    assert(fixed == 1);
152 by Brian Aker
longlong replacement
1081
    return (int64_t) rint(Item_func_udf_float::val_real());
1 by brian
clean slate
1082
  }
1083
  my_decimal *val_decimal(my_decimal *dec_buf)
1084
  {
1085
    double res=val_real();
1086
    if (null_value)
1087
      return NULL;
1088
    double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
1089
    return dec_buf;
1090
  }
1091
  double val_real();
1092
  String *val_str(String *str);
1093
  void fix_length_and_dec() { fix_num_length_and_dec(); }
1094
};
1095
1096
1097
class Item_func_udf_int :public Item_udf_func
1098
{
1099
public:
1100
  Item_func_udf_int(udf_func *udf_arg)
1101
    :Item_udf_func(udf_arg) {}
1102
  Item_func_udf_int(udf_func *udf_arg,
1103
                    List<Item> &list)
1104
    :Item_udf_func(udf_arg, list) {}
152 by Brian Aker
longlong replacement
1105
  int64_t val_int();
1 by brian
clean slate
1106
  double val_real() { return (double) Item_func_udf_int::val_int(); }
1107
  String *val_str(String *str);
1108
  enum Item_result result_type () const { return INT_RESULT; }
1109
  void fix_length_and_dec() { decimals= 0; max_length= 21; }
1110
};
1111
1112
1113
class Item_func_udf_decimal :public Item_udf_func
1114
{
1115
public:
1116
  Item_func_udf_decimal(udf_func *udf_arg)
1117
    :Item_udf_func(udf_arg) {}
1118
  Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
1119
    :Item_udf_func(udf_arg, list) {}
152 by Brian Aker
longlong replacement
1120
  int64_t val_int();
1 by brian
clean slate
1121
  double val_real();
1122
  my_decimal *val_decimal(my_decimal *);
1123
  String *val_str(String *str);
1124
  enum Item_result result_type () const { return DECIMAL_RESULT; }
1125
  void fix_length_and_dec();
1126
};
1127
1128
1129
class Item_func_udf_str :public Item_udf_func
1130
{
1131
public:
1132
  Item_func_udf_str(udf_func *udf_arg)
1133
    :Item_udf_func(udf_arg) {}
1134
  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
1135
    :Item_udf_func(udf_arg, list) {}
1136
  String *val_str(String *);
1137
  double val_real()
1138
  {
1139
    int err_not_used;
1140
    char *end_not_used;
1141
    String *res;
1142
    res= val_str(&str_value);
1143
    return res ? my_strntod(res->charset(),(char*) res->ptr(), 
1144
                            res->length(), &end_not_used, &err_not_used) : 0.0;
1145
  }
152 by Brian Aker
longlong replacement
1146
  int64_t val_int()
1 by brian
clean slate
1147
  {
1148
    int err_not_used;
1149
    String *res;  res=val_str(&str_value);
1150
    return res ? my_strntoll(res->charset(),res->ptr(),res->length(),10,
152 by Brian Aker
longlong replacement
1151
                             (char**) 0, &err_not_used) : (int64_t) 0;
1 by brian
clean slate
1152
  }
1153
  my_decimal *val_decimal(my_decimal *dec_buf)
1154
  {
1155
    String *res=val_str(&str_value);
1156
    if (!res)
1157
      return NULL;
1158
    string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
1159
    return dec_buf;
1160
  }
1161
  enum Item_result result_type () const { return STRING_RESULT; }
1162
  void fix_length_and_dec();
1163
};
1164
1165
1166
/* replication functions */
1167
1168
class Item_master_pos_wait :public Item_int_func
1169
{
1170
  String value;
1171
public:
1172
  Item_master_pos_wait(Item *a,Item *b) :Item_int_func(a,b) {}
1173
  Item_master_pos_wait(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
152 by Brian Aker
longlong replacement
1174
  int64_t val_int();
1 by brian
clean slate
1175
  const char *func_name() const { return "master_pos_wait"; }
1176
  void fix_length_and_dec() { max_length=21; maybe_null=1;}
1177
};
1178
1179
1180
/* Handling of user definable variables */
1181
1182
class user_var_entry;
1183
1184
class Item_func_set_user_var :public Item_func
1185
{
1186
  enum Item_result cached_result_type;
1187
  user_var_entry *entry;
1188
  char buffer[MAX_FIELD_WIDTH];
1189
  String value;
1190
  my_decimal decimal_buff;
1191
  bool null_item;
1192
  union
1193
  {
152 by Brian Aker
longlong replacement
1194
    int64_t vint;
1 by brian
clean slate
1195
    double vreal;
1196
    String *vstr;
1197
    my_decimal *vdec;
1198
  } save_result;
1199
1200
public:
1201
  LEX_STRING name; // keep it public
1202
  Item_func_set_user_var(LEX_STRING a,Item *b)
1203
    :Item_func(b), cached_result_type(INT_RESULT), name(a)
1204
  {}
1205
  enum Functype functype() const { return SUSERVAR_FUNC; }
1206
  double val_real();
152 by Brian Aker
longlong replacement
1207
  int64_t val_int();
1 by brian
clean slate
1208
  String *val_str(String *str);
1209
  my_decimal *val_decimal(my_decimal *);
1210
  double val_result();
152 by Brian Aker
longlong replacement
1211
  int64_t val_int_result();
1 by brian
clean slate
1212
  String *str_result(String *str);
1213
  my_decimal *val_decimal_result(my_decimal *);
1214
  bool update_hash(void *ptr, uint length, enum Item_result type,
1215
  		   CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
1216
  bool send(Protocol *protocol, String *str_arg);
1217
  void make_field(Send_field *tmp_field);
1218
  bool check(bool use_result_field);
1219
  bool update();
1220
  enum Item_result result_type () const { return cached_result_type; }
1221
  bool fix_fields(THD *thd, Item **ref);
1222
  void fix_length_and_dec();
1223
  virtual void print(String *str, enum_query_type query_type);
1224
  void print_as_stmt(String *str, enum_query_type query_type);
1225
  const char *func_name() const { return "set_user_var"; }
1226
  int save_in_field(Field *field, bool no_conversions,
1227
                    bool can_use_result_field);
1228
  int save_in_field(Field *field, bool no_conversions)
1229
  {
1230
    return save_in_field(field, no_conversions, 1);
1231
  }
1232
  void save_org_in_field(Field *field) { (void)save_in_field(field, 1, 0); }
1233
  bool register_field_in_read_map(uchar *arg);
1234
};
1235
1236
1237
class Item_func_get_user_var :public Item_func
1238
{
1239
  user_var_entry *var_entry;
1240
  Item_result m_cached_result_type;
1241
1242
public:
1243
  LEX_STRING name; // keep it public
1244
  Item_func_get_user_var(LEX_STRING a):
1245
    Item_func(), m_cached_result_type(STRING_RESULT), name(a) {}
1246
  enum Functype functype() const { return GUSERVAR_FUNC; }
1247
  LEX_STRING get_name() { return name; }
1248
  double val_real();
152 by Brian Aker
longlong replacement
1249
  int64_t val_int();
1 by brian
clean slate
1250
  my_decimal *val_decimal(my_decimal*);
1251
  String *val_str(String* str);
1252
  void fix_length_and_dec();
1253
  virtual void print(String *str, enum_query_type query_type);
1254
  enum Item_result result_type() const;
1255
  /*
1256
    We must always return variables as strings to guard against selects of type
1257
    select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
1258
  */
1259
  const char *func_name() const { return "get_user_var"; }
1260
  bool const_item() const;
1261
  table_map used_tables() const
1262
  { return const_item() ? 0 : RAND_TABLE_BIT; }
1263
  bool eq(const Item *item, bool binary_cmp) const;
1264
};
1265
1266
1267
/*
1268
  This item represents user variable used as out parameter (e.g in LOAD DATA),
1269
  and it is supposed to be used only for this purprose. So it is simplified
1270
  a lot. Actually you should never obtain its value.
1271
1272
  The only two reasons for this thing being an Item is possibility to store it
1273
  in List<Item> and desire to place this code somewhere near other functions
1274
  working with user variables.
1275
*/
1276
class Item_user_var_as_out_param :public Item
1277
{
1278
  LEX_STRING name;
1279
  user_var_entry *entry;
1280
public:
1281
  Item_user_var_as_out_param(LEX_STRING a) : name(a) {}
1282
  /* We should return something different from FIELD_ITEM here */
1283
  enum Type type() const { return STRING_ITEM;}
1284
  double val_real();
152 by Brian Aker
longlong replacement
1285
  int64_t val_int();
1 by brian
clean slate
1286
  String *val_str(String *str);
1287
  my_decimal *val_decimal(my_decimal *decimal_buffer);
1288
  /* fix_fields() binds variable name with its entry structure */
1289
  bool fix_fields(THD *thd, Item **ref);
1290
  virtual void print(String *str, enum_query_type query_type);
1291
  void set_null_value(CHARSET_INFO* cs);
1292
  void set_value(const char *str, uint length, CHARSET_INFO* cs);
1293
};
1294
1295
1296
/* A system variable */
1297
1298
class Item_func_get_system_var :public Item_func
1299
{
1300
  sys_var *var;
1301
  enum_var_type var_type;
1302
  LEX_STRING component;
1303
public:
1304
  Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
1305
                           LEX_STRING *component_arg, const char *name_arg,
1306
                           size_t name_len_arg);
1307
  bool fix_fields(THD *thd, Item **ref);
1308
  /*
1309
    Stubs for pure virtual methods. Should never be called: this
1310
    item is always substituted with a constant in fix_fields().
1311
  */
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
1312
  double val_real()         { assert(0); return 0.0; }
152 by Brian Aker
longlong replacement
1313
  int64_t val_int()        { assert(0); return 0; }
51.1.20 by Jay Pipes
Removed/replaced DBUG symbols and fixed TRUE/FALSEs
1314
  String* val_str(String*)  { assert(0); return 0; }
1315
  void fix_length_and_dec() { assert(0); }
1 by brian
clean slate
1316
  /* TODO: fix to support views */
1317
  const char *func_name() const { return "get_system_var"; }
1318
  /**
1319
    Indicates whether this system variable is written to the binlog or not.
1320
1321
    Variables are written to the binlog as part of "status_vars" in
1322
    Query_log_event, as an Intvar_log_event, or a Rand_log_event.
1323
1324
    @return true if the variable is written to the binlog, false otherwise.
1325
  */
1326
  bool is_written_to_binlog();
1327
};
1328
1329
class Item_func_bit_xor : public Item_func_bit
1330
{
1331
public:
1332
  Item_func_bit_xor(Item *a, Item *b) :Item_func_bit(a, b) {}
152 by Brian Aker
longlong replacement
1333
  int64_t val_int();
1 by brian
clean slate
1334
  const char *func_name() const { return "^"; }
1335
};
1336
1337
class Item_func_is_free_lock :public Item_int_func
1338
{
1339
  String value;
1340
public:
1341
  Item_func_is_free_lock(Item *a) :Item_int_func(a) {}
152 by Brian Aker
longlong replacement
1342
  int64_t val_int();
1 by brian
clean slate
1343
  const char *func_name() const { return "is_free_lock"; }
1344
  void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;}
1345
};
1346
1347
class Item_func_is_used_lock :public Item_int_func
1348
{
1349
  String value;
1350
public:
1351
  Item_func_is_used_lock(Item *a) :Item_int_func(a) {}
152 by Brian Aker
longlong replacement
1352
  int64_t val_int();
1 by brian
clean slate
1353
  const char *func_name() const { return "is_used_lock"; }
1354
  void fix_length_and_dec() { decimals=0; max_length=10; maybe_null=1;}
1355
};
1356
1357
/* For type casts */
1358
1359
enum Cast_target
1360
{
1361
  ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
1362
  ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR,
1363
  ITEM_CAST_DECIMAL
1364
};
1365
1366
1367
class Item_func_row_count :public Item_int_func
1368
{
1369
public:
1370
  Item_func_row_count() :Item_int_func() {}
152 by Brian Aker
longlong replacement
1371
  int64_t val_int();
1 by brian
clean slate
1372
  const char *func_name() const { return "row_count"; }
1373
  void fix_length_and_dec() { decimals= 0; maybe_null=0; }
1374
};
1375
1376
1377
/*
1378
 *
1379
 * Stored FUNCTIONs
1380
 *
1381
 */
1382
1383
struct st_sp_security_context;
1384
1385
class Item_func_found_rows :public Item_int_func
1386
{
1387
public:
1388
  Item_func_found_rows() :Item_int_func() {}
152 by Brian Aker
longlong replacement
1389
  int64_t val_int();
1 by brian
clean slate
1390
  const char *func_name() const { return "found_rows"; }
1391
  void fix_length_and_dec() { decimals= 0; maybe_null=0; }
1392
};