00001 /* Copyright (C) 2004-2005 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 00018 /* 00019 This class holds all information about triggers of table. 00020 00021 QQ: Will it be merged into TABLE in future ? 00022 */ 00023 00024 class Table_triggers_list: public Sql_alloc 00025 { 00026 /* Triggers as SPs grouped by event, action_time */ 00027 sp_head *bodies[TRG_EVENT_MAX][TRG_ACTION_MAX]; 00028 /* 00029 Heads of the lists linking items for all fields used in triggers 00030 grouped by event and action_time. 00031 */ 00032 Item_trigger_field *trigger_fields[TRG_EVENT_MAX][TRG_ACTION_MAX]; 00033 /* 00034 Copy of TABLE::Field array with field pointers set to TABLE::record[1] 00035 buffer instead of TABLE::record[0] (used for OLD values in on UPDATE 00036 trigger and DELETE trigger when it is called for REPLACE). 00037 */ 00038 Field **record1_field; 00039 /* 00040 During execution of trigger new_field and old_field should point to the 00041 array of fields representing new or old version of row correspondingly 00042 (so it can point to TABLE::field or to Tale_triggers_list::record1_field) 00043 */ 00044 Field **new_field; 00045 Field **old_field; 00046 /* TABLE instance for which this triggers list object was created */ 00047 TABLE *table; 00048 /* 00049 Names of triggers. 00050 Should correspond to order of triggers on definitions_list, 00051 used in CREATE/DROP TRIGGER for looking up trigger by name. 00052 */ 00053 List<LEX_STRING> names_list; 00054 /* 00055 List of "ON table_name" parts in trigger definitions, used for 00056 updating trigger definitions during RENAME TABLE. 00057 */ 00058 List<LEX_STRING> on_table_names_list; 00059 /* 00060 Key representing triggers for this table in set of all stored 00061 routines used by statement. 00062 TODO: We won't need this member once triggers namespace will be 00063 database-wide instead of table-wide because then we will be able 00064 to use key based on sp_name as for other stored routines. 00065 */ 00066 LEX_STRING sroutines_key; 00067 00068 /* 00069 Grant information for each trigger (pair: subject table, trigger definer). 00070 */ 00071 GRANT_INFO subject_table_grants[TRG_EVENT_MAX][TRG_ACTION_MAX]; 00072 00073 public: 00074 /* 00075 Field responsible for storing triggers definitions in file. 00076 It have to be public because we are using it directly from parser. 00077 */ 00078 List<LEX_STRING> definitions_list; 00079 /* 00080 List of sql modes for triggers 00081 */ 00082 List<ulonglong> definition_modes_list; 00083 00084 List<LEX_STRING> definers_list; 00085 00086 Table_triggers_list(TABLE *table_arg): 00087 record1_field(0), table(table_arg) 00088 { 00089 bzero((char *)bodies, sizeof(bodies)); 00090 bzero((char *)trigger_fields, sizeof(trigger_fields)); 00091 bzero((char *)&subject_table_grants, sizeof(subject_table_grants)); 00092 } 00093 ~Table_triggers_list(); 00094 00095 bool create_trigger(THD *thd, TABLE_LIST *table, 00096 LEX_STRING *definer_user, 00097 LEX_STRING *definer_host); 00098 bool drop_trigger(THD *thd, TABLE_LIST *table); 00099 bool process_triggers(THD *thd, trg_event_type event, 00100 trg_action_time_type time_type, 00101 bool old_row_is_record1); 00102 bool get_trigger_info(THD *thd, trg_event_type event, 00103 trg_action_time_type time_type, 00104 LEX_STRING *trigger_name, LEX_STRING *trigger_stmt, 00105 ulong *sql_mode, 00106 LEX_STRING *definer); 00107 00108 static bool check_n_load(THD *thd, const char *db, const char *table_name, 00109 TABLE *table, bool names_only); 00110 static bool drop_all_triggers(THD *thd, char *db, char *table_name); 00111 static bool change_table_name(THD *thd, const char *db, 00112 const char *old_table, 00113 const char *new_db, 00114 const char *new_table); 00115 bool has_delete_triggers() 00116 { 00117 return (bodies[TRG_EVENT_DELETE][TRG_ACTION_BEFORE] || 00118 bodies[TRG_EVENT_DELETE][TRG_ACTION_AFTER]); 00119 } 00120 00121 bool has_before_update_triggers() 00122 { 00123 return test(bodies[TRG_EVENT_UPDATE][TRG_ACTION_BEFORE]); 00124 } 00125 00126 void set_table(TABLE *new_table); 00127 00128 void mark_fields_used(trg_event_type event); 00129 00130 friend class Item_trigger_field; 00131 friend int sp_cache_routines_and_add_tables_for_triggers(THD *thd, LEX *lex, 00132 TABLE_LIST *table); 00133 00134 private: 00135 bool prepare_record1_accessors(TABLE *table); 00136 LEX_STRING* change_table_name_in_trignames(const char *db_name, 00137 LEX_STRING *new_table_name, 00138 LEX_STRING *stopper); 00139 bool change_table_name_in_triggers(THD *thd, 00140 const char *db_name, 00141 LEX_STRING *old_table_name, 00142 LEX_STRING *new_table_name); 00143 }; 00144 00145 extern const LEX_STRING trg_action_time_type_names[]; 00146 extern const LEX_STRING trg_event_type_names[];
1.4.7
