sp_head.h 25.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* -*- C++ -*- */
/* Copyright (C) 2002 MySQL AB

   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.

   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.

   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 */

#ifndef _SP_HEAD_H_
#define _SP_HEAD_H_

21
#ifdef USE_PRAGMA_INTERFACE
22 23 24 25 26
#pragma interface			/* gcc class implementation */
#endif

#include <stddef.h>

27 28 29 30
// Values for the type enum. This reflects the order of the enum declaration
// in the CREATE TABLE command.
#define TYPE_ENUM_FUNCTION  1
#define TYPE_ENUM_PROCEDURE 2
31
#define TYPE_ENUM_TRIGGER   3
32

33 34 35
Item_result
sp_map_result_type(enum enum_field_types type);

36 37 38
Item::Type
sp_map_item_type(enum enum_field_types type);

39 40
uint
sp_get_flags_for_command(LEX *lex);
41

42
struct sp_label;
43
class sp_instr;
44
struct sp_cond_type;
45
struct sp_pvar;
46

47 48 49 50 51 52 53
class sp_name : public Sql_alloc
{
public:

  LEX_STRING m_db;
  LEX_STRING m_name;
  LEX_STRING m_qname;
54 55 56 57 58 59 60
  /*
    Key representing routine in the set of stored routines used by statement.
    Consists of 1-byte routine type and m_qname (which usually refences to
    same buffer). Note that one must complete initialization of the key by
    calling set_routine_type().
  */
  LEX_STRING m_sroutines_key;
61 62 63 64

  sp_name(LEX_STRING name)
    : m_name(name)
  {
65 66
    m_db.str= m_qname.str= m_sroutines_key.str= 0;
    m_db.length= m_qname.length= m_sroutines_key.length= 0;
67 68 69 70 71
  }

  sp_name(LEX_STRING db, LEX_STRING name)
    : m_db(db), m_name(name)
  {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    m_qname.str= m_sroutines_key.str= 0;
    m_qname.length= m_sroutines_key.length= 0;
  }

  /*
    Creates temporary sp_name object from key, used mainly
    for SP-cache lookups.
  */
  sp_name(char *key, uint key_len)
  {
    m_sroutines_key.str= key;
    m_sroutines_key.length= key_len;
    m_name.str= m_qname.str= key + 1;
    m_name.length= m_qname.length= key_len - 1;
    m_db.str= 0;
    m_db.length= 0;
88 89 90 91 92
  }

  // Init. the qualified name from the db and name.
  void init_qname(THD *thd);	// thd for memroot allocation

93 94 95 96 97
  void set_routine_type(char type)
  {
    m_sroutines_key.str[0]= type;
  }

98 99 100 101
  ~sp_name()
  {}
};

102 103 104 105
sp_name *
sp_name_current_db_new(THD *thd, LEX_STRING name);


unknown's avatar
unknown committed
106
class sp_head :private Query_arena
107 108 109 110
{
  sp_head(const sp_head &);	/* Prevent use of these */
  void operator=(sp_head &);

111
  MEM_ROOT main_mem_root;
112
public:
113
  /* Possible values of m_flags */
114
  enum {
115 116 117 118 119
    HAS_RETURN= 1,              // For FUNCTIONs only: is set if has RETURN
    IN_SIMPLE_CASE= 2,          // Is set if parsing a simple CASE
    IN_HANDLER= 4,              // Is set if the parser is in a handler body
    MULTI_RESULTS= 8,           // Is set if a procedure with SELECT(s)
    CONTAINS_DYNAMIC_SQL= 16,   // Is set if a procedure with PREPARE/EXECUTE
120
    IS_INVOKED= 32,             // Is set if this sp_head is being used
121 122 123
    HAS_SET_AUTOCOMMIT_STMT= 64,// Is set if a procedure with 'set autocommit'
    /* Is set if a procedure with COMMIT (implicit or explicit) | ROLLBACK */
    HAS_COMMIT_OR_ROLLBACK= 128
124
  };
125

126 127
  /* TYPE_ENUM_FUNCTION, TYPE_ENUM_PROCEDURE or TYPE_ENUM_TRIGGER */
  int m_type;
128
  uint m_flags;                 // Boolean attributes of a stored routine
129 130 131

  create_field m_return_field_def; /* This is used for FUNCTIONs only. */

132
  uchar *m_tmp_query;		// Temporary pointer to sub query string
133
  uint m_old_cmq;		// Old CLIENT_MULTI_QUERIES value
134
  st_sp_chistics *m_chistics;
135
  ulong m_sql_mode;		// For SHOW CREATE and execution
136 137
  LEX_STRING m_qname;		// db.name
  LEX_STRING m_db;
138 139 140 141
  LEX_STRING m_name;
  LEX_STRING m_params;
  LEX_STRING m_body;
  LEX_STRING m_defstr;
142 143
  LEX_STRING m_definer_user;
  LEX_STRING m_definer_host;
144 145
  longlong m_created;
  longlong m_modified;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  /* Recursion level of the current SP instance. The levels are numbered from 0 */
  ulong m_recursion_level;
  /*
    A list of diferent recursion level instances for the same procedure.
    For every recursion level we have a sp_head instance. This instances
    connected in the list. The list ordered by increasing recursion level
    (m_recursion_level).
  */
  sp_head *m_next_cached_sp;
  /*
    Pointer to the first element of the above list
  */
  sp_head *m_first_instance;
  /*
    Pointer to the first free (non-INVOKED) routine in the list of
    cached instances for this SP. This pointer is set only for the first
    SP in the list of instences (see above m_first_cached_sp pointer).
    The pointer equal to 0 if we have no free instances.
    For non-first instance value of this pointer meanless (point to itself);
  */
  sp_head *m_first_free_instance;
  /*
    Pointer to the last element in the list of instances of the SP.
    For non-first instance value of this pointer meanless (point to itself);
  */
  sp_head *m_last_cached_sp;
172
  /*
173 174 175 176 177
    Set containing names of stored routines used by this routine.
    Note that unlike elements of similar set for statement elements of this
    set are not linked in one list. Because of this we are able save memory
    by using for this set same objects that are used in 'sroutines' sets
    for statements of which this stored routine consists.
178
  */
179
  HASH m_sroutines;
180
  // Pointers set during parsing
unknown's avatar
unknown committed
181
  uchar *m_param_begin, *m_param_end, *m_body_begin;
182

183 184 185 186 187
  /*
    Security context for stored routine which should be run under
    definer privileges.
  */
  Security_context m_security_ctx;
188

189 190 191
  static void *
  operator new(size_t size);

192
  static void
193 194
  operator delete(void *ptr, size_t size);

195 196 197 198
  sp_head();

  // Initialize after we have reset mem_root
  void
199 200 201 202
  init(LEX *lex);

  // Initialize strings after parsing header
  void
203
  init_strings(THD *thd, LEX *lex, sp_name *name);
unknown's avatar
unknown committed
204

205 206
  int
  create(THD *thd);
207

208 209
  virtual ~sp_head();

210 211 212 213
  // Free memory
  void
  destroy();

214 215
  bool
  execute_function(THD *thd, Item **args, uint argcount, Field *return_fld);
216

217
  bool
218
  execute_procedure(THD *thd, List<Item> *args);
219

unknown's avatar
unknown committed
220 221 222 223 224 225
  int
  show_create_procedure(THD *thd);

  int
  show_create_function(THD *thd);

226 227
  void
  add_instr(sp_instr *instr);
228 229 230 231 232 233 234

  inline uint
  instructions()
  {
    return m_instr.elements;
  }

235 236 237 238 239 240 241 242 243
  inline sp_instr *
  last_instruction()
  {
    sp_instr *i;

    get_dynamic(&m_instr, (gptr)&i, m_instr.elements-1);
    return i;
  }

244 245 246 247 248 249 250 251 252
  // Resets lex in 'thd' and keeps a copy of the old one.
  void
  reset_lex(THD *thd);

  // Restores lex in 'thd' from our copy, but keeps some status from the
  // one in 'thd', like ptr, tables, fields, etc.
  void
  restore_lex(THD *thd);

253
  // Put the instruction on the backpatch list, associated with the label.
254
  void
255
  push_backpatch(sp_instr *, struct sp_label *);
256

257 258
  // Update all instruction with this label in the backpatch list to
  // the current position.
259
  void
260
  backpatch(struct sp_label *);
261

262 263 264 265 266 267 268
  // Check that no unresolved references exist.
  // If none found, 0 is returned, otherwise errors have been issued
  // and -1 is returned.
  // This is called by the parser at the end of a create procedure/function.
  int
  check_backpatch(THD *thd);

269 270 271
  char *name(uint *lenp = 0) const
  {
    if (lenp)
272 273
      *lenp= m_name.length;
    return m_name.str;
274 275
  }

276 277
  char *create_string(THD *thd, ulong *lenp);

278 279 280 281 282 283
  Field *create_result_field(uint field_max_length, const char *field_name,
                             TABLE *table);

  bool fill_field_definition(THD *thd, LEX *lex,
                             enum enum_field_types field_type,
                             create_field *field_def);
284

285
  void set_info(longlong created, longlong modified,
286
		st_sp_chistics *chistics, ulong sql_mode);
287

unknown's avatar
unknown committed
288
  void set_definer(const char *definer, uint definerlen);
289

290 291 292
  void reset_thd_mem_root(THD *thd);

  void restore_thd_mem_root(THD *thd);
293

294 295 296
  void optimize();
  void opt_mark(uint ip);

unknown's avatar
unknown committed
297
  void recursion_level_error(THD *thd);
298

299 300 301 302 303 304 305 306 307 308 309
  inline sp_instr *
  get_instr(uint i)
  {
    sp_instr *ip;

    if (i < m_instr.elements)
      get_dynamic(&m_instr, (gptr)&ip, i);
    else
      ip= NULL;
    return ip;
  }
unknown's avatar
unknown committed
310

311 312
  /* Add tables used by routine to the table list. */
  bool add_used_tables_to_table_list(THD *thd,
313 314
                                     TABLE_LIST ***query_tables_last_ptr,
                                     TABLE_LIST *belong_to_view);
315

316 317 318 319 320 321 322 323 324 325 326
  /*
    Check if this stored routine contains statements disallowed
    in a stored function or trigger, and set an appropriate error message
    if this is the case.
  */
  bool is_not_allowed_in_function(const char *where)
  {
    if (m_flags & CONTAINS_DYNAMIC_SQL)
      my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "Dynamic SQL");
    else if (m_flags & MULTI_RESULTS)
      my_error(ER_SP_NO_RETSET, MYF(0), where);
327 328
    else if (m_flags & HAS_SET_AUTOCOMMIT_STMT)
      my_error(ER_SP_CANT_SET_AUTOCOMMIT, MYF(0));
329 330 331 332 333 334
    else if (m_type != TYPE_ENUM_PROCEDURE &&
             (m_flags & sp_head::HAS_COMMIT_OR_ROLLBACK))
    {
      my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
      return TRUE;
    }
335 336
    return test(m_flags &
		(CONTAINS_DYNAMIC_SQL|MULTI_RESULTS|HAS_SET_AUTOCOMMIT_STMT));
337
  }
unknown's avatar
unknown committed
338 339 340 341 342 343

#ifndef DBUG_OFF
  int show_routine_code(THD *thd);
#endif


344 345
private:

unknown's avatar
unknown committed
346
  MEM_ROOT *m_thd_root;		// Temp. store for thd's mem_root
347
  THD *m_thd;			// Set if we have reset mem_root
348
  char *m_thd_db;		// Original thd->db pointer
349

350
  sp_pcontext *m_pcont;		// Parse context
351
  List<LEX> m_lex;		// Temp. store for the other lex
352
  DYNAMIC_ARRAY m_instr;	// The "instructions"
353 354 355 356 357
  typedef struct
  {
    struct sp_label *lab;
    sp_instr *instr;
  } bp_t;
358
  List<bp_t> m_backpatch;	// Instructions needing backpatching
359 360 361
  /*
    Multi-set representing optimized list of tables to be locked by this
    routine. Does not include tables which are used by invoked routines.
362 363 364 365

    Note: for prelocking-free SPs this multiset is constructed too.
    We do so because the same instance of sp_head may be called both
    in prelocked mode and in non-prelocked mode.
366 367
  */
  HASH m_sptabs;
368

369
  bool
370 371
  execute(THD *thd);

372 373 374 375 376
  /*
    Merge the list of tables used by query into the multi-set of tables used
    by routine.
  */
  bool merge_table_list(THD *thd, TABLE_LIST *table, LEX *lex_for_tmp_check);
377 378 379
}; // class sp_head : public Sql_alloc


380 381 382 383
//
// "Instructions"...
//

384
class sp_instr :public Query_arena, public Sql_alloc
385 386 387 388 389 390
{
  sp_instr(const sp_instr &);	/* Prevent use of these */
  void operator=(sp_instr &);

public:

391 392
  uint marked;
  uint m_ip;			// My index
393
  sp_pcontext *m_ctx;		// My parse context
394

395
  // Should give each a name or type code for debugging purposes?
396
  sp_instr(uint ip, sp_pcontext *ctx)
397
    :Query_arena(0, INITIALIZED_FOR_SP), marked(0), m_ip(ip), m_ctx(ctx)
398 399 400
  {}

  virtual ~sp_instr()
401
  { free_items(); }
402

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

  /*
    Execute this instruction

    SYNOPSIS
       execute()
         thd        Thread handle
         nextp  OUT index of the next instruction to execute. (For most
                    instructions this will be the instruction following this
                    one).
 
     RETURN 
       0      on success, 
       other  if some error occured
  */
  
419 420
  virtual int execute(THD *thd, uint *nextp) = 0;

421 422 423 424 425 426 427
  /*
    Execute core function of instruction after all preparations (e.g.
    setting of proper LEX, saving part of the thread context have been
    done).

    Should be implemented for instructions using expressions or whole
    statements (thus having to have own LEX). Used in concert with
428
    sp_lex_keeper class and its descendants (there are none currently).
429 430 431
  */
  virtual int exec_core(THD *thd, uint *nextp);

432
  virtual void print(String *str) = 0;
433

434
  virtual void backpatch(uint dest, sp_pcontext *dst_ctx)
435
  {}
436

437 438 439 440 441 442
  virtual uint opt_mark(sp_head *sp)
  {
    marked= 1;
    return m_ip+1;
  }

443
  virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start)
444 445 446 447 448 449 450 451
  {
    return m_ip;
  }

  virtual void opt_move(uint dst, List<sp_instr> *ibp)
  {
    m_ip= dst;
  }
452

453 454 455
}; // class sp_instr : public Sql_alloc


456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
/*
  Auxilary class to which instructions delegate responsibility
  for handling LEX and preparations before executing statement
  or calculating complex expression.

  Exist mainly to avoid having double hierarchy between instruction
  classes.

  TODO: Add ability to not store LEX and do any preparations if
        expression used is simple.
*/

class sp_lex_keeper
{
  /* Prevent use of these */
  sp_lex_keeper(const sp_lex_keeper &);
  void operator=(sp_lex_keeper &);
public:

  sp_lex_keeper(LEX *lex, bool lex_resp)
476 477
    : m_lex(lex), m_lex_resp(lex_resp), 
      lex_query_tables_own_last(NULL)
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
  {
    lex->sp_lex_in_use= TRUE;
  }
  virtual ~sp_lex_keeper()
  {
    if (m_lex_resp)
      delete m_lex;
  }

  /*
    Prepare execution of instruction using LEX, if requested check whenever
    we have read access to tables used and open/lock them, call instruction's
    exec_core() method, perform cleanup afterwards.
  */
  int reset_lex_and_exec_core(THD *thd, uint *nextp, bool open_tables,
                              sp_instr* instr);

  inline uint sql_command() const
  {
    return (uint)m_lex->sql_command;
  }

500 501 502 503
  void disable_query_cache()
  {
    m_lex->safe_to_cache_query= 0;
  }
504 505 506 507 508 509 510 511
private:

  LEX *m_lex;
  /*
    Indicates whenever this sp_lex_keeper instance responsible
    for LEX deletion.
  */
  bool m_lex_resp;
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

  /*
    Support for being able to execute this statement in two modes:
    a) inside prelocked mode set by the calling procedure or its ancestor.
    b) outside of prelocked mode, when this statement enters/leaves
       prelocked mode itself.
  */
  
  /*
    List of additional tables this statement needs to lock when it
    enters/leaves prelocked mode on its own.
  */
  TABLE_LIST *prelocking_tables;

  /*
    The value m_lex->query_tables_own_last should be set to this when the
    statement enters/leaves prelocked mode on its own.
  */
  TABLE_LIST **lex_query_tables_own_last;
531 532 533
};


534 535 536 537 538 539 540 541 542 543
//
// Call out to some prepared SQL statement.
//
class sp_instr_stmt : public sp_instr
{
  sp_instr_stmt(const sp_instr_stmt &);	/* Prevent use of these */
  void operator=(sp_instr_stmt &);

public:

544 545
  LEX_STRING m_query;		// For thd->query

546 547
  sp_instr_stmt(uint ip, sp_pcontext *ctx, LEX *lex)
    : sp_instr(ip, ctx), m_lex_keeper(lex, TRUE)
548 549 550 551
  {
    m_query.str= 0;
    m_query.length= 0;
  }
552

553 554
  virtual ~sp_instr_stmt()
  {};
555

556
  virtual int execute(THD *thd, uint *nextp);
557

558
  virtual int exec_core(THD *thd, uint *nextp);
559

560
  virtual void print(String *str);
561

562 563
private:

564
  sp_lex_keeper m_lex_keeper;
565 566 567 568 569 570 571 572 573 574 575

}; // class sp_instr_stmt : public sp_instr


class sp_instr_set : public sp_instr
{
  sp_instr_set(const sp_instr_set &);	/* Prevent use of these */
  void operator=(sp_instr_set &);

public:

576
  sp_instr_set(uint ip, sp_pcontext *ctx,
577 578 579 580
	       uint offset, Item *val, enum enum_field_types type,
               LEX *lex, bool lex_resp)
    : sp_instr(ip, ctx), m_offset(offset), m_value(val), m_type(type),
      m_lex_keeper(lex, lex_resp)
581 582 583 584 585
  {}

  virtual ~sp_instr_set()
  {}

586
  virtual int execute(THD *thd, uint *nextp);
587

588 589
  virtual int exec_core(THD *thd, uint *nextp);

590 591
  virtual void print(String *str);

592 593
private:

594
  uint m_offset;		// Frame offset
595 596
  Item *m_value;
  enum enum_field_types m_type;	// The declared type
597
  sp_lex_keeper m_lex_keeper;
598 599 600

}; // class sp_instr_set : public sp_instr

601

602 603 604 605 606 607 608 609 610 611
/*
  Set NEW/OLD row field value instruction. Used in triggers.
*/
class sp_instr_set_trigger_field : public sp_instr
{
  sp_instr_set_trigger_field(const sp_instr_set_trigger_field &);
  void operator=(sp_instr_set_trigger_field &);

public:

unknown's avatar
unknown committed
612
  sp_instr_set_trigger_field(uint ip, sp_pcontext *ctx,
613 614
                             Item_trigger_field *trg_fld,
                             Item *val, LEX *lex)
unknown's avatar
unknown committed
615
    : sp_instr(ip, ctx),
616
      trigger_field(trg_fld),
617
      value(val), m_lex_keeper(lex, TRUE)
618 619 620 621 622 623 624
  {}

  virtual ~sp_instr_set_trigger_field()
  {}

  virtual int execute(THD *thd, uint *nextp);

625 626
  virtual int exec_core(THD *thd, uint *nextp);

627 628
  virtual void print(String *str);

629
private:
630
  Item_trigger_field *trigger_field;
631
  Item *value;
632
  sp_lex_keeper m_lex_keeper;
633 634 635
}; // class sp_instr_trigger_field : public sp_instr


636 637 638 639 640 641 642
class sp_instr_jump : public sp_instr
{
  sp_instr_jump(const sp_instr_jump &);	/* Prevent use of these */
  void operator=(sp_instr_jump &);

public:

643 644
  uint m_dest;			// Where we will go

645 646
  sp_instr_jump(uint ip, sp_pcontext *ctx)
    : sp_instr(ip, ctx), m_dest(0), m_optdest(0)
647 648
  {}

649 650
  sp_instr_jump(uint ip, sp_pcontext *ctx, uint dest)
    : sp_instr(ip, ctx), m_dest(dest), m_optdest(0)
651 652 653 654 655
  {}

  virtual ~sp_instr_jump()
  {}

656
  virtual int execute(THD *thd, uint *nextp);
657

658 659
  virtual void print(String *str);

660 661
  virtual uint opt_mark(sp_head *sp);

662
  virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start);
663 664 665

  virtual void opt_move(uint dst, List<sp_instr> *ibp);

666
  virtual void backpatch(uint dest, sp_pcontext *dst_ctx)
667
  {
668 669
    if (m_dest == 0)		// Don't reset
      m_dest= dest;
670 671
  }

672
protected:
673

674
  sp_instr *m_optdest;		// Used during optimization
675 676 677 678 679 680 681 682 683 684 685

}; // class sp_instr_jump : public sp_instr


class sp_instr_jump_if : public sp_instr_jump
{
  sp_instr_jump_if(const sp_instr_jump_if &); /* Prevent use of these */
  void operator=(sp_instr_jump_if &);

public:

686 687
  sp_instr_jump_if(uint ip, sp_pcontext *ctx, Item *i, LEX *lex)
    : sp_instr_jump(ip, ctx), m_expr(i), m_lex_keeper(lex, TRUE)
688 689
  {}

690 691
  sp_instr_jump_if(uint ip, sp_pcontext *ctx, Item *i, uint dest, LEX *lex)
    : sp_instr_jump(ip, ctx, dest), m_expr(i), m_lex_keeper(lex, TRUE)
692 693 694 695 696 697 698
  {}

  virtual ~sp_instr_jump_if()
  {}

  virtual int execute(THD *thd, uint *nextp);

699 700
  virtual int exec_core(THD *thd, uint *nextp);

701 702
  virtual void print(String *str);

703 704
  virtual uint opt_mark(sp_head *sp);

705
  virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start)
706 707 708 709
  {
    return m_ip;
  }

710 711 712
private:

  Item *m_expr;			// The condition
713
  sp_lex_keeper m_lex_keeper;
714 715 716 717 718 719 720 721 722 723 724

}; // class sp_instr_jump_if : public sp_instr_jump


class sp_instr_jump_if_not : public sp_instr_jump
{
  sp_instr_jump_if_not(const sp_instr_jump_if_not &); /* Prevent use of these */
  void operator=(sp_instr_jump_if_not &);

public:

725 726
  sp_instr_jump_if_not(uint ip, sp_pcontext *ctx, Item *i, LEX *lex)
    : sp_instr_jump(ip, ctx), m_expr(i), m_lex_keeper(lex, TRUE)
727 728
  {}

729 730
  sp_instr_jump_if_not(uint ip, sp_pcontext *ctx, Item *i, uint dest, LEX *lex)
    : sp_instr_jump(ip, ctx, dest), m_expr(i), m_lex_keeper(lex, TRUE)
731 732 733 734 735 736 737
  {}

  virtual ~sp_instr_jump_if_not()
  {}

  virtual int execute(THD *thd, uint *nextp);

738 739
  virtual int exec_core(THD *thd, uint *nextp);

740 741
  virtual void print(String *str);

742 743
  virtual uint opt_mark(sp_head *sp);

744
  virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start)
745 746 747 748
  {
    return m_ip;
  }

749 750 751
private:

  Item *m_expr;			// The condition
752
  sp_lex_keeper m_lex_keeper;
753 754 755

}; // class sp_instr_jump_if_not : public sp_instr_jump

756

757
class sp_instr_freturn : public sp_instr
758
{
759 760
  sp_instr_freturn(const sp_instr_freturn &);	/* Prevent use of these */
  void operator=(sp_instr_freturn &);
761 762 763

public:

764
  sp_instr_freturn(uint ip, sp_pcontext *ctx,
765 766
		   Item *val, enum enum_field_types type, LEX *lex)
    : sp_instr(ip, ctx), m_value(val), m_type(type), m_lex_keeper(lex, TRUE)
767 768
  {}

769
  virtual ~sp_instr_freturn()
770 771 772 773
  {}

  virtual int execute(THD *thd, uint *nextp);

774 775
  virtual int exec_core(THD *thd, uint *nextp);

776 777
  virtual void print(String *str);

778 779 780 781 782 783
  virtual uint opt_mark(sp_head *sp)
  {
    marked= 1;
    return UINT_MAX;
  }

784 785 786 787
protected:

  Item *m_value;
  enum enum_field_types m_type;
788
  sp_lex_keeper m_lex_keeper;
789

790 791 792 793 794 795 796 797 798 799
}; // class sp_instr_freturn : public sp_instr


class sp_instr_hpush_jump : public sp_instr_jump
{
  sp_instr_hpush_jump(const sp_instr_hpush_jump &); /* Prevent use of these */
  void operator=(sp_instr_hpush_jump &);

public:

800 801
  sp_instr_hpush_jump(uint ip, sp_pcontext *ctx, int htype, uint fp)
    : sp_instr_jump(ip, ctx), m_type(htype), m_frame(fp)
802 803 804 805 806 807 808 809 810 811 812
  {
    m_cond.empty();
  }

  virtual ~sp_instr_hpush_jump()
  {
    m_cond.empty();
  }

  virtual int execute(THD *thd, uint *nextp);

813 814
  virtual void print(String *str);

815 816
  virtual uint opt_mark(sp_head *sp);

817
  virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start)
818 819 820 821
  {
    return m_ip;
  }

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
  inline void add_condition(struct sp_cond_type *cond)
  {
    m_cond.push_front(cond);
  }

private:

  int m_type;			// Handler type
  uint m_frame;
  List<struct sp_cond_type> m_cond;

}; // class sp_instr_hpush_jump : public sp_instr_jump


class sp_instr_hpop : public sp_instr
{
  sp_instr_hpop(const sp_instr_hpop &);	/* Prevent use of these */
  void operator=(sp_instr_hpop &);

public:

843 844
  sp_instr_hpop(uint ip, sp_pcontext *ctx, uint count)
    : sp_instr(ip, ctx), m_count(count)
845 846 847 848 849 850 851
  {}

  virtual ~sp_instr_hpop()
  {}

  virtual int execute(THD *thd, uint *nextp);

852 853
  virtual void print(String *str);

854
  virtual void backpatch(uint dest, sp_pcontext *dst_ctx);
855 856 857 858 859 860 861 862

  virtual uint opt_mark(sp_head *sp)
  {
    if (m_count)
      marked= 1;
    return m_ip+1;
  }

863 864 865 866 867 868 869
private:

  uint m_count;

}; // class sp_instr_hpop : public sp_instr


870
class sp_instr_hreturn : public sp_instr_jump
871 872 873 874 875 876
{
  sp_instr_hreturn(const sp_instr_hreturn &);	/* Prevent use of these */
  void operator=(sp_instr_hreturn &);

public:

877
  sp_instr_hreturn(uint ip, sp_pcontext *ctx, uint fp)
878
    : sp_instr_jump(ip, ctx), m_frame(fp)
879 880 881 882 883 884 885
  {}

  virtual ~sp_instr_hreturn()
  {}

  virtual int execute(THD *thd, uint *nextp);

886 887
  virtual void print(String *str);

888
  virtual uint opt_mark(sp_head *sp);
889

890 891 892 893 894 895
private:

  uint m_frame;

}; // class sp_instr_hreturn : public sp_instr

896

897
/* This is DECLARE CURSOR */
898 899 900 901 902 903 904
class sp_instr_cpush : public sp_instr
{
  sp_instr_cpush(const sp_instr_cpush &); /* Prevent use of these */
  void operator=(sp_instr_cpush &);

public:

unknown's avatar
unknown committed
905 906
  sp_instr_cpush(uint ip, sp_pcontext *ctx, LEX *lex, uint offset)
    : sp_instr(ip, ctx), m_lex_keeper(lex, TRUE), m_cursor(offset)
907 908
  {}

909 910
  virtual ~sp_instr_cpush()
  {}
911 912 913

  virtual int execute(THD *thd, uint *nextp);

914 915
  virtual void print(String *str);

916 917 918 919 920 921
  /*
    This call is used to cleanup the instruction when a sensitive
    cursor is closed. For now stored procedures always use materialized
    cursors and the call is not used.
  */
  virtual void cleanup_stmt() { /* no op */ }
922 923
private:

924
  sp_lex_keeper m_lex_keeper;
unknown's avatar
unknown committed
925
  uint m_cursor;                /* Frame offset (for debugging) */
926 927 928 929 930 931 932 933 934 935 936

}; // class sp_instr_cpush : public sp_instr


class sp_instr_cpop : public sp_instr
{
  sp_instr_cpop(const sp_instr_cpop &); /* Prevent use of these */
  void operator=(sp_instr_cpop &);

public:

937 938
  sp_instr_cpop(uint ip, sp_pcontext *ctx, uint count)
    : sp_instr(ip, ctx), m_count(count)
939 940 941 942 943 944 945
  {}

  virtual ~sp_instr_cpop()
  {}

  virtual int execute(THD *thd, uint *nextp);

946 947
  virtual void print(String *str);

948
  virtual void backpatch(uint dest, sp_pcontext *dst_ctx);
949 950 951 952 953 954 955 956

  virtual uint opt_mark(sp_head *sp)
  {
    if (m_count)
      marked= 1;
    return m_ip+1;
  }

957 958 959 960 961 962 963
private:

  uint m_count;

}; // class sp_instr_cpop : public sp_instr


964
class sp_instr_copen : public sp_instr
965 966 967 968 969 970
{
  sp_instr_copen(const sp_instr_copen &); /* Prevent use of these */
  void operator=(sp_instr_copen &);

public:

971
  sp_instr_copen(uint ip, sp_pcontext *ctx, uint c)
972
    : sp_instr(ip, ctx), m_cursor(c)
973 974 975 976 977 978 979
  {}

  virtual ~sp_instr_copen()
  {}

  virtual int execute(THD *thd, uint *nextp);

980 981
  virtual int exec_core(THD *thd, uint *nextp);

982 983
  virtual void print(String *str);

984 985 986 987 988 989 990 991 992 993 994 995 996 997
private:

  uint m_cursor;		// Stack index

}; // class sp_instr_copen : public sp_instr_stmt


class sp_instr_cclose : public sp_instr
{
  sp_instr_cclose(const sp_instr_cclose &); /* Prevent use of these */
  void operator=(sp_instr_cclose &);

public:

998 999
  sp_instr_cclose(uint ip, sp_pcontext *ctx, uint c)
    : sp_instr(ip, ctx), m_cursor(c)
1000 1001 1002 1003 1004 1005 1006
  {}

  virtual ~sp_instr_cclose()
  {}

  virtual int execute(THD *thd, uint *nextp);

1007 1008
  virtual void print(String *str);

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
private:

  uint m_cursor;

}; // class sp_instr_cclose : public sp_instr


class sp_instr_cfetch : public sp_instr
{
  sp_instr_cfetch(const sp_instr_cfetch &); /* Prevent use of these */
  void operator=(sp_instr_cfetch &);

public:

1023 1024
  sp_instr_cfetch(uint ip, sp_pcontext *ctx, uint c)
    : sp_instr(ip, ctx), m_cursor(c)
1025 1026 1027 1028 1029 1030 1031 1032 1033
  {
    m_varlist.empty();
  }

  virtual ~sp_instr_cfetch()
  {}

  virtual int execute(THD *thd, uint *nextp);

1034 1035
  virtual void print(String *str);

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
  void add_to_varlist(struct sp_pvar *var)
  {
    m_varlist.push_back(var);
  }

private:

  uint m_cursor;
  List<struct sp_pvar> m_varlist;

}; // class sp_instr_cfetch : public sp_instr


1049 1050 1051 1052 1053 1054 1055
class sp_instr_error : public sp_instr
{
  sp_instr_error(const sp_instr_error &); /* Prevent use of these */
  void operator=(sp_instr_error &);

public:

1056 1057
  sp_instr_error(uint ip, sp_pcontext *ctx, int errcode)
    : sp_instr(ip, ctx), m_errcode(errcode)
1058 1059 1060 1061 1062 1063 1064 1065 1066
  {}

  virtual ~sp_instr_error()
  {}

  virtual int execute(THD *thd, uint *nextp);

  virtual void print(String *str);

1067 1068 1069 1070 1071 1072
  virtual uint opt_mark(sp_head *sp)
  {
    marked= 1;
    return UINT_MAX;
  }

1073 1074 1075 1076 1077 1078 1079
private:

  int m_errcode;

}; // class sp_instr_error : public sp_instr


1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
class sp_instr_set_case_expr :public sp_instr
{
public:

  sp_instr_set_case_expr(uint ip, sp_pcontext *ctx, uint case_expr_id,
                         Item *case_expr, LEX *lex)
    :sp_instr(ip, ctx), m_case_expr_id(case_expr_id), m_case_expr(case_expr),
     m_lex_keeper(lex, TRUE)
  {}

  virtual int execute(THD *thd, uint *nextp);

  virtual int exec_core(THD *thd, uint *nextp);

  virtual void print(String *str);

private:

  uint m_case_expr_id;
  Item *m_case_expr;
  sp_lex_keeper m_lex_keeper;

}; // class sp_instr_set_case_expr : public sp_instr


1105
#ifndef NO_EMBEDDED_ACCESS_CHECKS
1106 1107
bool
sp_change_security_context(THD *thd, sp_head *sp,
1108
                           Security_context **backup);
1109
void
1110
sp_restore_security_context(THD *thd, Security_context *backup);
1111
#endif /* NO_EMBEDDED_ACCESS_CHECKS */
1112

1113 1114 1115 1116
TABLE_LIST *
sp_add_to_query_tables(THD *thd, LEX *lex,
		       const char *db, const char *name,
		       thr_lock_type locktype);
1117 1118
Item *
sp_prepare_func_item(THD* thd, Item **it_addr);
1119

1120 1121
bool
sp_eval_expr(THD *thd, Field *result_field, Item *expr_item);
1122

1123
#endif /* _SP_HEAD_H_ */