You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1626 lines
49 KiB

21 years ago
20 years ago
  1. /* Copyright (C) 2004-2005 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  13. #define MYSQL_LEX 1
  14. #include "mysql_priv.h"
  15. #include "sp_head.h"
  16. #include "sql_trigger.h"
  17. #include "parse_file.h"
  18. static const LEX_STRING triggers_file_type=
  19. {(char *) STRING_WITH_LEN("TRIGGERS")};
  20. const char * const triggers_file_ext= ".TRG";
  21. /*
  22. Table of .TRG file field descriptors.
  23. We have here only one field now because in nearest future .TRG
  24. files will be merged into .FRM files (so we don't need something
  25. like md5 or created fields).
  26. */
  27. static File_option triggers_file_parameters[]=
  28. {
  29. {
  30. {(char *) STRING_WITH_LEN("triggers") },
  31. offsetof(class Table_triggers_list, definitions_list),
  32. FILE_OPTIONS_STRLIST
  33. },
  34. {
  35. {(char *) STRING_WITH_LEN("sql_modes") },
  36. offsetof(class Table_triggers_list, definition_modes_list),
  37. FILE_OPTIONS_ULLLIST
  38. },
  39. {
  40. {(char *) STRING_WITH_LEN("definers") },
  41. offsetof(class Table_triggers_list, definers_list),
  42. FILE_OPTIONS_STRLIST
  43. },
  44. { { 0, 0 }, 0, FILE_OPTIONS_STRING }
  45. };
  46. File_option sql_modes_parameters=
  47. {
  48. {(char*) STRING_WITH_LEN("sql_modes") },
  49. offsetof(class Table_triggers_list, definition_modes_list),
  50. FILE_OPTIONS_ULLLIST
  51. };
  52. /*
  53. This must be kept up to date whenever a new option is added to the list
  54. above, as it specifies the number of required parameters of the trigger in
  55. .trg file.
  56. */
  57. static const int TRG_NUM_REQUIRED_PARAMETERS= 4;
  58. /*
  59. Structure representing contents of .TRN file which are used to support
  60. database wide trigger namespace.
  61. */
  62. struct st_trigname
  63. {
  64. LEX_STRING trigger_table;
  65. };
  66. static const LEX_STRING trigname_file_type=
  67. {(char *) STRING_WITH_LEN("TRIGGERNAME")};
  68. const char * const trigname_file_ext= ".TRN";
  69. static File_option trigname_file_parameters[]=
  70. {
  71. {
  72. {(char *) STRING_WITH_LEN("trigger_table")},
  73. offsetof(struct st_trigname, trigger_table),
  74. FILE_OPTIONS_ESTRING
  75. },
  76. { { 0, 0 }, 0, FILE_OPTIONS_STRING }
  77. };
  78. const LEX_STRING trg_action_time_type_names[]=
  79. {
  80. { (char *) STRING_WITH_LEN("BEFORE") },
  81. { (char *) STRING_WITH_LEN("AFTER") }
  82. };
  83. const LEX_STRING trg_event_type_names[]=
  84. {
  85. { (char *) STRING_WITH_LEN("INSERT") },
  86. { (char *) STRING_WITH_LEN("UPDATE") },
  87. { (char *) STRING_WITH_LEN("DELETE") }
  88. };
  89. static TABLE_LIST *add_table_for_trigger(THD *thd, sp_name *trig);
  90. class Handle_old_incorrect_sql_modes_hook: public Unknown_key_hook
  91. {
  92. private:
  93. char *path;
  94. public:
  95. Handle_old_incorrect_sql_modes_hook(char *file_path)
  96. :path(file_path)
  97. {};
  98. virtual bool process_unknown_string(char *&unknown_key, gptr base,
  99. MEM_ROOT *mem_root, char *end);
  100. };
  101. class Handle_old_incorrect_trigger_table_hook: public Unknown_key_hook
  102. {
  103. public:
  104. Handle_old_incorrect_trigger_table_hook(char *file_path,
  105. LEX_STRING *trigger_table_arg)
  106. :path(file_path), trigger_table_value(trigger_table_arg)
  107. {};
  108. virtual bool process_unknown_string(char *&unknown_key, gptr base,
  109. MEM_ROOT *mem_root, char *end);
  110. private:
  111. char *path;
  112. LEX_STRING *trigger_table_value;
  113. };
  114. /*
  115. Create or drop trigger for table.
  116. SYNOPSIS
  117. mysql_create_or_drop_trigger()
  118. thd - current thread context (including trigger definition in LEX)
  119. tables - table list containing one table for which trigger is created.
  120. create - whenever we create (TRUE) or drop (FALSE) trigger
  121. NOTE
  122. This function is mainly responsible for opening and locking of table and
  123. invalidation of all its instances in table cache after trigger creation.
  124. Real work on trigger creation/dropping is done inside Table_triggers_list
  125. methods.
  126. RETURN VALUE
  127. FALSE Success
  128. TRUE error
  129. */
  130. bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create)
  131. {
  132. TABLE *table;
  133. bool result= TRUE;
  134. LEX_STRING definer_user;
  135. LEX_STRING definer_host;
  136. DBUG_ENTER("mysql_create_or_drop_trigger");
  137. /*
  138. QQ: This function could be merged in mysql_alter_table() function
  139. But do we want this ?
  140. */
  141. /*
  142. Note that once we will have check for TRIGGER privilege in place we won't
  143. need second part of condition below, since check_access() function also
  144. checks that db is specified.
  145. */
  146. if (!thd->lex->spname->m_db.length || create && !tables->db_length)
  147. {
  148. my_error(ER_NO_DB_ERROR, MYF(0));
  149. DBUG_RETURN(TRUE);
  150. }
  151. if (!create &&
  152. !(tables= add_table_for_trigger(thd, thd->lex->spname)))
  153. DBUG_RETURN(TRUE);
  154. /* We should have only one table in table list. */
  155. DBUG_ASSERT(tables->next_global == 0);
  156. /*
  157. Check that the user has TRIGGER privilege on the subject table.
  158. */
  159. {
  160. bool err_status;
  161. TABLE_LIST **save_query_tables_own_last= thd->lex->query_tables_own_last;
  162. thd->lex->query_tables_own_last= 0;
  163. err_status= check_table_access(thd, TRIGGER_ACL, tables, 0);
  164. thd->lex->query_tables_own_last= save_query_tables_own_last;
  165. if (err_status)
  166. DBUG_RETURN(TRUE);
  167. }
  168. /*
  169. There is no DETERMINISTIC clause for triggers, so can't check it.
  170. But a trigger can in theory be used to do nasty things (if it supported
  171. DROP for example) so we do the check for privileges. Triggers have the
  172. same nature as functions regarding binlogging: their body is implicitely
  173. binlogged, so they share the same danger, so trust_function_creators
  174. applies to them too.
  175. */
  176. if (!trust_function_creators && mysql_bin_log.is_open() &&
  177. !(thd->security_ctx->master_access & SUPER_ACL))
  178. {
  179. my_error(ER_BINLOG_CREATE_ROUTINE_NEED_SUPER, MYF(0));
  180. DBUG_RETURN(TRUE);
  181. }
  182. /* We do not allow creation of triggers on temporary tables. */
  183. if (create && find_temporary_table(thd, tables))
  184. {
  185. my_error(ER_TRG_ON_VIEW_OR_TEMP_TABLE, MYF(0), tables->alias);
  186. DBUG_RETURN(TRUE);
  187. }
  188. /*
  189. We don't want perform our operations while global read lock is held
  190. so we have to wait until its end and then prevent it from occuring
  191. again until we are done. (Acquiring LOCK_open is not enough because
  192. global read lock is held without helding LOCK_open).
  193. */
  194. if (wait_if_global_read_lock(thd, 0, 1))
  195. DBUG_RETURN(TRUE);
  196. VOID(pthread_mutex_lock(&LOCK_open));
  197. if (lock_table_names(thd, tables))
  198. goto end;
  199. /* We also don't allow creation of triggers on views. */
  200. tables->required_type= FRMTYPE_TABLE;
  201. if (reopen_name_locked_table(thd, tables))
  202. {
  203. unlock_table_name(thd, tables);
  204. goto end;
  205. }
  206. table= tables->table;
  207. if (!table->triggers)
  208. {
  209. if (!create)
  210. {
  211. my_error(ER_TRG_DOES_NOT_EXIST, MYF(0));
  212. goto end;
  213. }
  214. if (!(table->triggers= new (&table->mem_root) Table_triggers_list(table)))
  215. goto end;
  216. }
  217. result= (create ?
  218. table->triggers->create_trigger(thd, tables, &definer_user, &definer_host):
  219. table->triggers->drop_trigger(thd, tables));
  220. end:
  221. VOID(pthread_mutex_unlock(&LOCK_open));
  222. start_waiting_global_read_lock(thd);
  223. if (!result)
  224. {
  225. if (mysql_bin_log.is_open())
  226. {
  227. thd->clear_error();
  228. String log_query(thd->query, thd->query_length, system_charset_info);
  229. if (create)
  230. {
  231. log_query.set((char *) 0, 0, system_charset_info); /* reset log_query */
  232. log_query.append(STRING_WITH_LEN("CREATE "));
  233. if (definer_user.str && definer_host.str)
  234. {
  235. /*
  236. Append definer-clause if the trigger is SUID (a usual trigger in
  237. new MySQL versions).
  238. */
  239. append_definer(thd, &log_query, &definer_user, &definer_host);
  240. }
  241. log_query.append(thd->lex->stmt_definition_begin);
  242. }
  243. /* Such a statement can always go directly to binlog, no trans cache. */
  244. Query_log_event qinfo(thd, log_query.ptr(), log_query.length(), 0, FALSE);
  245. mysql_bin_log.write(&qinfo);
  246. }
  247. send_ok(thd);
  248. }
  249. DBUG_RETURN(result);
  250. }
  251. /*
  252. Create trigger for table.
  253. SYNOPSIS
  254. create_trigger()
  255. thd - current thread context (including trigger definition in
  256. LEX)
  257. tables - table list containing one open table for which the
  258. trigger is created.
  259. definer_user - [out] after a call it points to 0-terminated string or
  260. contains the NULL-string:
  261. - 0-terminated is returned if the trigger is SUID. The
  262. string contains user name part of the actual trigger
  263. definer.
  264. - NULL-string is returned if the trigger is non-SUID.
  265. Anyway, the caller is responsible to provide memory for
  266. storing LEX_STRING object.
  267. definer_host - [out] after a call it points to 0-terminated string or
  268. contains the NULL-string:
  269. - 0-terminated string is returned if the trigger is
  270. SUID. The string contains host name part of the
  271. actual trigger definer.
  272. - NULL-string is returned if the trigger is non-SUID.
  273. Anyway, the caller is responsible to provide memory for
  274. storing LEX_STRING object.
  275. NOTE
  276. - Assumes that trigger name is fully qualified.
  277. - NULL-string means the following LEX_STRING instance:
  278. { str = 0; length = 0 }.
  279. - In other words, definer_user and definer_host should contain
  280. simultaneously NULL-strings (non-SUID/old trigger) or valid strings
  281. (SUID/new trigger).
  282. RETURN VALUE
  283. False - success
  284. True - error
  285. */
  286. bool Table_triggers_list::create_trigger(THD *thd, TABLE_LIST *tables,
  287. LEX_STRING *definer_user,
  288. LEX_STRING *definer_host)
  289. {
  290. LEX *lex= thd->lex;
  291. TABLE *table= tables->table;
  292. char file_buff[FN_REFLEN], trigname_buff[FN_REFLEN];
  293. LEX_STRING file, trigname_file;
  294. LEX_STRING *trg_def, *name;
  295. ulonglong *trg_sql_mode;
  296. char trg_definer_holder[USER_HOST_BUFF_SIZE];
  297. LEX_STRING *trg_definer;
  298. Item_trigger_field *trg_field;
  299. struct st_trigname trigname;
  300. /* Trigger must be in the same schema as target table. */
  301. if (my_strcasecmp(table_alias_charset, table->s->db.str,
  302. lex->spname->m_db.str))
  303. {
  304. my_error(ER_TRG_IN_WRONG_SCHEMA, MYF(0));
  305. return 1;
  306. }
  307. /* We don't allow creation of several triggers of the same type yet */
  308. if (bodies[lex->trg_chistics.event][lex->trg_chistics.action_time])
  309. {
  310. my_message(ER_TRG_ALREADY_EXISTS, ER(ER_TRG_ALREADY_EXISTS), MYF(0));
  311. return 1;
  312. }
  313. if (!lex->definer)
  314. {
  315. /*
  316. DEFINER-clause is missing.
  317. If we are in slave thread, this means that we received CREATE TRIGGER
  318. from the master, that does not support definer in triggers. So, we
  319. should mark this trigger as non-SUID. Note that this does not happen
  320. when we parse triggers' definitions during opening .TRG file.
  321. LEX::definer is ignored in that case.
  322. Otherwise, we should use CURRENT_USER() as definer.
  323. NOTE: when CREATE TRIGGER statement is allowed to be executed in PS/SP,
  324. it will be required to create the definer below in persistent MEM_ROOT
  325. of PS/SP.
  326. */
  327. if (!thd->slave_thread)
  328. {
  329. if (!(lex->definer= create_default_definer(thd)))
  330. return 1;
  331. }
  332. }
  333. /*
  334. If the specified definer differs from the current user, we should check
  335. that the current user has SUPER privilege (in order to create trigger
  336. under another user one must have SUPER privilege).
  337. */
  338. if (lex->definer &&
  339. (strcmp(lex->definer->user.str, thd->security_ctx->priv_user) ||
  340. my_strcasecmp(system_charset_info,
  341. lex->definer->host.str,
  342. thd->security_ctx->priv_host)))
  343. {
  344. if (check_global_access(thd, SUPER_ACL))
  345. {
  346. my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
  347. return TRUE;
  348. }
  349. }
  350. /*
  351. Let us check if all references to fields in old/new versions of row in
  352. this trigger are ok.
  353. NOTE: We do it here more from ease of use standpoint. We still have to
  354. do some checks on each execution. E.g. we can catch privilege changes
  355. only during execution. Also in near future, when we will allow access
  356. to other tables from trigger we won't be able to catch changes in other
  357. tables...
  358. Since we don't plan to access to contents of the fields it does not
  359. matter that we choose for both OLD and NEW values the same versions
  360. of Field objects here.
  361. */
  362. old_field= new_field= table->field;
  363. for (trg_field= (Item_trigger_field *)(lex->trg_table_fields.first);
  364. trg_field; trg_field= trg_field->next_trg_field)
  365. {
  366. /*
  367. NOTE: now we do not check privileges at CREATE TRIGGER time. This will
  368. be changed in the future.
  369. */
  370. trg_field->setup_field(thd, table, NULL);
  371. if (!trg_field->fixed &&
  372. trg_field->fix_fields(thd, (Item **)0))
  373. return 1;
  374. }
  375. /*
  376. Here we are creating file with triggers and save all triggers in it.
  377. sql_create_definition_file() files handles renaming and backup of older
  378. versions
  379. */
  380. file.length= build_table_filename(file_buff, FN_REFLEN-1,
  381. tables->db, tables->table_name,
  382. triggers_file_ext);
  383. file.str= file_buff;
  384. trigname_file.length= build_table_filename(trigname_buff, FN_REFLEN-1,
  385. tables->db,
  386. lex->spname->m_name.str,
  387. trigname_file_ext);
  388. trigname_file.str= trigname_buff;
  389. /* Use the filesystem to enforce trigger namespace constraints. */
  390. if (!access(trigname_buff, F_OK))
  391. {
  392. my_error(ER_TRG_ALREADY_EXISTS, MYF(0));
  393. return 1;
  394. }
  395. trigname.trigger_table.str= tables->table_name;
  396. trigname.trigger_table.length= tables->table_name_length;
  397. if (sql_create_definition_file(NULL, &trigname_file, &trigname_file_type,
  398. (gptr)&trigname, trigname_file_parameters, 0))
  399. return 1;
  400. /*
  401. Soon we will invalidate table object and thus Table_triggers_list object
  402. so don't care about place to which trg_def->ptr points and other
  403. invariants (e.g. we don't bother to update names_list)
  404. QQ: Hmm... probably we should not care about setting up active thread
  405. mem_root too.
  406. */
  407. if (!(trg_def= (LEX_STRING *)alloc_root(&table->mem_root,
  408. sizeof(LEX_STRING))) ||
  409. definitions_list.push_back(trg_def, &table->mem_root) ||
  410. !(trg_sql_mode= (ulonglong*)alloc_root(&table->mem_root,
  411. sizeof(ulonglong))) ||
  412. definition_modes_list.push_back(trg_sql_mode, &table->mem_root) ||
  413. !(trg_definer= (LEX_STRING*) alloc_root(&table->mem_root,
  414. sizeof(LEX_STRING))) ||
  415. definers_list.push_back(trg_definer, &table->mem_root))
  416. goto err_with_cleanup;
  417. trg_def->str= thd->query;
  418. trg_def->length= thd->query_length;
  419. *trg_sql_mode= thd->variables.sql_mode;
  420. #ifndef NO_EMBEDDED_ACCESS_CHECKS
  421. if (lex->definer && !is_acl_user(lex->definer->host.str,
  422. lex->definer->user.str))
  423. {
  424. push_warning_printf(thd,
  425. MYSQL_ERROR::WARN_LEVEL_NOTE,
  426. ER_NO_SUCH_USER,
  427. ER(ER_NO_SUCH_USER),
  428. lex->definer->user.str,
  429. lex->definer->host.str);
  430. }
  431. #endif /* NO_EMBEDDED_ACCESS_CHECKS */
  432. if (lex->definer)
  433. {
  434. /* SUID trigger. */
  435. *definer_user= lex->definer->user;
  436. *definer_host= lex->definer->host;
  437. trg_definer->str= trg_definer_holder;
  438. trg_definer->length= strxmov(trg_definer->str, definer_user->str, "@",
  439. definer_host->str, NullS) - trg_definer->str;
  440. }
  441. else
  442. {
  443. /* non-SUID trigger. */
  444. definer_user->str= 0;
  445. definer_user->length= 0;
  446. definer_host->str= 0;
  447. definer_host->length= 0;
  448. trg_definer->str= (char*) "";
  449. trg_definer->length= 0;
  450. }
  451. if (!sql_create_definition_file(NULL, &file, &triggers_file_type,
  452. (gptr)this, triggers_file_parameters, 0))
  453. return 0;
  454. err_with_cleanup:
  455. my_delete(trigname_buff, MYF(MY_WME));
  456. return 1;
  457. }
  458. /*
  459. Deletes the .TRG file for a table
  460. SYNOPSIS
  461. rm_trigger_file()
  462. path - char buffer of size FN_REFLEN to be used
  463. for constructing path to .TRG file.
  464. db - table's database name
  465. table_name - table's name
  466. RETURN VALUE
  467. False - success
  468. True - error
  469. */
  470. static bool rm_trigger_file(char *path, const char *db,
  471. const char *table_name)
  472. {
  473. build_table_filename(path, FN_REFLEN-1, db, table_name, triggers_file_ext);
  474. return my_delete(path, MYF(MY_WME));
  475. }
  476. /*
  477. Deletes the .TRN file for a trigger
  478. SYNOPSIS
  479. rm_trigname_file()
  480. path - char buffer of size FN_REFLEN to be used
  481. for constructing path to .TRN file.
  482. db - trigger's database name
  483. table_name - trigger's name
  484. RETURN VALUE
  485. False - success
  486. True - error
  487. */
  488. static bool rm_trigname_file(char *path, const char *db,
  489. const char *trigger_name)
  490. {
  491. build_table_filename(path, FN_REFLEN-1, db, trigger_name, trigname_file_ext);
  492. return my_delete(path, MYF(MY_WME));
  493. }
  494. /*
  495. Helper function that saves .TRG file for Table_triggers_list object.
  496. SYNOPSIS
  497. save_trigger_file()
  498. triggers Table_triggers_list object for which file should be saved
  499. db Name of database for subject table
  500. table_name Name of subject table
  501. RETURN VALUE
  502. FALSE Success
  503. TRUE Error
  504. */
  505. static bool save_trigger_file(Table_triggers_list *triggers, const char *db,
  506. const char *table_name)
  507. {
  508. char file_buff[FN_REFLEN];
  509. LEX_STRING file;
  510. file.length= build_table_filename(file_buff, FN_REFLEN-1, db, table_name,
  511. triggers_file_ext);
  512. file.str= file_buff;
  513. return sql_create_definition_file(NULL, &file, &triggers_file_type,
  514. (gptr)triggers, triggers_file_parameters,
  515. 0);
  516. }
  517. /*
  518. Drop trigger for table.
  519. SYNOPSIS
  520. drop_trigger()
  521. thd - current thread context (including trigger definition in LEX)
  522. tables - table list containing one open table for which trigger is
  523. dropped.
  524. RETURN VALUE
  525. False - success
  526. True - error
  527. */
  528. bool Table_triggers_list::drop_trigger(THD *thd, TABLE_LIST *tables)
  529. {
  530. LEX *lex= thd->lex;
  531. LEX_STRING *name;
  532. List_iterator_fast<LEX_STRING> it_name(names_list);
  533. List_iterator<LEX_STRING> it_def(definitions_list);
  534. List_iterator<ulonglong> it_mod(definition_modes_list);
  535. List_iterator<LEX_STRING> it_definer(definers_list);
  536. char path[FN_REFLEN];
  537. while ((name= it_name++))
  538. {
  539. it_def++;
  540. it_mod++;
  541. it_definer++;
  542. if (my_strcasecmp(table_alias_charset, lex->spname->m_name.str,
  543. name->str) == 0)
  544. {
  545. /*
  546. Again we don't care much about other things required for
  547. clean trigger removing since table will be reopened anyway.
  548. */
  549. it_def.remove();
  550. it_mod.remove();
  551. it_definer.remove();
  552. if (definitions_list.is_empty())
  553. {
  554. /*
  555. TODO: Probably instead of removing .TRG file we should move
  556. to archive directory but this should be done as part of
  557. parse_file.cc functionality (because we will need it
  558. elsewhere).
  559. */
  560. if (rm_trigger_file(path, tables->db, tables->table_name))
  561. return 1;
  562. }
  563. else
  564. {
  565. if (save_trigger_file(this, tables->db, tables->table_name))
  566. return 1;
  567. }
  568. if (rm_trigname_file(path, tables->db, lex->spname->m_name.str))
  569. return 1;
  570. return 0;
  571. }
  572. }
  573. my_message(ER_TRG_DOES_NOT_EXIST, ER(ER_TRG_DOES_NOT_EXIST), MYF(0));
  574. return 1;
  575. }
  576. Table_triggers_list::~Table_triggers_list()
  577. {
  578. for (int i= 0; i < (int)TRG_EVENT_MAX; i++)
  579. for (int j= 0; j < (int)TRG_ACTION_MAX; j++)
  580. delete bodies[i][j];
  581. if (record1_field)
  582. for (Field **fld_ptr= record1_field; *fld_ptr; fld_ptr++)
  583. delete *fld_ptr;
  584. }
  585. /*
  586. Prepare array of Field objects referencing to TABLE::record[1] instead
  587. of record[0] (they will represent OLD.* row values in ON UPDATE trigger
  588. and in ON DELETE trigger which will be called during REPLACE execution).
  589. SYNOPSIS
  590. prepare_record1_accessors()
  591. table - pointer to TABLE object for which we are creating fields.
  592. RETURN VALUE
  593. False - success
  594. True - error
  595. */
  596. bool Table_triggers_list::prepare_record1_accessors(TABLE *table)
  597. {
  598. Field **fld, **old_fld;
  599. if (!(record1_field= (Field **)alloc_root(&table->mem_root,
  600. (table->s->fields + 1) *
  601. sizeof(Field*))))
  602. return 1;
  603. for (fld= table->field, old_fld= record1_field; *fld; fld++, old_fld++)
  604. {
  605. /*
  606. QQ: it is supposed that it is ok to use this function for field
  607. cloning...
  608. */
  609. if (!(*old_fld= (*fld)->new_field(&table->mem_root, table)))
  610. return 1;
  611. (*old_fld)->move_field_offset((my_ptrdiff_t)(table->record[1] -
  612. table->record[0]));
  613. }
  614. *old_fld= 0;
  615. return 0;
  616. }
  617. /*
  618. Adjust Table_triggers_list with new TABLE pointer.
  619. SYNOPSIS
  620. set_table()
  621. new_table - new pointer to TABLE instance
  622. */
  623. void Table_triggers_list::set_table(TABLE *new_table)
  624. {
  625. table= new_table;
  626. for (Field **field= table->triggers->record1_field ; *field ; field++)
  627. {
  628. (*field)->table= (*field)->orig_table= new_table;
  629. (*field)->table_name= &new_table->alias;
  630. }
  631. }
  632. /*
  633. Check whenever .TRG file for table exist and load all triggers it contains.
  634. SYNOPSIS
  635. check_n_load()
  636. thd - current thread context
  637. db - table's database name
  638. table_name - table's name
  639. table - pointer to table object
  640. names_only - stop after loading trigger names
  641. RETURN VALUE
  642. False - success
  643. True - error
  644. */
  645. bool Table_triggers_list::check_n_load(THD *thd, const char *db,
  646. const char *table_name, TABLE *table,
  647. bool names_only)
  648. {
  649. char path_buff[FN_REFLEN];
  650. LEX_STRING path;
  651. File_parser *parser;
  652. LEX_STRING save_db;
  653. DBUG_ENTER("Table_triggers_list::check_n_load");
  654. path.length= build_table_filename(path_buff, FN_REFLEN-1,
  655. db, table_name, triggers_file_ext);
  656. path.str= path_buff;
  657. // QQ: should we analyze errno somehow ?
  658. if (access(path_buff, F_OK))
  659. DBUG_RETURN(0);
  660. /*
  661. File exists so we got to load triggers.
  662. FIXME: A lot of things to do here e.g. how about other funcs and being
  663. more paranoical ?
  664. */
  665. if ((parser= sql_parse_prepare(&path, &table->mem_root, 1)))
  666. {
  667. if (is_equal(&triggers_file_type, parser->type()))
  668. {
  669. Table_triggers_list *triggers=
  670. new (&table->mem_root) Table_triggers_list(table);
  671. Handle_old_incorrect_sql_modes_hook sql_modes_hook(path.str);
  672. if (!triggers)
  673. DBUG_RETURN(1);
  674. /*
  675. We don't have the following attributes in old versions of .TRG file, so
  676. we should initialize the list for safety:
  677. - sql_modes;
  678. - definers;
  679. */
  680. triggers->definition_modes_list.empty();
  681. triggers->definers_list.empty();
  682. if (parser->parse((gptr)triggers, &table->mem_root,
  683. triggers_file_parameters,
  684. TRG_NUM_REQUIRED_PARAMETERS,
  685. &sql_modes_hook))
  686. DBUG_RETURN(1);
  687. List_iterator_fast<LEX_STRING> it(triggers->definitions_list);
  688. LEX_STRING *trg_create_str, *trg_name_str;
  689. ulonglong *trg_sql_mode;
  690. if (triggers->definition_modes_list.is_empty() &&
  691. !triggers->definitions_list.is_empty())
  692. {
  693. /*
  694. It is old file format => we should fill list of sql_modes.
  695. We use one mode (current) for all triggers, because we have not
  696. information about mode in old format.
  697. */
  698. if (!(trg_sql_mode= (ulonglong*)alloc_root(&table->mem_root,
  699. sizeof(ulonglong))))
  700. {
  701. DBUG_RETURN(1); // EOM
  702. }
  703. *trg_sql_mode= global_system_variables.sql_mode;
  704. while (it++)
  705. {
  706. if (triggers->definition_modes_list.push_back(trg_sql_mode,
  707. &table->mem_root))
  708. {
  709. DBUG_RETURN(1); // EOM
  710. }
  711. }
  712. it.rewind();
  713. }
  714. if (triggers->definers_list.is_empty() &&
  715. !triggers->definitions_list.is_empty())
  716. {
  717. /*
  718. It is old file format => we should fill list of definers.
  719. If there is no definer information, we should not switch context to
  720. definer when checking privileges. I.e. privileges for such triggers
  721. are checked for "invoker" rather than for "definer".
  722. */
  723. LEX_STRING *trg_definer;
  724. if (! (trg_definer= (LEX_STRING*)alloc_root(&table->mem_root,
  725. sizeof(LEX_STRING))))
  726. DBUG_RETURN(1); // EOM
  727. trg_definer->str= (char*) "";
  728. trg_definer->length= 0;
  729. while (it++)
  730. {
  731. if (triggers->definers_list.push_back(trg_definer,
  732. &table->mem_root))
  733. {
  734. DBUG_RETURN(1); // EOM
  735. }
  736. }
  737. it.rewind();
  738. }
  739. DBUG_ASSERT(triggers->definition_modes_list.elements ==
  740. triggers->definitions_list.elements);
  741. DBUG_ASSERT(triggers->definers_list.elements ==
  742. triggers->definitions_list.elements);
  743. table->triggers= triggers;
  744. /*
  745. Construct key that will represent triggers for this table in the set
  746. of routines used by statement.
  747. */
  748. triggers->sroutines_key.length= 1+strlen(db)+1+strlen(table_name)+1;
  749. if (!(triggers->sroutines_key.str=
  750. alloc_root(&table->mem_root, triggers->sroutines_key.length)))
  751. DBUG_RETURN(1);
  752. triggers->sroutines_key.str[0]= TYPE_ENUM_TRIGGER;
  753. strxmov(triggers->sroutines_key.str+1, db, ".", table_name, NullS);
  754. /*
  755. TODO: This could be avoided if there is no triggers
  756. for UPDATE and DELETE.
  757. */
  758. if (!names_only && triggers->prepare_record1_accessors(table))
  759. DBUG_RETURN(1);
  760. List_iterator_fast<ulonglong> itm(triggers->definition_modes_list);
  761. List_iterator_fast<LEX_STRING> it_definer(triggers->definers_list);
  762. LEX *old_lex= thd->lex, lex;
  763. sp_rcontext *save_spcont= thd->spcont;
  764. ulong save_sql_mode= thd->variables.sql_mode;
  765. LEX_STRING *on_table_name;
  766. thd->lex= &lex;
  767. save_db.str= thd->db;
  768. save_db.length= thd->db_length;
  769. thd->db_length= strlen(db);
  770. thd->db= (char *) db;
  771. while ((trg_create_str= it++))
  772. {
  773. trg_sql_mode= itm++;
  774. LEX_STRING *trg_definer= it_definer++;
  775. thd->variables.sql_mode= (ulong)*trg_sql_mode;
  776. lex_start(thd, (uchar*)trg_create_str->str, trg_create_str->length);
  777. thd->spcont= 0;
  778. if (MYSQLparse((void *)thd) || thd->is_fatal_error)
  779. {
  780. /*
  781. Free lex associated resources.
  782. QQ: Do we really need all this stuff here ?
  783. */
  784. delete lex.sphead;
  785. goto err_with_lex_cleanup;
  786. }
  787. lex.sphead->set_info(0, 0, &lex.sp_chistics, *trg_sql_mode);
  788. triggers->bodies[lex.trg_chistics.event]
  789. [lex.trg_chistics.action_time]= lex.sphead;
  790. if (!trg_definer->length)
  791. {
  792. /*
  793. This trigger was created/imported from the previous version of
  794. MySQL, which does not support triggers definers. We should emit
  795. warning here.
  796. */
  797. push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
  798. ER_TRG_NO_DEFINER, ER(ER_TRG_NO_DEFINER),
  799. (const char*) db,
  800. (const char*) lex.sphead->m_name.str);
  801. /*
  802. Set definer to the '' to correct displaying in the information
  803. schema.
  804. */
  805. lex.sphead->set_definer((char*) "", 0);
  806. /*
  807. Triggers without definer information are executed under the
  808. authorization of the invoker.
  809. */
  810. lex.sphead->m_chistics->suid= SP_IS_NOT_SUID;
  811. }
  812. else
  813. lex.sphead->set_definer(trg_definer->str, trg_definer->length);
  814. if (triggers->names_list.push_back(&lex.sphead->m_name,
  815. &table->mem_root))
  816. goto err_with_lex_cleanup;
  817. if (!(on_table_name= (LEX_STRING*) alloc_root(&table->mem_root,
  818. sizeof(LEX_STRING))))
  819. goto err_with_lex_cleanup;
  820. *on_table_name= lex.ident;
  821. if (triggers->on_table_names_list.push_back(on_table_name, &table->mem_root))
  822. goto err_with_lex_cleanup;
  823. /*
  824. Let us check that we correctly update trigger definitions when we
  825. rename tables with triggers.
  826. */
  827. DBUG_ASSERT(!my_strcasecmp(table_alias_charset, lex.query_tables->db, db) &&
  828. !my_strcasecmp(table_alias_charset, lex.query_tables->table_name,
  829. table_name));
  830. if (names_only)
  831. {
  832. lex_end(&lex);
  833. continue;
  834. }
  835. /*
  836. Let us bind Item_trigger_field objects representing access to fields
  837. in old/new versions of row in trigger to Field objects in table being
  838. opened.
  839. We ignore errors here, because if even something is wrong we still
  840. will be willing to open table to perform some operations (e.g.
  841. SELECT)...
  842. Anyway some things can be checked only during trigger execution.
  843. */
  844. for (Item_trigger_field *trg_field=
  845. (Item_trigger_field *)(lex.trg_table_fields.first);
  846. trg_field;
  847. trg_field= trg_field->next_trg_field)
  848. {
  849. trg_field->setup_field(thd, table,
  850. &triggers->subject_table_grants[lex.trg_chistics.event]
  851. [lex.trg_chistics.action_time]);
  852. }
  853. lex_end(&lex);
  854. }
  855. thd->db= save_db.str;
  856. thd->db_length= save_db.length;
  857. thd->lex= old_lex;
  858. thd->spcont= save_spcont;
  859. thd->variables.sql_mode= save_sql_mode;
  860. DBUG_RETURN(0);
  861. err_with_lex_cleanup:
  862. // QQ: anything else ?
  863. lex_end(&lex);
  864. thd->lex= old_lex;
  865. thd->spcont= save_spcont;
  866. thd->variables.sql_mode= save_sql_mode;
  867. thd->db= save_db.str;
  868. thd->db_length= save_db.length;
  869. DBUG_RETURN(1);
  870. }
  871. /*
  872. We don't care about this error message much because .TRG files will
  873. be merged into .FRM anyway.
  874. */
  875. my_error(ER_WRONG_OBJECT, MYF(0),
  876. table_name, triggers_file_ext+1, "TRIGGER");
  877. DBUG_RETURN(1);
  878. }
  879. DBUG_RETURN(1);
  880. }
  881. /*
  882. Obtains and returns trigger metadata
  883. SYNOPSIS
  884. get_trigger_info()
  885. thd - current thread context
  886. event - trigger event type
  887. time_type - trigger action time
  888. name - returns name of trigger
  889. stmt - returns statement of trigger
  890. sql_mode - returns sql_mode of trigger
  891. definer_user - returns definer/creator of trigger. The caller is
  892. responsible to allocate enough space for storing definer
  893. information.
  894. RETURN VALUE
  895. False - success
  896. True - error
  897. */
  898. bool Table_triggers_list::get_trigger_info(THD *thd, trg_event_type event,
  899. trg_action_time_type time_type,
  900. LEX_STRING *trigger_name,
  901. LEX_STRING *trigger_stmt,
  902. ulong *sql_mode,
  903. LEX_STRING *definer)
  904. {
  905. sp_head *body;
  906. DBUG_ENTER("get_trigger_info");
  907. if ((body= bodies[event][time_type]))
  908. {
  909. *trigger_name= body->m_name;
  910. *trigger_stmt= body->m_body;
  911. *sql_mode= body->m_sql_mode;
  912. if (body->m_chistics->suid == SP_IS_NOT_SUID)
  913. {
  914. definer->str[0]= 0;
  915. definer->length= 0;
  916. }
  917. else
  918. {
  919. definer->length= strxmov(definer->str, body->m_definer_user.str, "@",
  920. body->m_definer_host.str, NullS) - definer->str;
  921. }
  922. DBUG_RETURN(0);
  923. }
  924. DBUG_RETURN(1);
  925. }
  926. /*
  927. Find trigger's table from trigger identifier and add it to
  928. the statement table list.
  929. SYNOPSIS
  930. mysql_table_for_trigger()
  931. thd - current thread context
  932. trig - identifier for trigger
  933. RETURN VALUE
  934. 0 - error
  935. # - pointer to TABLE_LIST object for the table
  936. */
  937. static TABLE_LIST *add_table_for_trigger(THD *thd, sp_name *trig)
  938. {
  939. LEX *lex= thd->lex;
  940. char path_buff[FN_REFLEN];
  941. LEX_STRING path;
  942. File_parser *parser;
  943. struct st_trigname trigname;
  944. Handle_old_incorrect_trigger_table_hook trigger_table_hook(
  945. path_buff, &trigname.trigger_table);
  946. DBUG_ENTER("add_table_for_trigger");
  947. path.length= build_table_filename(path_buff, FN_REFLEN-1,
  948. trig->m_db.str, trig->m_name.str,
  949. trigname_file_ext);
  950. path.str= path_buff;
  951. if (access(path_buff, F_OK))
  952. {
  953. my_error(ER_TRG_DOES_NOT_EXIST, MYF(0));
  954. DBUG_RETURN(0);
  955. }
  956. if (!(parser= sql_parse_prepare(&path, thd->mem_root, 1)))
  957. DBUG_RETURN(0);
  958. if (!is_equal(&trigname_file_type, parser->type()))
  959. {
  960. my_error(ER_WRONG_OBJECT, MYF(0), trig->m_name.str, trigname_file_ext+1,
  961. "TRIGGERNAME");
  962. DBUG_RETURN(0);
  963. }
  964. if (parser->parse((gptr)&trigname, thd->mem_root,
  965. trigname_file_parameters, 1,
  966. &trigger_table_hook))
  967. DBUG_RETURN(0);
  968. /* We need to reset statement table list to be PS/SP friendly. */
  969. lex->query_tables= 0;
  970. lex->query_tables_last= &lex->query_tables;
  971. DBUG_RETURN(sp_add_to_query_tables(thd, lex, trig->m_db.str,
  972. trigname.trigger_table.str, TL_IGNORE));
  973. }
  974. /*
  975. Drop all triggers for table.
  976. SYNOPSIS
  977. drop_all_triggers()
  978. thd - current thread context
  979. db - schema for table
  980. name - name for table
  981. NOTE
  982. The calling thread should hold the LOCK_open mutex;
  983. RETURN VALUE
  984. False - success
  985. True - error
  986. */
  987. bool Table_triggers_list::drop_all_triggers(THD *thd, char *db, char *name)
  988. {
  989. TABLE table;
  990. char path[FN_REFLEN];
  991. bool result= 0;
  992. DBUG_ENTER("drop_all_triggers");
  993. bzero(&table, sizeof(table));
  994. init_alloc_root(&table.mem_root, 8192, 0);
  995. safe_mutex_assert_owner(&LOCK_open);
  996. if (Table_triggers_list::check_n_load(thd, db, name, &table, 1))
  997. {
  998. result= 1;
  999. goto end;
  1000. }
  1001. if (table.triggers)
  1002. {
  1003. LEX_STRING *trigger;
  1004. List_iterator_fast<LEX_STRING> it_name(table.triggers->names_list);
  1005. while ((trigger= it_name++))
  1006. {
  1007. if (rm_trigname_file(path, db, trigger->str))
  1008. {
  1009. /*
  1010. Instead of immediately bailing out with error if we were unable
  1011. to remove .TRN file we will try to drop other files.
  1012. */
  1013. result= 1;
  1014. continue;
  1015. }
  1016. }
  1017. if (rm_trigger_file(path, db, name))
  1018. {
  1019. result= 1;
  1020. goto end;
  1021. }
  1022. }
  1023. end:
  1024. if (table.triggers)
  1025. delete table.triggers;
  1026. free_root(&table.mem_root, MYF(0));
  1027. DBUG_RETURN(result);
  1028. }
  1029. /*
  1030. Update .TRG file after renaming triggers' subject table
  1031. (change name of table in triggers' definitions).
  1032. SYNOPSIS
  1033. change_table_name_in_triggers()
  1034. thd Thread context
  1035. db_name Database of subject table
  1036. old_table_name Old subject table's name
  1037. new_table_name New subject table's name
  1038. RETURN VALUE
  1039. FALSE Success
  1040. TRUE Failure
  1041. */
  1042. bool
  1043. Table_triggers_list::change_table_name_in_triggers(THD *thd,
  1044. const char *db_name,
  1045. LEX_STRING *old_table_name,
  1046. LEX_STRING *new_table_name)
  1047. {
  1048. char path_buff[FN_REFLEN];
  1049. LEX_STRING *def, *on_table_name, new_def;
  1050. ulonglong *sql_mode;
  1051. ulong save_sql_mode= thd->variables.sql_mode;
  1052. List_iterator_fast<LEX_STRING> it_def(definitions_list);
  1053. List_iterator_fast<LEX_STRING> it_on_table_name(on_table_names_list);
  1054. List_iterator_fast<ulonglong> it_mode(definition_modes_list);
  1055. uint on_q_table_name_len, before_on_len;
  1056. String buff;
  1057. DBUG_ASSERT(definitions_list.elements == on_table_names_list.elements &&
  1058. definitions_list.elements == definition_modes_list.elements);
  1059. while ((def= it_def++))
  1060. {
  1061. on_table_name= it_on_table_name++;
  1062. thd->variables.sql_mode= *(it_mode++);
  1063. /* Construct CREATE TRIGGER statement with new table name. */
  1064. buff.length(0);
  1065. before_on_len= on_table_name->str - def->str;
  1066. buff.append(def->str, before_on_len);
  1067. buff.append(STRING_WITH_LEN("ON "));
  1068. append_identifier(thd, &buff, new_table_name->str, new_table_name->length);
  1069. buff.append(STRING_WITH_LEN(" "));
  1070. on_q_table_name_len= buff.length() - before_on_len;
  1071. buff.append(on_table_name->str + on_table_name->length,
  1072. def->length - (before_on_len + on_table_name->length));
  1073. /*
  1074. It is OK to allocate some memory on table's MEM_ROOT since this
  1075. table instance will be thrown out at the end of rename anyway.
  1076. */
  1077. new_def.str= memdup_root(&table->mem_root, buff.ptr(), buff.length());
  1078. new_def.length= buff.length();
  1079. on_table_name->str= new_def.str + before_on_len;
  1080. on_table_name->length= on_q_table_name_len;
  1081. *def= new_def;
  1082. }
  1083. thd->variables.sql_mode= save_sql_mode;
  1084. if (thd->is_fatal_error)
  1085. return TRUE; /* OOM */
  1086. if (save_trigger_file(this, db_name, new_table_name->str))
  1087. return TRUE;
  1088. if (rm_trigger_file(path_buff, db_name, old_table_name->str))
  1089. {
  1090. (void) rm_trigger_file(path_buff, db_name, new_table_name->str);
  1091. return TRUE;
  1092. }
  1093. return FALSE;
  1094. }
  1095. /*
  1096. Iterate though Table_triggers_list::names_list list and update .TRN files
  1097. after renaming triggers' subject table.
  1098. SYNOPSIS
  1099. change_table_name_in_trignames()
  1100. db_name Database of subject table
  1101. new_table_name New subject table's name
  1102. stopper Pointer to Table_triggers_list::names_list at
  1103. which we should stop updating.
  1104. RETURN VALUE
  1105. 0 Success
  1106. non-0 Failure, pointer to Table_triggers_list::names_list element
  1107. for which update failed.
  1108. */
  1109. LEX_STRING*
  1110. Table_triggers_list::change_table_name_in_trignames(const char *db_name,
  1111. LEX_STRING *new_table_name,
  1112. LEX_STRING *stopper)
  1113. {
  1114. char trigname_buff[FN_REFLEN];
  1115. struct st_trigname trigname;
  1116. LEX_STRING trigname_file;
  1117. LEX_STRING *trigger;
  1118. List_iterator_fast<LEX_STRING> it_name(names_list);
  1119. while ((trigger= it_name++) != stopper)
  1120. {
  1121. trigname_file.length= build_table_filename(trigname_buff, FN_REFLEN-1,
  1122. db_name, trigger->str,
  1123. trigname_file_ext);
  1124. trigname_file.str= trigname_buff;
  1125. trigname.trigger_table= *new_table_name;
  1126. if (sql_create_definition_file(NULL, &trigname_file, &trigname_file_type,
  1127. (gptr)&trigname, trigname_file_parameters,
  1128. 0))
  1129. return trigger;
  1130. }
  1131. return 0;
  1132. }
  1133. /*
  1134. Update .TRG and .TRN files after renaming triggers' subject table.
  1135. SYNOPSIS
  1136. change_table_name()
  1137. thd Thread context
  1138. db Old database of subject table
  1139. old_table Old name of subject table
  1140. new_db New database for subject table
  1141. new_table New name of subject table
  1142. NOTE
  1143. This method tries to leave trigger related files in consistent state,
  1144. i.e. it either will complete successfully, or will fail leaving files
  1145. in their initial state.
  1146. Also this method assumes that subject table is not renamed to itself.
  1147. RETURN VALUE
  1148. FALSE Success
  1149. TRUE Error
  1150. */
  1151. bool Table_triggers_list::change_table_name(THD *thd, const char *db,
  1152. const char *old_table,
  1153. const char *new_db,
  1154. const char *new_table)
  1155. {
  1156. TABLE table;
  1157. bool result= 0;
  1158. LEX_STRING *err_trigname;
  1159. DBUG_ENTER("change_table_name");
  1160. bzero(&table, sizeof(table));
  1161. init_alloc_root(&table.mem_root, 8192, 0);
  1162. safe_mutex_assert_owner(&LOCK_open);
  1163. DBUG_ASSERT(my_strcasecmp(table_alias_charset, db, new_db) ||
  1164. my_strcasecmp(table_alias_charset, old_table, new_table));
  1165. if (Table_triggers_list::check_n_load(thd, db, old_table, &table, TRUE))
  1166. {
  1167. result= 1;
  1168. goto end;
  1169. }
  1170. if (table.triggers)
  1171. {
  1172. LEX_STRING old_table_name= { (char *) old_table, strlen(old_table) };
  1173. LEX_STRING new_table_name= { (char *) new_table, strlen(new_table) };
  1174. /*
  1175. Since triggers should be in the same schema as their subject tables
  1176. moving table with them between two schemas raises too many questions.
  1177. (E.g. what should happen if in new schema we already have trigger
  1178. with same name ?).
  1179. */
  1180. if (my_strcasecmp(table_alias_charset, db, new_db))
  1181. {
  1182. my_error(ER_TRG_IN_WRONG_SCHEMA, MYF(0));
  1183. result= 1;
  1184. goto end;
  1185. }
  1186. if (table.triggers->change_table_name_in_triggers(thd, db,
  1187. &old_table_name,
  1188. &new_table_name))
  1189. {
  1190. result= 1;
  1191. goto end;
  1192. }
  1193. if ((err_trigname= table.triggers->change_table_name_in_trignames(
  1194. db, &new_table_name, 0)))
  1195. {
  1196. /*
  1197. If we were unable to update one of .TRN files properly we will
  1198. revert all changes that we have done and report about error.
  1199. We assume that we will be able to undo our changes without errors
  1200. (we can't do much if there will be an error anyway).
  1201. */
  1202. (void) table.triggers->change_table_name_in_trignames(db,
  1203. &old_table_name,
  1204. err_trigname);
  1205. (void) table.triggers->change_table_name_in_triggers(thd, db,
  1206. &new_table_name,
  1207. &old_table_name);
  1208. result= 1;
  1209. goto end;
  1210. }
  1211. }
  1212. end:
  1213. delete table.triggers;
  1214. free_root(&table.mem_root, MYF(0));
  1215. DBUG_RETURN(result);
  1216. }
  1217. bool Table_triggers_list::process_triggers(THD *thd, trg_event_type event,
  1218. trg_action_time_type time_type,
  1219. bool old_row_is_record1)
  1220. {
  1221. bool err_status= FALSE;
  1222. sp_head *sp_trigger= bodies[event][time_type];
  1223. if (sp_trigger)
  1224. {
  1225. Sub_statement_state statement_state;
  1226. if (old_row_is_record1)
  1227. {
  1228. old_field= record1_field;
  1229. new_field= table->field;
  1230. }
  1231. else
  1232. {
  1233. new_field= record1_field;
  1234. old_field= table->field;
  1235. }
  1236. #ifndef NO_EMBEDDED_ACCESS_CHECKS
  1237. Security_context *save_ctx;
  1238. if (sp_change_security_context(thd, sp_trigger, &save_ctx))
  1239. return TRUE;
  1240. /*
  1241. Fetch information about table-level privileges to GRANT_INFO structure for
  1242. subject table. Check of privileges that will use it and information about
  1243. column-level privileges will happen in Item_trigger_field::fix_fields().
  1244. */
  1245. fill_effective_table_privileges(thd,
  1246. &subject_table_grants[event][time_type],
  1247. table->s->db.str, table->s->table_name.str);
  1248. /* Check that the definer has TRIGGER privilege on the subject table. */
  1249. if (!(subject_table_grants[event][time_type].privilege & TRIGGER_ACL))
  1250. {
  1251. char priv_desc[128];
  1252. get_privilege_desc(priv_desc, sizeof(priv_desc), TRIGGER_ACL);
  1253. my_error(ER_TABLEACCESS_DENIED_ERROR, MYF(0), priv_desc,
  1254. thd->security_ctx->priv_user, thd->security_ctx->host_or_ip,
  1255. table->s->table_name.str);
  1256. sp_restore_security_context(thd, save_ctx);
  1257. return TRUE;
  1258. }
  1259. #endif // NO_EMBEDDED_ACCESS_CHECKS
  1260. thd->reset_sub_statement_state(&statement_state, SUB_STMT_TRIGGER);
  1261. err_status= sp_trigger->execute_function(thd, 0, 0, 0);
  1262. thd->restore_sub_statement_state(&statement_state);
  1263. #ifndef NO_EMBEDDED_ACCESS_CHECKS
  1264. sp_restore_security_context(thd, save_ctx);
  1265. #endif // NO_EMBEDDED_ACCESS_CHECKS
  1266. }
  1267. return err_status;
  1268. }
  1269. /*
  1270. Trigger BUG#14090 compatibility hook
  1271. SYNOPSIS
  1272. Handle_old_incorrect_sql_modes_hook::process_unknown_string()
  1273. unknown_key [in/out] reference on the line with unknown
  1274. parameter and the parsing point
  1275. base [in] base address for parameter writing (structure
  1276. like TABLE)
  1277. mem_root [in] MEM_ROOT for parameters allocation
  1278. end [in] the end of the configuration
  1279. NOTE: this hook process back compatibility for incorrectly written
  1280. sql_modes parameter (see BUG#14090).
  1281. RETURN
  1282. FALSE OK
  1283. TRUE Error
  1284. */
  1285. #define INVALID_SQL_MODES_LENGTH 13
  1286. bool
  1287. Handle_old_incorrect_sql_modes_hook::process_unknown_string(char *&unknown_key,
  1288. gptr base,
  1289. MEM_ROOT *mem_root,
  1290. char *end)
  1291. {
  1292. DBUG_ENTER("Handle_old_incorrect_sql_modes_hook::process_unknown_string");
  1293. DBUG_PRINT("info", ("unknown key:%60s", unknown_key));
  1294. if (unknown_key + INVALID_SQL_MODES_LENGTH + 1 < end &&
  1295. unknown_key[INVALID_SQL_MODES_LENGTH] == '=' &&
  1296. !memcmp(unknown_key, STRING_WITH_LEN("sql_modes")))
  1297. {
  1298. char *ptr= unknown_key + INVALID_SQL_MODES_LENGTH + 1;
  1299. DBUG_PRINT("info", ("sql_modes affected by BUG#14090 detected"));
  1300. push_warning_printf(current_thd,
  1301. MYSQL_ERROR::WARN_LEVEL_NOTE,
  1302. ER_OLD_FILE_FORMAT,
  1303. ER(ER_OLD_FILE_FORMAT),
  1304. (char *)path, "TRIGGER");
  1305. if (get_file_options_ulllist(ptr, end, unknown_key, base,
  1306. &sql_modes_parameters, mem_root))
  1307. {
  1308. DBUG_RETURN(TRUE);
  1309. }
  1310. /*
  1311. Set parsing pointer to the last symbol of string (\n)
  1312. 1) to avoid problem with \0 in the junk after sql_modes
  1313. 2) to speed up skipping this line by parser.
  1314. */
  1315. unknown_key= ptr-1;
  1316. }
  1317. DBUG_RETURN(FALSE);
  1318. }
  1319. /*
  1320. Trigger BUG#15921 compatibility hook. For details see
  1321. Handle_old_incorrect_sql_modes_hook::process_unknown_string().
  1322. */
  1323. #define INVALID_TRIGGER_TABLE_LENGTH 15
  1324. bool
  1325. Handle_old_incorrect_trigger_table_hook::
  1326. process_unknown_string(char *&unknown_key, gptr base, MEM_ROOT *mem_root,
  1327. char *end)
  1328. {
  1329. DBUG_ENTER("Handle_old_incorrect_trigger_table_hook::process_unknown_string");
  1330. DBUG_PRINT("info", ("unknown key:%60s", unknown_key));
  1331. if (unknown_key + INVALID_TRIGGER_TABLE_LENGTH + 1 < end &&
  1332. unknown_key[INVALID_TRIGGER_TABLE_LENGTH] == '=' &&
  1333. !memcmp(unknown_key, STRING_WITH_LEN("trigger_table")))
  1334. {
  1335. char *ptr= unknown_key + INVALID_TRIGGER_TABLE_LENGTH + 1;
  1336. DBUG_PRINT("info", ("trigger_table affected by BUG#15921 detected"));
  1337. push_warning_printf(current_thd,
  1338. MYSQL_ERROR::WARN_LEVEL_NOTE,
  1339. ER_OLD_FILE_FORMAT,
  1340. ER(ER_OLD_FILE_FORMAT),
  1341. (char *)path, "TRIGGER");
  1342. if (!(ptr= parse_escaped_string(ptr, end, mem_root, trigger_table_value)))
  1343. {
  1344. my_error(ER_FPARSER_ERROR_IN_PARAMETER, MYF(0), "trigger_table",
  1345. unknown_key);
  1346. DBUG_RETURN(TRUE);
  1347. }
  1348. /* Set parsing pointer to the last symbol of string (\n). */
  1349. unknown_key= ptr-1;
  1350. }
  1351. DBUG_RETURN(FALSE);
  1352. }