MySQL 9.7.2
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
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 system_variables.h for a list of the possible
207 flags. Currently, 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 MODE_INTERPRET_UTF8_AS_UTF8MB4==0x200000000
231 </pre>
232 All these flags are replicated from the server. However, all
233 flags except @c MODE_NO_DIR_IN_CREATE are honored by the slave;
234 the slave always preserves its old value of @c
235 MODE_NO_DIR_IN_CREATE.
236
237 This field is always written to the binlog.
238 </td>
239 </tr>
240
241 <tr>
242 <td>catalog</td>
243 <td>Q_CATALOG_NZ_CODE == 6</td>
244 <td>Variable-length string: the length in bytes (1 byte) followed
245 by the characters (at most 255 bytes)
246 </td>
247 <td>Stores the client's current catalog. Every database belongs
248 to a catalog, the same way that every table belongs to a
249 database. Currently, there is only one catalog, "std".
250
251 This field is written if the length of the catalog is > 0;
252 otherwise it is not written.
253 </td>
254 </tr>
255
256 <tr>
257 <td>auto_increment</td>
258 <td>Q_AUTO_INCREMENT == 3</td>
259 <td>two 2 byte unsigned integers, totally 2+2=4 bytes</td>
260
261 <td>The two variables auto_increment_increment and
262 auto_increment_offset, in that order. For more information, see
263 "System variables" in the MySQL manual.
264
265 This field is written if auto_increment > 1. Otherwise, it is not
266 written.
267 </td>
268 </tr>
269
270 <tr>
271 <td>charset</td>
272 <td>Q_CHARSET_CODE == 4</td>
273 <td>three 2 byte unsigned integers, totally 2+2+2=6 bytes</td>
274 <td>The three variables character_set_client,
275 collation_connection, and collation_server, in that order.
276 character_set_client is a code identifying the character set and
277 collation used by the client to encode the query.
278 collation_connection identifies the character set and collation
279 that the master converts the query to when it receives it; this is
280 useful when comparing literal strings. collation_server is the
281 default character set and collation used when a new database is
282 created.
283
284 See also "Connection Character Sets and Collations" in the MySQL
285 5.1 manual.
286
287 All three variables are codes identifying a (character set,
288 collation) pair. To see which codes map to which pairs, run the
289 query "SELECT id, character_set_name, collation_name FROM
290 COLLATIONS".
291
292 Cf. Q_CHARSET_DATABASE_CODE below.
293
294 This field is always written.
295 </td>
296 </tr>
297
298 <tr>
299 <td>time_zone</td>
300 <td>Q_TIME_ZONE_CODE == 5</td>
301 <td>Variable-length string: the length in bytes (1 byte) followed
302 by the characters (at most 255 bytes).
303 <td>The time_zone of the master.
304
305 See also "System Variables" and "MySQL Server Time Zone Support"
306 in the MySQL manual.
307
308 This field is written if the length of the time zone string is >
309 0; otherwise, it is not written.
310 </td>
311 </tr>
312
313 <tr>
314 <td>lc_time_names_number</td>
315 <td>Q_LC_TIME_NAMES_CODE == 7</td>
316 <td>2 byte integer</td>
317 <td>A code identifying a table of month and day names. The
318 mapping from codes to languages is defined in @c sql_locale.cc.
319
320 This field is written if it is not 0, i.e., if the locale is not
321 en_US.
322 </td>
323 </tr>
324
325 <tr>
326 <td>charset_database_number</td>
327 <td>Q_CHARSET_DATABASE_CODE == 8</td>
328 <td>2 byte integer</td>
329
330 <td>The value of the collation_database system variable (in the
331 source code stored in @c thd->variables.collation_database), which
332 holds the code for a (character set, collation) pair as described
333 above (see Q_CHARSET_CODE).
334
335 collation_database was used in old versions (???WHEN). Its value
336 was loaded when issuing a "use db" query and could be changed by
337 issuing a "SET collation_database=xxx" query. It used to affect
338 the "LOAD DATA INFILE" and "CREATE TABLE" commands.
339
340 In newer versions, "CREATE TABLE" has been changed to take the
341 character set from the database of the created table, rather than
342 the character set of the current database. This makes a
343 difference when creating a table in another database than the
344 current one. "LOAD DATA INFILE" has not yet changed to do this,
345 but there are plans to eventually do it, and to make
346 collation_database read-only.
347
348 This field is written if it is not 0.
349 </td>
350 </tr>
351 <tr>
352 <td>table_map_for_update</td>
353 <td>Q_TABLE_MAP_FOR_UPDATE_CODE == 9</td>
354 <td>8 byte integer</td>
355
356 <td>The value of the table map that is to be updated by the
357 multi-table update query statement. Every bit of this variable
358 represents a table, and is set to 1 if the corresponding table is
359 to be updated by this statement.
360
361 The value of this variable is set when executing a multi-table update
362 statement and used by slave to apply filter rules without opening
363 all the tables on slave. This is required because some tables may
364 not exist on slave because of the filter rules.
365 </td>
366 </tr>
367 <tr>
368 <td>master_data_written</td>
369 <td>Q_MASTER_DATA_WRITTEN_CODE == 10</td>
370 <td>4 byte bitfield</td>
371
372 <td>The value of the original length of a Query_event that comes from a
373 master. Master's event is relay-logged with storing the original size of
374 event in this field by the IO thread. The size is to be restored by reading
375 Q_MASTER_DATA_WRITTEN_CODE-marked event from the relay log.
376
377 This field is not written to slave's server binlog by the SQL thread.
378 This field only exists in relay logs where master has binlog_version<4 i.e.
379 server_version < 5.0 and the slave has binlog_version=4.
380 </td>
381 </tr>
382 <tr>
383 <td>binlog_invoker</td>
384 <td>Q_INVOKER == 11</td>
385 <td>2 Variable-length strings: the length in bytes (1 byte) followed
386 by characters (user), again followed by length in bytes (1 byte) followed
387 by characters(host)</td>
388
389 <td>The value of boolean variable m_binlog_invoker is set TRUE if
390 CURRENT_USER() is called in account management statements. SQL thread
391 uses it as a default definer in CREATE/ALTER SP, SF, Event, TRIGGER or
392 VIEW statements.
393
394 The field Q_INVOKER has length of user stored in 1 byte followed by the
395 user string which is assigned to 'user' and the length of host stored in
396 1 byte followed by host string which is assigned to 'host'.
397 </td>
398 </tr>
399 <tr>
400 <td>mts_accessed_dbs</td>
401 <td>Q_UPDATED_DB_NAMES == 12</td>
402 <td>1 byte character, and a 2-D array</td>
403 <td>The total number and the names to of the databases accessed is stored,
404 to be propagated to the slave in order to facilitate the parallel
405 applying of the Query events.
406 </td>
407 </tr>
408 <tr>
409 <td>explicit_defaults_ts</td>
410 <td>Q_EXPLICIT_DEFAULTS_FOR_TIMESTAMP</td>
411 <td>1 byte boolean</td>
412 <td>Stores master connection @@session.explicit_defaults_for_timestamp when
413 CREATE and ALTER operate on a table with a TIMESTAMP column. </td>
414 </tr>
415 <tr>
416 <td>ddl_xid</td>
417 <td>Q_DDL_LOGGED_WITH_XID</td>
418 <td>8 byte integer</td>
419 <td>Stores variable carrying xid info of 2pc-aware (recoverable) DDL
420 queries. </td>
421 </tr>
422 <tr>
423 <td>default_collation_for_utf8mb4_number</td>
424 <td>Q_DEFAULT_COLLATION_FOR_UTF8MB4</td>
425 <td>2 byte integer</td>
426 <td>Stores variable carrying the the default collation for the utf8mb4
427 character set. Mainly used to support replication 5.7- master to a 8.0+
428 slave.
429 </td>
430 </tr>
431 <tr>
432 <td>sql_require_primary_key</td>
433 <td>Q_SQL_REQUIRE_PRIMARY_KEY</td>
434 <td>2 byte integer</td>
435 <td>Value of the config variable sql_require_primary_key</td>
436 </tr>
437 <tr>
438 <td>default_table_encryption</td>
439 <td>Q_DEFAULT_TABLE_ENCRYPTION</td>
440 <td>2 byte integer</td>
441 <td>Value of the config variable default_table_encryption</td>
442 </tr>
443 <tr>
444 <td>enable_cascade_triggers</td>
445 <td>Q_ENABLE_CASCADE_TRIGGERS</td>
446 <td>1 byte boolean</td>
447 <td>Value of the config variable enable_cascade_triggers</td>
448 </tr>
449 </table>
450
451 @subsection Query_event_notes_on_previous_versions Notes on Previous Versions
452
453 * Status vars were introduced in version 5.0. To read earlier
454 versions correctly, check the length of the Post-Header.
455
456 * The status variable Q_CATALOG_CODE == 2 existed in MySQL 5.0.x,
457 where 0<=x<=3. It was identical to Q_CATALOG_CODE, except that the
458 string had a trailing '\0'. The '\0' was removed in 5.0.4 since it
459 was redundant (the string length is stored before the string). The
460 Q_CATALOG_CODE will never be written by a new master, but can still
461 be understood by a new slave.
462
463 * See Q_CHARSET_DATABASE_CODE in the table above.
464
465 * When adding new status vars, please don't forget to update the
466 MAX_SIZE_LOG_EVENT_STATUS.
467
468*/
469
471 public:
472 /** query event post-header */
480 };
481
482 /* these are codes, not offsets; not more than 256 values (1 byte). */
486 /*
487 Q_CATALOG_CODE is catalog with end zero stored; it is used only by MySQL
488 5.0.x where 0<=x<=3. We have to keep it to be able to replicate these
489 old masters.
490 */
495 /*
496 Q_CATALOG_NZ_CODE is catalog withOUT end zero stored; it is used by MySQL
497 5.0.x where x>=4. Saves one byte in every Query_event in binlog,
498 compared to Q_CATALOG_CODE. The reason we didn't simply re-use
499 Q_CATALOG_CODE is that then a 5.0.3 slave of this 5.0.x (x>=4)
500 master would crash (segfault etc) because it would expect a 0 when there
501 is none.
502 */
507 /* It is just a placeholder after 8.0.2*/
510 /*
511 Q_UPDATED_DB_NAMES status variable collects information of accessed
512 databases i.e. the total number and the names to be propagated to the
513 slave in order to facilitate the parallel applying of the Query events.
514 */
517 /*
518 A old (unused now) code for Query_log_event status similar to G_COMMIT_TS.
519 */
521 /*
522 An old (unused after migration to Gtid_event) code for
523 Query_log_event status, similar to G_COMMIT_TS2.
524 */
526 /*
527 The master connection @@session.explicit_defaults_for_timestamp which
528 is recorded for queries, CREATE and ALTER table that is defined with
529 a TIMESTAMP column, that are dependent on that feature.
530 For pre-WL6292 master's the associated with this code value is zero.
531 */
533 /*
534 The variable carries xid info of 2pc-aware (recoverable) DDL queries.
535 */
537 /*
538 This variable stores the default collation for the utf8mb4 character set.
539 Used to support cross-version replication.
540 */
542
543 /*
544 Replicate sql_require_primary_key.
545 */
547
548 /*
549 Replicate default_table_encryption.
550 */
552
553 /*
554 Replicate enable_cascade_triggers.
555 */
557 };
558 const char *query;
559 const char *db;
560 const char *catalog;
561 const char *time_zone_str;
562
563 protected:
564 const char *user;
565 size_t user_len;
566 const char *host;
567 size_t host_len;
568
569 /* Required by the MySQL server class Log_event::Query_event */
570 unsigned long data_len;
571 /*
572 Copies data into the buffer in the following fashion
573 +--------+-----------+------+------+---------+----+-------+----+
574 | catlog | time_zone | user | host | db name | \0 | Query | \0 |
575 +--------+-----------+------+------+---------+----+-------+----+
576 */
577 int fill_data_buf(unsigned char *dest, unsigned long len);
578
579 public:
580 /* data members defined in order they are packed and written into the log */
581 uint32_t thread_id;
583 size_t db_len;
584 uint16_t error_code;
585 /*
586 We want to be able to store a variable number of N-bit status vars:
587 (generally N=32; but N=64 for SQL_MODE) a user may want to log the number
588 of affected rows (for debugging) while another does not want to lose 4
589 bytes in this.
590 The storage on disk is the following:
591 status_vars_len is part of the post-header,
592 status_vars are in the variable-length part, after the post-header, before
593 the db & query.
594 status_vars on disk is a sequence of pairs (code, value) where 'code' means
595 'sql_mode', 'affected' etc. Sometimes 'value' must be a short string, so
596 its first byte is its length. For now the order of status vars is:
597 flags2 - sql_mode - catalog - autoinc - charset
598 We should add the same thing to Load_event, but in fact
599 LOAD DATA INFILE is going to be logged with a new type of event (logging of
600 the plain text query), so Load_event would be frozen, so no need. The
601 new way of logging LOAD DATA INFILE would use a derived class of
602 Query_event, so automatically benefit from the work already done for
603 status variables in Query_event.
604 */
606 /*
607 If we already know the length of the query string
608 we pass it with q_len, so we would not have to call strlen()
609 otherwise, set it to 0, in which case, we compute it with strlen()
610 */
611 size_t q_len;
612
613 /* The members below represent the status variable block */
614
615 /*
616 'flags2' is a second set of flags (on top of those in Log_event), for
617 session variables. These are thd->options which is & against a mask
618 (OPTIONS_WRITTEN_TO_BIN_LOG).
619 flags2_inited helps make a difference between flags2==0 (3.23 or 4.x
620 master, we don't know flags2, so use the slave server's global options) and
621 flags2==0 (5.0 master, we know this has a meaning of flags all down which
622 must influence the query).
623 */
627
628 uint32_t flags2;
629 /* In connections sql_mode is 32 bits now but will be 64 bits soon */
630 uint64_t sql_mode;
632 char charset[6];
633 size_t time_zone_len; /* 0 means uninited */
634 /*
635 Binlog format 3 and 4 start to differ (as far as class members are
636 concerned) from here.
637 */
638 size_t catalog_len; // <= 255 char; 0 means uninited
639 uint16_t lc_time_names_number; /* 0 means en_US */
641 /*
642 map for tables that will be updated for a multi-table update query
643 statement, for other query statements, this will be zero.
644 */
646 /*
647 The following member gets set to OFF or ON value when the
648 Query-log-event is marked as dependent on
649 @@explicit_defaults_for_timestamp. That is the member is relevant
650 to queries that declare TIMESTAMP column attribute, like CREATE
651 and ALTER.
652 The value is set to @c TERNARY_OFF when @@explicit_defaults_for_timestamp
653 encoded value is zero, otherwise TERNARY_ON.
654 */
660 /*
661 number of updated databases by the query and their names. This info
662 is requested by both Coordinator and Worker.
663 */
664 unsigned char mts_accessed_dbs;
666 /* XID value when the event is a 2pc-capable DDL */
667 uint64_t ddl_xid;
668 /* Default collation for the utf8mb4 set. Used in cross-version replication */
671
673
675
676 /**
677 The constructor will be used while creating a Query_event, to be
678 written to the binary log.
679 */
680 Query_event(const char *query_arg, const char *catalog_arg,
681 const char *db_arg, uint32_t query_length,
682 unsigned long thread_id_arg, unsigned long long sql_mode_arg,
683 unsigned long auto_increment_increment_arg,
684 unsigned long auto_increment_offset_arg, unsigned int number,
685 unsigned long long table_map_for_update_arg, int errcode);
686
687 /**
688 The constructor receives a buffer and instantiates a Query_event filled in
689 with the data from the buffer
690
691 <pre>
692 The fixed event data part buffer layout is as follows:
693 +---------------------------------------------------------------------+
694 | thread_id | query_exec_time | db_len | error_code | status_vars_len |
695 +---------------------------------------------------------------------+
696 </pre>
697
698 <pre>
699 The fixed event data part buffer layout is as follows:
700 +--------------------------------------------+
701 | Zero or more status variables | db | query |
702 +--------------------------------------------+
703 </pre>
704
705 @param buf Contains the serialized event.
706 @param fde An FDE event (see Rotate_event constructor for more info).
707 @param event_type Required to determine whether the event type is
708 QUERY_EVENT or EXECUTE_LOAD_QUERY_EVENT
709 */
710 Query_event(const char *buf, const Format_description_event *fde,
711 Log_event_type event_type);
712 /**
713 The simplest constructor that could possibly work. This is used for
714 creating static objects that have a special meaning and are invisible
715 to the log.
716 */
718 ~Query_event() override = default;
719
720#ifndef HAVE_MYSYS
721 void print_event_info(std::ostream &info) override;
722 void print_long_info(std::ostream &info) override;
723#endif
724};
725
726/*
727 Check how many bytes are available on buffer.
728
729 @param buf_start Pointer to buffer start.
730 @param buf_current Pointer to the current position on buffer.
731 @param buf_len Buffer length.
732
733 @return Number of bytes available on event buffer.
734*/
735template <class T>
736T available_buffer(const char *buf_start, const char *buf_current, T buf_len) {
737 /* Sanity check */
738 if (buf_current < buf_start ||
739 buf_len < static_cast<T>(buf_current - buf_start))
740 return static_cast<T>(0);
741
742 return static_cast<T>(buf_len - (buf_current - buf_start));
743}
744
745/**
746 Check if jump value is within buffer limits.
747
748 @param jump Number of positions we want to advance.
749 @param buf_start Pointer to buffer start
750 @param buf_current Pointer to the current position on buffer.
751 @param buf_len Buffer length.
752
753 @retval True If jump value is within buffer limits.
754 @retval False Otherwise.
755*/
756template <class T>
757bool valid_buffer_range(T jump, const char *buf_start, const char *buf_current,
758 T buf_len) {
759 return (jump <= available_buffer(buf_start, buf_current, buf_len));
760}
761
762/**
763 @class User_var_event
764
765 Written every time a statement uses a user variable; precedes other
766 events for the statement. Indicates the value to use for the user
767 variable in the next statement. This is written only before a QUERY_EVENT
768 and is not used with row-based logging
769
770 The Post-Header has following components:
771
772 <table>
773 <caption>Post-Header for Format_description_event</caption>
774
775 <tr>
776 <th>Name</th>
777 <th>Format</th>
778 <th>Description</th>
779 </tr>
780
781 <tr>
782 <td>Value_type</td>
783 <td>enum</td>
784 <td>The user variable type.</td>
785 </tr>
786 <tr>
787 <td>User_var_event_data</td>
788 <td>enum</td>
789 <td>User_var event data</td>
790 </tr>
791 <tr>
792 <td>name</td>
793 <td>const char pointer</td>
794 <td>User variable name.</td>
795 </tr>
796 <tr>
797 <td>name_len</td>
798 <td>unsigned int</td>
799 <td>Length of the user variable name</td>
800 </tr>
801 <tr>
802 <td>val</td>
803 <td>char pointer</td>
804 <td>value of the user variable.</td>
805 </tr>
806 <tr>
807 <td>val_len</td>
808 <td>unsigned long</td>
809 <td>Length of the value of the user variable</td>
810 </tr>
811 <tr>
812 <td>type</td>
813 <td>enum Value_type</td>
814 <td>Type of the user variable</td>
815 </tr>
816 <tr>
817 <td>charset_number</td>
818 <td>unsigned int</td>
819 <td>The number of the character set for the user variable (needed for a
820 string variable). The character set number is really a collation
821 number that indicates a character set/collation pair.</td>
822 </tr>
823 <tr>
824 <td>is_null</td>
825 <td>bool</td>
826 <td>Non-zero if the variable value is the SQL NULL value, 0 otherwise.</td>
827 </tr>
828 </table>
829*/
831 public:
840 };
841
842 /**
843 This constructor will initialize the instance variables and the type_code,
844 it will be used only by the server code.
845 */
846 User_var_event(const char *name_arg, unsigned int name_len_arg, char *val_arg,
847 unsigned long val_len_arg, Value_type type_arg,
848 unsigned int charset_number_arg, unsigned char flags_arg)
850 name(bapi_strndup(name_arg, name_len_arg)),
851 name_len(name_len_arg),
852 val(val_arg),
853 val_len(val_len_arg),
854 type(type_arg),
855 charset_number(charset_number_arg),
856 is_null(!val),
857 flags(flags_arg) {}
858
859 /**
860 The constructor receives a buffer and instantiates a User_var_event filled
861 in with the data from the buffer
862 Written every time a statement uses a user variable, precedes other
863 events for the statement. Indicates the value to use for the
864 user variable in the next statement. This is written only before a
865 QUERY_EVENT and is not used with row-based logging.
866
867 The buffer layout for variable data part is as follows:
868 <pre>
869 +-------------------------------------------------------------------+
870 | name_len | name | is_null | type | charset_number | val_len | val |
871 +-------------------------------------------------------------------+
872 </pre>
873
874 @param buf Contains the serialized event.
875 @param fde An FDE event (see Rotate_event constructor for more info).
876 */
877 User_var_event(const char *buf, const Format_description_event *fde);
878 ~User_var_event() override;
879 const char *name;
880 unsigned int name_len;
881 char *val;
882 uint32_t val_len;
884 unsigned int charset_number;
886 unsigned char flags;
887#ifndef HAVE_MYSYS
888 void print_event_info(std::ostream &info) override;
889 void print_long_info(std::ostream &info) override;
890 const char *get_value_type_string(Value_type type_arg) const {
891 switch (type_arg) {
892 case STRING_RESULT:
893 return "String";
894 case REAL_RESULT:
895 return "Real";
896 case INT_RESULT:
897 return "Integer";
898 case ROW_RESULT:
899 return "Row";
900 case DECIMAL_RESULT:
901 return "Decimal";
902 default:
903 return "Unknown";
904 }
905 }
906#endif
907};
908
909/**
910 @class Intvar_event
911
912 An Intvar_event will be created just before a Query_event,
913 if the query uses one of the variables LAST_INSERT_ID or INSERT_ID.
914 Each Intvar_event holds the value of one of these variables.
915
916 @section Intvar_event_binary_format Binary Format
917
918 The Post-Header for this event type is empty. The Body has two
919 components:
920
921 <table>
922 <caption>Body for Intvar_event</caption>
923
924 <tr>
925 <th>Name</th>
926 <th>Format</th>
927 <th>Description</th>
928 </tr>
929
930 <tr>
931 <td>type</td>
932 <td>1 byte enumeration</td>
933 <td>One byte identifying the type of variable stored. Currently,
934 two identifiers are supported: LAST_INSERT_ID_EVENT == 1 and
935 INSERT_ID_EVENT == 2.
936 </td>
937 </tr>
938
939 <tr>
940 <td>val</td>
941 <td>8 byte unsigned integer</td>
942 <td>The value of the variable.</td>
943 </tr>
944
945 </table>
946*/
948 public:
949 uint8_t type;
950 uint64_t val;
951
952 /*
953 The enum recognizes the type of variables that can occur in an
954 INTVAR_EVENT. The two types supported are LAST_INSERT_ID and
955 INSERT_ID, in accordance to the SQL query using LAST_INSERT_ID
956 or INSERT_ID.
957 */
962 };
963
964 /**
965 moving from pre processor symbols from global scope in log_event.h
966 to an enum inside the class, since these are used only by
967 members of this class itself.
968 */
970
971 /**
972 This method returns the string representing the type of the variable
973 used in the event. Changed the definition to be similar to that
974 previously defined in log_event.cc.
975 */
976 std::string get_var_type_string() const {
977 switch (type) {
979 return "INVALID_INT";
981 return "LAST_INSERT_ID";
982 case INSERT_ID_EVENT:
983 return "INSERT_ID";
984 default: /* impossible */
985 return "UNKNOWN";
986 }
987 }
988
989 /**
990 Constructor receives a packet from the MySQL master or the binary
991 log and decodes it to create an Intvar_event.
992
993 The post header for the event is empty. Buffer layout for the variable
994 data part is as follows:
995 <pre>
996 +--------------------------------+
997 | type (4 bytes) | val (8 bytes) |
998 +--------------------------------+
999 </pre>
1000
1001 @param buf Contains the serialized event.
1002 @param fde An FDE event (see Rotate_event constructor for more info).
1003 */
1004 Intvar_event(const char *buf, const Format_description_event *fde);
1005 /**
1006 The minimal constructor for Intvar_event it initializes the instance
1007 variables type & val and set the type_code as INTVAR_EVENT in the header
1008 object in Binary_log_event
1009 */
1010 Intvar_event(uint8_t type_arg, uint64_t val_arg)
1011 : Binary_log_event(INTVAR_EVENT), type(type_arg), val(val_arg) {}
1012
1013 ~Intvar_event() override = default;
1014
1015#ifndef HAVE_MYSYS
1016 void print_event_info(std::ostream &info) override;
1017 void print_long_info(std::ostream &info) override;
1018#endif
1019};
1020
1021/**
1022 @class Rand_event
1023
1024 Logs random seed used by the next RAND(), and by PASSWORD() in 4.1.0.
1025 4.1.1 does not need it (it's repeatable again) so this event needn't be
1026 written in 4.1.1 for PASSWORD() (but the fact that it is written is just a
1027 waste, it does not cause bugs).
1028
1029 The state of the random number generation consists of 128 bits,
1030 which are stored internally as two 64-bit numbers.
1031
1032 @section Rand_event_binary_format Binary Format
1033
1034 The Post-Header for this event type is empty. The Body has two
1035 components:
1036
1037 <table>
1038 <caption>Body for Rand_event</caption>
1039
1040 <tr>
1041 <th>Name</th>
1042 <th>Format</th>
1043 <th>Description</th>
1044 </tr>
1045
1046 <tr>
1047 <td>seed1</td>
1048 <td>8 byte unsigned integer</td>
1049 <td>64 bit random seed1.</td>
1050 </tr>
1051
1052 <tr>
1053 <td>seed2</td>
1054 <td>8 byte unsigned integer</td>
1055 <td>64 bit random seed2.</td>
1056 </tr>
1057 </table>
1058*/
1060 public:
1061 unsigned long long seed1;
1062 unsigned long long seed2;
1064
1065 /**
1066 This will initialize the instance variables seed1 & seed2, and set the
1067type_code as RAND_EVENT in the header object in Binary_log_event
1068 */
1069 Rand_event(unsigned long long seed1_arg, unsigned long long seed2_arg)
1071 seed1 = seed1_arg;
1072 seed2 = seed2_arg;
1073 }
1074
1075 /**
1076 Written every time a statement uses the RAND() function; precedes other
1077 events for the statement. Indicates the seed values to use for generating a
1078 random number with RAND() in the next statement. This is written only before
1079 a QUERY_EVENT and is not used with row-based logging
1080
1081 <pre>
1082 The buffer layout for variable part is as follows:
1083 +----------------------------------------------+
1084 | value for first seed | value for second seed |
1085 +----------------------------------------------+
1086 </pre>
1087
1088 @param buf Contains the serialized event.
1089 @param fde An FDE event (see Rotate_event constructor for more info).
1090 */
1091 Rand_event(const char *buf, const Format_description_event *fde);
1092#ifndef HAVE_MYSYS
1093 void print_event_info(std::ostream &info) override;
1094 void print_long_info(std::ostream &info) override;
1095#endif
1096};
1097} // end namespace mysql::binlog::event
1098
1099/// @}
1100
1101#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:947
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:512
uint64_t val
Definition: statement_events.h:950
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:976
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:1010
Int_event_type
Definition: statement_events.h:958
@ LAST_INSERT_ID_EVENT
Definition: statement_events.h:960
@ INVALID_INT_EVENT
Definition: statement_events.h:959
@ INSERT_ID_EVENT
Definition: statement_events.h:961
uint8_t type
Definition: statement_events.h:949
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:969
@ I_TYPE_OFFSET
Definition: statement_events.h:969
@ I_VAL_OFFSET
Definition: statement_events.h:969
A Query_event is created for each query that modifies the database, unless the query is logged row-ba...
Definition: statement_events.h:470
uint16_t error_code
Definition: statement_events.h:584
size_t host_len
Definition: statement_events.h:567
uint8_t default_table_encryption
Definition: statement_events.h:672
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:566
uint64_t table_map_for_update
Definition: statement_events.h:645
uint16_t auto_increment_increment
Definition: statement_events.h:631
const char * db
Definition: statement_events.h:559
const char * user
Definition: statement_events.h:564
uint64_t sql_mode
Definition: statement_events.h:630
const char * query
Definition: statement_events.h:558
char charset[6]
Definition: statement_events.h:632
uint64_t ddl_xid
Definition: statement_events.h:667
enum mysql::binlog::event::Query_event::enum_ternary explicit_defaults_ts
bool charset_inited
Definition: statement_events.h:626
unsigned char mts_accessed_dbs
Definition: statement_events.h:664
const char * catalog
Definition: statement_events.h:560
uint16_t default_collation_for_utf8mb4_number
Definition: statement_events.h:669
size_t db_len
Definition: statement_events.h:583
const char * time_zone_str
Definition: statement_events.h:561
bool sql_mode_inited
Definition: statement_events.h:625
enum_ternary
Definition: statement_events.h:655
@ TERNARY_OFF
Definition: statement_events.h:657
@ TERNARY_UNSET
Definition: statement_events.h:656
@ TERNARY_ON
Definition: statement_events.h:658
char mts_accessed_db_names[MAX_DBS_IN_EVENT_MTS][NAME_LEN]
Definition: statement_events.h:665
int fill_data_buf(unsigned char *dest, unsigned long len)
Layout for the data buffer is as follows.
Definition: statement_events.cpp:394
uint8_t sql_require_primary_key
Definition: statement_events.h:670
uint32_t flags2
Definition: statement_events.h:628
size_t catalog_len
Definition: statement_events.h:638
Query_event_status_vars
Definition: statement_events.h:483
@ Q_COMMIT_TS2
Definition: statement_events.h:525
@ Q_LC_TIME_NAMES_CODE
Definition: statement_events.h:504
@ Q_COMMIT_TS
Definition: statement_events.h:520
@ Q_CHARSET_CODE
Definition: statement_events.h:493
@ Q_CHARSET_DATABASE_CODE
Definition: statement_events.h:505
@ Q_UPDATED_DB_NAMES
Definition: statement_events.h:515
@ Q_CATALOG_NZ_CODE
Definition: statement_events.h:503
@ Q_TABLE_MAP_FOR_UPDATE_CODE
Definition: statement_events.h:506
@ Q_MICROSECONDS
Definition: statement_events.h:516
@ Q_ENABLE_CASCADE_TRIGGERS
Definition: statement_events.h:556
@ Q_EXPLICIT_DEFAULTS_FOR_TIMESTAMP
Definition: statement_events.h:532
@ Q_DDL_LOGGED_WITH_XID
Definition: statement_events.h:536
@ Q_INVOKER
Definition: statement_events.h:509
@ Q_DEFAULT_TABLE_ENCRYPTION
Definition: statement_events.h:551
@ Q_SQL_REQUIRE_PRIMARY_KEY
Definition: statement_events.h:546
@ Q_FLAGS2_CODE
Definition: statement_events.h:484
@ Q_SQL_MODE_CODE
Definition: statement_events.h:485
@ Q_AUTO_INCREMENT
Definition: statement_events.h:492
@ Q_TIME_ZONE_CODE
Definition: statement_events.h:494
@ Q_MASTER_DATA_WRITTEN_CODE
Definition: statement_events.h:508
@ Q_CATALOG_CODE
Definition: statement_events.h:491
@ Q_DEFAULT_COLLATION_FOR_UTF8MB4
Definition: statement_events.h:541
uint32_t query_exec_time
Definition: statement_events.h:582
unsigned long data_len
Definition: statement_events.h:570
uint16_t status_vars_len
Definition: statement_events.h:605
size_t user_len
Definition: statement_events.h:565
uint16_t auto_increment_offset
Definition: statement_events.h:631
uint16_t charset_database_number
Definition: statement_events.h:640
uint32_t thread_id
Definition: statement_events.h:581
uint16_t lc_time_names_number
Definition: statement_events.h:639
bool flags2_inited
Definition: statement_events.h:624
Query_event_post_header_offset
query event post-header
Definition: statement_events.h:473
@ Q_DATA_OFFSET
Definition: statement_events.h:479
@ Q_EXEC_TIME_OFFSET
Definition: statement_events.h:475
@ Q_STATUS_VARS_LEN_OFFSET
Definition: statement_events.h:478
@ Q_THREAD_ID_OFFSET
Definition: statement_events.h:474
@ Q_ERR_CODE_OFFSET
Definition: statement_events.h:477
@ Q_DB_LEN_OFFSET
Definition: statement_events.h:476
uint8_t enable_cascade_triggers
Definition: statement_events.h:674
size_t time_zone_len
Definition: statement_events.h:633
size_t q_len
Definition: statement_events.h:611
Logs random seed used by the next RAND(), and by PASSWORD() in 4.1.0.
Definition: statement_events.h:1059
Rand_event(unsigned long long seed1_arg, unsigned long long seed2_arg)
Definition: statement_events.h:1069
unsigned long long seed1
Definition: statement_events.h:1061
unsigned long long seed2
Definition: statement_events.h:1062
Rand_event_data
Definition: statement_events.h:1063
@ RAND_SEED2_OFFSET
Definition: statement_events.h:1063
@ RAND_SEED1_OFFSET
Definition: statement_events.h:1063
Written every time a statement uses a user variable; precedes other events for the statement.
Definition: statement_events.h:830
const char * name
Definition: statement_events.h:879
uint32_t val_len
Definition: statement_events.h:882
@ UNSIGNED_F
Definition: statement_events.h:833
@ UNDEF_F
Definition: statement_events.h:833
Value_type type
Definition: statement_events.h:883
char * val
Definition: statement_events.h:881
bool is_null
Definition: statement_events.h:885
unsigned int charset_number
Definition: statement_events.h:884
User_var_event_data
Definition: statement_events.h:834
@ UV_VAL_LEN_SIZE
Definition: statement_events.h:835
@ UV_VAL_IS_NULL
Definition: statement_events.h:836
@ UV_CHARSET_NUMBER_SIZE
Definition: statement_events.h:839
@ UV_VAL_TYPE_SIZE
Definition: statement_events.h:837
@ UV_NAME_LEN_SIZE
Definition: statement_events.h:838
~User_var_event() override
Definition: statement_events.cpp:496
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:846
unsigned char flags
Definition: statement_events.h:886
unsigned int name_len
Definition: statement_events.h:880
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
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:736
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:757
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