/* -*- C++ -*- */
/* Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
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; version 2 of the License.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
#ifndef _SP_RCONTEXT_H_
#define _SP_RCONTEXT_H_
#ifdef USE_PRAGMA_INTERFACE
#pragma interface /* gcc class implementation */
#endif
#include "sql_class.h" // select_result_interceptor
#include "sp_pcontext.h" // sp_condition_value
///////////////////////////////////////////////////////////////////////////
// sp_rcontext declaration.
///////////////////////////////////////////////////////////////////////////
class sp_cursor;
class sp_lex_keeper;
class sp_instr_cpush;
class sp_instr_hpush_jump;
class Query_arena;
class sp_head;
class Item_cache;
class Virtual_tmp_table;
/*
This class is a runtime context of a Stored Routine. It is used in an
execution and is intended to contain all dynamic objects (i.e. objects, which
can be changed during execution), such as:
- stored routine variables;
- cursors;
- handlers;
Runtime context is used with sp_head class. sp_head class is intended to
contain all static things, related to the stored routines (code, for example).
sp_head instance creates runtime context for the execution of a stored
routine.
There is a parsing context (an instance of sp_pcontext class), which is used
on parsing stage. However, now it contains some necessary for an execution
things, such as definition of used stored routine variables. That's why
runtime context needs a reference to the parsing context.
*/
class sp_rcontext : public Sql_alloc
{
public:
/// Construct and properly initialize a new sp_rcontext instance. The static
/// create-function is needed because we need a way to return an error from
/// the constructor.
///
/// @param thd Thread handle.
/// @param root_parsing_ctx Top-level parsing context for this stored program.
/// @param return_value_fld Field object to store the return value
/// (for stored functions only).
///
/// @return valid sp_rcontext object or NULL in case of OOM-error.
static sp_rcontext *create(THD *thd,
const sp_head *owner,
const sp_pcontext *root_parsing_ctx,
Field *return_value_fld,
Row_definition_list &defs);
~sp_rcontext();
private:
sp_rcontext(const sp_head *owner,
const sp_pcontext *root_parsing_ctx,
Field *return_value_fld,
bool in_sub_stmt);
// Prevent use of copying constructor and operator.
sp_rcontext(const sp_rcontext &);
void operator=(sp_rcontext &);
public:
/// This class stores basic information about SQL-condition, such as:
/// - SQL error code;
/// - error level;
/// - SQLSTATE;
/// - text message.
///
/// It's used to organize runtime SQL-handler call stack.
///
/// Standard Sql_condition class can not be used, because we don't always have
/// an Sql_condition object for an SQL-condition in Diagnostics_area.
///
/// Eventually, this class should be moved to sql_error.h, and be a part of
/// standard SQL-condition processing (Diagnostics_area should contain an
/// object for active SQL-condition, not just information stored in DA's
/// fields).
class Sql_condition_info : public Sql_alloc,
public Sql_condition_identity
{
public:
/// Text message.
char *message;
/// The constructor.
///
/// @param _sql_condition The SQL condition.
/// @param arena Query arena for SP
Sql_condition_info(const Sql_condition *_sql_condition,
Query_arena *arena)
:Sql_condition_identity(*_sql_condition)
{
message= strdup_root(arena->mem_root, _sql_condition->get_message_text());
}
};
private:
/// This class represents a call frame of SQL-handler (one invocation of a
/// handler). Basically, it's needed to store continue instruction pointer for
/// CONTINUE SQL-handlers.
class Handler_call_frame : public Sql_alloc
{
public:
/// SQL-condition, triggered handler activation.
const Sql_condition_info *sql_condition;
/// Continue-instruction-pointer for CONTINUE-handlers.
/// The attribute contains 0 for EXIT-handlers.
uint continue_ip;
/// The constructor.
///
/// @param _sql_condition SQL-condition, triggered handler activation.
/// @param _continue_ip Continue instruction pointer.
Handler_call_frame(const Sql_condition_info *_sql_condition,
uint _continue_ip)
:sql_condition(_sql_condition),
continue_ip(_continue_ip)
{ }
};
public:
/// Arena used to (re) allocate items on. E.g. reallocate INOUT/OUT
/// SP-variables when they don't fit into prealloced items. This is common
/// situation with String items. It is used mainly in sp_eval_func_item().
Query_arena *callers_arena;
/// Flag to end an open result set before start executing an SQL-handler
/// (if one is found). Otherwise the client will hang due to a violation
/// of the client/server protocol.
bool end_partial_result_set;
bool pause_state;
bool quit_func;
uint instr_ptr;
/// The stored program for which this runtime context is created. Used for
/// checking if correct runtime context is used for variable handling,
/// and to access the package run-time context.
/// Also used by slow log.
const sp_head *m_sp;
/////////////////////////////////////////////////////////////////////////
// SP-variables.
/////////////////////////////////////////////////////////////////////////
uint argument_count() const
{
return m_root_parsing_ctx->context_var_count();
}
int set_variable(THD *thd, uint var_idx, Item **value);
int set_variable_row_field(THD *thd, uint var_idx, uint field_idx,
Item **value);
int set_variable_row_field_by_name(THD *thd, uint var_idx,
const LEX_CSTRING &field_name,
Item **value);
int set_variable_row(THD *thd, uint var_idx, List