sp_head.h 29.9 KB
Newer Older
1 2 3 4 5
/* -*- 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
6
   the Free Software Foundation; version 2 of the License.
7 8 9 10 11 12 13 14 15 16 17 18 19

   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_

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

#include <stddef.h>

26 27 28 29
// 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
30
#define TYPE_ENUM_TRIGGER   3
31

32 33 34
Item_result
sp_map_result_type(enum enum_field_types type);

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

38 39
uint
sp_get_flags_for_command(LEX *lex);
40

41
struct sp_label;
42
class sp_instr;
43
class sp_instr_opt_meta;
44
class sp_instr_jump_if_not;
45
struct sp_cond_type;
46
struct sp_variable;
47

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

  LEX_STRING m_db;
  LEX_STRING m_name;
  LEX_STRING m_qname;
55 56 57 58 59 60 61
  /*
    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;
62
  bool       m_explicit_name;                   /**< Prepend the db name? */
63

64 65
  sp_name(LEX_STRING db, LEX_STRING name, bool use_explicit_name)
    : m_db(db), m_name(name), m_explicit_name(use_explicit_name)
66
  {
67 68 69 70 71 72 73 74
    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.
  */
75
  sp_name(THD *thd, char *key, uint key_len);
76 77 78 79

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

80 81 82 83 84
  void set_routine_type(char type)
  {
    m_sroutines_key.str[0]= type;
  }

85 86 87 88
  ~sp_name()
  {}
};

89

90
bool
91
check_routine_name(LEX_STRING name);
92

serg@serg.mylan's avatar
serg@serg.mylan committed
93
class sp_head :private Query_arena
94 95 96 97
{
  sp_head(const sp_head &);	/* Prevent use of these */
  void operator=(sp_head &);

98
  MEM_ROOT main_mem_root;
99
public:
100
  /* Possible values of m_flags */
101
  enum {
102 103 104
    HAS_RETURN= 1,              // For FUNCTIONs only: is set if has RETURN
    MULTI_RESULTS= 8,           // Is set if a procedure with SELECT(s)
    CONTAINS_DYNAMIC_SQL= 16,   // Is set if a procedure with PREPARE/EXECUTE
105
    IS_INVOKED= 32,             // Is set if this sp_head is being used
106 107
    HAS_SET_AUTOCOMMIT_STMT= 64,// Is set if a procedure with 'set autocommit'
    /* Is set if a procedure with COMMIT (implicit or explicit) | ROLLBACK */
108 109 110
    HAS_COMMIT_OR_ROLLBACK= 128,
    HAS_SQLCOM_RESET= 2048,
    HAS_SQLCOM_FLUSH= 4096
111
  };
112

113 114
  /* TYPE_ENUM_FUNCTION, TYPE_ENUM_PROCEDURE or TYPE_ENUM_TRIGGER */
  int m_type;
115
  uint m_flags;                 // Boolean attributes of a stored routine
116 117 118

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

119
  const char *m_tmp_query;	// Temporary pointer to sub query string
120
  uint m_old_cmq;		// Old CLIENT_MULTI_QUERIES value
121
  st_sp_chistics *m_chistics;
122
  ulong m_sql_mode;		// For SHOW CREATE and execution
123
  LEX_STRING m_qname;		// db.name
124 125 126 127 128 129
  /**
    Key representing routine in the set of stored routines used by statement.
    [routine_type]db.name\0
    @sa sp_name::m_sroutines_key
  */
  LEX_STRING m_sroutines_key;
130
  LEX_STRING m_db;
131 132 133 134
  LEX_STRING m_name;
  LEX_STRING m_params;
  LEX_STRING m_body;
  LEX_STRING m_defstr;
135 136
  LEX_STRING m_definer_user;
  LEX_STRING m_definer_host;
137 138
  longlong m_created;
  longlong m_modified;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  /* 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;
165
  /*
166 167 168 169 170
    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.
171
  */
172
  HASH m_sroutines;
173
  // Pointers set during parsing
174 175 176
  const char *m_param_begin;
  const char *m_param_end;
  const char *m_body_begin;
177

178 179 180 181 182
  /*
    Security context for stored routine which should be run under
    definer privileges.
  */
  Security_context m_security_ctx;
183

184
  static void *
185
  operator new(size_t size) throw ();
186

187
  static void
188
  operator delete(void *ptr, size_t size) throw ();
189

190 191 192 193
  sp_head();

  // Initialize after we have reset mem_root
  void
194 195
  init(LEX *lex);

196 197 198 199
  /* Copy sp name from parser. */
  void
  init_sp_name(THD *thd, sp_name *spname);

200 201
  // Initialize strings after parsing header
  void
202
  init_strings(THD *thd, LEX *lex);
203

204 205
  int
  create(THD *thd);
206

207 208
  virtual ~sp_head();

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

213 214 215 216
  bool
  execute_trigger(THD *thd, const char *db, const char *table,
                  GRANT_INFO *grant_onfo);

217 218
  bool
  execute_function(THD *thd, Item **args, uint argcount, Field *return_fld);
219

220
  bool
221
  execute_procedure(THD *thd, List<Item> *args);
222

223 224 225 226 227 228
  int
  show_create_procedure(THD *thd);

  int
  show_create_function(THD *thd);

229 230
  void
  add_instr(sp_instr *instr);
231 232 233 234 235 236 237

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

238 239 240 241 242 243 244 245 246
  inline sp_instr *
  last_instruction()
  {
    sp_instr *i;

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

247
  // Resets lex in 'thd' and keeps a copy of the old one.
248
  bool
249 250 251 252 253 254 255
  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);

256
  // Put the instruction on the backpatch list, associated with the label.
257
  void
258
  push_backpatch(sp_instr *, struct sp_label *);
259

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

265 266
  // Start a new cont. backpatch level. If 'i' is NULL, the level is just incr.
  void
267
  new_cont_backpatch(sp_instr_opt_meta *i);
268 269 270

  // Add an instruction to the current level
  void
271
  add_cont_backpatch(sp_instr_opt_meta *i);
272 273 274 275 276

  // Backpatch (and pop) the current level to the current position.
  void
  do_cont_backpatch();

277 278 279
  char *name(uint *lenp = 0) const
  {
    if (lenp)
280 281
      *lenp= m_name.length;
    return m_name.str;
282 283
  }

284 285
  char *create_string(THD *thd, ulong *lenp);

286 287 288 289 290 291
  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);
292

293
  void set_info(longlong created, longlong modified,
294
		st_sp_chistics *chistics, ulong sql_mode);
295

296
  void set_definer(const char *definer, uint definerlen);
297
  void set_definer(const LEX_STRING *user_name, const LEX_STRING *host_name);
298

299 300 301
  void reset_thd_mem_root(THD *thd);

  void restore_thd_mem_root(THD *thd);
302

303 304 305
  /**
    Optimize the code.
  */
306
  void optimize();
307 308 309 310 311 312 313 314 315

  /**
    Helper used during flow analysis during code optimization.
    See the implementation of <code>opt_mark()</code>.
    @param ip the instruction to add to the leads list
    @param leads the list of remaining paths to explore in the graph that
    represents the code, during flow analysis.
  */
  void add_mark_lead(uint ip, List<sp_instr> *leads);
316

317
  void recursion_level_error(THD *thd);
318

319 320 321 322 323 324 325 326 327 328 329
  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;
  }
330

331 332
  /* Add tables used by routine to the table list. */
  bool add_used_tables_to_table_list(THD *thd,
333 334
                                     TABLE_LIST ***query_tables_last_ptr,
                                     TABLE_LIST *belong_to_view);
335

336 337 338 339 340 341 342 343 344 345 346
  /*
    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);
347 348
    else if (m_flags & HAS_SET_AUTOCOMMIT_STMT)
      my_error(ER_SP_CANT_SET_AUTOCOMMIT, MYF(0));
349
    else if (m_flags & HAS_COMMIT_OR_ROLLBACK)
350
      my_error(ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0));
351 352 353 354 355
    else if (m_flags & HAS_SQLCOM_RESET)
      my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "RESET");
    else if (m_flags & HAS_SQLCOM_FLUSH)
      my_error(ER_STMT_NOT_ALLOWED_IN_SF_OR_TRG, MYF(0), "FLUSH");

356
    return test(m_flags &
357 358
		(CONTAINS_DYNAMIC_SQL|MULTI_RESULTS|HAS_SET_AUTOCOMMIT_STMT|
                 HAS_COMMIT_OR_ROLLBACK|HAS_SQLCOM_RESET|HAS_SQLCOM_FLUSH));
359
  }
pem@mysql.com's avatar
pem@mysql.com committed
360 361 362 363 364 365

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


366 367
private:

monty@mysql.com's avatar
monty@mysql.com committed
368
  MEM_ROOT *m_thd_root;		// Temp. store for thd's mem_root
369
  THD *m_thd;			// Set if we have reset mem_root
370

371
  sp_pcontext *m_pcont;		// Parse context
372
  List<LEX> m_lex;		// Temp. store for the other lex
373
  DYNAMIC_ARRAY m_instr;	// The "instructions"
374 375 376 377 378
  typedef struct
  {
    struct sp_label *lab;
    sp_instr *instr;
  } bp_t;
379
  List<bp_t> m_backpatch;	// Instructions needing backpatching
380
  /*
381
    We need a special list for backpatching of instructions with a continue
382 383
    destination (in the case of a continue handler catching an error in
    the test), since it would otherwise interfere with the normal backpatch
384
    mechanism - e.g. jump_if_not instructions have two different destinations
385 386 387 388
    which are to be patched differently.
    Since these occur in a more restricted way (always the same "level" in
    the code), we don't need the label.
   */
389
  List<sp_instr_opt_meta> m_cont_backpatch;
390 391
  uint m_cont_level;            // The current cont. backpatch level

392 393 394
  /*
    Multi-set representing optimized list of tables to be locked by this
    routine. Does not include tables which are used by invoked routines.
395 396 397 398

    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.
399 400
  */
  HASH m_sptabs;
401

402
  bool
403 404
  execute(THD *thd);

405 406 407 408 409 410
  /**
    Perform a forward flow analysis in the generated code.
    Mark reachable instructions, for the optimizer.
  */
  void opt_mark();

411 412 413 414 415
  /*
    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);
416 417 418
}; // class sp_head : public Sql_alloc


419 420 421 422
//
// "Instructions"...
//

423
class sp_instr :public Query_arena, public Sql_alloc
424 425 426 427 428 429
{
  sp_instr(const sp_instr &);	/* Prevent use of these */
  void operator=(sp_instr &);

public:

430 431
  uint marked;
  uint m_ip;			// My index
432
  sp_pcontext *m_ctx;		// My parse context
433

434
  // Should give each a name or type code for debugging purposes?
435
  sp_instr(uint ip, sp_pcontext *ctx)
436
    :Query_arena(0, INITIALIZED_FOR_SP), marked(0), m_ip(ip), m_ctx(ctx)
437 438 439
  {}

  virtual ~sp_instr()
440
  { free_items(); }
441

442 443 444 445 446 447 448 449 450

  /*
    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
451 452 453 454 455 456 457
                    one). Note that this parameter is undefined in case of
                    errors, use get_cont_dest() to find the continuation
                    instruction for CONTINUE error handlers.

     RETURN
       0      on success,
       other  if some error occurred
458
  */
459

460 461
  virtual int execute(THD *thd, uint *nextp) = 0;

462 463 464 465 466 467 468 469 470
  /**
    Execute <code>open_and_lock_tables()</code> for this statement.
    Open and lock the tables used by this statement, as a pre-requisite
    to execute the core logic of this instruction with
    <code>exec_core()</code>.
    @param thd the current thread
    @param tables the list of tables to open and lock
    @return zero on success, non zero on failure.
  */
471
  int exec_open_and_lock_tables(THD *thd, TABLE_LIST *tables);
472 473 474

  /**
    Get the continuation destination of this instruction.
475
    @return the continuation destination
476
  */
477
  virtual uint get_cont_dest();
478

479 480 481 482 483 484 485
  /*
    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
486
    sp_lex_keeper class and its descendants (there are none currently).
487 488 489
  */
  virtual int exec_core(THD *thd, uint *nextp);

490
  virtual void print(String *str) = 0;
491

492
  virtual void backpatch(uint dest, sp_pcontext *dst_ctx)
493
  {}
494

495 496
  /*
    Mark this instruction as reachable during optimization and return the
497 498
    index to the next instruction. Jump instruction will add their
    destination to the leads list.
499
  */
500
  virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads)
501 502 503 504 505
  {
    marked= 1;
    return m_ip+1;
  }

506 507 508 509 510 511
  /*
    Short-cut jumps to jumps during optimization. This is used by the
    jump instructions' opt_mark() methods. 'start' is the starting point,
    used to prevent the mark sweep from looping for ever. Return the
    end destination.
  */
512
  virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start)
513 514 515 516
  {
    return m_ip;
  }

517 518 519 520 521 522
  /*
    Inform the instruction that it has been moved during optimization.
    Most instructions will simply update its index, but jump instructions
    must also take care of their destination pointers. Forward jumps get
    pushed to the backpatch list 'ibp'.
  */
523 524 525 526
  virtual void opt_move(uint dst, List<sp_instr> *ibp)
  {
    m_ip= dst;
  }
527

528 529 530
}; // class sp_instr : public Sql_alloc


531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
/*
  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)
551 552
    : m_lex(lex), m_lex_resp(lex_resp), 
      lex_query_tables_own_last(NULL)
553 554 555 556 557 558
  {
    lex->sp_lex_in_use= TRUE;
  }
  virtual ~sp_lex_keeper()
  {
    if (m_lex_resp)
559 560
    {
      lex_end(m_lex);
561
      delete m_lex;
562
    }
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
  }

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

578 579 580 581
  void disable_query_cache()
  {
    m_lex->safe_to_cache_query= 0;
  }
582 583 584 585 586 587 588 589
private:

  LEX *m_lex;
  /*
    Indicates whenever this sp_lex_keeper instance responsible
    for LEX deletion.
  */
  bool m_lex_resp;
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

  /*
    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;
609 610 611
};


612 613 614 615 616 617 618 619 620 621
//
// 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:

622 623
  LEX_STRING m_query;		// For thd->query

624 625
  sp_instr_stmt(uint ip, sp_pcontext *ctx, LEX *lex)
    : sp_instr(ip, ctx), m_lex_keeper(lex, TRUE)
626 627 628 629
  {
    m_query.str= 0;
    m_query.length= 0;
  }
630

631 632
  virtual ~sp_instr_stmt()
  {};
633

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

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

638
  virtual void print(String *str);
639

640 641
private:

642
  sp_lex_keeper m_lex_keeper;
643 644 645 646 647 648 649 650 651 652 653

}; // 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:

654
  sp_instr_set(uint ip, sp_pcontext *ctx,
655
	       uint offset, Item *val, enum enum_field_types type_arg,
656
               LEX *lex, bool lex_resp)
657
    : sp_instr(ip, ctx), m_offset(offset), m_value(val), m_type(type_arg),
658
      m_lex_keeper(lex, lex_resp)
659 660 661 662 663
  {}

  virtual ~sp_instr_set()
  {}

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

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

668 669
  virtual void print(String *str);

670 671
private:

672
  uint m_offset;		// Frame offset
673 674
  Item *m_value;
  enum enum_field_types m_type;	// The declared type
675
  sp_lex_keeper m_lex_keeper;
676 677 678

}; // class sp_instr_set : public sp_instr

679

680 681 682 683 684 685 686 687 688 689
/*
  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:

690
  sp_instr_set_trigger_field(uint ip, sp_pcontext *ctx,
691 692
                             Item_trigger_field *trg_fld,
                             Item *val, LEX *lex)
693
    : sp_instr(ip, ctx),
694
      trigger_field(trg_fld),
695
      value(val), m_lex_keeper(lex, TRUE)
696 697 698 699 700 701 702
  {}

  virtual ~sp_instr_set_trigger_field()
  {}

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

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

705 706
  virtual void print(String *str);

707
private:
708
  Item_trigger_field *trigger_field;
709
  Item *value;
710
  sp_lex_keeper m_lex_keeper;
711 712 713
}; // class sp_instr_trigger_field : public sp_instr


714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
/*
  An abstract class for all instructions with destinations that
  needs to be updated by the optimizer.
  Even if not all subclasses will use both the normal destination and
  the continuation destination, we put them both here for simplicity.
 */
class sp_instr_opt_meta : public sp_instr
{
public:

  uint m_dest;			// Where we will go
  uint m_cont_dest;             // Where continue handlers will go

  sp_instr_opt_meta(uint ip, sp_pcontext *ctx)
    : sp_instr(ip, ctx),
      m_dest(0), m_cont_dest(0), m_optdest(0), m_cont_optdest(0)
  {}

  sp_instr_opt_meta(uint ip, sp_pcontext *ctx, uint dest)
    : sp_instr(ip, ctx),
      m_dest(dest), m_cont_dest(0), m_optdest(0), m_cont_optdest(0)
  {}

  virtual ~sp_instr_opt_meta()
  {}

  virtual void set_destination(uint old_dest, uint new_dest)
    = 0;

743
  virtual uint get_cont_dest();
744

745 746 747 748 749 750 751 752
protected:

  sp_instr *m_optdest;		// Used during optimization
  sp_instr *m_cont_optdest;     // Used during optimization

}; // class sp_instr_opt_meta : public sp_instr

class sp_instr_jump : public sp_instr_opt_meta
753 754 755 756 757 758
{
  sp_instr_jump(const sp_instr_jump &);	/* Prevent use of these */
  void operator=(sp_instr_jump &);

public:

759
  sp_instr_jump(uint ip, sp_pcontext *ctx)
760
    : sp_instr_opt_meta(ip, ctx)
761 762
  {}

763
  sp_instr_jump(uint ip, sp_pcontext *ctx, uint dest)
764
    : sp_instr_opt_meta(ip, ctx, dest)
765 766 767 768 769
  {}

  virtual ~sp_instr_jump()
  {}

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

772 773
  virtual void print(String *str);

774
  virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads);
775

776
  virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start);
777 778 779

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

780
  virtual void backpatch(uint dest, sp_pcontext *dst_ctx)
781
  {
782 783
    if (m_dest == 0)		// Don't reset
      m_dest= dest;
784 785
  }

786 787 788
  /*
    Update the destination; used by the optimizer.
  */
789
  virtual void set_destination(uint old_dest, uint new_dest)
790
  {
791 792
    if (m_dest == old_dest)
      m_dest= new_dest;
793 794
  }

795
}; // class sp_instr_jump : public sp_instr_opt_meta
796 797 798 799 800 801 802 803 804


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:

805
  sp_instr_jump_if_not(uint ip, sp_pcontext *ctx, Item *i, LEX *lex)
806 807
    : sp_instr_jump(ip, ctx), m_expr(i),
      m_lex_keeper(lex, TRUE)
808 809
  {}

810
  sp_instr_jump_if_not(uint ip, sp_pcontext *ctx, Item *i, uint dest, LEX *lex)
811 812
    : sp_instr_jump(ip, ctx, dest), m_expr(i),
      m_lex_keeper(lex, TRUE)
813 814 815 816 817 818 819
  {}

  virtual ~sp_instr_jump_if_not()
  {}

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

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

822 823
  virtual void print(String *str);

824
  virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads);
825

826
  /* Override sp_instr_jump's shortcut; we stop here */
827
  virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start)
828 829 830 831
  {
    return m_ip;
  }

832 833 834 835 836 837 838 839 840
  virtual void opt_move(uint dst, List<sp_instr> *ibp);

  virtual void set_destination(uint old_dest, uint new_dest)
  {
    sp_instr_jump::set_destination(old_dest, new_dest);
    if (m_cont_dest == old_dest)
      m_cont_dest= new_dest;
  }

841 842 843
private:

  Item *m_expr;			// The condition
844
  sp_lex_keeper m_lex_keeper;
845 846 847

}; // class sp_instr_jump_if_not : public sp_instr_jump

848

849
class sp_instr_freturn : public sp_instr
850
{
851 852
  sp_instr_freturn(const sp_instr_freturn &);	/* Prevent use of these */
  void operator=(sp_instr_freturn &);
853 854 855

public:

856
  sp_instr_freturn(uint ip, sp_pcontext *ctx,
857 858 859
		   Item *val, enum enum_field_types type_arg, LEX *lex)
    : sp_instr(ip, ctx), m_value(val), m_type(type_arg),
      m_lex_keeper(lex, TRUE)
860 861
  {}

862
  virtual ~sp_instr_freturn()
863 864 865 866
  {}

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

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

869 870
  virtual void print(String *str);

871
  virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads)
872 873 874 875 876
  {
    marked= 1;
    return UINT_MAX;
  }

877 878 879 880
protected:

  Item *m_value;
  enum enum_field_types m_type;
881
  sp_lex_keeper m_lex_keeper;
882

883 884 885 886 887 888 889 890 891 892
}; // 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:

893 894
  sp_instr_hpush_jump(uint ip, sp_pcontext *ctx, int htype, uint fp)
    : sp_instr_jump(ip, ctx), m_type(htype), m_frame(fp)
895 896 897 898 899 900 901 902 903 904 905
  {
    m_cond.empty();
  }

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

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

906 907
  virtual void print(String *str);

908
  virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads);
909

910
  /* Override sp_instr_jump's shortcut; we stop here. */
911
  virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start)
912 913 914 915
  {
    return m_ip;
  }

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
  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:

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

  virtual ~sp_instr_hpop()
  {}

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

946 947
  virtual void print(String *str);

948 949 950 951 952 953 954
private:

  uint m_count;

}; // class sp_instr_hpop : public sp_instr


955
class sp_instr_hreturn : public sp_instr_jump
956 957 958 959 960 961
{
  sp_instr_hreturn(const sp_instr_hreturn &);	/* Prevent use of these */
  void operator=(sp_instr_hreturn &);

public:

962
  sp_instr_hreturn(uint ip, sp_pcontext *ctx, uint fp)
963
    : sp_instr_jump(ip, ctx), m_frame(fp)
964 965 966 967 968 969 970
  {}

  virtual ~sp_instr_hreturn()
  {}

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

971 972
  virtual void print(String *str);

973 974 975 976 977 978
  /* This instruction will not be short cut optimized. */
  virtual uint opt_shortcut_jump(sp_head *sp, sp_instr *start)
  {
    return m_ip;
  }

979
  virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads);
980

981 982 983 984
private:

  uint m_frame;

985
}; // class sp_instr_hreturn : public sp_instr_jump
986

987

988
/* This is DECLARE CURSOR */
989 990 991 992 993 994 995
class sp_instr_cpush : public sp_instr
{
  sp_instr_cpush(const sp_instr_cpush &); /* Prevent use of these */
  void operator=(sp_instr_cpush &);

public:

pem@mysql.com's avatar
pem@mysql.com committed
996 997
  sp_instr_cpush(uint ip, sp_pcontext *ctx, LEX *lex, uint offset)
    : sp_instr(ip, ctx), m_lex_keeper(lex, TRUE), m_cursor(offset)
998 999
  {}

1000 1001
  virtual ~sp_instr_cpush()
  {}
1002 1003 1004

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

1005 1006
  virtual void print(String *str);

1007 1008 1009 1010 1011 1012
  /*
    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 */ }
1013 1014
private:

1015
  sp_lex_keeper m_lex_keeper;
pem@mysql.com's avatar
pem@mysql.com committed
1016
  uint m_cursor;                /* Frame offset (for debugging) */
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

}; // 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:

1028 1029
  sp_instr_cpop(uint ip, sp_pcontext *ctx, uint count)
    : sp_instr(ip, ctx), m_count(count)
1030 1031 1032 1033 1034 1035 1036
  {}

  virtual ~sp_instr_cpop()
  {}

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

1037 1038
  virtual void print(String *str);

1039 1040 1041 1042 1043 1044 1045
private:

  uint m_count;

}; // class sp_instr_cpop : public sp_instr


1046
class sp_instr_copen : public sp_instr
1047 1048 1049 1050 1051 1052
{
  sp_instr_copen(const sp_instr_copen &); /* Prevent use of these */
  void operator=(sp_instr_copen &);

public:

1053
  sp_instr_copen(uint ip, sp_pcontext *ctx, uint c)
1054
    : sp_instr(ip, ctx), m_cursor(c)
1055 1056 1057 1058 1059 1060 1061
  {}

  virtual ~sp_instr_copen()
  {}

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

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

1064 1065
  virtual void print(String *str);

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
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:

1080 1081
  sp_instr_cclose(uint ip, sp_pcontext *ctx, uint c)
    : sp_instr(ip, ctx), m_cursor(c)
1082 1083 1084 1085 1086 1087 1088
  {}

  virtual ~sp_instr_cclose()
  {}

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

1089 1090
  virtual void print(String *str);

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
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:

1105 1106
  sp_instr_cfetch(uint ip, sp_pcontext *ctx, uint c)
    : sp_instr(ip, ctx), m_cursor(c)
1107 1108 1109 1110 1111 1112 1113 1114 1115
  {
    m_varlist.empty();
  }

  virtual ~sp_instr_cfetch()
  {}

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

1116 1117
  virtual void print(String *str);

1118
  void add_to_varlist(struct sp_variable *var)
1119 1120 1121 1122 1123 1124 1125
  {
    m_varlist.push_back(var);
  }

private:

  uint m_cursor;
1126
  List<struct sp_variable> m_varlist;
1127 1128 1129 1130

}; // class sp_instr_cfetch : public sp_instr


1131 1132 1133 1134 1135 1136 1137
class sp_instr_error : public sp_instr
{
  sp_instr_error(const sp_instr_error &); /* Prevent use of these */
  void operator=(sp_instr_error &);

public:

1138 1139
  sp_instr_error(uint ip, sp_pcontext *ctx, int errcode)
    : sp_instr(ip, ctx), m_errcode(errcode)
1140 1141 1142 1143 1144 1145 1146 1147 1148
  {}

  virtual ~sp_instr_error()
  {}

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

  virtual void print(String *str);

1149
  virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads)
1150 1151 1152 1153 1154
  {
    marked= 1;
    return UINT_MAX;
  }

1155 1156 1157 1158 1159 1160 1161
private:

  int m_errcode;

}; // class sp_instr_error : public sp_instr


1162
class sp_instr_set_case_expr : public sp_instr_opt_meta
1163 1164 1165 1166 1167
{
public:

  sp_instr_set_case_expr(uint ip, sp_pcontext *ctx, uint case_expr_id,
                         Item *case_expr, LEX *lex)
1168 1169 1170 1171 1172 1173
    : sp_instr_opt_meta(ip, ctx),
      m_case_expr_id(case_expr_id), m_case_expr(case_expr),
      m_lex_keeper(lex, TRUE)
  {}

  virtual ~sp_instr_set_case_expr()
1174 1175 1176 1177 1178 1179 1180 1181
  {}

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

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

  virtual void print(String *str);

1182
  virtual uint opt_mark(sp_head *sp, List<sp_instr> *leads);
1183 1184 1185 1186 1187 1188 1189 1190 1191

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

  virtual void set_destination(uint old_dest, uint new_dest)
  {
    if (m_cont_dest == old_dest)
      m_cont_dest= new_dest;
  }

1192 1193 1194 1195 1196 1197
private:

  uint m_case_expr_id;
  Item *m_case_expr;
  sp_lex_keeper m_lex_keeper;

1198
}; // class sp_instr_set_case_expr : public sp_instr_opt_meta
1199 1200


1201
#ifndef NO_EMBEDDED_ACCESS_CHECKS
1202 1203
bool
sp_change_security_context(THD *thd, sp_head *sp,
1204
                           Security_context **backup);
1205
void
1206
sp_restore_security_context(THD *thd, Security_context *backup);
1207 1208 1209 1210

bool
set_routine_security_ctx(THD *thd, sp_head *sp, bool is_proc,
                         Security_context **save_ctx);
1211
#endif /* NO_EMBEDDED_ACCESS_CHECKS */
1212

1213 1214 1215 1216
TABLE_LIST *
sp_add_to_query_tables(THD *thd, LEX *lex,
		       const char *db, const char *name,
		       thr_lock_type locktype);
1217 1218
Item *
sp_prepare_func_item(THD* thd, Item **it_addr);
1219

1220
bool
1221
sp_eval_expr(THD *thd, Field *result_field, Item **expr_item_ptr);
1222

1223
#endif /* _SP_HEAD_H_ */