17 #include "my_global.h"
20 #include "sql_parse.h"
27 #include "sql_cache.h"
31 #include "sql_locale.h"
34 #include "sql_delete.h"
35 #include "sql_insert.h"
36 #include "sql_update.h"
37 #include "sql_partition.h"
43 #include "sql_table.h"
48 #include "sql_reload.h"
49 #include "sql_admin.h"
50 #include "sql_connect.h"
54 #include "sql_rename.h"
55 #include "sql_tablespace.h"
67 #include "sql_servers.h"
69 #include "sql_handler.h"
71 #include "sql_binlog.h"
74 #include "rpl_constants.h"
77 #include "rpl_master.h"
78 #include "rpl_filter.h"
82 #include "rpl_handler.h"
88 #include "sql_trigger.h"
89 #include "transaction.h"
90 #include "sql_audit.h"
91 #include "sql_prepare.h"
93 #include "probes_mysql.h"
97 #include "sql_bootstrap.h"
98 #include "opt_explain.h"
99 #include "sql_rewrite.h"
100 #include "global_threads.h"
101 #include "sql_analyse.h"
102 #include "table_cache.h"
108 #define FLAGSTR(V,F) ((V)&(F)?#F" ":"")
116 #define SP_TYPE_STRING(LP) \
117 ((LP)->sphead->m_type == SP_TYPE_FUNCTION ? "FUNCTION" : "PROCEDURE")
118 #define SP_COM_STRING(LP) \
119 ((LP)->sql_command == SQLCOM_CREATE_SPFUNCTION || \
120 (LP)->sql_command == SQLCOM_ALTER_FUNCTION || \
121 (LP)->sql_command == SQLCOM_SHOW_CREATE_FUNC || \
122 (LP)->sql_command == SQLCOM_DROP_FUNCTION ? \
123 "FUNCTION" : "PROCEDURE")
125 static bool execute_sqlcom_select(THD *thd,
TABLE_LIST *all_tables);
127 static void sql_kill(THD *thd, ulong
id,
bool only_kill_query);
128 static bool lock_tables_precheck(THD *thd,
TABLE_LIST *tables);
130 const char *any_db=
"*any*";
133 { C_STRING_WITH_LEN(
"Sleep") },
134 { C_STRING_WITH_LEN(
"Quit") },
135 { C_STRING_WITH_LEN(
"Init DB") },
136 { C_STRING_WITH_LEN(
"Query") },
137 { C_STRING_WITH_LEN(
"Field List") },
138 { C_STRING_WITH_LEN(
"Create DB") },
139 { C_STRING_WITH_LEN(
"Drop DB") },
140 { C_STRING_WITH_LEN(
"Refresh") },
141 { C_STRING_WITH_LEN(
"Shutdown") },
142 { C_STRING_WITH_LEN(
"Statistics") },
143 { C_STRING_WITH_LEN(
"Processlist") },
144 { C_STRING_WITH_LEN(
"Connect") },
145 { C_STRING_WITH_LEN(
"Kill") },
146 { C_STRING_WITH_LEN(
"Debug") },
147 { C_STRING_WITH_LEN(
"Ping") },
148 { C_STRING_WITH_LEN(
"Time") },
149 { C_STRING_WITH_LEN(
"Delayed insert") },
150 { C_STRING_WITH_LEN(
"Change user") },
151 { C_STRING_WITH_LEN(
"Binlog Dump") },
152 { C_STRING_WITH_LEN(
"Table Dump") },
153 { C_STRING_WITH_LEN(
"Connect Out") },
154 { C_STRING_WITH_LEN(
"Register Slave") },
155 { C_STRING_WITH_LEN(
"Prepare") },
156 { C_STRING_WITH_LEN(
"Execute") },
157 { C_STRING_WITH_LEN(
"Long Data") },
158 { C_STRING_WITH_LEN(
"Close stmt") },
159 { C_STRING_WITH_LEN(
"Reset stmt") },
160 { C_STRING_WITH_LEN(
"Set option") },
161 { C_STRING_WITH_LEN(
"Fetch") },
162 { C_STRING_WITH_LEN(
"Daemon") },
163 { C_STRING_WITH_LEN(
"Binlog Dump GTID") },
164 { C_STRING_WITH_LEN(
"Error") }
167 const char *xa_state_names[]={
168 "NON-EXISTING",
"ACTIVE",
"IDLE",
"PREPARED",
"ROLLBACK ONLY"
172 Slow_log_throttle log_throttle_qni(&opt_log_throttle_queries_not_using_indexes,
173 &LOCK_log_throttle_qni,
174 Log_throttle::LOG_THROTTLE_WINDOW_SIZE,
176 "throttle: %10lu 'index "
177 "not used' warning(s) suppressed.");
180 #ifdef HAVE_REPLICATION
184 inline bool all_tables_not_ok(THD *thd,
TABLE_LIST *tables)
186 return rpl_filter->is_on() && tables && !thd->sp_runtime_ctx &&
187 !rpl_filter->tables_ok(thd->db, tables);
206 inline bool db_stmt_db_ok(THD *thd,
char* db)
208 DBUG_ENTER(
"db_stmt_db_ok");
210 if (!thd->slave_thread)
218 bool db_ok= (rpl_filter->get_do_db()->is_empty() &&
219 rpl_filter->get_ignore_db()->is_empty()) ?
220 rpl_filter->db_ok_with_wild_table(db) :
221 rpl_filter->db_ok(db);
228 static bool some_non_temp_table_to_be_updated(THD *thd,
TABLE_LIST *tables)
247 bool stmt_causes_implicit_commit(
const THD *thd, uint mask)
249 const LEX *lex= thd->lex;
251 DBUG_ENTER(
"stmt_causes_implicit_commit");
256 switch (lex->sql_command) {
257 case SQLCOM_DROP_TABLE:
258 skip= lex->drop_temporary;
260 case SQLCOM_ALTER_TABLE:
261 case SQLCOM_CREATE_TABLE:
263 skip= (lex->create_info.options & HA_LEX_CREATE_TMP_TABLE);
265 case SQLCOM_SET_OPTION:
266 skip= lex->autocommit ? FALSE : TRUE;
290 uint server_command_flags[COM_END+1];
292 void init_update_queries(
void)
295 memset(server_command_flags, 0,
sizeof(server_command_flags));
297 server_command_flags[COM_STATISTICS]= CF_SKIP_QUESTIONS;
298 server_command_flags[COM_PING]= CF_SKIP_QUESTIONS;
299 server_command_flags[COM_STMT_PREPARE]= CF_SKIP_QUESTIONS;
300 server_command_flags[COM_STMT_CLOSE]= CF_SKIP_QUESTIONS;
301 server_command_flags[COM_STMT_RESET]= CF_SKIP_QUESTIONS;
304 memset(sql_command_flags, 0,
sizeof(sql_command_flags));
316 sql_command_flags[SQLCOM_CREATE_TABLE]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
317 CF_AUTO_COMMIT_TRANS |
318 CF_CAN_GENERATE_ROW_EVENTS;
319 sql_command_flags[SQLCOM_CREATE_INDEX]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
320 sql_command_flags[SQLCOM_ALTER_TABLE]= CF_CHANGES_DATA | CF_WRITE_LOGS_COMMAND |
321 CF_AUTO_COMMIT_TRANS;
322 sql_command_flags[SQLCOM_TRUNCATE]= CF_CHANGES_DATA | CF_WRITE_LOGS_COMMAND |
323 CF_AUTO_COMMIT_TRANS;
324 sql_command_flags[SQLCOM_DROP_TABLE]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
325 sql_command_flags[SQLCOM_LOAD]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
326 CF_CAN_GENERATE_ROW_EVENTS;
327 sql_command_flags[SQLCOM_CREATE_DB]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
328 sql_command_flags[SQLCOM_DROP_DB]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
329 sql_command_flags[SQLCOM_ALTER_DB_UPGRADE]= CF_AUTO_COMMIT_TRANS;
330 sql_command_flags[SQLCOM_ALTER_DB]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
331 sql_command_flags[SQLCOM_RENAME_TABLE]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
332 sql_command_flags[SQLCOM_DROP_INDEX]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
333 sql_command_flags[SQLCOM_CREATE_VIEW]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
334 CF_AUTO_COMMIT_TRANS;
335 sql_command_flags[SQLCOM_DROP_VIEW]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
336 sql_command_flags[SQLCOM_CREATE_TRIGGER]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
337 sql_command_flags[SQLCOM_DROP_TRIGGER]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
338 sql_command_flags[SQLCOM_CREATE_EVENT]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
339 sql_command_flags[SQLCOM_ALTER_EVENT]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
340 sql_command_flags[SQLCOM_DROP_EVENT]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
342 sql_command_flags[SQLCOM_UPDATE]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
343 CF_CAN_GENERATE_ROW_EVENTS |
346 sql_command_flags[SQLCOM_UPDATE_MULTI]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
347 CF_CAN_GENERATE_ROW_EVENTS |
351 sql_command_flags[SQLCOM_INSERT]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
352 CF_CAN_GENERATE_ROW_EVENTS |
355 sql_command_flags[SQLCOM_INSERT_SELECT]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
356 CF_CAN_GENERATE_ROW_EVENTS |
359 sql_command_flags[SQLCOM_DELETE]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
360 CF_CAN_GENERATE_ROW_EVENTS |
363 sql_command_flags[SQLCOM_DELETE_MULTI]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
364 CF_CAN_GENERATE_ROW_EVENTS |
367 sql_command_flags[SQLCOM_REPLACE]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
368 CF_CAN_GENERATE_ROW_EVENTS |
371 sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
372 CF_CAN_GENERATE_ROW_EVENTS |
375 sql_command_flags[SQLCOM_SELECT]= CF_REEXECUTION_FRAGILE |
376 CF_CAN_GENERATE_ROW_EVENTS |
385 sql_command_flags[SQLCOM_SET_OPTION]= CF_REEXECUTION_FRAGILE |
386 CF_AUTO_COMMIT_TRANS |
387 CF_CAN_GENERATE_ROW_EVENTS |
390 sql_command_flags[SQLCOM_DO]= CF_REEXECUTION_FRAGILE |
391 CF_CAN_GENERATE_ROW_EVENTS |
394 sql_command_flags[SQLCOM_SHOW_STATUS_PROC]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
395 sql_command_flags[SQLCOM_SHOW_STATUS]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
396 sql_command_flags[SQLCOM_SHOW_DATABASES]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
397 sql_command_flags[SQLCOM_SHOW_TRIGGERS]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
398 sql_command_flags[SQLCOM_SHOW_EVENTS]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
399 sql_command_flags[SQLCOM_SHOW_OPEN_TABLES]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
400 sql_command_flags[SQLCOM_SHOW_PLUGINS]= CF_STATUS_COMMAND;
401 sql_command_flags[SQLCOM_SHOW_FIELDS]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
402 sql_command_flags[SQLCOM_SHOW_KEYS]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
403 sql_command_flags[SQLCOM_SHOW_VARIABLES]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
404 sql_command_flags[SQLCOM_SHOW_CHARSETS]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
405 sql_command_flags[SQLCOM_SHOW_COLLATIONS]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
406 sql_command_flags[SQLCOM_SHOW_BINLOGS]= CF_STATUS_COMMAND;
407 sql_command_flags[SQLCOM_SHOW_SLAVE_HOSTS]= CF_STATUS_COMMAND;
408 sql_command_flags[SQLCOM_SHOW_BINLOG_EVENTS]= CF_STATUS_COMMAND;
409 sql_command_flags[SQLCOM_SHOW_STORAGE_ENGINES]= CF_STATUS_COMMAND;
410 sql_command_flags[SQLCOM_SHOW_PRIVILEGES]= CF_STATUS_COMMAND;
411 sql_command_flags[SQLCOM_SHOW_WARNS]= CF_STATUS_COMMAND | CF_DIAGNOSTIC_STMT;
412 sql_command_flags[SQLCOM_SHOW_ERRORS]= CF_STATUS_COMMAND | CF_DIAGNOSTIC_STMT;
413 sql_command_flags[SQLCOM_SHOW_ENGINE_STATUS]= CF_STATUS_COMMAND;
414 sql_command_flags[SQLCOM_SHOW_ENGINE_MUTEX]= CF_STATUS_COMMAND;
415 sql_command_flags[SQLCOM_SHOW_ENGINE_LOGS]= CF_STATUS_COMMAND;
416 sql_command_flags[SQLCOM_SHOW_PROCESSLIST]= CF_STATUS_COMMAND;
417 sql_command_flags[SQLCOM_SHOW_GRANTS]= CF_STATUS_COMMAND;
418 sql_command_flags[SQLCOM_SHOW_CREATE_DB]= CF_STATUS_COMMAND;
419 sql_command_flags[SQLCOM_SHOW_CREATE]= CF_STATUS_COMMAND;
420 sql_command_flags[SQLCOM_SHOW_MASTER_STAT]= CF_STATUS_COMMAND;
421 sql_command_flags[SQLCOM_SHOW_SLAVE_STAT]= CF_STATUS_COMMAND;
422 sql_command_flags[SQLCOM_SHOW_CREATE_PROC]= CF_STATUS_COMMAND;
423 sql_command_flags[SQLCOM_SHOW_CREATE_FUNC]= CF_STATUS_COMMAND;
424 sql_command_flags[SQLCOM_SHOW_CREATE_TRIGGER]= CF_STATUS_COMMAND;
425 sql_command_flags[SQLCOM_SHOW_STATUS_FUNC]= CF_STATUS_COMMAND | CF_REEXECUTION_FRAGILE;
426 sql_command_flags[SQLCOM_SHOW_PROC_CODE]= CF_STATUS_COMMAND;
427 sql_command_flags[SQLCOM_SHOW_FUNC_CODE]= CF_STATUS_COMMAND;
428 sql_command_flags[SQLCOM_SHOW_CREATE_EVENT]= CF_STATUS_COMMAND;
429 sql_command_flags[SQLCOM_SHOW_PROFILES]= CF_STATUS_COMMAND;
430 sql_command_flags[SQLCOM_SHOW_PROFILE]= CF_STATUS_COMMAND;
431 sql_command_flags[SQLCOM_BINLOG_BASE64_EVENT]= CF_STATUS_COMMAND |
432 CF_CAN_GENERATE_ROW_EVENTS;
434 sql_command_flags[SQLCOM_SHOW_TABLES]= (CF_STATUS_COMMAND |
435 CF_SHOW_TABLE_COMMAND |
436 CF_REEXECUTION_FRAGILE);
437 sql_command_flags[SQLCOM_SHOW_TABLE_STATUS]= (CF_STATUS_COMMAND |
438 CF_SHOW_TABLE_COMMAND |
439 CF_REEXECUTION_FRAGILE);
441 sql_command_flags[SQLCOM_CREATE_USER]= CF_CHANGES_DATA;
442 sql_command_flags[SQLCOM_RENAME_USER]= CF_CHANGES_DATA;
443 sql_command_flags[SQLCOM_DROP_USER]= CF_CHANGES_DATA;
444 sql_command_flags[SQLCOM_ALTER_USER]= CF_CHANGES_DATA;
445 sql_command_flags[SQLCOM_GRANT]= CF_CHANGES_DATA;
446 sql_command_flags[SQLCOM_REVOKE]= CF_CHANGES_DATA;
447 sql_command_flags[SQLCOM_REVOKE_ALL]= CF_CHANGES_DATA;
448 sql_command_flags[SQLCOM_OPTIMIZE]= CF_CHANGES_DATA;
449 sql_command_flags[SQLCOM_CREATE_FUNCTION]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
450 sql_command_flags[SQLCOM_CREATE_PROCEDURE]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
451 sql_command_flags[SQLCOM_CREATE_SPFUNCTION]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
452 sql_command_flags[SQLCOM_DROP_PROCEDURE]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
453 sql_command_flags[SQLCOM_DROP_FUNCTION]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
454 sql_command_flags[SQLCOM_ALTER_PROCEDURE]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
455 sql_command_flags[SQLCOM_ALTER_FUNCTION]= CF_CHANGES_DATA | CF_AUTO_COMMIT_TRANS;
456 sql_command_flags[SQLCOM_INSTALL_PLUGIN]= CF_CHANGES_DATA;
457 sql_command_flags[SQLCOM_UNINSTALL_PLUGIN]= CF_CHANGES_DATA;
460 sql_command_flags[SQLCOM_GET_DIAGNOSTICS]= CF_DIAGNOSTIC_STMT;
466 sql_command_flags[SQLCOM_CALL]= CF_REEXECUTION_FRAGILE |
467 CF_CAN_GENERATE_ROW_EVENTS |
469 sql_command_flags[SQLCOM_EXECUTE]= CF_CAN_GENERATE_ROW_EVENTS;
475 sql_command_flags[SQLCOM_REPAIR]= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS;
476 sql_command_flags[SQLCOM_OPTIMIZE]|= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS;
477 sql_command_flags[SQLCOM_ANALYZE]= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS;
478 sql_command_flags[SQLCOM_CHECK]= CF_WRITE_LOGS_COMMAND | CF_AUTO_COMMIT_TRANS;
480 sql_command_flags[SQLCOM_CREATE_USER]|= CF_AUTO_COMMIT_TRANS;
481 sql_command_flags[SQLCOM_DROP_USER]|= CF_AUTO_COMMIT_TRANS;
482 sql_command_flags[SQLCOM_RENAME_USER]|= CF_AUTO_COMMIT_TRANS;
483 sql_command_flags[SQLCOM_ALTER_USER]|= CF_AUTO_COMMIT_TRANS;
484 sql_command_flags[SQLCOM_REVOKE]|= CF_AUTO_COMMIT_TRANS;
485 sql_command_flags[SQLCOM_REVOKE_ALL]|= CF_AUTO_COMMIT_TRANS;
486 sql_command_flags[SQLCOM_GRANT]|= CF_AUTO_COMMIT_TRANS;
488 sql_command_flags[SQLCOM_ASSIGN_TO_KEYCACHE]= CF_AUTO_COMMIT_TRANS;
489 sql_command_flags[SQLCOM_PRELOAD_KEYS]= CF_AUTO_COMMIT_TRANS;
491 sql_command_flags[SQLCOM_FLUSH]= CF_AUTO_COMMIT_TRANS;
492 sql_command_flags[SQLCOM_RESET]= CF_AUTO_COMMIT_TRANS;
493 sql_command_flags[SQLCOM_CREATE_SERVER]= CF_AUTO_COMMIT_TRANS;
494 sql_command_flags[SQLCOM_ALTER_SERVER]= CF_AUTO_COMMIT_TRANS;
495 sql_command_flags[SQLCOM_DROP_SERVER]= CF_AUTO_COMMIT_TRANS;
496 sql_command_flags[SQLCOM_CHANGE_MASTER]= CF_AUTO_COMMIT_TRANS;
497 sql_command_flags[SQLCOM_SLAVE_START]= CF_AUTO_COMMIT_TRANS;
498 sql_command_flags[SQLCOM_SLAVE_STOP]= CF_AUTO_COMMIT_TRANS;
509 sql_command_flags[SQLCOM_CREATE_TABLE]|= CF_PREOPEN_TMP_TABLES;
510 sql_command_flags[SQLCOM_DROP_TABLE]|= CF_PREOPEN_TMP_TABLES;
511 sql_command_flags[SQLCOM_CREATE_INDEX]|= CF_PREOPEN_TMP_TABLES;
512 sql_command_flags[SQLCOM_ALTER_TABLE]|= CF_PREOPEN_TMP_TABLES;
513 sql_command_flags[SQLCOM_TRUNCATE]|= CF_PREOPEN_TMP_TABLES;
514 sql_command_flags[SQLCOM_LOAD]|= CF_PREOPEN_TMP_TABLES;
515 sql_command_flags[SQLCOM_DROP_INDEX]|= CF_PREOPEN_TMP_TABLES;
516 sql_command_flags[SQLCOM_UPDATE]|= CF_PREOPEN_TMP_TABLES;
517 sql_command_flags[SQLCOM_UPDATE_MULTI]|= CF_PREOPEN_TMP_TABLES;
518 sql_command_flags[SQLCOM_INSERT_SELECT]|= CF_PREOPEN_TMP_TABLES;
519 sql_command_flags[SQLCOM_DELETE]|= CF_PREOPEN_TMP_TABLES;
520 sql_command_flags[SQLCOM_DELETE_MULTI]|= CF_PREOPEN_TMP_TABLES;
521 sql_command_flags[SQLCOM_REPLACE_SELECT]|= CF_PREOPEN_TMP_TABLES;
522 sql_command_flags[SQLCOM_SELECT]|= CF_PREOPEN_TMP_TABLES;
523 sql_command_flags[SQLCOM_SET_OPTION]|= CF_PREOPEN_TMP_TABLES;
524 sql_command_flags[SQLCOM_DO]|= CF_PREOPEN_TMP_TABLES;
525 sql_command_flags[SQLCOM_CALL]|= CF_PREOPEN_TMP_TABLES;
526 sql_command_flags[SQLCOM_CHECKSUM]|= CF_PREOPEN_TMP_TABLES;
527 sql_command_flags[SQLCOM_ANALYZE]|= CF_PREOPEN_TMP_TABLES;
528 sql_command_flags[SQLCOM_CHECK]|= CF_PREOPEN_TMP_TABLES;
529 sql_command_flags[SQLCOM_OPTIMIZE]|= CF_PREOPEN_TMP_TABLES;
530 sql_command_flags[SQLCOM_REPAIR]|= CF_PREOPEN_TMP_TABLES;
531 sql_command_flags[SQLCOM_PRELOAD_KEYS]|= CF_PREOPEN_TMP_TABLES;
532 sql_command_flags[SQLCOM_ASSIGN_TO_KEYCACHE]|= CF_PREOPEN_TMP_TABLES;
540 sql_command_flags[SQLCOM_CREATE_TABLE]|= CF_HA_CLOSE;
541 sql_command_flags[SQLCOM_DROP_TABLE]|= CF_HA_CLOSE;
542 sql_command_flags[SQLCOM_ALTER_TABLE]|= CF_HA_CLOSE;
543 sql_command_flags[SQLCOM_TRUNCATE]|= CF_HA_CLOSE;
544 sql_command_flags[SQLCOM_REPAIR]|= CF_HA_CLOSE;
545 sql_command_flags[SQLCOM_OPTIMIZE]|= CF_HA_CLOSE;
546 sql_command_flags[SQLCOM_ANALYZE]|= CF_HA_CLOSE;
547 sql_command_flags[SQLCOM_CHECK]|= CF_HA_CLOSE;
548 sql_command_flags[SQLCOM_CREATE_INDEX]|= CF_HA_CLOSE;
549 sql_command_flags[SQLCOM_DROP_INDEX]|= CF_HA_CLOSE;
550 sql_command_flags[SQLCOM_PRELOAD_KEYS]|= CF_HA_CLOSE;
551 sql_command_flags[SQLCOM_ASSIGN_TO_KEYCACHE]|= CF_HA_CLOSE;
558 sql_command_flags[SQLCOM_CREATE_TABLE]|= CF_DISALLOW_IN_RO_TRANS;
559 sql_command_flags[SQLCOM_ALTER_TABLE]|= CF_DISALLOW_IN_RO_TRANS;
560 sql_command_flags[SQLCOM_DROP_TABLE]|= CF_DISALLOW_IN_RO_TRANS;
561 sql_command_flags[SQLCOM_RENAME_TABLE]|= CF_DISALLOW_IN_RO_TRANS;
562 sql_command_flags[SQLCOM_CREATE_INDEX]|= CF_DISALLOW_IN_RO_TRANS;
563 sql_command_flags[SQLCOM_DROP_INDEX]|= CF_DISALLOW_IN_RO_TRANS;
564 sql_command_flags[SQLCOM_CREATE_DB]|= CF_DISALLOW_IN_RO_TRANS;
565 sql_command_flags[SQLCOM_DROP_DB]|= CF_DISALLOW_IN_RO_TRANS;
566 sql_command_flags[SQLCOM_ALTER_DB_UPGRADE]|= CF_DISALLOW_IN_RO_TRANS;
567 sql_command_flags[SQLCOM_ALTER_DB]|= CF_DISALLOW_IN_RO_TRANS;
568 sql_command_flags[SQLCOM_CREATE_VIEW]|= CF_DISALLOW_IN_RO_TRANS;
569 sql_command_flags[SQLCOM_DROP_VIEW]|= CF_DISALLOW_IN_RO_TRANS;
570 sql_command_flags[SQLCOM_CREATE_TRIGGER]|= CF_DISALLOW_IN_RO_TRANS;
571 sql_command_flags[SQLCOM_DROP_TRIGGER]|= CF_DISALLOW_IN_RO_TRANS;
572 sql_command_flags[SQLCOM_CREATE_EVENT]|= CF_DISALLOW_IN_RO_TRANS;
573 sql_command_flags[SQLCOM_ALTER_EVENT]|= CF_DISALLOW_IN_RO_TRANS;
574 sql_command_flags[SQLCOM_DROP_EVENT]|= CF_DISALLOW_IN_RO_TRANS;
575 sql_command_flags[SQLCOM_CREATE_USER]|= CF_DISALLOW_IN_RO_TRANS;
576 sql_command_flags[SQLCOM_RENAME_USER]|= CF_DISALLOW_IN_RO_TRANS;
577 sql_command_flags[SQLCOM_ALTER_USER]|= CF_DISALLOW_IN_RO_TRANS;
578 sql_command_flags[SQLCOM_DROP_USER]|= CF_DISALLOW_IN_RO_TRANS;
579 sql_command_flags[SQLCOM_CREATE_SERVER]|= CF_DISALLOW_IN_RO_TRANS;
580 sql_command_flags[SQLCOM_ALTER_SERVER]|= CF_DISALLOW_IN_RO_TRANS;
581 sql_command_flags[SQLCOM_DROP_SERVER]|= CF_DISALLOW_IN_RO_TRANS;
582 sql_command_flags[SQLCOM_CREATE_FUNCTION]|= CF_DISALLOW_IN_RO_TRANS;
583 sql_command_flags[SQLCOM_CREATE_PROCEDURE]|= CF_DISALLOW_IN_RO_TRANS;
584 sql_command_flags[SQLCOM_CREATE_SPFUNCTION]|=CF_DISALLOW_IN_RO_TRANS;
585 sql_command_flags[SQLCOM_DROP_PROCEDURE]|= CF_DISALLOW_IN_RO_TRANS;
586 sql_command_flags[SQLCOM_DROP_FUNCTION]|= CF_DISALLOW_IN_RO_TRANS;
587 sql_command_flags[SQLCOM_ALTER_PROCEDURE]|= CF_DISALLOW_IN_RO_TRANS;
588 sql_command_flags[SQLCOM_ALTER_FUNCTION]|= CF_DISALLOW_IN_RO_TRANS;
589 sql_command_flags[SQLCOM_TRUNCATE]|= CF_DISALLOW_IN_RO_TRANS;
590 sql_command_flags[SQLCOM_ALTER_TABLESPACE]|= CF_DISALLOW_IN_RO_TRANS;
591 sql_command_flags[SQLCOM_REPAIR]|= CF_DISALLOW_IN_RO_TRANS;
592 sql_command_flags[SQLCOM_OPTIMIZE]|= CF_DISALLOW_IN_RO_TRANS;
593 sql_command_flags[SQLCOM_GRANT]|= CF_DISALLOW_IN_RO_TRANS;
594 sql_command_flags[SQLCOM_REVOKE]|= CF_DISALLOW_IN_RO_TRANS;
595 sql_command_flags[SQLCOM_REVOKE_ALL]|= CF_DISALLOW_IN_RO_TRANS;
596 sql_command_flags[SQLCOM_INSTALL_PLUGIN]|= CF_DISALLOW_IN_RO_TRANS;
597 sql_command_flags[SQLCOM_UNINSTALL_PLUGIN]|= CF_DISALLOW_IN_RO_TRANS;
600 bool sqlcom_can_generate_row_events(
const THD *thd)
602 return (sql_command_flags[thd->lex->sql_command] &
603 CF_CAN_GENERATE_ROW_EVENTS);
606 bool is_update_query(
enum enum_sql_command command)
608 DBUG_ASSERT(command >= 0 && command <= SQLCOM_END);
609 return (sql_command_flags[command] & CF_CHANGES_DATA) != 0;
613 bool is_explainable_query(
enum enum_sql_command command)
615 DBUG_ASSERT(command >= 0 && command <= SQLCOM_END);
616 return (sql_command_flags[command] & CF_CAN_BE_EXPLAINED) != 0;
626 DBUG_ASSERT(command >= 0 && command <= SQLCOM_END);
627 return (sql_command_flags[command] & CF_WRITE_LOGS_COMMAND) != 0;
630 void execute_init_command(THD *thd,
LEX_STRING *init_command,
634 ulong save_client_capabilities;
637 if (!init_command->length)
648 size_t len= init_command->length;
649 char *
buf= thd->strmake(init_command->str, len);
652 #if defined(ENABLED_PROFILING)
653 thd->profiling.start_new_query();
654 thd->profiling.set_query_source(buf, len);
657 THD_STAGE_INFO(thd, stage_execution_of_init_command);
658 save_client_capabilities= thd->client_capabilities;
659 thd->client_capabilities|= CLIENT_MULTI_QUERIES;
664 save_vio= thd->net.vio;
667 thd->client_capabilities= save_client_capabilities;
668 thd->net.vio= save_vio;
670 #if defined(ENABLED_PROFILING)
671 thd->profiling.finish_current_query();
675 static char *fgets_fn(
char *buffer,
size_t size, fgets_input_t input,
int *error)
680 *error= (line == NULL) ? ferror(in->
m_file) : 0;
684 static void handle_bootstrap_impl(THD *thd)
687 char buffer[MAX_BOOTSTRAP_QUERY_SIZE];
693 DBUG_ENTER(
"handle_bootstrap");
695 #ifndef EMBEDDED_LIBRARY
696 pthread_detach_this_thread();
697 thd->thread_stack= (
char*) &thd;
700 thd->security_ctx->user= (
char*) my_strdup(
"boot", MYF(MY_WME));
701 thd->security_ctx->priv_user[0]= thd->security_ctx->priv_host[0]=0;
707 thd->client_capabilities|= CLIENT_MULTI_RESULTS;
709 thd->init_for_queries();
715 rc= read_bootstrap_query(buffer, &length, file, fgets_fn, &error);
717 if (rc == READ_BOOTSTRAP_EOF)
723 if (rc != READ_BOOTSTRAP_SUCCESS)
729 thd->get_stmt_da()->reset_diagnostics_area();
732 char *err_ptr= buffer + (length <= MAX_BOOTSTRAP_ERROR_LEN ?
733 0 : (length - MAX_BOOTSTRAP_ERROR_LEN));
736 case READ_BOOTSTRAP_ERROR:
737 my_printf_error(ER_UNKNOWN_ERROR,
"Bootstrap file error, return code (%d). "
738 "Nearest query: '%s'", MYF(0), error, err_ptr);
741 case READ_BOOTSTRAP_QUERY_SIZE:
742 my_printf_error(ER_UNKNOWN_ERROR,
"Boostrap file error. Query size "
743 "exceeded %d bytes near '%s'.", MYF(0),
744 MAX_BOOTSTRAP_LINE_SIZE, err_ptr);
752 thd->protocol->end_statement();
757 query= (
char *) thd->memdup_w_gap(buffer, length + 1,
759 QUERY_CACHE_FLAGS_SIZE);
761 memcpy(query + length + 1, (
char *) &db_len,
sizeof(
size_t));
762 thd->set_query_and_id(query, length, thd->charset(), next_query_id());
763 DBUG_PRINT(
"query",(
"%-.4096s",thd->query()));
764 #if defined(ENABLED_PROFILING)
765 thd->profiling.start_new_query();
766 thd->profiling.set_query_source(thd->query(), length);
774 Parser_state parser_state;
775 if (parser_state.init(thd, thd->query(), length))
777 thd->protocol->end_statement();
782 mysql_parse(thd, thd->query(), length, &parser_state);
784 bootstrap_error= thd->is_error();
785 thd->protocol->end_statement();
787 #if defined(ENABLED_PROFILING)
788 thd->profiling.finish_current_query();
794 free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
795 free_root(&thd->transaction.mem_root,MYF(MY_KEEP_PREALLOC));
814 do_handle_bootstrap(thd);
818 void do_handle_bootstrap(THD *thd)
820 bool thd_added=
false;
822 thd->thread_stack= (
char*) &thd;
823 if (my_thread_init() || thd->store_globals())
825 #ifndef EMBEDDED_LIBRARY
826 close_connection(thd, ER_OUT_OF_RESOURCES);
834 add_global_thread(thd);
837 handle_bootstrap_impl(thd);
841 thd->release_resources();
846 remove_global_thread(thd);
860 #ifndef EMBEDDED_LIBRARY
871 void free_items(
Item *item)
874 DBUG_ENTER(
"free_items");
875 for (; item ; item=next)
889 DBUG_ENTER(
"cleanup_items");
890 for (; item ; item=item->
next)
895 #ifndef EMBEDDED_LIBRARY
915 enum enum_server_command command;
917 DBUG_ENTER(
"do_command");
923 thd->lex->current_select= 0;
931 my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
938 thd->get_stmt_da()->reset_diagnostics_area();
940 net_new_transaction(net);
956 DEBUG_SYNC(thd,
"before_do_command_net_read");
969 thd->m_server_idle=
true;
971 thd->m_server_idle=
false;
973 if (packet_length == packet_error)
975 DBUG_PRINT(
"info",(
"Got error %d reading command from socket %s",
977 vio_description(net->vio)));
980 thd->m_statement_psi= MYSQL_REFINE_STATEMENT(thd->m_statement_psi,
981 com_statement_info[COM_END].m_key);
986 DBUG_ASSERT(thd->is_error());
987 thd->protocol->end_statement();
990 MYSQL_END_STATEMENT(thd->m_statement_psi, thd->get_stmt_da());
991 thd->m_statement_psi= NULL;
1000 return_value= FALSE;
1004 packet= (
char*) net->read_pos;
1013 if (packet_length == 0)
1016 packet[0]= (uchar) COM_SLEEP;
1020 packet[packet_length]=
'\0';
1022 command= (
enum enum_server_command) (uchar) packet[0];
1024 if (command >= COM_END)
1027 DBUG_PRINT(
"info",(
"Command on %s = %d (%s)",
1028 vio_description(net->vio), command,
1029 command_name[command].str));
1032 my_net_set_read_timeout(net, thd->variables.net_read_timeout);
1034 DBUG_ASSERT(packet_length);
1036 return_value=
dispatch_command(command, thd, packet+1, (uint) (packet_length-1));
1040 DBUG_ASSERT(thd->m_statement_psi == NULL);
1041 DBUG_RETURN(return_value);
1059 static my_bool deny_updates_if_read_only_option(THD *thd,
1062 DBUG_ENTER(
"deny_updates_if_read_only_option");
1069 const my_bool user_is_super=
1070 ((ulong)(thd->security_ctx->master_access & SUPER_ACL) ==
1076 if (!(sql_command_flags[lex->sql_command] & CF_CHANGES_DATA))
1080 if (lex->sql_command == SQLCOM_UPDATE_MULTI)
1083 const my_bool create_temp_tables=
1084 (lex->sql_command == SQLCOM_CREATE_TABLE) &&
1085 (lex->create_info.options & HA_LEX_CREATE_TMP_TABLE);
1087 const my_bool drop_temp_tables=
1088 (lex->sql_command == SQLCOM_DROP_TABLE) &&
1089 lex->drop_temporary;
1091 const my_bool update_real_tables=
1092 some_non_temp_table_to_be_updated(thd, all_tables) &&
1093 !(create_temp_tables || drop_temp_tables);
1096 const my_bool create_or_drop_databases=
1097 (lex->sql_command == SQLCOM_CREATE_DB) ||
1098 (lex->sql_command == SQLCOM_DROP_DB);
1100 if (update_real_tables || create_or_drop_databases)
1135 char* packet, uint packet_length)
1137 NET *net= &thd->net;
1139 DBUG_ENTER(
"dispatch_command");
1140 DBUG_PRINT(
"info",(
"packet: '%*.s'; command: %d", packet_length, packet, command));
1143 #if defined(ENABLED_PROFILING)
1144 thd->profiling.start_new_query();
1148 MYSQL_COMMAND_START(thd->thread_id, command,
1149 &thd->security_ctx->priv_user[0],
1150 (
char *) thd->security_ctx->host_or_ip);
1153 thd->m_statement_psi= MYSQL_REFINE_STATEMENT(thd->m_statement_psi,
1154 com_statement_info[command].m_key);
1156 thd->set_command(command);
1161 thd->enable_slow_log= TRUE;
1162 thd->lex->sql_command= SQLCOM_END;
1164 if (!thd->is_valid_time())
1173 thd->security_ctx->master_access|= SHUTDOWN_ACL;
1174 command= COM_SHUTDOWN;
1176 thd->set_query_id(next_query_id());
1177 inc_thread_running();
1179 if (!(server_command_flags[command] & CF_SKIP_QUESTIONS))
1180 statistic_increment(thd->status_var.questions, &LOCK_status);
1186 thd->server_status&= ~SERVER_STATUS_CLEAR_SET;
1198 if (unlikely(thd->security_ctx->password_expired &&
1199 command != COM_QUERY &&
1200 command != COM_STMT_CLOSE &&
1201 command != COM_STMT_SEND_LONG_DATA &&
1202 command != COM_PING &&
1203 command != COM_QUIT))
1205 my_error(ER_MUST_CHANGE_PASSWORD, MYF(0));
1213 status_var_increment(thd->status_var.com_stat[SQLCOM_CHANGE_DB]);
1214 thd->convert_string(&tmp, system_charset_info,
1215 packet, packet_length, thd->charset());
1216 if (!mysql_change_db(thd, &tmp, FALSE))
1218 general_log_write(thd, command, thd->db, thd->db_length);
1223 #ifdef HAVE_REPLICATION
1224 case COM_REGISTER_SLAVE:
1226 if (!register_slave(thd, (uchar*)packet, packet_length))
1231 case COM_CHANGE_USER:
1234 status_var_increment(thd->status_var.com_other);
1240 net->read_pos= (uchar*)packet;
1243 const_cast<USER_CONN*
>(thd->get_user_connect());
1244 char *save_db= thd->db;
1245 uint save_db_length= thd->db_length;
1246 Security_context save_security_ctx= *thd->security_ctx;
1248 auth_rc= acl_authenticate(thd, packet_length);
1249 MYSQL_AUDIT_NOTIFY_CONNECTION_CHANGE_USER(thd);
1252 my_free(thd->security_ctx->user);
1253 *thd->security_ctx= save_security_ctx;
1254 thd->set_user_connect(save_user_connect);
1255 thd->reset_db(save_db, save_db_length);
1257 my_error(ER_ACCESS_DENIED_CHANGE_USER_ERROR, MYF(0),
1258 thd->security_ctx->user,
1259 thd->security_ctx->host_or_ip,
1260 (thd->password ? ER(ER_YES) : ER(ER_NO)));
1261 thd->killed= THD::KILL_CONNECTION;
1266 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1268 if (save_user_connect)
1269 decrease_user_connections(save_user_connect);
1274 my_free(save_security_ctx.user);
1279 case COM_STMT_EXECUTE:
1284 case COM_STMT_FETCH:
1289 case COM_STMT_SEND_LONG_DATA:
1294 case COM_STMT_PREPARE:
1299 case COM_STMT_CLOSE:
1304 case COM_STMT_RESET:
1313 MYSQL_QUERY_START(thd->query(), thd->thread_id,
1314 (
char *) (thd->db ? thd->db :
""),
1315 &thd->security_ctx->priv_user[0],
1316 (
char *) thd->security_ctx->host_or_ip);
1317 char *packet_end= thd->query() + thd->query_length();
1320 general_log_write(thd, command, thd->query(), thd->query_length());
1322 DBUG_PRINT(
"query",(
"%-.4096s",thd->query()));
1324 #if defined(ENABLED_PROFILING)
1325 thd->profiling.set_query_source(thd->query(), thd->query_length());
1328 MYSQL_SET_STATEMENT_TEXT(thd->m_statement_psi, thd->query(), thd->query_length());
1330 Parser_state parser_state;
1331 if (parser_state.init(thd, thd->query(), thd->query_length()))
1334 mysql_parse(thd, thd->query(), thd->query_length(), &parser_state);
1336 while (!thd->killed && (parser_state.m_lip.found_semicolon != NULL) &&
1342 char *beginning_of_next_stmt= (
char*) parser_state.m_lip.found_semicolon;
1345 thd->update_server_status();
1346 thd->protocol->end_statement();
1347 query_cache_end_of_result(thd);
1349 mysql_audit_general(thd, MYSQL_AUDIT_GENERAL_STATUS,
1350 thd->get_stmt_da()->is_error() ?
1351 thd->get_stmt_da()->sql_errno() : 0,
1352 command_name[command].str);
1354 ulong length= (ulong)(packet_end - beginning_of_next_stmt);
1359 while (length > 0 && my_isspace(thd->charset(), *beginning_of_next_stmt))
1361 beginning_of_next_stmt++;
1366 MYSQL_END_STATEMENT(thd->m_statement_psi, thd->get_stmt_da());
1367 thd->m_statement_psi= NULL;
1370 if (MYSQL_QUERY_DONE_ENABLED())
1372 MYSQL_QUERY_DONE(thd->is_error());
1376 #if defined(ENABLED_PROFILING)
1377 thd->profiling.finish_current_query();
1381 #if defined(ENABLED_PROFILING)
1382 thd->profiling.start_new_query(
"continuing");
1383 thd->profiling.set_query_source(beginning_of_next_stmt, length);
1387 MYSQL_QUERY_START(beginning_of_next_stmt, thd->thread_id,
1388 (
char *) (thd->db ? thd->db :
""),
1389 &thd->security_ctx->priv_user[0],
1390 (
char *) thd->security_ctx->host_or_ip);
1393 thd->m_statement_psi= MYSQL_START_STATEMENT(&thd->m_statement_state,
1394 com_statement_info[command].m_key,
1395 thd->db, thd->db_length,
1397 THD_STAGE_INFO(thd, stage_init);
1398 MYSQL_SET_STATEMENT_TEXT(thd->m_statement_psi, beginning_of_next_stmt, length);
1400 thd->set_query_and_id(beginning_of_next_stmt, length,
1401 thd->charset(), next_query_id());
1405 statistic_increment(thd->status_var.questions, &LOCK_status);
1407 parser_state.reset(beginning_of_next_stmt, length);
1409 mysql_parse(thd, beginning_of_next_stmt, length, &parser_state);
1412 DBUG_PRINT(
"info",(
"query ready"));
1415 case COM_FIELD_LIST:
1416 #ifdef DONT_ALLOW_SHOW_COMMANDS
1417 my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND),
1422 char *fields, *packet_end= packet + packet_length, *arg_end;
1431 MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
1433 status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_FIELDS]);
1434 if (thd->copy_db_to(&db.str, &db.length))
1439 arg_end= strend(packet);
1440 uint arg_length= arg_end - packet;
1443 if (arg_length >= packet_length || arg_length > NAME_LEN)
1445 my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
1448 thd->convert_string(&table_name, system_charset_info,
1449 packet, arg_length, thd->charset());
1450 enum_ident_name_check ident_check_status=
1451 check_table_name(table_name.str, table_name.length, FALSE);
1452 if (ident_check_status == IDENT_NAME_WRONG)
1455 my_error(ER_WRONG_TABLE_NAME, MYF(0), table_name.str);
1458 else if (ident_check_status == IDENT_NAME_TOO_LONG)
1460 my_error(ER_TOO_LONG_IDENT, MYF(0), table_name.str);
1463 packet= arg_end + 1;
1467 if (lower_case_table_names)
1468 table_name.length= my_casedn_str(files_charset_info, table_name.str);
1470 table_name.length, table_name.str, TL_READ);
1475 table_list.select_lex= &(thd->lex->select_lex);
1477 select_lex.table_list.link_in_list(&table_list,
1478 &table_list.next_local);
1479 thd->lex->add_to_query_tables(&table_list);
1481 if (is_infoschema_db(table_list.db, table_list.db_length))
1483 ST_SCHEMA_TABLE *schema_table= find_schema_table(thd, table_list.alias);
1485 table_list.schema_table= schema_table;
1488 uint query_length= (uint) (packet_end - packet);
1489 if (!(fields= (
char *) thd->memdup(packet, query_length + 1)))
1491 thd->set_query(fields, query_length);
1492 general_log_print(thd, command,
"%s %s", table_list.table_name, fields);
1498 TRUE, UINT_MAX, FALSE))
1504 thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
1508 NULL, 0, NULL, NULL);
1510 mysqld_list_fields(thd,&table_list,fields);
1512 thd->lex->unit.cleanup();
1514 DBUG_ASSERT(thd->transaction.stmt.is_empty());
1515 close_thread_tables(thd);
1516 thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
1518 if (thd->transaction_rollback_request)
1526 trans_rollback_implicit(thd);
1527 thd->mdl_context.release_transactional_locks();
1530 thd->cleanup_after_query();
1536 general_log_print(thd, command, NullS);
1538 thd->get_stmt_da()->disable_status();
1541 #ifndef EMBEDDED_LIBRARY
1542 case COM_BINLOG_DUMP_GTID:
1543 error= com_binlog_dump_gtid(thd, packet, packet_length);
1545 case COM_BINLOG_DUMP:
1546 error= com_binlog_dump(thd, packet, packet_length);
1553 if (packet_length < 1)
1555 my_error(ER_MALFORMED_PACKET, MYF(0));
1566 status_var_increment(thd->status_var.com_stat[SQLCOM_FLUSH]);
1567 ulong options= (ulong) (uchar) packet[0];
1568 if (trans_commit_implicit(thd))
1570 thd->mdl_context.release_transactional_locks();
1573 general_log_print(thd, command, NullS);
1575 bool debug_simulate= FALSE;
1576 DBUG_EXECUTE_IF(
"simulate_detached_thread_refresh", debug_simulate= TRUE;);
1586 my_pthread_setspecific_ptr(THR_THD, NULL);
1587 res= reload_acl_and_cache(NULL, options | REFRESH_FAST,
1589 my_pthread_setspecific_ptr(THR_THD, thd);
1595 if (reload_acl_and_cache(thd, options, (
TABLE_LIST*) 0, ¬_used))
1597 if (trans_commit_implicit(thd))
1599 close_thread_tables(thd);
1600 thd->mdl_context.release_transactional_locks();
1604 #ifndef EMBEDDED_LIBRARY
1607 if (packet_length < 1)
1609 my_error(ER_MALFORMED_PACKET, MYF(0));
1612 status_var_increment(thd->status_var.com_other);
1621 enum mysql_enum_shutdown_level
level;
1622 if (!thd->is_valid_time())
1623 level= SHUTDOWN_DEFAULT;
1625 level= (
enum mysql_enum_shutdown_level) (uchar) packet[0];
1626 if (level == SHUTDOWN_DEFAULT)
1627 level= SHUTDOWN_WAIT_ALL_BUFFERS;
1628 else if (level != SHUTDOWN_WAIT_ALL_BUFFERS)
1630 my_error(ER_NOT_SUPPORTED_YET, MYF(0),
"this shutdown level");
1633 DBUG_PRINT(
"quit",(
"Got shutdown command for level %u", level));
1634 general_log_print(thd, command, NullS);
1641 case COM_STATISTICS:
1645 uint length __attribute__((unused));
1646 ulonglong queries_per_second1000;
1648 uint buff_len=
sizeof(buff);
1650 general_log_print(thd, command, NullS);
1651 status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_STATUS]);
1652 calc_sum_of_all_status(¤t_global_status_var);
1653 if (!(uptime= (ulong) (thd->start_time.tv_sec - server_start_time)))
1654 queries_per_second1000= 0;
1656 queries_per_second1000= thd->query_id * LL(1000) / uptime;
1658 length= my_snprintf(buff, buff_len - 1,
1659 "Uptime: %lu Threads: %d Questions: %lu "
1660 "Slow queries: %llu Opens: %llu Flush tables: %lu "
1661 "Open tables: %u Queries per second avg: %u.%03u",
1663 (
int) get_thread_count(), (ulong) thd->query_id,
1664 current_global_status_var.long_query_count,
1665 current_global_status_var.opened_tables,
1668 (uint) (queries_per_second1000 / 1000),
1669 (uint) (queries_per_second1000 % 1000));
1670 #ifdef EMBEDDED_LIBRARY
1672 my_ok(thd, 0, 0, buff);
1676 thd->get_stmt_da()->disable_status();
1681 status_var_increment(thd->status_var.com_other);
1684 case COM_PROCESS_INFO:
1685 status_var_increment(thd->status_var.com_stat[SQLCOM_SHOW_PROCESSLIST]);
1686 if (!thd->security_ctx->priv_user[0] &&
1689 general_log_print(thd, command, NullS);
1690 mysqld_list_processes(thd,
1691 thd->security_ctx->master_access & PROCESS_ACL ?
1692 NullS : thd->security_ctx->priv_user, 0);
1694 case COM_PROCESS_KILL:
1696 if (thread_id & (~0xfffffffful))
1697 my_error(ER_DATA_OUT_OF_RANGE, MYF(0),
"thread_id",
"mysql_kill()");
1698 else if (packet_length < 4)
1699 my_error(ER_MALFORMED_PACKET, MYF(0));
1702 status_var_increment(thd->status_var.com_stat[SQLCOM_KILL]);
1703 ulong
id=(ulong) uint4korr(packet);
1704 sql_kill(thd,
id,
false);
1708 case COM_SET_OPTION:
1711 if (packet_length < 2)
1713 my_error(ER_MALFORMED_PACKET, MYF(0));
1716 status_var_increment(thd->status_var.com_stat[SQLCOM_SET_OPTION]);
1717 uint opt_command= uint2korr(packet);
1719 switch (opt_command) {
1720 case (
int) MYSQL_OPTION_MULTI_STATEMENTS_ON:
1721 thd->client_capabilities|= CLIENT_MULTI_STATEMENTS;
1724 case (
int) MYSQL_OPTION_MULTI_STATEMENTS_OFF:
1725 thd->client_capabilities&= ~CLIENT_MULTI_STATEMENTS;
1729 my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
1735 status_var_increment(thd->status_var.com_other);
1738 mysql_print_status();
1739 general_log_print(thd, command, NullS);
1745 case COM_DELAYED_INSERT:
1748 my_message(ER_UNKNOWN_COM_ERROR, ER(ER_UNKNOWN_COM_ERROR), MYF(0));
1753 DBUG_ASSERT(thd->derived_tables == NULL &&
1754 (thd->open_tables == NULL ||
1755 (thd->locked_tables_mode == LTM_LOCK_TABLES)));
1758 thd->update_server_status();
1760 thd->send_kill_message();
1761 thd->protocol->end_statement();
1762 query_cache_end_of_result(thd);
1764 if (!thd->is_error() && !thd->killed_errno())
1765 mysql_audit_general(thd, MYSQL_AUDIT_GENERAL_RESULT, 0, 0);
1767 mysql_audit_general(thd, MYSQL_AUDIT_GENERAL_STATUS,
1768 thd->get_stmt_da()->is_error() ?
1769 thd->get_stmt_da()->sql_errno() : 0,
1770 command_name[command].str);
1774 THD_STAGE_INFO(thd, stage_cleaning_up);
1777 thd->set_command(COM_SLEEP);
1780 MYSQL_END_STATEMENT(thd->m_statement_psi, thd->get_stmt_da());
1781 thd->m_statement_psi= NULL;
1783 dec_thread_running();
1784 thd->packet.shrink(thd->variables.net_buffer_length);
1785 free_root(thd->mem_root,MYF(MY_KEEP_PREALLOC));
1788 if (MYSQL_QUERY_DONE_ENABLED() || MYSQL_COMMAND_DONE_ENABLED())
1790 int res __attribute__((unused));
1791 res= (int) thd->is_error();
1792 if (command == COM_QUERY)
1794 MYSQL_QUERY_DONE(res);
1796 MYSQL_COMMAND_DONE(res);
1800 #if defined(ENABLED_PROFILING)
1801 thd->profiling.finish_current_query();
1823 DBUG_ENTER(
"log_slow_applicable");
1830 if (unlikely(thd->in_sub_stmt))
1837 if (thd->enable_slow_log)
1839 bool warn_no_index= ((thd->server_status &
1840 (SERVER_QUERY_NO_INDEX_USED |
1841 SERVER_QUERY_NO_GOOD_INDEX_USED)) &&
1842 opt_log_queries_not_using_indexes &&
1843 !(sql_command_flags[thd->lex->sql_command] &
1844 CF_STATUS_COMMAND));
1845 bool log_this_query= ((thd->server_status & SERVER_QUERY_WAS_SLOW) ||
1847 (thd->get_examined_row_count() >=
1848 thd->variables.min_examined_row_limit);
1849 bool suppress_logging= log_throttle_qni.log(thd, warn_no_index);
1851 if (!suppress_logging && log_this_query)
1867 DBUG_ENTER(
"log_slow_do");
1869 THD_STAGE_INFO(thd, stage_logging_slow_query);
1870 thd->status_var.long_query_count++;
1872 if (thd->rewritten_query.length())
1874 thd->rewritten_query.c_ptr_safe(),
1875 thd->rewritten_query.length());
1877 slow_log_print(thd, thd->query(), thd->query_length());
1898 DBUG_ENTER(
"log_slow_statement");
1934 enum enum_schema_tables schema_table_idx)
1936 SELECT_LEX *schema_select_lex= NULL;
1937 DBUG_ENTER(
"prepare_schema_table");
1939 switch (schema_table_idx) {
1941 #if defined(DONT_ALLOW_SHOW_COMMANDS)
1942 my_message(ER_NOT_ALLOWED_COMMAND,
1943 ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
1949 case SCH_TABLE_NAMES:
1954 #ifdef DONT_ALLOW_SHOW_COMMANDS
1955 my_message(ER_NOT_ALLOWED_COMMAND,
1956 ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
1962 if (lex->select_lex.db == NULL &&
1963 lex->copy_db_to(&lex->select_lex.db, &dummy))
1967 schema_select_lex=
new SELECT_LEX();
1968 db.str= schema_select_lex->db= lex->select_lex.db;
1969 schema_select_lex->table_list.first= NULL;
1970 db.length= strlen(db.str);
1972 if (check_and_convert_db_name(&db, FALSE) != IDENT_NAME_OK)
1978 case SCH_STATISTICS:
1980 #ifdef DONT_ALLOW_SHOW_COMMANDS
1981 my_message(ER_NOT_ALLOWED_COMMAND,
1982 ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
1985 DBUG_ASSERT(table_ident);
1986 TABLE_LIST **query_tables_last= lex->query_tables_last;
1987 schema_select_lex=
new SELECT_LEX();
1989 schema_select_lex->parent_lex= lex;
1990 schema_select_lex->init_query();
1991 if (!schema_select_lex->add_table_to_list(thd, table_ident, 0, 0, TL_READ,
1994 lex->query_tables_last= query_tables_last;
2003 #if defined(ENABLED_PROFILING)
2004 thd->profiling.discard_current_query();
2007 case SCH_OPTIMIZER_TRACE:
2008 case SCH_OPEN_TABLES:
2011 case SCH_PROCEDURES:
2014 case SCH_COLLATIONS:
2015 case SCH_COLLATION_CHARACTER_SET_APPLICABILITY:
2016 case SCH_USER_PRIVILEGES:
2017 case SCH_SCHEMA_PRIVILEGES:
2018 case SCH_TABLE_PRIVILEGES:
2019 case SCH_COLUMN_PRIVILEGES:
2020 case SCH_TABLE_CONSTRAINTS:
2021 case SCH_KEY_COLUMN_USAGE:
2026 SELECT_LEX *select_lex= lex->current_select;
2027 if (make_schema_select(thd, select_lex, schema_table_idx))
2031 TABLE_LIST *table_list= select_lex->table_list.first;
2032 table_list->schema_select_lex= schema_select_lex;
2033 table_list->schema_table_reformed= 1;
2056 while (packet_length > 0 && my_isspace(thd->charset(), packet[0]))
2061 const char *pos= packet + packet_length;
2062 while (packet_length > 0 &&
2063 (pos[-1] ==
';' || my_isspace(thd->charset() ,pos[-1])))
2078 if (! (query= (
char*) thd->memdup_w_gap(packet,
2080 1 +
sizeof(
size_t) + thd->db_length +
2081 QUERY_CACHE_FLAGS_SIZE)))
2083 query[packet_length]=
'\0';
2089 char *len_pos = (query + packet_length + 1);
2090 memcpy(len_pos, (
char *) &thd->db_length,
sizeof(
size_t));
2092 thd->set_query(query, packet_length);
2093 thd->rewritten_query.free();
2096 thd->packet.shrink(thd->variables.net_buffer_length);
2097 thd->convert_buffer.shrink(thd->variables.net_buffer_length);
2102 static void reset_one_shot_variables(THD *thd)
2104 thd->variables.character_set_client=
2105 global_system_variables.character_set_client;
2106 thd->variables.collation_connection=
2107 global_system_variables.collation_connection;
2108 thd->variables.collation_database=
2109 global_system_variables.collation_database;
2110 thd->variables.collation_server=
2111 global_system_variables.collation_server;
2112 thd->update_charset();
2113 thd->variables.time_zone=
2114 global_system_variables.time_zone;
2115 thd->variables.lc_time_names= &my_locale_en_US;
2116 thd->one_shot_set= 0;
2121 bool sp_process_definer(THD *thd)
2123 DBUG_ENTER(
"sp_process_definer");
2154 Prepared_stmt_arena_holder ps_arena_holder(thd);
2159 if (lex->definer == NULL)
2162 if (thd->slave_thread && lex->sphead)
2163 lex->sphead->m_chistics->suid= SP_IS_NOT_SUID;
2173 if ((strcmp(lex->definer->user.str, thd->security_ctx->priv_user) ||
2174 my_strcasecmp(system_charset_info, lex->definer->host.str,
2175 thd->security_ctx->priv_host)) &&
2178 my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0),
"SUPER");
2185 #ifndef NO_EMBEDDED_ACCESS_CHECKS
2186 if (!is_acl_user(lex->definer->host.str, lex->definer->user.str))
2188 push_warning_printf(thd,
2189 Sql_condition::WARN_LEVEL_NOTE,
2191 ER(ER_NO_SUCH_USER),
2192 lex->definer->user.str,
2193 lex->definer->host.str);
2211 static bool lock_tables_open_and_lock_tables(THD *thd,
TABLE_LIST *tables)
2217 thd->in_lock_tables= 1;
2219 if (
open_tables(thd, &tables, &counter, 0, &lock_tables_prelocking_strategy))
2232 for (table= tables;
table; table= table->next_global)
2233 if (!table->placeholder() && table->table->s->tmp_table)
2234 table->table->reginfo.lock_type= TL_WRITE;
2237 thd->locked_tables_list.init_locked_tables(thd))
2240 thd->in_lock_tables= 0;
2245 thd->in_lock_tables= 0;
2247 trans_rollback_stmt(thd);
2253 trans_rollback(thd);
2255 close_thread_tables(thd);
2256 DBUG_ASSERT(!thd->locked_tables_mode);
2257 thd->mdl_context.release_transactional_locks();
2287 SELECT_LEX *select_lex= &lex->select_lex;
2289 TABLE_LIST *first_table= select_lex->table_list.first;
2293 SELECT_LEX_UNIT *unit= &lex->unit;
2294 #ifdef HAVE_REPLICATION
2296 bool have_table_map_for_update= FALSE;
2298 DBUG_ENTER(
"mysql_execute_command");
2299 DBUG_ASSERT(!lex->describe || is_explainable_query(lex->sql_command));
2301 if (unlikely(lex->is_broken()))
2305 if (reprepare_observer &&
2308 DBUG_ASSERT(thd->is_error());
2313 #ifdef WITH_PARTITION_STORAGE_ENGINE
2314 thd->work_part_info= 0;
2317 DBUG_ASSERT(thd->transaction.stmt.is_empty() || thd->in_sub_stmt);
2324 DBUG_ASSERT(! thd->transaction_rollback_request || thd->in_sub_stmt);
2340 lex->first_lists_tables_same();
2342 all_tables= lex->query_tables;
2345 context.resolve_in_table_list_only(select_lex->
2354 if ((sql_command_flags[lex->sql_command] & CF_DIAGNOSTIC_STMT) != 0)
2355 thd->get_stmt_da()->set_warning_info_read_only(TRUE);
2358 thd->get_stmt_da()->set_warning_info_read_only(FALSE);
2360 thd->get_stmt_da()->opt_clear_warning_info(thd->query_id);
2363 #ifdef HAVE_REPLICATION
2364 if (unlikely(thd->slave_thread))
2366 if (lex->sql_command == SQLCOM_DROP_TRIGGER)
2372 add_table_for_trigger(thd, thd->lex->spname, 1, &all_tables);
2387 all_tables->updating= 1;
2404 if (lex->sql_command == SQLCOM_UPDATE_MULTI &&
2405 thd->table_map_for_update)
2407 have_table_map_for_update= TRUE;
2408 table_map table_map_for_update= thd->table_map_for_update;
2411 for (table=all_tables;
table; table=table->next_global, nr++)
2413 if (table_map_for_update & ((table_map)1 << nr))
2414 table->updating= TRUE;
2416 table->updating= FALSE;
2419 if (all_tables_not_ok(thd, all_tables))
2422 my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2423 if (thd->one_shot_set)
2424 reset_one_shot_variables(thd);
2428 for (table=all_tables;
table; table=table->next_global)
2429 table->updating= TRUE;
2445 if (!(lex->sql_command == SQLCOM_UPDATE_MULTI) &&
2446 !(lex->sql_command == SQLCOM_SET_OPTION) &&
2447 !(lex->sql_command == SQLCOM_DROP_TABLE &&
2448 lex->drop_temporary && lex->drop_if_exists) &&
2449 all_tables_not_ok(thd, all_tables))
2452 my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
2453 if (thd->one_shot_set)
2468 reset_one_shot_variables(thd);
2475 if (slave_execute_deferred_events(thd))
2485 if (deny_updates_if_read_only_option(thd, all_tables))
2487 my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
"--read-only");
2490 #ifdef HAVE_REPLICATION
2494 status_var_increment(thd->status_var.com_stat[lex->sql_command]);
2496 Opt_trace_start ots(thd, all_tables, lex->sql_command, &lex->var_list,
2497 thd->query(), thd->query_length(), NULL,
2498 thd->variables.character_set_client);
2503 DBUG_ASSERT(thd->transaction.stmt.cannot_safely_rollback() == FALSE);
2505 switch (gtid_pre_statement_checks(thd))
2507 case GTID_STATEMENT_EXECUTE:
2509 case GTID_STATEMENT_CANCEL:
2511 case GTID_STATEMENT_SKIP:
2522 if (stmt_causes_implicit_commit(thd, CF_IMPLICIT_COMMIT_BEGIN))
2528 DBUG_ASSERT(! thd->in_sub_stmt);
2530 DBUG_ASSERT(thd->transaction.stmt.is_empty());
2537 if (trans_check_state(thd))
2541 if (trans_commit_implicit(thd))
2544 thd->mdl_context.release_transactional_locks();
2548 if (lex->sql_command != SQLCOM_SET_OPTION)
2549 DEBUG_SYNC(thd,
"before_execute_sql_command");
2558 if (thd->tx_read_only &&
2559 (sql_command_flags[lex->sql_command] & CF_DISALLOW_IN_RO_TRANS))
2561 my_error(ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION, MYF(0));
2577 if (sql_command_flags[lex->sql_command] & CF_HA_CLOSE)
2578 mysql_ha_rm_tables(thd, all_tables);
2584 if (sql_command_flags[lex->sql_command] & CF_PREOPEN_TMP_TABLES)
2590 switch (lex->sql_command) {
2592 case SQLCOM_SHOW_STATUS:
2595 thd->initial_status_var= &old_status_var;
2598 res= execute_sqlcom_select(thd, all_tables);
2601 thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED |
2602 SERVER_QUERY_NO_GOOD_INDEX_USED);
2608 add_diff_to_status(&global_status_var, &thd->status_var,
2610 thd->status_var= old_status_var;
2614 case SQLCOM_SHOW_EVENTS:
2615 #ifndef HAVE_EVENT_SCHEDULER
2616 my_error(ER_NOT_SUPPORTED_YET, MYF(0),
"embedded server");
2619 case SQLCOM_SHOW_STATUS_PROC:
2620 case SQLCOM_SHOW_STATUS_FUNC:
2621 case SQLCOM_SHOW_DATABASES:
2622 case SQLCOM_SHOW_TABLES:
2623 case SQLCOM_SHOW_TRIGGERS:
2624 case SQLCOM_SHOW_TABLE_STATUS:
2625 case SQLCOM_SHOW_OPEN_TABLES:
2626 case SQLCOM_SHOW_PLUGINS:
2627 case SQLCOM_SHOW_FIELDS:
2628 case SQLCOM_SHOW_KEYS:
2629 case SQLCOM_SHOW_VARIABLES:
2630 case SQLCOM_SHOW_CHARSETS:
2631 case SQLCOM_SHOW_COLLATIONS:
2632 case SQLCOM_SHOW_STORAGE_ENGINES:
2633 case SQLCOM_SHOW_PROFILE:
2636 thd->status_var.last_query_cost= 0.0;
2637 thd->status_var.last_query_partial_plans= 0;
2642 res= execute_sqlcom_select(thd, all_tables);
2645 case SQLCOM_PREPARE:
2650 case SQLCOM_EXECUTE:
2655 case SQLCOM_DEALLOCATE_PREPARE:
2665 res= mysql_do(thd, *lex->insert_list);
2668 case SQLCOM_EMPTY_QUERY:
2673 res= mysqld_help(thd,lex->help_arg);
2676 #ifndef EMBEDDED_LIBRARY
2685 case SQLCOM_PURGE_BEFORE:
2692 it= (
Item *)lex->value_list.head();
2693 if ((!it->fixed && it->fix_fields(lex->thd, &it)) ||
2696 my_error(ER_WRONG_ARGUMENTS, MYF(0),
"PURGE LOGS BEFORE");
2704 it->quick_fix_field();
2709 case SQLCOM_SHOW_WARNS:
2711 res= mysqld_show_warnings(thd, (ulong)
2712 ((1L << (uint) Sql_condition::WARN_LEVEL_NOTE) |
2713 (1L << (uint) Sql_condition::WARN_LEVEL_WARN) |
2714 (1L << (uint) Sql_condition::WARN_LEVEL_ERROR)
2718 case SQLCOM_SHOW_ERRORS:
2720 res= mysqld_show_warnings(thd, (ulong)
2721 (1L << (uint) Sql_condition::WARN_LEVEL_ERROR));
2724 case SQLCOM_SHOW_PROFILES:
2726 #if defined(ENABLED_PROFILING)
2727 thd->profiling.discard_current_query();
2728 res= thd->profiling.show_profiles();
2732 my_error(ER_FEATURE_DISABLED, MYF(0),
"SHOW PROFILES",
"enable-profiling");
2738 #ifdef HAVE_REPLICATION
2739 case SQLCOM_SHOW_SLAVE_HOSTS:
2743 res= show_slave_hosts(thd);
2746 case SQLCOM_SHOW_RELAYLOG_EVENTS:
2750 res = mysql_show_relaylog_events(thd);
2753 case SQLCOM_SHOW_BINLOG_EVENTS:
2757 res = mysql_show_binlog_events(thd);
2762 case SQLCOM_ASSIGN_TO_KEYCACHE:
2764 DBUG_ASSERT(first_table == all_tables && first_table != 0);
2770 res= mysql_assign_to_keycache(thd, first_table, &lex->ident);
2773 case SQLCOM_PRELOAD_KEYS:
2775 DBUG_ASSERT(first_table == all_tables && first_table != 0);
2781 res = mysql_preload_keys(thd, first_table);
2784 #ifdef HAVE_REPLICATION
2785 case SQLCOM_CHANGE_MASTER:
2790 if (active_mi != NULL)
2791 res= change_master(thd, active_mi);
2793 my_message(ER_SLAVE_CONFIGURATION, ER(ER_SLAVE_CONFIGURATION),
2798 case SQLCOM_SHOW_SLAVE_STAT:
2804 res= show_slave_status(thd, active_mi);
2808 case SQLCOM_SHOW_MASTER_STAT:
2813 res = show_master_status(thd);
2818 case SQLCOM_SHOW_ENGINE_STATUS:
2822 res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_STATUS);
2825 case SQLCOM_SHOW_ENGINE_MUTEX:
2829 res = ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_MUTEX);
2832 case SQLCOM_CREATE_TABLE:
2834 DBUG_ASSERT(first_table == all_tables && first_table != 0);
2837 TABLE_LIST *select_tables= lex->create_last_non_select_table->next_global;
2852 Alter_info alter_info(lex->alter_info, thd->mem_root);
2854 if (thd->is_fatal_error)
2858 goto end_with_restore_list;
2862 goto end_with_restore_list;
2865 create_info.alias= create_table->alias;
2869 create_table->table_name) ||
2871 create_table->table_name))
2872 goto end_with_restore_list;
2878 if (!(create_info.used_fields & HA_CREATE_USED_ENGINE))
2879 create_info.db_type= create_info.options & HA_LEX_CREATE_TMP_TABLE ?
2885 if ((create_info.used_fields &
2886 (HA_CREATE_USED_DEFAULT_CHARSET | HA_CREATE_USED_CHARSET)) ==
2887 HA_CREATE_USED_CHARSET)
2889 create_info.used_fields&= ~HA_CREATE_USED_CHARSET;
2890 create_info.used_fields|= HA_CREATE_USED_DEFAULT_CHARSET;
2891 create_info.default_table_charset= create_info.table_charset;
2892 create_info.table_charset= 0;
2895 #ifdef WITH_PARTITION_STORAGE_ENGINE
2898 if (part_info && !(part_info= thd->lex->part_info->get_clone()))
2901 goto end_with_restore_list;
2903 thd->work_part_info= part_info;
2907 if (select_lex->item_list.elements)
2909 select_result *result;
2919 lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_CREATE_IGNORE_SELECT);
2921 if(lex->duplicates == DUP_REPLACE)
2922 lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_CREATE_REPLACE_SELECT);
2932 if (thd->query_name_consts &&
2933 mysql_bin_log.is_open() &&
2934 thd->variables.binlog_format == BINLOG_FORMAT_STMT &&
2935 !mysql_bin_log.is_query_in_union(thd, thd->query_id))
2939 uint splocal_refs= 0;
2941 while ((item= it++))
2943 if (item->is_splocal())
2952 if (splocal_refs != thd->query_name_consts)
2954 Sql_condition::WARN_LEVEL_WARN,
2956 "Invoked routine ran a statement that may cause problems with "
2957 "binary log, see 'NAME_CONST issues' in 'Binary Logging of Stored Programs' "
2958 "section of the manual.");
2961 select_lex->options|= SELECT_NO_UNLOCK;
2962 unit->set_limit(select_lex);
2969 if (create_info.used_fields & HA_CREATE_USED_UNION)
2971 my_error(ER_WRONG_OBJECT, MYF(0), create_table->db,
2972 create_table->table_name,
"BASE TABLE");
2974 goto end_with_restore_list;
2980 if (create_table->table || create_table->view)
2982 if (create_info.options & HA_LEX_CREATE_IF_NOT_EXISTS)
2984 push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
2985 ER_TABLE_EXISTS_ERROR,
2986 ER(ER_TABLE_EXISTS_ERROR),
2992 my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_info.alias);
2995 goto end_with_restore_list;
3003 lex->unlink_first_table(&link_to_local);
3006 for (
TABLE_LIST *table= lex->query_tables; table;
3007 table= table->next_global)
3008 if (table->lock_type >= TL_WRITE_ALLOW_WRITE)
3011 my_error(ER_CANT_UPDATE_TABLE_IN_CREATE_TABLE_SELECT, MYF(0),
3012 table->table_name, create_info.alias);
3013 goto end_with_restore_list;
3020 if ((result=
new select_create(create_table,
3023 select_lex->item_list,
3036 lex->link_first_table_back(create_table, link_to_local);
3042 if (create_info.options & HA_LEX_CREATE_TABLE_LIKE)
3045 res= mysql_create_like_table(thd, create_table, select_tables,
3051 res= mysql_create_table(thd, create_table,
3052 &create_info, &alter_info);
3058 end_with_restore_list:
3061 case SQLCOM_CREATE_INDEX:
3063 case SQLCOM_DROP_INDEX:
3075 Alter_info alter_info(lex->alter_info, thd->mem_root);
3077 if (thd->is_fatal_error)
3080 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3088 thd->enable_slow_log= opt_log_slow_admin_statements;
3090 memset(&create_info, 0,
sizeof(create_info));
3091 create_info.db_type= 0;
3092 create_info.
row_type= ROW_TYPE_NOT_USED;
3093 create_info.default_table_charset= thd->variables.collation_database;
3095 res= mysql_alter_table(thd, first_table->db, first_table->table_name,
3096 &create_info, first_table, &alter_info,
3100 #ifdef HAVE_REPLICATION
3101 case SQLCOM_SLAVE_START:
3104 if (active_mi != NULL)
3105 res= start_slave(thd, active_mi, 1 );
3107 my_message(ER_SLAVE_CONFIGURATION, ER(ER_SLAVE_CONFIGURATION),
3112 case SQLCOM_SLAVE_STOP:
3126 if (thd->locked_tables_mode ||
3127 thd->in_active_multi_stmt_transaction() || thd->global_read_lock.is_acquired())
3129 my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
3130 ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
3135 if (active_mi != NULL)
3136 res= stop_slave(thd, active_mi, 1 );
3138 my_message(ER_SLAVE_CONFIGURATION, ER(ER_SLAVE_CONFIGURATION),
3145 case SQLCOM_RENAME_TABLE:
3147 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3149 for (table= first_table;
table; table= table->next_local->next_local)
3155 check_access(thd, INSERT_ACL | CREATE_ACL, table->next_local->db,
3166 new_list= table->next_local[0];
3167 if (check_grant(thd, ALTER_ACL | DROP_ACL, &old_list, FALSE, 1, FALSE) ||
3168 (!test_all_bits(table->next_local->grant.
privilege,
3169 INSERT_ACL | CREATE_ACL) &&
3170 check_grant(thd, INSERT_ACL | CREATE_ACL, &new_list, FALSE, 1,
3175 if (mysql_rename_tables(thd, first_table, 0))
3179 #ifndef EMBEDDED_LIBRARY
3180 case SQLCOM_SHOW_BINLOGS:
3181 #ifdef DONT_ALLOW_SHOW_COMMANDS
3182 my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND),
3189 res = show_binlogs(thd);
3194 case SQLCOM_SHOW_CREATE:
3195 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3196 #ifdef DONT_ALLOW_SHOW_COMMANDS
3197 my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND),
3211 DBUG_PRINT(
"debug", (
"lex->only_view: %d, table: %s.%s",
3213 first_table->db, first_table->table_name));
3218 DBUG_PRINT(
"debug", (
"check_table_access failed"));
3219 my_error(ER_TABLEACCESS_DENIED_ERROR, MYF(0),
3220 "SHOW", thd->security_ctx->priv_user,
3221 thd->security_ctx->host_or_ip, first_table->alias);
3224 DBUG_PRINT(
"debug", (
"check_table_access succeeded"));
3244 DBUG_PRINT(
"debug", (
"first_table->grant.privilege: %lx",
3247 (first_table->grant.
privilege & SHOW_CREATE_TABLE_ACLS) == 0)
3249 my_error(ER_TABLEACCESS_DENIED_ERROR, MYF(0),
3250 "SHOW", thd->security_ctx->priv_user,
3251 thd->security_ctx->host_or_ip, first_table->alias);
3257 res= mysqld_show_create(thd, first_table);
3261 case SQLCOM_CHECKSUM:
3263 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3265 FALSE, UINT_MAX, FALSE))
3268 res = mysql_checksum_table(thd, first_table, &lex->check_opt);
3273 ha_rows found= 0, updated= 0;
3274 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3285 lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_UPDATE_IGNORE);
3287 DBUG_ASSERT(select_lex->offset_limit == 0);
3288 unit->set_limit(select_lex);
3289 MYSQL_UPDATE_START(thd->query());
3290 res= (up_result= mysql_update(thd, all_tables,
3291 select_lex->item_list,
3294 select_lex->order_list.elements,
3295 select_lex->order_list.first,
3296 unit->select_limit_cnt,
3297 lex->duplicates, lex->ignore,
3299 MYSQL_UPDATE_DONE(res, found, updated);
3305 case SQLCOM_UPDATE_MULTI:
3307 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3317 res= mysql_multi_update_prepare(thd);
3319 #ifdef HAVE_REPLICATION
3321 if (unlikely(thd->slave_thread && !have_table_map_for_update))
3323 if (all_tables_not_ok(thd, all_tables))
3331 my_error(ER_SLAVE_IGNORED_TABLE, MYF(0));
3343 !(thd->security_ctx->master_access & SUPER_ACL) &&
3344 some_non_temp_table_to_be_updated(thd, all_tables))
3346 my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
"--read-only");
3349 #ifdef HAVE_REPLICATION
3353 multi_update *result_obj;
3354 MYSQL_MULTI_UPDATE_START(thd->query());
3355 res= mysql_multi_update(thd, all_tables,
3356 &select_lex->item_list,
3359 select_lex->options,
3367 MYSQL_MULTI_UPDATE_DONE(res, result_obj->num_found(),
3368 result_obj->num_updated());
3374 MYSQL_MULTI_UPDATE_DONE(1, 0, 0);
3379 case SQLCOM_REPLACE:
3381 if (mysql_bin_log.is_open())
3397 Incident incident= INCIDENT_NONE;
3398 DBUG_PRINT(
"debug", (
"Just before generate_incident()"));
3399 DBUG_EXECUTE_IF(
"incident_database_resync_on_replace",
3400 incident= INCIDENT_LOST_EVENTS;);
3410 DBUG_PRINT(
"debug", (
"Just after generate_incident()"));
3415 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3422 if (first_table->lock_type != TL_WRITE_DELAYED)
3431 MYSQL_INSERT_START(thd->query());
3432 res= mysql_insert(thd, all_tables, lex->field_list, lex->many_values,
3433 lex->update_list, lex->value_list,
3434 lex->duplicates, lex->ignore);
3435 MYSQL_INSERT_DONE(res, (ulong) thd->get_row_count_func());
3442 if (first_table->view && !first_table->contain_auto_increment)
3443 thd->first_successful_insert_id_in_cur_stmt=
3444 thd->first_successful_insert_id_in_prev_stmt;
3446 DBUG_EXECUTE_IF(
"after_mysql_insert",
3450 "wait_for signal.continue";
3451 DBUG_ASSERT(opt_debug_sync_timeout > 0);
3452 DBUG_ASSERT(!debug_sync_set_action(current_thd,
3453 STRING_WITH_LEN(act)));
3457 case SQLCOM_REPLACE_SELECT:
3458 case SQLCOM_INSERT_SELECT:
3460 select_insert *sel_result;
3461 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3472 if (lex->sql_command == SQLCOM_INSERT_SELECT &&
3473 lex->duplicates == DUP_UPDATE)
3474 lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_INSERT_SELECT_UPDATE);
3476 if (lex->sql_command == SQLCOM_INSERT_SELECT && lex->ignore)
3477 lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_INSERT_IGNORE_SELECT);
3479 if (lex->sql_command == SQLCOM_REPLACE_SELECT)
3480 lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_REPLACE_SELECT);
3483 if (first_table->lock_type == TL_WRITE_DELAYED)
3484 first_table->lock_type= TL_WRITE;
3487 select_lex->options|= SELECT_NO_UNLOCK;
3489 unit->set_limit(select_lex);
3493 MYSQL_INSERT_SELECT_START(thd->query());
3495 TABLE_LIST *second_table= first_table->next_local;
3496 select_lex->table_list.first= second_table;
3497 select_lex->context.table_list=
3498 select_lex->context.first_name_resolution_table= second_table;
3499 res= mysql_insert_select_prepare(thd);
3500 if (!res && (sel_result=
new select_insert(first_table,
3510 res= explain_multi_table_modification(thd, sel_result);
3520 if (!res && first_table->lock_type == TL_WRITE_CONCURRENT_INSERT &&
3524 TABLE_LIST *save_table= first_table->next_local;
3525 first_table->next_local= 0;
3526 query_cache_invalidate3(thd, first_table, 1);
3527 first_table->next_local= save_table;
3533 MYSQL_INSERT_SELECT_DONE(res, (ulong) thd->get_row_count_func());
3534 select_lex->table_list.first= first_table;
3542 if (first_table->view && !first_table->contain_auto_increment)
3543 thd->first_successful_insert_id_in_cur_stmt=
3544 thd->first_successful_insert_id_in_prev_stmt;
3550 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3553 DBUG_ASSERT(select_lex->offset_limit == 0);
3554 unit->set_limit(select_lex);
3556 MYSQL_DELETE_START(thd->query());
3557 res = mysql_delete(thd, all_tables, select_lex->where,
3558 &select_lex->order_list,
3559 unit->select_limit_cnt, select_lex->options);
3560 MYSQL_DELETE_DONE(res, (ulong) thd->get_row_count_func());
3563 case SQLCOM_DELETE_MULTI:
3565 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3566 TABLE_LIST *aux_tables= thd->lex->auxiliary_table_list.first;
3567 uint del_table_count;
3568 multi_delete *del_result;
3574 if (select_lex->item_list.elements != 0)
3575 select_lex->item_list.empty();
3576 if (add_item_to_list(thd,
new Item_null()))
3579 THD_STAGE_INFO(thd, stage_init);
3583 MYSQL_MULTI_DELETE_START(thd->query());
3584 if ((res= mysql_multi_delete_prepare(thd, &del_table_count)))
3586 MYSQL_MULTI_DELETE_DONE(1, 0);
3590 if (!thd->is_fatal_error &&
3591 (del_result=
new multi_delete(aux_tables, del_table_count)))
3594 res= explain_multi_table_modification(thd, del_result);
3598 select_lex->get_table_list(),
3599 select_lex->with_wild,
3600 select_lex->item_list,
3604 (select_lex->options | thd->variables.option_bits |
3605 SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
3607 del_result, unit, select_lex);
3608 res|= thd->is_error();
3610 del_result->abort_result_set();
3612 MYSQL_MULTI_DELETE_DONE(res, del_result->num_deleted());
3618 MYSQL_MULTI_DELETE_DONE(1, 0);
3622 case SQLCOM_DROP_TABLE:
3624 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3625 if (!lex->drop_temporary)
3631 res= mysql_rm_table(thd, first_table, lex->drop_if_exists,
3632 lex->drop_temporary);
3635 case SQLCOM_SHOW_PROCESSLIST:
3636 if (!thd->security_ctx->priv_user[0] &&
3639 mysqld_list_processes(thd,
3640 (thd->security_ctx->master_access & PROCESS_ACL ?
3642 thd->security_ctx->priv_user),
3645 case SQLCOM_SHOW_PRIVILEGES:
3646 res= mysqld_show_privileges(thd);
3648 case SQLCOM_SHOW_ENGINE_LOGS:
3649 #ifdef DONT_ALLOW_SHOW_COMMANDS
3650 my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND),
3655 if (
check_access(thd, FILE_ACL, any_db, NULL, NULL, 0, 0))
3657 res= ha_show_status(thd, lex->create_info.db_type, HA_ENGINE_LOGS);
3661 case SQLCOM_CHANGE_DB:
3663 LEX_STRING db_str= { (
char *) select_lex->db, strlen(select_lex->db) };
3665 if (!mysql_change_db(thd, &db_str, FALSE))
3673 DBUG_ASSERT(first_table == all_tables && first_table != 0);
3674 uint privilege= (lex->duplicates == DUP_REPLACE ?
3675 INSERT_ACL | DELETE_ACL : INSERT_ACL) |
3676 (lex->local_file ? 0 : FILE_ACL);
3678 if (lex->local_file)
3680 if (!(thd->client_capabilities & CLIENT_LOCAL_FILES) ||
3683 my_message(ER_NOT_ALLOWED_COMMAND, ER(ER_NOT_ALLOWED_COMMAND), MYF(0));
3691 res= mysql_load(thd, lex->exchange, first_table, lex->field_list,
3692 lex->update_list, lex->value_list, lex->duplicates,
3693 lex->ignore, (
bool) lex->local_file);
3697 case SQLCOM_SET_OPTION:
3704 if (!(res= sql_set_variables(thd, lex_var_list)))
3710 thd->one_shot_set|= lex->one_shot_set;
3720 if (!thd->is_error())
3721 my_error(ER_WRONG_ARGUMENTS,MYF(0),
"SET");
3728 case SQLCOM_UNLOCK_TABLES:
3735 if (thd->variables.option_bits & OPTION_TABLE_LOCK)
3741 if (trans_check_state(thd))
3743 res= trans_commit_implicit(thd);
3744 thd->locked_tables_list.unlock_locked_tables(thd);
3745 thd->mdl_context.release_transactional_locks();
3746 thd->variables.option_bits&= ~(OPTION_TABLE_LOCK);
3748 if (thd->global_read_lock.is_acquired())
3749 thd->global_read_lock.unlock_global_read_lock(thd);
3754 case SQLCOM_LOCK_TABLES:
3759 if (trans_check_state(thd))
3762 res= trans_commit_implicit(thd);
3763 thd->locked_tables_list.unlock_locked_tables(thd);
3765 thd->mdl_context.release_transactional_locks();
3782 if (lock_tables_precheck(thd, all_tables))
3785 thd->variables.option_bits|= OPTION_TABLE_LOCK;
3787 res= lock_tables_open_and_lock_tables(thd, all_tables);
3791 thd->variables.option_bits&= ~(OPTION_TABLE_LOCK);
3795 #ifdef HAVE_QUERY_CACHE
3796 if (thd->variables.query_cache_wlock_invalidate)
3797 query_cache.invalidate_locked_for_write(first_table);
3802 case SQLCOM_CREATE_DB:
3811 if (!(alias=thd->strmake(lex->name.str, lex->name.length)) ||
3812 (check_and_convert_db_name(&lex->name, FALSE) != IDENT_NAME_OK))
3821 #ifdef HAVE_REPLICATION
3822 if (!db_stmt_db_ok(thd, lex->name.str))
3824 my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
3828 if (
check_access(thd, CREATE_ACL, lex->name.str, NULL, NULL, 1, 0))
3830 res= mysql_create_db(thd,(lower_case_table_names == 2 ? alias :
3831 lex->name.str), &create_info, 0);
3834 case SQLCOM_DROP_DB:
3836 if (check_and_convert_db_name(&lex->name, FALSE) != IDENT_NAME_OK)
3845 #ifdef HAVE_REPLICATION
3846 if (!db_stmt_db_ok(thd, lex->name.str))
3848 my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
3852 if (
check_access(thd, DROP_ACL, lex->name.str, NULL, NULL, 1, 0))
3854 res= mysql_rm_db(thd, lex->name.str, lex->drop_if_exists, 0);
3857 case SQLCOM_ALTER_DB_UPGRADE:
3860 #ifdef HAVE_REPLICATION
3861 if (!db_stmt_db_ok(thd, lex->name.str))
3864 my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
3868 if (check_and_convert_db_name(db, FALSE) != IDENT_NAME_OK)
3870 if (
check_access(thd, ALTER_ACL, db->str, NULL, NULL, 1, 0) ||
3871 check_access(thd, DROP_ACL, db->str, NULL, NULL, 1, 0) ||
3872 check_access(thd, CREATE_ACL, db->str, NULL, NULL, 1, 0))
3877 res= mysql_upgrade_db(thd, db);
3882 case SQLCOM_ALTER_DB:
3886 if (check_and_convert_db_name(db, FALSE) != IDENT_NAME_OK)
3895 #ifdef HAVE_REPLICATION
3896 if (!db_stmt_db_ok(thd, lex->name.str))
3898 my_message(ER_SLAVE_IGNORED_TABLE, ER(ER_SLAVE_IGNORED_TABLE), MYF(0));
3902 if (
check_access(thd, ALTER_ACL, db->str, NULL, NULL, 1, 0))
3904 res= mysql_alter_db(thd, db->str, &create_info);
3907 case SQLCOM_SHOW_CREATE_DB:
3909 DBUG_EXECUTE_IF(
"4x_server_emul",
3910 my_error(ER_UNKNOWN_ERROR, MYF(0));
goto error;);
3911 if (check_and_convert_db_name(&lex->name, TRUE) != IDENT_NAME_OK)
3913 res= mysqld_show_create_db(thd, lex->name.str, &lex->create_info);
3916 case SQLCOM_CREATE_EVENT:
3917 case SQLCOM_ALTER_EVENT:
3918 #ifdef HAVE_EVENT_SCHEDULER
3921 DBUG_ASSERT(lex->event_parse_data);
3922 if (lex->table_or_sp_used())
3924 my_error(ER_NOT_SUPPORTED_YET, MYF(0),
"Usage of subqueries or stored "
3925 "function calls as part of this statement");
3929 res= sp_process_definer(thd);
3933 switch (lex->sql_command) {
3934 case SQLCOM_CREATE_EVENT:
3936 bool if_not_exists= (lex->create_info.options &
3937 HA_LEX_CREATE_IF_NOT_EXISTS);
3941 case SQLCOM_ALTER_EVENT:
3943 lex->spname ? &lex->spname->m_db : NULL,
3944 lex->spname ? &lex->spname->m_name : NULL);
3949 DBUG_PRINT(
"info",(
"DDL error code=%d", res));
3955 if (!thd->sp_runtime_ctx)
3962 case SQLCOM_SHOW_CREATE_EVENT:
3964 lex->spname->m_name);
3966 case SQLCOM_DROP_EVENT:
3968 lex->spname->m_db, lex->spname->m_name,
3969 lex->drop_if_exists)))
3973 my_error(ER_NOT_SUPPORTED_YET,MYF(0),
"embedded server");
3976 case SQLCOM_CREATE_FUNCTION:
3978 if (
check_access(thd, INSERT_ACL,
"mysql", NULL, NULL, 1, 0))
3981 if (!(res = mysql_create_function(thd, &lex->udf)))
3984 my_error(ER_CANT_OPEN_LIBRARY, MYF(0), lex->udf.dl, 0,
"feature disabled");
3989 #ifndef NO_EMBEDDED_ACCESS_CHECKS
3990 case SQLCOM_CREATE_USER:
3992 if (
check_access(thd, INSERT_ACL,
"mysql", NULL, NULL, 1, 1) &&
3996 if (!(res= mysql_create_user(thd, lex->users_list)))
4000 case SQLCOM_DROP_USER:
4002 if (
check_access(thd, DELETE_ACL,
"mysql", NULL, NULL, 1, 1) &&
4006 if (!(res= mysql_drop_user(thd, lex->users_list)))
4010 case SQLCOM_RENAME_USER:
4012 if (
check_access(thd, UPDATE_ACL,
"mysql", NULL, NULL, 1, 1) &&
4016 if (!(res= mysql_rename_user(thd, lex->users_list)))
4020 case SQLCOM_REVOKE_ALL:
4022 if (
check_access(thd, UPDATE_ACL,
"mysql", NULL, NULL, 1, 1) &&
4027 thd->binlog_invoker();
4030 if (!(res = mysql_revoke_all(thd, lex->users_list)))
4037 if (lex->type != TYPE_ENUM_PROXY &&
4038 check_access(thd, lex->grant | lex->grant_tot_col | GRANT_ACL,
4039 first_table ? first_table->db : select_lex->db,
4040 first_table ? &first_table->grant.
privilege : NULL,
4041 first_table ? &first_table->grant.
m_internal : NULL,
4042 first_table ? 0 : 1, 0))
4046 thd->binlog_invoker();
4048 if (thd->security_ctx->user)
4051 bool first_user= TRUE;
4054 while ((tmp_user= user_list++))
4058 if (specialflag & SPECIAL_NO_RESOLVE &&
4059 hostname_requires_resolving(user->host.str))
4060 push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
4061 ER_WARN_HOSTNAME_WONT_WORK,
4062 ER(ER_WARN_HOSTNAME_WONT_WORK));
4064 DBUG_ASSERT(user->host.str != 0);
4069 if (lex->type == TYPE_ENUM_PROXY && first_user)
4072 if (acl_check_proxy_grant_access (thd, user->host.str, user->user.str,
4073 lex->grant & GRANT_ACL))
4076 else if (is_acl_user(user->host.str, user->user.str) &&
4077 user->password.str &&
4078 check_change_password (thd, user->host.str, user->user.str,
4080 user->password.length))
4086 if (lex->type == TYPE_ENUM_PROCEDURE ||
4087 lex->type == TYPE_ENUM_FUNCTION)
4089 uint grants= lex->all_privileges
4090 ? (PROC_ACLS & ~GRANT_ACL) | (lex->grant & GRANT_ACL)
4092 if (check_grant_routine(thd, grants | GRANT_ACL, all_tables,
4093 lex->type == TYPE_ENUM_PROCEDURE, 0))
4096 res= mysql_routine_grant(thd, all_tables,
4097 lex->type == TYPE_ENUM_PROCEDURE,
4098 lex->users_list, grants,
4099 lex->sql_command == SQLCOM_REVOKE, TRUE);
4105 if (check_grant(thd,(lex->grant | lex->grant_tot_col | GRANT_ACL),
4106 all_tables, FALSE, UINT_MAX, FALSE))
4109 res= mysql_table_grant(thd, all_tables, lex->users_list,
4110 lex->columns, lex->grant,
4111 lex->sql_command == SQLCOM_REVOKE);
4116 if (lex->columns.elements || (lex->type && lex->type != TYPE_ENUM_PROXY))
4118 my_message(ER_ILLEGAL_GRANT_FOR_TABLE, ER(ER_ILLEGAL_GRANT_FOR_TABLE),
4125 res = mysql_grant(thd, select_lex->db, lex->users_list, lex->grant,
4126 lex->sql_command == SQLCOM_REVOKE,
4127 lex->type == TYPE_ENUM_PROXY);
4131 if (lex->sql_command == SQLCOM_GRANT)
4135 while ((tmp_user=str_list++))
4152 lex->no_write_to_binlog= 1;
4155 int write_to_binlog;
4159 if (first_table && lex->type & REFRESH_READ_LOCK)
4163 FALSE, UINT_MAX, FALSE))
4165 if (flush_tables_with_read_lock(thd, all_tables))
4170 else if (first_table && lex->type & REFRESH_FOR_EXPORT)
4174 FALSE, UINT_MAX, FALSE))
4176 if (flush_tables_for_export(thd, all_tables))
4186 if (!reload_acl_and_cache(thd, lex->type, first_table, &write_to_binlog))
4196 if (write_to_binlog > 0)
4198 if (!lex->no_write_to_binlog)
4199 res= write_bin_log(thd, FALSE, thd->query(), thd->query_length());
4200 }
else if (write_to_binlog < 0)
4217 Item *it= (
Item *)lex->value_list.head();
4219 if (lex->table_or_sp_used())
4221 my_error(ER_NOT_SUPPORTED_YET, MYF(0),
"Usage of subqueries or stored "
4222 "function calls as part of this statement");
4226 if ((!it->fixed && it->fix_fields(lex->thd, &it)) || it->check_cols(1))
4228 my_message(ER_SET_CONSTANTS_ONLY, ER(ER_SET_CONSTANTS_ONLY),
4232 sql_kill(thd, (ulong)it->val_int(), lex->type & ONLY_KILL_QUERY);
4235 #ifndef NO_EMBEDDED_ACCESS_CHECKS
4236 case SQLCOM_SHOW_GRANTS:
4241 if ((thd->security_ctx->priv_user &&
4242 !strcmp(thd->security_ctx->priv_user, grant_user->user.str)) ||
4243 !
check_access(thd, SELECT_ACL,
"mysql", NULL, NULL, 1, 0))
4245 res = mysql_show_grants(thd, grant_user);
4251 if (trans_begin(thd, lex->start_transaction_opt))
4257 DBUG_ASSERT(thd->lock == NULL ||
4258 thd->locked_tables_mode == LTM_LOCK_TABLES);
4259 bool tx_chain= (lex->tx_chain == TVL_YES ||
4260 (thd->variables.completion_type == 1 &&
4261 lex->tx_chain != TVL_NO));
4262 bool tx_release= (lex->tx_release == TVL_YES ||
4263 (thd->variables.completion_type == 2 &&
4264 lex->tx_release != TVL_NO));
4265 if (trans_commit(thd))
4267 thd->mdl_context.release_transactional_locks();
4271 if (trans_begin(thd))
4277 thd->tx_isolation= (enum_tx_isolation) thd->variables.tx_isolation;
4278 thd->tx_read_only= thd->variables.tx_read_only;
4282 thd->killed= THD::KILL_CONNECTION;
4286 case SQLCOM_ROLLBACK:
4288 DBUG_ASSERT(thd->lock == NULL ||
4289 thd->locked_tables_mode == LTM_LOCK_TABLES);
4290 bool tx_chain= (lex->tx_chain == TVL_YES ||
4291 (thd->variables.completion_type == 1 &&
4292 lex->tx_chain != TVL_NO));
4293 bool tx_release= (lex->tx_release == TVL_YES ||
4294 (thd->variables.completion_type == 2 &&
4295 lex->tx_release != TVL_NO));
4296 if (trans_rollback(thd))
4298 thd->mdl_context.release_transactional_locks();
4302 if (trans_begin(thd))
4308 thd->tx_isolation= (enum_tx_isolation) thd->variables.tx_isolation;
4309 thd->tx_read_only= thd->variables.tx_read_only;
4313 thd->killed= THD::KILL_CONNECTION;
4317 case SQLCOM_RELEASE_SAVEPOINT:
4318 if (trans_release_savepoint(thd, lex->ident))
4322 case SQLCOM_ROLLBACK_TO_SAVEPOINT:
4323 if (trans_rollback_to_savepoint(thd, lex->ident))
4327 case SQLCOM_SAVEPOINT:
4328 if (trans_savepoint(thd, lex->ident))
4332 case SQLCOM_CREATE_PROCEDURE:
4333 case SQLCOM_CREATE_SPFUNCTION:
4337 int sp_result= SP_INTERNAL_ERROR;
4339 DBUG_ASSERT(lex->sphead != 0);
4340 DBUG_ASSERT(lex->sphead->m_db.str);
4345 if (check_and_convert_db_name(&lex->sphead->m_db, FALSE) != IDENT_NAME_OK)
4346 goto create_sp_error;
4348 if (
check_access(thd, CREATE_PROC_ACL, lex->sphead->m_db.str,
4350 goto create_sp_error;
4357 if (check_db_dir_existence(lex->sphead->m_db.str))
4359 my_error(ER_BAD_DB_ERROR, MYF(0), lex->sphead->m_db.str);
4360 goto create_sp_error;
4363 name= lex->sphead->name(&namelen);
4365 if (lex->sphead->m_type == SP_TYPE_FUNCTION)
4367 udf_func *udf = find_udf(name, namelen);
4371 my_error(ER_UDF_EXISTS, MYF(0), name);
4372 goto create_sp_error;
4377 if (sp_process_definer(thd))
4378 goto create_sp_error;
4380 res= (sp_result= sp_create_routine(thd, lex->sphead));
4381 switch (sp_result) {
4383 #ifndef NO_EMBEDDED_ACCESS_CHECKS
4386 Security_context security_context;
4387 bool restore_backup_context=
false;
4388 Security_context *backup= NULL;
4389 LEX_USER *definer= thd->lex->definer;
4405 DBUG_ASSERT(thd->transaction.stmt.is_empty());
4406 close_thread_tables(thd);
4415 if (thd->slave_thread && is_acl_user(definer->host.str, definer->user.str))
4417 security_context.change_security_context(thd,
4418 &thd->lex->definer->user,
4419 &thd->lex->definer->host,
4420 &thd->lex->sphead->m_db,
4422 restore_backup_context=
true;
4425 if (sp_automatic_privileges && !opt_noacl &&
4426 check_routine_access(thd, DEFAULT_CREATE_PROC_ACLS,
4427 lex->sphead->m_db.str, name,
4428 lex->sql_command == SQLCOM_CREATE_PROCEDURE, 1))
4430 if (sp_grant_privileges(thd, lex->sphead->m_db.str, name,
4431 lex->sql_command == SQLCOM_CREATE_PROCEDURE))
4432 push_warning(thd, Sql_condition::WARN_LEVEL_WARN,
4433 ER_PROC_AUTO_GRANT_FAIL, ER(ER_PROC_AUTO_GRANT_FAIL));
4440 if (restore_backup_context)
4442 DBUG_ASSERT(thd->slave_thread == 1);
4443 thd->security_ctx->restore_security_context(thd, backup);
4449 case SP_WRITE_ROW_FAILED:
4450 my_error(ER_SP_ALREADY_EXISTS, MYF(0), SP_TYPE_STRING(lex), name);
4452 case SP_BAD_IDENTIFIER:
4453 my_error(ER_TOO_LONG_IDENT, MYF(0), name);
4455 case SP_BODY_TOO_LONG:
4456 my_error(ER_TOO_LONG_BODY, MYF(0), name);
4458 case SP_FLD_STORE_FAILED:
4459 my_error(ER_CANT_CREATE_SROUTINE, MYF(0), name);
4462 my_error(ER_SP_STORE_FAILED, MYF(0), SP_TYPE_STRING(lex), name);
4471 if (sp_result != SP_OK )
4481 if (check_routine_access(thd, EXECUTE_ACL, lex->spname->m_db.str,
4482 lex->spname->m_name.str,
4483 lex->sql_command == SQLCOM_CALL, 0))
4499 if (!(sp= sp_find_routine(thd, SP_TYPE_PROCEDURE, lex->spname,
4500 &thd->sp_proc_cache, TRUE)))
4502 my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
"PROCEDURE",
4503 lex->spname->m_qname.str);
4508 ha_rows select_limit;
4510 uint bits_to_be_cleared= 0;
4516 if (thd->in_sub_stmt)
4518 const char *where= (thd->in_sub_stmt & SUB_STMT_TRIGGER ?
4519 "trigger" :
"function");
4524 if (sp->
m_flags & sp_head::MULTI_RESULTS)
4526 if (! (thd->client_capabilities & CLIENT_MULTI_RESULTS))
4532 my_error(ER_SP_BADSELECT, MYF(0), sp->
m_qname.str);
4539 bits_to_be_cleared= (~thd->server_status &
4540 SERVER_MORE_RESULTS_EXISTS);
4541 thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
4544 if (check_routine_access(thd, EXECUTE_ACL,
4545 sp->m_db.str, sp->m_name.str, TRUE, FALSE))
4549 select_limit= thd->variables.select_limit;
4550 thd->variables.select_limit= HA_POS_ERROR;
4562 thd->variables.select_limit= select_limit;
4564 thd->server_status&= ~bits_to_be_cleared;
4568 my_ok(thd, (thd->get_row_count_func() < 0) ? 0 : thd->get_row_count_func());
4572 DBUG_ASSERT(thd->is_error() || thd->killed);
4578 case SQLCOM_ALTER_PROCEDURE:
4579 case SQLCOM_ALTER_FUNCTION:
4581 if (check_routine_access(thd, ALTER_PROC_ACL, lex->spname->m_db.str,
4582 lex->spname->m_name.str,
4583 lex->sql_command == SQLCOM_ALTER_PROCEDURE,
4587 enum_sp_type sp_type= (lex->sql_command == SQLCOM_ALTER_PROCEDURE) ?
4588 SP_TYPE_PROCEDURE : SP_TYPE_FUNCTION;
4596 int sp_result= sp_update_routine(thd, sp_type, lex->spname,
4605 case SP_KEY_NOT_FOUND:
4606 my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
4607 SP_COM_STRING(lex), lex->spname->m_qname.str);
4610 my_error(ER_SP_CANT_ALTER, MYF(0),
4611 SP_COM_STRING(lex), lex->spname->m_qname.str);
4616 case SQLCOM_DROP_PROCEDURE:
4617 case SQLCOM_DROP_FUNCTION:
4620 if (lex->sql_command == SQLCOM_DROP_FUNCTION &&
4621 ! lex->spname->m_explicit_name)
4624 udf_func *udf = find_udf(lex->spname->m_name.str,
4625 lex->spname->m_name.length);
4628 if (
check_access(thd, DELETE_ACL,
"mysql", NULL, NULL, 1, 0))
4631 if (!(res = mysql_drop_function(thd, &lex->spname->m_name)))
4636 my_error(ER_SP_DROP_FAILED, MYF(0),
4637 "FUNCTION (UDF)", lex->spname->m_name.str);
4641 if (lex->spname->m_db.str == NULL)
4643 if (lex->drop_if_exists)
4645 push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
4646 ER_SP_DOES_NOT_EXIST, ER(ER_SP_DOES_NOT_EXIST),
4647 "FUNCTION (UDF)", lex->spname->m_name.str);
4652 my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
4653 "FUNCTION (UDF)", lex->spname->m_name.str);
4660 char *db= lex->spname->m_db.str;
4661 char *
name= lex->spname->m_name.str;
4663 if (check_routine_access(thd, ALTER_PROC_ACL, db, name,
4664 lex->sql_command == SQLCOM_DROP_PROCEDURE,
4668 enum_sp_type sp_type= (lex->sql_command == SQLCOM_DROP_PROCEDURE) ?
4669 SP_TYPE_PROCEDURE : SP_TYPE_FUNCTION;
4672 int sp_result= sp_drop_routine(thd, sp_type, lex->spname);
4674 #ifndef NO_EMBEDDED_ACCESS_CHECKS
4690 DBUG_ASSERT(thd->transaction.stmt.is_empty());
4691 close_thread_tables(thd);
4693 if (sp_result != SP_KEY_NOT_FOUND &&
4694 sp_automatic_privileges && !opt_noacl &&
4695 sp_revoke_privileges(thd, db, name,
4696 lex->sql_command == SQLCOM_DROP_PROCEDURE))
4698 push_warning(thd, Sql_condition::WARN_LEVEL_WARN,
4699 ER_PROC_AUTO_REVOKE_FAIL,
4700 ER(ER_PROC_AUTO_REVOKE_FAIL));
4707 switch (sp_result) {
4711 case SP_KEY_NOT_FOUND:
4712 if (lex->drop_if_exists)
4714 res= write_bin_log(thd, TRUE, thd->query(), thd->query_length());
4715 push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
4716 ER_SP_DOES_NOT_EXIST, ER(ER_SP_DOES_NOT_EXIST),
4717 SP_COM_STRING(lex), lex->spname->m_qname.str);
4722 my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
4723 SP_COM_STRING(lex), lex->spname->m_qname.str);
4726 my_error(ER_SP_DROP_FAILED, MYF(0),
4727 SP_COM_STRING(lex), lex->spname->m_qname.str);
4732 case SQLCOM_SHOW_CREATE_PROC:
4734 if (sp_show_create_routine(thd, SP_TYPE_PROCEDURE, lex->spname))
4738 case SQLCOM_SHOW_CREATE_FUNC:
4740 if (sp_show_create_routine(thd, SP_TYPE_FUNCTION, lex->spname))
4744 case SQLCOM_SHOW_PROC_CODE:
4745 case SQLCOM_SHOW_FUNC_CODE:
4749 enum_sp_type sp_type= (lex->sql_command == SQLCOM_SHOW_PROC_CODE) ?
4750 SP_TYPE_PROCEDURE : SP_TYPE_FUNCTION;
4752 if (sp_cache_routine(thd, sp_type, lex->spname,
false, &sp))
4757 my_error(ER_SP_DOES_NOT_EXIST, MYF(0),
4758 SP_COM_STRING(lex), lex->spname->m_name.str);
4763 my_error(ER_FEATURE_DISABLED, MYF(0),
4764 "SHOW PROCEDURE|FUNCTION CODE",
"--with-debug");
4766 #endif // ifndef DBUG_OFF
4768 case SQLCOM_SHOW_CREATE_TRIGGER:
4770 if (lex->spname->m_name.length > NAME_LEN)
4772 my_error(ER_TOO_LONG_IDENT, MYF(0), lex->spname->m_name.str);
4776 if (show_create_trigger(thd, lex->spname))
4781 case SQLCOM_CREATE_VIEW:
4787 res= mysql_create_view(thd, first_table, thd->lex->create_view_mode);
4790 case SQLCOM_DROP_VIEW:
4795 res= mysql_drop_view(thd, first_table, thd->lex->drop_mode);
4798 case SQLCOM_CREATE_TRIGGER:
4801 res= mysql_create_or_drop_trigger(thd, all_tables, 1);
4805 case SQLCOM_DROP_TRIGGER:
4808 res= mysql_create_or_drop_trigger(thd, all_tables, 0);
4811 case SQLCOM_XA_START:
4812 if (trans_xa_start(thd))
4817 if (trans_xa_end(thd))
4821 case SQLCOM_XA_PREPARE:
4822 if (trans_xa_prepare(thd))
4826 case SQLCOM_XA_COMMIT:
4827 if (trans_xa_commit(thd))
4829 thd->mdl_context.release_transactional_locks();
4834 thd->tx_isolation= (enum_tx_isolation) thd->variables.tx_isolation;
4835 thd->tx_read_only= thd->variables.tx_read_only;
4838 case SQLCOM_XA_ROLLBACK:
4839 if (trans_xa_rollback(thd))
4841 thd->mdl_context.release_transactional_locks();
4846 thd->tx_isolation= (enum_tx_isolation) thd->variables.tx_isolation;
4847 thd->tx_read_only= thd->variables.tx_read_only;
4850 case SQLCOM_XA_RECOVER:
4853 case SQLCOM_ALTER_TABLESPACE:
4856 if (!(res= mysql_alter_tablespace(thd, lex->alter_tablespace_info)))
4859 case SQLCOM_INSTALL_PLUGIN:
4860 if (! (res= mysql_install_plugin(thd, &thd->lex->comment,
4864 case SQLCOM_UNINSTALL_PLUGIN:
4865 if (! (res= mysql_uninstall_plugin(thd, &thd->lex->comment)))
4868 case SQLCOM_BINLOG_BASE64_EVENT:
4870 #ifndef EMBEDDED_LIBRARY
4871 mysql_client_binlog_statement(thd);
4873 my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0),
"embedded");
4877 case SQLCOM_CREATE_SERVER:
4882 if (create_server(thd, &thd->lex->server_options))
4888 case SQLCOM_ALTER_SERVER:
4893 if (alter_server(thd, &thd->lex->server_options))
4899 case SQLCOM_DROP_SERVER:
4905 if (drop_server(thd, &lex->server_options, lex->drop_if_exists))
4911 if (thd->is_error() || thd->killed)
4913 DBUG_ASSERT(lex->drop_if_exists);
4921 case SQLCOM_ANALYZE:
4923 case SQLCOM_OPTIMIZE:
4925 case SQLCOM_TRUNCATE:
4926 case SQLCOM_ALTER_TABLE:
4927 case SQLCOM_HA_OPEN:
4928 case SQLCOM_HA_READ:
4929 case SQLCOM_HA_CLOSE:
4930 DBUG_ASSERT(first_table == all_tables && first_table != 0);
4933 case SQLCOM_RESIGNAL:
4934 case SQLCOM_GET_DIAGNOSTICS:
4935 DBUG_ASSERT(lex->m_sql_cmd != NULL);
4936 res= lex->m_sql_cmd->execute(thd);
4939 #ifndef NO_EMBEDDED_ACCESS_CHECKS
4940 case SQLCOM_ALTER_USER:
4941 if (
check_access(thd, UPDATE_ACL,
"mysql", NULL, NULL, 1, 1) &&
4945 if (!(res= mysql_user_password_expire(thd, lex->users_list)))
4950 #ifndef EMBEDDED_LIBRARY
4956 THD_STAGE_INFO(thd, stage_query_end);
4968 if (thd->one_shot_set && lex->sql_command != SQLCOM_SET_OPTION)
4969 reset_one_shot_variables(thd);
4978 DBUG_ASSERT(!thd->in_active_multi_stmt_transaction() ||
4979 thd->in_multi_stmt_transaction_mode());
4981 if (! thd->in_sub_stmt)
4984 if (thd->killed_errno())
4985 thd->send_kill_message();
4986 if (thd->killed == THD::KILL_QUERY || thd->killed == THD::KILL_BAD_DATA)
4988 thd->killed= THD::NOT_KILLED;
4989 thd->mysys_var->abort= 0;
4992 trans_rollback_stmt(thd);
4996 thd->get_stmt_da()->set_overwrite_status(
true);
4997 trans_commit_stmt(thd);
4998 thd->get_stmt_da()->set_overwrite_status(
false);
5002 lex->unit.cleanup();
5004 THD_STAGE_INFO(thd, stage_closing_tables);
5005 close_thread_tables(thd);
5008 if (lex->sql_command != SQLCOM_SET_OPTION && ! thd->in_sub_stmt)
5009 DEBUG_SYNC(thd,
"execute_command_after_close_tables");
5012 if (! thd->in_sub_stmt && thd->transaction_rollback_request)
5019 trans_rollback_implicit(thd);
5020 thd->mdl_context.release_transactional_locks();
5022 else if (stmt_causes_implicit_commit(thd, CF_IMPLICIT_COMMIT_END))
5025 DBUG_ASSERT(! thd->in_sub_stmt);
5027 thd->get_stmt_da()->set_overwrite_status(
true);
5029 trans_commit_implicit(thd);
5030 thd->get_stmt_da()->set_overwrite_status(
false);
5031 thd->mdl_context.release_transactional_locks();
5033 else if (! thd->in_sub_stmt && ! thd->in_multi_stmt_transaction_mode())
5045 thd->mdl_context.release_transactional_locks();
5047 else if (! thd->in_sub_stmt)
5049 thd->mdl_context.release_statement_locks();
5052 DBUG_RETURN(res || thd->is_error());
5055 static bool execute_sqlcom_select(THD *thd,
TABLE_LIST *all_tables)
5058 select_result *result= lex->result;
5062 SELECT_LEX *param= lex->unit.global_parameters;
5063 if (!param->explicit_limit)
5064 param->select_limit=
5065 new Item_int((ulonglong) thd->variables.select_limit);
5077 if (!(result=
new select_send()))
5079 res= explain_query_expression(thd, result);
5084 if (!result && !(result=
new select_send()))
5086 select_result *save_result= result;
5087 select_result *analyse_result= NULL;
5088 if (lex->proc_analyse)
5090 if ((result= analyse_result=
5095 delete analyse_result;
5096 if (save_result != lex->result)
5104 #ifndef NO_EMBEDDED_ACCESS_CHECKS
5123 Security_context * backup_ctx= thd->security_ctx;
5126 if (all_tables->security_ctx)
5127 thd->security_ctx= all_tables->security_ctx;
5129 const char *db_name;
5130 if ((all_tables->view || all_tables->field_translation) &&
5131 !all_tables->schema_table)
5132 db_name= all_tables->view_db.str;
5134 db_name= all_tables->db;
5143 if (!(all_tables->belong_to_view &&
5144 (thd->lex->sql_command == SQLCOM_SHOW_FIELDS)) &&
5145 check_grant(thd, privilege, all_tables, FALSE, 1, no_errors))
5148 thd->security_ctx= backup_ctx;
5152 thd->security_ctx= backup_ctx;
5176 TABLE_LIST *subselects_tables, *
view= all_tables->view ? all_tables : 0;
5177 if ((subselects_tables= all_tables->next_global))
5183 if (view && subselects_tables->belong_to_view == view)
5187 subselects_tables= subselects_tables->next_global;
5189 if (subselects_tables &&
5226 check_access(THD *thd, ulong want_access,
const char *db, ulong *save_priv,
5228 bool dont_check_global_grants,
bool no_errors)
5230 Security_context *sctx= thd->security_ctx;
5242 bool db_is_pattern= ((want_access & GRANT_ACL) && dont_check_global_grants);
5244 DBUG_ENTER(
"check_access");
5245 DBUG_PRINT(
"enter",(
"db: %s want_access: %lu master_access: %lu",
5246 db ? db :
"", want_access, sctx->master_access));
5256 THD_STAGE_INFO(thd, stage_checking_permissions);
5257 if ((!db || !db[0]) && !thd->db && !dont_check_global_grants)
5259 DBUG_PRINT(
"error",(
"No database"));
5261 my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR),
5266 if ((db != NULL) && (db != any_db))
5269 access= get_cached_schema_access(grant_internal_info, db);
5272 switch (access->
check(want_access, save_priv))
5274 case ACL_INTERNAL_ACCESS_GRANTED:
5280 case ACL_INTERNAL_ACCESS_DENIED:
5283 my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
5284 sctx->priv_user, sctx->priv_host, db);
5287 case ACL_INTERNAL_ACCESS_CHECK_GRANT:
5292 want_access&= ~(*save_priv);
5298 if ((sctx->master_access & want_access) == want_access)
5306 if (!(sctx->master_access & SELECT_ACL))
5308 if (db && (!thd->db || db_is_pattern || strcmp(db, thd->db)))
5309 db_access= acl_get(sctx->get_host()->ptr(), sctx->get_ip()->ptr(),
5310 sctx->priv_user, db, db_is_pattern);
5314 db_access= sctx->db_access;
5321 *save_priv|= sctx->master_access | db_access;
5324 *save_priv|= sctx->master_access;
5327 if (((want_access & ~sctx->master_access) & ~DB_ACLS) ||
5328 (! db && dont_check_global_grants))
5330 DBUG_PRINT(
"error",(
"No possible access"));
5333 if (thd->password == 2)
5334 my_error(ER_ACCESS_DENIED_NO_PASSWORD_ERROR, MYF(0),
5338 my_error(ER_ACCESS_DENIED_ERROR, MYF(0),
5357 if (db && (!thd->db || db_is_pattern || strcmp(db,thd->db)))
5358 db_access= acl_get(sctx->get_host()->ptr(), sctx->get_ip()->ptr(),
5359 sctx->priv_user, db, db_is_pattern);
5361 db_access= sctx->db_access;
5362 DBUG_PRINT(
"info",(
"db_access: %lu want_access: %lu",
5363 db_access, want_access));
5369 db_access= (db_access | sctx->master_access);
5370 *save_priv|= db_access;
5376 bool need_table_or_column_check=
5377 (want_access & (TABLE_ACLS | PROC_ACLS | db_access)) == want_access;
5385 if ( (db_access & want_access) == want_access ||
5386 (!dont_check_global_grants &&
5387 need_table_or_column_check))
5400 DBUG_PRINT(
"error",(
"Access denied"));
5402 my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
5403 sctx->priv_user, sctx->priv_host,
5404 (db ? db : (thd->db ?
5423 static bool check_show_access(THD *thd,
TABLE_LIST *table)
5425 switch (get_schema_table_idx(table->schema_table)) {
5427 return (specialflag & SPECIAL_SKIP_SHOW_DB) &&
5430 case SCH_TABLE_NAMES:
5436 const char *dst_db_name= table->schema_select_lex->db;
5438 DBUG_ASSERT(dst_db_name);
5441 &thd->col_access, NULL, FALSE, FALSE))
5444 if (!thd->col_access && check_grant_db(thd, dst_db_name))
5446 my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
5447 thd->security_ctx->priv_user,
5448 thd->security_ctx->priv_host,
5457 case SCH_STATISTICS:
5460 dst_table= table->schema_select_lex->table_list.first;
5462 DBUG_ASSERT(dst_table);
5480 if (check_grant(thd, SELECT_ACL, dst_table, TRUE, UINT_MAX, FALSE))
5483 close_thread_tables(thd);
5484 dst_table->table= NULL;
5535 bool any_combination_of_privileges_will_do,
5536 uint number,
bool no_errors)
5539 TABLE_LIST *first_not_own_table= thd->lex->first_not_own_table();
5541 Security_context *sctx= thd->security_ctx, *backup_ctx= thd->security_ctx;
5547 for (; i < number && tables != first_not_own_table && tables;
5548 tables= tables->next_global, i++)
5550 ulong want_access= requirements;
5551 if (tables->security_ctx)
5552 sctx= tables->security_ctx;
5560 tables->grant.orig_want_privilege= (want_access & ~SHOW_VIEW_ACL);
5570 DBUG_ASSERT(!tables->schema_table_reformed ||
5571 tables == thd->lex->select_lex.table_list.first);
5573 DBUG_PRINT(
"info", (
"derived: %d view: %d", tables->derived != 0,
5574 tables->view != 0));
5576 if (tables->is_anonymous_derived_table())
5579 thd->security_ctx= sctx;
5581 if (
check_access(thd, want_access, tables->get_db_name(),
5582 &tables->grant.privilege,
5583 &tables->grant.m_internal,
5587 thd->security_ctx= backup_ctx;
5588 return check_grant(thd,requirements,org_tables,
5589 any_combination_of_privileges_will_do,
5592 thd->security_ctx= backup_ctx;
5598 check_routine_access(THD *thd, ulong want_access,
char *db,
char *
name,
5599 bool is_proc,
bool no_errors)
5605 tables->table_name= tables->alias=
name;
5619 DBUG_ASSERT((want_access & CREATE_PROC_ACL) == 0);
5620 if ((thd->security_ctx->master_access & want_access) == want_access)
5628 return check_grant_routine(thd, want_access, tables, is_proc, no_errors);
5658 if (thd->security_ctx->master_access & SHOW_PROC_ACLS)
5660 if (!
check_access(thd, SHOW_PROC_ACLS, db, &save_priv, NULL, 0, 1) ||
5661 (save_priv & SHOW_PROC_ACLS))
5663 return check_routine_level_acl(thd, db, name, is_proc);
5682 DBUG_ENTER(
"check_some_access");
5685 for (access= 1; access < want_access ; access<<= 1)
5687 if (access & want_access)
5693 !check_grant(thd, access, table, FALSE, 1, TRUE))
5697 DBUG_PRINT(
"exit",(
"no matching access rights"));
5703 static bool check_show_access(THD *thd,
TABLE_LIST *table)
5731 DBUG_ENTER(
"check_global_access");
5732 #ifndef NO_EMBEDDED_ACCESS_CHECKS
5734 if ((thd->security_ctx->master_access & want_access))
5736 get_privilege_desc(command,
sizeof(command), want_access);
5737 my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), command);
5749 #if STACK_DIRECTION < 0
5750 #define used_stack(A,B) (long) (A - B)
5752 #define used_stack(A,B) (long) (B - A)
5756 long max_stack_used;
5767 uchar *buf __attribute__((unused)))
5770 DBUG_ASSERT(thd == current_thd);
5771 if ((stack_used=used_stack(thd->thread_stack,(
char*) &stack_used)) >=
5772 (
long) (my_thread_stack_size - margin))
5778 char* ebuff=
new (std::nothrow)
char[MYSQL_ERRMSG_SIZE];
5780 my_snprintf(ebuff, MYSQL_ERRMSG_SIZE, ER(ER_STACK_OVERRUN_NEED_MORE),
5781 stack_used, my_thread_stack_size, margin);
5782 my_message(ER_STACK_OVERRUN_NEED_MORE, ebuff, MYF(ME_FATALERROR));
5788 max_stack_used= max(max_stack_used, stack_used);
5794 #define MY_YACC_INIT 1000 // Start with big alloc
5795 #define MY_YACC_MAX 32000 // Because of 'short'
5797 bool my_yyoverflow(
short **yyss,
YYSTYPE **yyvs, ulong *yystacksize)
5799 Yacc_state *state= & current_thd->m_parser_state->m_yacc;
5802 if ((uint) *yystacksize >= MY_YACC_MAX)
5804 if (!state->yacc_yyvs)
5805 old_info= *yystacksize;
5806 *yystacksize= set_zone((*yystacksize)*2,MY_YACC_INIT,MY_YACC_MAX);
5807 if (!(state->yacc_yyvs= (uchar*)
5808 my_realloc(state->yacc_yyvs,
5809 *yystacksize*
sizeof(**yyvs),
5810 MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))) ||
5811 !(state->yacc_yyss= (uchar*)
5812 my_realloc(state->yacc_yyss,
5813 *yystacksize*
sizeof(**yyss),
5814 MYF(MY_ALLOW_ZERO_PTR | MY_FREE_ON_ERROR))))
5823 memcpy(state->yacc_yyss, *yyss, old_info*
sizeof(**yyss));
5824 memcpy(state->yacc_yyvs, *yyvs, old_info*
sizeof(**yyvs));
5826 *yyss= (
short*) state->yacc_yyss;
5827 *yyvs= (
YYSTYPE*) state->yacc_yyvs;
5847 thd->reset_for_next_command();
5850 void THD::reset_for_next_command()
5855 DBUG_ENTER(
"mysql_reset_thd_for_next_command");
5856 DBUG_ASSERT(!thd->sp_runtime_ctx);
5857 DBUG_ASSERT(! thd->in_sub_stmt);
5859 thd->select_number= 1;
5864 thd->auto_inc_intervals_in_cur_stmt_for_binlog.empty();
5865 thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt= 0;
5867 thd->query_start_used= thd->query_start_usec_used= 0;
5868 thd->is_fatal_error= thd->time_zone_used= 0;
5873 thd->server_status&= ~SERVER_STATUS_CLEAR_SET;
5883 if (!thd->in_multi_stmt_transaction_mode())
5885 thd->transaction.all.reset_unsafe_rollback_flags();
5887 DBUG_ASSERT(thd->security_ctx== &thd->main_security_ctx);
5888 thd->thread_specific_used= FALSE;
5892 reset_dynamic(&thd->user_var_events);
5893 thd->user_var_events_alloc= thd->mem_root;
5896 thd->get_stmt_da()->reset_diagnostics_area();
5897 thd->get_stmt_da()->reset_for_next_command();
5899 thd->m_sent_row_count= thd->m_examined_row_count= 0;
5901 thd->reset_current_stmt_binlog_format_row();
5902 thd->binlog_unsafe_warning_flags= 0;
5904 thd->m_trans_end_pos= 0;
5905 thd->m_trans_log_file= NULL;
5906 thd->m_trans_fixed_log_file= NULL;
5907 thd->commit_error= THD::CE_NONE;
5908 thd->durability_property= HA_REGULAR_DURABILITY;
5909 thd->set_trans_pos(NULL, 0);
5912 (
"is_current_stmt_binlog_format_row(): %d",
5913 thd->is_current_stmt_binlog_format_row()));
5930 SELECT_LEX *select_lex= lex->current_select;
5931 select_lex->init_select();
5933 if (select_lex == &lex->select_lex)
5935 DBUG_ASSERT(lex->result == 0);
5956 SELECT_LEX *select_lex;
5959 DBUG_ENTER(
"mysql_new_select");
5961 if (!(select_lex=
new (thd->mem_root) SELECT_LEX()))
5963 select_lex->select_number= ++thd->select_number;
5964 select_lex->parent_lex= lex;
5965 select_lex->init_query();
5966 select_lex->init_select();
5968 if (lex->nest_level > (
int) MAX_SELECT_NESTING)
5970 my_error(ER_TOO_HIGH_LEVEL_OF_NESTING_FOR_SELECT, MYF(0));
5973 select_lex->nest_level= lex->nest_level;
5976 SELECT_LEX_UNIT *unit;
5977 lex->subqueries= TRUE;
5979 if (!(unit=
new (thd->mem_root) SELECT_LEX_UNIT()))
5983 unit->init_select();
5985 unit->include_down(lex->current_select);
5988 select_lex->include_down(unit);
5993 if (select_lex->outer_select()->parsing_place == IN_ON)
6011 select_lex->context.outer_context= outer_context;
6013 select_lex->context.outer_context= &select_lex->outer_select()->context;
6017 if (lex->current_select->order_list.first && !lex->current_select->braces)
6019 my_error(ER_WRONG_USAGE, MYF(0),
"UNION",
"ORDER BY");
6022 select_lex->include_neighbour(lex->current_select);
6023 SELECT_LEX_UNIT *unit= select_lex->master_unit();
6024 if (!unit->fake_select_lex && unit->add_fake_select_lex(lex->thd))
6026 select_lex->context.outer_context=
6027 unit->first_select()->context.outer_context;
6030 select_lex->master_unit()->global_parameters= select_lex;
6031 select_lex->include_global((st_select_lex_node**)&lex->all_selects_list);
6032 lex->current_select= select_lex;
6037 select_lex->context.resolve_in_select_list= TRUE;
6057 char buff[MAX_SYS_VAR_LENGTH*2+4+8], *end;
6058 DBUG_ENTER(
"create_select_for_variable");
6063 lex->sql_command= SQLCOM_SELECT;
6064 tmp.str= (
char*) var_name;
6065 tmp.length=strlen(var_name);
6066 memset(&null_lex_string, 0,
sizeof(null_lex_string));
6071 if ((var=
get_system_var(thd, OPT_SESSION, tmp, null_lex_string)))
6073 end= strxmov(buff,
"@@session.", var_name, NullS);
6074 var->item_name.
copy(buff, end - buff);
6075 add_item_to_list(thd, var);
6081 void mysql_init_multi_delete(LEX *lex)
6083 lex->sql_command= SQLCOM_DELETE_MULTI;
6085 lex->select_lex.select_limit= 0;
6086 lex->unit.select_limit_cnt= HA_POS_ERROR;
6087 lex->select_lex.table_list.save_and_clear(&lex->auxiliary_table_list);
6088 lex->query_tables= 0;
6089 lex->query_tables_last= &lex->query_tables;
6109 Parser_state *parser_state)
6111 int error __attribute__((unused));
6112 DBUG_ENTER(
"mysql_parse");
6114 DBUG_EXECUTE_IF(
"parser_debug", turn_parser_debug_on(););
6135 if (query_cache_send_result_to_client(thd, rawbuf, length) <= 0)
6139 bool err=
parse_sql(thd, parser_state, NULL);
6141 const char *found_semicolon= parser_state->m_lip.found_semicolon;
6142 size_t qlen= found_semicolon
6143 ? (found_semicolon - thd->query())
6144 : thd->query_length();
6163 bool general= (opt_log && ! (opt_log_raw || thd->slave_thread));
6165 if (general || opt_slow_log || opt_bin_log)
6167 mysql_rewrite_query(thd);
6169 if (thd->rewritten_query.length())
6170 lex->safe_to_cache_query=
false;
6175 if (thd->rewritten_query.length())
6176 general_log_write(thd, COM_QUERY, thd->rewritten_query.c_ptr_safe(),
6177 thd->rewritten_query.length());
6179 general_log_write(thd, COM_QUERY, thd->query(), qlen);
6185 thd->m_statement_psi= MYSQL_REFINE_STATEMENT(thd->m_statement_psi,
6186 sql_statement_info[thd->lex->sql_command].m_key);
6188 #ifndef NO_EMBEDDED_ACCESS_CHECKS
6189 if (mqh_used && thd->get_user_connect() &&
6190 check_mqh(thd, lex->sql_command))
6197 if (! thd->is_error())
6209 if (found_semicolon && (ulong) (found_semicolon - thd->query()))
6210 thd->set_query_inner(thd->query(),
6211 (uint32) (found_semicolon -
6215 if (found_semicolon)
6217 lex->safe_to_cache_query= 0;
6218 thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
6220 lex->set_trg_event_type_for_tables();
6221 MYSQL_QUERY_EXEC_START(thd->query(),
6223 (
char *) (thd->db ? thd->db :
""),
6224 &thd->security_ctx->priv_user[0],
6225 (
char *) thd->security_ctx->host_or_ip,
6227 if (unlikely(thd->security_ctx->password_expired &&
6228 !lex->is_change_password &&
6229 lex->sql_command != SQLCOM_SET_OPTION))
6231 my_error(ER_MUST_CHANGE_PASSWORD, MYF(0));
6237 thd->variables.gtid_next.type == GTID_GROUP &&
6238 thd->owned_gtid.sidno != 0 &&
6239 (thd->lex->sql_command == SQLCOM_COMMIT ||
6240 stmt_causes_implicit_commit(thd, CF_IMPLICIT_COMMIT_END)))
6247 MYSQL_QUERY_EXEC_DONE(error);
6254 thd->m_statement_psi= MYSQL_REFINE_STATEMENT(thd->m_statement_psi,
6255 sql_statement_info[SQLCOM_END].m_key);
6257 DBUG_ASSERT(thd->is_error());
6258 DBUG_PRINT(
"info",(
"Command aborted. Fatal_error: %d",
6259 thd->is_fatal_error));
6261 query_cache_abort(&thd->query_cache_tls);
6264 THD_STAGE_INFO(thd, stage_freeing_items);
6265 sp_cache_enforce_limit(thd->sp_proc_cache, stored_program_cache_size);
6266 sp_cache_enforce_limit(thd->sp_func_cache, stored_program_cache_size);
6267 thd->end_statement();
6268 thd->cleanup_after_query();
6269 DBUG_ASSERT(thd->change_list.is_empty());
6283 thd->m_statement_psi= MYSQL_REFINE_STATEMENT(thd->m_statement_psi,
6284 sql_statement_info[SQLCOM_SELECT].m_key);
6286 general_log_write(thd, COM_QUERY, thd->query(), thd->query_length());
6287 parser_state->m_lip.found_semicolon= NULL;
6294 #ifdef HAVE_REPLICATION
6305 bool mysql_test_parse_for_slave(THD *thd,
char *rawbuf, uint length)
6309 PSI_statement_locker *parent_locker= thd->m_statement_psi;
6310 DBUG_ENTER(
"mysql_test_parse_for_slave");
6312 Parser_state parser_state;
6313 if (!(error= parser_state.init(thd, rawbuf, length)))
6318 thd->m_statement_psi= NULL;
6319 if (!
parse_sql(thd, & parser_state, NULL) &&
6320 all_tables_not_ok(thd, lex->select_lex.table_list.first))
6322 thd->m_statement_psi= parent_locker;
6323 thd->end_statement();
6325 thd->cleanup_after_query();
6340 char *length,
char *decimals,
6342 Item *default_value,
Item *on_update_value,
6346 uint uint_geom_type)
6350 uint8 datetime_precision= decimals ? atoi(decimals) : 0;
6351 DBUG_ENTER(
"add_field_to_list");
6353 if (check_string_char_length(field_name,
"", NAME_CHAR_LEN,
6354 system_charset_info, 1))
6356 my_error(ER_TOO_LONG_IDENT, MYF(0), field_name->str);
6359 if (type_modifier & PRI_KEY_FLAG)
6363 key=
new Key(Key::PRIMARY, null_lex_str,
6364 &default_key_create_info,
6366 lex->alter_info.key_list.push_back(key);
6367 lex->col_list.empty();
6369 if (type_modifier & (UNIQUE_FLAG | UNIQUE_KEY_FLAG))
6373 key=
new Key(Key::UNIQUE, null_lex_str,
6374 &default_key_create_info, 0,
6376 lex->alter_info.key_list.push_back(key);
6377 lex->col_list.empty();
6389 if (default_value->type() == Item::FUNC_ITEM &&
6390 (
static_cast<Item_func*
>(default_value)->functype() !=
6391 Item_func::NOW_FUNC ||
6392 (!real_type_with_now_as_default(type)) ||
6393 default_value->decimals != datetime_precision))
6395 my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
6398 else if (default_value->type() == Item::NULL_ITEM)
6401 if ((type_modifier & (NOT_NULL_FLAG | AUTO_INCREMENT_FLAG)) ==
6404 my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
6408 else if (type_modifier & AUTO_INCREMENT_FLAG)
6410 my_error(ER_INVALID_DEFAULT, MYF(0), field_name->str);
6415 if (on_update_value &&
6416 (!real_type_with_now_on_update(type) ||
6417 on_update_value->decimals != datetime_precision))
6419 my_error(ER_INVALID_ON_UPDATE, MYF(0), field_name->str);
6424 new_field->
init(thd, field_name->str, type, length, decimals, type_modifier,
6425 default_value, on_update_value, comment, change,
6426 interval_list, cs, uint_geom_type))
6429 lex->alter_info.create_list.push_back(new_field);
6430 lex->last_field=new_field;
6439 current_thd->lex->last_field->after=(
char*) (name);
6450 DBUG_ENTER(
"add_to_list");
6451 if (!(order = (
ORDER *) thd->alloc(
sizeof(
ORDER))))
6453 order->item_ptr= item;
6454 order->item= &order->item_ptr;
6455 order->direction= (asc ? ORDER::ORDER_ASC : ORDER::ORDER_DESC);
6458 order->counter_used= 0;
6459 list.link_in_list(order, &order->next);
6484 TABLE_LIST *st_select_lex::add_table_to_list(THD *thd,
6487 ulong table_options,
6488 thr_lock_type lock_type,
6489 enum_mdl_type mdl_type,
6498 DBUG_ENTER(
"add_table_to_list");
6499 LINT_INIT(previous_table_ref);
6503 alias_str= alias ? alias->str : table->table.str;
6504 if (!
test(table_options & TL_OPTION_ALIAS))
6506 enum_ident_name_check ident_check_status=
6507 check_table_name(table->table.str, table->table.length, FALSE);
6508 if (ident_check_status == IDENT_NAME_WRONG)
6510 my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
6513 else if (ident_check_status == IDENT_NAME_TOO_LONG)
6515 my_error(ER_TOO_LONG_IDENT, MYF(0), table->table.str);
6519 if (table->is_derived_table() == FALSE && table->db.str &&
6520 (check_and_convert_db_name(&table->db, FALSE) != IDENT_NAME_OK))
6527 my_message(ER_DERIVED_MUST_HAVE_ALIAS,
6528 ER(ER_DERIVED_MUST_HAVE_ALIAS), MYF(0));
6531 if (!(alias_str= (
char*) thd->memdup(alias_str,table->table.length+1)))
6539 ptr->db= table->db.str;
6540 ptr->db_length= table->db.length;
6542 else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
6547 ptr->alias= alias_str;
6548 ptr->
is_alias= alias ? TRUE : FALSE;
6549 if (lower_case_table_names && table->table.length)
6550 table->table.length= my_casedn_str(files_charset_info, table->table.str);
6551 ptr->table_name=table->table.str;
6552 ptr->table_name_length=table->table.length;
6553 ptr->lock_type= lock_type;
6554 ptr->updating=
test(table_options & TL_OPTION_UPDATING);
6556 ptr->force_index=
test(table_options & TL_OPTION_FORCE_INDEX);
6557 ptr->ignore_leaves=
test(table_options & TL_OPTION_IGNORE_LEAVES);
6559 if (!ptr->
derived && is_infoschema_db(ptr->db, ptr->db_length))
6562 if (ptr->updating &&
6564 lex->sql_command != SQLCOM_CHECK &&
6565 lex->sql_command != SQLCOM_CHECKSUM)
6567 my_error(ER_DBACCESS_DENIED_ERROR, MYF(0),
6568 thd->security_ctx->priv_user,
6569 thd->security_ctx->priv_host,
6570 INFORMATION_SCHEMA_NAME.str);
6573 schema_table= find_schema_table(thd, ptr->table_name);
6574 if (!schema_table ||
6575 (schema_table->hidden &&
6576 ((sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0 ||
6580 lex->sql_command == SQLCOM_SHOW_FIELDS ||
6581 lex->sql_command == SQLCOM_SHOW_KEYS)))
6583 my_error(ER_UNKNOWN_TABLE, MYF(0),
6584 ptr->table_name, INFORMATION_SCHEMA_NAME.str);
6587 ptr->schema_table_name= ptr->table_name;
6588 ptr->schema_table= schema_table;
6590 ptr->select_lex= lex->current_select;
6591 ptr->cacheable_table= 1;
6592 ptr->index_hints= index_hints_arg;
6593 ptr->option= option ? option->str : 0;
6595 if (lock_type != TL_IGNORE)
6598 if (lex->sql_command == SQLCOM_CREATE_VIEW)
6599 first_table= first_table ? first_table->next_local : NULL;
6602 tables=tables->next_local)
6604 if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
6605 !strcmp(ptr->db, tables->db))
6607 my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
6613 if (table_list.elements > 0)
6620 previous_table_ref= (
TABLE_LIST*) ((
char*) table_list.next -
6621 ((
char*) &(ptr->next_local) -
6630 previous_table_ref->next_name_resolution_table= ptr;
6639 table_list.link_in_list(ptr, &ptr->next_local);
6640 ptr->next_name_resolution_table= NULL;
6641 #ifdef WITH_PARTITION_STORAGE_ENGINE
6642 ptr->partition_names= partition_names;
6645 lex->add_to_query_tables(ptr);
6648 if (!
test(table_options & TL_OPTION_ALIAS))
6650 ptr->mdl_request.
init(MDL_key::TABLE, ptr->db, ptr->table_name, mdl_type,
6653 if (table->is_derived_table())
6656 ptr->derived_key_list.empty();
6681 bool st_select_lex::init_nested_join(THD *thd)
6683 DBUG_ENTER(
"init_nested_join");
6687 embedding, join_list,
this);
6691 join_list->push_front(ptr);
6693 join_list= &ptr->nested_join->join_list;
6713 TABLE_LIST *st_select_lex::end_nested_join(THD *thd)
6717 DBUG_ENTER(
"end_nested_join");
6719 DBUG_ASSERT(embedding);
6721 join_list= ptr->join_list;
6722 embedding= ptr->embedding;
6723 nested_join= ptr->nested_join;
6724 if (nested_join->join_list.elements == 1)
6726 TABLE_LIST *embedded= nested_join->join_list.head();
6728 embedded->join_list= join_list;
6729 embedded->embedding= embedding;
6730 join_list->push_front(embedded);
6733 else if (nested_join->join_list.elements == 0)
6755 TABLE_LIST *st_select_lex::nest_last_join(THD *thd)
6757 DBUG_ENTER(
"nest_last_join");
6761 embedding, join_list,
this);
6767 for (uint
i=0;
i < 2;
i++)
6770 table->join_list= embedded_list;
6771 table->embedding= ptr;
6772 embedded_list->push_back(table);
6773 if (table->natural_join)
6775 ptr->is_natural_join= TRUE;
6780 if (prev_join_using)
6781 ptr->join_using_fields= prev_join_using;
6784 join_list->push_front(ptr);
6804 void st_select_lex::add_joined_table(
TABLE_LIST *table)
6806 DBUG_ENTER(
"add_joined_table");
6807 join_list->push_front(table);
6808 table->join_list= join_list;
6809 table->embedding= embedding;
6845 TABLE_LIST *st_select_lex::convert_right_join()
6849 DBUG_ENTER(
"convert_right_join");
6851 join_list->push_front(tab2);
6852 join_list->push_front(tab1);
6853 tab1->outer_join|= JOIN_TYPE_RIGHT;
6869 void st_select_lex::set_lock_for_tables(thr_lock_type lock_type)
6871 bool for_update= lock_type >= TL_READ_NO_INSERT;
6872 DBUG_ENTER(
"set_lock_for_tables");
6873 DBUG_PRINT(
"enter", (
"lock_type: %d for_update: %d", lock_type,
6877 tables= tables->next_local)
6879 tables->lock_type= lock_type;
6880 tables->updating= for_update;
6881 tables->mdl_request.
set_type((lock_type >= TL_WRITE_ALLOW_WRITE) ?
6882 MDL_SHARED_WRITE : MDL_SHARED_READ);
6914 bool st_select_lex_unit::add_fake_select_lex(THD *thd_arg)
6916 SELECT_LEX *first_sl= first_select();
6917 DBUG_ENTER(
"add_fake_select_lex");
6918 DBUG_ASSERT(!fake_select_lex);
6920 if (!(fake_select_lex=
new (thd_arg->mem_root) SELECT_LEX()))
6922 fake_select_lex->include_standalone(
this,
6923 (SELECT_LEX_NODE**)&fake_select_lex);
6924 fake_select_lex->select_number= INT_MAX;
6925 fake_select_lex->parent_lex= thd_arg->lex;
6926 fake_select_lex->make_empty_select();
6927 fake_select_lex->linkage= GLOBAL_OPTIONS_TYPE;
6928 fake_select_lex->select_limit= 0;
6930 fake_select_lex->context.outer_context=first_sl->context.outer_context;
6932 fake_select_lex->context.resolve_in_select_list= TRUE;
6933 fake_select_lex->context.select_lex= fake_select_lex;
6943 global_parameters= fake_select_lex;
6944 fake_select_lex->no_table_names_allowed= 1;
6945 thd_arg->lex->current_select= fake_select_lex;
6947 thd_arg->lex->pop_context();
6975 push_new_name_resolution_context(THD *thd,
6982 on_context->first_name_resolution_table=
6983 left_op->first_leaf_for_name_resolution();
6984 on_context->last_name_resolution_table=
6985 right_op->last_leaf_for_name_resolution();
6986 on_context->select_lex= thd->lex->current_select;
6990 return thd->lex->push_context(on_context);
7007 if (!b->join_cond())
7008 b->set_join_cond(expr);
7018 b->join_cond()->top_level_item();
7060 lex->prev_join_using= using_fields;
7078 uint error=ER_NO_SUCH_THREAD;
7079 DBUG_ENTER(
"kill_one_thread");
7080 DBUG_PRINT(
"enter", (
"id=%lu only_kill=%d",
id, only_kill_query));
7083 Thread_iterator it= global_thread_list_begin();
7084 Thread_iterator end= global_thread_list_end();
7085 for (; it != end; ++it)
7087 if ((*it)->get_command() == COM_DAEMON)
7089 if ((*it)->thread_id ==
id)
7117 if ((thd->security_ctx->master_access & SUPER_ACL) ||
7118 thd->security_ctx->user_matches(tmp->security_ctx))
7123 if (tmp->killed != THD::KILL_CONNECTION)
7125 tmp->awake(only_kill_query ? THD::KILL_QUERY : THD::KILL_CONNECTION);
7130 error=ER_KILL_DENIED_ERROR;
7133 DBUG_PRINT(
"exit", (
"%d", error));
7149 void sql_kill(THD *thd, ulong
id,
bool only_kill_query)
7158 my_error(error, MYF(0),
id);
7167 char buff[FN_REFLEN],*ptr, *end;
7172 if (strlen(*filename_ptr)+strlen(table_name) >= FN_REFLEN-1 ||
7173 !test_if_hard_path(*filename_ptr))
7175 my_error(ER_WRONG_TABLE_NAME, MYF(0), *filename_ptr);
7179 strmov(buff,*filename_ptr);
7180 end=convert_dirname(buff, *filename_ptr, NullS);
7181 if (!(ptr= (
char*) thd->alloc((
size_t) (end-buff) + strlen(table_name)+1)))
7184 strxmov(ptr,buff,table_name,NullS);
7200 THD *thd= current_thd;
7202 if (lex->current_select != &lex->select_lex)
7205 Lex_input_stream *lip= & thd->m_parser_state->m_lip;
7206 strmake(command, lip->yylval->symbol.str,
7207 min<size_t>(lip->yylval->symbol.length,
sizeof(command)-1));
7208 my_error(ER_CANT_USE_OPTION_HERE, MYF(0), command);
7263 chooser_compare_func_creator cmp,
7265 SELECT_LEX *select_lex)
7267 if ((cmp == &comp_eq_creator) && !all)
7270 if ((cmp == &comp_ne_creator) && all)
7303 ulong privileges_requested= lex->exchange ? SELECT_ACL | FILE_ACL :
7309 privileges_requested,
7310 tables, FALSE, UINT_MAX, FALSE) ||
7311 (first_table && first_table->schema_table_reformed &&
7312 check_show_access(thd, first_table));
7315 res=
check_access(thd, privileges_requested, any_db, NULL, NULL, 0, 0);
7338 SELECT_LEX *select_lex= &lex->select_lex;
7339 DBUG_ENTER(
"multi_update_precheck");
7341 if (select_lex->item_list.elements != lex->value_list.elements)
7343 my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
7350 for (table= tables;
table; table= table->next_local)
7358 check_grant(thd, UPDATE_ACL, table, FALSE, 1, TRUE)) &&
7363 check_grant(thd, SELECT_ACL, table, FALSE, 1, FALSE)))
7366 table->table_in_first_from_clause= 1;
7371 if (&lex->select_lex != lex->all_selects_list)
7373 DBUG_PRINT(
"info",(
"Checking sub query list"));
7374 for (table= tables;
table; table= table->next_global)
7376 if (!table->table_in_first_from_clause)
7382 check_grant(thd, SELECT_ACL, table, FALSE, 1, FALSE))
7388 if (select_lex->order_list.elements)
7390 else if (select_lex->select_limit)
7394 my_error(ER_WRONG_USAGE, MYF(0),
"UPDATE", msg);
7414 SELECT_LEX *select_lex= &thd->lex->select_lex;
7415 TABLE_LIST *aux_tables= thd->lex->auxiliary_table_list.first;
7416 TABLE_LIST **save_query_tables_own_last= thd->lex->query_tables_own_last;
7417 DBUG_ENTER(
"multi_delete_precheck");
7423 for (
TABLE_LIST *tl= aux_tables; tl; tl= tl->next_global)
7428 if (tl->correspondent_table)
7429 tl->table= tl->correspondent_table->table;
7433 DBUG_ASSERT(aux_tables != 0);
7442 thd->lex->query_tables_own_last= 0;
7445 thd->lex->query_tables_own_last= save_query_tables_own_last;
7448 thd->lex->query_tables_own_last= save_query_tables_own_last;
7450 if ((thd->variables.option_bits & OPTION_SAFE_UPDATES) && !select_lex->where)
7452 my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
7453 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
7481 DBUG_ENTER(
"multi_delete_table_match");
7483 for (
TABLE_LIST *elem= tables; elem; elem= elem->next_local)
7487 if (tbl->
is_fqtn && elem->is_alias)
7489 if (tbl->
is_fqtn && elem->is_fqtn)
7490 cmp= my_strcasecmp(table_alias_charset, tbl->table_name, elem->table_name) ||
7491 strcmp(tbl->db, elem->db);
7492 else if (elem->is_alias)
7493 cmp= my_strcasecmp(table_alias_charset, tbl->alias, elem->alias);
7495 cmp= my_strcasecmp(table_alias_charset, tbl->table_name, elem->table_name) ||
7496 strcmp(tbl->db, elem->db);
7503 my_error(ER_NONUNIQ_TABLE, MYF(0), elem->alias);
7511 my_error(ER_UNKNOWN_TABLE, MYF(0), tbl->table_name,
"MULTI DELETE");
7531 TABLE_LIST *tables= lex->select_lex.table_list.first;
7533 DBUG_ENTER(
"multi_delete_set_locks_and_link_aux_tables");
7535 for (target_tbl= lex->auxiliary_table_list.first;
7536 target_tbl; target_tbl= target_tbl->next_local)
7539 TABLE_LIST *walk= multi_delete_table_match(lex, target_tbl, tables);
7544 target_tbl->table_name= walk->table_name;
7545 target_tbl->table_name_length= walk->table_name_length;
7547 walk->updating= target_tbl->updating;
7548 walk->lock_type= target_tbl->lock_type;
7550 DBUG_ASSERT(walk->lock_type >= TL_WRITE_ALLOW_WRITE);
7551 walk->mdl_request.
set_type(MDL_SHARED_WRITE);
7552 target_tbl->correspondent_table= walk;
7572 DBUG_ENTER(
"update_precheck");
7573 if (thd->lex->select_lex.item_list.elements != thd->lex->value_list.elements)
7575 my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
7596 DBUG_ENTER(
"delete_precheck");
7620 DBUG_ENTER(
"insert_precheck");
7626 ulong privilege= (INSERT_ACL |
7627 (lex->duplicates == DUP_REPLACE ? DELETE_ACL : 0) |
7628 (lex->value_list.elements ? UPDATE_ACL : 0));
7633 if (lex->update_list.elements != lex->value_list.elements)
7635 my_message(ER_WRONG_VALUE_COUNT, ER(ER_WRONG_VALUE_COUNT), MYF(0));
7651 if (lex->create_info.options & HA_LEX_CREATE_TMP_TABLE)
7652 create_table->
open_type= OT_TEMPORARY_ONLY;
7656 if (!lex->select_lex.item_list.elements)
7665 create_table->lock_type= TL_READ;
7687 SELECT_LEX *select_lex= &lex->select_lex;
7690 DBUG_ENTER(
"create_table_precheck");
7697 want_priv= (lex->create_info.options & HA_LEX_CREATE_TMP_TABLE) ?
7699 (CREATE_ACL | (select_lex->item_list.elements ? INSERT_ACL : 0));
7708 if (lex->create_info.merge_list.first)
7746 lex->create_info.merge_list.first,
7747 FALSE, UINT_MAX, FALSE))
7751 if (want_priv != CREATE_TMP_ACL &&
7752 check_grant(thd, want_priv, create_table, FALSE, 1, FALSE))
7755 if (select_lex->item_list.elements)
7762 else if (lex->create_info.options & HA_LEX_CREATE_TABLE_LIKE)
7784 static bool lock_tables_precheck(THD *thd,
TABLE_LIST *tables)
7786 TABLE_LIST *first_not_own_table= thd->lex->first_not_own_table();
7788 for (
TABLE_LIST *table= tables; table != first_not_own_table &&
table;
7789 table= table->next_global)
7791 if (is_temporary_table(table))
7816 if (expr->type() == Item::FUNC_ITEM &&
7817 ((
Item_func *) expr)->functype() == Item_func::NOT_FUNC)
7821 enum_parsing_place place= thd->lex->current_select->parsing_place;
7822 if (arg->is_bool_func() || place == IN_WHERE || place == IN_HAVING)
7831 if ((negated= expr->neg_transformer(thd)) != 0)
7846 const Security_context *sctx= thd->security_ctx;
7848 definer->user.str= (
char *) sctx->priv_user;
7849 definer->user.length= strlen(definer->user.str);
7851 definer->host.str= (
char *) sctx->priv_host;
7852 definer->host.length= strlen(definer->host.str);
7854 definer->password= null_lex_str;
7855 definer->plugin= empty_lex_str;
7856 definer->auth= empty_lex_str;
7857 definer->uses_identified_with_clause=
false;
7858 definer->uses_identified_by_clause=
false;
7859 definer->uses_authentication_string_clause=
false;
7860 definer->uses_identified_by_password_clause=
false;
7882 thd->get_definer(definer);
7910 definer->user= *user_name;
7911 definer->host= *host_name;
7912 definer->password.str= NULL;
7913 definer->password.length= 0;
7914 definer->uses_authentication_string_clause=
false;
7915 definer->uses_identified_by_clause=
false;
7916 definer->uses_identified_by_password_clause=
false;
7917 definer->uses_identified_with_clause=
false;
7936 if (!user->user.str)
7939 if (default_definer)
7947 default_definer->uses_authentication_string_clause=
7948 user->uses_authentication_string_clause;
7949 default_definer->uses_identified_by_clause=
7950 user->uses_identified_by_clause;
7951 default_definer->uses_identified_by_password_clause=
7952 user->uses_identified_by_password_clause;
7953 default_definer->uses_identified_with_clause=
7954 user->uses_identified_with_clause;
7955 default_definer->plugin.str= user->plugin.str;
7956 default_definer->plugin.length= user->plugin.length;
7957 default_definer->auth.str= user->auth.str;
7958 default_definer->auth.length= user->auth.length;
7959 return default_definer;
7984 uint max_byte_length)
7986 if (str->length <= max_byte_length)
7989 my_error(ER_WRONG_STRING_LENGTH, MYF(0), str->str, err_msg, max_byte_length);
8011 bool check_string_char_length(
LEX_STRING *str,
const char *err_msg,
8015 int well_formed_error;
8016 uint res= cs->cset->well_formed_len(cs, str->str, str->str + str->length,
8017 max_char_length, &well_formed_error);
8019 if (!well_formed_error && str->length == res)
8025 my_error(ER_WRONG_STRING_LENGTH, MYF(0), err.ptr(), err_msg, max_char_length);
8045 int test_if_data_home_dir(
const char *dir)
8047 char path[FN_REFLEN];
8049 DBUG_ENTER(
"test_if_data_home_dir");
8054 (void) fn_format(path, dir,
"",
"",
8055 (MY_RETURN_REAL_PATH|MY_RESOLVE_SYMLINKS));
8056 dir_len= strlen(path);
8057 if (mysql_unpacked_real_data_home_len<= dir_len)
8059 if (dir_len > mysql_unpacked_real_data_home_len &&
8060 path[mysql_unpacked_real_data_home_len] != FN_LIBCHAR)
8063 if (lower_case_file_system)
8065 if (!my_strnncoll(default_charset_info, (
const uchar*) path,
8066 mysql_unpacked_real_data_home_len,
8067 (
const uchar*) mysql_unpacked_real_data_home,
8068 mysql_unpacked_real_data_home_len))
8071 else if (!memcmp(path, mysql_unpacked_real_data_home,
8072 mysql_unpacked_real_data_home_len))
8094 const char *name= str->str;
8095 const char *end= str->str + str->length;
8103 my_printf_error(ER_UNKNOWN_ERROR,
8104 "Malformed hostname (illegal symbol: '%c')", MYF(0),
8114 extern int MYSQLparse(
void *thd);
8131 Parser_state *parser_state,
8135 DBUG_ASSERT(thd->m_parser_state == NULL);
8136 DBUG_ASSERT(thd->lex->m_sql_cmd == NULL);
8138 MYSQL_QUERY_PARSE_START(thd->query());
8144 backup_ctx= creation_ctx->set_n_backup(thd);
8148 thd->m_parser_state= parser_state;
8150 #ifdef HAVE_PSI_STATEMENT_DIGEST_INTERFACE
8152 thd->m_parser_state->m_lip.m_digest_psi= MYSQL_DIGEST_START(thd->m_statement_psi);
8157 bool mysql_parse_status= MYSQLparse(thd) != 0;
8170 DBUG_ASSERT(!mysql_parse_status ||
8171 (mysql_parse_status && thd->is_error()) ||
8172 (mysql_parse_status && thd->get_internal_handler()));
8176 thd->m_parser_state= NULL;
8181 creation_ctx->restore_env(thd, backup_ctx);
8185 ret_value= mysql_parse_status || thd->is_fatal_error;
8186 MYSQL_QUERY_PARSE_DONE(ret_value);
8218 if (!my_charset_same(cs, cl))
8220 my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0), cl->name, cs->csname);