Debian Codegraph @

libbinlogevents/include/rows_event.h

raw
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 @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 */
63 enum class enum_extra_row_info_typecode { NDB = 0, PART = 1 };
64
65 namespace 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 */
524 class Table_map_event : public Binary_log_event {
525 public:
526 /** Constants representing offsets */
527 enum Table_map_event_offset {
528 /** TM = "Table Map" */
529 TM_MAPID_OFFSET = 0,
530 TM_FLAGS_OFFSET = 6
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 */
547 enum Optional_metadata_field_type {
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. */
555 COLUMN_NAME,
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 */
577 struct Optional_metadata_fields {
578 typedef std::pair<unsigned int, unsigned int> uint_pair;
579 typedef std::vector<std::string> str_vector;
580
581 struct Default_charset {
582 Default_charset() : default_charset(0) {}
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.
593 Default_charset m_default_charset;
594 // Contents of ENUM_AND_SET_DEFAULT_CHARSET are converted into
595 // Default_charset.
596 Default_charset m_enum_and_set_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
624 bool is_valid;
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)
652 : Binary_log_event(TABLE_MAP_EVENT),
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),
660 m_field_metadata_size(0),
661 m_field_metadata(nullptr),
662 m_null_bits(nullptr),
663 m_optional_metadata_len(0),
664 m_optional_metadata(nullptr) {
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 */
672 Table_id m_table_id;
673 flag_set m_flags;
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 */
688 unsigned long m_field_metadata_size;
689 unsigned char *m_field_metadata; /** field metadata */
690 unsigned char *m_null_bits;
691 unsigned int m_optional_metadata_len;
692 unsigned char *m_optional_metadata;
693
694 Table_map_event()
695 : Binary_log_event(TABLE_MAP_EVENT),
696 m_coltype(nullptr),
697 m_field_metadata_size(0),
698 m_field_metadata(nullptr),
699 m_null_bits(nullptr),
700 m_optional_metadata_len(0),
701 m_optional_metadata(nullptr) {}
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 */
879 class Rows_event : public Binary_log_event {
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 */
888 enum enum_flag {
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 */
892 NO_FOREIGN_KEY_CHECKS_F = (1U << 1),
893 /** Value of the OPTION_RELAXED_UNIQUE_CHECKS flag in thd->options */
894 RELAXED_UNIQUE_CHECKS_F = (1U << 2),
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),
915 columns_before_image(0),
916 columns_after_image(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 */
947 Table_id m_table_id;
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 */
953 uint16_t var_header_len;
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:
960 class Extra_row_info {
961 private:
962 /** partition_id for a row in a partitioned table */
963 int m_partition_id;
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 */
969 int m_source_partition_id;
970 /** The extra row info provided by NDB */
971 unsigned char *m_extra_row_ndb_info;
972
973 public:
974 Extra_row_info()
975 : m_partition_id(UNDEFINED),
976 m_source_partition_id(UNDEFINED),
977 m_extra_row_ndb_info(nullptr) {}
978
979 Extra_row_info(const Extra_row_info &) = delete;
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
987 int get_source_partition_id() const { return m_source_partition_id; }
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) {
995 BAPI_ASSERT(!have_ndb_info());
996 m_extra_row_ndb_info =
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();
1019 ~Extra_row_info();
1020
1021 static const int UNDEFINED{INT_MAX};
1022 };
1023 Extra_row_info m_extra_row_info;
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");
1039 if (flag & ~(STMT_END_F | NO_FOREIGN_KEY_CHECKS_F |
1040 RELAXED_UNIQUE_CHECKS_F | COMPLETE_ROWS_F))
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>
1050 friend class Row_event_iterator;
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 */
1061 class Write_rows_event : public virtual Rows_event {
1062 public:
1063 Write_rows_event(const char *buf, const Format_description_event *fde);
1064 Write_rows_event() : Rows_event(WRITE_ROWS_EVENT) {}
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 */
1079 class 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 */
1098 class Delete_rows_event : public virtual Rows_event {
1099 public:
1100 Delete_rows_event(const char *buf, const Format_description_event *fde);
1101 Delete_rows_event() : Rows_event(DELETE_ROWS_EVENT) {}
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 */
1134 class 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 */
1156 Rows_query_event()
1157 : Ignorable_event(ROWS_QUERY_LOG_EVENT), m_rows_query(nullptr) {}
1158
1159 ~Rows_query_event() override;
1160
1161 protected:
1162 char *m_rows_query;
1163 };
1164 } // namespace binary_log
1165
1166 /**
1167 @} (end of group Replication)
1168 */
1169 #endif /* ROWS_EVENT_INCLUDED */