My Project
sql_trigger.h
00001 #ifndef SQL_TRIGGER_INCLUDED
00002 #define SQL_TRIGGER_INCLUDED
00003 
00004 /*
00005    Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; version 2 of the License.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software Foundation,
00018    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
00019 
00020 /* Forward declarations */
00021 
00022 class Item_trigger_field;
00023 class sp_head;
00024 class sp_name;
00025 class Query_tables_list;
00026 struct TABLE_LIST;
00027 class Query_tables_list;
00028 
00030 enum trg_event_type
00031 {
00032   TRG_EVENT_INSERT= 0,
00033   TRG_EVENT_UPDATE= 1,
00034   TRG_EVENT_DELETE= 2,
00035   TRG_EVENT_MAX
00036 };
00037 
00038 #include "table.h"                              /* GRANT_INFO */
00039 
00040 /*
00041   We need this two enums here instead of sql_lex.h because
00042   at least one of them is used by Item_trigger_field interface.
00043 
00044   Time when trigger is invoked (i.e. before or after row actually
00045   inserted/updated/deleted).
00046 */
00047 enum trg_action_time_type
00048 {
00049   TRG_ACTION_BEFORE= 0, TRG_ACTION_AFTER= 1, TRG_ACTION_MAX
00050 };
00051 
00052 
00059 class Table_triggers_list: public Sql_alloc
00060 {
00062   sp_head *bodies[TRG_EVENT_MAX][TRG_ACTION_MAX];
00063 
00069   Field             **record1_field;
00070 
00076   Field             **new_field;
00077   Field             **old_field;
00078 
00079 public:
00081   TABLE *trigger_table;
00082 
00083 private:
00089   List<LEX_STRING>  names_list;
00090 
00095   List<LEX_STRING>  on_table_names_list;
00096 
00097 public:
00101   GRANT_INFO        subject_table_grants[TRG_EVENT_MAX][TRG_ACTION_MAX];
00102 
00103 private:
00115   bool m_has_unparseable_trigger;
00116 
00123   char m_parse_error_message[MYSQL_ERRMSG_SIZE];
00124 
00125 public:
00130   List<LEX_STRING>  definitions_list;
00131 
00135   List<ulonglong> definition_modes_list;
00136 
00137   List<LEX_STRING>  definers_list;
00138 
00139   /* Character set context, used for parsing and executing triggers. */
00140 
00141   List<LEX_STRING> client_cs_names;
00142   List<LEX_STRING> connection_cl_names;
00143   List<LEX_STRING> db_cl_names;
00144 
00145   /* End of character ser context. */
00146 
00147   Table_triggers_list(TABLE *table_arg)
00148     :record1_field(0), trigger_table(table_arg),
00149     m_has_unparseable_trigger(false)
00150   {
00151     memset(bodies, 0, sizeof(bodies));
00152     memset(&subject_table_grants, 0, sizeof(subject_table_grants));
00153   }
00154 
00155   ~Table_triggers_list();
00156 
00157   bool create_trigger(THD *thd, TABLE_LIST *table, String *stmt_query);
00158   bool drop_trigger(THD *thd, TABLE_LIST *table, String *stmt_query);
00159   bool process_triggers(THD *thd, trg_event_type event,
00160                         trg_action_time_type time_type,
00161                         bool old_row_is_record1);
00162 
00163   bool get_trigger_info(THD *thd, trg_event_type event,
00164                         trg_action_time_type time_type,
00165                         LEX_STRING *trigger_name, LEX_STRING *trigger_stmt,
00166                         sql_mode_t *sql_mode,
00167                         LEX_STRING *definer,
00168                         LEX_STRING *client_cs_name,
00169                         LEX_STRING *connection_cl_name,
00170                         LEX_STRING *db_cl_name);
00171 
00172   void get_trigger_info(THD *thd,
00173                         int trigger_idx,
00174                         LEX_STRING *trigger_name,
00175                         sql_mode_t *sql_mode,
00176                         LEX_STRING *sql_original_stmt,
00177                         LEX_STRING *client_cs_name,
00178                         LEX_STRING *connection_cl_name,
00179                         LEX_STRING *db_cl_name);
00180 
00181   int find_trigger_by_name(const LEX_STRING *trigger_name);
00182 
00183   static bool check_n_load(THD *thd, const char *db, const char *table_name,
00184                            TABLE *table, bool names_only);
00185   static bool drop_all_triggers(THD *thd, char *db, char *table_name);
00186   static bool change_table_name(THD *thd, const char *db,
00187                                 const char *old_alias,
00188                                 const char *old_table,
00189                                 const char *new_db,
00190                                 const char *new_table);
00191   bool has_triggers(trg_event_type event_type, 
00192                     trg_action_time_type action_time)
00193   {
00194     return (bodies[event_type][action_time] != NULL);
00195   }
00196   bool has_delete_triggers()
00197   {
00198     return (bodies[TRG_EVENT_DELETE][TRG_ACTION_BEFORE] ||
00199             bodies[TRG_EVENT_DELETE][TRG_ACTION_AFTER]);
00200   }
00201   bool has_update_triggers()
00202   {
00203     return (bodies[TRG_EVENT_UPDATE][TRG_ACTION_BEFORE] ||
00204             bodies[TRG_EVENT_UPDATE][TRG_ACTION_AFTER]);
00205   }
00206 
00207   void set_table(TABLE *new_table);
00208 
00209   void mark_fields_used(trg_event_type event);
00210 
00211   void set_parse_error_message(char *error_message);
00212 
00213   friend class Item_trigger_field;
00214 
00215   bool add_tables_and_routines_for_triggers(THD *thd,
00216                                             Query_tables_list *prelocking_ctx,
00217                                             TABLE_LIST *table_list);
00218   bool is_fields_updated_in_trigger(MY_BITMAP *used_fields,
00219                                     trg_event_type event_type,
00220                                     trg_action_time_type action_time);
00221 private:
00222   bool prepare_record1_accessors();
00223   LEX_STRING* change_table_name_in_trignames(const char *old_db_name,
00224                                              const char *new_db_name,
00225                                              LEX_STRING *new_table_name,
00226                                              LEX_STRING *stopper);
00227   bool change_table_name_in_triggers(THD *thd,
00228                                      const char *old_db_name,
00229                                      const char *new_db_name,
00230                                      LEX_STRING *old_table_name,
00231                                      LEX_STRING *new_table_name);
00232 
00233   bool check_for_broken_triggers() 
00234   {
00235     if (m_has_unparseable_trigger)
00236     {
00237       my_message(ER_PARSE_ERROR, m_parse_error_message, MYF(0));
00238       return true;
00239     }
00240     return false;
00241   }
00242 };
00243 
00244 extern const LEX_STRING trg_action_time_type_names[];
00245 extern const LEX_STRING trg_event_type_names[];
00246 
00247 bool add_table_for_trigger(THD *thd,
00248                            const sp_name *trg_name,
00249                            bool continue_if_not_exist,
00250                            TABLE_LIST **table);
00251 
00252 void build_trn_path(THD *thd, const sp_name *trg_name, LEX_STRING *trn_path);
00253 
00254 bool check_trn_exists(const LEX_STRING *trn_path);
00255 
00256 bool load_table_name_for_trigger(THD *thd,
00257                                  const sp_name *trg_name,
00258                                  const LEX_STRING *trn_path,
00259                                  LEX_STRING *tbl_name);
00260 bool mysql_create_or_drop_trigger(THD *thd, TABLE_LIST *tables, bool create);
00261 
00262 extern const char * const TRG_EXT;
00263 extern const char * const TRN_EXT;
00264 
00265 #endif /* SQL_TRIGGER_INCLUDED */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines