18 #include "sp_pcontext.h"
19 #include "sp_rcontext.h"
20 #include "sql_signal.h"
28 #define MAX_MYSQL_ERRNO UINT_MAX16
32 { C_STRING_WITH_LEN(
"CLASS_ORIGIN") },
33 { C_STRING_WITH_LEN(
"SUBCLASS_ORIGIN") },
34 { C_STRING_WITH_LEN(
"CONSTRAINT_CATALOG") },
35 { C_STRING_WITH_LEN(
"CONSTRAINT_SCHEMA") },
36 { C_STRING_WITH_LEN(
"CONSTRAINT_NAME") },
37 { C_STRING_WITH_LEN(
"CATALOG_NAME") },
38 { C_STRING_WITH_LEN(
"SCHEMA_NAME") },
39 { C_STRING_WITH_LEN(
"TABLE_NAME") },
40 { C_STRING_WITH_LEN(
"COLUMN_NAME") },
41 { C_STRING_WITH_LEN(
"CURSOR_NAME") },
42 { C_STRING_WITH_LEN(
"MESSAGE_TEXT") },
43 { C_STRING_WITH_LEN(
"MYSQL_ERRNO") },
45 { C_STRING_WITH_LEN(
"CONDITION_IDENTIFIER") },
46 { C_STRING_WITH_LEN(
"CONDITION_NUMBER") },
47 { C_STRING_WITH_LEN(
"CONNECTION_NAME") },
48 { C_STRING_WITH_LEN(
"MESSAGE_LENGTH") },
49 { C_STRING_WITH_LEN(
"MESSAGE_OCTET_LENGTH") },
50 { C_STRING_WITH_LEN(
"PARAMETER_MODE") },
51 { C_STRING_WITH_LEN(
"PARAMETER_NAME") },
52 { C_STRING_WITH_LEN(
"PARAMETER_ORDINAL_POSITION") },
53 { C_STRING_WITH_LEN(
"RETURNED_SQLSTATE") },
54 { C_STRING_WITH_LEN(
"ROUTINE_CATALOG") },
55 { C_STRING_WITH_LEN(
"ROUTINE_NAME") },
56 { C_STRING_WITH_LEN(
"ROUTINE_SCHEMA") },
57 { C_STRING_WITH_LEN(
"SERVER_NAME") },
58 { C_STRING_WITH_LEN(
"SPECIFIC_NAME") },
59 { C_STRING_WITH_LEN(
"TRIGGER_CATALOG") },
60 { C_STRING_WITH_LEN(
"TRIGGER_NAME") },
61 { C_STRING_WITH_LEN(
"TRIGGER_SCHEMA") }
66 { C_STRING_WITH_LEN(
"NUMBER") },
67 { C_STRING_WITH_LEN(
"MORE") },
68 { C_STRING_WITH_LEN(
"COMMAND_FUNCTION") },
69 { C_STRING_WITH_LEN(
"COMMAND_FUNCTION_CODE") },
70 { C_STRING_WITH_LEN(
"DYNAMIC_FUNCTION") },
71 { C_STRING_WITH_LEN(
"DYNAMIC_FUNCTION_CODE") },
72 { C_STRING_WITH_LEN(
"ROW_COUNT") },
73 { C_STRING_WITH_LEN(
"TRANSACTIONS_COMMITTED") },
74 { C_STRING_WITH_LEN(
"TRANSACTIONS_ROLLED_BACK") },
75 { C_STRING_WITH_LEN(
"TRANSACTION_ACTIVE") }
79 Set_signal_information::Set_signal_information(
80 const Set_signal_information&
set)
82 memcpy(m_item,
set.m_item,
sizeof(m_item));
85 void Set_signal_information::clear()
87 memset(m_item, 0,
sizeof(m_item));
93 Sql_condition::enum_warning_level
level,
99 cond->m_sql_errno= sqlcode;
102 cond->set_builtin_message_text(ER(sqlcode));
109 const char* sqlstate;
110 bool set_defaults= (
m_cond != 0);
117 DBUG_ASSERT(
m_cond->
type == sp_condition_value::SQLSTATE);
119 cond->set_sqlstate(sqlstate);
124 DBUG_ASSERT(sqlstate);
126 DBUG_ASSERT((sqlstate[0] !=
'0') || (sqlstate[1] !=
'0'));
128 if ((sqlstate[0] ==
'0') && (sqlstate[1] ==
'1'))
132 Sql_condition::WARN_LEVEL_WARN, ER_SIGNAL_WARN);
134 else if ((sqlstate[0] ==
'0') && (sqlstate[1] ==
'2'))
138 Sql_condition::WARN_LEVEL_ERROR, ER_SIGNAL_NOT_FOUND);
144 Sql_condition::WARN_LEVEL_ERROR, ER_SIGNAL_EXCEPTION);
148 static bool assign_fixed_string(
MEM_ROOT *mem_root,
169 dst->set((
const char*) NULL, 0, dst_cs);
173 src_cs= src->charset();
174 src_len= src->length();
175 src_end= src_str + src_len;
176 numchars= src_cs->cset->numchars(src_cs, src_str, src_end);
178 if (numchars <= max_char)
180 to_copy= src->length();
186 to_copy= dst_cs->cset->charpos(dst_cs, src_str, src_end, numchars);
190 if (String::needs_conversion(to_copy, src_cs, dst_cs, & dummy_offset))
192 dst_len= numchars * dst_cs->mbmaxlen;
193 dst_str= (
char*) alloc_root(mem_root, dst_len + 1);
196 const char* well_formed_error_pos;
197 const char* cannot_convert_error_pos;
198 const char* from_end_pos;
200 dst_copied= well_formed_copy_nchars(dst_cs, dst_str, dst_len,
201 src_cs, src_str, src_len,
203 & well_formed_error_pos,
204 & cannot_convert_error_pos,
206 DBUG_ASSERT(dst_copied <= dst_len);
208 dst_str[dst_copied]=
'\0';
214 dst_str= (
char*) alloc_root(mem_root, dst_len + 1);
217 memcpy(dst_str, src_str, to_copy);
218 dst_str[to_copy]=
'\0';
221 dst->set(dst_str, dst_len, dst_cs);
226 static int assign_condition_item(
MEM_ROOT *mem_root,
const char*
name, THD *thd,
229 char str_buff[(64+1)*4];
230 String str_value(str_buff,
sizeof(str_buff), & my_charset_utf8_bin);
234 DBUG_ENTER(
"assign_condition_item");
238 thd->raise_error_printf(ER_WRONG_VALUE_FOR_VAR, name,
"NULL");
242 str=
set->val_str(& str_value);
243 truncated= assign_fixed_string(mem_root, & my_charset_utf8_bin, 64, ci, str);
246 if (thd->is_strict_mode())
248 thd->raise_error_printf(ER_COND_ITEM_TOO_LONG, name);
252 thd->raise_warning_printf(WARN_COND_ITEM_TRUNCATED, name);
263 enum enum_diag_condition_item_name m_item;
267 static cond_item_map map[]=
269 { DIAG_CLASS_ORIGIN, & Sql_condition::m_class_origin },
270 { DIAG_SUBCLASS_ORIGIN, & Sql_condition::m_subclass_origin },
271 { DIAG_CONSTRAINT_CATALOG, & Sql_condition::m_constraint_catalog },
272 { DIAG_CONSTRAINT_SCHEMA, & Sql_condition::m_constraint_schema },
273 { DIAG_CONSTRAINT_NAME, & Sql_condition::m_constraint_name },
274 { DIAG_CATALOG_NAME, & Sql_condition::m_catalog_name },
275 { DIAG_SCHEMA_NAME, & Sql_condition::m_schema_name },
276 { DIAG_TABLE_NAME, & Sql_condition::m_table_name },
277 { DIAG_COLUMN_NAME, & Sql_condition::m_column_name },
278 { DIAG_CURSOR_NAME, & Sql_condition::m_cursor_name }
287 enum enum_diag_condition_item_name item_enum;
291 DBUG_ENTER(
"Sql_cmd_common_signal::eval_signal_informations");
293 for (i= FIRST_DIAG_SET_PROPERTY;
294 i <= LAST_DIAG_SET_PROPERTY;
302 if (set->fix_fields(thd, &
set))
313 for (j= 0; j < array_elements(map); j++)
315 item_enum= map[j].m_item;
319 member= & (cond->* map[j].m_member);
320 name= & Diag_condition_item_names[item_enum];
321 if (assign_condition_item(cond->m_mem_root, name->str, thd,
set, member))
335 thd->raise_error_printf(ER_WRONG_VALUE_FOR_VAR,
336 "MESSAGE_TEXT",
"NULL");
345 str=
set->val_str(& str_value);
346 truncated= assign_fixed_string(thd->mem_root, & my_charset_utf8_bin, 128,
350 if (thd->is_strict_mode())
352 thd->raise_error_printf(ER_COND_ITEM_TOO_LONG,
357 thd->raise_warning_printf(WARN_COND_ITEM_TRUNCATED,
367 converted_text.set_charset(error_message_charset_info);
368 converted_text.append(utf8_text.ptr(), utf8_text.length(),
369 utf8_text.charset());
370 cond->set_builtin_message_text(converted_text.c_ptr_safe());
378 thd->raise_error_printf(ER_WRONG_VALUE_FOR_VAR,
379 "MYSQL_ERRNO",
"NULL");
382 longlong
code=
set->val_int();
383 if ((code <= 0) || (code > MAX_MYSQL_ERRNO))
385 str=
set->val_str(& str_value);
386 thd->raise_error_printf(ER_WRONG_VALUE_FOR_VAR,
387 "MYSQL_ERRNO", str->c_ptr_safe());
390 cond->m_sql_errno= (int) code;
397 if (! thd->is_error())
401 for (i= FIRST_DIAG_SET_PROPERTY;
402 i <= LAST_DIAG_SET_PROPERTY;
420 DBUG_ENTER(
"Sql_cmd_common_signal::raise_condition");
422 DBUG_ASSERT(thd->lex->query_tables == NULL);
429 DBUG_ASSERT((cond->m_level == Sql_condition::WARN_LEVEL_WARN) ||
430 (cond->m_level == Sql_condition::WARN_LEVEL_ERROR));
438 raised->copy_opt_attributes(cond);
440 if (cond->m_level == Sql_condition::WARN_LEVEL_WARN)
454 DBUG_ENTER(
"Sql_cmd_signal::execute");
468 thd->get_stmt_da()->reset_diagnostics_area();
469 thd->set_row_count_func(0);
470 thd->get_stmt_da()->clear_warning_info(thd->query_id);
493 DBUG_ENTER(
"Sql_cmd_resignal::execute");
497 da->set_warning_info_id(thd->query_id);
499 if (! thd->sp_runtime_ctx ||
500 ! (signaled= thd->sp_runtime_ctx->raised_condition()))
502 thd->raise_error(ER_RESIGNAL_WITHOUT_ACTIVE_HANDLER);
515 query_cache_abort(&thd->query_cache_tls);
518 da->unmark_sql_conditions_from_removal();
521 if (da->has_sql_condition(signaled->
message, strlen(signaled->
message)))
524 da->reserve_space(thd, 1);
529 da->reserve_space(thd, 2);
531 da->push_warning(thd, &signaled_err);