Commit 6010662c authored by Alexander Barkov's avatar Alexander Barkov

MDEV-11037 Diagnostics_area refactoring for user defined exceptions

parent ffca1e48
......@@ -36,10 +36,10 @@ bool sp_condition_value::equals(const sp_condition_value *cv) const
switch (type)
{
case sp_condition_value::ERROR_CODE:
return (mysqlerr == cv->mysqlerr);
return (get_sql_errno() == cv->get_sql_errno());
case sp_condition_value::SQLSTATE:
return (strcmp(sql_state, cv->sql_state) == 0);
return Sql_state::eq(cv);
default:
return true;
......@@ -354,8 +354,7 @@ bool sp_pcontext::check_duplicate_handler(
sp_handler*
sp_pcontext::find_handler(const char *sql_state,
uint sql_errno,
sp_pcontext::find_handler(const Sql_state_errno *value,
Sql_condition::enum_warning_level level) const
{
sp_handler *found_handler= NULL;
......@@ -373,7 +372,7 @@ sp_pcontext::find_handler(const char *sql_state,
switch (cv->type)
{
case sp_condition_value::ERROR_CODE:
if (sql_errno == cv->mysqlerr &&
if (value->get_sql_errno() == cv->get_sql_errno() &&
(!found_cv ||
found_cv->type > sp_condition_value::ERROR_CODE))
{
......@@ -383,7 +382,7 @@ sp_pcontext::find_handler(const char *sql_state,
break;
case sp_condition_value::SQLSTATE:
if (strcmp(sql_state, cv->sql_state) == 0 &&
if (cv->Sql_state::eq(value) &&
(!found_cv ||
found_cv->type > sp_condition_value::SQLSTATE))
{
......@@ -393,7 +392,7 @@ sp_pcontext::find_handler(const char *sql_state,
break;
case sp_condition_value::WARNING:
if ((is_sqlstate_warning(sql_state) ||
if ((value->Sql_state::is_warning() ||
level == Sql_condition::WARN_LEVEL_WARN) && !found_cv)
{
found_cv= cv;
......@@ -402,7 +401,7 @@ sp_pcontext::find_handler(const char *sql_state,
break;
case sp_condition_value::NOT_FOUND:
if (is_sqlstate_not_found(sql_state) && !found_cv)
if (value->Sql_state::is_not_found() && !found_cv)
{
found_cv= cv;
found_handler= h;
......@@ -418,7 +417,7 @@ sp_pcontext::find_handler(const char *sql_state,
and it should be caught.
*/
if (((current_thd->variables.sql_mode & MODE_ORACLE) ||
(is_sqlstate_exception(sql_state) &&
(value->Sql_state::is_exception() &&
level == Sql_condition::WARN_LEVEL_ERROR)) && !found_cv)
{
found_cv= cv;
......@@ -467,7 +466,7 @@ sp_pcontext::find_handler(const char *sql_state,
if (!p || !p->m_parent)
return NULL;
return p->m_parent->find_handler(sql_state, sql_errno, level);
return p->m_parent->find_handler(value, level);
}
......
......@@ -127,12 +127,8 @@ class sp_label : public Sql_alloc
/// In some sense, this class is a union -- a set of filled attributes
/// depends on the sp_condition_value::type value.
class sp_condition_value : public Sql_alloc
class sp_condition_value : public Sql_alloc, public Sql_state_errno
{
void init_sql_state()
{
sql_state[0]= '\0';
}
public:
enum enum_type
{
......@@ -146,44 +142,30 @@ class sp_condition_value : public Sql_alloc
/// Type of the condition value.
enum_type type;
/// SQLSTATE of the condition value.
char sql_state[SQLSTATE_LENGTH+1];
/// MySQL error code of the condition value.
uint mysqlerr;
public:
sp_condition_value(uint _mysqlerr)
:Sql_alloc(),
type(ERROR_CODE),
mysqlerr(_mysqlerr)
{ init_sql_state(); }
Sql_state_errno(_mysqlerr),
type(ERROR_CODE)
{ }
sp_condition_value(uint _mysqlerr, const char *_sql_state)
:Sql_alloc(),
type(ERROR_CODE),
mysqlerr(_mysqlerr)
{
memcpy(sql_state, _sql_state, SQLSTATE_LENGTH);
sql_state[SQLSTATE_LENGTH]= 0;
}
Sql_state_errno(_mysqlerr, _sql_state),
type(ERROR_CODE)
{ }
sp_condition_value(const char *_sql_state)
:Sql_alloc(),
type(SQLSTATE),
mysqlerr(0)
{
memcpy(sql_state, _sql_state, SQLSTATE_LENGTH);
sql_state[SQLSTATE_LENGTH]= 0;
}
Sql_state_errno(0, _sql_state),
type(SQLSTATE)
{ }
sp_condition_value(enum_type _type)
:Sql_alloc(),
type(_type),
mysqlerr(0)
type(_type)
{
DBUG_ASSERT(type != ERROR_CODE && type != SQLSTATE);
init_sql_state();
}
/// Check if two instances of sp_condition_value are equal or not.
......@@ -192,8 +174,6 @@ class sp_condition_value : public Sql_alloc
///
/// @return true if the instances are equal, false otherwise.
bool equals(const sp_condition_value *cv) const;
bool has_sql_state() const { return sql_state[0] != '\0'; }
};
///////////////////////////////////////////////////////////////////////////
......@@ -532,13 +512,11 @@ class sp_pcontext : public Sql_alloc
/// Find an SQL handler for the given SQL condition according to the
/// SQL-handler resolution rules. This function is used at runtime.
///
/// @param sql_state The SQL condition state
/// @param sql_errno The error code
/// @param value The error code and the SQL state
/// @param level The SQL condition level
///
/// @return a pointer to the found SQL-handler or NULL.
sp_handler *find_handler(const char *sql_state,
uint sql_errno,
sp_handler *find_handler(const Sql_state_errno *value,
Sql_condition::enum_warning_level level) const;
/////////////////////////////////////////////////////////////////////////
......
......@@ -228,9 +228,7 @@ bool sp_rcontext::handle_sql_condition(THD *thd,
if (thd->is_error())
{
found_handler=
cur_spi->m_ctx->find_handler(da->get_sqlstate(),
da->sql_errno(),
Sql_condition::WARN_LEVEL_ERROR);
cur_spi->m_ctx->find_handler(da, Sql_condition::WARN_LEVEL_ERROR);
if (found_handler)
found_condition= da->get_error_condition();
......@@ -246,9 +244,7 @@ bool sp_rcontext::handle_sql_condition(THD *thd,
{
Sql_condition *condition=
new (callers_arena->mem_root) Sql_condition(callers_arena->mem_root);
condition->set(da->sql_errno(), da->get_sqlstate(),
Sql_condition::WARN_LEVEL_ERROR,
da->message());
condition->set(da, Sql_condition::WARN_LEVEL_ERROR, da->message());
found_condition= condition;
}
}
......@@ -267,9 +263,7 @@ bool sp_rcontext::handle_sql_condition(THD *thd,
c->get_level() == Sql_condition::WARN_LEVEL_NOTE)
{
const sp_handler *handler=
cur_spi->m_ctx->find_handler(c->get_sqlstate(),
c->get_sql_errno(),
c->get_level());
cur_spi->m_ctx->find_handler(c, c->get_level());
if (handler)
{
found_handler= handler;
......
......@@ -120,18 +120,9 @@ class sp_rcontext : public Sql_alloc
/// 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
class Sql_condition_info : public Sql_alloc, public Sql_state_errno_level
{
public:
/// SQL error code.
uint sql_errno;
/// Error level.
Sql_condition::enum_warning_level level;
/// SQLSTATE.
char sql_state[SQLSTATE_LENGTH + 1];
/// Text message.
char *message;
......@@ -141,12 +132,8 @@ class sp_rcontext : public Sql_alloc
/// @param arena Query arena for SP
Sql_condition_info(const Sql_condition *_sql_condition,
Query_arena *arena)
:sql_errno(_sql_condition->get_sql_errno()),
level(_sql_condition->get_level())
:Sql_state_errno_level(*_sql_condition)
{
memcpy(sql_state, _sql_condition->get_sqlstate(), SQLSTATE_LENGTH);
sql_state[SQLSTATE_LENGTH]= '\0';
message= strdup_root(arena->mem_root, _sql_condition->get_message_text());
}
};
......
......@@ -185,11 +185,8 @@ Sql_condition::Sql_condition()
m_column_name((const char*) NULL, 0, & my_charset_utf8_bin),
m_cursor_name((const char*) NULL, 0, & my_charset_utf8_bin),
m_message_text(),
m_sql_errno(0),
m_level(Sql_condition::WARN_LEVEL_ERROR),
m_mem_root(NULL)
{
memset(m_returned_sqlstate, 0, sizeof(m_returned_sqlstate));
}
void Sql_condition::init(MEM_ROOT *mem_root)
......@@ -229,12 +226,9 @@ Sql_condition::Sql_condition(MEM_ROOT *mem_root)
m_column_name((const char*) NULL, 0, & my_charset_utf8_bin),
m_cursor_name((const char*) NULL, 0, & my_charset_utf8_bin),
m_message_text(),
m_sql_errno(0),
m_level(Sql_condition::WARN_LEVEL_ERROR),
m_mem_root(mem_root)
{
DBUG_ASSERT(mem_root != NULL);
memset(m_returned_sqlstate, 0, sizeof(m_returned_sqlstate));
}
static void copy_string(MEM_ROOT *mem_root, String* dst, const String* src)
......@@ -270,21 +264,6 @@ Sql_condition::copy_opt_attributes(const Sql_condition *cond)
copy_string(m_mem_root, & m_cursor_name, & cond->m_cursor_name);
}
void
Sql_condition::set(uint sql_errno, const char* sqlstate,
Sql_condition::enum_warning_level level, const char* msg)
{
DBUG_ASSERT(sql_errno != 0);
DBUG_ASSERT(sqlstate != NULL);
DBUG_ASSERT(msg != NULL);
m_sql_errno= sql_errno;
memcpy(m_returned_sqlstate, sqlstate, SQLSTATE_LENGTH);
m_returned_sqlstate[SQLSTATE_LENGTH]= '\0';
set_builtin_message_text(msg);
m_level= level;
}
void
Sql_condition::set_builtin_message_text(const char* str)
......@@ -312,13 +291,46 @@ Sql_condition::get_message_octet_length() const
return m_message_text.length();
}
void
Sql_condition::set_sqlstate(const char* sqlstate)
void Sql_state_errno_level::assign_defaults(const Sql_state_errno *from)
{
DBUG_ASSERT(from);
int sqlerrno= from->get_sql_errno();
/*
SIGNAL is restricted in sql_yacc.yy to only signal SQLSTATE conditions.
*/
DBUG_ASSERT(from->has_sql_state());
set_sqlstate(from);
/* SQLSTATE class "00": illegal, rejected in the parser. */
DBUG_ASSERT(m_sqlstate[0] != '0' || get_sqlstate()[1] != '0');
if (Sql_state::is_warning()) /* SQLSTATE class "01": warning. */
{
m_level= Sql_condition::WARN_LEVEL_WARN;
m_sql_errno= sqlerrno ? sqlerrno : ER_SIGNAL_WARN;
}
else if (Sql_state::is_not_found()) /* SQLSTATE class "02": not found. */
{
m_level= Sql_condition::WARN_LEVEL_ERROR;
m_sql_errno= sqlerrno ? sqlerrno : ER_SIGNAL_NOT_FOUND;
}
else /* other SQLSTATE classes : error. */
{
m_level= Sql_condition::WARN_LEVEL_ERROR;
m_sql_errno= sqlerrno ? sqlerrno : ER_SIGNAL_EXCEPTION;
}
}
void Sql_condition::assign_defaults(THD *thd, const Sql_state_errno *from)
{
memcpy(m_returned_sqlstate, sqlstate, SQLSTATE_LENGTH);
m_returned_sqlstate[SQLSTATE_LENGTH]= '\0';
if (from)
Sql_state_errno_level::assign_defaults(from);
if (!get_message_text())
set_builtin_message_text(ER(get_sql_errno()));
}
Diagnostics_area::Diagnostics_area(bool initialize)
: is_bulk_execution(0), m_main_wi(0, false, initialize)
{
......@@ -501,9 +513,7 @@ Diagnostics_area::set_error_status(uint sql_errno,
return;
#endif
m_sql_errno= sql_errno;
memcpy(m_sqlstate, sqlstate, SQLSTATE_LENGTH);
m_sqlstate[SQLSTATE_LENGTH]= '\0';
set_condition_value(sql_errno, sqlstate);
strmake_buf(m_message, message);
get_warning_info()->set_error_condition(error_condition);
......@@ -695,8 +705,7 @@ void Warning_info::reserve_space(THD *thd, uint count)
}
Sql_condition *Warning_info::push_warning(THD *thd,
uint sql_errno, const char* sqlstate,
Sql_condition::enum_warning_level level,
const Sql_state_errno_level *value,
const char *msg)
{
Sql_condition *cond= NULL;
......@@ -709,11 +718,11 @@ Sql_condition *Warning_info::push_warning(THD *thd,
cond= new (& m_warn_root) Sql_condition(& m_warn_root);
if (cond)
{
cond->set(sql_errno, sqlstate, level, msg);
cond->set(value, msg);
m_warn_list.push_back(cond);
}
}
m_warn_count[(uint) level]++;
m_warn_count[(uint) value->get_level()]++;
}
m_current_statement_warn_count++;
......@@ -721,12 +730,10 @@ Sql_condition *Warning_info::push_warning(THD *thd,
}
Sql_condition *Warning_info::push_warning(THD *thd, const Sql_condition *sql_condition)
Sql_condition *Warning_info::push_warning(THD *thd,
const Sql_condition *sql_condition)
{
Sql_condition *new_condition= push_warning(thd,
sql_condition->get_sql_errno(),
sql_condition->get_sqlstate(),
sql_condition->get_level(),
Sql_condition *new_condition= push_warning(thd, sql_condition,
sql_condition->get_message_text());
if (new_condition)
......
This diff is collapsed.
......@@ -75,69 +75,6 @@ void Set_signal_information::clear()
memset(m_item, 0, sizeof(m_item));
}
void
Sql_cmd_common_signal::assign_defaults(Sql_condition *cond,
bool set_level_code,
Sql_condition::enum_warning_level level,
int sqlcode)
{
if (set_level_code)
{
cond->m_level= level;
cond->m_sql_errno= sqlcode;
}
if (! cond->get_message_text())
cond->set_builtin_message_text(ER(sqlcode));
}
void Sql_cmd_common_signal::eval_defaults(THD *thd, Sql_condition *cond)
{
DBUG_ASSERT(cond);
const char* sqlstate;
bool set_defaults= (m_cond != 0);
if (set_defaults)
{
/*
SIGNAL is restricted in sql_yacc.yy to only signal SQLSTATE conditions.
*/
DBUG_ASSERT(m_cond->has_sql_state());
sqlstate= m_cond->sql_state;
cond->set_sqlstate(sqlstate);
}
else
sqlstate= cond->get_sqlstate();
DBUG_ASSERT(sqlstate);
/* SQLSTATE class "00": illegal, rejected in the parser. */
DBUG_ASSERT((sqlstate[0] != '0') || (sqlstate[1] != '0'));
if ((sqlstate[0] == '0') && (sqlstate[1] == '1'))
{
/* SQLSTATE class "01": warning. */
assign_defaults(cond, set_defaults,
Sql_condition::WARN_LEVEL_WARN,
m_cond && m_cond->mysqlerr ? m_cond->mysqlerr :
ER_SIGNAL_WARN);
}
else if ((sqlstate[0] == '0') && (sqlstate[1] == '2'))
{
/* SQLSTATE class "02": not found. */
assign_defaults(cond, set_defaults,
Sql_condition::WARN_LEVEL_ERROR,
m_cond && m_cond->mysqlerr ? m_cond->mysqlerr :
ER_SIGNAL_NOT_FOUND);
}
else
{
/* other SQLSTATE classes : error. */
assign_defaults(cond, set_defaults,
Sql_condition::WARN_LEVEL_ERROR,
m_cond && m_cond->mysqlerr ? m_cond->mysqlerr :
ER_SIGNAL_EXCEPTION);
}
}
static bool assign_fixed_string(MEM_ROOT *mem_root,
CHARSET_INFO *dst_cs,
......@@ -408,7 +345,7 @@ bool Sql_cmd_common_signal::raise_condition(THD *thd, Sql_condition *cond)
DBUG_ASSERT(thd->lex->query_tables == NULL);
eval_defaults(thd, cond);
cond->assign_defaults(thd, m_cond);
if (eval_signal_informations(thd, cond))
DBUG_RETURN(result);
......@@ -491,10 +428,7 @@ bool Sql_cmd_resignal::execute(THD *thd)
}
Sql_condition signaled_err(thd->mem_root);
signaled_err.set(signaled->sql_errno,
signaled->sql_state,
signaled->level,
signaled->message);
signaled_err.set(signaled, signaled->message);
if (m_cond)
{
......
......@@ -39,27 +39,6 @@ class Sql_cmd_common_signal : public Sql_cmd
virtual ~Sql_cmd_common_signal()
{}
/**
Assign the condition items 'MYSQL_ERRNO', 'level' and 'MESSAGE_TEXT'
default values of a condition.
@param cond the condition to update.
@param set_level_code true if 'level' and 'MYSQL_ERRNO' needs to be overwritten
@param level the level to assign
@param sqlcode the sql code to assign
*/
static void assign_defaults(Sql_condition *cond,
bool set_level_code,
Sql_condition::enum_warning_level level,
int sqlcode);
/**
Evaluate the condition items 'SQLSTATE', 'MYSQL_ERRNO', 'level' and 'MESSAGE_TEXT'
default values for this statement.
@param thd the current thread.
@param cond the condition to update.
*/
void eval_defaults(THD *thd, Sql_condition *cond);
/**
Evaluate each signal condition items for this statement.
@param thd the current thread.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment