item_func.h 28.8 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 34 35 36 37 38 39 40 41
   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:
  Item **args,*tmp_arg[2];
public:
  uint arg_count;
  table_map used_tables_cache;
  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,
42
		  COND_AND_FUNC, COND_OR_FUNC, COND_XOR_FUNC, BETWEEN, IN_FUNC,
unknown's avatar
unknown committed
43
		  INTERVAL_FUNC,
unknown's avatar
unknown committed
44 45 46 47 48
		  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,
		  SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN};
unknown's avatar
unknown committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  enum optimize_type { OPTIMIZE_NONE,OPTIMIZE_KEY,OPTIMIZE_OP, OPTIMIZE_NULL };
  enum Type type() const { return FUNC_ITEM; }
  virtual enum Functype functype() const   { return UNKNOWN_FUNC; }
  Item_func(void)
  {
    arg_count=0; with_sum_func=0;
  }
  Item_func(Item *a)
  {
    arg_count=1;
    args=tmp_arg;
    args[0]=a;
    with_sum_func=a->with_sum_func;
  }
  Item_func(Item *a,Item *b)
  {
    arg_count=2;
    args=tmp_arg;
    args[0]=a; args[1]=b;
    with_sum_func=a->with_sum_func || b->with_sum_func;
  }
  Item_func(Item *a,Item *b,Item *c)
  {
    arg_count=0;
    if ((args=(Item**) sql_alloc(sizeof(Item*)*3)))
    {
      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;
    }
  }
  Item_func(Item *a,Item *b,Item *c,Item *d)
  {
    arg_count=0;
    if ((args=(Item**) sql_alloc(sizeof(Item*)*4)))
    {
      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;
    }
  }
  Item_func(Item *a,Item *b,Item *c,Item *d,Item* e)
  {
    arg_count=5;
    if ((args=(Item**) sql_alloc(sizeof(Item*)*5)))
    {
      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 ;
    }
  }
  Item_func(List<Item> &list);
  ~Item_func() {} /* Nothing to do; Items are freed automaticly */
unknown's avatar
unknown committed
103
  bool fix_fields(THD *,struct st_table_list *, Item **ref);
unknown's avatar
unknown committed
104 105 106
  void make_field(Send_field *field);
  table_map used_tables() const;
  void update_used_tables();
107
  bool eq(const Item *item, bool binary_cmp) const;
unknown's avatar
unknown committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  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; }
  inline uint argument_count() const { return arg_count; }
  inline void remove_arguments() { arg_count=0; }
  virtual void split_sum_func(List<Item> &fields);
  void print(String *str);
  void print_op(String *str);
  void fix_num_length_and_dec();
  inline bool get_arg0_date(TIME *ltime,bool fuzzy_date)
  {
    return (null_value=args[0]->get_date(ltime,fuzzy_date));
  }
  inline bool get_arg0_time(TIME *ltime)
  {
    return (null_value=args[0]->get_time(ltime));
  }
unknown's avatar
unknown committed
128
  bool is_null() { (void) val_int(); return null_value; }
unknown's avatar
unknown committed
129
  friend class udf_handler;
130
  Field *tmp_table_field(TABLE *t_arg);
unknown's avatar
unknown committed
131
  bool check_loop(uint id);
unknown's avatar
unknown committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
};


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);
  longlong val_int() { return (longlong) val(); }
  enum Item_result result_type () const { return REAL_RESULT; }
  void fix_length_and_dec() { decimals=NOT_FIXED_DEC; max_length=float_length(decimals); }
};

148

unknown's avatar
unknown committed
149 150 151 152 153 154 155 156 157 158 159
class Item_num_func :public Item_func
{
 protected:
  Item_result hybrid_type;
public:
  Item_num_func(Item *a) :Item_func(a),hybrid_type(REAL_RESULT) {}
  Item_num_func(Item *a,Item *b) :Item_func(a,b),hybrid_type(REAL_RESULT) {}
  String *val_str(String*str);
  longlong val_int() { return (longlong) val(); }
  enum Item_result result_type () const { return hybrid_type; }
  void fix_length_and_dec() { fix_num_length_and_dec(); }
unknown's avatar
unknown committed
160
  bool is_null() { (void) val(); return null_value; }
unknown's avatar
unknown committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174
};


class Item_num_op :public Item_func
{
 protected:
  Item_result hybrid_type;
 public:
  Item_num_op(Item *a,Item *b) :Item_func(a,b),hybrid_type(REAL_RESULT) {}
  String *val_str(String*str);
  void print(String *str) { print_op(str); }
  enum Item_result result_type () const { return hybrid_type; }
  void fix_length_and_dec() { fix_num_length_and_dec(); find_num_type(); }
  void find_num_type(void);
unknown's avatar
unknown committed
175
  bool is_null() { (void) val(); return null_value; }
unknown's avatar
unknown committed
176 177 178 179 180 181
};


class Item_int_func :public Item_func
{
public:
unknown's avatar
unknown committed
182 183 184 185 186
  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; }
unknown's avatar
unknown committed
187 188 189
  double val() { return (double) val_int(); }
  String *val_str(String*str);
  enum Item_result result_type () const { return INT_RESULT; }
unknown's avatar
unknown committed
190
  void fix_length_and_dec() {}
unknown's avatar
unknown committed
191 192
};

193

194 195 196 197 198 199 200
class Item_func_signed :public Item_int_func
{
public:
  Item_func_signed(Item *a) :Item_int_func(a) {}
  double val() { return args[0]->val(); }
  longlong val_int() { return args[0]->val_int(); }
  void fix_length_and_dec()
unknown's avatar
unknown committed
201
  { max_length=args[0]->max_length; unsigned_flag=0; }
202 203
};

204

205 206 207 208 209 210 211
class Item_func_unsigned :public Item_int_func
{
public:
  Item_func_unsigned(Item *a) :Item_int_func(a) {}
  double val() { return args[0]->val(); }
  longlong val_int() { return args[0]->val_int(); }
  void fix_length_and_dec()
unknown's avatar
unknown committed
212
  { max_length=args[0]->max_length; unsigned_flag=1; }
213 214 215
};


unknown's avatar
unknown committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
class Item_func_plus :public Item_num_op
{
public:
  Item_func_plus(Item *a,Item *b) :Item_num_op(a,b) {}
  const char *func_name() const { return "+"; }
  double val();
  longlong val_int();
};

class Item_func_minus :public Item_num_op
{
public:
  Item_func_minus(Item *a,Item *b) :Item_num_op(a,b) {}
  const char *func_name() const { return "-"; }
  double val();
  longlong val_int();
232
  void fix_length_and_dec();
unknown's avatar
unknown committed
233 234
};

235

unknown's avatar
unknown committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
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 "*"; }
  double val();
  longlong val_int();
};


class Item_func_div :public Item_num_op
{
public:
  Item_func_div(Item *a,Item *b) :Item_num_op(a,b) {}
  double val();
  longlong val_int();
  const char *func_name() const { return "/"; }
  void fix_length_and_dec();
};


257 258 259 260 261 262 263 264 265 266 267 268
class Item_func_int_div :public Item_num_op
{
public:
  Item_func_int_div(Item *a,Item *b) :Item_num_op(a,b)
  { hybrid_type=INT_RESULT; }
  double val() { return (double) val_int(); }
  longlong val_int();
  const char *func_name() const { return "DIV"; }
  void fix_length_and_dec();
};


unknown's avatar
unknown committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
class Item_func_mod :public Item_num_op
{
public:
  Item_func_mod(Item *a,Item *b) :Item_num_op(a,b) {}
  double val();
  longlong val_int();
  const char *func_name() const { return "%"; }
  void fix_length_and_dec();
};


class Item_func_neg :public Item_num_func
{
public:
  Item_func_neg(Item *a) :Item_num_func(a) {}
  double val();
  longlong val_int();
  const char *func_name() const { return "-"; }
  void fix_length_and_dec();
};


class Item_func_abs :public Item_num_func
{
public:
  Item_func_abs(Item *a) :Item_num_func(a) {}
  const char *func_name() const { return "abs"; }
  double val();
  longlong val_int();
  enum Item_result result_type () const
  { return args[0]->result_type() == INT_RESULT ? INT_RESULT : REAL_RESULT; }
  void fix_length_and_dec();
};

// A class to handle logaritmic and trigometric functions

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()
  {
    decimals=6; max_length=float_length(decimals);
    maybe_null=1;
  }
  inline double fix_result(double value)
  {
#ifndef HAVE_FINITE
    return value;
#else
    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) {}
  double val();
  const char *func_name() const { return "exp"; }
};

336 337 338 339 340 341 342 343 344 345

class Item_func_ln :public Item_dec_func
{
public:
  Item_func_ln(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "ln"; }
};


unknown's avatar
unknown committed
346 347 348 349
class Item_func_log :public Item_dec_func
{
public:
  Item_func_log(Item *a) :Item_dec_func(a) {}
350
  Item_func_log(Item *a,Item *b) :Item_dec_func(a,b) {}
unknown's avatar
unknown committed
351 352 353 354 355
  double val();
  const char *func_name() const { return "log"; }
};


356 357 358 359 360 361 362 363 364
class Item_func_log2 :public Item_dec_func
{
public:
  Item_func_log2(Item *a) :Item_dec_func(a) {}
  double val();
  const char *func_name() const { return "log2"; }
};


unknown's avatar
unknown committed
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
class Item_func_log10 :public Item_dec_func
{
public:
  Item_func_log10(Item *a) :Item_dec_func(a) {}
  double val();
  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) {}
  double val();
  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) {}
  double val();
  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) {}
  double val();
  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) {}
  double val();
  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) {}
  double val();
  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) {}
  double val();
  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) {}
  double val();
  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) {}
  double val();
  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();
};


class Item_func_ceiling :public Item_func_integer
{
  Item_func_ceiling();				/* Never called */
public:
  Item_func_ceiling(Item *a) :Item_func_integer(a) {}
  const char *func_name() const { return "ceiling"; }
  longlong val_int();
};

class Item_func_floor :public Item_func_integer
{
public:
  Item_func_floor(Item *a) :Item_func_integer(a) {}
  const char *func_name() const { return "floor"; }
  longlong val_int();
};

/* This handles round and truncate */

class Item_func_round :public Item_real_func
{
  bool truncate;
public:
  Item_func_round(Item *a,Item *b,bool trunc_arg)
    :Item_real_func(a,b),truncate(trunc_arg) {}
  const char *func_name() const { return truncate ? "truncate" : "round"; }
  double val();
  void fix_length_and_dec();
};


class Item_func_rand :public Item_real_func
{
public:
  Item_func_rand(Item *a) :Item_real_func(a) {}
  Item_func_rand()	  :Item_real_func()  {}
  double val();
  const char *func_name() const { return "rand"; }
  void fix_length_and_dec() { decimals=NOT_FIXED_DEC; max_length=float_length(decimals); }
  bool const_item() const { return 0; }
  table_map used_tables() const { return RAND_TABLE_BIT; }
};


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) {}
  double val();
  const char *func_name() const { return name; }
  void fix_length_and_dec() { decimals=NOT_FIXED_DEC; max_length=float_length(decimals); }
};


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),
    cmp_sign(cmp_sign_arg) {}
  double val();
  longlong val_int();
  String *val_str(String *);
  void fix_length_and_dec();
  enum Item_result result_type () const { return cmp_type; }
};

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
544 545 546 547 548 549 550 551 552 553
class Item_func_crc32 :public Item_int_func
{
  String value;
public:
  Item_func_crc32(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "crc32"; }
  void fix_length_and_dec() { max_length=10; }
};

unknown's avatar
unknown committed
554 555 556 557 558 559 560 561 562 563 564

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
565 566 567 568 569 570 571 572
class Item_func_bit_length :public Item_func_length
{
public:
  Item_func_bit_length(Item *a) :Item_func_length(a) {}
  longlong val_int() { return Item_func_length::val_int()*8; }
  const char *func_name() const { return "bit_length"; }
};

unknown's avatar
unknown committed
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
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; }
};

class Item_func_locate :public Item_int_func
{
  String value1,value2;
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();
  void fix_length_and_dec() { maybe_null=0; max_length=11; }
};


class Item_func_field :public Item_int_func
{
  Item *item;
  String value,tmp;
public:
  Item_func_field(Item *a,List<Item> &list) :Item_int_func(list),item(a) {}
  ~Item_func_field() { delete item; }
  longlong val_int();
unknown's avatar
unknown committed
603
  bool fix_fields(THD *thd,struct st_table_list *tlist, Item **ref)
unknown's avatar
unknown committed
604
  {
unknown's avatar
unknown committed
605 606
    return (item->fix_fields(thd, tlist, &item) ||
	    Item_func::fix_fields(thd, tlist, ref));
unknown's avatar
unknown committed
607 608 609 610
  }
  void update_used_tables()
  {
    item->update_used_tables() ; Item_func::update_used_tables();
611 612
    used_tables_cache|= item->used_tables();
    const_item_cache&=  item->const_item();
unknown's avatar
unknown committed
613 614 615
  }
  const char *func_name() const { return "field"; }
  void fix_length_and_dec()
616 617 618 619 620 621
  {
    maybe_null=0; max_length=3;
    used_tables_cache|= item->used_tables();
    const_item_cache&=  item->const_item();
    with_sum_func= with_sum_func || item->with_sum_func;
  }
unknown's avatar
unknown committed
622 623 624 625 626 627 628
  bool check_loop(uint id)
  {
    DBUG_ENTER("Item_func_field::check_loop");
    if (Item_int_func::check_loop(id))
      DBUG_RETURN(1);
    DBUG_RETURN(item->check_loop(id));
  }
unknown's avatar
unknown committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
};


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;
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();
};


class Item_func_bit_or :public Item_int_func
{
public:
  Item_func_bit_or(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return "|"; }
unknown's avatar
unknown committed
670
  void fix_length_and_dec() { unsigned_flag=1; }
unknown's avatar
unknown committed
671 672 673 674 675 676 677 678
};

class Item_func_bit_and :public Item_int_func
{
public:
  Item_func_bit_and(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return "&"; }
unknown's avatar
unknown committed
679
  void fix_length_and_dec() { unsigned_flag=1; }
unknown's avatar
unknown committed
680 681 682 683 684 685 686 687
};

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
688
  void fix_length_and_dec() { max_length=2; }
unknown's avatar
unknown committed
689 690 691 692 693 694 695 696
};

class Item_func_shift_left :public Item_int_func
{
public:
  Item_func_shift_left(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return "<<"; }
unknown's avatar
unknown committed
697
  void fix_length_and_dec() { unsigned_flag=1; }
unknown's avatar
unknown committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
};

class Item_func_shift_right :public Item_int_func
{
public:
  Item_func_shift_right(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return ">>"; }
};

class Item_func_bit_neg :public Item_int_func
{
public:
  Item_func_bit_neg(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "~"; }
unknown's avatar
unknown committed
714
  void fix_length_and_dec() { unsigned_flag=1; }
unknown's avatar
unknown committed
715 716 717 718 719 720 721 722
};

class Item_func_set_last_insert_id :public Item_int_func
{
public:
  Item_func_set_last_insert_id(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "last_insert_id"; }
unknown's avatar
unknown committed
723
  void fix_length_and_dec() { max_length=args[0]->max_length; }
unknown's avatar
unknown committed
724 725 726 727 728 729 730 731 732 733 734
};

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
735
  void fix_length_and_dec() { max_length=1; maybe_null=0; }
unknown's avatar
unknown committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
};


#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) {}
  ~Item_udf_func() {}
  const char *func_name() const { return udf.name(); }
unknown's avatar
unknown committed
752
  bool fix_fields(THD *thd, struct st_table_list *tables, Item **ref)
unknown's avatar
unknown committed
753
  {
unknown's avatar
unknown committed
754 755 756
    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;
757
    fixed= 1;
unknown's avatar
unknown committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
    return res;
  }
  Item_result result_type () const { return udf.result_type(); }
};


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) {}
  ~Item_func_udf_float() {}
  longlong val_int() { return (longlong) Item_func_udf_float::val(); }
  double val();
  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) {}
  ~Item_func_udf_int() {}
  longlong val_int();
  double val() { return (double) Item_func_udf_int::val_int(); }
  String *val_str(String *str);
  enum Item_result result_type () const { return INT_RESULT; }
  void fix_length_and_dec() { decimals=0; max_length=21; }
};


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) {}
  ~Item_func_udf_str() {}
  String *val_str(String *);
  double val()
  {
    String *res;  res=val_str(&str_value);
unknown's avatar
unknown committed
804
    return res ? my_strntod(res->charset(),res->ptr(),res->length(),0) : 0.0;
unknown's avatar
unknown committed
805 806 807 808
  }
  longlong val_int()
  {
    String *res;  res=val_str(&str_value);
unknown's avatar
unknown committed
809
    return res ? my_strntoll(res->charset(),res->ptr(),res->length(),(char**) 0,10) : (longlong) 0;
unknown's avatar
unknown committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
  }
  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) {}
  ~Item_func_udf_float() {}
  double val() { return 0.0; }
};


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) {}
  ~Item_func_udf_int() {}
  longlong val_int() { return 0; }
};


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) {}
  ~Item_func_udf_str() {}
  String *val_str(String *) { null_value=1; return 0; }
  double val() { null_value=1; return 0.0; }
  longlong val_int() { null_value=1; return 0; }
  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
*/

class ULL;
void item_user_lock_init(void);
void item_user_lock_release(ULL *ull);
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
868
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
unknown's avatar
unknown committed
869 870 871 872 873 874 875 876 877
};

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
878
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
unknown's avatar
unknown committed
879 880
};

unknown's avatar
unknown committed
881 882 883 884 885 886 887 888 889
/* 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) {}
  longlong val_int();
  const char *func_name() const { return "master_pos_wait"; }
unknown's avatar
unknown committed
890
  void fix_length_and_dec() { max_length=1; maybe_null=1;}
unknown's avatar
unknown committed
891 892
};

unknown's avatar
unknown committed
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908

/* Handling of user definiable variables */

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;

public:
  Item_func_set_user_var(LEX_STRING a,Item *b): Item_func(b), name(a) {}
  double val();
  longlong val_int();
  String *val_str(String *str);
909
  void update_hash(void *ptr, uint length, enum Item_result type, CHARSET_INFO *cs);
unknown's avatar
unknown committed
910 911
  bool update();
  enum Item_result result_type () const { return cached_result_type; }
unknown's avatar
unknown committed
912
  bool fix_fields(THD *thd, struct st_table_list *tables, Item **ref);
unknown's avatar
unknown committed
913
  void fix_length_and_dec();
unknown's avatar
unknown committed
914
  void print(String *str);
unknown's avatar
unknown committed
915 916 917 918 919 920 921
  const char *func_name() const { return "set_user_var"; }
};


class Item_func_get_user_var :public Item_func
{
  LEX_STRING name;
922
  user_var_entry *var_entry;
923
  bool const_var_flag;
unknown's avatar
unknown committed
924 925

public:
926 927
  Item_func_get_user_var(LEX_STRING a):
    Item_func(), name(a), const_var_flag(1) {}
unknown's avatar
unknown committed
928 929 930 931 932
  user_var_entry *get_entry();
  double val();
  longlong val_int();
  String *val_str(String* str);
  void fix_length_and_dec();
unknown's avatar
unknown committed
933
  void print(String *str);
unknown's avatar
unknown committed
934 935
  enum Item_result result_type() const;
  const char *func_name() const { return "get_user_var"; }
936 937 938
  bool const_item() const { return const_var_flag; }
  table_map used_tables() const
  { return const_var_flag ? 0 : RAND_TABLE_BIT; }
939
  bool eq(const Item *item, bool binary_cmp) const;
940
};
unknown's avatar
unknown committed
941

unknown's avatar
unknown committed
942

unknown's avatar
unknown committed
943 944 945 946 947 948 949 950 951 952
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;}
};


953
/* for fulltext search */
954
#include <ft_global.h>
unknown's avatar
unknown committed
955 956 957 958 959

class Item_func_match :public Item_real_func
{
public:
  List<Item> fields;
960
  String value;
unknown's avatar
unknown committed
961
  TABLE *table;
962
  Item_func_match *master;
unknown's avatar
unknown committed
963
  FT_INFO * ft_handler;
unknown's avatar
unknown committed
964
  Item *concat;
965
  byte *record;
unknown's avatar
unknown committed
966 967
  uint key, mode;
  bool join_key;
unknown's avatar
unknown committed
968 969

  Item_func_match(List<Item> &a, Item *b): Item_real_func(b),
unknown's avatar
unknown committed
970
       fields(a), table(0), master(0), ft_handler(0),
971 972
       concat(0), key(0), join_key(0)
  {}
973 974
  ~Item_func_match()
  {
975
    if (!master && ft_handler)
976
    {
unknown's avatar
unknown committed
977 978
      ft_handler->please->close_search(ft_handler);
      ft_handler=0;
979 980
      if(join_key)
	table->file->ft_handler=0;
unknown's avatar
unknown committed
981
      table->fulltext_searched=0;
982
    }
983 984
    if (concat)
      delete concat;
985
  }
unknown's avatar
unknown committed
986 987
  enum Functype functype() const { return FT_FUNC; }
  void update_used_tables() {}
unknown's avatar
unknown committed
988
  bool fix_fields(THD *thd, struct st_table_list *tlist, Item **ref);
989
  bool eq(const Item *, bool binary_cmp) const;
unknown's avatar
unknown committed
990
  longlong val_int() { return val()!=0.0; }
991
  double val();
992 993

  bool fix_index();
994
  void init_search(bool no_order);
unknown's avatar
unknown committed
995
  bool check_loop(uint id);
unknown's avatar
unknown committed
996
};
997

unknown's avatar
unknown committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

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


class Item_func_x :public Item_real_func
{
  String value;
public:
  Item_func_x(Item *a) :Item_real_func(a) {}
  double val();
  const char *func_name() const { return "x"; }
};


class Item_func_y :public Item_real_func
{
  String value;
public:
  Item_func_y(Item *a) :Item_real_func(a) {}
  double val();
  const char *func_name() const { return "y"; }
};


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

unknown's avatar
unknown committed
1040

unknown's avatar
unknown committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
class Item_func_numinteriorring :public Item_int_func
{
  String value;
public:
  Item_func_numinteriorring(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "numinteriorring"; }
  void fix_length_and_dec() { max_length=10; }
};

unknown's avatar
unknown committed
1051

unknown's avatar
unknown committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
class Item_func_numpoints :public Item_int_func
{
  String value;
public:
  Item_func_numpoints(Item *a) :Item_int_func(a) {}
  longlong val_int();
  const char *func_name() const { return "numpoints"; }
  void fix_length_and_dec() { max_length=10; }
};

unknown's avatar
unknown committed
1062

unknown's avatar
unknown committed
1063 1064 1065 1066 1067 1068 1069 1070 1071
class Item_func_area :public Item_real_func
{
  String value;
public:
  Item_func_area(Item *a) :Item_real_func(a) {}
  double val();
  const char *func_name() const { return "area"; }
};

unknown's avatar
unknown committed
1072

unknown's avatar
unknown committed
1073 1074 1075 1076 1077 1078 1079
class Item_func_glength :public Item_real_func
{
  String value;
public:
  Item_func_glength(Item *a) :Item_real_func(a) {}
  double val();
  const char *func_name() const { return "glength"; }
unknown's avatar
unknown committed
1080
};
1081

1082

1083 1084 1085
class Item_func_match_nl :public Item_func_match
{
public:
1086 1087 1088
  Item_func_match_nl(List<Item> &a, Item *b)
    :Item_func_match(a,b)
  { mode=FT_NL; }
1089 1090
  const char *func_name() const { return "match_nl"; }
};
unknown's avatar
unknown committed
1091

1092

1093 1094 1095
class Item_func_match_bool :public Item_func_match
{
public:
1096 1097 1098
  Item_func_match_bool(List<Item> &a, Item *b)
    :Item_func_match(a,b)
  { mode=FT_BOOL; }
1099
  const char *func_name() const { return "match_bool"; }
1100
};
unknown's avatar
unknown committed
1101

unknown's avatar
unknown committed
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111

class Item_func_bit_xor : public Item_int_func
{
public:
  Item_func_bit_xor(Item *a,Item *b) :Item_int_func(a,b) {}
  longlong val_int();
  const char *func_name() const { return "^"; }
  void fix_length_xor_dec() { unsigned_flag=1; }
};

1112
class Item_func_is_free_lock :public Item_int_func
unknown's avatar
unknown committed
1113 1114 1115
{
  String value;
public:
1116
  Item_func_is_free_lock(Item *a) :Item_int_func(a) {}
unknown's avatar
unknown committed
1117 1118 1119
  longlong val_int();
  const char *func_name() const { return "check_lock"; }
  void fix_length_and_dec() { decimals=0; max_length=1; maybe_null=1;}
unknown's avatar
unknown committed
1120 1121
};

unknown's avatar
unknown committed
1122 1123 1124 1125 1126
/* For type casts */

enum Item_cast
{
  ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
unknown's avatar
unknown committed
1127
  ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR
unknown's avatar
unknown committed
1128
};