21 #include "sql_parse.h"
27 #include "sql_table.h"
39 const char *db, ulong dblen,
40 const char *
name, ulong namelen,
41 const char *params, ulong paramslen,
42 const char *returns, ulong returnslen,
43 const char *body, ulong bodylen,
44 st_sp_chistics *chistics,
51 sql_mode_t sql_mode,
const char *params,
const char *returns,
52 const char *body, st_sp_chistics &chistics,
53 const char *definer, longlong created, longlong modified,
60 { C_STRING_WITH_LEN(
"db") },
61 { C_STRING_WITH_LEN(
"char(64)") },
62 { C_STRING_WITH_LEN(
"utf8") }
65 { C_STRING_WITH_LEN(
"name") },
66 { C_STRING_WITH_LEN(
"char(64)") },
67 { C_STRING_WITH_LEN(
"utf8") }
70 { C_STRING_WITH_LEN(
"type") },
71 { C_STRING_WITH_LEN(
"enum('FUNCTION','PROCEDURE')") },
75 { C_STRING_WITH_LEN(
"specific_name") },
76 { C_STRING_WITH_LEN(
"char(64)") },
77 { C_STRING_WITH_LEN(
"utf8") }
80 { C_STRING_WITH_LEN(
"language") },
81 { C_STRING_WITH_LEN(
"enum('SQL')") },
85 { C_STRING_WITH_LEN(
"sql_data_access") },
86 { C_STRING_WITH_LEN(
"enum('CONTAINS_SQL','NO_SQL','READS_SQL_DATA','MODIFIES_SQL_DATA')") },
90 { C_STRING_WITH_LEN(
"is_deterministic") },
91 { C_STRING_WITH_LEN(
"enum('YES','NO')") },
95 { C_STRING_WITH_LEN(
"security_type") },
96 { C_STRING_WITH_LEN(
"enum('INVOKER','DEFINER')") },
100 { C_STRING_WITH_LEN(
"param_list") },
101 { C_STRING_WITH_LEN(
"blob") },
106 { C_STRING_WITH_LEN(
"returns") },
107 { C_STRING_WITH_LEN(
"longblob") },
111 { C_STRING_WITH_LEN(
"body") },
112 { C_STRING_WITH_LEN(
"longblob") },
116 { C_STRING_WITH_LEN(
"definer") },
117 { C_STRING_WITH_LEN(
"char(77)") },
118 { C_STRING_WITH_LEN(
"utf8") }
121 { C_STRING_WITH_LEN(
"created") },
122 { C_STRING_WITH_LEN(
"timestamp") },
126 { C_STRING_WITH_LEN(
"modified") },
127 { C_STRING_WITH_LEN(
"timestamp") },
131 { C_STRING_WITH_LEN(
"sql_mode") },
132 { C_STRING_WITH_LEN(
"set('REAL_AS_FLOAT','PIPES_AS_CONCAT','ANSI_QUOTES',"
133 "'IGNORE_SPACE','NOT_USED','ONLY_FULL_GROUP_BY','NO_UNSIGNED_SUBTRACTION',"
134 "'NO_DIR_IN_CREATE','POSTGRESQL','ORACLE','MSSQL','DB2','MAXDB',"
135 "'NO_KEY_OPTIONS','NO_TABLE_OPTIONS','NO_FIELD_OPTIONS','MYSQL323','MYSQL40',"
136 "'ANSI','NO_AUTO_VALUE_ON_ZERO','NO_BACKSLASH_ESCAPES','STRICT_TRANS_TABLES',"
137 "'STRICT_ALL_TABLES','NO_ZERO_IN_DATE','NO_ZERO_DATE','INVALID_DATES',"
138 "'ERROR_FOR_DIVISION_BY_ZERO','TRADITIONAL','NO_AUTO_CREATE_USER',"
139 "'HIGH_NOT_PRECEDENCE','NO_ENGINE_SUBSTITUTION','PAD_CHAR_TO_FULL_LENGTH')") },
143 { C_STRING_WITH_LEN(
"comment") },
144 { C_STRING_WITH_LEN(
"text") },
145 { C_STRING_WITH_LEN(
"utf8") }
148 { C_STRING_WITH_LEN(
"character_set_client") },
149 { C_STRING_WITH_LEN(
"char(32)") },
150 { C_STRING_WITH_LEN(
"utf8") }
153 { C_STRING_WITH_LEN(
"collation_connection") },
154 { C_STRING_WITH_LEN(
"char(32)") },
155 { C_STRING_WITH_LEN(
"utf8") }
158 { C_STRING_WITH_LEN(
"db_collation") },
159 { C_STRING_WITH_LEN(
"char(32)") },
160 { C_STRING_WITH_LEN(
"utf8") }
163 { C_STRING_WITH_LEN(
"body_utf8") },
164 { C_STRING_WITH_LEN(
"longblob") },
170 proc_table_def= {MYSQL_PROC_FIELD_COUNT, proc_table_fields};
197 DBUG_ENTER(
"Stored_routine_creation_ctx::create_backup_ctx");
217 bool load_charset(
MEM_ROOT *mem_root,
224 if (get_field(mem_root, field, &cs_name))
230 *cs= get_charset_by_csname(cs_name.c_ptr(), MY_CS_PRIMARY, MYF(0));
243 bool load_collation(
MEM_ROOT *mem_root,
250 if (get_field(mem_root, field, &cl_name))
256 *cl= get_charset_by_name(cl_name.c_ptr(), MYF(0));
270 Stored_routine_creation_ctx::load_from_db(THD *thd,
280 const char *db_name= thd->strmake(name->m_db.str, name->m_db.length);
281 const char *sr_name= thd->strmake(name->m_name.str, name->m_name.length);
283 bool invalid_creation_ctx= FALSE;
285 if (load_charset(thd->mem_root,
286 proc_tbl->field[MYSQL_PROC_FIELD_CHARACTER_SET_CLIENT],
287 thd->variables.character_set_client,
290 sql_print_warning(
"Stored routine '%s'.'%s': invalid value "
291 "in column mysql.proc.character_set_client.",
292 (
const char *) db_name,
293 (
const char *) sr_name);
295 invalid_creation_ctx= TRUE;
298 if (load_collation(thd->mem_root,
299 proc_tbl->field[MYSQL_PROC_FIELD_COLLATION_CONNECTION],
300 thd->variables.collation_connection,
303 sql_print_warning(
"Stored routine '%s'.'%s': invalid value "
304 "in column mysql.proc.collation_connection.",
305 (
const char *) db_name,
306 (
const char *) sr_name);
308 invalid_creation_ctx= TRUE;
311 if (load_collation(thd->mem_root,
312 proc_tbl->field[MYSQL_PROC_FIELD_DB_COLLATION],
316 sql_print_warning(
"Stored routine '%s'.'%s': invalid value "
317 "in column mysql.proc.db_collation.",
318 (
const char *) db_name,
319 (
const char *) sr_name);
321 invalid_creation_ctx= TRUE;
324 if (invalid_creation_ctx)
326 push_warning_printf(thd,
327 Sql_condition::WARN_LEVEL_WARN,
328 ER_SR_INVALID_CREATION_CTX,
329 ER(ER_SR_INVALID_CREATION_CTX),
330 (
const char *) db_name,
331 (
const char *) sr_name);
340 db_cl= get_default_db_collation(thd, name->m_db.str);
373 my_vsnprintf(buf,
sizeof(buf), fmt, args);
377 my_message(code, buf, MYF(0));
379 my_error(ER_CANNOT_LOAD_FROM_TABLE_V2, MYF(0),
"mysql",
"proc");
384 sql_print_error(
"%s", buf);
407 TABLE *open_proc_table_for_read(THD *thd, Open_tables_backup *backup)
411 DBUG_ENTER(
"open_proc_table_for_read");
415 if (open_system_tables_for_read(thd, &table, backup))
418 if (!table.table->key_info)
420 my_error(ER_TABLE_CORRUPT, MYF(0), table.table->s->db.str,
421 table.table->s->table_name.str);
425 if (!proc_table_intact.
check(table.table, &proc_table_def))
426 DBUG_RETURN(table.table);
429 close_system_tables(thd, backup);
448 static TABLE *open_proc_table_for_update(THD *thd)
452 MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
453 DBUG_ENTER(
"open_proc_table_for_update");
455 table_list.
init_one_table(
"mysql", 5,
"proc", 4,
"proc", TL_WRITE);
457 if (!(table= open_system_table_for_update(thd, &table_list)))
460 if (!proc_table_intact.
check(table, &proc_table_def))
463 close_thread_tables(thd);
464 thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
480 static void recursion_level_error(THD *thd,
sp_head *sp)
482 if (sp->
m_type == SP_TYPE_PROCEDURE)
484 my_error(ER_SP_RECURSION_LIMIT, MYF(0),
485 static_cast<int>(thd->variables.max_sp_recursion_depth),
489 my_error(ER_SP_NO_RECURSION, MYF(0));
508 db_find_routine_aux(THD *thd, enum_sp_type
type,
sp_name *name,
TABLE *table)
510 uchar key[MAX_KEY_LENGTH];
511 DBUG_ENTER(
"db_find_routine_aux");
512 DBUG_PRINT(
"enter", (
"type: %d name: %.*s",
513 type, (
int) name->m_name.length, name->m_name.str));
522 if (name->m_name.length > table->field[1]->field_length)
523 DBUG_RETURN(SP_KEY_NOT_FOUND);
524 table->field[0]->store(name->m_db.str, name->m_db.length, &my_charset_bin);
525 table->field[1]->store(name->m_name.str, name->m_name.length,
527 table->field[2]->store((longlong) type, TRUE);
528 key_copy(key, table->record[0], table->key_info,
533 DBUG_RETURN(SP_KEY_NOT_FOUND);
560 db_find_routine(THD *thd, enum_sp_type type,
sp_name *name,
sp_head **sphp)
563 const char *params, *returns, *body;
568 st_sp_chistics chistics;
572 String str(buff,
sizeof(buff), &my_charset_bin);
573 bool saved_time_zone_used= thd->time_zone_used;
574 sql_mode_t sql_mode, saved_mode= thd->variables.sql_mode;
575 Open_tables_backup open_tables_state_backup;
578 DBUG_ENTER(
"db_find_routine");
579 DBUG_PRINT(
"enter", (
"type: %d name: %.*s",
580 type, (
int) name->m_name.length, name->m_name.str));
583 if (!(table= open_proc_table_for_read(thd, &open_tables_state_backup)))
584 DBUG_RETURN(SP_OPEN_TABLE_FAILED);
587 thd->variables.sql_mode= 0;
589 if ((ret= db_find_routine_aux(thd, type, name, table)) != SP_OK)
592 if (table->s->fields < MYSQL_PROC_FIELD_COUNT)
594 ret= SP_GET_FIELD_FAILED;
598 memset(&chistics, 0,
sizeof(chistics));
599 if ((ptr= get_field(thd->mem_root,
600 table->field[MYSQL_PROC_FIELD_ACCESS])) == NULL)
602 ret= SP_GET_FIELD_FAILED;
607 chistics.daccess= SP_NO_SQL;
610 chistics.daccess= SP_CONTAINS_SQL;
613 chistics.daccess= SP_READS_SQL_DATA;
616 chistics.daccess= SP_MODIFIES_SQL_DATA;
619 chistics.daccess= SP_DEFAULT_ACCESS_MAPPING;
622 if ((ptr= get_field(thd->mem_root,
623 table->field[MYSQL_PROC_FIELD_DETERMINISTIC])) == NULL)
625 ret= SP_GET_FIELD_FAILED;
628 chistics.detistic= (ptr[0] ==
'N' ? FALSE : TRUE);
630 if ((ptr= get_field(thd->mem_root,
631 table->field[MYSQL_PROC_FIELD_SECURITY_TYPE])) == NULL)
633 ret= SP_GET_FIELD_FAILED;
636 chistics.suid= (ptr[0] ==
'I' ? SP_IS_NOT_SUID : SP_IS_SUID);
638 if ((params= get_field(thd->mem_root,
639 table->field[MYSQL_PROC_FIELD_PARAM_LIST])) == NULL)
644 if (type == SP_TYPE_PROCEDURE)
646 else if ((returns= get_field(thd->mem_root,
647 table->field[MYSQL_PROC_FIELD_RETURNS])) == NULL)
649 ret= SP_GET_FIELD_FAILED;
653 if ((body= get_field(thd->mem_root,
654 table->field[MYSQL_PROC_FIELD_BODY])) == NULL)
656 ret= SP_GET_FIELD_FAILED;
661 if ((definer= get_field(thd->mem_root,
662 table->field[MYSQL_PROC_FIELD_DEFINER])) == NULL)
664 ret= SP_GET_FIELD_FAILED;
668 modified= table->field[MYSQL_PROC_FIELD_MODIFIED]->val_int();
669 created= table->field[MYSQL_PROC_FIELD_CREATED]->val_int();
671 sql_mode= (sql_mode_t) table->field[MYSQL_PROC_FIELD_SQL_MODE]->val_int();
673 table->field[MYSQL_PROC_FIELD_COMMENT]->val_str(&str, &str);
676 if ((length= str.length()))
677 ptr= thd->strmake(str.ptr(), length);
678 chistics.comment.str= ptr;
679 chistics.comment.length= length;
681 creation_ctx= Stored_routine_creation_ctx::load_from_db(thd, name, table);
683 close_system_tables(thd, &open_tables_state_backup);
686 ret= db_load_routine(thd, type, name, sphp,
687 sql_mode, params, returns, body, chistics,
688 definer, created, modified, creation_ctx);
694 thd->time_zone_used= saved_time_zone_used;
696 close_system_tables(thd, &open_tables_state_backup);
697 thd->variables.sql_mode= saved_mode;
709 virtual bool handle_condition(THD *thd,
711 const char* sqlstate,
712 Sql_condition::enum_warning_level
level,
718 Silence_deprecated_warning::handle_condition(
722 Sql_condition::enum_warning_level
level,
727 if (sql_errno == ER_WARN_DEPRECATED_SYNTAX &&
728 level == Sql_condition::WARN_LEVEL_WARN)
748 static sp_head *sp_compile(THD *thd,
String *defstr, sql_mode_t sql_mode,
752 sql_mode_t old_sql_mode= thd->variables.sql_mode;
753 ha_rows old_select_limit= thd->variables.select_limit;
754 sp_rcontext *sp_runtime_ctx_saved= thd->sp_runtime_ctx;
756 Parser_state parser_state;
757 PSI_statement_locker *parent_locker= thd->m_statement_psi;
759 thd->variables.sql_mode= sql_mode;
760 thd->variables.select_limit= HA_POS_ERROR;
762 if (parser_state.init(thd, defstr->c_ptr(), defstr->length()))
764 thd->variables.sql_mode= old_sql_mode;
765 thd->variables.select_limit= old_select_limit;
770 thd->push_internal_handler(&warning_handler);
771 thd->sp_runtime_ctx= NULL;
773 thd->m_statement_psi= NULL;
774 if (
parse_sql(thd, & parser_state, creation_ctx) || thd->lex == NULL)
776 sp= thd->lex->sphead;
782 sp= thd->lex->sphead;
784 thd->m_statement_psi= parent_locker;
786 thd->pop_internal_handler();
787 thd->sp_runtime_ctx= sp_runtime_ctx_saved;
788 thd->variables.sql_mode= old_sql_mode;
789 thd->variables.select_limit= old_select_limit;
798 :m_error_caught(
false)
801 virtual bool handle_condition(THD *thd,
803 const char* sqlstate,
804 Sql_condition::enum_warning_level level,
808 bool error_caught()
const {
return m_error_caught; }
815 Bad_db_error_handler::handle_condition(THD *thd,
817 const char* sqlstate,
818 Sql_condition::enum_warning_level level,
822 if (sql_errno == ER_BAD_DB_ERROR)
824 m_error_caught=
true;
832 db_load_routine(THD *thd, enum_sp_type type,
sp_name *name,
sp_head **sphp,
833 sql_mode_t sql_mode,
const char *params,
const char *returns,
834 const char *body, st_sp_chistics &chistics,
835 const char *definer, longlong created, longlong modified,
838 LEX *old_lex= thd->lex, newlex;
840 char saved_cur_db_name_buf[NAME_LEN+1];
842 { saved_cur_db_name_buf,
sizeof(saved_cur_db_name_buf) };
845 char definer_user_name_holder[USERNAME_LENGTH + 1];
846 LEX_STRING definer_user_name= { definer_user_name_holder,
849 char definer_host_name_holder[HOSTNAME_LENGTH + 1];
850 LEX_STRING definer_host_name= { definer_host_name_holder, HOSTNAME_LENGTH };
855 newlex.current_select= NULL;
857 parse_user(definer, strlen(definer),
858 definer_user_name.str, &definer_user_name.length,
859 definer_host_name.str, &definer_host_name.length);
861 defstr.set_charset(creation_ctx->get_client_cs());
869 if (!create_string(thd, &defstr,
872 name->m_name.str, name->m_name.length,
873 params, strlen(params),
874 returns, strlen(returns),
876 &chistics, &definer_user_name, &definer_host_name,
879 ret= SP_INTERNAL_ERROR;
883 thd->push_internal_handler(&db_not_exists_handler);
890 if (mysql_opt_change_db(thd, &name->m_db, &saved_cur_db_name, TRUE,
893 ret= SP_INTERNAL_ERROR;
894 thd->pop_internal_handler();
897 thd->pop_internal_handler();
898 if (db_not_exists_handler.error_caught())
900 ret= SP_INTERNAL_ERROR;
901 my_error(ER_BAD_DB_ERROR, MYF(0), name->m_db.str);
907 *sphp= sp_compile(thd, &defstr, sql_mode, creation_ctx);
914 if (cur_db_changed && mysql_change_db(thd, &saved_cur_db_name, TRUE))
916 ret= SP_INTERNAL_ERROR;
926 (*sphp)->set_definer(&definer_user_name, &definer_host_name);
927 (*sphp)->set_info(created, modified, &chistics, sql_mode);
928 (*sphp)->set_creation_ctx(creation_ctx);
938 newlex.set_trg_event_type_for_tables();
942 thd->lex->sphead= NULL;
955 memset(&table, 0,
sizeof(table));
956 memset(&share, 0,
sizeof(share));
960 field->sql_type(result);
962 if (field->has_charset())
964 result.append(STRING_WITH_LEN(
" CHARSET "));
965 result.append(field->charset()->csname);
966 if (!(field->charset()->state & MY_CS_PRIMARY))
968 result.append(STRING_WITH_LEN(
" COLLATE "));
969 result.append(field->charset()->name);
998 int sp_create_routine(THD *thd,
sp_head *sp)
1002 char definer[USER_HOST_BUFF_SIZE];
1003 sql_mode_t saved_mode= thd->variables.sql_mode;
1005 MDL_key::FUNCTION : MDL_key::PROCEDURE;
1007 const CHARSET_INFO *db_cs= get_default_db_collation(thd, sp->m_db.str);
1009 enum_check_fields saved_count_cuted_fields;
1011 bool store_failed= FALSE;
1013 bool save_binlog_row_based;
1015 DBUG_ENTER(
"sp_create_routine");
1016 DBUG_PRINT(
"enter", (
"type: %d name: %.*s",sp->
m_type,
1017 (
int) sp->m_name.length, sp->m_name.str));
1019 retstr.set_charset(system_charset_info);
1021 DBUG_ASSERT(sp->
m_type == SP_TYPE_PROCEDURE ||
1022 sp->
m_type == SP_TYPE_FUNCTION);
1026 DBUG_RETURN(SP_OPEN_TABLE_FAILED);
1029 thd->variables.sql_mode= 0;
1036 if ((save_binlog_row_based= thd->is_current_stmt_binlog_format_row()))
1037 thd->clear_current_stmt_binlog_format_row();
1039 saved_count_cuted_fields= thd->count_cuted_fields;
1040 thd->count_cuted_fields= CHECK_FIELD_WARN;
1042 if (!(table= open_proc_table_for_update(thd)))
1043 ret= SP_OPEN_TABLE_FAILED;
1046 restore_record(table, s->default_values);
1049 strxnmov(definer,
sizeof(definer)-1, thd->lex->definer->user.str,
"@",
1050 thd->lex->definer->host.str, NullS);
1052 if (table->s->fields < MYSQL_PROC_FIELD_COUNT)
1054 ret= SP_GET_FIELD_FAILED;
1058 if (system_charset_info->cset->numchars(system_charset_info,
1060 sp->m_name.str+sp->m_name.length) >
1061 table->field[MYSQL_PROC_FIELD_NAME]->char_length())
1063 ret= SP_BAD_IDENTIFIER;
1066 if (sp->m_body.length > table->field[MYSQL_PROC_FIELD_BODY]->field_length)
1068 ret= SP_BODY_TOO_LONG;
1073 table->field[MYSQL_PROC_FIELD_DB]->
1074 store(sp->m_db.str, sp->m_db.length, system_charset_info);
1076 store_failed= store_failed ||
1077 table->field[MYSQL_PROC_FIELD_NAME]->
1078 store(sp->m_name.str, sp->m_name.length, system_charset_info);
1080 store_failed= store_failed ||
1081 table->field[MYSQL_PROC_MYSQL_TYPE]->
1082 store((longlong)sp->
m_type, TRUE);
1084 store_failed= store_failed ||
1085 table->field[MYSQL_PROC_FIELD_SPECIFIC_NAME]->
1086 store(sp->m_name.str, sp->m_name.length, system_charset_info);
1088 if (sp->
m_chistics->daccess != SP_DEFAULT_ACCESS)
1090 store_failed= store_failed ||
1091 table->field[MYSQL_PROC_FIELD_ACCESS]->
1092 store((longlong)sp->
m_chistics->daccess, TRUE);
1095 store_failed= store_failed ||
1096 table->field[MYSQL_PROC_FIELD_DETERMINISTIC]->
1097 store((longlong)(sp->
m_chistics->detistic ? 1 : 2), TRUE);
1099 if (sp->
m_chistics->suid != SP_IS_DEFAULT_SUID)
1101 store_failed= store_failed ||
1102 table->field[MYSQL_PROC_FIELD_SECURITY_TYPE]->
1106 store_failed= store_failed ||
1107 table->field[MYSQL_PROC_FIELD_PARAM_LIST]->
1108 store(sp->m_params.str, sp->m_params.length, system_charset_info);
1110 if (sp->
m_type == SP_TYPE_FUNCTION)
1112 sp_returns_type(thd, retstr, sp);
1114 store_failed= store_failed ||
1115 table->field[MYSQL_PROC_FIELD_RETURNS]->
1116 store(retstr.ptr(), retstr.length(), system_charset_info);
1119 store_failed= store_failed ||
1120 table->field[MYSQL_PROC_FIELD_BODY]->
1121 store(sp->m_body.str, sp->m_body.length, system_charset_info);
1123 store_failed= store_failed ||
1124 table->field[MYSQL_PROC_FIELD_DEFINER]->
1125 store(definer, (uint)strlen(definer), system_charset_info);
1130 store_failed= store_failed ||
1131 table->field[MYSQL_PROC_FIELD_SQL_MODE]->
1132 store((longlong)saved_mode, TRUE);
1136 store_failed= store_failed ||
1137 table->field[MYSQL_PROC_FIELD_COMMENT]->
1139 system_charset_info);
1142 if ((sp->
m_type == SP_TYPE_FUNCTION) &&
1143 !trust_function_creators && mysql_bin_log.is_open())
1151 enum enum_sp_data_access access=
1152 (sp->
m_chistics->daccess == SP_DEFAULT_ACCESS) ?
1153 SP_DEFAULT_ACCESS_MAPPING : sp->
m_chistics->daccess;
1154 if (access == SP_CONTAINS_SQL ||
1155 access == SP_MODIFIES_SQL_DATA)
1157 my_message(ER_BINLOG_UNSAFE_ROUTINE,
1158 ER(ER_BINLOG_UNSAFE_ROUTINE), MYF(0));
1159 ret= SP_INTERNAL_ERROR;
1163 if (!(thd->security_ctx->master_access & SUPER_ACL))
1165 my_message(ER_BINLOG_CREATE_ROUTINE_NEED_SUPER,
1166 ER(ER_BINLOG_CREATE_ROUTINE_NEED_SUPER), MYF(0));
1167 ret= SP_INTERNAL_ERROR;
1172 table->field[MYSQL_PROC_FIELD_CHARACTER_SET_CLIENT]->set_notnull();
1173 store_failed= store_failed ||
1174 table->field[MYSQL_PROC_FIELD_CHARACTER_SET_CLIENT]->store(
1175 thd->charset()->csname,
1176 strlen(thd->charset()->csname),
1177 system_charset_info);
1179 table->field[MYSQL_PROC_FIELD_COLLATION_CONNECTION]->set_notnull();
1180 store_failed= store_failed ||
1181 table->field[MYSQL_PROC_FIELD_COLLATION_CONNECTION]->store(
1182 thd->variables.collation_connection->name,
1183 strlen(thd->variables.collation_connection->name),
1184 system_charset_info);
1186 table->field[MYSQL_PROC_FIELD_DB_COLLATION]->set_notnull();
1187 store_failed= store_failed ||
1188 table->field[MYSQL_PROC_FIELD_DB_COLLATION]->store(
1189 db_cs->name, strlen(db_cs->name), system_charset_info);
1191 table->field[MYSQL_PROC_FIELD_BODY_UTF8]->set_notnull();
1192 store_failed= store_failed ||
1193 table->field[MYSQL_PROC_FIELD_BODY_UTF8]->store(
1194 sp->m_body_utf8.str, sp->m_body_utf8.length, system_charset_info);
1198 ret= SP_FLD_STORE_FAILED;
1203 if (table->file->ha_write_row(table->record[0]))
1204 ret= SP_WRITE_ROW_FAILED;
1206 sp_cache_invalidate();
1208 if (ret == SP_OK && mysql_bin_log.is_open())
1213 log_query.set_charset(system_charset_info);
1215 if (!create_string(thd, &log_query,
1219 sp->m_name.str, sp->m_name.length,
1220 sp->m_params.str, sp->m_params.length,
1221 retstr.c_ptr(), retstr.length(),
1222 sp->m_body.str, sp->m_body.length,
1224 &(thd->lex->definer->host),
1227 ret= SP_INTERNAL_ERROR;
1231 thd->variables.sql_mode= saved_mode;
1232 thd->add_to_binlog_accessed_dbs(sp->m_db.str);
1234 if (thd->binlog_query(THD::STMT_QUERY_TYPE,
1235 log_query.c_ptr(), log_query.length(),
1236 FALSE, FALSE, FALSE, 0))
1237 ret= SP_INTERNAL_ERROR;
1238 thd->variables.sql_mode= 0;
1243 thd->count_cuted_fields= saved_count_cuted_fields;
1244 thd->variables.sql_mode= saved_mode;
1246 DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row());
1247 if (save_binlog_row_based)
1248 thd->set_current_stmt_binlog_format_row();
1268 int sp_drop_routine(THD *thd, enum_sp_type type,
sp_name *name)
1272 bool save_binlog_row_based;
1274 MDL_key::FUNCTION : MDL_key::PROCEDURE;
1275 DBUG_ENTER(
"sp_drop_routine");
1276 DBUG_PRINT(
"enter", (
"type: %d name: %.*s",
1277 type, (
int) name->m_name.length, name->m_name.str));
1279 DBUG_ASSERT(type == SP_TYPE_PROCEDURE || type == SP_TYPE_FUNCTION);
1283 DBUG_RETURN(SP_DELETE_ROW_FAILED);
1285 if (!(table= open_proc_table_for_update(thd)))
1286 DBUG_RETURN(SP_OPEN_TABLE_FAILED);
1293 if ((save_binlog_row_based= thd->is_current_stmt_binlog_format_row()))
1294 thd->clear_current_stmt_binlog_format_row();
1296 if ((ret= db_find_routine_aux(thd, type, name, table)) == SP_OK)
1298 if (table->file->ha_delete_row(table->record[0]))
1299 ret= SP_DELETE_ROW_FAILED;
1304 thd->add_to_binlog_accessed_dbs(name->m_db.str);
1305 if (write_bin_log(thd, TRUE, thd->query(), thd->query_length()))
1306 ret= SP_INTERNAL_ERROR;
1307 sp_cache_invalidate();
1316 sp_cache **spc= (type == SP_TYPE_FUNCTION ?
1317 &thd->sp_func_cache : &thd->sp_proc_cache);
1318 sp= sp_cache_lookup(spc, name);
1320 sp_cache_flush_obsolete(spc, &sp);
1324 DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row());
1325 if (save_binlog_row_based)
1326 thd->set_current_stmt_binlog_format_row();
1348 int sp_update_routine(THD *thd, enum_sp_type type,
sp_name *name,
1349 st_sp_chistics *chistics)
1353 bool save_binlog_row_based;
1355 MDL_key::FUNCTION : MDL_key::PROCEDURE;
1356 DBUG_ENTER(
"sp_update_routine");
1357 DBUG_PRINT(
"enter", (
"type: %d name: %.*s",
1358 type, (
int) name->m_name.length, name->m_name.str));
1360 DBUG_ASSERT(type == SP_TYPE_PROCEDURE || type == SP_TYPE_FUNCTION);
1364 DBUG_RETURN(SP_OPEN_TABLE_FAILED);
1366 if (!(table= open_proc_table_for_update(thd)))
1367 DBUG_RETURN(SP_OPEN_TABLE_FAILED);
1374 if ((save_binlog_row_based= thd->is_current_stmt_binlog_format_row()))
1375 thd->clear_current_stmt_binlog_format_row();
1377 if ((ret= db_find_routine_aux(thd, type, name, table)) == SP_OK)
1379 if (type == SP_TYPE_FUNCTION && ! trust_function_creators &&
1380 mysql_bin_log.is_open() &&
1381 (chistics->daccess == SP_CONTAINS_SQL ||
1382 chistics->daccess == SP_MODIFIES_SQL_DATA))
1385 bool is_deterministic;
1386 ptr= get_field(thd->mem_root,
1387 table->field[MYSQL_PROC_FIELD_DETERMINISTIC]);
1390 ret= SP_INTERNAL_ERROR;
1393 is_deterministic= ptr[0] ==
'N' ? FALSE : TRUE;
1394 if (!is_deterministic)
1396 my_message(ER_BINLOG_UNSAFE_ROUTINE,
1397 ER(ER_BINLOG_UNSAFE_ROUTINE), MYF(0));
1398 ret= SP_INTERNAL_ERROR;
1403 store_record(table,
record[1]);
1405 if (chistics->suid != SP_IS_DEFAULT_SUID)
1406 table->field[MYSQL_PROC_FIELD_SECURITY_TYPE]->
1407 store((longlong)chistics->suid, TRUE);
1408 if (chistics->daccess != SP_DEFAULT_ACCESS)
1409 table->field[MYSQL_PROC_FIELD_ACCESS]->
1410 store((longlong)chistics->daccess, TRUE);
1411 if (chistics->comment.str)
1412 table->field[MYSQL_PROC_FIELD_COMMENT]->store(chistics->comment.str,
1413 chistics->comment.length,
1414 system_charset_info);
1415 if ((ret= table->file->ha_update_row(table->record[1],table->record[0])) &&
1416 ret != HA_ERR_RECORD_IS_THE_SAME)
1417 ret= SP_WRITE_ROW_FAILED;
1424 if (write_bin_log(thd, TRUE, thd->query(), thd->query_length()))
1425 ret= SP_INTERNAL_ERROR;
1426 sp_cache_invalidate();
1430 DBUG_ASSERT(!thd->is_current_stmt_binlog_format_row());
1431 if (save_binlog_row_based)
1432 thd->set_current_stmt_binlog_format_row();
1444 bool handle_condition(THD *thd,
1446 const char* sqlstate,
1447 Sql_condition::enum_warning_level level,
1451 if (sql_errno == ER_NO_SUCH_TABLE ||
1452 sql_errno == ER_CANNOT_LOAD_FROM_TABLE_V2 ||
1453 sql_errno == ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE ||
1454 sql_errno == ER_COL_COUNT_DOESNT_MATCH_CORRUPTED_V2)
1470 bool lock_db_routines(THD *thd,
char *db)
1474 Open_tables_backup open_tables_state_backup;
1477 DBUG_ENTER(
"lock_db_routines");
1484 thd->push_internal_handler(&err_handler);
1485 table= open_proc_table_for_read(thd, &open_tables_state_backup);
1486 thd->pop_internal_handler();
1494 DBUG_RETURN(thd->is_error() || thd->killed);
1497 table->field[MYSQL_PROC_FIELD_DB]->store(db, strlen(db), system_charset_info);
1498 key_len= table->key_info->key_part[0].store_length;
1503 close_system_tables(thd, &open_tables_state_backup);
1508 table->field[MYSQL_PROC_FIELD_DB]->ptr,
1509 (key_part_map)1, HA_READ_KEY_EXACT))
1513 char *
sp_name= get_field(thd->mem_root,
1514 table->field[MYSQL_PROC_FIELD_NAME]);
1515 longlong sp_type= table->field[MYSQL_PROC_MYSQL_TYPE]->val_int();
1517 mdl_request->
init(sp_type == SP_TYPE_FUNCTION ?
1518 MDL_key::FUNCTION : MDL_key::PROCEDURE,
1519 db, sp_name, MDL_EXCLUSIVE, MDL_TRANSACTION);
1520 mdl_requests.push_front(mdl_request);
1522 table->field[MYSQL_PROC_FIELD_DB]->ptr,
1526 if (nxtres != 0 && nxtres != HA_ERR_END_OF_FILE)
1529 close_system_tables(thd, &open_tables_state_backup);
1532 close_system_tables(thd, &open_tables_state_backup);
1535 DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::GLOBAL,
"",
"",
1536 MDL_INTENTION_EXCLUSIVE) &&
1537 thd->mdl_context.is_lock_owner(MDL_key::SCHEMA, db,
"",
1539 DBUG_RETURN(thd->mdl_context.acquire_locks(&mdl_requests,
1540 thd->variables.lock_wait_timeout));
1555 sp_drop_db_routines(THD *thd,
char *db)
1560 MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
1561 DBUG_ENTER(
"sp_drop_db_routines");
1562 DBUG_PRINT(
"enter", (
"db: %s", db));
1564 ret= SP_OPEN_TABLE_FAILED;
1565 if (!(table= open_proc_table_for_update(thd)))
1568 table->field[MYSQL_PROC_FIELD_DB]->store(db, strlen(db), system_charset_info);
1569 key_len= table->key_info->key_part[0].store_length;
1574 ret= SP_KEY_NOT_FOUND;
1579 (uchar *)table->field[MYSQL_PROC_FIELD_DB]->ptr,
1580 (key_part_map)1, HA_READ_KEY_EXACT))
1583 bool deleted= FALSE;
1587 if (! table->file->ha_delete_row(table->record[0]))
1591 ret= SP_DELETE_ROW_FAILED;
1596 (uchar *)table->field[MYSQL_PROC_FIELD_DB]->ptr,
1598 if (nxtres != HA_ERR_END_OF_FILE)
1599 ret= SP_KEY_NOT_FOUND;
1601 sp_cache_invalidate();
1606 close_thread_tables(thd);
1611 thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
1634 bool sp_show_create_routine(THD *thd, enum_sp_type type, sp_name *name)
1638 DBUG_ENTER(
"sp_show_create_routine");
1639 DBUG_PRINT(
"enter", (
"name: %.*s",
1640 (
int) name->m_name.length,
1643 DBUG_ASSERT(type == SP_TYPE_PROCEDURE || type == SP_TYPE_FUNCTION);
1652 if (sp_cache_routine(thd, type, name, FALSE, &sp))
1661 my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
1662 type == SP_TYPE_FUNCTION ?
"FUNCTION" :
"PROCEDURE",
1688 sp_head *sp_find_routine(THD *thd, enum_sp_type type, sp_name *name,
1692 ulong depth= (type == SP_TYPE_PROCEDURE ?
1693 thd->variables.max_sp_recursion_depth :
1695 DBUG_ENTER(
"sp_find_routine");
1696 DBUG_PRINT(
"enter", (
"name: %.*s.%.*s type: %d cache only %d",
1697 (
int) name->m_db.length, name->m_db.str,
1698 (
int) name->m_name.length, name->m_name.str,
1701 if ((sp= sp_cache_lookup(cp, name)))
1705 const char *returns=
"";
1706 char definer[USER_HOST_BUFF_SIZE];
1713 retstr.set_charset(sp->get_creation_ctx()->get_client_cs());
1715 DBUG_PRINT(
"info", (
"found: 0x%lx", (ulong)sp));
1718 DBUG_PRINT(
"info", (
"first free: 0x%lx level: %lu flags %x",
1725 recursion_level_error(thd, sp);
1739 recursion_level_error(thd, sp);
1743 strxmov(definer, sp->m_definer_user.str,
"@",
1744 sp->m_definer_host.str, NullS);
1745 if (type == SP_TYPE_FUNCTION)
1747 sp_returns_type(thd, retstr, sp);
1748 returns= retstr.ptr();
1750 if (db_load_routine(thd, type, name, &new_sp,
1753 sp->m_created, sp->m_modified,
1754 sp->get_creation_ctx()) == SP_OK)
1760 DBUG_PRINT(
"info", (
"added level: 0x%lx, level: %lu, flags %x",
1763 DBUG_RETURN(new_sp);
1769 if (db_find_routine(thd, type, name, &sp) == SP_OK)
1771 sp_cache_insert(cp, sp);
1772 DBUG_PRINT(
"info", (
"added new: 0x%lx, level: %lu, flags %x",
1796 sp_exist_routines(THD *thd,
TABLE_LIST *routines,
bool is_proc)
1799 bool sp_object_found;
1800 DBUG_ENTER(
"sp_exists_routine");
1801 for (routine= routines; routine; routine= routine->next_global)
1806 lex_db.length= strlen(routine->db);
1807 lex_name.length= strlen(routine->table_name);
1808 lex_db.str= thd->strmake(routine->db, lex_db.length);
1809 lex_name.str= thd->strmake(routine->table_name, lex_name.length);
1810 name=
new sp_name(lex_db, lex_name,
true);
1812 sp_object_found= is_proc ? sp_find_routine(thd, SP_TYPE_PROCEDURE,
1813 name, &thd->sp_proc_cache,
1815 sp_find_routine(thd, SP_TYPE_FUNCTION,
1816 name, &thd->sp_func_cache,
1818 thd->get_stmt_da()->clear_warning_info(thd->query_id);
1819 if (! sp_object_found)
1821 my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
"FUNCTION or PROCEDURE",
1822 routine->table_name);
1830 extern "C" uchar* sp_sroutine_key(
const uchar *ptr,
size_t *plen,
1872 bool sp_add_used_routine(Query_tables_list *prelocking_ctx, Query_arena *arena,
1875 my_hash_init_opt(&prelocking_ctx->sroutines, system_charset_info,
1876 Query_tables_list::START_SROUTINES_HASH_SIZE,
1877 0, 0, sp_sroutine_key, 0, 0);
1879 if (!my_hash_search(&prelocking_ctx->sroutines, key->ptr(), key->length()))
1886 if (my_hash_insert(&prelocking_ctx->sroutines, (uchar *)rn))
1888 prelocking_ctx->sroutines_list.link_in_list(rn, &rn->
next);
1915 void sp_add_used_routine(Query_tables_list *prelocking_ctx, Query_arena *arena,
1916 sp_name *rt, enum_sp_type rt_type)
1918 MDL_key key((rt_type == SP_TYPE_FUNCTION) ? MDL_key::FUNCTION :
1920 rt->m_db.str, rt->m_name.str);
1921 (void)sp_add_used_routine(prelocking_ctx, arena, &key, 0);
1922 prelocking_ctx->sroutines_list_own_last= prelocking_ctx->sroutines_list.next;
1923 prelocking_ctx->sroutines_list_own_elements=
1924 prelocking_ctx->sroutines_list.elements;
1935 void sp_remove_not_own_routines(Query_tables_list *prelocking_ctx)
1938 for (not_own_rt= *prelocking_ctx->sroutines_list_own_last;
1939 not_own_rt; not_own_rt= next_rt)
1945 next_rt= not_own_rt->
next;
1946 my_hash_delete(&prelocking_ctx->sroutines, (uchar *)not_own_rt);
1949 *prelocking_ctx->sroutines_list_own_last= NULL;
1950 prelocking_ctx->sroutines_list.next= prelocking_ctx->sroutines_list_own_last;
1951 prelocking_ctx->sroutines_list.elements=
1952 prelocking_ctx->sroutines_list_own_elements;
1971 sp_update_stmt_used_routines(THD *thd, Query_tables_list *prelocking_ctx,
1974 for (uint
i=0 ;
i < src->records ;
i++)
1977 (void)sp_add_used_routine(prelocking_ctx, thd->stmt_arena,
1997 void sp_update_stmt_used_routines(THD *thd, Query_tables_list *prelocking_ctx,
2002 (void)sp_add_used_routine(prelocking_ctx, thd->stmt_arena,
2013 bool lookup_only,
sp_head **sp)
2015 char qname_buff[NAME_LEN*2+1+1];
2018 enum_sp_type type= (mdl_type == MDL_key::FUNCTION) ?
2019 SP_TYPE_FUNCTION : SP_TYPE_PROCEDURE;
2029 return sp_cache_routine(thd, type, &name, lookup_only, sp);
2053 int sp_cache_routine(THD *thd, enum_sp_type type, sp_name *name,
2054 bool lookup_only,
sp_head **sp)
2057 sp_cache **spc= (type == SP_TYPE_FUNCTION) ?
2058 &thd->sp_func_cache : &thd->sp_proc_cache;
2060 DBUG_ENTER(
"sp_cache_routine");
2062 DBUG_ASSERT(type == SP_TYPE_FUNCTION || type == SP_TYPE_PROCEDURE);
2065 *sp= sp_cache_lookup(spc, name);
2072 sp_cache_flush_obsolete(spc, sp);
2077 switch ((ret= db_find_routine(thd, type, name, sp)))
2080 sp_cache_insert(spc, *sp);
2082 case SP_KEY_NOT_FOUND:
2094 if (ret == SP_PARSE_ERROR)
2101 if (! thd->is_error())
2111 char n[NAME_LEN*2+2];
2114 memcpy(n, name->m_qname.str, name->m_qname.length);
2115 n[name->m_qname.length]=
'\0';
2116 my_error(ER_SP_PROC_TABLE_CORRUPT, MYF(0), n, ret);
2130 static bool create_string(THD *thd,
String *
buf,
2132 const char *db, ulong dblen,
2133 const char *name, ulong namelen,
2134 const char *params, ulong paramslen,
2135 const char *returns, ulong returnslen,
2136 const char *body, ulong bodylen,
2137 st_sp_chistics *chistics,
2140 sql_mode_t sql_mode)
2142 sql_mode_t old_sql_mode= thd->variables.sql_mode;
2144 if (buf->alloc(100 + dblen + 1 + namelen + paramslen + returnslen + bodylen +
2145 chistics->comment.length + 10 +
2146 USER_HOST_BUFF_SIZE))
2149 thd->variables.sql_mode= sql_mode;
2150 buf->append(STRING_WITH_LEN(
"CREATE "));
2151 append_definer(thd, buf, definer_user, definer_host);
2152 if (type == SP_TYPE_FUNCTION)
2153 buf->append(STRING_WITH_LEN(
"FUNCTION "));
2155 buf->append(STRING_WITH_LEN(
"PROCEDURE "));
2158 append_identifier(thd, buf, db, dblen);
2161 append_identifier(thd, buf, name, namelen);
2163 buf->append(params, paramslen);
2165 if (type == SP_TYPE_FUNCTION)
2167 buf->append(STRING_WITH_LEN(
" RETURNS "));
2168 buf->append(returns, returnslen);
2171 switch (chistics->daccess) {
2173 buf->append(STRING_WITH_LEN(
" NO SQL\n"));
2175 case SP_READS_SQL_DATA:
2176 buf->append(STRING_WITH_LEN(
" READS SQL DATA\n"));
2178 case SP_MODIFIES_SQL_DATA:
2179 buf->append(STRING_WITH_LEN(
" MODIFIES SQL DATA\n"));
2181 case SP_DEFAULT_ACCESS:
2182 case SP_CONTAINS_SQL:
2186 if (chistics->detistic)
2187 buf->append(STRING_WITH_LEN(
" DETERMINISTIC\n"));
2188 if (chistics->suid == SP_IS_NOT_SUID)
2189 buf->append(STRING_WITH_LEN(
" SQL SECURITY INVOKER\n"));
2190 if (chistics->comment.length)
2192 buf->append(STRING_WITH_LEN(
" COMMENT "));
2193 append_unescaped(buf, chistics->comment.str, chistics->comment.length);
2196 buf->append(body, bodylen);
2197 thd->variables.sql_mode= old_sql_mode;
2223 sp_load_for_information_schema(THD *thd,
TABLE *proc_table,
String *db,
2224 String *name, sql_mode_t sql_mode,
2226 const char *returns,
const char *params,
2229 const char *sp_body;
2231 struct st_sp_chistics sp_chistics;
2232 const LEX_STRING definer_user= {(
char*)STRING_WITH_LEN(
"")};
2233 const LEX_STRING definer_host= {(
char*)STRING_WITH_LEN(
"")};
2237 sp_cache **spc= (type == SP_TYPE_FUNCTION) ?
2238 &thd->sp_func_cache : &thd->sp_proc_cache;
2239 sp_db_str.str= db->c_ptr();
2240 sp_db_str.length= db->length();
2241 sp_name_str.str= name->c_ptr();
2242 sp_name_str.length= name->length();
2243 sp_name sp_name_obj(sp_db_str, sp_name_str,
true);
2244 sp_name_obj.init_qname(thd);
2246 if ((sp= sp_cache_lookup(spc, &sp_name_obj)))
2251 LEX *old_lex= thd->lex, newlex;
2253 Stored_routine_creation_ctx::load_from_db(thd, &sp_name_obj, proc_table);
2254 sp_body= (type == SP_TYPE_FUNCTION) ?
"RETURN NULL" :
"BEGIN END";
2255 memset(&sp_chistics, 0,
sizeof(sp_chistics));
2256 defstr.set_charset(creation_ctx->get_client_cs());
2257 if (!create_string(thd, &defstr, type,
2258 sp_db_str.str, sp_db_str.length,
2259 sp_name_obj.m_name.str, sp_name_obj.m_name.length,
2260 params, strlen(params),
2261 returns, strlen(returns),
2262 sp_body, strlen(sp_body),
2263 &sp_chistics, &definer_user, &definer_host, sql_mode))
2267 newlex.current_select= NULL;
2268 sp= sp_compile(thd, &defstr, sql_mode, creation_ctx);
2270 thd->lex->sphead= NULL;
2294 enum_sp_type sp_type,
2311 sp->m_root_parsing_ctx=
new (thd->mem_root)
sp_pcontext();
2313 if (!sp->m_root_parsing_ctx)
2316 thd->lex->set_sp_current_parsing_ctx(sp->m_root_parsing_ctx);
2320 sp->init_sp_name(thd, sp_name);
2333 void sp_finish_parsing(THD *thd)
2335 sp_head *sp= thd->lex->sphead;
2346 Item_result sp_map_result_type(
enum enum_field_types type)
2349 case MYSQL_TYPE_BIT:
2350 case MYSQL_TYPE_TINY:
2351 case MYSQL_TYPE_SHORT:
2352 case MYSQL_TYPE_LONG:
2353 case MYSQL_TYPE_LONGLONG:
2354 case MYSQL_TYPE_INT24:
2356 case MYSQL_TYPE_DECIMAL:
2357 case MYSQL_TYPE_NEWDECIMAL:
2358 return DECIMAL_RESULT;
2359 case MYSQL_TYPE_FLOAT:
2360 case MYSQL_TYPE_DOUBLE:
2363 return STRING_RESULT;
2369 Item::Type sp_map_item_type(
enum enum_field_types type)
2372 case MYSQL_TYPE_BIT:
2373 case MYSQL_TYPE_TINY:
2374 case MYSQL_TYPE_SHORT:
2375 case MYSQL_TYPE_LONG:
2376 case MYSQL_TYPE_LONGLONG:
2377 case MYSQL_TYPE_INT24:
2378 return Item::INT_ITEM;
2379 case MYSQL_TYPE_DECIMAL:
2380 case MYSQL_TYPE_NEWDECIMAL:
2381 return Item::DECIMAL_ITEM;
2382 case MYSQL_TYPE_FLOAT:
2383 case MYSQL_TYPE_DOUBLE:
2384 return Item::REAL_ITEM;
2386 return Item::STRING_ITEM;
2400 uint sp_get_flags_for_command(LEX *lex)
2404 switch (lex->sql_command) {
2412 case SQLCOM_ANALYZE:
2413 case SQLCOM_OPTIMIZE:
2414 case SQLCOM_PRELOAD_KEYS:
2415 case SQLCOM_ASSIGN_TO_KEYCACHE:
2416 case SQLCOM_CHECKSUM:
2418 case SQLCOM_HA_READ:
2419 case SQLCOM_SHOW_BINLOGS:
2420 case SQLCOM_SHOW_BINLOG_EVENTS:
2421 case SQLCOM_SHOW_RELAYLOG_EVENTS:
2422 case SQLCOM_SHOW_CHARSETS:
2423 case SQLCOM_SHOW_COLLATIONS:
2424 case SQLCOM_SHOW_CREATE:
2425 case SQLCOM_SHOW_CREATE_DB:
2426 case SQLCOM_SHOW_CREATE_FUNC:
2427 case SQLCOM_SHOW_CREATE_PROC:
2428 case SQLCOM_SHOW_CREATE_EVENT:
2429 case SQLCOM_SHOW_CREATE_TRIGGER:
2430 case SQLCOM_SHOW_DATABASES:
2431 case SQLCOM_SHOW_ERRORS:
2432 case SQLCOM_SHOW_FIELDS:
2433 case SQLCOM_SHOW_FUNC_CODE:
2434 case SQLCOM_SHOW_GRANTS:
2435 case SQLCOM_SHOW_ENGINE_STATUS:
2436 case SQLCOM_SHOW_ENGINE_LOGS:
2437 case SQLCOM_SHOW_ENGINE_MUTEX:
2438 case SQLCOM_SHOW_EVENTS:
2439 case SQLCOM_SHOW_KEYS:
2440 case SQLCOM_SHOW_MASTER_STAT:
2441 case SQLCOM_SHOW_OPEN_TABLES:
2442 case SQLCOM_SHOW_PRIVILEGES:
2443 case SQLCOM_SHOW_PROCESSLIST:
2444 case SQLCOM_SHOW_PROC_CODE:
2445 case SQLCOM_SHOW_SLAVE_HOSTS:
2446 case SQLCOM_SHOW_SLAVE_STAT:
2447 case SQLCOM_SHOW_STATUS:
2448 case SQLCOM_SHOW_STATUS_FUNC:
2449 case SQLCOM_SHOW_STATUS_PROC:
2450 case SQLCOM_SHOW_STORAGE_ENGINES:
2451 case SQLCOM_SHOW_TABLES:
2452 case SQLCOM_SHOW_TABLE_STATUS:
2453 case SQLCOM_SHOW_VARIABLES:
2454 case SQLCOM_SHOW_WARNS:
2456 flags= sp_head::MULTI_RESULTS;
2464 case SQLCOM_EXECUTE:
2465 flags= sp_head::MULTI_RESULTS | sp_head::CONTAINS_DYNAMIC_SQL;
2467 case SQLCOM_PREPARE:
2468 case SQLCOM_DEALLOCATE_PREPARE:
2469 flags= sp_head::CONTAINS_DYNAMIC_SQL;
2471 case SQLCOM_CREATE_TABLE:
2472 if (lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)
2475 flags= sp_head::HAS_COMMIT_OR_ROLLBACK;
2477 case SQLCOM_DROP_TABLE:
2478 if (lex->drop_temporary)
2481 flags= sp_head::HAS_COMMIT_OR_ROLLBACK;
2484 flags= sp_head::HAS_SQLCOM_FLUSH;
2487 flags= sp_head::HAS_SQLCOM_RESET;
2489 case SQLCOM_CREATE_INDEX:
2490 case SQLCOM_CREATE_DB:
2491 case SQLCOM_CREATE_VIEW:
2492 case SQLCOM_CREATE_TRIGGER:
2493 case SQLCOM_CREATE_USER:
2494 case SQLCOM_ALTER_TABLE:
2498 case SQLCOM_RENAME_TABLE:
2499 case SQLCOM_RENAME_USER:
2500 case SQLCOM_DROP_INDEX:
2501 case SQLCOM_DROP_DB:
2502 case SQLCOM_REVOKE_ALL:
2503 case SQLCOM_DROP_USER:
2504 case SQLCOM_DROP_VIEW:
2505 case SQLCOM_DROP_TRIGGER:
2506 case SQLCOM_TRUNCATE:
2508 case SQLCOM_ROLLBACK:
2510 case SQLCOM_LOCK_TABLES:
2511 case SQLCOM_CREATE_PROCEDURE:
2512 case SQLCOM_CREATE_SPFUNCTION:
2513 case SQLCOM_ALTER_PROCEDURE:
2514 case SQLCOM_ALTER_FUNCTION:
2515 case SQLCOM_DROP_PROCEDURE:
2516 case SQLCOM_DROP_FUNCTION:
2517 case SQLCOM_CREATE_EVENT:
2518 case SQLCOM_ALTER_EVENT:
2519 case SQLCOM_DROP_EVENT:
2520 case SQLCOM_INSTALL_PLUGIN:
2521 case SQLCOM_UNINSTALL_PLUGIN:
2522 flags= sp_head::HAS_COMMIT_OR_ROLLBACK;
2525 flags= lex->describe ? sp_head::MULTI_RESULTS : 0;
2545 if (!ident || !ident->str || !ident->str[0] ||
2546 ident->str[ident->length-1] ==
' ')
2548 my_error(ER_SP_WRONG_NAME, MYF(0), ident->str);
2552 if (check_string_char_length(ident,
"", NAME_CHAR_LEN,
2553 system_charset_info, 1))
2555 my_error(ER_TOO_LONG_IDENT, MYF(0), ident->str);
2567 TABLE_LIST *sp_add_to_query_tables(THD *thd, LEX *lex,
2568 const char *db,
const char *name,
2569 thr_lock_type locktype,
2570 enum_mdl_type mdl_type)
2577 table->db_length= strlen(db);
2578 table->db= thd->strmake(db, table->db_length);
2579 table->table_name_length= strlen(name);
2580 table->table_name= thd->strmake(name, table->table_name_length);
2581 table->alias= thd->strdup(name);
2582 table->lock_type= locktype;
2583 table->select_lex= lex->current_select;
2584 table->cacheable_table= 1;
2585 table->mdl_request.
init(MDL_key::TABLE, table->db, table->table_name,
2586 mdl_type, MDL_TRANSACTION);
2588 lex->add_to_query_tables(table);
2605 Item *sp_prepare_func_item(THD* thd,
Item **it_addr)
2607 it_addr= (*it_addr)->this_item_addr(thd, it_addr);
2609 if (!(*it_addr)->fixed &&
2610 ((*it_addr)->fix_fields(thd, it_addr) ||
2611 (*it_addr)->check_cols(1)))
2613 DBUG_PRINT(
"info", (
"fix_fields() failed"));
2633 bool sp_eval_expr(THD *thd,
Field *result_field,
Item **expr_item_ptr)
2636 enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
2637 bool save_abort_on_warning= thd->abort_on_warning;
2638 unsigned int stmt_unsafe_rollback_flags=
2639 thd->transaction.stmt.get_unsafe_rollback_flags();
2641 if (!*expr_item_ptr)
2644 if (!(expr_item= sp_prepare_func_item(thd, expr_item_ptr)))
2654 thd->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
2655 thd->abort_on_warning= thd->is_strict_mode();
2656 thd->transaction.stmt.reset_unsafe_rollback_flags();
2662 thd->count_cuted_fields= save_count_cuted_fields;
2663 thd->abort_on_warning= save_abort_on_warning;
2664 thd->transaction.stmt.set_unsafe_rollback_flags(stmt_unsafe_rollback_flags);
2666 if (!thd->is_error())
2676 result_field->set_null();
2696 switch (item->result_type()) {
2699 case DECIMAL_RESULT:
2700 if (item->field_type() != MYSQL_TYPE_BIT)
2701 return item->val_str(str);
2705 String *result= item->val_str(str);
2711 char buf_holder[STRING_BUFFER_USUAL_SIZE];
2712 String buf(buf_holder,
sizeof(buf_holder), result->charset());
2713 const CHARSET_INFO *cs= thd->variables.character_set_client;
2719 buf.append(result->charset()->csname);
2720 if (cs->escape_with_backslash_is_dangerous)
2723 buf.append(
" COLLATE '");
2724 buf.append(item->collation.collation->name);