mirror of
https://github.com/MariaDB/server.git
synced 2025-02-01 19:41:47 +01:00
2a0d2fef51
cause ROLLBACK of statement", part 1. Review fixes. Do not send OK/EOF packets to the client until we reached the end of the current statement. This is a consolidation, to keep the functionality that is shared by all SQL statements in one place in the server. Currently this functionality includes: - close_thread_tables() - log_slow_statement(). After this patch and the subsequent patch for Bug#12713, it shall also include: - ha_autocommit_or_rollback() - net_end_statement() - query_cache_end_of_result(). In future it may also include: - mysql_reset_thd_for_next_command(). include/mysql_com.h: Rename now unused members of NET: no_send_ok, no_send_error, report_error. These were server-specific variables related to the client/server protocol. They have been made obsolete by this patch. Previously the same members of NET were used to store the error message both on the client and on the server. The error message was stored in net.last_error (client: mysql->net.last_error, server: thd->net.last_error). The error code was stored in net.last_errno (client: mysql->net.last_errno, server: thd->net.last_errno). The server error code and message are now stored elsewhere (in the Diagnostics_area), thus NET members are no longer used by the server. Rename last_error to client_last_error, last_errno to client_last_errno to avoid potential bugs introduced by merges. include/mysql_h.ic: Update the ABI file to reflect a rename. Renames do not break the binary compatibility. libmysql/libmysql.c: Rename last_error to client_last_error, last_errno to client_last_errno. This is necessary to ensure no unnoticed bugs introduced by merged changesets. Remove net.report_error, net.no_send_ok, net.no_send_error. libmysql/manager.c: Rename net.last_errno to net.client_last_errno. libmysqld/lib_sql.cc: Rename net.last_errno to net.client_last_errno. Update the embedded implementation of the client-server protocol to reflect the refactoring of protocol.cc. libmysqld/libmysqld.c: Rename net.last_errno to net.client_last_errno. mysql-test/r/events.result: Update to reflect the change in mysql_rm_db(). Now we drop stored routines and events for a given database name only if there is a directory for this database name. ha_drop_database() and query_cache_invalidate() are called likewise. Previously we would attempt to drop routines/events even if database directory was not found (it worked, since routines and events are stored in tables). This fixes Bug 29958 "Weird message on DROP DATABASE if mysql.proc does not exist". The change was done because the previous code used to call send_ok() twice, which led to an assertion failure when asserts against it were added by this patch. mysql-test/r/grant.result: Fix the patch for Bug 16470, now FLUSH PRIVILEGES produces an error if mysql.procs_priv is missing. This fixes the assert that send_ok() must not called after send_error() (the original patch for Bug 16470 was prone to this). mysql-test/suite/rpl/r/rpl_row_tabledefs_2myisam.result: Produce a more detailed error message. mysql-test/suite/rpl/r/rpl_row_tabledefs_3innodb.result: Produce a more detailed error message. mysql-test/t/grant.test: Update the test, now FLUSH PRIVILEGES returns an error if mysql.procs_priv is missing. server-tools/instance-manager/mysql_connection.cc: Rename net.last_errno to net.client_last_errno. sql/ha_ndbcluster_binlog.cc: Add asserts. Use getters to access statement status information. Add a comment why run_query() is broken. Reset the diagnostics area in the end of run_query() to fulfill the invariant that the diagnostics_area is never assigned twice per statement (see the comment in the code when this can happen). We still do not clear thd->is_fatal_error and thd->is_slave_error, which may lead to bugs, I consider the whole affair as something to be dealt with separately. sql/ha_partition.cc: fatal_error() doesn't set an error by itself. Perhaps we should remove this method altogether and instead add a flag to my_error to set thd->is_fatal_error property. Meanwhile, this change is a part of inspection made to the entire source code with the goal to ensure that fatal_error() is always accompanied by my_error(). sql/item_func.cc: There is no net.last_error anymore. Remove the obsolete assignment. sql/log_event.cc: Use getters to access statement error status information. sql/log_event_old.cc: Use getters to access statement error status information. sql/mysqld.cc: Previously, if a continue handler for an error was found, my_message_sql() would not set an error in THD. Since the current statement must be aborted in any case, find_handler() had a hack to assign thd->net.report_error to 1. Remove this hack. Set an error in my_message_sql() even if the continue handler is found. The error will be cleared anyway when the handler is executed. This is one action among many in this patch to ensure the invariant that whenever thd->is_error() is TRUE, we have a message in thd->main_da.message(). sql/net_serv.cc: Use a full-blown my_error() in net_serv.cc to report an error, instead of just setting net->last_errno. This ensures the invariant that whenever thd->is_error() returns TRUE, we have a message in thd->main_da.message(). Remove initialization of removed NET members. sql/opt_range.cc: Use my_error() instead of just raising thd->net.report_error. This ensures the invariant that whenever thd->is_error() returns TRUE, there is a message in thd->main_da.message(). sql/opt_sum.cc: Move invocation of fatal_error() right next to the place where we set the error message. That makes it easier to track that whenever fatal_error() is called, there is a message in THD. sql/protocol.cc: Rename send_ok() and send_eof() to net_send_ok() and net_send_eof() respectively. These functions write directly to the network and are not for use anywhere outside the client/server protocol code. Remove the code that was responsible for cases when either there is no error code, or no error message, or both. Instead the calling code ensures that they are always present. Asserts are added to enforce the invariant. Instead of a direct access to thd->server_status and thd->total_warn_count use function parameters, since these from now on don't always come directly from THD. Introduce net_end_statement(), the single-entry-point replacement API for send_ok(), send_eof() and net_send_error(). Implement Protocol::end_partial_result_set to use in select_send::abort() when there is a continue handler. sql/protocol.h: Update declarations. sql/repl_failsafe.cc: Use getters to access statement status information in THD. Rename net.last_error to net.client_last_error. sql/rpl_record.cc: Set an error message in prepare_record() if there is no default value for the field -- later we do print this message to the client. sql/rpl_rli.cc: Use getters to access statement status information in THD. sql/slave.cc: In create_table_from_dump() (a common function that is used in LOAD MASTER TABLE SQL statement and COM_LOAD_MASTER_DATA), instead of hacks with no_send_ok, clear the diagnostics area when mysql_rm_table() succeeded. Update has_temporary_error() to work correctly when no error is set. This is the case when Incident_log_event is executed: it always returns an error but does not set an error message. Use getters to access error status information. sql/sp_head.cc: Instead of hacks with no_send_error, work through the diagnostics area interface to suppress sending of OK/ERROR packets to the client. Move query_cache_end_of_result before log_slow_statement(), similarly to how it's done in dispatch_command(). sql/sp_rcontext.cc: Remove hacks with assignment of thd->net.report_error, they are not necessary any more (see the changes in mysqld.cc). sql/sql_acl.cc: Use getters to access error status information in THD. sql/sql_base.cc: Access thd->main_da.sql_errno() only if there is an error. This fixes a bug when auto-discovery, that was effectively disabled under pre-locking. sql/sql_binlog.cc: Remove hacks with no_send_ok/no_send_error, they are not necessary anymore: the caller is responsible for network communication. sql/sql_cache.cc: Disable sending of OK/ERROR/EOF packet in the end of dispatch_command if the response has been served from the query cache. This raises the question whether we should store EOF packet in the query cache at all, or generate it anew for each statement (we should generate it anew), but this is to be addressed separately. sql/sql_class.cc: Implement class Diagnostics_area. Please see comments in sql_class.h for details. Fix a subtle coding mistake in select_send::send_data: when on slave, an error in Item::send() was ignored. The problem became visible due to asserts that the diagnostics area is never double assigned. Remove initialization of removed NET members. In select_send::abort() do not call select_send::send_eof(). This is not inheritance-safe. Even if a stored procedure continue handler is found, the current statement is aborted, not succeeded. Instead introduce a Protocol API to send the required response, Protocol::end_partial_result_set(). This simplifies implementation of select_send::send_eof(). No need to add more asserts that there is no error, there is an assert inside Diagnostics_area::set_ok_status() already. Leave no trace of no_send_* in the code. sql/sql_class.h: Declare class Diagnostics_area. Remove the hack with no_send_ok from Substatement_state. Provide inline implementations of send_ok/send_eof. Add commetns. sql/sql_connect.cc: Remove hacks with no_send_error. Since now an error in THD is always set if net->error, it's not necessary to check both net->error and thd->is_error() in the do_command loop. Use thd->main_da.message() instead of net->last_errno. Remove the hack with is_slave_error in sys_init_connect. Since now we do not reset the diagnostics area in net_send_error (it's reset at the beginning of the next statement), we can access it safely even after execute_init_command. sql/sql_db.cc: Update the code to satisfy the invariant that the diagnostics area is never assigned twice. Incidentally, this fixes Bug 29958 "Weird message on DROP DATABASE if mysql.proc does not exist". sql/sql_delete.cc: Change multi-delete to abort in abort(), as per select_send protocol. Fixes the merge error with the test for Bug 29136 sql/sql_derived.cc: Use getters to access error information. sql/sql_insert.cc: Use getters to access error information. sql-common/client.c: Rename last_error to client_last_error, last_errno to client_last_errno. sql/sql_parse.cc: Remove hacks with no_send_error. Deploy net_end_statement(). The story of COM_SHUTDOWN is interesting. Long story short, the server would become on its death's door, and only no_send_ok/no_send_error assigned by send_ok()/net_send_error() would hide its babbling from the client. First of all, COM_QUIT does not require a response. So, the comment saying "Let's send a response to possible COM_QUIT" is not only groundless (even mysqladmin shutdown/mysql_shutdown() doesn't send COM_QUIT after COM_SHUTDOWN), it's plainly incorrect. Secondly, besides this additional 'OK' packet to respond to a hypothetical COM_QUIT, there was the following code in dispatch_command(): if (thd->killed) thd->send_kill_message(); if (thd->is_error() net_send_error(thd); This worked out really funny for the thread through which COM_SHUTDOWN was delivered: we would get COM_SHUTDOWN, say okay, say okay again, kill everybody, get the kill signal ourselves, and then attempt to say "Server shutdown in progress" to the client that is very likely long gone. This all became visible when asserts were added that the Diagnostics_area is not assigned twice. Move query_cache_end_of_result() to the end of dispatch_command(), since net_send_eof() has been moved there. This is safe, query_cache_end_of_result() is a no-op if there is no started query in the cache. Consistently use select_send interface to call abort() or send_eof() depending on the operation result. Remove thd->fatal_error() from reset_master(), it was a no-op. in hacks with no_send_error woudl save us from complete breakage of the client/server protocol. Consistently use select_send::abort() whenever there is an error, and select_send::send_eof() in case of success. The issue became visible due to added asserts. sql/sql_partition.cc: Always set an error in THD whenever there is a call to fatal_error(). sql/sql_prepare.cc: Deploy class Diagnostics_area. Remove the unnecessary juggling with the protocol in Select_fetch_protocol_binary::send_eof(). EOF packet format is protocol-independent. sql/sql_select.cc: Call fatal_error() directly in opt_sum_query. Call my_error() whenever we call thd->fatal_error(). sql/sql_servers.cc: Use getters to access error information in THD. sql/sql_show.cc: Use getters to access error information in THD. Add comments. Call my_error() whenever we call fatal_error(). sql/sql_table.cc: Replace hacks with no_send_ok with the interface of the diagnostics area. Clear the error if ENOENT error in ha_delete_table(). sql/sql_update.cc: Introduce multi_update::abort(), which is the proper way to abort a multi-update. This fixes the merge conflict between this patch and the patch for Bug 29136. sql/table.cc: Use a getter to access error information in THD. sql/tztime.cc: Use a getter to access error information in THD.
600 lines
14 KiB
C++
600 lines
14 KiB
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; 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
#include "mysql_priv.h"
|
|
#ifdef USE_PRAGMA_IMPLEMENTATION
|
|
#pragma implementation
|
|
#endif
|
|
|
|
#if defined(WIN32) || defined(__WIN__)
|
|
#undef SAFEMALLOC /* Problems with threads */
|
|
#endif
|
|
|
|
#include "mysql.h"
|
|
#include "sp_head.h"
|
|
#include "sql_cursor.h"
|
|
#include "sp_rcontext.h"
|
|
#include "sp_pcontext.h"
|
|
|
|
|
|
sp_rcontext::sp_rcontext(sp_pcontext *root_parsing_ctx,
|
|
Field *return_value_fld,
|
|
sp_rcontext *prev_runtime_ctx)
|
|
:m_root_parsing_ctx(root_parsing_ctx),
|
|
m_var_table(0),
|
|
m_var_items(0),
|
|
m_return_value_fld(return_value_fld),
|
|
m_return_value_set(FALSE),
|
|
in_sub_stmt(FALSE),
|
|
m_hcount(0),
|
|
m_hsp(0),
|
|
m_ihsp(0),
|
|
m_hfound(-1),
|
|
m_ccount(0),
|
|
m_case_expr_holders(0),
|
|
m_prev_runtime_ctx(prev_runtime_ctx)
|
|
{
|
|
}
|
|
|
|
|
|
sp_rcontext::~sp_rcontext()
|
|
{
|
|
if (m_var_table)
|
|
free_blobs(m_var_table);
|
|
}
|
|
|
|
|
|
/*
|
|
Initialize sp_rcontext instance.
|
|
|
|
SYNOPSIS
|
|
thd Thread handle
|
|
RETURN
|
|
FALSE on success
|
|
TRUE on error
|
|
*/
|
|
|
|
bool sp_rcontext::init(THD *thd)
|
|
{
|
|
in_sub_stmt= thd->in_sub_stmt;
|
|
|
|
if (init_var_table(thd) || init_var_items())
|
|
return TRUE;
|
|
|
|
return
|
|
!(m_handler=
|
|
(sp_handler_t*)thd->alloc(m_root_parsing_ctx->max_handler_index() *
|
|
sizeof(sp_handler_t))) ||
|
|
!(m_hstack=
|
|
(uint*)thd->alloc(m_root_parsing_ctx->max_handler_index() *
|
|
sizeof(uint))) ||
|
|
!(m_in_handler=
|
|
(uint*)thd->alloc(m_root_parsing_ctx->max_handler_index() *
|
|
sizeof(uint))) ||
|
|
!(m_cstack=
|
|
(sp_cursor**)thd->alloc(m_root_parsing_ctx->max_cursor_index() *
|
|
sizeof(sp_cursor*))) ||
|
|
!(m_case_expr_holders=
|
|
(Item_cache**)thd->calloc(m_root_parsing_ctx->get_num_case_exprs() *
|
|
sizeof (Item_cache*)));
|
|
}
|
|
|
|
|
|
/*
|
|
Create and initialize a table to store SP-vars.
|
|
|
|
SYNOPSIS
|
|
thd Thread handler.
|
|
RETURN
|
|
FALSE on success
|
|
TRUE on error
|
|
*/
|
|
|
|
bool
|
|
sp_rcontext::init_var_table(THD *thd)
|
|
{
|
|
List<Create_field> field_def_lst;
|
|
|
|
if (!m_root_parsing_ctx->max_var_index())
|
|
return FALSE;
|
|
|
|
m_root_parsing_ctx->retrieve_field_definitions(&field_def_lst);
|
|
|
|
DBUG_ASSERT(field_def_lst.elements == m_root_parsing_ctx->max_var_index());
|
|
|
|
if (!(m_var_table= create_virtual_tmp_table(thd, field_def_lst)))
|
|
return TRUE;
|
|
|
|
m_var_table->copy_blobs= TRUE;
|
|
m_var_table->alias= "";
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
/*
|
|
Create and initialize an Item-adapter (Item_field) for each SP-var field.
|
|
|
|
RETURN
|
|
FALSE on success
|
|
TRUE on error
|
|
*/
|
|
|
|
bool
|
|
sp_rcontext::init_var_items()
|
|
{
|
|
uint idx;
|
|
uint num_vars= m_root_parsing_ctx->max_var_index();
|
|
|
|
if (!(m_var_items= (Item**) sql_alloc(num_vars * sizeof (Item *))))
|
|
return TRUE;
|
|
|
|
for (idx = 0; idx < num_vars; ++idx)
|
|
{
|
|
if (!(m_var_items[idx]= new Item_field(m_var_table->field[idx])))
|
|
return TRUE;
|
|
}
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
bool
|
|
sp_rcontext::set_return_value(THD *thd, Item **return_value_item)
|
|
{
|
|
DBUG_ASSERT(m_return_value_fld);
|
|
|
|
m_return_value_set = TRUE;
|
|
|
|
return sp_eval_expr(thd, m_return_value_fld, return_value_item);
|
|
}
|
|
|
|
|
|
#define IS_WARNING_CONDITION(S) ((S)[0] == '0' && (S)[1] == '1')
|
|
#define IS_NOT_FOUND_CONDITION(S) ((S)[0] == '0' && (S)[1] == '2')
|
|
#define IS_EXCEPTION_CONDITION(S) ((S)[0] != '0' || (S)[1] > '2')
|
|
|
|
/*
|
|
Find a handler for the given errno.
|
|
This is called from all error message functions (e.g. push_warning,
|
|
net_send_error, et al) when a sp_rcontext is in effect. If a handler
|
|
is found, no error is sent, and the the SP execution loop will instead
|
|
invoke the found handler.
|
|
This might be called several times before we get back to the execution
|
|
loop, so m_hfound can be >= 0 if a handler has already been found.
|
|
(In which case we don't search again - the first found handler will
|
|
be used.)
|
|
Handlers are pushed on the stack m_handler, with the latest/innermost
|
|
one on the top; we then search for matching handlers from the top and
|
|
down.
|
|
We search through all the handlers, looking for the most specific one
|
|
(sql_errno more specific than sqlstate more specific than the rest).
|
|
Note that mysql error code handlers is a MySQL extension, not part of
|
|
the standard.
|
|
|
|
SYNOPSIS
|
|
sql_errno The error code
|
|
level Warning level
|
|
|
|
RETURN
|
|
1 if a handler was found, m_hfound is set to its index (>= 0)
|
|
0 if not found, m_hfound is -1
|
|
*/
|
|
|
|
bool
|
|
sp_rcontext::find_handler(THD *thd, uint sql_errno,
|
|
MYSQL_ERROR::enum_warning_level level)
|
|
{
|
|
if (m_hfound >= 0)
|
|
return 1; // Already got one
|
|
|
|
const char *sqlstate= mysql_errno_to_sqlstate(sql_errno);
|
|
int i= m_hcount, found= -1;
|
|
|
|
/*
|
|
If this is a fatal sub-statement error, and this runtime
|
|
context corresponds to a sub-statement, no CONTINUE/EXIT
|
|
handlers from this context are applicable: try to locate one
|
|
in the outer scope.
|
|
*/
|
|
if (thd->is_fatal_sub_stmt_error && in_sub_stmt)
|
|
i= 0;
|
|
|
|
/* Search handlers from the latest (innermost) to the oldest (outermost) */
|
|
while (i--)
|
|
{
|
|
sp_cond_type_t *cond= m_handler[i].cond;
|
|
int j= m_ihsp;
|
|
|
|
/* Check active handlers, to avoid invoking one recursively */
|
|
while (j--)
|
|
if (m_in_handler[j] == m_handler[i].handler)
|
|
break;
|
|
if (j >= 0)
|
|
continue; // Already executing this handler
|
|
|
|
switch (cond->type)
|
|
{
|
|
case sp_cond_type_t::number:
|
|
if (sql_errno == cond->mysqlerr &&
|
|
(found < 0 || m_handler[found].cond->type > sp_cond_type_t::number))
|
|
found= i; // Always the most specific
|
|
break;
|
|
case sp_cond_type_t::state:
|
|
if (strcmp(sqlstate, cond->sqlstate) == 0 &&
|
|
(found < 0 || m_handler[found].cond->type > sp_cond_type_t::state))
|
|
found= i;
|
|
break;
|
|
case sp_cond_type_t::warning:
|
|
if ((IS_WARNING_CONDITION(sqlstate) ||
|
|
level == MYSQL_ERROR::WARN_LEVEL_WARN) &&
|
|
found < 0)
|
|
found= i;
|
|
break;
|
|
case sp_cond_type_t::notfound:
|
|
if (IS_NOT_FOUND_CONDITION(sqlstate) && found < 0)
|
|
found= i;
|
|
break;
|
|
case sp_cond_type_t::exception:
|
|
if (IS_EXCEPTION_CONDITION(sqlstate) &&
|
|
level == MYSQL_ERROR::WARN_LEVEL_ERROR &&
|
|
found < 0)
|
|
found= i;
|
|
break;
|
|
}
|
|
}
|
|
if (found < 0)
|
|
{
|
|
/*
|
|
Only "exception conditions" are propagated to handlers in calling
|
|
contexts. If no handler is found locally for a "completion condition"
|
|
(warning or "not found") we will simply resume execution.
|
|
*/
|
|
if (m_prev_runtime_ctx && IS_EXCEPTION_CONDITION(sqlstate) &&
|
|
level == MYSQL_ERROR::WARN_LEVEL_ERROR)
|
|
return m_prev_runtime_ctx->find_handler(thd, sql_errno, level);
|
|
return FALSE;
|
|
}
|
|
m_hfound= found;
|
|
return TRUE;
|
|
}
|
|
|
|
/*
|
|
Handle the error for a given errno.
|
|
The severity of the error is adjusted depending of the current sql_mode.
|
|
If an handler is present for the error (see find_handler()),
|
|
this function will return true.
|
|
If a handler is found and if the severity of the error indicate
|
|
that the current instruction executed should abort,
|
|
the flag thd->net.report_error is also set.
|
|
This will cause the execution of the current instruction in a
|
|
sp_instr* to fail, and give control to the handler code itself
|
|
in the sp_head::execute() loop.
|
|
|
|
SYNOPSIS
|
|
sql_errno The error code
|
|
level Warning level
|
|
thd The current thread
|
|
|
|
RETURN
|
|
TRUE if a handler was found.
|
|
FALSE if no handler was found.
|
|
*/
|
|
bool
|
|
sp_rcontext::handle_error(uint sql_errno,
|
|
MYSQL_ERROR::enum_warning_level level,
|
|
THD *thd)
|
|
{
|
|
MYSQL_ERROR::enum_warning_level elevated_level= level;
|
|
|
|
|
|
/* Depending on the sql_mode of execution,
|
|
warnings may be considered errors */
|
|
if ((level == MYSQL_ERROR::WARN_LEVEL_WARN) &&
|
|
thd->really_abort_on_warning())
|
|
{
|
|
elevated_level= MYSQL_ERROR::WARN_LEVEL_ERROR;
|
|
}
|
|
|
|
return find_handler(thd, sql_errno, elevated_level);
|
|
}
|
|
|
|
void
|
|
sp_rcontext::push_cursor(sp_lex_keeper *lex_keeper, sp_instr_cpush *i)
|
|
{
|
|
m_cstack[m_ccount++]= new sp_cursor(lex_keeper, i);
|
|
}
|
|
|
|
|
|
void
|
|
sp_rcontext::pop_cursors(uint count)
|
|
{
|
|
while (count--)
|
|
{
|
|
delete m_cstack[--m_ccount];
|
|
}
|
|
}
|
|
|
|
|
|
int
|
|
sp_rcontext::set_variable(THD *thd, uint var_idx, Item **value)
|
|
{
|
|
return set_variable(thd, m_var_table->field[var_idx], value);
|
|
}
|
|
|
|
|
|
int
|
|
sp_rcontext::set_variable(THD *thd, Field *field, Item **value)
|
|
{
|
|
if (!value)
|
|
{
|
|
field->set_null();
|
|
return 0;
|
|
}
|
|
|
|
return sp_eval_expr(thd, field, value);
|
|
}
|
|
|
|
|
|
Item *
|
|
sp_rcontext::get_item(uint var_idx)
|
|
{
|
|
return m_var_items[var_idx];
|
|
}
|
|
|
|
|
|
Item **
|
|
sp_rcontext::get_item_addr(uint var_idx)
|
|
{
|
|
return m_var_items + var_idx;
|
|
}
|
|
|
|
|
|
/*
|
|
*
|
|
* sp_cursor
|
|
*
|
|
*/
|
|
|
|
sp_cursor::sp_cursor(sp_lex_keeper *lex_keeper, sp_instr_cpush *i)
|
|
:m_lex_keeper(lex_keeper),
|
|
server_side_cursor(NULL),
|
|
m_i(i)
|
|
{
|
|
/*
|
|
currsor can't be stored in QC, so we should prevent opening QC for
|
|
try to write results which are absent.
|
|
*/
|
|
lex_keeper->disable_query_cache();
|
|
}
|
|
|
|
|
|
/*
|
|
Open an SP cursor
|
|
|
|
SYNOPSIS
|
|
open()
|
|
THD Thread handler
|
|
|
|
|
|
RETURN
|
|
0 in case of success, -1 otherwise
|
|
*/
|
|
|
|
int
|
|
sp_cursor::open(THD *thd)
|
|
{
|
|
if (server_side_cursor)
|
|
{
|
|
my_message(ER_SP_CURSOR_ALREADY_OPEN, ER(ER_SP_CURSOR_ALREADY_OPEN),
|
|
MYF(0));
|
|
return -1;
|
|
}
|
|
if (mysql_open_cursor(thd, (uint) ALWAYS_MATERIALIZED_CURSOR, &result,
|
|
&server_side_cursor))
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
|
|
int
|
|
sp_cursor::close(THD *thd)
|
|
{
|
|
if (! server_side_cursor)
|
|
{
|
|
my_message(ER_SP_CURSOR_NOT_OPEN, ER(ER_SP_CURSOR_NOT_OPEN), MYF(0));
|
|
return -1;
|
|
}
|
|
destroy();
|
|
return 0;
|
|
}
|
|
|
|
|
|
void
|
|
sp_cursor::destroy()
|
|
{
|
|
delete server_side_cursor;
|
|
server_side_cursor= 0;
|
|
}
|
|
|
|
|
|
int
|
|
sp_cursor::fetch(THD *thd, List<struct sp_variable> *vars)
|
|
{
|
|
if (! server_side_cursor)
|
|
{
|
|
my_message(ER_SP_CURSOR_NOT_OPEN, ER(ER_SP_CURSOR_NOT_OPEN), MYF(0));
|
|
return -1;
|
|
}
|
|
if (vars->elements != result.get_field_count())
|
|
{
|
|
my_message(ER_SP_WRONG_NO_OF_FETCH_ARGS,
|
|
ER(ER_SP_WRONG_NO_OF_FETCH_ARGS), MYF(0));
|
|
return -1;
|
|
}
|
|
|
|
result.set_spvar_list(vars);
|
|
|
|
/* Attempt to fetch one row */
|
|
if (server_side_cursor->is_open())
|
|
server_side_cursor->fetch(1);
|
|
|
|
/*
|
|
If the cursor was pointing after the last row, the fetch will
|
|
close it instead of sending any rows.
|
|
*/
|
|
if (! server_side_cursor->is_open())
|
|
{
|
|
my_message(ER_SP_FETCH_NO_DATA, ER(ER_SP_FETCH_NO_DATA), MYF(0));
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
Create an instance of appropriate Item_cache class depending on the
|
|
specified type in the callers arena.
|
|
|
|
SYNOPSIS
|
|
thd thread handler
|
|
result_type type of the expression
|
|
|
|
RETURN
|
|
Pointer to valid object on success
|
|
NULL on error
|
|
|
|
NOTE
|
|
We should create cache items in the callers arena, as they are used
|
|
between in several instructions.
|
|
*/
|
|
|
|
Item_cache *
|
|
sp_rcontext::create_case_expr_holder(THD *thd, const Item *item)
|
|
{
|
|
Item_cache *holder;
|
|
Query_arena current_arena;
|
|
|
|
thd->set_n_backup_active_arena(thd->spcont->callers_arena, ¤t_arena);
|
|
|
|
holder= Item_cache::get_cache(item);
|
|
|
|
thd->restore_active_arena(thd->spcont->callers_arena, ¤t_arena);
|
|
|
|
return holder;
|
|
}
|
|
|
|
|
|
/*
|
|
Set CASE expression to the specified value.
|
|
|
|
SYNOPSIS
|
|
thd thread handler
|
|
case_expr_id identifier of the CASE expression
|
|
case_expr_item a value of the CASE expression
|
|
|
|
RETURN
|
|
FALSE on success
|
|
TRUE on error
|
|
|
|
NOTE
|
|
The idea is to reuse Item_cache for the expression of the one CASE
|
|
statement. This optimization takes place when there is CASE statement
|
|
inside of a loop. So, in other words, we will use the same object on each
|
|
iteration instead of creating a new one for each iteration.
|
|
|
|
TODO
|
|
Hypothetically, a type of CASE expression can be different for each
|
|
iteration. For instance, this can happen if the expression contains a
|
|
session variable (something like @@VAR) and its type is changed from one
|
|
iteration to another.
|
|
|
|
In order to cope with this problem, we check type each time, when we use
|
|
already created object. If the type does not match, we re-create Item.
|
|
This also can (should?) be optimized.
|
|
*/
|
|
|
|
int
|
|
sp_rcontext::set_case_expr(THD *thd, int case_expr_id, Item **case_expr_item_ptr)
|
|
{
|
|
Item *case_expr_item= sp_prepare_func_item(thd, case_expr_item_ptr);
|
|
if (!case_expr_item)
|
|
return TRUE;
|
|
|
|
if (!m_case_expr_holders[case_expr_id] ||
|
|
m_case_expr_holders[case_expr_id]->result_type() !=
|
|
case_expr_item->result_type())
|
|
{
|
|
m_case_expr_holders[case_expr_id]=
|
|
create_case_expr_holder(thd, case_expr_item);
|
|
}
|
|
|
|
m_case_expr_holders[case_expr_id]->store(case_expr_item);
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
|
|
Item *
|
|
sp_rcontext::get_case_expr(int case_expr_id)
|
|
{
|
|
return m_case_expr_holders[case_expr_id];
|
|
}
|
|
|
|
|
|
Item **
|
|
sp_rcontext::get_case_expr_addr(int case_expr_id)
|
|
{
|
|
return (Item**) m_case_expr_holders + case_expr_id;
|
|
}
|
|
|
|
|
|
/***************************************************************************
|
|
Select_fetch_into_spvars
|
|
****************************************************************************/
|
|
|
|
int Select_fetch_into_spvars::prepare(List<Item> &fields, SELECT_LEX_UNIT *u)
|
|
{
|
|
/*
|
|
Cache the number of columns in the result set in order to easily
|
|
return an error if column count does not match value count.
|
|
*/
|
|
field_count= fields.elements;
|
|
return select_result_interceptor::prepare(fields, u);
|
|
}
|
|
|
|
|
|
bool Select_fetch_into_spvars::send_data(List<Item> &items)
|
|
{
|
|
List_iterator_fast<struct sp_variable> spvar_iter(*spvar_list);
|
|
List_iterator_fast<Item> item_iter(items);
|
|
sp_variable_t *spvar;
|
|
Item *item;
|
|
|
|
/* Must be ensured by the caller */
|
|
DBUG_ASSERT(spvar_list->elements == items.elements);
|
|
|
|
/*
|
|
Assign the row fetched from a server side cursor to stored
|
|
procedure variables.
|
|
*/
|
|
for (; spvar= spvar_iter++, item= item_iter++; )
|
|
{
|
|
if (thd->spcont->set_variable(thd, spvar->offset, &item))
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|