MySQL 8.0.39
Source Code Documentation
rows_event.h
Go to the documentation of this file.
1/* Copyright (c) 2014, 2024, 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 @addtogroup Replication
26 @{
27
28 @file rows_event.h
29
30 @brief Contains the classes representing events which are used for row based
31 replication. In row-based replication, the master writes events to the binary
32 log that indicate how individual table rows are changed.
33*/
34
35#ifndef ROWS_EVENT_INCLUDED
36#define ROWS_EVENT_INCLUDED
37
38#include <vector>
39#include "control_events.h"
40#include "table_id.h"
41
42/**
43 1 byte length, 1 byte format
44 Length is total length in bytes, including 2 byte header
45 Length values 0 and 1 are currently invalid and reserved.
46*/
47#define EXTRA_ROW_INFO_LEN_OFFSET 0
48#define EXTRA_ROW_INFO_FORMAT_OFFSET 1
49#define EXTRA_ROW_INFO_HEADER_LENGTH 2
50#define EXTRA_ROW_INFO_MAX_PAYLOAD (255 - EXTRA_ROW_INFO_HEADER_LENGTH)
51
52#define ROWS_MAPID_OFFSET 0
53#define ROWS_FLAGS_OFFSET 6
54#define ROWS_VHLEN_OFFSET 8
55#define EXTRA_ROW_INFO_TYPECODE_LENGTH 1
56#define EXTRA_ROW_PART_INFO_VALUE_LENGTH 2
57
58/**
59 This is the typecode defined for the different elements present in
60 the container Extra_row_info, this is different from the format information
61 stored inside extra_row_ndb_info at EXTRA_ROW_INFO_FORMAT_OFFSET.
62*/
63enum class enum_extra_row_info_typecode { NDB = 0, PART = 1 };
64
65namespace binary_log {
66/**
67 @class Table_map_event
68
69 In row-based mode, every row operation event is preceded by a
70 Table_map_event which maps a table definition to a number. The
71 table definition consists of database name, table name, and column
72 definitions.
73
74 @section Table_map_event_binary_format Binary Format
75
76 The Post-Header has the following components:
77
78 <table>
79 <caption>Post-Header for Table_map_event</caption>
80
81 <tr>
82 <th>Name</th>
83 <th>Format</th>
84 <th>Description</th>
85 </tr>
86
87 <tr>
88 <td>table_id</td>
89 <td>6 bytes unsigned integer</td>
90 <td>The number that identifies the table.</td>
91 </tr>
92
93 <tr>
94 <td>flags</td>
95 <td>2 byte bitfield</td>
96 <td>Reserved for future use; currently always 0.</td>
97 </tr>
98
99 </table>
100
101 The Body has the following components:
102
103 <table>
104 <caption>Body for Table_map_event</caption>
105
106 <tr>
107 <th>Name</th>
108 <th>Format</th>
109 <th>Description</th>
110 </tr>
111
112 <tr>
113 <td>database_name</td>
114 <td>one byte string length, followed by null-terminated string</td>
115 <td>The name of the database in which the table resides. The name
116 is represented as a packed variable-length integer representing the
117 number of bytes in the name, followed by length bytes containing
118 the database name, followed by a terminating 0 byte. (Note the
119 redundancy in the representation of the length.) </td>
120 </tr>
121
122 <tr>
123 <td>table_name</td>
124 <td> The table name is represented as a packed variable-length integer
125 representing the number of bytes in the name, followed by null-terminated
126 string</td>
127 <td>The name of the table, encoded the same way as the database
128 name above.</td>
129 </tr>
130
131 <tr>
132 <td>column_count</td>
133 <td>@ref packed_integer "Packed Integer"</td>
134 <td>The number of columns in the table, represented as a packed
135 variable-length integer.</td>
136 </tr>
137
138 <tr>
139 <td>column_type</td>
140 <td>List of column_count 1 byte enumeration values</td>
141 <td>The type of each column in the table, listed from left to
142 right. Each byte is mapped to a column type according to the
143 enumeration type enum_field_types defined in mysql_com.h. The
144 mapping of types to numbers is listed in the table @ref
145 Table_table_map_event_column_types "below" (along with
146 description of the associated metadata field). </td>
147 </tr>
148
149 <tr>
150 <td>metadata_length</td>
151 <td>@ref packed_integer "Packed Integer"</td>
152 <td>The length of the following metadata block</td>
153 </tr>
154
155 <tr>
156 <td>metadata</td>
157 <td>list of metadata for each column</td>
158 <td>For each column from left to right, a chunk of data who's
159 length and semantics depends on the type of the column. The
160 length and semantics for the metadata for each column are listed
161 in the table @ref Table_table_map_event_column_types
162 "below".</td>
163 </tr>
164
165 <tr>
166 <td>null_bits</td>
167 <td>column_count bits, rounded up to nearest byte</td>
168 <td>For each column, a bit indicating whether data in the column
169 can be NULL or not. The number of bytes needed for this is
170 int((column_count + 7) / 8). The flag for the first column from the
171 left is in the least-significant bit of the first byte, the second
172 is in the second least significant bit of the first byte, the
173 ninth is in the least significant bit of the second byte, and so
174 on. </td>
175 </tr>
176
177 <tr>
178 <td>optional metadata fields</td>
179 <td>optional metadata fields are stored in Type, Length, Value(TLV) format.
180 Type takes 1 byte. Length is a packed integer value. Values takes
181 Length bytes.
182 </td>
183 <td>There are some optional metadata defined. They are listed in the table
184 @ref Table_table_map_event_optional_metadata. Optional metadata fields
185 follow null_bits. Whether binlogging an optional metadata is decided by the
186 server. The order is not defined, so they can be binlogged in any order.
187 </td>
188 </tr>
189 </table>
190
191 The table below lists all column types, along with the numerical
192 identifier for it and the size and interpretation of meta-data used
193 to describe the type.
194
195 @anchor Table_table_map_event_column_types
196 <table>
197 <caption>Table_map_event column types: numerical identifier and
198 metadata</caption>
199 <tr>
200 <th>Name</th>
201 <th>Identifier</th>
202 <th>Size of metadata in bytes</th>
203 <th>Description of metadata</th>
204 </tr>
205
206 <tr>
207 <td>MYSQL_TYPE_DECIMAL</td><td>0</td>
208 <td>0</td>
209 <td>No column metadata.</td>
210 </tr>
211
212 <tr>
213 <td>MYSQL_TYPE_TINY</td><td>1</td>
214 <td>0</td>
215 <td>No column metadata.</td>
216 </tr>
217
218 <tr>
219 <td>MYSQL_TYPE_SHORT</td><td>2</td>
220 <td>0</td>
221 <td>No column metadata.</td>
222 </tr>
223
224 <tr>
225 <td>MYSQL_TYPE_LONG</td><td>3</td>
226 <td>0</td>
227 <td>No column metadata.</td>
228 </tr>
229
230 <tr>
231 <td>MYSQL_TYPE_FLOAT</td><td>4</td>
232 <td>1 byte</td>
233 <td>1 byte unsigned integer, representing the "pack_length", which
234 is equal to sizeof(float) on the server from which the event
235 originates.</td>
236 </tr>
237
238 <tr>
239 <td>MYSQL_TYPE_DOUBLE</td><td>5</td>
240 <td>1 byte</td>
241 <td>1 byte unsigned integer, representing the "pack_length", which
242 is equal to sizeof(double) on the server from which the event
243 originates.</td>
244 </tr>
245
246 <tr>
247 <td>MYSQL_TYPE_NULL</td><td>6</td>
248 <td>0</td>
249 <td>No column metadata.</td>
250 </tr>
251
252 <tr>
253 <td>MYSQL_TYPE_TIMESTAMP</td><td>7</td>
254 <td>0</td>
255 <td>No column metadata.</td>
256 </tr>
257
258 <tr>
259 <td>MYSQL_TYPE_LONGLONG</td><td>8</td>
260 <td>0</td>
261 <td>No column metadata.</td>
262 </tr>
263
264 <tr>
265 <td>MYSQL_TYPE_INT24</td><td>9</td>
266 <td>0</td>
267 <td>No column metadata.</td>
268 </tr>
269
270 <tr>
271 <td>MYSQL_TYPE_DATE</td><td>10</td>
272 <td>0</td>
273 <td>No column metadata.</td>
274 </tr>
275
276 <tr>
277 <td>MYSQL_TYPE_TIME</td><td>11</td>
278 <td>0</td>
279 <td>No column metadata.</td>
280 </tr>
281
282 <tr>
283 <td>MYSQL_TYPE_DATETIME</td><td>12</td>
284 <td>0</td>
285 <td>No column metadata.</td>
286 </tr>
287
288 <tr>
289 <td>MYSQL_TYPE_YEAR</td><td>13</td>
290 <td>0</td>
291 <td>No column metadata.</td>
292 </tr>
293
294 <tr>
295 <td><i>MYSQL_TYPE_NEWDATE</i></td><td><i>14</i></td>
296 <td>&ndash;</td>
297 <td><i>This enumeration value is only used internally and cannot
298 exist in a binlog.</i></td>
299 </tr>
300
301 <tr>
302 <td>MYSQL_TYPE_VARCHAR</td><td>15</td>
303 <td>2 bytes</td>
304 <td>2 byte unsigned integer representing the maximum length of
305 the string.</td>
306 </tr>
307
308 <tr>
309 <td>MYSQL_TYPE_BIT</td><td>16</td>
310 <td>2 bytes</td>
311 <td>A 1 byte unsigned int representing the length in bits of the
312 bitfield (0 to 64), followed by a 1 byte unsigned int
313 representing the number of bytes occupied by the bitfield. The
314 number of bytes is either int((length + 7) / 8) or int(length / 8).
315 </td>
316 </tr>
317
318 <tr>
319 <td>MYSQL_TYPE_NEWDECIMAL</td><td>246</td>
320 <td>2 bytes</td>
321 <td>A 1 byte unsigned int representing the precision, followed
322 by a 1 byte unsigned int representing the number of decimals.</td>
323 </tr>
324
325 <tr>
326 <td><i>MYSQL_TYPE_ENUM</i></td><td><i>247</i></td>
327 <td>&ndash;</td>
328 <td><i>This enumeration value is only used internally and cannot
329 exist in a binlog.</i></td>
330 </tr>
331
332 <tr>
333 <td><i>MYSQL_TYPE_SET</i></td><td><i>248</i></td>
334 <td>&ndash;</td>
335 <td><i>This enumeration value is only used internally and cannot
336 exist in a binlog.</i></td>
337 </tr>
338
339 <tr>
340 <td>MYSQL_TYPE_TINY_BLOB</td><td>249</td>
341 <td>&ndash;</td>
342 <td><i>This enumeration value is only used internally and cannot
343 exist in a binlog.</i></td>
344 </tr>
345
346 <tr>
347 <td><i>MYSQL_TYPE_MEDIUM_BLOB</i></td><td><i>250</i></td>
348 <td>&ndash;</td>
349 <td><i>This enumeration value is only used internally and cannot
350 exist in a binlog.</i></td>
351 </tr>
352
353 <tr>
354 <td><i>MYSQL_TYPE_LONG_BLOB</i></td><td><i>251</i></td>
355 <td>&ndash;</td>
356 <td><i>This enumeration value is only used internally and cannot
357 exist in a binlog.</i></td>
358 </tr>
359
360 <tr>
361 <td>MYSQL_TYPE_BLOB</td><td>252</td>
362 <td>1 byte</td>
363 <td>The pack length, i.e., the number of bytes needed to represent
364 the length of the blob: 1, 2, 3, or 4.</td>
365 </tr>
366
367 <tr>
368 <td>MYSQL_TYPE_VAR_STRING</td><td>253</td>
369 <td>2 bytes</td>
370 <td>This is used to store both strings and enumeration values.
371 The first byte is a enumeration value storing the <i>real
372 type</i>, which may be either MYSQL_TYPE_VAR_STRING or
373 MYSQL_TYPE_ENUM. The second byte is a 1 byte unsigned integer
374 representing the field size, i.e., the number of bytes needed to
375 store the length of the string.</td>
376 </tr>
377
378 <tr>
379 <td>MYSQL_TYPE_STRING</td><td>254</td>
380 <td>2 bytes</td>
381 <td>The first byte is always MYSQL_TYPE_VAR_STRING (i.e., 253).
382 The second byte is the field size, i.e., the number of bytes in
383 the representation of size of the string: 3 or 4.</td>
384 </tr>
385
386 <tr>
387 <td>MYSQL_TYPE_GEOMETRY</td><td>255</td>
388 <td>1 byte</td>
389 <td>The pack length, i.e., the number of bytes needed to represent
390 the length of the geometry: 1, 2, 3, or 4.</td>
391 </tr>
392
393 <tr>
394 <td>MYSQL_TYPE_TYPED_ARRAY</td><td>15</td>
395 <td>up to 4 bytes</td>
396 <td>
397 - The first byte holds the MySQL type for the elements.
398 - The following 0, 1, 2, or 3 bytes holds the metadata for the MySQL
399 type for the elements. The contents of these bytes depends on the
400 element type, as described in the other rows of this table.
401 </td>
402 </tr>
403
404 </table>
405
406 The table below lists all optional metadata types, along with the numerical
407 identifier for it and the size and interpretation of meta-data used
408 to describe the type.
409
410 @anchor Table_table_map_event_optional_metadata
411 <table>
412 <caption>Table_map_event optional metadata types: numerical identifier and
413 metadata. Optional metadata fields are stored in TLV fields.
414 Format of values are described in this table. </caption>
415 <tr>
416 <th>Type</th>
417 <th>Description</th>
418 <th>Format</th>
419 </tr>
420 <tr>
421 <td>SIGNEDNESS</td>
422 <td>signedness of numeric columns. This is included for all values of
423 binlog_row_metadata.</td>
424 <td>For each numeric column, a bit indicates whether the numeric
425 colunm has unsigned flag. 1 means it is unsigned. The number of
426 bytes needed for this is int((column_count + 7) / 8). The order is
427 the same as the order of column_type field.</td>
428 </tr>
429 <tr>
430 <td>DEFAULT_CHARSET</td>
431 <td>Charsets of character columns. It has a default charset for
432 the case that most of character columns have same charset and the
433 most used charset is binlogged as default charset.Collation
434 numbers are binlogged for identifying charsets. They are stored in
435 packed length format. Either DEFAULT_CHARSET or COLUMN_CHARSET is
436 included for all values of binlog_row_metadata.</td>
437 <td>Default charset's collation is logged first. The charsets which are not
438 same to default charset are logged following default charset. They are
439 logged as column index and charset collation number pair sequence. The
440 column index is counted only in all character columns. The order is same to
441 the order of column_type
442 field. </td>
443 </tr>
444 <tr>
445 <td>COLUMN_CHARSET</td>
446 <td>Charsets of character columns. For the case that most of columns have
447 different charsets, this field is logged. It is never logged with
448 DEFAULT_CHARSET together. Either DEFAULT_CHARSET or COLUMN_CHARSET is
449 included for all values of binlog_row_metadata.</td>
450 <td>It is a collation number sequence for all character columns.</td>
451 </tr>
452 <tr>
453 <td>COLUMN_NAME</td>
454 <td>Names of columns. This is only included if
455 binlog_row_metadata=FULL.</td>
456 <td>A sequence of column names. For each column name, 1 byte for
457 the string length in bytes is followed by a string without null
458 terminator.</td>
459 </tr>
460 <tr>
461 <td>SET_STR_VALUE</td>
462 <td>The string values of SET columns. This is only included if
463 binlog_row_metadata=FULL.</td>
464 <td>For each SET column, a pack_length representing the value
465 count is followed by a sequence of length and string pairs. length
466 is the byte count in pack_length format. The string has no null
467 terminator.</td>
468 </tr>
469 <tr>
470 <td>ENUM_STR_VALUE</td>
471 <td>The string values is ENUM columns. This is only included
472 if binlog_row_metadata=FULL.</td>
473 <td>The format is the same as SET_STR_VALUE.</td>
474 </tr>
475 <tr>
476 <td>GEOMETRY_TYPE</td>
477 <td>The real type of geometry columns. This is only included
478 if binlog_row_metadata=FULL.</td>
479 <td>A sequence of real type of geometry columns are stored in pack_length
480 format. </td>
481 </tr>
482 <tr>
483 <td>SIMPLE_PRIMARY_KEY</td>
484 <td>The primary key without any prefix. This is only included
485 if binlog_row_metadata=FULL and there is a primary key where every
486 key part covers an entire column.</td>
487 <td>A sequence of column indexes. The indexes are stored in pack_length
488 format.</td>
489 </tr>
490 <tr>
491 <td>PRIMARY_KEY_WITH_PREFIX</td>
492 <td>The primary key with some prefix. It doesn't appear together with
493 SIMPLE_PRIMARY_KEY. This is only included if
494 binlog_row_metadata=FULL and there is a primary key where some key
495 part covers a prefix of the column.</td>
496 <td>A sequence of column index and prefix length pairs. Both
497 column index and prefix length are in pack_length format. Prefix length
498 0 means that the whole column value is used.</td>
499 </tr>
500 <tr>
501 <td>ENUM_AND_SET_DEFAULT_CHARSET</td>
502 <td>Charsets of ENUM and SET columns. It has the same layout as
503 DEFAULT_CHARSET. If there are SET or ENUM columns and
504 binlog_row_metadata=FULL, exactly one of
505 ENUM_AND_SET_DEFAULT_CHARSET and ENUM_AND_SET_COLUMN_CHARSET
506 appears (the encoder chooses the representation that uses the
507 least amount of space). Otherwise, none of them appears.</td>
508 <td>The same format as for DEFAULT_CHARSET, except it counts ENUM
509 and SET columns rather than character columns.</td>
510 </tr>
511 <tr>
512 <td>ENUM_AND_SET_COLUMN_CHARSET</td>
513 <td>Charsets of ENUM and SET columns. It has the same layout as
514 COLUMN_CHARSET. If there are SET or ENUM columns and
515 binlog_row_metadata=FULL, exactly one of
516 ENUM_AND_SET_DEFAULT_CHARSET and ENUM_AND_SET_COLUMN_CHARSET
517 appears (the encoder chooses the representation that uses the
518 least amount of space). Otherwise, none of them appears.</td>
519 <td>The same format as for COLUMN_CHARSET, except it counts ENUM
520 and SET columns rather than character columns.</td>
521 </tr>
522 </table>
523*/
525 public:
526 /** Constants representing offsets */
528 /** TM = "Table Map" */
531 };
532
533 typedef uint16_t flag_set;
534
535 /**
536 DEFAULT_CHARSET and COLUMN_CHARSET don't appear together, and
537 ENUM_AND_SET_DEFAULT_CHARSET and ENUM_AND_SET_COLUMN_CHARSET don't
538 appear together. They are just alternative ways to pack character
539 set information. When binlogging, it logs character sets in the
540 way that occupies least storage.
541
542 SIMPLE_PRIMARY_KEY and PRIMARY_KEY_WITH_PREFIX don't appear together.
543 SIMPLE_PRIMARY_KEY is for the primary keys which only use whole values of
544 pk columns. PRIMARY_KEY_WITH_PREFIX is
545 for the primary keys which just use part value of pk columns.
546 */
548 SIGNEDNESS = 1, // UNSIGNED flag of numeric columns
549 DEFAULT_CHARSET, /* Character set of string columns, optimized to
550 minimize space when many columns have the
551 same charset. */
552 COLUMN_CHARSET, /* Character set of string columns, optimized to
553 minimize space when columns have many
554 different charsets. */
556 SET_STR_VALUE, // String value of SET columns
557 ENUM_STR_VALUE, // String value of ENUM columns
558 GEOMETRY_TYPE, // Real type of geometry columns
559 SIMPLE_PRIMARY_KEY, // Primary key without prefix
560 PRIMARY_KEY_WITH_PREFIX, // Primary key with prefix
561 ENUM_AND_SET_DEFAULT_CHARSET, /* Character set of enum and set
562 columns, optimized to minimize
563 space when many columns have the
564 same charset. */
565 ENUM_AND_SET_COLUMN_CHARSET, /* Character set of enum and set
566 columns, optimized to minimize
567 space when many columns have the
568 same charset. */
569 COLUMN_VISIBILITY /* Flag to indicate column visibility
570 attribute. */
571 };
572
573 /**
574 Metadata_fields organizes m_optional_metadata into a structured format which
575 is easy to access.
576 */
578 typedef std::pair<unsigned int, unsigned int> uint_pair;
579 typedef std::vector<std::string> str_vector;
580
583 bool empty() const { return default_charset == 0; }
584
585 // Default charset for the columns which are not in charset_pairs.
586 unsigned int default_charset;
587
588 /* The uint_pair means <column index, column charset number>. */
589 std::vector<uint_pair> charset_pairs;
590 };
591
592 // Contents of DEFAULT_CHARSET field are converted into Default_charset.
594 // Contents of ENUM_AND_SET_DEFAULT_CHARSET are converted into
595 // Default_charset.
597 std::vector<bool> m_signedness;
598 // Character set number of every string column
599 std::vector<unsigned int> m_column_charset;
600 // Character set number of every ENUM or SET column.
601 std::vector<unsigned int> m_enum_and_set_column_charset;
602 std::vector<std::string> m_column_name;
603 // each str_vector stores values of one enum/set column
604 std::vector<str_vector> m_enum_str_value;
605 std::vector<str_vector> m_set_str_value;
606 std::vector<unsigned int> m_geometry_type;
607 /*
608 The uint_pair means <column index, prefix length>. Prefix length is 0 if
609 whole column value is used.
610 */
611 std::vector<uint_pair> m_primary_key;
612 std::vector<bool> m_column_visibility;
613
614 /*
615 It parses m_optional_metadata and populates into above variables.
616
617 @param[in] optional_metadata points to the begin of optional metadata
618 fields in table_map_event.
619 @param[in] optional_metadata_len length of optional_metadata field.
620 */
621 Optional_metadata_fields(unsigned char *optional_metadata,
622 unsigned int optional_metadata_len);
623 // It is used to specify the validity of the deserialized structure
625 };
626
627 /**
628 <pre>
629 The buffer layout for fixed data part is as follows:
630 +-----------------------------------+
631 | table_id | Reserved for future use|
632 +-----------------------------------+
633 </pre>
634
635 <pre>
636 The buffer layout for variable data part is as follows:
637 +--------------------------------------------------------------------------+
638 | db len| db name | table len| table name | no of cols | array of col types|
639 +--------------------------------------------------------------------------+
640 +---------------------------------------------+
641 | metadata len | metadata block | m_null_bits |
642 +---------------------------------------------+
643 </pre>
644
645 @param buf Contains the serialized event.
646 @param fde An FDE event (see Rotate_event constructor for more info).
647 */
648 Table_map_event(const char *buf, const Format_description_event *fde);
649
650 Table_map_event(const Table_id &tid, unsigned long colcnt, const char *dbnam,
651 size_t dblen, const char *tblnam, size_t tbllen)
653 m_table_id(tid),
654 m_data_size(0),
655 m_dbnam(""),
656 m_dblen(dblen),
657 m_tblnam(""),
658 m_tbllen(tbllen),
659 m_colcnt(colcnt),
665 if (dbnam) m_dbnam = std::string(dbnam, m_dblen);
666 if (tblnam) m_tblnam = std::string(tblnam, m_tbllen);
667 }
668
669 ~Table_map_event() override;
670
671 /** Event post header contents */
674
675 size_t m_data_size; /** event data size */
676
677 /** Event body contents */
678 std::string m_dbnam;
679 unsigned long long int m_dblen;
680 std::string m_tblnam;
681 unsigned long long int m_tbllen;
682 unsigned long m_colcnt;
683 unsigned char *m_coltype;
684
685 /**
686 The size of field metadata buffer set by calling save_field_metadata()
687 */
689 unsigned char *m_field_metadata; /** field metadata */
690 unsigned char *m_null_bits;
692 unsigned char *m_optional_metadata;
693
702
703 unsigned long long get_table_id() { return m_table_id.id(); }
704 std::string get_table_name() { return m_tblnam; }
705 std::string get_db_name() { return m_dbnam; }
706
707#ifndef HAVE_MYSYS
708 void print_event_info(std::ostream &info) override;
709 void print_long_info(std::ostream &info) override;
710#endif
711};
712
713/**
714 @class Rows_event
715
716 Common base class for all row-containing binary log events.
717
718 RESPONSIBILITIES
719
720 - Provide an interface for adding an individual row to the event.
721
722 @section Rows_event_binary_format Binary Format
723
724 The Post-Header has the following components:
725
726 <table>
727 <caption>Post-Header for Rows_event</caption>
728
729 <tr>
730 <th>Name</th>
731 <th>Format</th>
732 <th>Description</th>
733 </tr>
734
735 <tr>
736 <td>table_id</td>
737 <td>6 bytes unsigned integer</td>
738 <td>The number that identifies the table</td>
739 </tr>
740
741 <tr>
742 <td>flags</td>
743 <td>2 byte bitfield</td>
744 <td>Reserved for future use; currently always 0.</td>
745 </tr>
746
747 </table>
748
749 The Body has the following components:
750
751 <table>
752 <caption>Body for Rows_event</caption>
753
754 <tr>
755 <th>Name</th>
756 <th>Format</th>
757 <th>Description</th>
758 </tr>
759
760
761 <tr>
762 <td>width</td>
763 <td>packed integer</td>
764 <td>Represents the number of columns in the table</td>
765 </tr>
766
767 <tr>
768 <td>cols</td>
769 <td>Bitfield, variable sized</td>
770 <td>Indicates whether each column is used, one bit per column.
771 For this field, the amount of storage required is
772 INT((width + 7) / 8) bytes. </td>
773 </tr>
774
775 <tr>
776 <td>extra_row_info</td>
777 <td>An object of class Extra_row_info</td>
778 <td>The class Extra_row_info will be storing the information related
779 to m_extra_row_ndb_info and partition info (partition_id and
780 source_partition_id). At any given time a Rows_event can have both, one
781 or none of ndb_info and partition_info present as part of Rows_event.
782 In case both ndb_info and partition_info are present then below will
783 be the order in which they will be stored.
784
785 @verbatim
786 +----------+--------------------------------------+
787 |type_code | extra_row_ndb_info |
788 +--- ------+--------------------------------------+
789 | NDB |Len of ndb_info |Format |ndb_data |
790 | 1 byte |1 byte |1 byte |len - 2 byte |
791 +----------+----------------+-------+-------------+
792
793 In case of INSERT/DELETE
794 +-----------+----------------+
795 | type_code | partition_info |
796 +-----------+----------------+
797 | PART | partition_id |
798 | (1 byte) | 2 byte |
799 +-----------+----------------+
800
801 In case of UPDATE
802 +-----------+------------------------------------+
803 | type_code | partition_info |
804 +-----------+--------------+---------------------+
805 | PART | partition_id | source_partition_id |
806 | (1 byte) | 2 byte | 2 byte |
807 +-----------+--------------+---------------------+
808
809 source_partition_id is used only in the case of Update_event
810 to log the partition_id of the source partition.
811
812 @endverbatim
813 This is the format for any information stored as extra_row_info.
814 type_code is not a part of the class Extra_row_info as it is a constant
815 values used at the time of serializing and decoding the event.
816 </td>
817 </tr>
818
819 <tr>
820 <td>columns_before_image</td>
821 <td>vector of elements of type uint8_t</td>
822 <td>For DELETE and UPDATE only.
823 Bit-field indicating whether each column is used
824 one bit per column. For this field, the amount of storage
825 required for N columns is INT((N + 7) / 8) bytes.</td>
826 </tr>
827
828 <tr>
829 <td>columns_after_image</td>
830 <td>vector of elements of type uint8_t</td>
831 <td>For WRITE and UPDATE only.
832 Bit-field indicating whether each column is used in the
833 UPDATE_ROWS_EVENT and WRITE_ROWS_EVENT after-image; one bit per column.
834 For this field, the amount of storage required for N columns
835 is INT((N + 7) / 8) bytes.
836
837 @verbatim
838 +-------------------------------------------------------+
839 | Event Type | Cols_before_image | Cols_after_image |
840 +-------------------------------------------------------+
841 | DELETE | Deleted row | NULL |
842 | INSERT | NULL | Inserted row |
843 | UPDATE | Old row | Updated row |
844 +-------------------------------------------------------+
845 @endverbatim
846 </td>
847 </tr>
848
849 <tr>
850 <td>row</td>
851 <td>vector of elements of type uint8_t</td>
852 <td> A sequence of zero or more rows. The end is determined by the size
853 of the event. Each row has the following format:
854 - A Bit-field indicating whether each field in the row is NULL.
855 Only columns that are "used" according to the second field in
856 the variable data part are listed here. If the second field in
857 the variable data part has N one-bits, the amount of storage
858 required for this field is INT((N + 7) / 8) bytes.
859 - The row-image, containing values of all table fields. This only
860 lists table fields that are used (according to the second field
861 of the variable data part) and non-NULL (according to the
862 previous field). In other words, the number of values listed here
863 is equal to the number of zero bits in the previous field.
864 (not counting padding bits in the last byte).
865 @verbatim
866 For example, if a INSERT statement inserts into 4 columns of a
867 table, N= 4 (in the formula above).
868 length of bitmask= (4 + 7) / 8 = 1
869 Number of fields in the row= 4.
870
871 +------------------------------------------------+
872 |Null_bit_mask(4)|field-1|field-2|field-3|field 4|
873 +------------------------------------------------+
874 @endverbatim
875 </td>
876 </tr>
877 </table>
878*/
880 public:
881 /**
882 These definitions allow to combine the flags into an
883 appropriate flag set using the normal bitwise operators. The
884 implicit conversion from an enum-constant to an integer is
885 accepted by the compiler, which is then used to set the real set
886 of flags.
887 */
889 /** Last event of a statement */
890 STMT_END_F = (1U << 0),
891 /** Value of the OPTION_NO_FOREIGN_KEY_CHECKS flag in thd->options */
893 /** Value of the OPTION_RELAXED_UNIQUE_CHECKS flag in thd->options */
895 /**
896 Indicates that rows in this event are complete, that is contain
897 values for all columns of the table.
898 */
899 COMPLETE_ROWS_F = (1U << 3)
900 };
901
902 /**
903 Constructs an event directly. The members are assigned default values.
904
905 @param type_arg Type of ROW_EVENT. Expected types are:
906 - WRITE_ROWS_EVENT, WRITE_ROWS_EVENT_V1
907 - UPDATE_ROWS_EVENT, UPDATE_ROWS_EVENT_V1,
908 PARTIAL_UPDATE_ROWS_EVENT
909 - DELETE_ROWS_EVENT, DELETE_ROWS_EVENT_V1
910 */
911 explicit Rows_event(Log_event_type type_arg)
912 : Binary_log_event(type_arg),
913 m_table_id(0),
914 m_width(0),
917 row(0) {}
918 /**
919 The constructor is responsible for decoding the event contained in
920 the buffer.
921
922 <pre>
923 The buffer layout for fixed data part is as follows
924 +------------------------------------+
925 | table_id | reserved for future use |
926 +------------------------------------+
927 </pre>
928
929 <pre>
930 The buffer layout for variable data part is as follows
931 +------------------------------------------------------------------+
932 | var_header_len | column_before_image | columns_after_image | row |
933 +------------------------------------------------------------------+
934 </pre>
935
936 @param buf Contains the serialized event.
937 @param fde An FDE event (see Rotate_event constructor for more info).
938 */
939 Rows_event(const char *buf, const Format_description_event *fde);
940
941 ~Rows_event() override;
942
943 protected:
944 Log_event_type m_type; /** Actual event type */
945
946 /** Post header content */
948 uint16_t m_flags; /** Flags for row-level events */
949
950 /* Body of the event */
951 unsigned long m_width; /** The width of the columns bitmap */
952 uint32_t n_bits_len; /** value determined by (m_width + 7) / 8 */
954
955 std::vector<uint8_t> columns_before_image;
956 std::vector<uint8_t> columns_after_image;
957 std::vector<uint8_t> row;
958
959 public:
961 private:
962 /** partition_id for a row in a partitioned table */
964 /**
965 It is the partition_id of the source partition in case
966 of Update_event, the target's partition_id is m_partition_id.
967 This variable is used only in case of Update_event.
968 */
970 /** The extra row info provided by NDB */
971 unsigned char *m_extra_row_ndb_info;
972
973 public:
978
980
981 int get_partition_id() const { return m_partition_id; }
982 void set_partition_id(int partition_id) {
983 BAPI_ASSERT(partition_id <= 65535);
984 m_partition_id = partition_id;
985 }
986
988 void set_source_partition_id(int source_partition_id) {
989 BAPI_ASSERT(source_partition_id <= 65535);
990 m_source_partition_id = source_partition_id;
991 }
992
993 unsigned char *get_ndb_info() const { return m_extra_row_ndb_info; }
994 void set_ndb_info(const unsigned char *ndb_info, size_t len) {
997 static_cast<unsigned char *>(bapi_malloc(len, 16 /* flags */));
998 std::copy(ndb_info, ndb_info + len, m_extra_row_ndb_info);
999 }
1000 /**
1001 Compares the extra_row_info in a Row event, it checks three things
1002 1. The m_extra_row_ndb_info pointers. It compares their significant bytes.
1003 2. Partition_id
1004 3. source_partition_id
1005
1006 @return
1007 true all the above variables are same in the event and the one passed
1008 in parameter.
1009 false Any of the above variable has a different value.
1010 */
1011 bool compare_extra_row_info(const unsigned char *ndb_info_arg,
1012 int part_id_arg, int source_part_id);
1013
1014 bool have_part() const { return m_partition_id != UNDEFINED; }
1015
1016 bool have_ndb_info() const { return m_extra_row_ndb_info != nullptr; }
1017 size_t get_ndb_length();
1018 size_t get_part_length();
1020
1021 static const int UNDEFINED{INT_MAX};
1022 };
1024
1025 unsigned long long get_table_id() const { return m_table_id.id(); }
1026
1027 enum_flag get_flags() const { return static_cast<enum_flag>(m_flags); }
1028
1029 uint32_t get_null_bits_len() const { return n_bits_len; }
1030
1031 unsigned long get_width() const { return m_width; }
1032
1033 static std::string get_flag_string(enum_flag flag) {
1034 std::string str = "";
1035 if (flag & STMT_END_F) str.append(" Last event of the statement");
1036 if (flag & NO_FOREIGN_KEY_CHECKS_F) str.append(" No foreign Key checks");
1037 if (flag & RELAXED_UNIQUE_CHECKS_F) str.append(" No unique key checks");
1038 if (flag & COMPLETE_ROWS_F) str.append(" Complete Rows");
1041 str.append("Unknown Flag");
1042 return str;
1043 }
1044#ifndef HAVE_MYSYS
1045 void print_event_info(std::ostream &info) override;
1046 void print_long_info(std::ostream &info) override;
1047#endif
1048
1049 template <class Iterator_value_type>
1051};
1052
1053/**
1054 @class Write_rows_event
1055
1056 Log row insertions. The event contain several insert/update rows
1057 for a table. Note that each event contains only rows for one table.
1058
1059 @section Write_rows_event_binary_format Binary Format
1060*/
1061class Write_rows_event : public virtual Rows_event {
1062 public:
1063 Write_rows_event(const char *buf, const Format_description_event *fde);
1065};
1066
1067/**
1068 @class Update_rows_event
1069
1070 Log row updates with a before image. The event contain several
1071 update rows for a table. Note that each event contains only rows for
1072 one table.
1073
1074 Also note that the row data consists of pairs of row data: one row
1075 for the old data and one row for the new data.
1076
1077 @section Update_rows_event_binary_format Binary Format
1078*/
1079class Update_rows_event : public virtual Rows_event {
1080 public:
1081 Update_rows_event(const char *buf, const Format_description_event *fde);
1082 Update_rows_event(Log_event_type event_type) : Rows_event(event_type) {}
1083};
1084
1085/**
1086 @class Delete_rows_event
1087
1088 Log row deletions. The event contain several delete rows for a
1089 table. Note that each event contains only rows for one table.
1090
1091 RESPONSIBILITIES
1092
1093 - Act as a container for rows that has been deleted on the master
1094 and should be deleted on the slave.
1095
1096 @section Delete_rows_event_binary_format Binary Format
1097*/
1098class Delete_rows_event : public virtual Rows_event {
1099 public:
1100 Delete_rows_event(const char *buf, const Format_description_event *fde);
1102};
1103
1104/**
1105 @class Rows_query_event
1106
1107 Rows query event type, which is a subclass
1108 of the Ignorable_event, to record the original query for the rows
1109 events in RBR. This event can be used to display the original query as
1110 comments by SHOW BINLOG EVENTS query, or mysqlbinlog client when the
1111 --verbose option is given twice
1112
1113 @section Rows_query_var_event_binary_format Binary Format
1114
1115 The Post-Header for this event type is empty. The Body has one
1116 component:
1117
1118 <table>
1119 <caption>Body for Rows_query_event</caption>
1120
1121 <tr>
1122 <th>Name</th>
1123 <th>Format</th>
1124 <th>Description</th>
1125 </tr>
1126
1127 <tr>
1128 <td>m_rows_query</td>
1129 <td>char array</td>
1130 <td>Records the original query executed in RBR </td>
1131 </tr>
1132 </table>
1133*/
1134class Rows_query_event : public virtual Ignorable_event {
1135 public:
1136 /**
1137 It is used to write the original query in the binlog file in case of RBR
1138 when the session flag binlog_rows_query_log_events is set.
1139
1140 <pre>
1141 The buffer layout is as follows:
1142 +------------------------------------+
1143 | The original query executed in RBR |
1144 +------------------------------------+
1145 </pre>
1146
1147 @param buf Contains the serialized event.
1148 @param fde An FDE event (see Rotate_event constructor for more info).
1149 */
1150
1151 Rows_query_event(const char *buf, const Format_description_event *fde);
1152 /**
1153 It is the minimal constructor, and all it will do is set the type_code as
1154 ROWS_QUERY_LOG_EVENT in the header object in Binary_log_event.
1155 */
1158
1159 ~Rows_query_event() override;
1160
1161 protected:
1163};
1164} // namespace binary_log
1165
1166/**
1167 @} (end of group Replication)
1168*/
1169#endif /* ROWS_EVENT_INCLUDED */
Each table share has a table id, it is mainly used for row based replication.
Definition: table_id.h:40
unsigned long long id() const
Definition: table_id.h:50
This is the abstract base class for binary log events.
Definition: binlog_event.h:826
Log row deletions.
Definition: rows_event.h:1098
Delete_rows_event()
Definition: rows_event.h:1101
For binlog version 4.
Definition: control_events.h:231
Base class for ignorable log events.
Definition: control_events.h:666
Definition: rows_event.h:960
size_t get_part_length()
Definition: rows_event.cpp:510
Extra_row_info(const Extra_row_info &)=delete
int m_source_partition_id
It is the partition_id of the source partition in case of Update_event, the target's partition_id is ...
Definition: rows_event.h:969
bool compare_extra_row_info(const unsigned char *ndb_info_arg, int part_id_arg, int source_part_id)
Compares the extra_row_info in a Row event, it checks three things.
Definition: rows_event.cpp:487
unsigned char * m_extra_row_ndb_info
The extra row info provided by NDB.
Definition: rows_event.h:971
int get_partition_id() const
Definition: rows_event.h:981
void set_ndb_info(const unsigned char *ndb_info, size_t len)
Definition: rows_event.h:994
bool have_part() const
Definition: rows_event.h:1014
int m_partition_id
partition_id for a row in a partitioned table
Definition: rows_event.h:963
int get_source_partition_id() const
Definition: rows_event.h:987
unsigned char * get_ndb_info() const
Definition: rows_event.h:993
void set_source_partition_id(int source_partition_id)
Definition: rows_event.h:988
~Extra_row_info()
Definition: rows_event.cpp:519
static const int UNDEFINED
Definition: rows_event.h:1021
bool have_ndb_info() const
Definition: rows_event.h:1016
size_t get_ndb_length()
Definition: rows_event.cpp:503
Extra_row_info()
Definition: rows_event.h:974
void set_partition_id(int partition_id)
Definition: rows_event.h:982
Common base class for all row-containing binary log events.
Definition: rows_event.h:879
uint16_t m_flags
Definition: rows_event.h:948
unsigned long long get_table_id() const
Definition: rows_event.h:1025
enum_flag get_flags() const
Definition: rows_event.h:1027
enum_flag
These definitions allow to combine the flags into an appropriate flag set using the normal bitwise op...
Definition: rows_event.h:888
@ STMT_END_F
Last event of a statement.
Definition: rows_event.h:890
@ NO_FOREIGN_KEY_CHECKS_F
Value of the OPTION_NO_FOREIGN_KEY_CHECKS flag in thd->options.
Definition: rows_event.h:892
@ COMPLETE_ROWS_F
Indicates that rows in this event are complete, that is contain values for all columns of the table.
Definition: rows_event.h:899
@ RELAXED_UNIQUE_CHECKS_F
Value of the OPTION_RELAXED_UNIQUE_CHECKS flag in thd->options.
Definition: rows_event.h:894
uint32_t n_bits_len
The width of the columns bitmap.
Definition: rows_event.h:952
std::vector< uint8_t > columns_after_image
Definition: rows_event.h:956
Log_event_type m_type
Definition: rows_event.h:944
static std::string get_flag_string(enum_flag flag)
Definition: rows_event.h:1033
unsigned long m_width
Flags for row-level events.
Definition: rows_event.h:951
unsigned long get_width() const
Definition: rows_event.h:1031
friend class Row_event_iterator
Definition: rows_event.h:1050
uint16_t var_header_len
value determined by (m_width + 7) / 8
Definition: rows_event.h:953
uint32_t get_null_bits_len() const
Definition: rows_event.h:1029
Rows_event(Log_event_type type_arg)
Constructs an event directly.
Definition: rows_event.h:911
std::vector< uint8_t > columns_before_image
Definition: rows_event.h:955
Table_id m_table_id
Actual event type.
Definition: rows_event.h:947
Extra_row_info m_extra_row_info
Definition: rows_event.h:1023
std::vector< uint8_t > row
Definition: rows_event.h:957
Rows query event type, which is a subclass of the Ignorable_event, to record the original query for t...
Definition: rows_event.h:1134
~Rows_query_event() override
Definition: rows_event.cpp:550
Rows_query_event()
It is the minimal constructor, and all it will do is set the type_code as ROWS_QUERY_LOG_EVENT in the...
Definition: rows_event.h:1156
char * m_rows_query
Definition: rows_event.h:1162
In row-based mode, every row operation event is preceded by a Table_map_event which maps a table defi...
Definition: rows_event.h:524
unsigned char * m_optional_metadata
Definition: rows_event.h:692
Optional_metadata_field_type
DEFAULT_CHARSET and COLUMN_CHARSET don't appear together, and ENUM_AND_SET_DEFAULT_CHARSET and ENUM_A...
Definition: rows_event.h:547
@ ENUM_AND_SET_COLUMN_CHARSET
Definition: rows_event.h:565
@ SIGNEDNESS
Definition: rows_event.h:548
@ DEFAULT_CHARSET
Definition: rows_event.h:549
@ SET_STR_VALUE
Definition: rows_event.h:556
@ COLUMN_NAME
Definition: rows_event.h:555
@ SIMPLE_PRIMARY_KEY
Definition: rows_event.h:559
@ ENUM_AND_SET_DEFAULT_CHARSET
Definition: rows_event.h:561
@ GEOMETRY_TYPE
Definition: rows_event.h:558
@ ENUM_STR_VALUE
Definition: rows_event.h:557
@ COLUMN_CHARSET
Definition: rows_event.h:552
@ COLUMN_VISIBILITY
Definition: rows_event.h:569
@ PRIMARY_KEY_WITH_PREFIX
Definition: rows_event.h:560
unsigned long m_field_metadata_size
The size of field metadata buffer set by calling save_field_metadata()
Definition: rows_event.h:688
~Table_map_event() override
Definition: rows_event.cpp:106
std::string m_dbnam
event data size
Definition: rows_event.h:678
uint16_t flag_set
Definition: rows_event.h:533
unsigned char * m_null_bits
field metadata
Definition: rows_event.h:690
unsigned char * m_field_metadata
Definition: rows_event.h:689
unsigned long long int m_tbllen
Definition: rows_event.h:681
unsigned long m_colcnt
Definition: rows_event.h:682
Table_map_event()
Definition: rows_event.h:694
unsigned int m_optional_metadata_len
Definition: rows_event.h:691
Table_map_event_offset
Constants representing offsets.
Definition: rows_event.h:527
@ TM_MAPID_OFFSET
TM = "Table Map".
Definition: rows_event.h:529
@ TM_FLAGS_OFFSET
Definition: rows_event.h:530
unsigned char * m_coltype
Definition: rows_event.h:683
flag_set m_flags
Definition: rows_event.h:673
std::string m_tblnam
Definition: rows_event.h:680
std::string get_table_name()
Definition: rows_event.h:704
size_t m_data_size
Definition: rows_event.h:675
std::string get_db_name()
Definition: rows_event.h:705
Table_map_event(const Table_id &tid, unsigned long colcnt, const char *dbnam, size_t dblen, const char *tblnam, size_t tbllen)
Definition: rows_event.h:650
unsigned long long int m_dblen
Definition: rows_event.h:679
Table_id m_table_id
Event post header contents.
Definition: rows_event.h:672
unsigned long long get_table_id()
Definition: rows_event.h:703
Log row updates with a before image.
Definition: rows_event.h:1079
Update_rows_event(Log_event_type event_type)
Definition: rows_event.h:1082
Update_rows_event(const char *buf, const Format_description_event *fde)
Definition: rows_event.cpp:563
Log row insertions.
Definition: rows_event.h:1061
Write_rows_event()
Definition: rows_event.h:1064
Contains the classes representing events operating in the replication stream properties.
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
enum_extra_row_info_typecode
This is the typecode defined for the different elements present in the container Extra_row_info,...
Definition: rows_event.h:63
static int flag
Definition: hp_test1.cc:40
Log info(cout, "NOTE")
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
The namespace contains classes representing events that can occur in a replication stream.
Log_event_type
Enumeration type for the different types of log events.
Definition: binlog_event.h:275
@ TABLE_MAP_EVENT
Definition: binlog_event.h:306
@ DELETE_ROWS_EVENT
Definition: binlog_event.h:338
@ ROWS_QUERY_LOG_EVENT
Definition: binlog_event.h:333
@ WRITE_ROWS_EVENT
Version 2 of the Row events.
Definition: binlog_event.h:336
Definition: buf0block_hint.cc:30
std::vector< uint_pair > charset_pairs
Definition: rows_event.h:589
Metadata_fields organizes m_optional_metadata into a structured format which is easy to access.
Definition: rows_event.h:577
Default_charset m_default_charset
Definition: rows_event.h:593
std::vector< std::string > str_vector
Definition: rows_event.h:579
std::vector< str_vector > m_enum_str_value
Definition: rows_event.h:604
std::vector< unsigned int > m_column_charset
Definition: rows_event.h:599
Optional_metadata_fields(unsigned char *optional_metadata, unsigned int optional_metadata_len)
Definition: rows_event.cpp:321
std::vector< unsigned int > m_geometry_type
Definition: rows_event.h:606
std::pair< unsigned int, unsigned int > uint_pair
Definition: rows_event.h:578
std::vector< bool > m_column_visibility
Definition: rows_event.h:612
std::vector< uint_pair > m_primary_key
Definition: rows_event.h:611
std::vector< str_vector > m_set_str_value
Definition: rows_event.h:605
std::vector< bool > m_signedness
Definition: rows_event.h:597
std::vector< std::string > m_column_name
Definition: rows_event.h:602
Default_charset m_enum_and_set_default_charset
Definition: rows_event.h:596
std::vector< unsigned int > m_enum_and_set_column_charset
Definition: rows_event.h:601
Contains the class Table_id, mainly used for row based replication.
#define BAPI_ASSERT(x)
Definition: wrapper_functions.h:62
void * bapi_malloc(size_t size, int flags)
This is a wrapper function in order to allocate memory from the heap in the binlogevent library.
Definition: wrapper_functions.h:192