MySQL 9.7.0
Source Code Documentation
statement_events.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24/**
25 @file
26
27 @brief Contains the classes representing statement events occurring in the
28 replication stream. Each event is represented as a byte sequence with logical
29 divisions as event header, event specific data and event footer. The header
30 and footer are common to all the events and are represented as two different
31 subclasses.
32*/
33
34#ifndef MYSQL_BINLOG_EVENT_STATEMENT_EVENTS_H
35#define MYSQL_BINLOG_EVENT_STATEMENT_EVENTS_H
36
39
40/// @addtogroup GroupLibsMysqlBinlogEvent
41/// @{
42
43namespace mysql::binlog::event {
44/**
45 The following constant represents the maximum of MYSQL_XID domain.
46 The maximum XID value practically is never supposed to grow beyond UINT64
47 range.
48*/
49const uint64_t INVALID_XID = 0xffffffffffffffffULL;
50
51/**
52 @class Query_event
53
54 A @c Query_event is created for each query that modifies the
55 database, unless the query is logged row-based.
56
57 @section Query_event_binary_format Binary format
58
59 See @ref Binary_log_event_binary_format "Binary format for log events" for
60 a general discussion and introduction to the binary format of binlog
61 events.
62
63 The Post-Header has five components:
64
65 <table>
66 <caption>Post-Header for Query_event</caption>
67
68 <tr>
69 <th>Name</th>
70 <th>Format</th>
71 <th>Description</th>
72 </tr>
73
74 <tr>
75 <td>thread_id</td>
76 <td>4 byte unsigned integer</td>
77 <td>The ID of the thread that issued this statement. It is needed for
78 temporary tables.</td>
79 </tr>
80
81 <tr>
82 <td>query_exec_time</td>
83 <td>4 byte unsigned integer</td>
84 <td>The time from when the query started to when it was logged in
85 the binlog, in seconds.</td>
86 </tr>
87
88 <tr>
89 <td>db_len</td>
90 <td>1 byte integer</td>
91 <td>The length of the name of the currently selected database.</td>
92 </tr>
93
94 <tr>
95 <td>error_code</td>
96 <td>2 byte unsigned integer</td>
97 <td>Error code generated by the master. If the master fails, the
98 slave will fail with the same error code.
99 </td>
100 </tr>
101
102 <tr>
103 <td>status_vars_len</td>
104 <td>2 byte unsigned integer</td>
105 <td>The length of the status_vars block of the Body, in bytes. This is not
106 present for binlog version 1 and 3. See
107 @ref Query_event_status_vars "below".
108 </td>
109 </tr>
110 </table>
111
112 The Body has the following components:
113
114 <table>
115 <caption>Body for Query_event</caption>
116
117 <tr>
118 <th>Name</th>
119 <th>Format</th>
120 <th>Description</th>
121 </tr>
122
123 <tr>
124 <td>@anchor Query_event_status_vars status_vars</td>
125 <td>status_vars_len bytes</td>
126 <td>Zero or more status variables. Each status variable consists
127 of one byte identifying the variable stored, followed by the value
128 of the variable. The possible variables are listed separately in
129 the table @ref Table_query_event_status_vars "below". MySQL
130 always writes events in the order defined below; however, it is
131 capable of reading them in any order. </td>
132 </tr>
133
134 <tr>
135 <td>m_db</td>
136 <td>db_len + 1</td>
137 <td>The currently selected database, as a null-terminated string.
138
139 (The trailing zero is redundant since the length is already known;
140 it is db_len from Post-Header.)
141 </td>
142 </tr>
143
144 <tr>
145 <td>m_query</td>
146 <td>variable length string without trailing zero, extending to the
147 end of the event (determined by the length field of the
148 Common-Header)
149 </td>
150 <td>The SQL query.</td>
151 </tr>
152 </table>
153
154 The following table lists the status variables that may appear in
155 the status_vars field.
156
157 @anchor Table_query_event_status_vars
158 <table>
159 <caption>Status variables for Query_event</caption>
160
161 <tr>
162 <th>Status variable</th>
163 <th>1 byte identifier</th>
164 <th>Format</th>
165 <th>Description</th>
166 </tr>
167
168 <tr>
169 <td>flags2</td>
170 <td>Q_FLAGS2_CODE == 0</td>
171 <td>4 byte bitfield</td>
172 <td>The flags in @c thd->options, binary AND-ed with @c
173 OPTIONS_WRITTEN_TO_BIN_LOG. The @c thd->options bitfield contains
174 options for "SELECT". @c OPTIONS_WRITTEN identifies those options
175 that need to be written to the binlog (not all do). Specifically,
176 @c OPTIONS_WRITTEN_TO_BIN_LOG equals (@c OPTION_AUTO_IS_NULL | @c
177 OPTION_NO_FOREIGN_KEY_CHECKS | @c OPTION_RELAXED_UNIQUE_CHECKS |
178 @c OPTION_NOT_AUTOCOMMIT), or 0x0c084000 in hex.
179
180 These flags correspond to the SQL variables SQL_AUTO_IS_NULL,
181 FOREIGN_KEY_CHECKS, UNIQUE_CHECKS, and AUTOCOMMIT, documented in
182 the "SET Syntax" section of the MySQL Manual.
183
184 This field is always written to the binlog in version >= 5.0, and
185 never written in version < 5.0.
186 </td>
187 </tr>
188
189 <tr>
190 <td>sql_mode</td>
191 <td>Q_SQL_MODE_CODE == 1</td>
192 <td>8 byte bitfield</td>
193 <td>The @c sql_mode variable. See the section "SQL Modes" in the
194 MySQL manual, and see system_variables.h for a list of the possible
195 flags. Currently, the following flags are available:
196 <pre>
197 MODE_REAL_AS_FLOAT==0x1
198 MODE_PIPES_AS_CONCAT==0x2
199 MODE_ANSI_QUOTES==0x4
200 MODE_IGNORE_SPACE==0x8
201 MODE_NOT_USED==0x10
202 MODE_ONLY_FULL_GROUP_BY==0x20
203 MODE_NO_UNSIGNED_SUBTRACTION==0x40
204 MODE_NO_DIR_IN_CREATE==0x80
205 MODE_ANSI==0x80000
206 MODE_NO_AUTO_VALUE_ON_ZERO==0x100000
207 MODE_NO_BACKSLASH_ESCAPES==0x200000
208 MODE_STRICT_TRANS_TABLES==0x400000
209 MODE_STRICT_ALL_TABLES==0x800000
210 MODE_NO_ZERO_IN_DATE==0x1000000
211 MODE_NO_ZERO_DATE==0x2000000
212 MODE_INVALID_DATES==0x4000000
213 MODE_ERROR_FOR_DIVISION_BY_ZERO==0x8000000
214 MODE_TRADITIONAL==0x10000000
215 MODE_HIGH_NOT_PRECEDENCE==0x40000000
216 MODE_PAD_CHAR_TO_FULL_LENGTH==0x80000000
217 MODE_TIME_TRUNCATE_FRACTIONAL==0x100000000
218 MODE_INTERPRET_UTF8_AS_UTF8MB4==0x200000000
219 </pre>
220 All these flags are replicated from the server. However, all
221 flags except @c MODE_NO_DIR_IN_CREATE are honored by the slave;
222 the slave always preserves its old value of @c
223 MODE_NO_DIR_IN_CREATE.
224
225 This field is always written to the binlog.
226 </td>
227 </tr>
228
229 <tr>
230 <td>catalog</td>
231 <td>Q_CATALOG_NZ_CODE == 6</td>
232 <td>Variable-length string: the length in bytes (1 byte) followed
233 by the characters (at most 255 bytes)
234 </td>
235 <td>Stores the client's current catalog. Every database belongs
236 to a catalog, the same way that every table belongs to a
237 database. Currently, there is only one catalog, "std".
238
239 This field is written if the length of the catalog is > 0;
240 otherwise it is not written.
241 </td>
242 </tr>
243
244 <tr>
245 <td>auto_increment</td>
246 <td>Q_AUTO_INCREMENT == 3</td>
247 <td>two 2 byte unsigned integers, totally 2+2=4 bytes</td>
248
249 <td>The two variables auto_increment_increment and
250 auto_increment_offset, in that order. For more information, see
251 "System variables" in the MySQL manual.
252
253 This field is written if auto_increment > 1. Otherwise, it is not
254 written.
255 </td>
256 </tr>
257
258 <tr>
259 <td>charset</td>
260 <td>Q_CHARSET_CODE == 4</td>
261 <td>three 2 byte unsigned integers, totally 2+2+2=6 bytes</td>
262 <td>The three variables character_set_client,
263 collation_connection, and collation_server, in that order.
264 character_set_client is a code identifying the character set and
265 collation used by the client to encode the query.
266 collation_connection identifies the character set and collation
267 that the master converts the query to when it receives it; this is
268 useful when comparing literal strings. collation_server is the
269 default character set and collation used when a new database is
270 created.
271
272 See also "Connection Character Sets and Collations" in the MySQL
273 5.1 manual.
274
275 All three variables are codes identifying a (character set,
276 collation) pair. To see which codes map to which pairs, run the
277 query "SELECT id, character_set_name, collation_name FROM
278 COLLATIONS".
279
280 Cf. Q_CHARSET_DATABASE_CODE below.
281
282 This field is always written.
283 </td>
284 </tr>
285
286 <tr>
287 <td>time_zone</td>
288 <td>Q_TIME_ZONE_CODE == 5</td>
289 <td>Variable-length string: the length in bytes (1 byte) followed
290 by the characters (at most 255 bytes).
291 <td>The time_zone of the master.
292
293 See also "System Variables" and "MySQL Server Time Zone Support"
294 in the MySQL manual.
295
296 This field is written if the length of the time zone string is >
297 0; otherwise, it is not written.
298 </td>
299 </tr>
300
301 <tr>
302 <td>lc_time_names_number</td>
303 <td>Q_LC_TIME_NAMES_CODE == 7</td>
304 <td>2 byte integer</td>
305 <td>A code identifying a table of month and day names. The
306 mapping from codes to languages is defined in @c sql_locale.cc.
307
308 This field is written if it is not 0, i.e., if the locale is not
309 en_US.
310 </td>
311 </tr>
312
313 <tr>
314 <td>charset_database_number</td>
315 <td>Q_CHARSET_DATABASE_CODE == 8</td>
316 <td>2 byte integer</td>
317
318 <td>The value of the collation_database system variable (in the
319 source code stored in @c thd->variables.collation_database), which
320 holds the code for a (character set, collation) pair as described
321 above (see Q_CHARSET_CODE).
322
323 collation_database was used in old versions (???WHEN). Its value
324 was loaded when issuing a "use db" query and could be changed by
325 issuing a "SET collation_database=xxx" query. It used to affect
326 the "LOAD DATA INFILE" and "CREATE TABLE" commands.
327
328 In newer versions, "CREATE TABLE" has been changed to take the
329 character set from the database of the created table, rather than
330 the character set of the current database. This makes a
331 difference when creating a table in another database than the
332 current one. "LOAD DATA INFILE" has not yet changed to do this,
333 but there are plans to eventually do it, and to make
334 collation_database read-only.
335
336 This field is written if it is not 0.
337 </td>
338 </tr>
339 <tr>
340 <td>table_map_for_update</td>
341 <td>Q_TABLE_MAP_FOR_UPDATE_CODE == 9</td>
342 <td>8 byte integer</td>
343
344 <td>The value of the table map that is to be updated by the
345 multi-table update query statement. Every bit of this variable
346 represents a table, and is set to 1 if the corresponding table is
347 to be updated by this statement.
348
349 The value of this variable is set when executing a multi-table update
350 statement and used by slave to apply filter rules without opening
351 all the tables on slave. This is required because some tables may
352 not exist on slave because of the filter rules.
353 </td>
354 </tr>
355 <tr>
356 <td>master_data_written</td>
357 <td>Q_MASTER_DATA_WRITTEN_CODE == 10</td>
358 <td>4 byte bitfield</td>
359
360 <td>The value of the original length of a Query_event that comes from a
361 master. Master's event is relay-logged with storing the original size of
362 event in this field by the IO thread. The size is to be restored by reading
363 Q_MASTER_DATA_WRITTEN_CODE-marked event from the relay log.
364
365 This field is not written to slave's server binlog by the SQL thread.
366 This field only exists in relay logs where master has binlog_version<4 i.e.
367 server_version < 5.0 and the slave has binlog_version=4.
368 </td>
369 </tr>
370 <tr>
371 <td>binlog_invoker</td>
372 <td>Q_INVOKER == 11</td>
373 <td>2 Variable-length strings: the length in bytes (1 byte) followed
374 by characters (user), again followed by length in bytes (1 byte) followed
375 by characters(host)</td>
376
377 <td>The value of boolean variable m_binlog_invoker is set TRUE if
378 CURRENT_USER() is called in account management statements. SQL thread
379 uses it as a default definer in CREATE/ALTER SP, SF, Event, TRIGGER or
380 VIEW statements.
381
382 The field Q_INVOKER has length of user stored in 1 byte followed by the
383 user string which is assigned to 'user' and the length of host stored in
384 1 byte followed by host string which is assigned to 'host'.
385 </td>
386 </tr>
387 <tr>
388 <td>mts_accessed_dbs</td>
389 <td>Q_UPDATED_DB_NAMES == 12</td>
390 <td>1 byte character, and a 2-D array</td>
391 <td>The total number and the names to of the databases accessed is stored,
392 to be propagated to the slave in order to facilitate the parallel
393 applying of the Query events.
394 </td>
395 </tr>
396 <tr>
397 <td>explicit_defaults_ts</td>
398 <td>Q_EXPLICIT_DEFAULTS_FOR_TIMESTAMP</td>
399 <td>1 byte boolean</td>
400 <td>Stores master connection @@session.explicit_defaults_for_timestamp when
401 CREATE and ALTER operate on a table with a TIMESTAMP column. </td>
402 </tr>
403 <tr>
404 <td>ddl_xid</td>
405 <td>Q_DDL_LOGGED_WITH_XID</td>
406 <td>8 byte integer</td>
407 <td>Stores variable carrying xid info of 2pc-aware (recoverable) DDL
408 queries. </td>
409 </tr>
410 <tr>
411 <td>default_collation_for_utf8mb4_number</td>
412 <td>Q_DEFAULT_COLLATION_FOR_UTF8MB4</td>
413 <td>2 byte integer</td>
414 <td>Stores variable carrying the the default collation for the utf8mb4
415 character set. Mainly used to support replication 5.7- master to a 8.0+
416 slave.
417 </td>
418 </tr>
419 <tr>
420 <td>sql_require_primary_key</td>
421 <td>Q_SQL_REQUIRE_PRIMARY_KEY</td>
422 <td>2 byte integer</td>
423 <td>Value of the config variable sql_require_primary_key</td>
424 </tr>
425 <tr>
426 <td>default_table_encryption</td>
427 <td>Q_DEFAULT_TABLE_ENCRYPTION</td>
428 <td>2 byte integer</td>
429 <td>Value of the config variable default_table_encryption</td>
430 </tr>
431 <tr>
432 <td>enable_cascade_triggers</td>
433 <td>Q_ENABLE_CASCADE_TRIGGERS</td>
434 <td>1 byte boolean</td>
435 <td>Value of the config variable enable_cascade_triggers</td>
436 </tr>
437 </table>
438
439 @subsection Query_event_notes_on_previous_versions Notes on Previous Versions
440
441 * Status vars were introduced in version 5.0. To read earlier
442 versions correctly, check the length of the Post-Header.
443
444 * The status variable Q_CATALOG_CODE == 2 existed in MySQL 5.0.x,
445 where 0<=x<=3. It was identical to Q_CATALOG_CODE, except that the
446 string had a trailing '\0'. The '\0' was removed in 5.0.4 since it
447 was redundant (the string length is stored before the string). The
448 Q_CATALOG_CODE will never be written by a new master, but can still
449 be understood by a new slave.
450
451 * See Q_CHARSET_DATABASE_CODE in the table above.
452
453 * When adding new status vars, please don't forget to update the
454 MAX_SIZE_LOG_EVENT_STATUS.
455
456*/
457
459 public:
460 /** query event post-header */
468 };
469
470 /* these are codes, not offsets; not more than 256 values (1 byte). */
474 /*
475 Q_CATALOG_CODE is catalog with end zero stored; it is used only by MySQL
476 5.0.x where 0<=x<=3. We have to keep it to be able to replicate these
477 old masters.
478 */
483 /*
484 Q_CATALOG_NZ_CODE is catalog withOUT end zero stored; it is used by MySQL
485 5.0.x where x>=4. Saves one byte in every Query_event in binlog,
486 compared to Q_CATALOG_CODE. The reason we didn't simply re-use
487 Q_CATALOG_CODE is that then a 5.0.3 slave of this 5.0.x (x>=4)
488 master would crash (segfault etc) because it would expect a 0 when there
489 is none.
490 */
495 /* It is just a placeholder after 8.0.2*/
498 /*
499 Q_UPDATED_DB_NAMES status variable collects information of accessed
500 databases i.e. the total number and the names to be propagated to the
501 slave in order to facilitate the parallel applying of the Query events.
502 */
505 /*
506 A old (unused now) code for Query_log_event status similar to G_COMMIT_TS.
507 */
509 /*
510 An old (unused after migration to Gtid_event) code for
511 Query_log_event status, similar to G_COMMIT_TS2.
512 */
514 /*
515 The master connection @@session.explicit_defaults_for_timestamp which
516 is recorded for queries, CREATE and ALTER table that is defined with
517 a TIMESTAMP column, that are dependent on that feature.
518 For pre-WL6292 master's the associated with this code value is zero.
519 */
521 /*
522 The variable carries xid info of 2pc-aware (recoverable) DDL queries.
523 */
525 /*
526 This variable stores the default collation for the utf8mb4 character set.
527 Used to support cross-version replication.
528 */
530
531 /*
532 Replicate sql_require_primary_key.
533 */
535
536 /*
537 Replicate default_table_encryption.
538 */
540
541 /*
542 Replicate enable_cascade_triggers.
543 */
545 };
546 const char *query;
547 const char *db;
548 const char *catalog;
549 const char *time_zone_str;
550
551 protected:
552 const char *user;
553 size_t user_len;
554 const char *host;
555 size_t host_len;
556
557 /* Required by the MySQL server class Log_event::Query_event */
558 unsigned long data_len;
559 /*
560 Copies data into the buffer in the following fashion
561 +--------+-----------+------+------+---------+----+-------+----+
562 | catlog | time_zone | user | host | db name | \0 | Query | \0 |
563 +--------+-----------+------+------+---------+----+-------+----+
564 */
565 int fill_data_buf(unsigned char *dest, unsigned long len);
566
567 public:
568 /* data members defined in order they are packed and written into the log */
569 uint32_t thread_id;
571 size_t db_len;
572 uint16_t error_code;
573 /*
574 We want to be able to store a variable number of N-bit status vars:
575 (generally N=32; but N=64 for SQL_MODE) a user may want to log the number
576 of affected rows (for debugging) while another does not want to lose 4
577 bytes in this.
578 The storage on disk is the following:
579 status_vars_len is part of the post-header,
580 status_vars are in the variable-length part, after the post-header, before
581 the db & query.
582 status_vars on disk is a sequence of pairs (code, value) where 'code' means
583 'sql_mode', 'affected' etc. Sometimes 'value' must be a short string, so
584 its first byte is its length. For now the order of status vars is:
585 flags2 - sql_mode - catalog - autoinc - charset
586 We should add the same thing to Load_event, but in fact
587 LOAD DATA INFILE is going to be logged with a new type of event (logging of
588 the plain text query), so Load_event would be frozen, so no need. The
589 new way of logging LOAD DATA INFILE would use a derived class of
590 Query_event, so automatically benefit from the work already done for
591 status variables in Query_event.
592 */
594 /*
595 If we already know the length of the query string
596 we pass it with q_len, so we would not have to call strlen()
597 otherwise, set it to 0, in which case, we compute it with strlen()
598 */
599 size_t q_len;
600
601 /* The members below represent the status variable block */
602
603 /*
604 'flags2' is a second set of flags (on top of those in Log_event), for
605 session variables. These are thd->options which is & against a mask
606 (OPTIONS_WRITTEN_TO_BIN_LOG).
607 flags2_inited helps make a difference between flags2==0 (3.23 or 4.x
608 master, we don't know flags2, so use the slave server's global options) and
609 flags2==0 (5.0 master, we know this has a meaning of flags all down which
610 must influence the query).
611 */
615
616 uint32_t flags2;
617 /* In connections sql_mode is 32 bits now but will be 64 bits soon */
618 uint64_t sql_mode;
620 char charset[6];
621 size_t time_zone_len; /* 0 means uninited */
622 /*
623 Binlog format 3 and 4 start to differ (as far as class members are
624 concerned) from here.
625 */
626 size_t catalog_len; // <= 255 char; 0 means uninited
627 uint16_t lc_time_names_number; /* 0 means en_US */
629 /*
630 map for tables that will be updated for a multi-table update query
631 statement, for other query statements, this will be zero.
632 */
634 /*
635 The following member gets set to OFF or ON value when the
636 Query-log-event is marked as dependent on
637 @@explicit_defaults_for_timestamp. That is the member is relevant
638 to queries that declare TIMESTAMP column attribute, like CREATE
639 and ALTER.
640 The value is set to @c TERNARY_OFF when @@explicit_defaults_for_timestamp
641 encoded value is zero, otherwise TERNARY_ON.
642 */
648 /*
649 number of updated databases by the query and their names. This info
650 is requested by both Coordinator and Worker.
651 */
652 unsigned char mts_accessed_dbs;
654 /* XID value when the event is a 2pc-capable DDL */
655 uint64_t ddl_xid;
656 /* Default collation for the utf8mb4 set. Used in cross-version replication */
659
661
663
664 /**
665 The constructor will be used while creating a Query_event, to be
666 written to the binary log.
667 */
668 Query_event(const char *query_arg, const char *catalog_arg,
669 const char *db_arg, uint32_t query_length,
670 unsigned long thread_id_arg, unsigned long long sql_mode_arg,
671 unsigned long auto_increment_increment_arg,
672 unsigned long auto_increment_offset_arg, unsigned int number,
673 unsigned long long table_map_for_update_arg, int errcode);
674
675 /**
676 The constructor receives a buffer and instantiates a Query_event filled in
677 with the data from the buffer
678
679 <pre>
680 The fixed event data part buffer layout is as follows:
681 +---------------------------------------------------------------------+
682 | thread_id | query_exec_time | db_len | error_code | status_vars_len |
683 +---------------------------------------------------------------------+
684 </pre>
685
686 <pre>
687 The fixed event data part buffer layout is as follows:
688 +--------------------------------------------+
689 | Zero or more status variables | db | query |
690 +--------------------------------------------+
691 </pre>
692
693 @param buf Contains the serialized event.
694 @param fde An FDE event (see Rotate_event constructor for more info).
695 @param event_type Required to determine whether the event type is
696 QUERY_EVENT or EXECUTE_LOAD_QUERY_EVENT
697 */
698 Query_event(const char *buf, const Format_description_event *fde,
699 Log_event_type event_type);
700 /**
701 The simplest constructor that could possibly work. This is used for
702 creating static objects that have a special meaning and are invisible
703 to the log.
704 */
706 ~Query_event() override = default;
707
708#ifndef HAVE_MYSYS
709 void print_event_info(std::ostream &info) override;
710 void print_long_info(std::ostream &info) override;
711#endif
712};
713
714/*
715 Check how many bytes are available on buffer.
716
717 @param buf_start Pointer to buffer start.
718 @param buf_current Pointer to the current position on buffer.
719 @param buf_len Buffer length.
720
721 @return Number of bytes available on event buffer.
722*/
723template <class T>
724T available_buffer(const char *buf_start, const char *buf_current, T buf_len) {
725 /* Sanity check */
726 if (buf_current < buf_start ||
727 buf_len < static_cast<T>(buf_current - buf_start))
728 return static_cast<T>(0);
729
730 return static_cast<T>(buf_len - (buf_current - buf_start));
731}
732
733/**
734 Check if jump value is within buffer limits.
735
736 @param jump Number of positions we want to advance.
737 @param buf_start Pointer to buffer start
738 @param buf_current Pointer to the current position on buffer.
739 @param buf_len Buffer length.
740
741 @retval True If jump value is within buffer limits.
742 @retval False Otherwise.
743*/
744template <class T>
745bool valid_buffer_range(T jump, const char *buf_start, const char *buf_current,
746 T buf_len) {
747 return (jump <= available_buffer(buf_start, buf_current, buf_len));
748}
749
750/**
751 @class User_var_event
752
753 Written every time a statement uses a user variable; precedes other
754 events for the statement. Indicates the value to use for the user
755 variable in the next statement. This is written only before a QUERY_EVENT
756 and is not used with row-based logging
757
758 The Post-Header has following components:
759
760 <table>
761 <caption>Post-Header for Format_description_event</caption>
762
763 <tr>
764 <th>Name</th>
765 <th>Format</th>
766 <th>Description</th>
767 </tr>
768
769 <tr>
770 <td>Value_type</td>
771 <td>enum</td>
772 <td>The user variable type.</td>
773 </tr>
774 <tr>
775 <td>User_var_event_data</td>
776 <td>enum</td>
777 <td>User_var event data</td>
778 </tr>
779 <tr>
780 <td>name</td>
781 <td>const char pointer</td>
782 <td>User variable name.</td>
783 </tr>
784 <tr>
785 <td>name_len</td>
786 <td>unsigned int</td>
787 <td>Length of the user variable name</td>
788 </tr>
789 <tr>
790 <td>val</td>
791 <td>char pointer</td>
792 <td>value of the user variable.</td>
793 </tr>
794 <tr>
795 <td>val_len</td>
796 <td>unsigned long</td>
797 <td>Length of the value of the user variable</td>
798 </tr>
799 <tr>
800 <td>type</td>
801 <td>enum Value_type</td>
802 <td>Type of the user variable</td>
803 </tr>
804 <tr>
805 <td>charset_number</td>
806 <td>unsigned int</td>
807 <td>The number of the character set for the user variable (needed for a
808 string variable). The character set number is really a collation
809 number that indicates a character set/collation pair.</td>
810 </tr>
811 <tr>
812 <td>is_null</td>
813 <td>bool</td>
814 <td>Non-zero if the variable value is the SQL NULL value, 0 otherwise.</td>
815 </tr>
816 </table>
817*/
819 public:
828 };
829
830 /**
831 This constructor will initialize the instance variables and the type_code,
832 it will be used only by the server code.
833 */
834 User_var_event(const char *name_arg, unsigned int name_len_arg, char *val_arg,
835 unsigned long val_len_arg, Value_type type_arg,
836 unsigned int charset_number_arg, unsigned char flags_arg)
838 name(bapi_strndup(name_arg, name_len_arg)),
839 name_len(name_len_arg),
840 val(val_arg),
841 val_len(val_len_arg),
842 type(type_arg),
843 charset_number(charset_number_arg),
844 is_null(!val),
845 flags(flags_arg) {}
846
847 /**
848 The constructor receives a buffer and instantiates a User_var_event filled
849 in with the data from the buffer
850 Written every time a statement uses a user variable, precedes other
851 events for the statement. Indicates the value to use for the
852 user variable in the next statement. This is written only before a
853 QUERY_EVENT and is not used with row-based logging.
854
855 The buffer layout for variable data part is as follows:
856 <pre>
857 +-------------------------------------------------------------------+
858 | name_len | name | is_null | type | charset_number | val_len | val |
859 +-------------------------------------------------------------------+
860 </pre>
861
862 @param buf Contains the serialized event.
863 @param fde An FDE event (see Rotate_event constructor for more info).
864 */
865 User_var_event(const char *buf, const Format_description_event *fde);
866 ~User_var_event() override;
867 const char *name;
868 unsigned int name_len;
869 char *val;
870 uint32_t val_len;
872 unsigned int charset_number;
874 unsigned char flags;
875#ifndef HAVE_MYSYS
876 void print_event_info(std::ostream &info) override;
877 void print_long_info(std::ostream &info) override;
878 const char *get_value_type_string(Value_type type_arg) const {
879 switch (type_arg) {
880 case STRING_RESULT:
881 return "String";
882 case REAL_RESULT:
883 return "Real";
884 case INT_RESULT:
885 return "Integer";
886 case ROW_RESULT:
887 return "Row";
888 case DECIMAL_RESULT:
889 return "Decimal";
890 default:
891 return "Unknown";
892 }
893 }
894#endif
895};
896
897/**
898 @class Intvar_event
899
900 An Intvar_event will be created just before a Query_event,
901 if the query uses one of the variables LAST_INSERT_ID or INSERT_ID.
902 Each Intvar_event holds the value of one of these variables.
903
904 @section Intvar_event_binary_format Binary Format
905
906 The Post-Header for this event type is empty. The Body has two
907 components:
908
909 <table>
910 <caption>Body for Intvar_event</caption>
911
912 <tr>
913 <th>Name</th>
914 <th>Format</th>
915 <th>Description</th>
916 </tr>
917
918 <tr>
919 <td>type</td>
920 <td>1 byte enumeration</td>
921 <td>One byte identifying the type of variable stored. Currently,
922 two identifiers are supported: LAST_INSERT_ID_EVENT == 1 and
923 INSERT_ID_EVENT == 2.
924 </td>
925 </tr>
926
927 <tr>
928 <td>val</td>
929 <td>8 byte unsigned integer</td>
930 <td>The value of the variable.</td>
931 </tr>
932
933 </table>
934*/
936 public:
937 uint8_t type;
938 uint64_t val;
939
940 /*
941 The enum recognizes the type of variables that can occur in an
942 INTVAR_EVENT. The two types supported are LAST_INSERT_ID and
943 INSERT_ID, in accordance to the SQL query using LAST_INSERT_ID
944 or INSERT_ID.
945 */
950 };
951
952 /**
953 moving from pre processor symbols from global scope in log_event.h
954 to an enum inside the class, since these are used only by
955 members of this class itself.
956 */
958
959 /**
960 This method returns the string representing the type of the variable
961 used in the event. Changed the definition to be similar to that
962 previously defined in log_event.cc.
963 */
964 std::string get_var_type_string() const {
965 switch (type) {
967 return "INVALID_INT";
969 return "LAST_INSERT_ID";
970 case INSERT_ID_EVENT:
971 return "INSERT_ID";
972 default: /* impossible */
973 return "UNKNOWN";
974 }
975 }
976
977 /**
978 Constructor receives a packet from the MySQL master or the binary
979 log and decodes it to create an Intvar_event.
980
981 The post header for the event is empty. Buffer layout for the variable
982 data part is as follows:
983 <pre>
984 +--------------------------------+
985 | type (4 bytes) | val (8 bytes) |
986 +--------------------------------+
987 </pre>
988
989 @param buf Contains the serialized event.
990 @param fde An FDE event (see Rotate_event constructor for more info).
991 */
992 Intvar_event(const char *buf, const Format_description_event *fde);
993 /**
994 The minimal constructor for Intvar_event it initializes the instance
995 variables type & val and set the type_code as INTVAR_EVENT in the header
996 object in Binary_log_event
997 */
998 Intvar_event(uint8_t type_arg, uint64_t val_arg)
999 : Binary_log_event(INTVAR_EVENT), type(type_arg), val(val_arg) {}
1000
1001 ~Intvar_event() override = default;
1002
1003#ifndef HAVE_MYSYS
1004 void print_event_info(std::ostream &info) override;
1005 void print_long_info(std::ostream &info) override;
1006#endif
1007};
1008
1009/**
1010 @class Rand_event
1011
1012 Logs random seed used by the next RAND(), and by PASSWORD() in 4.1.0.
1013 4.1.1 does not need it (it's repeatable again) so this event needn't be
1014 written in 4.1.1 for PASSWORD() (but the fact that it is written is just a
1015 waste, it does not cause bugs).
1016
1017 The state of the random number generation consists of 128 bits,
1018 which are stored internally as two 64-bit numbers.
1019
1020 @section Rand_event_binary_format Binary Format
1021
1022 The Post-Header for this event type is empty. The Body has two
1023 components:
1024
1025 <table>
1026 <caption>Body for Rand_event</caption>
1027
1028 <tr>
1029 <th>Name</th>
1030 <th>Format</th>
1031 <th>Description</th>
1032 </tr>
1033
1034 <tr>
1035 <td>seed1</td>
1036 <td>8 byte unsigned integer</td>
1037 <td>64 bit random seed1.</td>
1038 </tr>
1039
1040 <tr>
1041 <td>seed2</td>
1042 <td>8 byte unsigned integer</td>
1043 <td>64 bit random seed2.</td>
1044 </tr>
1045 </table>
1046*/
1048 public:
1049 unsigned long long seed1;
1050 unsigned long long seed2;
1052
1053 /**
1054 This will initialize the instance variables seed1 & seed2, and set the
1055type_code as RAND_EVENT in the header object in Binary_log_event
1056 */
1057 Rand_event(unsigned long long seed1_arg, unsigned long long seed2_arg)
1059 seed1 = seed1_arg;
1060 seed2 = seed2_arg;
1061 }
1062
1063 /**
1064 Written every time a statement uses the RAND() function; precedes other
1065 events for the statement. Indicates the seed values to use for generating a
1066 random number with RAND() in the next statement. This is written only before
1067 a QUERY_EVENT and is not used with row-based logging
1068
1069 <pre>
1070 The buffer layout for variable part is as follows:
1071 +----------------------------------------------+
1072 | value for first seed | value for second seed |
1073 +----------------------------------------------+
1074 </pre>
1075
1076 @param buf Contains the serialized event.
1077 @param fde An FDE event (see Rotate_event constructor for more info).
1078 */
1079 Rand_event(const char *buf, const Format_description_event *fde);
1080#ifndef HAVE_MYSYS
1081 void print_event_info(std::ostream &info) override;
1082 void print_long_info(std::ostream &info) override;
1083#endif
1084};
1085} // end namespace mysql::binlog::event
1086
1087/// @}
1088
1089#endif // MYSQL_BINLOG_EVENT_STATEMENT_EVENTS_H
This is the abstract base class for binary log events.
Definition: binlog_event.h:852
@ QUERY_HEADER_LEN
Definition: binlog_event.h:869
For binlog version 4.
Definition: control_events.h:240
An Intvar_event will be created just before a Query_event, if the query uses one of the variables LAS...
Definition: statement_events.h:935
Intvar_event(const char *buf, const Format_description_event *fde)
Constructor receives a packet from the MySQL master or the binary log and decodes it to create an Int...
Definition: statement_events.cpp:488
uint64_t val
Definition: statement_events.h:938
std::string get_var_type_string() const
This method returns the string representing the type of the variable used in the event.
Definition: statement_events.h:964
Intvar_event(uint8_t type_arg, uint64_t val_arg)
The minimal constructor for Intvar_event it initializes the instance variables type & val and set the...
Definition: statement_events.h:998
Int_event_type
Definition: statement_events.h:946
@ LAST_INSERT_ID_EVENT
Definition: statement_events.h:948
@ INVALID_INT_EVENT
Definition: statement_events.h:947
@ INSERT_ID_EVENT
Definition: statement_events.h:949
uint8_t type
Definition: statement_events.h:937
Intvar_event_offset
moving from pre processor symbols from global scope in log_event.h to an enum inside the class,...
Definition: statement_events.h:957
@ I_TYPE_OFFSET
Definition: statement_events.h:957
@ I_VAL_OFFSET
Definition: statement_events.h:957
A Query_event is created for each query that modifies the database, unless the query is logged row-ba...
Definition: statement_events.h:458
uint16_t error_code
Definition: statement_events.h:572
size_t host_len
Definition: statement_events.h:555
uint8_t default_table_encryption
Definition: statement_events.h:660
Query_event(const char *query_arg, const char *catalog_arg, const char *db_arg, uint32_t query_length, unsigned long thread_id_arg, unsigned long long sql_mode_arg, unsigned long auto_increment_increment_arg, unsigned long auto_increment_offset_arg, unsigned int number, unsigned long long table_map_for_update_arg, int errcode)
The constructor will be used while creating a Query_event, to be written to the binary log.
Definition: statement_events.cpp:53
const char * host
Definition: statement_events.h:554
uint64_t table_map_for_update
Definition: statement_events.h:633
uint16_t auto_increment_increment
Definition: statement_events.h:619
const char * db
Definition: statement_events.h:547
const char * user
Definition: statement_events.h:552
uint64_t sql_mode
Definition: statement_events.h:618
const char * query
Definition: statement_events.h:546
char charset[6]
Definition: statement_events.h:620
uint64_t ddl_xid
Definition: statement_events.h:655
enum mysql::binlog::event::Query_event::enum_ternary explicit_defaults_ts
bool charset_inited
Definition: statement_events.h:614
unsigned char mts_accessed_dbs
Definition: statement_events.h:652
const char * catalog
Definition: statement_events.h:548
uint16_t default_collation_for_utf8mb4_number
Definition: statement_events.h:657
size_t db_len
Definition: statement_events.h:571
const char * time_zone_str
Definition: statement_events.h:549
bool sql_mode_inited
Definition: statement_events.h:613
enum_ternary
Definition: statement_events.h:643
@ TERNARY_OFF
Definition: statement_events.h:645
@ TERNARY_UNSET
Definition: statement_events.h:644
@ TERNARY_ON
Definition: statement_events.h:646
char mts_accessed_db_names[MAX_DBS_IN_EVENT_MTS][NAME_LEN]
Definition: statement_events.h:653
int fill_data_buf(unsigned char *dest, unsigned long len)
Layout for the data buffer is as follows.
Definition: statement_events.cpp:371
uint8_t sql_require_primary_key
Definition: statement_events.h:658
uint32_t flags2
Definition: statement_events.h:616
size_t catalog_len
Definition: statement_events.h:626
Query_event_status_vars
Definition: statement_events.h:471
@ Q_COMMIT_TS2
Definition: statement_events.h:513
@ Q_LC_TIME_NAMES_CODE
Definition: statement_events.h:492
@ Q_COMMIT_TS
Definition: statement_events.h:508
@ Q_CHARSET_CODE
Definition: statement_events.h:481
@ Q_CHARSET_DATABASE_CODE
Definition: statement_events.h:493
@ Q_UPDATED_DB_NAMES
Definition: statement_events.h:503
@ Q_CATALOG_NZ_CODE
Definition: statement_events.h:491
@ Q_TABLE_MAP_FOR_UPDATE_CODE
Definition: statement_events.h:494
@ Q_MICROSECONDS
Definition: statement_events.h:504
@ Q_ENABLE_CASCADE_TRIGGERS
Definition: statement_events.h:544
@ Q_EXPLICIT_DEFAULTS_FOR_TIMESTAMP
Definition: statement_events.h:520
@ Q_DDL_LOGGED_WITH_XID
Definition: statement_events.h:524
@ Q_INVOKER
Definition: statement_events.h:497
@ Q_DEFAULT_TABLE_ENCRYPTION
Definition: statement_events.h:539
@ Q_SQL_REQUIRE_PRIMARY_KEY
Definition: statement_events.h:534
@ Q_FLAGS2_CODE
Definition: statement_events.h:472
@ Q_SQL_MODE_CODE
Definition: statement_events.h:473
@ Q_AUTO_INCREMENT
Definition: statement_events.h:480
@ Q_TIME_ZONE_CODE
Definition: statement_events.h:482
@ Q_MASTER_DATA_WRITTEN_CODE
Definition: statement_events.h:496
@ Q_CATALOG_CODE
Definition: statement_events.h:479
@ Q_DEFAULT_COLLATION_FOR_UTF8MB4
Definition: statement_events.h:529
uint32_t query_exec_time
Definition: statement_events.h:570
unsigned long data_len
Definition: statement_events.h:558
uint16_t status_vars_len
Definition: statement_events.h:593
size_t user_len
Definition: statement_events.h:553
uint16_t auto_increment_offset
Definition: statement_events.h:619
uint16_t charset_database_number
Definition: statement_events.h:628
uint32_t thread_id
Definition: statement_events.h:569
uint16_t lc_time_names_number
Definition: statement_events.h:627
bool flags2_inited
Definition: statement_events.h:612
Query_event_post_header_offset
query event post-header
Definition: statement_events.h:461
@ Q_DATA_OFFSET
Definition: statement_events.h:467
@ Q_EXEC_TIME_OFFSET
Definition: statement_events.h:463
@ Q_STATUS_VARS_LEN_OFFSET
Definition: statement_events.h:466
@ Q_THREAD_ID_OFFSET
Definition: statement_events.h:462
@ Q_ERR_CODE_OFFSET
Definition: statement_events.h:465
@ Q_DB_LEN_OFFSET
Definition: statement_events.h:464
uint8_t enable_cascade_triggers
Definition: statement_events.h:662
size_t time_zone_len
Definition: statement_events.h:621
size_t q_len
Definition: statement_events.h:599
Logs random seed used by the next RAND(), and by PASSWORD() in 4.1.0.
Definition: statement_events.h:1047
Rand_event(unsigned long long seed1_arg, unsigned long long seed2_arg)
Definition: statement_events.h:1057
unsigned long long seed1
Definition: statement_events.h:1049
unsigned long long seed2
Definition: statement_events.h:1050
Rand_event_data
Definition: statement_events.h:1051
@ RAND_SEED2_OFFSET
Definition: statement_events.h:1051
@ RAND_SEED1_OFFSET
Definition: statement_events.h:1051
Written every time a statement uses a user variable; precedes other events for the statement.
Definition: statement_events.h:818
const char * name
Definition: statement_events.h:867
uint32_t val_len
Definition: statement_events.h:870
@ UNSIGNED_F
Definition: statement_events.h:821
@ UNDEF_F
Definition: statement_events.h:821
Value_type type
Definition: statement_events.h:871
char * val
Definition: statement_events.h:869
bool is_null
Definition: statement_events.h:873
unsigned int charset_number
Definition: statement_events.h:872
User_var_event_data
Definition: statement_events.h:822
@ UV_VAL_LEN_SIZE
Definition: statement_events.h:823
@ UV_VAL_IS_NULL
Definition: statement_events.h:824
@ UV_CHARSET_NUMBER_SIZE
Definition: statement_events.h:827
@ UV_VAL_TYPE_SIZE
Definition: statement_events.h:825
@ UV_NAME_LEN_SIZE
Definition: statement_events.h:826
~User_var_event() override
Definition: statement_events.cpp:472
User_var_event(const char *name_arg, unsigned int name_len_arg, char *val_arg, unsigned long val_len_arg, Value_type type_arg, unsigned int charset_number_arg, unsigned char flags_arg)
This constructor will initialize the instance variables and the type_code, it will be used only by th...
Definition: statement_events.h:834
unsigned char flags
Definition: statement_events.h:874
unsigned int name_len
Definition: statement_events.h:868
Contains the classes representing events operating in the replication stream properties.
#define MAX_DBS_IN_EVENT_MTS
The maximum number of updated databases that a status of Query-log-event can carry.
Definition: binlog_event.h:109
#define T
Definition: jit_executor_value.cc:373
#define NAME_LEN
Definition: mysql_com.h:67
Definition: buf0block_hint.cc:30
The namespace contains classes representing events that can occur in a replication stream.
Definition: binlog_event.cpp:38
Log_event_type
Enumeration type for the different types of log events.
Definition: binlog_event.h:279
@ QUERY_EVENT
Definition: binlog_event.h:293
@ RAND_EVENT
Definition: binlog_event.h:303
@ INTVAR_EVENT
Definition: binlog_event.h:296
@ USER_VAR_EVENT
Definition: binlog_event.h:304
T available_buffer(const char *buf_start, const char *buf_current, T buf_len)
Definition: statement_events.h:724
bool valid_buffer_range(T jump, const char *buf_start, const char *buf_current, T buf_len)
Check if jump value is within buffer limits.
Definition: statement_events.h:745
const uint64_t INVALID_XID
The following constant represents the maximum of MYSQL_XID domain.
Definition: statement_events.h:49
const char * bapi_strndup(const char *destination, size_t n)
This is a wrapper function, and returns a pointer to a new string which is a duplicate of the input s...
Definition: wrapper_functions.h:150
Item_result
Type of the user defined function return slot and arguments.
Definition: udf_registration_types.h:39
@ STRING_RESULT
not valid for UDFs
Definition: udf_registration_types.h:41
@ DECIMAL_RESULT
not valid for UDFs
Definition: udf_registration_types.h:45
@ REAL_RESULT
char *
Definition: udf_registration_types.h:42
@ INT_RESULT
double
Definition: udf_registration_types.h:43
@ ROW_RESULT
long long
Definition: udf_registration_types.h:44