item_func.h 34.6 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2

unknown's avatar
unknown committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
7

unknown's avatar
unknown committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
12

unknown's avatar
unknown committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


/* Function items used by mysql */

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

#ifdef HAVE_IEEEFP_H
extern "C"				/* Bug in BSDI include file */
{
#include <ieeefp.h>
}
#endif

class Item_func :public Item_result_field
{
protected:
34
  Item **args, *tmp_arg[2];
35 36 37 38
  /*
    Allowed numbers of columns in result (usually 1, which means scalar value)
    0 means get this number from first argument
  */
unknown's avatar
unknown committed
39
  uint allowed_arg_cols;
unknown's avatar
unknown committed
40 41
public:
  uint arg_count;
42
  table_map used_tables_cache, not_null_tables_cache;
unknown's avatar
unknown committed
43 44 45 46
  bool const_item_cache;
  enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC,
		  GE_FUNC,GT_FUNC,FT_FUNC,
		  LIKE_FUNC,NOTLIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC,
47 48
		  COND_AND_FUNC, COND_OR_FUNC, COND_XOR_FUNC,
                  BETWEEN, IN_FUNC, MULT_EQUAL_FUNC,
49
		  INTERVAL_FUNC, ISNOTNULLTEST_FUNC,
unknown's avatar
unknown committed
50 51 52 53
		  SP_EQUALS_FUNC, SP_DISJOINT_FUNC,SP_INTERSECTS_FUNC,
		  SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC,
		  SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC,
		  SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING,
54
		  SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN,
unknown's avatar
unknown committed
55 56
                  NOT_FUNC, NOT_ALL_FUNC,
                  NOW_FUNC, TRIG_COND_FUNC,
57
                  GUSERVAR_FUNC};
58 59
  enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL,
                       OPTIMIZE_EQUAL };
unknown's avatar
unknown committed
60 61
  enum Type type() const { return FUNC_ITEM; }
  virtual enum Functype functype() const   { return UNKNOWN_FUNC; }
unknown's avatar
unknown committed
62 63
  Item_func(void):
    allowed_arg_cols(1), arg_count(0)
unknown's avatar
unknown committed
64
  {
65
    with_sum_func= 0;
unknown's avatar
unknown committed
66
  }
unknown's avatar
unknown committed
67 68
  Item_func(Item *a):
    allowed_arg_cols(1), arg_count(1)
unknown's avatar
unknown committed
69
  {
70
    args= tmp_arg;
71 72
    args[0]= a;
    with_sum_func= a->with_sum_func;
unknown's avatar
unknown committed
73
  }
unknown's avatar
unknown committed
74 75
  Item_func(Item *a,Item *b):
    allowed_arg_cols(1), arg_count(2)
unknown's avatar
unknown committed
76
  {
77
    args= tmp_arg;
78 79
    args[0]= a; args[1]= b;
    with_sum_func= a->with_sum_func || b->with_sum_func;
unknown's avatar
unknown committed
80
  }
unknown's avatar
unknown committed
81 82
  Item_func(Item *a,Item *b,Item *c):
    allowed_arg_cols(1)
unknown's avatar
unknown committed
83
  {
84 85
    arg_count= 0;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*3)))
unknown's avatar
unknown committed
86
    {
87 88 89
      arg_count= 3;
      args[0]= a; args[1]= b; args[2]= c;
      with_sum_func= a->with_sum_func || b->with_sum_func || c->with_sum_func;
unknown's avatar
unknown committed
90 91
    }
  }
unknown's avatar
unknown committed
92 93
  Item_func(Item *a,Item *b,Item *c,Item *d):
    allowed_arg_cols(1)
unknown's avatar
unknown committed
94
  {
95 96
    arg_count= 0;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*4)))
unknown's avatar
unknown committed
97
    {
98 99 100 101
      arg_count= 4;
      args[0]= a; args[1]= b; args[2]= c; args[3]= d;
      with_sum_func= a->with_sum_func || b->with_sum_func ||
	c->with_sum_func || d->with_sum_func;
unknown's avatar
unknown committed
102 103
    }
  }
unknown's avatar
unknown committed
104 105
  Item_func(Item *a,Item *b,Item *c,Item *d,Item* e):
    allowed_arg_cols(1)
unknown's avatar
unknown committed
106
  {
107 108
    arg_count= 5;
    if ((args= (Item**) sql_alloc(sizeof(Item*)*5)))
unknown's avatar
unknown committed
109
    {
110 111 112
      args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
      with_sum_func= a->with_sum_func || b->with_sum_func ||
	c->with_sum_func || d->with_sum_func || e->with_sum_func ;
unknown's avatar
unknown committed
113 114 115
    }
  }
  Item_func(List<Item> &list);
116
  // Constructor used for Item_cond_and/or (see Item comment)
117
  Item_func(THD *thd, Item_func *item);
unknown's avatar
unknown committed
118
  bool fix_fields(THD *,struct st_table_list *, Item **ref);
unknown's avatar
unknown committed
119
  table_map used_tables() const;
120
  table_map not_null_tables() const;
unknown's avatar
unknown committed
121
  void update_used_tables();
122
  bool eq(const Item *item, bool binary_cmp) const;
unknown's avatar
unknown committed
123 124 125 126 127 128
  virtual optimize_type select_optimize() const { return OPTIMIZE_NONE; }
  virtual bool have_rev_func() const { return 0; }
  virtual Item *key_item() const { return args[0]; }
  virtual const char *func_name() const { return "?"; }
  virtual bool const_item() const { return const_item_cache; }
  inline Item **arguments() const { return args; }
129
  void set_arguments(List<Item> &list);
unknown's avatar
unknown committed
130 131
  inline uint argument_count() const { return arg_count; }
  inline void remove_arguments() { arg_count=0; }
132
  void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
unknown's avatar
unknown committed
133 134
  void print(String *str);
  void print_op(String *str);
unknown's avatar
unknown committed
135
  void print_args(String *str, uint from);
unknown's avatar
unknown committed
136 137 138 139
  virtual void fix_num_length_and_dec();
  void count_only_length();
  void count_real_length();
  void count_decimal_length();
140
  inline bool get_arg0_date(TIME *ltime, uint fuzzy_date)
unknown's avatar
unknown committed
141
  {
142
    return (null_value=args[0]->get_date(ltime, fuzzy_date));
unknown's avatar
unknown committed
143 144 145 146 147
  }
  inline bool get_arg0_time(TIME *ltime)
  {
    return (null_value=args[0]->get_time(ltime));
  }
unknown's avatar
unknown committed
148
  bool is_null() { (void) val_int(); return null_value; }
unknown's avatar
unknown committed
149
  void signal_divide_by_null();
unknown's avatar
unknown committed
150
  friend class udf_handler;
151
  Field *tmp_table_field() { return result_field; }
152
  Field *tmp_table_field(TABLE *t_arg);
153
  Item *get_tmp_table_item(THD *thd);
unknown's avatar
unknown committed
154 155 156

  my_decimal *val_decimal(my_decimal *);

157
  bool agg_arg_collations(DTCollation &c, Item **items, uint nitems,
158
                          uint flags= 0);
159 160
  bool agg_arg_collations_for_comparison(DTCollation &c,
                                         Item **items, uint nitems,
161
                                         uint flags= 0);
unknown's avatar
unknown committed
162 163
  bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems,
                        uint flags= 0);
unknown's avatar
unknown committed
164
  bool walk(Item_processor processor, byte *arg);
unknown's avatar
unknown committed
165
  Item *transform(Item_transformer transformer, byte *arg);
166 167
  void traverse_cond(Cond_traverser traverser,
                     void * arg, traverse_order order);
unknown's avatar
unknown committed
168 169 170 171 172 173 174 175 176 177 178
};


class Item_real_func :public Item_func
{
public:
  Item_real_func() :Item_func() {}
  Item_real_func(Item *a) :Item_func(a) {}
  Item_real_func(Item *a,Item *b) :Item_func(a,b) {}
  Item_real_func(List<Item> &list) :Item_func(list) {}
  String *val_str(String*str);
179 180
  longlong val_int()
    { DBUG_ASSERT(fixed == 1); return (longlong) val_real(); }
unknown's avatar
unknown committed
181
  enum Item_result result_type () const { return REAL_RESULT; }
unknown's avatar
unknown committed
182 183
  void fix_length_and_dec()
  { decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
unknown's avatar
unknown committed
184 185
};

186

unknown's avatar
unknown committed
187
class Item_func_numhybrid: public Item_func
unknown's avatar
unknown committed
188
{
unknown's avatar
unknown committed
189
protected:
unknown's avatar
unknown committed
190 191
  Item_result hybrid_type;
public:
unknown's avatar
unknown committed
192 193 194 195 196 197
  Item_func_numhybrid(Item *a) :Item_func(a),hybrid_type(REAL_RESULT)
  {}
  Item_func_numhybrid(Item *a,Item *b)
    :Item_func(a,b),hybrid_type(REAL_RESULT)
  {}

unknown's avatar
unknown committed
198
  enum Item_result result_type () const { return hybrid_type; }
unknown's avatar
unknown committed
199 200 201 202 203 204 205 206 207 208 209 210
  void fix_length_and_dec();
  void fix_num_length_and_dec();
  virtual void find_num_type()= 0; /* To be called from fix_length_and_dec */

  double val_real();
  longlong val_int();
  my_decimal *val_decimal(my_decimal *);
  String *val_str(String*str);

  virtual longlong int_op()= 0;
  virtual double real_op()= 0;
  virtual my_decimal *decimal_op(my_decimal *)= 0;
211
  bool is_null() { (void) val_real(); return null_value; }
unknown's avatar
unknown committed
212 213
};

unknown's avatar
unknown committed
214 215 216 217 218 219
/* function where type of result detected by first argument */
class Item_func_num1: public Item_func_numhybrid
{
public:
  Item_func_num1(Item *a) :Item_func_numhybrid(a) {}
  Item_func_num1(Item *a, Item *b) :Item_func_numhybrid(a, b) {}
unknown's avatar
unknown committed
220

unknown's avatar
unknown committed
221 222 223
  void fix_num_length_and_dec();
  void find_num_type();
};
unknown's avatar
unknown committed
224

unknown's avatar
unknown committed
225 226 227

/* Base class for operations like '+', '-', '*' */
class Item_num_op :public Item_func_numhybrid
unknown's avatar
unknown committed
228 229
{
 public:
unknown's avatar
unknown committed
230 231
  Item_num_op(Item *a,Item *b) :Item_func_numhybrid(a, b) {}
  virtual void result_precision()= 0;
unknown's avatar
unknown committed
232
  void print(String *str) { print_op(str); }
unknown's avatar
unknown committed
233
  void find_num_type();
unknown's avatar
unknown committed
234 235 236 237 238 239
};


class Item_int_func :public Item_func
{
public:
unknown's avatar
unknown committed
240 241 242 243 244 245
  Item_int_func() :Item_func() { max_length= 21; }
  Item_int_func(Item *a) :Item_func(a) { max_length= 21; }
  Item_int_func(Item *a,Item *b) :Item_func(a,b) { max_length= 21; }
  Item_int_func(Item *a,Item *b,Item *c) :Item_func(a,b,c)
  { max_length= 21; }
  Item_int_func(List<Item> &list) :Item_func(list) { max_length= 21; }
246
  Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item) {}
247
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
unknown's avatar
unknown committed
248 249
  String *val_str(String*str);
  enum Item_result result_type () const { return INT_RESULT; }
unknown's avatar
unknown committed
250
  void fix_length_and_dec() {}
unknown's avatar
unknown committed
251 252
};

253

254 255 256 257
class Item_func_signed :public Item_int_func
{
public:
  Item_func_signed(Item *a) :Item_int_func(a) {}
258
  const char *func_name() const { return "cast_as_signed"; }
259
  double val_real()
unknown's avatar
unknown committed
260
  {
261
    double tmp= args[0]->val_real();
unknown's avatar
unknown committed
262 263 264 265 266 267 268 269 270
    null_value= args[0]->null_value;
    return tmp;
  }
  longlong val_int()
  {
    longlong tmp= args[0]->val_int();
    null_value= args[0]->null_value; 
    return tmp;
  }
271
  void fix_length_and_dec()
unknown's avatar
unknown committed
272
  { max_length=args[0]->max_length; unsigned_flag=0; }
273
  void print(String *str);
274 275
};

276

unknown's avatar
unknown committed
277
class Item_func_unsigned :public Item_func_signed
278 279
{
public:
unknown's avatar
unknown committed
280
  Item_func_unsigned(Item *a) :Item_func_signed(a) {}
281
  const char *func_name() const { return "cast_as_unsigned"; }
282
  void fix_length_and_dec()
unknown's avatar
unknown committed
283
  { max_length=args[0]->max_length; unsigned_flag=1; }
284
  void print(String *str);
285 286 287
};


unknown's avatar
unknown committed
288
class Item_decimal_typecast :public Item_func
unknown's avatar
unknown committed
289
{
unknown's avatar
unknown committed
290
  my_decimal decimal_value;
unknown's avatar
unknown committed
291
public:
unknown's avatar
unknown committed
292 293 294 295 296 297
  Item_decimal_typecast(Item *a, int len, int dec) :Item_func(a)
  {
    max_length= len + 2;
    decimals= dec;
  }
  String *val_str(String *str);
298
  double val_real();
unknown's avatar
unknown committed
299
  longlong val_int();
unknown's avatar
unknown committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
  my_decimal *val_decimal(my_decimal*);
  enum Item_result result_type () const { return DECIMAL_RESULT; }
  enum_field_types field_type() const { return MYSQL_TYPE_DECIMAL; }
  void fix_length_and_dec() {};
};


class Item_func_additive_op :public Item_num_op
{
public:
  Item_func_additive_op(Item *a,Item *b) :Item_num_op(a,b) {}
  void result_precision();
};


class Item_func_plus :public Item_func_additive_op
{
public:
  Item_func_plus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
  const char *func_name() const { return "+"; }
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
unknown's avatar
unknown committed
323 324
};

unknown's avatar
unknown committed
325
class Item_func_minus :public Item_func_additive_op
unknown's avatar
unknown committed
326 327
{
public:
unknown's avatar
unknown committed
328
  Item_func_minus(Item *a,Item *b) :Item_func_additive_op(a,b) {}
unknown's avatar
unknown committed
329
  const char *func_name() const { return "-"; }
unknown's avatar
unknown committed
330 331 332
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
333
  void fix_length_and_dec();
unknown's avatar
unknown committed
334 335
};

336

unknown's avatar
unknown committed
337 338 339 340 341
class Item_func_mul :public Item_num_op
{
public:
  Item_func_mul(Item *a,Item *b) :Item_num_op(a,b) {}
  const char *func_name() const { return "*"; }
unknown's avatar
unknown committed
342 343 344 345
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
  void result_precision();
unknown's avatar
unknown committed
346 347 348 349 350 351 352
};


class Item_func_div :public Item_num_op
{
public:
  Item_func_div(Item *a,Item *b) :Item_num_op(a,b) {}
353
  longlong int_op() { DBUG_ASSERT(0); return 0; }
unknown's avatar
unknown committed
354 355
  double real_op();
  my_decimal *decimal_op(my_decimal *);
unknown's avatar
unknown committed
356 357
  const char *func_name() const { return "/"; }
  void fix_length_and_dec();
unknown's avatar
unknown committed
358
  void result_precision();
unknown's avatar
unknown committed
359 360 361
};


362
class Item_func_int_div :public Item_int_func
363 364
{
public:
365
  Item_func_int_div(Item *a,Item *b) :Item_int_func(a,b)
unknown's avatar
unknown committed
366
  {}
367 368 369
  longlong val_int();
  const char *func_name() const { return "DIV"; }
  void fix_length_and_dec();
unknown's avatar
unknown committed
370
  void print(String *str) { print_op(str); }
371 372 373
};


unknown's avatar
unknown committed
374 375 376 377
class Item_func_mod :public Item_num_op
{
public:
  Item_func_mod(Item *a,Item *b) :Item_num_op(a,b) {}
unknown's avatar
unknown committed
378 379 380
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
unknown's avatar
unknown committed
381
  const char *func_name() const { return "%"; }
unknown's avatar
unknown committed
382 383 384 385
  void result_precision();
};


386
class Item_func_neg :public Item_func_num1
unknown's avatar
unknown committed
387 388
{
public:
389
  Item_func_neg(Item *a) :Item_func_num1(a) {}
unknown's avatar
unknown committed
390 391 392
  double real_op();
  longlong int_op();
  my_decimal *decimal_op(my_decimal *);
unknown's avatar
unknown committed
393
  const char *func_name() const { return "-"; }
394
  void fix_length_and_dec();
unknown's avatar
unknown committed
395
  void fix_num_length_and_dec();
unknown's avatar
unknown committed
396 397 398
};


unknown's avatar
unknown committed
399
class Item_func_abs :public Item_func_num1
unknown's avatar
unknown committed
400 401
{
public:
unknown's avatar
unknown committed
402 403 404 405
  Item_func_abs(Item *a) :Item_func_num1(a) {}
  double real_op();
  longlong int_op();
  my_decimal *decimal_op(my_decimal *);
unknown's avatar
unknown committed
406 407 408 409
  const char *func_name() const { return "abs"; }
  void fix_length_and_dec();
};

unknown's avatar
unknown committed
410
// A class to handle logarithmic and trigonometric functions
unknown's avatar
unknown committed
411 412 413 414 415 416 417 418

class Item_dec_func :public Item_real_func
{
 public:
  Item_dec_func(Item *a) :Item_real_func(a) {}
  Item_dec_func(Item *a,Item *b) :Item_real_func(a,b) {}
  void fix_length_and_dec()
  {
419
    decimals=NOT_FIXED_DEC; max_length=float_length(decimals);
unknown's avatar
unknown committed
420 421 422 423 424 425 426
    maybe_null=1;
  }
  inline double fix_result(double value)
  {
#ifndef HAVE_FINITE
    return value;
#else
427
    /* The following should be safe, even if we compare doubles */
unknown's avatar
unknown committed
428 429 430 431 432 433 434 435 436 437 438 439
    if (finite(value) && value != POSTFIX_ERROR)
      return value;
    null_value=1;
    return 0.0;
#endif
  }
};

class Item_func_exp :public Item_dec_func
{
public:
  Item_func_exp(Item *a) :Item_dec_func(a) {}
440
  double val_real();
unknown's avatar
unknown committed
441 442 443
  const char *func_name() const { return "exp"; }
};

444 445 446 447 448

class Item_func_ln :public Item_dec_func
{
public:
  Item_func_ln(Item *a) :Item_dec_func(a) {}
449
  double val_real();
450 451 452 453
  const char *func_name() const { return "ln"; }
};


unknown's avatar
unknown committed
454 455 456 457
class Item_func_log :public Item_dec_func
{
public:
  Item_func_log(Item *a) :Item_dec_func(a) {}
458
  Item_func_log(Item *a,Item *b) :Item_dec_func(a,b) {}
459
  double val_real();
unknown's avatar
unknown committed
460 461 462 463
  const char *func_name() const { return "log"; }
};


464 465 466 467
class Item_func_log2 :public Item_dec_func
{
public:
  Item_func_log2(Item *a) :Item_dec_func(a) {}
468
  double val_real();
469 470 471 472
  const char *func_name() const { return "log2"; }
};


unknown's avatar
unknown committed
473 474 475 476
class Item_func_log10 :public Item_dec_func
{
public:
  Item_func_log10(Item *a) :Item_dec_func(a) {}
477
  double val_real();
unknown's avatar
unknown committed
478 479 480 481 482 483 484 485
  const char *func_name() const { return "log10"; }
};


class Item_func_sqrt :public Item_dec_func
{
public:
  Item_func_sqrt(Item *a) :Item_dec_func(a) {}
486
  double val_real();
unknown's avatar
unknown committed
487 488 489 490 491 492 493 494
  const char *func_name() const { return "sqrt"; }
};


class Item_func_pow :public Item_dec_func
{
public:
  Item_func_pow(Item *a,Item *b) :Item_dec_func(a,b) {}
495
  double val_real();
unknown's avatar
unknown committed
496 497 498 499 500 501 502 503
  const char *func_name() const { return "pow"; }
};


class Item_func_acos :public Item_dec_func
{
 public:
  Item_func_acos(Item *a) :Item_dec_func(a) {}
504
  double val_real();
unknown's avatar
unknown committed
505 506 507 508 509 510 511
  const char *func_name() const { return "acos"; }
};

class Item_func_asin :public Item_dec_func
{
 public:
  Item_func_asin(Item *a) :Item_dec_func(a) {}
512
  double val_real();
unknown's avatar
unknown committed
513 514 515 516 517 518 519 520
  const char *func_name() const { return "asin"; }
};

class Item_func_atan :public Item_dec_func
{
 public:
  Item_func_atan(Item *a) :Item_dec_func(a) {}
  Item_func_atan(Item *a,Item *b) :Item_dec_func(a,b) {}
521
  double val_real();
unknown's avatar
unknown committed
522 523 524 525 526 527 528
  const char *func_name() const { return "atan"; }
};

class Item_func_cos :public Item_dec_func
{
 public:
  Item_func_cos(Item *a) :Item_dec_func(a) {}
529
  double val_real();
unknown's avatar
unknown committed
530 531 532 533 534 535 536
  const char *func_name() const { return "cos"; }
};

class Item_func_sin :public Item_dec_func
{
 public:
  Item_func_sin(Item *a) :Item_dec_func(a) {}
537
  double val_real();
unknown's avatar
unknown committed
538 539 540 541 542 543 544
  const char *func_name() const { return "sin"; }
};

class Item_func_tan :public Item_dec_func
{
 public:
  Item_func_tan(Item *a) :Item_dec_func(a) {}
545
  double val_real();
unknown's avatar
unknown committed
546 547 548 549 550 551 552 553 554 555 556
  const char *func_name() const { return "tan"; }
};

class Item_func_integer :public Item_int_func
{
public:
  inline Item_func_integer(Item *a) :Item_int_func(a) {}
  void fix_length_and_dec();
};


unknown's avatar
unknown committed
557 558 559 560 561 562 563 564 565 566
class Item_func_int_val :public Item_func_num1
{
public:
  Item_func_int_val(Item *a) :Item_func_num1(a) {}
  void fix_num_length_and_dec();
  void find_num_type();
};


class Item_func_ceiling :public Item_func_int_val
unknown's avatar
unknown committed
567 568
{
public:
unknown's avatar
unknown committed
569
  Item_func_ceiling(Item *a) :Item_func_int_val(a) {}
unknown's avatar
unknown committed
570
  const char *func_name() const { return "ceiling"; }
unknown's avatar
unknown committed
571 572 573
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
unknown's avatar
unknown committed
574 575
};

unknown's avatar
unknown committed
576 577

class Item_func_floor :public Item_func_int_val
unknown's avatar
unknown committed
578 579
{
public:
unknown's avatar
unknown committed
580
  Item_func_floor(Item *a) :Item_func_int_val(a) {}
unknown's avatar
unknown committed
581
  const char *func_name() const { return "floor"; }
unknown's avatar
unknown committed
582 583 584
  longlong int_op();
  double real_op();
  my_decimal *decimal_op(my_decimal *);
unknown's avatar
unknown committed
585 586 587 588
};

/* This handles round and truncate */

unknown's avatar
unknown committed
589
class Item_func_round :public Item_func_num1
unknown's avatar
unknown committed
590 591 592
{
  bool truncate;
public:
unknown's avatar
unknown committed
593 594
  Item_func_round(Item *a, Item *b, bool trunc_arg)
    :Item_func_num1(a,b), truncate(trunc_arg) {}
unknown's avatar
unknown committed
595
  const char *func_name() const { return truncate ? "truncate" : "round"; }
unknown's avatar
unknown committed
596 597 598 599
  double real_op();
  longlong int_op();
  my_decimal *decimal_op(my_decimal *);
  void fix_num_length_and_dec();
unknown's avatar
unknown committed
600 601 602 603 604
};


class Item_func_rand :public Item_real_func
{
605
  struct rand_struct *rand;
unknown's avatar
unknown committed
606
public:
607 608
  Item_func_rand(Item *a) :Item_real_func(a), rand(0) {}
  Item_func_rand()	  :Item_real_func() {}
609
  double val_real();
unknown's avatar
unknown committed
610 611
  const char *func_name() const { return "rand"; }
  bool const_item() const { return 0; }
unknown's avatar
unknown committed
612
  void update_used_tables();
613
  bool fix_fields(THD *thd, struct st_table_list *tables, Item **ref);
unknown's avatar
unknown committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
};


class Item_func_sign :public Item_int_func
{
public:
  Item_func_sign(Item *a) :Item_int_func(a) {}
  const char *func_name() const { return "sign"; }
  longlong val_int();
};


class Item_func_units :public Item_real_func
{
  char *name;
  double mul,add;
 public:
  Item_func_units(char *name_arg,Item *a,double mul_arg,double add_arg)
    :Item_real_func(a),name(name_arg),mul(mul_arg),add(add_arg) {}
633
  double val_real();
unknown's avatar
unknown committed
634
  const char *func_name() const { return name; }
unknown's avatar
unknown committed
635 636
  void fix_length_and_dec()
  { decimals= NOT_FIXED_DEC; max_length= float_length(decimals); }
unknown's avatar
unknown committed
637 638 639 640 641 642 643 644 645 646
};


class Item_func_min_max :public Item_func
{
  Item_result cmp_type;
  String tmp_value;
  int cmp_sign;
public:
  Item_func_min_max(List<Item> &list,int cmp_sign_arg) :Item_func(list),
647
    cmp_type(INT_RESULT), cmp_sign(cmp_sign_arg) {}
648
  double val_real();
unknown's avatar
unknown committed
649 650
  longlong val_int();
  String *val_str(String *);
unknown's avatar
unknown committed
651
  my_decimal *val_decimal(my_decimal *);
unknown's avatar
unknown committed
652 653
  void fix_length_and_dec();
  enum Item_result result_type () const { return cmp_type; }
unknown's avatar
unknown committed
654
  table_map not_null_tables() const { return 0; }
unknown's avatar
unknown committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
};

class Item_func_min :public Item_func_min_max
{
public:
  Item_func_min(List<Item> &list) :Item_func_min_max(list,1) {}
  const char *func_name() const { return "least"; }
};

class Item_func_max :public Item_func_min_max
{
public:
  Item_func_max(List<Item> &list) :Item_func_min_max(list,-1) {}
  const char *func_name() const { return "greatest"; }
};

unknown's avatar
unknown committed
671

unknown's avatar
unknown committed
672 673 674 675 676 677 678 679 680 681
class Item_func_length :public Item_int_func
{
  String value;
public:
  Item_func_length(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "length"; }
  void fix_length_and_dec() { max_length=10; }
};

unknown's avatar
unknown committed
682 683 684 685
class Item_func_bit_length :public Item_func_length
{
public:
  Item_func_bit_length(Item *a) :Item_func_length(a) {}
686 687
  longlong val_int()
    { DBUG_ASSERT(fixed == 1); return Item_func_length::val_int()*8; }
unknown's avatar
unknown committed
688 689 690
  const char *func_name() const { return "bit_length"; }
};

unknown's avatar
unknown committed
691 692 693 694 695 696 697 698 699 700
class Item_func_char_length :public Item_int_func
{
  String value;
public:
  Item_func_char_length(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "char_length"; }
  void fix_length_and_dec() { max_length=10; }
};

unknown's avatar
unknown committed
701 702 703 704 705 706 707 708 709
class Item_func_coercibility :public Item_int_func
{
public:
  Item_func_coercibility(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "coercibility"; }
  void fix_length_and_dec() { max_length=10; }
};

unknown's avatar
unknown committed
710 711 712
class Item_func_locate :public Item_int_func
{
  String value1,value2;
713
  DTCollation cmp_collation;
unknown's avatar
unknown committed
714 715 716 717 718
public:
  Item_func_locate(Item *a,Item *b) :Item_int_func(a,b) {}
  Item_func_locate(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
  const char *func_name() const { return "locate"; }
  longlong val_int();
719
  void fix_length_and_dec();
720
  void print(String *str);
unknown's avatar
unknown committed
721 722 723 724 725 726
};


class Item_func_field :public Item_int_func
{
  String value,tmp;
727 728
  Item_result cmp_type;
  DTCollation cmp_collation;
unknown's avatar
unknown committed
729
public:
730
  Item_func_field(List<Item> &list) :Item_int_func(list) {}
unknown's avatar
unknown committed
731 732
  longlong val_int();
  const char *func_name() const { return "field"; }
733
  void fix_length_and_dec();
unknown's avatar
unknown committed
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
};


class Item_func_ascii :public Item_int_func
{
  String value;
public:
  Item_func_ascii(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "ascii"; }
  void fix_length_and_dec() { max_length=3; }
};

class Item_func_ord :public Item_int_func
{
  String value;
public:
  Item_func_ord(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "ord"; }
};

class Item_func_find_in_set :public Item_int_func
{
  String value,value2;
  uint enum_value;
  ulonglong enum_bit;
761
  DTCollation cmp_collation;
unknown's avatar
unknown committed
762 763 764 765 766 767 768
public:
  Item_func_find_in_set(Item *a,Item *b) :Item_int_func(a,b),enum_value(0) {}
  longlong val_int();
  const char *func_name() const { return "find_in_set"; }
  void fix_length_and_dec();
};

769
/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
unknown's avatar
unknown committed
770

771
class Item_func_bit: public Item_int_func
unknown's avatar
unknown committed
772 773
{
public:
774 775 776
  Item_func_bit(Item *a, Item *b) :Item_int_func(a, b) {}
  Item_func_bit(Item *a) :Item_int_func(a) {}
  void fix_length_and_dec() { unsigned_flag= 1; }
unknown's avatar
unknown committed
777
  void print(String *str) { print_op(str); }
778 779 780 781 782 783
};

class Item_func_bit_or :public Item_func_bit
{
public:
  Item_func_bit_or(Item *a, Item *b) :Item_func_bit(a, b) {}
unknown's avatar
unknown committed
784 785 786 787
  longlong val_int();
  const char *func_name() const { return "|"; }
};

788
class Item_func_bit_and :public Item_func_bit
unknown's avatar
unknown committed
789 790
{
public:
791
  Item_func_bit_and(Item *a, Item *b) :Item_func_bit(a, b) {}
unknown's avatar
unknown committed
792 793 794 795 796 797 798 799 800 801
  longlong val_int();
  const char *func_name() const { return "&"; }
};

class Item_func_bit_count :public Item_int_func
{
public:
  Item_func_bit_count(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "bit_count"; }
unknown's avatar
unknown committed
802
  void fix_length_and_dec() { max_length=2; }
unknown's avatar
unknown committed
803 804
};

805
class Item_func_shift_left :public Item_func_bit
unknown's avatar
unknown committed
806 807
{
public:
808
  Item_func_shift_left(Item *a, Item *b) :Item_func_bit(a, b) {}
unknown's avatar
unknown committed
809 810 811 812
  longlong val_int();
  const char *func_name() const { return "<<"; }
};

813
class Item_func_shift_right :public Item_func_bit
unknown's avatar
unknown committed
814 815
{
public:
816
  Item_func_shift_right(Item *a, Item *b) :Item_func_bit(a, b) {}
unknown's avatar
unknown committed
817 818 819 820
  longlong val_int();
  const char *func_name() const { return ">>"; }
};

821
class Item_func_bit_neg :public Item_func_bit
unknown's avatar
unknown committed
822 823
{
public:
824
  Item_func_bit_neg(Item *a) :Item_func_bit(a) {}
unknown's avatar
unknown committed
825 826
  longlong val_int();
  const char *func_name() const { return "~"; }
unknown's avatar
unknown committed
827
  void print(String *str) { Item_func::print(str); }
unknown's avatar
unknown committed
828 829
};

unknown's avatar
unknown committed
830

831
class Item_func_last_insert_id :public Item_int_func
unknown's avatar
unknown committed
832 833
{
public:
834 835
  Item_func_last_insert_id() :Item_int_func() {}
  Item_func_last_insert_id(Item *a) :Item_int_func(a) {}
unknown's avatar
unknown committed
836 837
  longlong val_int();
  const char *func_name() const { return "last_insert_id"; }
unknown's avatar
unknown committed
838 839 840 841 842
  void fix_length_and_dec()
  {
    if (arg_count)
      max_length= args[0]->max_length;
  }
unknown's avatar
unknown committed
843 844 845 846 847 848 849 850 851 852 853
};

class Item_func_benchmark :public Item_int_func
{
  ulong loop_count;
 public:
  Item_func_benchmark(ulong loop_count_arg,Item *expr)
    :Item_int_func(expr), loop_count(loop_count_arg)
  {}
  longlong val_int();
  const char *func_name() const { return "benchmark"; }
unknown's avatar
unknown committed
854
  void fix_length_and_dec() { max_length=1; maybe_null=0; }
855
  void print(String *str);
unknown's avatar
unknown committed
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
};


#ifdef HAVE_DLOPEN

class Item_udf_func :public Item_func
{
 protected:
  udf_handler udf;

public:
  Item_udf_func(udf_func *udf_arg) :Item_func(), udf(udf_arg) {}
  Item_udf_func(udf_func *udf_arg, List<Item> &list)
    :Item_func(list), udf(udf_arg) {}
  const char *func_name() const { return udf.name(); }
unknown's avatar
unknown committed
871
  bool fix_fields(THD *thd, struct st_table_list *tables, Item **ref)
unknown's avatar
unknown committed
872
  {
873
    DBUG_ASSERT(fixed == 0);
unknown's avatar
unknown committed
874 875 876
    bool res= udf.fix_fields(thd, tables, this, arg_count, args);
    used_tables_cache= udf.used_tables_cache;
    const_item_cache= udf.const_item_cache;
877
    fixed= 1;
unknown's avatar
unknown committed
878 879 880
    return res;
  }
  Item_result result_type () const { return udf.result_type(); }
881
  table_map not_null_tables() const { return 0; }
unknown's avatar
unknown committed
882 883 884 885 886 887 888 889 890
};


class Item_func_udf_float :public Item_udf_func
{
 public:
  Item_func_udf_float(udf_func *udf_arg) :Item_udf_func(udf_arg) {}
  Item_func_udf_float(udf_func *udf_arg, List<Item> &list)
    :Item_udf_func(udf_arg,list) {}
891
  longlong val_int()
892 893 894 895
  {
    DBUG_ASSERT(fixed == 1);
    return (longlong) Item_func_udf_float::val_real();
  }
unknown's avatar
unknown committed
896 897 898 899 900 901 902 903
  my_decimal *val_decimal(my_decimal *dec_buf)
  {
    double res=val_real();
    if (null_value)
      return NULL;
    double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
    return dec_buf;
  }
904
  double val_real();
unknown's avatar
unknown committed
905 906 907 908 909 910 911 912 913 914 915 916
  String *val_str(String *str);
  void fix_length_and_dec() { fix_num_length_and_dec(); }
};


class Item_func_udf_int :public Item_udf_func
{
public:
  Item_func_udf_int(udf_func *udf_arg) :Item_udf_func(udf_arg) {}
  Item_func_udf_int(udf_func *udf_arg, List<Item> &list)
    :Item_udf_func(udf_arg,list) {}
  longlong val_int();
917
  double val_real() { return (double) Item_func_udf_int::val_int(); }
unknown's avatar
unknown committed
918 919
  String *val_str(String *str);
  enum Item_result result_type () const { return INT_RESULT; }
unknown's avatar
unknown committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
  void fix_length_and_dec() { decimals= 0; max_length= 21; }
};


class Item_func_udf_decimal :public Item_udf_func
{
public:
  Item_func_udf_decimal(udf_func *udf_arg) :Item_udf_func(udf_arg) {}
  Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list)
    :Item_udf_func(udf_arg,list) {}
  longlong val_int();
  double val_real();
  my_decimal *val_decimal(my_decimal *);
  String *val_str(String *str);
  enum Item_result result_type () const { return DECIMAL_RESULT; }
  void fix_length_and_dec();
unknown's avatar
unknown committed
936 937 938 939 940 941 942 943 944 945
};


class Item_func_udf_str :public Item_udf_func
{
public:
  Item_func_udf_str(udf_func *udf_arg) :Item_udf_func(udf_arg) {}
  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)
    :Item_udf_func(udf_arg,list) {}
  String *val_str(String *);
946
  double val_real()
unknown's avatar
unknown committed
947
  {
unknown's avatar
unknown committed
948 949 950 951 952 953
    int err_not_used;
    char *end_not_used;
    String *res;
    res= val_str(&str_value);
    return res ? my_strntod(res->charset(),(char*) res->ptr(), 
                            res->length(), &end_not_used, &err_not_used) : 0.0;
unknown's avatar
unknown committed
954 955 956
  }
  longlong val_int()
  {
unknown's avatar
unknown committed
957
    int err_not_used;
unknown's avatar
unknown committed
958
    String *res;  res=val_str(&str_value);
unknown's avatar
unknown committed
959 960
    return res ? my_strntoll(res->charset(),res->ptr(),res->length(),10,
                             (char**) 0, &err_not_used) : (longlong) 0;
unknown's avatar
unknown committed
961
  }
unknown's avatar
unknown committed
962 963 964 965 966 967 968 969
  my_decimal *val_decimal(my_decimal *dec_buf)
  {
    String *res=val_str(&str_value);
    if (!res)
      return NULL;
    string2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
    return dec_buf;
  }
unknown's avatar
unknown committed
970 971 972 973 974 975 976 977 978 979 980
  enum Item_result result_type () const { return STRING_RESULT; }
  void fix_length_and_dec();
};

#else /* Dummy functions to get sql_yacc.cc compiled */

class Item_func_udf_float :public Item_real_func
{
 public:
  Item_func_udf_float(udf_func *udf_arg) :Item_real_func() {}
  Item_func_udf_float(udf_func *udf_arg, List<Item> &list) :Item_real_func(list) {}
981
  double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
unknown's avatar
unknown committed
982 983 984 985 986 987 988 989
};


class Item_func_udf_int :public Item_int_func
{
public:
  Item_func_udf_int(udf_func *udf_arg) :Item_int_func() {}
  Item_func_udf_int(udf_func *udf_arg, List<Item> &list) :Item_int_func(list) {}
990
  longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; }
unknown's avatar
unknown committed
991 992 993
};


unknown's avatar
unknown committed
994 995 996 997 998 999 1000 1001 1002
class Item_func_udf_decimal :public Item_int_func
{
public:
  Item_func_udf_decimal(udf_func *udf_arg) :Item_int_func() {}
  Item_func_udf_decimal(udf_func *udf_arg, List<Item> &list) :Item_int_func(list) {}
  my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed == 1); return 0; }
};


unknown's avatar
unknown committed
1003 1004 1005 1006 1007
class Item_func_udf_str :public Item_func
{
public:
  Item_func_udf_str(udf_func *udf_arg) :Item_func() {}
  Item_func_udf_str(udf_func *udf_arg, List<Item> &list)  :Item_func(list) {}
1008 1009
  String *val_str(String *)
    { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
1010
  double val_real() { DBUG_ASSERT(fixed == 1); null_value= 1; return 0.0; }
1011
  longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
unknown's avatar
unknown committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
  enum Item_result result_type () const { return STRING_RESULT; }
  void fix_length_and_dec() { maybe_null=1; max_length=0; }
};

#endif /* HAVE_DLOPEN */

/*
** User level locks
*/

1022
class User_level_lock;
unknown's avatar
unknown committed
1023
void item_user_lock_init(void);
1024
void item_user_lock_release(User_level_lock *ull);
unknown's avatar
unknown committed
1025 1026 1027 1028 1029 1030 1031 1032 1033
void item_user_lock_free(void);

class Item_func_get_lock :public Item_int_func
{
  String value;
 public:
  Item_func_get_lock(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return "get_lock"; }
unknown's avatar
unknown committed
1034
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
unknown's avatar
unknown committed
1035 1036 1037 1038 1039 1040 1041 1042 1043
};

class Item_func_release_lock :public Item_int_func
{
  String value;
 public:
  Item_func_release_lock(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "release_lock"; }
unknown's avatar
unknown committed
1044
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
unknown's avatar
unknown committed
1045 1046
};

unknown's avatar
unknown committed
1047 1048 1049 1050 1051 1052 1053
/* replication functions */

class Item_master_pos_wait :public Item_int_func
{
  String value;
 public:
  Item_master_pos_wait(Item *a,Item *b) :Item_int_func(a,b) {}
1054
  Item_master_pos_wait(Item *a,Item *b,Item *c) :Item_int_func(a,b,c) {}
unknown's avatar
unknown committed
1055 1056
  longlong val_int();
  const char *func_name() const { return "master_pos_wait"; }
1057
  void fix_length_and_dec() { max_length=21; maybe_null=1;}
unknown's avatar
unknown committed
1058 1059
};

unknown's avatar
unknown committed
1060

unknown's avatar
unknown committed
1061
/* Handling of user definable variables */
unknown's avatar
unknown committed
1062 1063 1064 1065 1066 1067 1068 1069

class user_var_entry;

class Item_func_set_user_var :public Item_func
{
  enum Item_result cached_result_type;
  LEX_STRING name;
  user_var_entry *entry;
1070 1071
  char buffer[MAX_FIELD_WIDTH];
  String value;
unknown's avatar
unknown committed
1072
  my_decimal decimal_buff;
1073 1074 1075 1076 1077
  union
  {
    longlong vint;
    double vreal;
    String *vstr;
unknown's avatar
unknown committed
1078
    my_decimal *vdec;
1079 1080 1081
  } save_result;
  String save_buff;
  
unknown's avatar
unknown committed
1082 1083

public:
1084 1085 1086
  Item_func_set_user_var(LEX_STRING a,Item *b)
    :Item_func(b), cached_result_type(INT_RESULT), name(a)
  {}
1087
  double val_real();
unknown's avatar
unknown committed
1088 1089
  longlong val_int();
  String *val_str(String *str);
unknown's avatar
unknown committed
1090
  my_decimal *val_decimal(my_decimal *);
unknown's avatar
unknown committed
1091
  bool update_hash(void *ptr, uint length, enum Item_result type, 
1092
  		   CHARSET_INFO *cs, Derivation dv);
1093
  bool check();
unknown's avatar
unknown committed
1094 1095
  bool update();
  enum Item_result result_type () const { return cached_result_type; }
unknown's avatar
unknown committed
1096
  bool fix_fields(THD *thd, struct st_table_list *tables, Item **ref);
unknown's avatar
unknown committed
1097
  void fix_length_and_dec();
unknown's avatar
unknown committed
1098
  void print(String *str);
1099
  void print_as_stmt(String *str);
unknown's avatar
unknown committed
1100 1101 1102 1103 1104 1105 1106
  const char *func_name() const { return "set_user_var"; }
};


class Item_func_get_user_var :public Item_func
{
  LEX_STRING name;
1107
  user_var_entry *var_entry;
unknown's avatar
unknown committed
1108 1109

public:
1110
  Item_func_get_user_var(LEX_STRING a):
1111
    Item_func(), name(a) {}
1112 1113
  enum Functype functype() const { return GUSERVAR_FUNC; }
  LEX_STRING get_name() { return name; }
1114
  double val_real();
unknown's avatar
unknown committed
1115
  longlong val_int();
unknown's avatar
unknown committed
1116
  my_decimal *val_decimal(my_decimal*);
unknown's avatar
unknown committed
1117 1118
  String *val_str(String* str);
  void fix_length_and_dec();
unknown's avatar
unknown committed
1119
  void print(String *str);
unknown's avatar
unknown committed
1120
  enum Item_result result_type() const;
1121 1122 1123 1124
  /*
    We must always return variables as strings to guard against selects of type
    select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
  */
1125
  enum_field_types field_type() const  { return MYSQL_TYPE_VARCHAR; }
unknown's avatar
unknown committed
1126
  const char *func_name() const { return "get_user_var"; }
1127
  bool const_item() const;
1128
  table_map used_tables() const
1129
  { return const_item() ? 0 : RAND_TABLE_BIT; }
1130
  bool eq(const Item *item, bool binary_cmp) const;
1131
};
unknown's avatar
unknown committed
1132

unknown's avatar
unknown committed
1133

unknown's avatar
unknown committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
class Item_func_inet_aton : public Item_int_func
{
public:
   Item_func_inet_aton(Item *a) :Item_int_func(a) {}
   longlong val_int();
   const char *func_name() const { return "inet_aton"; }
   void fix_length_and_dec() { decimals = 0; max_length = 21; maybe_null=1;}
};


1144
/* for fulltext search */
1145
#include <ft_global.h>
unknown's avatar
unknown committed
1146 1147 1148 1149

class Item_func_match :public Item_real_func
{
public:
1150
  uint key, flags;
unknown's avatar
unknown committed
1151
  bool join_key;
1152 1153 1154 1155 1156 1157 1158
  DTCollation cmp_collation;
  FT_INFO *ft_handler;
  TABLE *table;
  Item_func_match *master;   // for master-slave optimization
  Item *concat;              // Item_func_concat_ws
  String value;              // value of concat
  String search_value;       // key_item()'s value converted to cmp_collation
unknown's avatar
unknown committed
1159

unknown's avatar
unknown committed
1160 1161
  Item_func_match(List<Item> &a, uint b): Item_real_func(a), key(0), flags(b),
       join_key(0), ft_handler(0), table(0), master(0), concat(0) { }
unknown's avatar
unknown committed
1162
  void cleanup()
1163
  {
unknown's avatar
unknown committed
1164
    DBUG_ENTER("Item_func_match");
1165
    Item_real_func::cleanup();
1166
    if (!master && ft_handler)
1167
    {
unknown's avatar
unknown committed
1168 1169
      ft_handler->please->close_search(ft_handler);
      ft_handler=0;
unknown's avatar
unknown committed
1170
      if (join_key)
1171
	table->file->ft_handler=0;
unknown's avatar
unknown committed
1172
      table->fulltext_searched=0;
1173
    }
unknown's avatar
unknown committed
1174
    concat= 0;
unknown's avatar
unknown committed
1175
    DBUG_VOID_RETURN;
1176
  }
unknown's avatar
unknown committed
1177
  enum Functype functype() const { return FT_FUNC; }
1178
  const char *func_name() const { return "match"; }
unknown's avatar
unknown committed
1179
  void update_used_tables() {}
1180
  table_map not_null_tables() const { return 0; }
unknown's avatar
unknown committed
1181
  bool fix_fields(THD *thd, struct st_table_list *tlist, Item **ref);
1182
  bool eq(const Item *, bool binary_cmp) const;
1183
  /* The following should be safe, even if we compare doubles */
1184 1185
  longlong val_int() { DBUG_ASSERT(fixed == 1); return val_real() != 0.0; }
  double val_real();
1186
  void print(String *str);
1187 1188

  bool fix_index();
1189
  void init_search(bool no_order);
1190
};
unknown's avatar
unknown committed
1191

unknown's avatar
unknown committed
1192

1193
class Item_func_bit_xor : public Item_func_bit
unknown's avatar
unknown committed
1194 1195
{
public:
1196
  Item_func_bit_xor(Item *a, Item *b) :Item_func_bit(a, b) {}
unknown's avatar
unknown committed
1197 1198 1199 1200
  longlong val_int();
  const char *func_name() const { return "^"; }
};

1201
class Item_func_is_free_lock :public Item_int_func
unknown's avatar
unknown committed
1202 1203 1204
{
  String value;
public:
1205
  Item_func_is_free_lock(Item *a) :Item_int_func(a) {}
unknown's avatar
unknown committed
1206
  longlong val_int();
1207
  const char *func_name() const { return "is_free_lock"; }
unknown's avatar
unknown committed
1208
  void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;}
unknown's avatar
unknown committed
1209 1210
};

unknown's avatar
SCRUM  
unknown committed
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
class Item_func_is_used_lock :public Item_int_func
{
  String value;
public:
  Item_func_is_used_lock(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "is_used_lock"; }
  void fix_length_and_dec() { decimals=0; max_length=10; maybe_null=1;}
};

unknown's avatar
unknown committed
1221 1222
/* For type casts */

1223
enum Cast_target
unknown's avatar
unknown committed
1224 1225
{
  ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
unknown's avatar
unknown committed
1226 1227
  ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR,
  ITEM_CAST_DECIMAL
unknown's avatar
unknown committed
1228
};
1229 1230


1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
class Item_func_row_count :public Item_int_func
{
public:
  Item_func_row_count() :Item_int_func() {}
  longlong val_int();
  const char *func_name() const { return "row_count"; }
  void fix_length_and_dec() { decimals= 0; maybe_null=0; }
};


1241 1242 1243 1244 1245 1246 1247
/*
 *
 * Stored FUNCTIONs
 *
 */

class sp_head;
1248
class sp_name;
1249 1250 1251 1252

class Item_func_sp :public Item_func
{
private:
1253
  sp_name *m_name;
1254
  mutable sp_head *m_sp;
unknown's avatar
unknown committed
1255 1256 1257 1258
  mutable struct {
    TABLE table;
    TABLE_SHARE share;
  } dummy_table;
1259 1260

  int execute(Item **itp);
unknown's avatar
unknown committed
1261
  Field *sp_result_field(void) const;
1262 1263 1264

public:

1265
  Item_func_sp(sp_name *name);
1266

1267
  Item_func_sp(sp_name *name, List<Item> &list);
1268 1269 1270 1271

  virtual ~Item_func_sp()
  {}

1272
  const char *func_name() const;
1273

1274 1275
  enum enum_field_types field_type() const;

unknown's avatar
unknown committed
1276 1277 1278 1279
  Field *tmp_table_field(TABLE *t_arg);

  void make_field(Send_field *tmp_field);

1280 1281 1282 1283
  Item_result result_type() const;

  longlong val_int()
  {
1284
    return (longlong)Item_func_sp::val_real();
1285 1286
  }

1287
  double val_real()
1288 1289
  {
    Item *it;
1290
    double d;
1291 1292

    if (execute(&it))
1293 1294
    {
      null_value= 1;
1295
      return 0.0;
1296
    }
1297
    d= it->val_real();
1298 1299
    null_value= it->null_value;
    return d;
1300 1301
  }

unknown's avatar
unknown committed
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
  my_decimal *val_decimal(my_decimal *dec_buf)
  {
    Item *it;
    my_decimal *result;

    if (execute(&it))
    {
      null_value= 1;
      return NULL;
    }
    result= it->val_decimal(dec_buf);
    null_value= it->null_value;
    return result;
  }


1318 1319 1320
  String *val_str(String *str)
  {
    Item *it;
1321
    String *s;
1322 1323

    if (execute(&it))
1324 1325
    {
      null_value= 1;
1326
      return NULL;
1327
    }
1328 1329 1330
    s= it->val_str(str);
    null_value= it->null_value;
    return s;
1331 1332 1333 1334 1335
  }

  void fix_length_and_dec();

};
unknown's avatar
unknown committed
1336 1337


1338 1339 1340 1341 1342 1343 1344 1345
class Item_func_found_rows :public Item_int_func
{
public:
  Item_func_found_rows() :Item_int_func() {}
  longlong val_int();
  const char *func_name() const { return "found_rows"; }
  void fix_length_and_dec() { decimals= 0; maybe_null=0; }
};