MySQL 8.0.39
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, 2024, Oracle and/or its affiliates.
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 designed to work 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 either included with
17 the program or referenced in the documentation.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License, version 2.0, for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
27
28///////////////////////////////////////////////////////////////////////////
29
30/**
31 @file
32
33 @brief
34 This file contains declarations of global public functions which are used
35 directly from parser/executioner to perform basic operations on triggers
36 (CREATE TRIGGER, DROP TRIGGER, ALTER TABLE, DROP TABLE, ...)
37*/
38
39///////////////////////////////////////////////////////////////////////////
40
41#include "lex_string.h"
42#include "my_psi_config.h"
43#include "my_sqlcommand.h" // SQLCOM_CREATE_TRIGGER, SQLCOM_DROP_TRIGGER
44#include "sql/mdl.h" // enum_mdl_type
45#include "sql/sql_cmd.h" // Sql_cmd
46
47class THD;
48struct TABLE;
49class Table_ref;
50
51namespace dd {
52class Table;
53}
54///////////////////////////////////////////////////////////////////////////
55
56/**
57 Find trigger's table from trigger identifier.
58
59 @param[in] thd Thread context.
60 @param[in] db_name Schema name.
61 @param[in] trigger_name Trigger name.
62 @param[in] continue_if_not_exist true if SQL statement contains
63 "IF EXISTS" clause. That means a warning
64 instead of error should be thrown if trigger
65 with given name does not exist.
66 @param[out] table Pointer to Table_ref object for the
67 table trigger.
68
69 @return Operation status
70 @retval false On success.
71 @retval true Otherwise.
72*/
73
75 const LEX_STRING &trigger_name,
76 bool continue_if_not_exist, Table_ref **table);
77
78/**
79 Check for table with triggers that old database name and new database name
80 are the same. This functions is called while handling the statement
81 RENAME TABLE to ensure that table moved within the same database.
82
83 @param[in] db_name Schema name.
84 @param[in] table Table.
85 @param[in] new_db_name New schema name
86
87 @note
88 Set error ER_TRG_IN_WRONG_SCHEMA in Diagnostics_area in case
89 function returns true.
90
91 @return Operation status
92 @retval false Either there is no triggers assigned to a table or
93 old and new schema name are the same.
94 @retval true Old and new schema name aren't the same.
95*/
96
98 const dd::Table &table,
99 const char *new_db_name);
100
101/**
102 Acquire either exclusive or shared MDL lock for a trigger
103 in specified schema.
104
105 @param[in] thd Current thread context
106 @param[in] db Schema name
107 @param[in] trg_name Trigger name
108 @param[in] trigger_name_mdl_type Type of MDL to acquire for trigger name
109
110 @return Operation status.
111 @retval false Success
112 @retval true Failure
113*/
114
115bool acquire_mdl_for_trigger(THD *thd, const char *db, const char *trg_name,
116 enum_mdl_type trigger_name_mdl_type);
117
118/**
119 Acquire exclusive MDL lock for a trigger in specified schema.
120
121 @param[in] thd Current thread context
122 @param[in] db Schema name
123 @param[in] trg_name Trigger name
124
125 @return Operation status.
126 @retval false Success
127 @retval true Failure
128*/
129
130bool acquire_exclusive_mdl_for_trigger(THD *thd, const char *db,
131 const char *trg_name);
132/**
133 Acquire shared MDL lock for a trigger in specified schema.
134
135 @param[in] thd Current thread context
136 @param[in] db Schema name
137 @param[in] trg_name Trigger name
138
139 @return Operation status.
140 @retval false Success
141 @retval true Failure
142*/
143
144inline bool acquire_shared_mdl_for_trigger(THD *thd, const char *db,
145 const char *trg_name) {
146 return acquire_mdl_for_trigger(thd, db, trg_name, MDL_SHARED_HIGH_PRIO);
147}
148
149/**
150 Drop statistics from performance schema for every trigger
151 associated with a table.
152
153 @param schema_name Name of schema containing the table.
154 @param table Table reference, for that associated
155 triggers statistics has to be deleted.
156*/
157
158#ifdef HAVE_PSI_SP_INTERFACE
159void remove_all_triggers_from_perfschema(const char *schema_name,
160 const dd::Table &table);
161#endif
162///////////////////////////////////////////////////////////////////////////
163
164/**
165 This class has common code for CREATE/DROP TRIGGER statements.
166*/
167
169 public:
170 /**
171 Set a table associated with a trigger.
172
173 @param trigger_table a table associated with a trigger.
174 */
175
176 void set_table(Table_ref *trigger_table) { m_trigger_table = trigger_table; }
177
178 protected:
180
181 bool check_trg_priv_on_subj_table(THD *thd, Table_ref *table) const;
183 MDL_ticket **mdl_ticket) const;
184
185 /**
186 Restore original state of meta-data locks.
187
188 @param thd current thread context
189 @param mdl_ticket granted metadata lock
190 */
191
192 void restore_original_mdl_state(THD *thd, MDL_ticket *mdl_ticket) const;
193
195};
196
197/**
198 This class implements the CREATE TRIGGER statement.
199*/
200
202 public:
203 /**
204 Return the command code for CREATE TRIGGER
205 */
206
209 }
210
211 bool execute(THD *thd) final;
212};
213
214/**
215 This class implements the DROP TRIGGER statement.
216*/
217
219 public:
220 /**
221 Return the command code for DROP TRIGGER
222 */
223
225 return SQLCOM_DROP_TRIGGER;
226 }
227
228 bool execute(THD *thd) final;
229};
230
231#endif /* SQL_TRIGGER_INCLUDED */
A granted metadata lock.
Definition: mdl.h:984
This class implements the CREATE TRIGGER statement.
Definition: sql_trigger.h:201
bool execute(THD *thd) final
Execute CREATE TRIGGER statement.
Definition: sql_trigger.cc:366
enum_sql_command sql_command_code() const final
Return the command code for CREATE TRIGGER.
Definition: sql_trigger.h:207
This class has common code for CREATE/DROP TRIGGER statements.
Definition: sql_trigger.h:168
Table_ref * m_trigger_table
Definition: sql_trigger.h:194
bool check_trg_priv_on_subj_table(THD *thd, Table_ref *table) const
Check that the user has TRIGGER privilege on the subject table.
Definition: sql_trigger.cc:225
Sql_cmd_ddl_trigger_common()
Definition: sql_trigger.h:179
void set_table(Table_ref *trigger_table)
Set a table associated with a trigger.
Definition: sql_trigger.h:176
void restore_original_mdl_state(THD *thd, MDL_ticket *mdl_ticket) const
Restore original state of meta-data locks.
Definition: sql_trigger.cc:335
TABLE * open_and_lock_subj_table(THD *thd, Table_ref *tables, MDL_ticket **mdl_ticket) const
Open and lock a table associated with a trigger.
Definition: sql_trigger.cc:248
This class implements the DROP TRIGGER statement.
Definition: sql_trigger.h:218
bool execute(THD *thd) final
Execute DROP TRIGGER statement.
Definition: sql_trigger.cc:496
enum_sql_command sql_command_code() const final
Return the command code for DROP TRIGGER.
Definition: sql_trigger.h:224
Representation of an SQL command.
Definition: sql_cmd.h:65
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Definition: table.h:2791
Definition: table.h:47
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
Defines various enable/disable and HAVE_ macros related to the performance schema instrumentation sys...
enum_sql_command
Definition: my_sqlcommand.h:46
@ SQLCOM_CREATE_TRIGGER
Definition: my_sqlcommand.h:151
@ SQLCOM_DROP_TRIGGER
Definition: my_sqlcommand.h:152
The version of the current data dictionary table definitions.
Definition: dictionary_client.h:43
const char * db_name
Definition: rules_table_service.cc:55
Representation of an SQL command.
enum_mdl_type
Type of metadata lock request.
Definition: sql_lexer_yacc_state.h:106
@ MDL_SHARED_HIGH_PRIO
Definition: sql_lexer_yacc_state.h:158
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.cc:176
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: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:144
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:148
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:158
bool get_table_for_trigger(THD *thd, const LEX_CSTRING &db_name, const LEX_STRING &trigger_name, bool continue_if_not_exist, Table_ref **table)
Find trigger's table from trigger identifier.
Definition: sql_trigger.cc:79
Definition: mysql_lex_string.h:40
Definition: mysql_lex_string.h:35
Definition: table.h:1399