MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_trigger.h
1 #ifndef SQL_TRIGGER_INCLUDED
2 #define SQL_TRIGGER_INCLUDED
3 
4 /*
5  Copyright (c) 2004, 2012, 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 as published by
9  the Free Software Foundation; version 2 of the License.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software Foundation,
18  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
19 
20 /* Forward declarations */
21 
22 class Item_trigger_field;
23 class sp_head;
24 class sp_name;
25 class Query_tables_list;
26 struct TABLE_LIST;
27 class Query_tables_list;
28 
30 enum trg_event_type
31 {
32  TRG_EVENT_INSERT= 0,
33  TRG_EVENT_UPDATE= 1,
34  TRG_EVENT_DELETE= 2,
35  TRG_EVENT_MAX
36 };
37 
38 #include "table.h" /* GRANT_INFO */
39 
40 /*
41  We need this two enums here instead of sql_lex.h because
42  at least one of them is used by Item_trigger_field interface.
43 
44  Time when trigger is invoked (i.e. before or after row actually
45  inserted/updated/deleted).
46 */
47 enum trg_action_time_type
48 {
49  TRG_ACTION_BEFORE= 0, TRG_ACTION_AFTER= 1, TRG_ACTION_MAX
50 };
51 
52 
60 {
62  sp_head *bodies[TRG_EVENT_MAX][TRG_ACTION_MAX];
63 
69  Field **record1_field;
70 
76  Field **new_field;
77  Field **old_field;
78 
79 public:
82 
83 private:
89  List<LEX_STRING> names_list;
90 
95  List<LEX_STRING> on_table_names_list;
96 
97 public:
101  GRANT_INFO subject_table_grants[TRG_EVENT_MAX][TRG_ACTION_MAX];
102 
103 private:
115  bool m_has_unparseable_trigger;
116 
123  char m_parse_error_message[MYSQL_ERRMSG_SIZE];
124 
125 public:
131 
136 
137  List<LEX_STRING> definers_list;
138 
139  /* Character set context, used for parsing and executing triggers. */
140 
141  List<LEX_STRING> client_cs_names;
142  List<LEX_STRING> connection_cl_names;
143  List<LEX_STRING> db_cl_names;
144 
145  /* End of character ser context. */
146 
147  Table_triggers_list(TABLE *table_arg)
148  :record1_field(0), trigger_table(table_arg),
149  m_has_unparseable_trigger(false)
150  {
151  memset(bodies, 0, sizeof(bodies));
152  memset(&subject_table_grants, 0, sizeof(subject_table_grants));
153  }
154 
156 
157  bool create_trigger(THD *thd, TABLE_LIST *table, String *stmt_query);
158  bool drop_trigger(THD *thd, TABLE_LIST *table, String *stmt_query);
159  bool process_triggers(THD *thd, trg_event_type event,
160  trg_action_time_type time_type,
161  bool old_row_is_record1);
162 
163  bool get_trigger_info(THD *thd, trg_event_type event,
164  trg_action_time_type time_type,
165  LEX_STRING *trigger_name, LEX_STRING *trigger_stmt,
166  sql_mode_t *sql_mode,
167  LEX_STRING *definer,
168  LEX_STRING *client_cs_name,
169  LEX_STRING *connection_cl_name,
170  LEX_STRING *db_cl_name);
171 
172  void get_trigger_info(THD *thd,
173  int trigger_idx,
174  LEX_STRING *trigger_name,
175  sql_mode_t *sql_mode,
176  LEX_STRING *sql_original_stmt,
177  LEX_STRING *client_cs_name,
178  LEX_STRING *connection_cl_name,
179  LEX_STRING *db_cl_name);
180 
181  int find_trigger_by_name(const LEX_STRING *trigger_name);
182 
183  static bool check_n_load(THD *thd, const char *db, const char *table_name,
184  TABLE *table, bool names_only);
185  static bool drop_all_triggers(THD *thd, char *db, char *table_name);
186  static bool change_table_name(THD *thd, const char *db,
187  const char *old_alias,
188  const char *old_table,
189  const char *new_db,
190  const char *new_table);
191  bool has_triggers(trg_event_type event_type,
192  trg_action_time_type action_time)
193  {
194  return (bodies[event_type][action_time] != NULL);
195  }
196  bool has_delete_triggers()
197  {
198  return (bodies[TRG_EVENT_DELETE][TRG_ACTION_BEFORE] ||
199  bodies[TRG_EVENT_DELETE][TRG_ACTION_AFTER]);
200  }
201 
202  void set_table(TABLE *new_table);
203 
204  void mark_fields_used(trg_event_type event);
205 
206  void set_parse_error_message(char *error_message);
207 
208  friend class Item_trigger_field;
209 
211  Query_tables_list *prelocking_ctx,
212  TABLE_LIST *table_list);
213  bool is_fields_updated_in_trigger(MY_BITMAP *used_fields,
214  trg_event_type event_type,
215  trg_action_time_type action_time);
216 private:
217  bool prepare_record1_accessors();
218  LEX_STRING* change_table_name_in_trignames(const char *old_db_name,
219  const char *new_db_name,
220  LEX_STRING *new_table_name,
221  LEX_STRING *stopper);
222  bool change_table_name_in_triggers(THD *thd,
223  const char *old_db_name,
224  const char *new_db_name,
225  LEX_STRING *old_table_name,
226  LEX_STRING *new_table_name);
227 
228  bool check_for_broken_triggers()
229  {
230  if (m_has_unparseable_trigger)
231  {
232  my_message(ER_PARSE_ERROR, m_parse_error_message, MYF(0));
233  return true;
234  }
235  return false;
236  }
237 };
238 
239 extern const LEX_STRING trg_action_time_type_names[];
240 extern const LEX_STRING trg_event_type_names[];
241 
242 bool add_table_for_trigger(THD *thd,
243  const sp_name *trg_name,
244  bool continue_if_not_exist,
245  TABLE_LIST **table);
246 
247 void build_trn_path(THD *thd, const sp_name *trg_name, LEX_STRING *trn_path);
248 
249 bool check_trn_exists(const LEX_STRING *trn_path);
250 
251 bool load_table_name_for_trigger(THD *thd,
252  const sp_name *trg_name,
253  const LEX_STRING *trn_path,
254  LEX_STRING *tbl_name);
255 bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create);
256 
257 extern const char * const TRG_EXT;
258 extern const char * const TRN_EXT;
259 
260 #endif /* SQL_TRIGGER_INCLUDED */