1 /* 2 Copyright (c) 2014, 2021, Oracle and/or its affiliates. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2.0, 6 as published by the Free Software Foundation. 7 8 This program is also distributed with certain software (including 9 but not limited to OpenSSL) that is licensed under separate terms, 10 as designated in a particular file or component or in included license 11 documentation. The authors of MySQL hereby grant you an additional 12 permission to link the program and your derivative works with the 13 separately licensed software that they have included with MySQL. 14 15 This program is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License, version 2.0, for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this program; if not, write to the Free Software Foundation, 22 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */ 23 24 #ifndef TRIGGER_H_INCLUDED 25 #define TRIGGER_H_INCLUDED 26 27 /////////////////////////////////////////////////////////////////////////// 28 29 #include "sql_alloc.h" 30 #include "trigger_def.h" // enum_trigger_event_type 31 32 struct GRANT_INFO; 33 34 class sp_head; 35 class Stored_program_creation_ctx; 36 struct TABLE; 37 class Query_tables_list; 38 39 typedef ulonglong sql_mode_t; 40 41 /** 42 This class represents a trigger object. 43 Trigger can be created, initialized, parsed and executed. 44 45 Trigger attributes are usually stored on the memory root of the subject table. 46 Trigger object however can exist when the subject table does not. In this 47 case, trigger attributes are stored on a separate memory root. 48 49 Trigger objects are created in two ways: 50 51 1. loading from Data Dictionary (by Trigger_loader) 52 53 In this case Trigger object is initialized in two phases: 54 - from the data which is directly available in TRG-file; 55 - from the data which gets available after parsing CREATE TRIGGER 56 statement (trigger name, ...) 57 58 @see Trigger::create_from_dd(). 59 60 2. creating a new Trigger object that represents the trigger object being 61 created by CREATE TRIGGER statement (by Table_trigger_dispatcher). 62 63 In this case Trigger object is created temporarily. 64 65 @see Trigger::create_from_parser(). 66 */ 67 class Trigger : public Sql_alloc 68 { 69 public: 70 static Trigger *create_from_parser(THD *thd, 71 TABLE *subject_table, 72 String *binlog_create_trigger_stmt); 73 74 static Trigger *create_from_dd(MEM_ROOT *mem_root, 75 const LEX_CSTRING &db_name, 76 const LEX_CSTRING &subject_table_name, 77 const LEX_STRING &definition, 78 sql_mode_t sql_mode, 79 const LEX_STRING &definer, 80 const LEX_STRING &client_cs_name, 81 const LEX_STRING &connection_cl_name, 82 const LEX_STRING &db_cl_name, 83 const longlong *created_timestamp); 84 85 public: 86 bool execute(THD *thd); 87 88 bool parse(THD *thd); 89 90 void add_tables_and_routines(THD *thd, 91 Query_tables_list *prelocking_ctx, 92 TABLE_LIST *table_list); 93 94 void print_upgrade_warning(THD *thd); 95 96 void rename_subject_table(THD *thd, const LEX_STRING &new_table_name); 97 98 public: 99 /************************************************************************ 100 * Attribute accessors. 101 ***********************************************************************/ 102 get_db_name()103 const LEX_CSTRING &get_db_name() const 104 { return m_db_name; } 105 get_subject_table_name()106 const LEX_CSTRING &get_subject_table_name() const 107 { return m_subject_table_name; } 108 get_trigger_name()109 const LEX_STRING &get_trigger_name() const 110 { return m_trigger_name; } 111 get_definition()112 const LEX_STRING &get_definition() const 113 { return m_definition; } 114 get_sql_mode()115 sql_mode_t get_sql_mode() const 116 { return m_sql_mode; } 117 get_definer()118 const LEX_STRING &get_definer() const 119 { return m_definer; } 120 get_on_table_name()121 const LEX_STRING &get_on_table_name() const 122 { return m_on_table_name; } 123 get_client_cs_name()124 const LEX_STRING &get_client_cs_name() const 125 { return m_client_cs_name; } 126 get_connection_cl_name()127 const LEX_STRING &get_connection_cl_name() const 128 { return m_connection_cl_name; } 129 get_db_cl_name()130 const LEX_STRING &get_db_cl_name() const 131 { return m_db_cl_name; } 132 get_event()133 enum_trigger_event_type get_event() const 134 { return m_event; } 135 get_action_time()136 enum_trigger_action_time_type get_action_time() const 137 { return m_action_time; } 138 is_created_timestamp_null()139 bool is_created_timestamp_null() const 140 { return m_created_timestamp == 0; } 141 get_created_timestamp()142 timeval get_created_timestamp() const 143 { 144 timeval timestamp_value; 145 timestamp_value.tv_sec= static_cast<long>(m_created_timestamp / 100); 146 timestamp_value.tv_usec= (m_created_timestamp % 100) * 10000; 147 return timestamp_value; 148 } 149 get_action_order()150 ulonglong get_action_order() const 151 { return m_action_order; } 152 set_action_order(ulonglong action_order)153 void set_action_order(ulonglong action_order) 154 { m_action_order= action_order; } 155 get_sp()156 sp_head *get_sp() 157 { return m_sp; } 158 get_subject_table_grant()159 GRANT_INFO *get_subject_table_grant() 160 { return &m_subject_table_grant; } 161 has_parse_error()162 bool has_parse_error() const 163 { return m_has_parse_error; } 164 get_parse_error_message()165 const char *get_parse_error_message() const 166 { return m_parse_error_message; } 167 168 public: 169 /************************************************************************ 170 * To be used by Trigger_loader only 171 ***********************************************************************/ 172 get_definition_ptr()173 LEX_STRING *get_definition_ptr() 174 { return &m_definition; } 175 get_sql_mode_ptr()176 sql_mode_t *get_sql_mode_ptr() 177 { return &m_sql_mode; } 178 get_definer_ptr()179 LEX_STRING *get_definer_ptr() 180 { return &m_definer; } 181 get_client_cs_name_ptr()182 LEX_STRING *get_client_cs_name_ptr() 183 { return &m_client_cs_name; } 184 get_connection_cl_name_ptr()185 LEX_STRING *get_connection_cl_name_ptr() 186 { return &m_connection_cl_name; } 187 get_db_cl_name_ptr()188 LEX_STRING *get_db_cl_name_ptr() 189 { return &m_db_cl_name; } 190 get_created_timestamp_ptr()191 longlong *get_created_timestamp_ptr() 192 { return &m_created_timestamp; } 193 194 private: 195 Trigger(MEM_ROOT *mem_root, 196 const LEX_CSTRING &db_name, 197 const LEX_CSTRING &table_name, 198 const LEX_STRING &definition, 199 sql_mode_t sql_mode, 200 const LEX_STRING &definer, 201 const LEX_STRING &client_cs_name, 202 const LEX_STRING &connection_cl_name, 203 const LEX_STRING &db_cl_name, 204 enum_trigger_event_type event_type, 205 enum_trigger_action_time_type action_time, 206 longlong created_timestamp); 207 208 public: 209 ~Trigger(); 210 211 private: set_trigger_name(const LEX_STRING & trigger_name)212 void set_trigger_name(const LEX_STRING &trigger_name) 213 { m_trigger_name= trigger_name; } 214 set_parse_error_message(const char * error_message)215 void set_parse_error_message(const char *error_message) 216 { 217 m_has_parse_error= true; 218 strncpy(m_parse_error_message, error_message, 219 sizeof(m_parse_error_message)); 220 } 221 222 private: 223 /** 224 Memory root to store all data of this Trigger object. 225 226 This can be a pointer to the subject table memory root, or it can be a 227 pointer to a dedicated memory root if subject table does not exist. 228 */ 229 MEM_ROOT *m_mem_root; 230 231 private: 232 /************************************************************************ 233 * Mandatory trigger attributes loaded from TRG-file. 234 * All these strings are allocated on m_mem_root. 235 ***********************************************************************/ 236 237 /// Database name. 238 LEX_CSTRING m_db_name; 239 240 /// Table name. 241 LEX_CSTRING m_subject_table_name; 242 243 /// Trigger definition to save in TRG-file. 244 LEX_STRING m_definition; 245 246 /// Trigger sql-mode. 247 sql_mode_t m_sql_mode; 248 249 /// Trigger definer. 250 LEX_STRING m_definer; 251 252 /// Character set context, used for parsing and executing trigger. 253 LEX_STRING m_client_cs_name; 254 255 /// Collation name of the connection within one a trigger are created. 256 LEX_STRING m_connection_cl_name; 257 258 /// Default database collation. 259 LEX_STRING m_db_cl_name; 260 261 /// Trigger event. 262 enum_trigger_event_type m_event; 263 264 /// Trigger action time. 265 enum_trigger_action_time_type m_action_time; 266 267 /** 268 Current time when the trigger was created (measured in milliseconds since 269 since 0 hours, 0 minutes, 0 seconds, January 1, 1970, UTC). This is the 270 value of CREATED attribute. 271 272 There is special value -- zero means CREATED is not set (NULL). 273 */ 274 longlong m_created_timestamp; 275 276 /** 277 Action_order value for the trigger. Action_order is the ordinal position 278 of the trigger in the list of triggers with the same EVENT_MANIPULATION, 279 CONDITION_TIMING, and ACTION_ORIENTATION. 280 281 At the moment action order is not explicitly stored in the TRG-file. Trigger 282 execution order however is mantained by the order of trigger attributes in 283 the TRG-file. This attribute is calculated after loading. 284 */ 285 ulonglong m_action_order; 286 287 private: 288 /************************************************************************ 289 * The following attributes can be set only after parsing trigger definition 290 * statement (CREATE TRIGGER). There is no way to retrieve them directly from 291 * TRG-file. 292 * 293 * All these strings are allocated on the trigger table's mem-root. 294 ***********************************************************************/ 295 296 /// Trigger name. 297 LEX_STRING m_trigger_name; 298 299 /** 300 A pointer to the "ON <table name>" part of the trigger definition. It is 301 used for updating trigger definition in RENAME TABLE. 302 */ 303 LEX_STRING m_on_table_name; 304 305 private: 306 /************************************************************************ 307 * Other attributes. 308 ***********************************************************************/ 309 310 /// Grant information for the trigger. 311 GRANT_INFO m_subject_table_grant; 312 313 /// Pointer to the sp_head corresponding to the trigger. 314 sp_head *m_sp; 315 316 /// This flags specifies whether the trigger has parse error or not. 317 bool m_has_parse_error; 318 319 /** 320 This error will be displayed when the user tries to manipulate or invoke 321 triggers on a table that has broken triggers. It will get set only once 322 per statement and thus will contain the first parse error encountered in 323 the trigger file. 324 */ 325 char m_parse_error_message[MYSQL_ERRMSG_SIZE]; 326 }; 327 328 /////////////////////////////////////////////////////////////////////////// 329 330 #endif // TRIGGER_H_INCLUDED 331 332