File: trigger_chain.cc

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (208 lines) | stat: -rw-r--r-- 6,622 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
   Copyright (c) 2013, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include "sql/trigger_chain.h"

#include <assert.h>
#include "m_ctype.h"

#include "my_inttypes.h"
#include "my_sys.h"
#include "mysqld_error.h"          // ER_*
#include "sql/dd/types/trigger.h"  // name_collation
#include "sql/mysqld.h"            // table_alias_charset
#include "sql/sp_head.h"           // sp_head
#include "sql/trigger.h"           // Trigger

struct MEM_ROOT;

Trigger_chain::~Trigger_chain() { m_triggers.destroy_elements(); }

/**
  Add a new trigger into the list of triggers with the same
  ACTION/TIMING value combination. This method is called during
  handling of statement CREATE TRIGGER.

  @param mem_root                 memory root for allocations
  @param new_trigger              pointer to the Trigger to add into the list
  @param ordering_clause          trigger ordering clause (FOLLOWS/PRECEDES)
  @param referenced_trigger_name  trigger name referenced by clause
                                  FOLLOWS/PRECEDES in the CREATE TRIGGER
                                  statement

  @return Operation status.
    @retval false Success.
    @retval true  Failure: either trigger not found or OOM. Set error in
    Diagnostic_area in case when trigger not found.
*/

bool Trigger_chain::add_trigger(MEM_ROOT *mem_root, Trigger *new_trigger,
                                enum_trigger_order_type ordering_clause,
                                const LEX_CSTRING &referenced_trigger_name) {
  switch (ordering_clause) {
    case TRG_ORDER_NONE:
      return add_trigger(mem_root, new_trigger);

    case TRG_ORDER_FOLLOWS:
    case TRG_ORDER_PRECEDES: {
      assert(referenced_trigger_name.str);

      List_iterator<Trigger> it(m_triggers);
      List_iterator<Trigger> it2 = it;

      while (true) {
        Trigger *t = it2++;

        if (!t) {
          my_error(ER_REFERENCED_TRG_DOES_NOT_EXIST, MYF(0),
                   referenced_trigger_name.str);
          return true;
        }

        if (!my_strnncoll(
                dd::Trigger::name_collation(),
                pointer_cast<const uchar *>(t->get_trigger_name().str),
                t->get_trigger_name().length,
                pointer_cast<const uchar *>(referenced_trigger_name.str),
                referenced_trigger_name.length))
          break;

        it = it2;
      }

      if (ordering_clause == TRG_ORDER_FOLLOWS) it = it2;

      bool rc;

      if (it.is_before_first())
        rc = m_triggers.push_front(new_trigger, mem_root);
      else
        rc = it.after(new_trigger, mem_root);

      return rc;
    }
  }

  assert(false);
  return true;
}

/**
  Add a new trigger into the list of triggers with the same
  ACTION/TIMING value combination. This method is called when a trigger
  is loaded from Data Dictionary.

  @param mem_root        memory root for allocations
  @param new_trigger     pointer to the Trigger to add into the list

  @return Operation status.
    @retval false Success.
    @retval true  Failure.
*/

bool Trigger_chain::add_trigger(MEM_ROOT *mem_root, Trigger *new_trigger) {
  return m_triggers.push_back(new_trigger, mem_root);
}

/**
  Run every trigger in the list of triggers.

  @param thd  Thread context
  @return  Result of trigger execution
    @retval false  all triggers in the list were executed successfully.
    @retval true   some trigger was failed. We stop triggers execution
                   on the first failed trigger and don't attempt to finish
                   the rest of triggers located after the failed one.
*/

bool Trigger_chain::execute_triggers(THD *thd) {
  List_iterator_fast<Trigger> it(m_triggers);
  Trigger *t;

  while ((t = it++)) {
    if (t->execute(thd)) return true;
  }

  return false;
}

/**
  Iterate over the list of triggers and add tables and routines used by trigger
  to the set of elements used by statement.

  @param [in]     thd               thread context
  @param [in,out] prelocking_ctx    prelocking context of the statement
  @param [in]     table_list        Table_ref for the table
*/

void Trigger_chain::add_tables_and_routines(THD *thd,
                                            Query_tables_list *prelocking_ctx,
                                            Table_ref *table_list) {
  List_iterator_fast<Trigger> it(m_triggers);
  Trigger *t;

  while ((t = it++))
    t->add_tables_and_routines(thd, prelocking_ctx, table_list);
}

/**
  Iterate over the list of triggers and mark fields of subject table
  which we read/set in every trigger.

  @param [in] subject_table trigger subject table
*/

void Trigger_chain::mark_fields(TABLE *subject_table) {
  List_iterator_fast<Trigger> it(m_triggers);
  Trigger *t;

  while ((t = it++)) t->get_sp()->mark_used_trigger_fields(subject_table);
}

/**
  Iterate over the list of triggers and check whether some
  table's fields are used in any trigger.

  @param [in] used_fields       bitmap of fields to check

  @return Check result
    @retval true   Some table fields are used in trigger
    @retval false  None of table fields are used in trigger
*/

bool Trigger_chain::has_updated_trigger_fields(const MY_BITMAP *used_fields) {
  List_iterator_fast<Trigger> it(m_triggers);
  Trigger *t;

  while ((t = it++)) {
    // Even if one trigger is unparsable, the whole thing is not usable.

    if (t->has_parse_error()) return false;

    if (t->get_sp()->has_updated_trigger_fields(used_fields)) return true;
  }

  return false;
}