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