MySQL  8.0.18
Source Code Documentation
sql_trigger.h
Go to the documentation of this file.
1 #ifndef SQL_TRIGGER_INCLUDED
2 #define SQL_TRIGGER_INCLUDED
3 
4 /*
5  Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License, version 2.0,
9  as published by the Free Software Foundation.
10 
11  This program is also distributed with certain software (including
12  but not limited to OpenSSL) that is licensed under separate terms,
13  as designated in a particular file or component or in included license
14  documentation. The authors of MySQL hereby grant you an additional
15  permission to link the program and your derivative works with the
16  separately licensed software that they have included with MySQL.
17 
18  This program is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License, version 2.0, for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26 
27 ///////////////////////////////////////////////////////////////////////////
28 
29 /**
30  @file
31 
32  @brief
33  This file contains declarations of global public functions which are used
34  directly from parser/executioner to perform basic operations on triggers
35  (CREATE TRIGGER, DROP TRIGGER, ALTER TABLE, DROP TABLE, ...)
36 */
37 
38 ///////////////////////////////////////////////////////////////////////////
39 
40 #include "lex_string.h"
41 #include "my_psi_config.h"
42 #include "my_sqlcommand.h" // SQLCOM_CREATE_TRIGGER, SQLCOM_DROP_TRIGGER
43 #include "sql/mdl.h" // enum_mdl_type
44 #include "sql/sql_cmd.h" // Sql_cmd
45 
46 class THD;
47 struct TABLE;
48 struct TABLE_LIST;
49 
50 namespace dd {
51 class Table;
52 }
53 ///////////////////////////////////////////////////////////////////////////
54 
55 /**
56  Find trigger's table from trigger identifier.
57 
58  @param[in] thd Thread context.
59  @param[in] db_name Schema name.
60  @param[in] trigger_name Trigger name.
61  @param[in] continue_if_not_exist true if SQL statement contains
62  "IF EXISTS" clause. That means a warning
63  instead of error should be thrown if trigger
64  with given name does not exist.
65  @param[out] table Pointer to TABLE_LIST object for the
66  table trigger.
67 
68  @return Operation status
69  @retval false On success.
70  @retval true Otherwise.
71 */
72 
74  const LEX_STRING &trigger_name,
75  bool continue_if_not_exist, TABLE_LIST **table);
76 
77 /**
78  Drop all triggers for table.
79 
80  @param thd current thread context
81  @param db_name name of the table schema
82  @param table_name table name
83 
84  @return Operation status.
85  @retval false Success
86  @retval true Failure
87 */
88 
89 bool drop_all_triggers(THD *thd, const char *db_name, const char *table_name);
90 
91 /**
92  Check for table with triggers that old database name and new database name
93  are the same. This functions is called while handling the statement
94  RENAME TABLE to ensure that table moved within the same database.
95 
96  @param[in] db_name Schema name.
97  @param[in] table Table.
98  @param[in] new_db_name New schema name
99 
100  @note
101  Set error ER_TRG_IN_WRONG_SCHEMA in Diagnostics_area in case
102  function returns true.
103 
104  @return Operation status
105  @retval false Either there is no triggers assigned to a table or
106  old and new schema name are the same.
107  @retval true Old and new schema name aren't the same.
108 */
109 
111  const dd::Table &table,
112  const char *new_db_name);
113 
114 /**
115  Reload triggers from DD for specified table.
116 
117  @param[in] thd Thread context
118  @param[in] db_name Current database of subject table
119  @param[in] table_alias Current alias of subject table
120  @param[in] table_name Current name of subject table
121  @param[in] new_db_name New database for subject table
122  @param[in] new_table_name New name of subject table
123 
124  @note
125  This method assumes that subject table is not renamed to itself.
126 
127  @note
128  This method needs to be called under an exclusive table metadata lock.
129 
130  @return Operation status.
131  @retval false Success
132  @retval true Failure
133 */
134 
135 bool reload_triggers_for_table(THD *thd, const char *db_name,
136  const char *table_alias, const char *table_name,
137  const char *new_db_name,
138  const char *new_table_name);
139 
140 /**
141  Acquire either exclusive or shared MDL lock for a trigger
142  in specified schema.
143 
144  @param[in] thd Current thread context
145  @param[in] db Schema name
146  @param[in] trg_name Trigger name
147  @param[in] trigger_name_mdl_type Type of MDL to acquire for trigger name
148 
149  @return Operation status.
150  @retval false Success
151  @retval true Failure
152 */
153 
154 bool acquire_mdl_for_trigger(THD *thd, const char *db, const char *trg_name,
155  enum_mdl_type trigger_name_mdl_type);
156 
157 /**
158  Acquire exclusive MDL lock for a trigger in specified schema.
159 
160  @param[in] thd Current thread context
161  @param[in] db Schema name
162  @param[in] trg_name Trigger name
163 
164  @return Operation status.
165  @retval false Success
166  @retval true Failure
167 */
168 
169 inline bool acquire_exclusive_mdl_for_trigger(THD *thd, const char *db,
170  const char *trg_name) {
171  return acquire_mdl_for_trigger(thd, db, trg_name, MDL_EXCLUSIVE);
172 }
173 
174 /**
175  Acquire shared MDL lock for a trigger in specified schema.
176 
177  @param[in] thd Current thread context
178  @param[in] db Schema name
179  @param[in] trg_name Trigger name
180 
181  @return Operation status.
182  @retval false Success
183  @retval true Failure
184 */
185 
186 inline bool acquire_shared_mdl_for_trigger(THD *thd, const char *db,
187  const char *trg_name) {
188  return acquire_mdl_for_trigger(thd, db, trg_name, MDL_SHARED_HIGH_PRIO);
189 }
190 
191 /**
192  Drop statistics from performance schema for every trigger
193  associated with a table.
194 
195  @param schema_name Name of schema containing the table.
196  @param table Table reference, for that associated
197  triggers statistics has to be deleted.
198 */
199 
200 #ifdef HAVE_PSI_SP_INTERFACE
201 void remove_all_triggers_from_perfschema(const char *schema_name,
202  const dd::Table &table);
203 #endif
204 ///////////////////////////////////////////////////////////////////////////
205 
206 /**
207  This class has common code for CREATE/DROP TRIGGER statements.
208 */
209 
211  public:
212  /**
213  Set a table associated with a trigger.
214 
215  @param trigger_table a table associated with a trigger.
216  */
217 
218  void set_table(TABLE_LIST *trigger_table) { m_trigger_table = trigger_table; }
219 
220  protected:
222 
223  bool check_trg_priv_on_subj_table(THD *thd, TABLE_LIST *table) const;
225  MDL_ticket **mdl_ticket) const;
226 
227  /**
228  Restore original state of meta-data locks.
229 
230  @param thd current thread context
231  @param mdl_ticket granted metadata lock
232  */
233 
234  void restore_original_mdl_state(THD *thd, MDL_ticket *mdl_ticket) const;
235 
237 };
238 
239 /**
240  This class implements the CREATE TRIGGER statement.
241 */
242 
244  public:
245  /**
246  Return the command code for CREATE TRIGGER
247  */
248 
250  return SQLCOM_CREATE_TRIGGER;
251  }
252 
253  virtual bool execute(THD *thd);
254 };
255 
256 /**
257  This class implements the DROP TRIGGER statement.
258 */
259 
261  public:
262  /**
263  Return the command code for DROP TRIGGER
264  */
265 
267  return SQLCOM_DROP_TRIGGER;
268  }
269 
270  virtual bool execute(THD *thd);
271 };
272 
273 #endif /* SQL_TRIGGER_INCLUDED */
Definition: my_sqlcommand.h:150
Representation of an SQL command.
Definition: sql_cmd.h:63
A granted metadata lock.
Definition: mdl.h:963
void remove_all_triggers_from_perfschema(const char *schema_name, const dd::Table &table)
Drop statistics from performance schema for every trigger associated with a table.
Definition: sql_trigger.cc:176
const char * db_name
Definition: rules_table_service.cc:54
Definition: mysql_lex_string.h:34
bool acquire_exclusive_mdl_for_trigger(THD *thd, const char *db, const char *trg_name)
Acquire exclusive MDL lock for a trigger in specified schema.
Definition: sql_trigger.h:169
TABLE_LIST * m_trigger_table
Definition: sql_trigger.h:236
Definition: mdl.h:309
virtual bool execute(THD *thd)
Execute CREATE TRIGGER statement.
Definition: sql_trigger.cc:421
enum_mdl_type
Type of metadata lock request.
Definition: mdl.h:179
enum_sql_command
Definition: my_sqlcommand.h:45
Definition: mysql_lex_string.h:39
bool check_trg_priv_on_subj_table(THD *thd, TABLE_LIST *table) const
Check that the user has TRIGGER privilege on the subject table.
Definition: sql_trigger.cc:287
Representation of an SQL command.
Definition: table.h:1301
bool check_table_triggers_are_not_in_the_same_schema(const char *db_name, const dd::Table &table, const char *new_db_name)
Check for table with triggers that old database name and new database name are the same...
Definition: sql_trigger.cc:186
bool acquire_shared_mdl_for_trigger(THD *thd, const char *db, const char *trg_name)
Acquire shared MDL lock for a trigger in specified schema.
Definition: sql_trigger.h:186
bool reload_triggers_for_table(THD *thd, const char *db_name, const char *table_alias, const char *table_name, const char *new_db_name, const char *new_table_name)
Reload triggers from DD for specified table.
Definition: sql_trigger.cc:205
void set_table(TABLE_LIST *trigger_table)
Set a table associated with a trigger.
Definition: sql_trigger.h:218
This class implements the DROP TRIGGER statement.
Definition: sql_trigger.h:260
TABLE * open_and_lock_subj_table(THD *thd, TABLE_LIST *tables, MDL_ticket **mdl_ticket) const
Open and lock a table associated with a trigger.
Definition: sql_trigger.cc:310
Definition: mdl.h:231
void restore_original_mdl_state(THD *thd, MDL_ticket *mdl_ticket) const
Restore original state of meta-data locks.
Definition: sql_trigger.cc:390
This class has common code for CREATE/DROP TRIGGER statements.
Definition: sql_trigger.h:210
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
Definition: my_sqlcommand.h:151
virtual enum_sql_command sql_command_code() const
Return the command code for DROP TRIGGER.
Definition: sql_trigger.h:266
bool get_table_for_trigger(THD *thd, const LEX_CSTRING &db_name, const LEX_STRING &trigger_name, bool continue_if_not_exist, TABLE_LIST **table)
Find trigger's table from trigger identifier.
Definition: sql_trigger.cc:76
virtual enum_sql_command sql_command_code() const
Return the command code for CREATE TRIGGER.
Definition: sql_trigger.h:249
Sql_cmd_ddl_trigger_common()
Definition: sql_trigger.h:221
bool drop_all_triggers(THD *thd, const char *db_name, const char *table_name)
Drop all triggers for table.
Definition: sql_trigger.cc:145
virtual bool execute(THD *thd)
Execute DROP TRIGGER statement.
Definition: sql_trigger.cc:538
This class implements the CREATE TRIGGER statement.
Definition: sql_trigger.h:243
Definition: test_sql_stmt.cc:150
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:39
Definition: table.h:2468
Definition: table.h:43
bool acquire_mdl_for_trigger(THD *thd, const char *db, const char *trg_name, enum_mdl_type trigger_name_mdl_type)
Acquire either exclusive or shared MDL lock for a trigger in specified schema.
Definition: sql_trigger.cc:247
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778
const char * table_name
Definition: rules_table_service.cc:55