1 /* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
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 as published by
5    the Free Software Foundation; version 2 of the License.
6 
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11 
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
15 
16 /**
17   @addtogroup Replication
18   @{
19 
20   @file
21 
22   @brief Binary log event definitions.  This includes generic code
23   common to all types of log events, as well as specific code for each
24   type of log event.
25 */
26 
27 
28 #ifndef _log_event_h
29 #define _log_event_h
30 
31 #if defined(USE_PRAGMA_INTERFACE) && defined(MYSQL_SERVER)
32 #pragma interface			/* gcc class implementation */
33 #endif
34 
35 #include <my_bitmap.h>
36 #include "rpl_constants.h"
37 
38 #ifdef MYSQL_CLIENT
39 #include "sql_const.h"
40 #include "rpl_utility.h"
41 #include "hash.h"
42 #include "rpl_tblmap.h"
43 #endif
44 
45 #ifdef MYSQL_SERVER
46 #include "rpl_record.h"
47 #include "rpl_reporting.h"
48 #include "sql_class.h"                          /* THD */
49 #endif
50 
51 /* Forward declarations */
52 class String;
53 
54 #define PREFIX_SQL_LOAD "SQL_LOAD-"
55 #define LONG_FIND_ROW_THRESHOLD 60 /* seconds */
56 
57 /**
58    Either assert or return an error.
59 
60    In debug build, the condition will be checked, but in non-debug
61    builds, the error code given will be returned instead.
62 
63    @param COND   Condition to check
64    @param ERRNO  Error number to return in non-debug builds
65 */
66 #ifdef DBUG_OFF
67 #define ASSERT_OR_RETURN_ERROR(COND, ERRNO) \
68   do { if (!(COND)) return ERRNO; } while (0)
69 #else
70 #define ASSERT_OR_RETURN_ERROR(COND, ERRNO) \
71   DBUG_ASSERT(COND)
72 #endif
73 
74 #define LOG_READ_EOF    -1
75 #define LOG_READ_BOGUS  -2
76 #define LOG_READ_IO     -3
77 #define LOG_READ_MEM    -5
78 #define LOG_READ_TRUNC  -6
79 #define LOG_READ_TOO_LARGE -7
80 
81 #define LOG_EVENT_OFFSET 4
82 
83 /*
84    3 is MySQL 4.x; 4 is MySQL 5.0.0.
85    Compared to version 3, version 4 has:
86    - a different Start_log_event, which includes info about the binary log
87    (sizes of headers); this info is included for better compatibility if the
88    master's MySQL version is different from the slave's.
89    - all events have a unique ID (the triplet (server_id, timestamp at server
90    start, other) to be sure an event is not executed more than once in a
91    multimaster setup, example:
92                 M1
93               /   \
94              v     v
95              M2    M3
96              \     /
97               v   v
98                 S
99    if a query is run on M1, it will arrive twice on S, so we need that S
100    remembers the last unique ID it has processed, to compare and know if the
101    event should be skipped or not. Example of ID: we already have the server id
102    (4 bytes), plus:
103    timestamp_when_the_master_started (4 bytes), a counter (a sequence number
104    which increments every time we write an event to the binlog) (3 bytes).
105    Q: how do we handle when the counter is overflowed and restarts from 0 ?
106 
107    - Query and Load (Create or Execute) events may have a more precise
108      timestamp (with microseconds), number of matched/affected/warnings rows
109    and fields of session variables: SQL_MODE,
110    FOREIGN_KEY_CHECKS, UNIQUE_CHECKS, SQL_AUTO_IS_NULL, the collations and
111    charsets, the PASSWORD() version (old/new/...).
112 */
113 #define BINLOG_VERSION    4
114 
115 /*
116  We could have used SERVER_VERSION_LENGTH, but this introduces an
117  obscure dependency - if somebody decided to change SERVER_VERSION_LENGTH
118  this would break the replication protocol
119 */
120 #define ST_SERVER_VER_LEN 50
121 
122 /*
123   These are flags and structs to handle all the LOAD DATA INFILE options (LINES
124   TERMINATED etc).
125 */
126 
127 /*
128   These are flags and structs to handle all the LOAD DATA INFILE options (LINES
129   TERMINATED etc).
130   DUMPFILE_FLAG is probably useless (DUMPFILE is a clause of SELECT, not of LOAD
131   DATA).
132 */
133 #define DUMPFILE_FLAG		0x1
134 #define OPT_ENCLOSED_FLAG	0x2
135 #define REPLACE_FLAG		0x4
136 #define IGNORE_FLAG		0x8
137 
138 #define FIELD_TERM_EMPTY	0x1
139 #define ENCLOSED_EMPTY		0x2
140 #define LINE_TERM_EMPTY		0x4
141 #define LINE_START_EMPTY	0x8
142 #define ESCAPED_EMPTY		0x10
143 
144 /*****************************************************************************
145 
146   old_sql_ex struct
147 
148  ****************************************************************************/
149 struct old_sql_ex
150 {
151   char field_term;
152   char enclosed;
153   char line_term;
154   char line_start;
155   char escaped;
156   char opt_flags;
157   char empty_flags;
158 };
159 
160 #define NUM_LOAD_DELIM_STRS 5
161 
162 /*****************************************************************************
163 
164   sql_ex_info struct
165 
166  ****************************************************************************/
167 struct sql_ex_info
168 {
sql_ex_infosql_ex_info169   sql_ex_info() {}                            /* Remove gcc warning */
170   const char* field_term;
171   const char* enclosed;
172   const char* line_term;
173   const char* line_start;
174   const char* escaped;
175   int cached_new_format;
176   uint8 field_term_len,enclosed_len,line_term_len,line_start_len, escaped_len;
177   char opt_flags;
178   char empty_flags;
179 
180   // store in new format even if old is possible
force_new_formatsql_ex_info181   void force_new_format() { cached_new_format = 1;}
data_sizesql_ex_info182   int data_size()
183   {
184     return (new_format() ?
185 	    field_term_len + enclosed_len + line_term_len +
186 	    line_start_len + escaped_len + 6 : 7);
187   }
188   bool write_data(IO_CACHE* file);
189   const char* init(const char* buf, const char* buf_end, bool use_new_format);
new_formatsql_ex_info190   bool new_format()
191   {
192     return ((cached_new_format != -1) ? cached_new_format :
193 	    (cached_new_format=(field_term_len > 1 ||
194 				enclosed_len > 1 ||
195 				line_term_len > 1 || line_start_len > 1 ||
196 				escaped_len > 1)));
197   }
198 };
199 
200 /*****************************************************************************
201 
202   MySQL Binary Log
203 
204   This log consists of events.  Each event has a fixed-length header,
205   possibly followed by a variable length data body.
206 
207   The data body consists of an optional fixed length segment (post-header)
208   and  an optional variable length segment.
209 
210   See the #defines below for the format specifics.
211 
212   The events which really update data are Query_log_event,
213   Execute_load_query_log_event and old Load_log_event and
214   Execute_load_log_event events (Execute_load_query is used together with
215   Begin_load_query and Append_block events to replicate LOAD DATA INFILE.
216   Create_file/Append_block/Execute_load (which includes Load_log_event)
217   were used to replicate LOAD DATA before the 5.0.3).
218 
219  ****************************************************************************/
220 
221 #define LOG_EVENT_HEADER_LEN 19     /* the fixed header length */
222 #define OLD_HEADER_LEN       13     /* the fixed header length in 3.23 */
223 /*
224    Fixed header length, where 4.x and 5.0 agree. That is, 5.0 may have a longer
225    header (it will for sure when we have the unique event's ID), but at least
226    the first 19 bytes are the same in 4.x and 5.0. So when we have the unique
227    event's ID, LOG_EVENT_HEADER_LEN will be something like 26, but
228    LOG_EVENT_MINIMAL_HEADER_LEN will remain 19.
229 */
230 #define LOG_EVENT_MINIMAL_HEADER_LEN 19
231 
232 /* event-specific post-header sizes */
233 // where 3.23, 4.x and 5.0 agree
234 #define QUERY_HEADER_MINIMAL_LEN     (4 + 4 + 1 + 2)
235 // where 5.0 differs: 2 for len of N-bytes vars.
236 #define QUERY_HEADER_LEN     (QUERY_HEADER_MINIMAL_LEN + 2)
237 #define STOP_HEADER_LEN      0
238 #define LOAD_HEADER_LEN      (4 + 4 + 4 + 1 +1 + 4)
239 #define SLAVE_HEADER_LEN     0
240 #define START_V3_HEADER_LEN     (2 + ST_SERVER_VER_LEN + 4)
241 #define ROTATE_HEADER_LEN    8 // this is FROZEN (the Rotate post-header is frozen)
242 #define INTVAR_HEADER_LEN      0
243 #define CREATE_FILE_HEADER_LEN 4
244 #define APPEND_BLOCK_HEADER_LEN 4
245 #define EXEC_LOAD_HEADER_LEN   4
246 #define DELETE_FILE_HEADER_LEN 4
247 #define NEW_LOAD_HEADER_LEN    LOAD_HEADER_LEN
248 #define RAND_HEADER_LEN        0
249 #define USER_VAR_HEADER_LEN    0
250 #define FORMAT_DESCRIPTION_HEADER_LEN (START_V3_HEADER_LEN+1+LOG_EVENT_TYPES)
251 #define XID_HEADER_LEN         0
252 #define BEGIN_LOAD_QUERY_HEADER_LEN APPEND_BLOCK_HEADER_LEN
253 #define ROWS_HEADER_LEN        8
254 #define TABLE_MAP_HEADER_LEN   8
255 #define EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN (4 + 4 + 4 + 1)
256 #define EXECUTE_LOAD_QUERY_HEADER_LEN  (QUERY_HEADER_LEN + EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN)
257 #define INCIDENT_HEADER_LEN    2
258 #define HEARTBEAT_HEADER_LEN   0
259 /*
260   Max number of possible extra bytes in a replication event compared to a
261   packet (i.e. a query) sent from client to master;
262   First, an auxiliary log_event status vars estimation:
263 */
264 #define MAX_SIZE_LOG_EVENT_STATUS (1 + 4          /* type, flags2 */   + \
265                                    1 + 8          /* type, sql_mode */ + \
266                                    1 + 1 + 255    /* type, length, catalog */ + \
267                                    1 + 4          /* type, auto_increment */ + \
268                                    1 + 6          /* type, charset */ + \
269                                    1 + 1 + 255    /* type, length, time_zone */ + \
270                                    1 + 2          /* type, lc_time_names_number */ + \
271                                    1 + 2          /* type, charset_database_number */ + \
272                                    1 + 8          /* type, table_map_for_update */ + \
273                                    1 + 4          /* type, master_data_written */ + \
274                                    1 + 16 + 1 + 60/* type, user_len, user, host_len, host */)
275 #define MAX_LOG_EVENT_HEADER   ( /* in order of Query_log_event::write */ \
276   LOG_EVENT_HEADER_LEN + /* write_header */ \
277   QUERY_HEADER_LEN     + /* write_data */   \
278   EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN + /*write_post_header_for_derived */ \
279   MAX_SIZE_LOG_EVENT_STATUS + /* status */ \
280   NAME_LEN + 1)
281 
282 /*
283   The new option is added to handle large packets that are sent from the master
284   to the slave. It is used to increase the thd(max_allowed) for both the
285   DUMP thread on the master and the SQL/IO thread on the slave.
286 */
287 #define MAX_MAX_ALLOWED_PACKET 1024*1024*1024
288 
289 /*
290    Event header offsets;
291    these point to places inside the fixed header.
292 */
293 
294 #define EVENT_TYPE_OFFSET    4
295 #define SERVER_ID_OFFSET     5
296 #define EVENT_LEN_OFFSET     9
297 #define LOG_POS_OFFSET       13
298 #define FLAGS_OFFSET         17
299 
300 /* start event post-header (for v3 and v4) */
301 
302 #define ST_BINLOG_VER_OFFSET  0
303 #define ST_SERVER_VER_OFFSET  2
304 #define ST_CREATED_OFFSET     (ST_SERVER_VER_OFFSET + ST_SERVER_VER_LEN)
305 #define ST_COMMON_HEADER_LEN_OFFSET (ST_CREATED_OFFSET + 4)
306 
307 /* slave event post-header (this event is never written) */
308 
309 #define SL_MASTER_PORT_OFFSET   8
310 #define SL_MASTER_POS_OFFSET    0
311 #define SL_MASTER_HOST_OFFSET   10
312 
313 /* query event post-header */
314 
315 #define Q_THREAD_ID_OFFSET	0
316 #define Q_EXEC_TIME_OFFSET	4
317 #define Q_DB_LEN_OFFSET		8
318 #define Q_ERR_CODE_OFFSET	9
319 #define Q_STATUS_VARS_LEN_OFFSET 11
320 #define Q_DATA_OFFSET		QUERY_HEADER_LEN
321 /* these are codes, not offsets; not more than 256 values (1 byte). */
322 #define Q_FLAGS2_CODE           0
323 #define Q_SQL_MODE_CODE         1
324 /*
325   Q_CATALOG_CODE is catalog with end zero stored; it is used only by MySQL
326   5.0.x where 0<=x<=3. We have to keep it to be able to replicate these
327   old masters.
328 */
329 #define Q_CATALOG_CODE          2
330 #define Q_AUTO_INCREMENT	3
331 #define Q_CHARSET_CODE          4
332 #define Q_TIME_ZONE_CODE        5
333 /*
334   Q_CATALOG_NZ_CODE is catalog withOUT end zero stored; it is used by MySQL
335   5.0.x where x>=4. Saves one byte in every Query_log_event in binlog,
336   compared to Q_CATALOG_CODE. The reason we didn't simply re-use
337   Q_CATALOG_CODE is that then a 5.0.3 slave of this 5.0.x (x>=4) master would
338   crash (segfault etc) because it would expect a 0 when there is none.
339 */
340 #define Q_CATALOG_NZ_CODE       6
341 
342 #define Q_LC_TIME_NAMES_CODE    7
343 
344 #define Q_CHARSET_DATABASE_CODE 8
345 
346 #define Q_TABLE_MAP_FOR_UPDATE_CODE 9
347 
348 #define Q_MASTER_DATA_WRITTEN_CODE 10
349 
350 #define Q_INVOKER 11
351 
352 /* Intvar event post-header */
353 
354 /* Intvar event data */
355 #define I_TYPE_OFFSET        0
356 #define I_VAL_OFFSET         1
357 
358 /* Rand event data */
359 #define RAND_SEED1_OFFSET 0
360 #define RAND_SEED2_OFFSET 8
361 
362 /* User_var event data */
363 #define UV_VAL_LEN_SIZE        4
364 #define UV_VAL_IS_NULL         1
365 #define UV_VAL_TYPE_SIZE       1
366 #define UV_NAME_LEN_SIZE       4
367 #define UV_CHARSET_NUMBER_SIZE 4
368 
369 /* Load event post-header */
370 #define L_THREAD_ID_OFFSET   0
371 #define L_EXEC_TIME_OFFSET   4
372 #define L_SKIP_LINES_OFFSET  8
373 #define L_TBL_LEN_OFFSET     12
374 #define L_DB_LEN_OFFSET      13
375 #define L_NUM_FIELDS_OFFSET  14
376 #define L_SQL_EX_OFFSET      18
377 #define L_DATA_OFFSET        LOAD_HEADER_LEN
378 
379 /* Rotate event post-header */
380 #define R_POS_OFFSET       0
381 #define R_IDENT_OFFSET     8
382 
383 /* CF to DF handle LOAD DATA INFILE */
384 
385 /* CF = "Create File" */
386 #define CF_FILE_ID_OFFSET  0
387 #define CF_DATA_OFFSET     CREATE_FILE_HEADER_LEN
388 
389 /* AB = "Append Block" */
390 #define AB_FILE_ID_OFFSET  0
391 #define AB_DATA_OFFSET     APPEND_BLOCK_HEADER_LEN
392 
393 /* EL = "Execute Load" */
394 #define EL_FILE_ID_OFFSET  0
395 
396 /* DF = "Delete File" */
397 #define DF_FILE_ID_OFFSET  0
398 
399 /* TM = "Table Map" */
400 #define TM_MAPID_OFFSET    0
401 #define TM_FLAGS_OFFSET    6
402 
403 /* RW = "RoWs" */
404 #define RW_MAPID_OFFSET    0
405 #define RW_FLAGS_OFFSET    6
406 
407 /* ELQ = "Execute Load Query" */
408 #define ELQ_FILE_ID_OFFSET QUERY_HEADER_LEN
409 #define ELQ_FN_POS_START_OFFSET ELQ_FILE_ID_OFFSET + 4
410 #define ELQ_FN_POS_END_OFFSET ELQ_FILE_ID_OFFSET + 8
411 #define ELQ_DUP_HANDLING_OFFSET ELQ_FILE_ID_OFFSET + 12
412 
413 /* 4 bytes which all binlogs should begin with */
414 #define BINLOG_MAGIC        "\xfe\x62\x69\x6e"
415 
416 /*
417   The 2 flags below were useless :
418   - the first one was never set
419   - the second one was set in all Rotate events on the master, but not used for
420   anything useful.
421   So they are now removed and their place may later be reused for other
422   flags. Then one must remember that Rotate events in 4.x have
423   LOG_EVENT_FORCED_ROTATE_F set, so one should not rely on the value of the
424   replacing flag when reading a Rotate event.
425   I keep the defines here just to remember what they were.
426 */
427 #ifdef TO_BE_REMOVED
428 #define LOG_EVENT_TIME_F            0x1
429 #define LOG_EVENT_FORCED_ROTATE_F   0x2
430 #endif
431 
432 /*
433    This flag only makes sense for Format_description_log_event. It is set
434    when the event is written, and *reset* when a binlog file is
435    closed (yes, it's the only case when MySQL modifies already written
436    part of binlog).  Thus it is a reliable indicator that binlog was
437    closed correctly.  (Stop_log_event is not enough, there's always a
438    small chance that mysqld crashes in the middle of insert and end of
439    the binlog would look like a Stop_log_event).
440 
441    This flag is used to detect a restart after a crash, and to provide
442    "unbreakable" binlog. The problem is that on a crash storage engines
443    rollback automatically, while binlog does not.  To solve this we use this
444    flag and automatically append ROLLBACK to every non-closed binlog (append
445    virtually, on reading, file itself is not changed). If this flag is found,
446    mysqlbinlog simply prints "ROLLBACK" Replication master does not abort on
447    binlog corruption, but takes it as EOF, and replication slave forces a
448    rollback in this case.
449 
450    Note, that old binlogs does not have this flag set, so we get a
451    a backward-compatible behaviour.
452 */
453 
454 #define LOG_EVENT_BINLOG_IN_USE_F       0x1
455 
456 /**
457   @def LOG_EVENT_THREAD_SPECIFIC_F
458 
459   If the query depends on the thread (for example: TEMPORARY TABLE).
460   Currently this is used by mysqlbinlog to know it must print
461   SET @@PSEUDO_THREAD_ID=xx; before the query (it would not hurt to print it
462   for every query but this would be slow).
463 */
464 #define LOG_EVENT_THREAD_SPECIFIC_F 0x4
465 
466 /**
467   @def LOG_EVENT_SUPPRESS_USE_F
468 
469   Suppress the generation of 'USE' statements before the actual
470   statement. This flag should be set for any events that does not need
471   the current database set to function correctly. Most notable cases
472   are 'CREATE DATABASE' and 'DROP DATABASE'.
473 
474   This flags should only be used in exceptional circumstances, since
475   it introduce a significant change in behaviour regarding the
476   replication logic together with the flags --binlog-do-db and
477   --replicated-do-db.
478  */
479 #define LOG_EVENT_SUPPRESS_USE_F    0x8
480 
481 /*
482   Note: this is a place holder for the flag
483   LOG_EVENT_UPDATE_TABLE_MAP_VERSION_F (0x10), which is not used any
484   more, please do not reused this value for other flags.
485  */
486 
487 /**
488    @def LOG_EVENT_ARTIFICIAL_F
489 
490    Artificial events are created arbitarily and not written to binary
491    log
492 
493    These events should not update the master log position when slave
494    SQL thread executes them.
495 */
496 #define LOG_EVENT_ARTIFICIAL_F 0x20
497 
498 /**
499    @def LOG_EVENT_RELAY_LOG_F
500 
501    Events with this flag set are created by slave IO thread and written
502    to relay log
503 */
504 #define LOG_EVENT_RELAY_LOG_F 0x40
505 
506 /**
507   @def OPTIONS_WRITTEN_TO_BIN_LOG
508 
509   OPTIONS_WRITTEN_TO_BIN_LOG are the bits of thd->options which must
510   be written to the binlog. OPTIONS_WRITTEN_TO_BIN_LOG could be
511   written into the Format_description_log_event, so that if later we
512   don't want to replicate a variable we did replicate, or the
513   contrary, it's doable. But it should not be too hard to decide once
514   for all of what we replicate and what we don't, among the fixed 32
515   bits of thd->options.
516 
517   I (Guilhem) have read through every option's usage, and it looks
518   like OPTION_AUTO_IS_NULL and OPTION_NO_FOREIGN_KEYS are the only
519   ones which alter how the query modifies the table. It's good to
520   replicate OPTION_RELAXED_UNIQUE_CHECKS too because otherwise, the
521   slave may insert data slower than the master, in InnoDB.
522   OPTION_BIG_SELECTS is not needed (the slave thread runs with
523   max_join_size=HA_POS_ERROR) and OPTION_BIG_TABLES is not needed
524   either, as the manual says (because a too big in-memory temp table
525   is automatically written to disk).
526 */
527 #define OPTIONS_WRITTEN_TO_BIN_LOG \
528   (OPTION_AUTO_IS_NULL | OPTION_NO_FOREIGN_KEY_CHECKS |  \
529    OPTION_RELAXED_UNIQUE_CHECKS | OPTION_NOT_AUTOCOMMIT)
530 
531 /* Shouldn't be defined before */
532 #define EXPECTED_OPTIONS \
533   ((ULL(1) << 14) | (ULL(1) << 26) | (ULL(1) << 27) | (ULL(1) << 19))
534 
535 #if OPTIONS_WRITTEN_TO_BIN_LOG != EXPECTED_OPTIONS
536 #error OPTIONS_WRITTEN_TO_BIN_LOG must NOT change their values!
537 #endif
538 #undef EXPECTED_OPTIONS         /* You shouldn't use this one */
539 
540 /**
541   @enum Log_event_type
542 
543   Enumeration type for the different types of log events.
544 */
545 enum Log_event_type
546 {
547   /*
548     Every time you update this enum (when you add a type), you have to
549     fix Format_description_log_event::Format_description_log_event().
550   */
551   UNKNOWN_EVENT= 0,
552   START_EVENT_V3= 1,
553   QUERY_EVENT= 2,
554   STOP_EVENT= 3,
555   ROTATE_EVENT= 4,
556   INTVAR_EVENT= 5,
557   LOAD_EVENT= 6,
558   SLAVE_EVENT= 7,
559   CREATE_FILE_EVENT= 8,
560   APPEND_BLOCK_EVENT= 9,
561   EXEC_LOAD_EVENT= 10,
562   DELETE_FILE_EVENT= 11,
563   /*
564     NEW_LOAD_EVENT is like LOAD_EVENT except that it has a longer
565     sql_ex, allowing multibyte TERMINATED BY etc; both types share the
566     same class (Load_log_event)
567   */
568   NEW_LOAD_EVENT= 12,
569   RAND_EVENT= 13,
570   USER_VAR_EVENT= 14,
571   FORMAT_DESCRIPTION_EVENT= 15,
572   XID_EVENT= 16,
573   BEGIN_LOAD_QUERY_EVENT= 17,
574   EXECUTE_LOAD_QUERY_EVENT= 18,
575 
576   TABLE_MAP_EVENT = 19,
577 
578   /*
579     These event numbers were used for 5.1.0 to 5.1.15 and are
580     therefore obsolete.
581    */
582   PRE_GA_WRITE_ROWS_EVENT = 20,
583   PRE_GA_UPDATE_ROWS_EVENT = 21,
584   PRE_GA_DELETE_ROWS_EVENT = 22,
585 
586   /*
587     These event numbers are used from 5.1.16 and forward
588    */
589   WRITE_ROWS_EVENT = 23,
590   UPDATE_ROWS_EVENT = 24,
591   DELETE_ROWS_EVENT = 25,
592 
593   /*
594     Something out of the ordinary happened on the master
595    */
596   INCIDENT_EVENT= 26,
597 
598   /*
599     Heartbeat event to be send by master at its idle time
600     to ensure master's online status to slave
601   */
602   HEARTBEAT_LOG_EVENT= 27,
603 
604   /*
605     Add new events here - right above this comment!
606     Existing events (except ENUM_END_EVENT) should never change their numbers
607   */
608 
609   ENUM_END_EVENT /* end marker */
610 };
611 
612 /*
613    The number of types we handle in Format_description_log_event (UNKNOWN_EVENT
614    is not to be handled, it does not exist in binlogs, it does not have a
615    format).
616 */
617 #define LOG_EVENT_TYPES (ENUM_END_EVENT-1)
618 
619 enum Int_event_type
620 {
621   INVALID_INT_EVENT = 0, LAST_INSERT_ID_EVENT = 1, INSERT_ID_EVENT = 2
622 };
623 
624 
625 #ifdef MYSQL_SERVER
626 class String;
627 class MYSQL_BIN_LOG;
628 class THD;
629 #endif
630 
631 class Format_description_log_event;
632 class Relay_log_info;
633 
634 #ifdef MYSQL_CLIENT
635 enum enum_base64_output_mode {
636   BASE64_OUTPUT_NEVER= 0,
637   BASE64_OUTPUT_AUTO= 1,
638   BASE64_OUTPUT_ALWAYS= 2,
639   BASE64_OUTPUT_UNSPEC= 3,
640   BASE64_OUTPUT_DECODE_ROWS= 4,
641   /* insert new output modes here */
642   BASE64_OUTPUT_MODE_COUNT
643 };
644 
645 /*
646   A structure for mysqlbinlog to know how to print events
647 
648   This structure is passed to the event's print() methods,
649 
650   There are two types of settings stored here:
651   1. Last db, flags2, sql_mode etc comes from the last printed event.
652      They are stored so that only the necessary USE and SET commands
653      are printed.
654   2. Other information on how to print the events, e.g. short_form,
655      hexdump_from.  These are not dependent on the last event.
656 */
657 typedef struct st_print_event_info
658 {
659   /*
660     Settings for database, sql_mode etc that comes from the last event
661     that was printed.  We cache these so that we don't have to print
662     them if they are unchanged.
663   */
664   // TODO: have the last catalog here ??
665   char db[FN_REFLEN+1]; // TODO: make this a LEX_STRING when thd->db is
666   bool flags2_inited;
667   uint32 flags2;
668   bool sql_mode_inited;
669   ulong sql_mode;		/* must be same as THD.variables.sql_mode */
670   ulong auto_increment_increment, auto_increment_offset;
671   bool charset_inited;
672   char charset[6]; // 3 variables, each of them storable in 2 bytes
673   char time_zone_str[MAX_TIME_ZONE_NAME_LENGTH];
674   uint lc_time_names_number;
675   uint charset_database_number;
676   uint thread_id;
677   bool thread_id_printed;
678 
679   st_print_event_info();
680 
~st_print_event_infost_print_event_info681   ~st_print_event_info() {
682     close_cached_file(&head_cache);
683     close_cached_file(&body_cache);
684   }
init_okst_print_event_info685   bool init_ok() /* tells if construction was successful */
686     { return my_b_inited(&head_cache) && my_b_inited(&body_cache); }
687 
688 
689   /* Settings on how to print the events */
690   bool short_form;
691   enum_base64_output_mode base64_output_mode;
692   /*
693     This is set whenever a Format_description_event is printed.
694     Later, when an event is printed in base64, this flag is tested: if
695     no Format_description_event has been seen, it is unsafe to print
696     the base64 event, so an error message is generated.
697   */
698   bool printed_fd_event;
699   my_off_t hexdump_from;
700   uint8 common_header_len;
701   char delimiter[16];
702 
703   uint verbose;
704   table_mapping m_table_map;
705   table_mapping m_table_map_ignored;
706 
707   /*
708      These two caches are used by the row-based replication events to
709      collect the header information and the main body of the events
710      making up a statement.
711    */
712   IO_CACHE head_cache;
713   IO_CACHE body_cache;
714 } PRINT_EVENT_INFO;
715 #endif
716 
717 /**
718   the struct aggregates two paramenters that identify an event
719   uniquely in scope of communication of a particular master and slave couple.
720   I.e there can not be 2 events from the same staying connected master which
721   have the same coordinates.
722   @note
723   Such identifier is not yet unique generally as the event originating master
724   is resetable. Also the crashed master can be replaced with some other.
725 */
726 typedef struct event_coordinates
727 {
728   char * file_name; // binlog file name (directories stripped)
729   my_off_t  pos;       // event's position in the binlog file
730 } LOG_POS_COORD;
731 
732 /**
733   @class Log_event
734 
735   This is the abstract base class for binary log events.
736 
737   @section Log_event_binary_format Binary Format
738 
739   Any @c Log_event saved on disk consists of the following three
740   components.
741 
742   - Common-Header
743   - Post-Header
744   - Body
745 
746   The Common-Header, documented in the table @ref Table_common_header
747   "below", always has the same form and length within one version of
748   MySQL.  Each event type specifies a format and length of the
749   Post-Header.  The length of the Common-Header is the same for all
750   events of the same type.  The Body may be of different format and
751   length even for different events of the same type.  The binary
752   formats of Post-Header and Body are documented separately in each
753   subclass.  The binary format of Common-Header is as follows.
754 
755   <table>
756   <caption>Common-Header</caption>
757 
758   <tr>
759     <th>Name</th>
760     <th>Format</th>
761     <th>Description</th>
762   </tr>
763 
764   <tr>
765     <td>timestamp</td>
766     <td>4 byte unsigned integer</td>
767     <td>The time when the query started, in seconds since 1970.
768     </td>
769   </tr>
770 
771   <tr>
772     <td>type</td>
773     <td>1 byte enumeration</td>
774     <td>See enum #Log_event_type.</td>
775   </tr>
776 
777   <tr>
778     <td>server_id</td>
779     <td>4 byte unsigned integer</td>
780     <td>Server ID of the server that created the event.</td>
781   </tr>
782 
783   <tr>
784     <td>total_size</td>
785     <td>4 byte unsigned integer</td>
786     <td>The total size of this event, in bytes.  In other words, this
787     is the sum of the sizes of Common-Header, Post-Header, and Body.
788     </td>
789   </tr>
790 
791   <tr>
792     <td>master_position</td>
793     <td>4 byte unsigned integer</td>
794     <td>The position of the next event in the master binary log, in
795     bytes from the beginning of the file.  In a binlog that is not a
796     relay log, this is just the position of the next event, in bytes
797     from the beginning of the file.  In a relay log, this is
798     the position of the next event in the master's binlog.
799     </td>
800   </tr>
801 
802   <tr>
803     <td>flags</td>
804     <td>2 byte bitfield</td>
805     <td>See Log_event::flags.</td>
806   </tr>
807   </table>
808 
809   Summing up the numbers above, we see that the total size of the
810   common header is 19 bytes.
811 
812   @subsection Log_event_format_of_atomic_primitives Format of Atomic Primitives
813 
814   - All numbers, whether they are 16-, 24-, 32-, or 64-bit numbers,
815   are stored in little endian, i.e., the least significant byte first,
816   unless otherwise specified.
817 
818   @anchor packed_integer
819   - Some events use a special format for efficient representation of
820   unsigned integers, called Packed Integer.  A Packed Integer has the
821   capacity of storing up to 8-byte integers, while small integers
822   still can use 1, 3, or 4 bytes.  The value of the first byte
823   determines how to read the number, according to the following table:
824 
825   <table>
826   <caption>Format of Packed Integer</caption>
827 
828   <tr>
829     <th>First byte</th>
830     <th>Format</th>
831   </tr>
832 
833   <tr>
834     <td>0-250</td>
835     <td>The first byte is the number (in the range 0-250), and no more
836     bytes are used.</td>
837   </tr>
838 
839   <tr>
840     <td>252</td>
841     <td>Two more bytes are used.  The number is in the range
842     251-0xffff.</td>
843   </tr>
844 
845   <tr>
846     <td>253</td>
847     <td>Three more bytes are used.  The number is in the range
848     0xffff-0xffffff.</td>
849   </tr>
850 
851   <tr>
852     <td>254</td>
853     <td>Eight more bytes are used.  The number is in the range
854     0xffffff-0xffffffffffffffff.</td>
855   </tr>
856 
857   </table>
858 
859   - Strings are stored in various formats.  The format of each string
860   is documented separately.
861 */
862 class Log_event
863 {
864 public:
865   /**
866      Enumeration of what kinds of skipping (and non-skipping) that can
867      occur when the slave executes an event.
868 
869      @see shall_skip
870      @see do_shall_skip
871    */
872   enum enum_skip_reason {
873     /**
874        Don't skip event.
875     */
876     EVENT_SKIP_NOT,
877 
878     /**
879        Skip event by ignoring it.
880 
881        This means that the slave skip counter will not be changed.
882     */
883     EVENT_SKIP_IGNORE,
884 
885     /**
886        Skip event and decrease skip counter.
887     */
888     EVENT_SKIP_COUNT
889   };
890 
891   enum enum_event_cache_type
892   {
893     EVENT_INVALID_CACHE,
894     /*
895       If possible the event should use a non-transactional cache before
896       being flushed to the binary log. This means that it must be flushed
897       right after its correspondent statement is completed.
898     */
899     EVENT_STMT_CACHE,
900     /*
901       The event should use a transactional cache before being flushed to
902       the binary log. This means that it must be flushed upon commit or
903       rollback.
904     */
905     EVENT_TRANSACTIONAL_CACHE,
906     /*
907       The event must be written directly to the binary log without going
908       through a cache.
909     */
910     EVENT_NO_CACHE,
911     /**
912        If there is a need for different types, introduce them before this.
913     */
914     EVENT_CACHE_COUNT
915   };
916 
917   /*
918     The following type definition is to be used whenever data is placed
919     and manipulated in a common buffer. Use this typedef for buffers
920     that contain data containing binary and character data.
921   */
922   typedef unsigned char Byte;
923 
924   /*
925     The offset in the log where this event originally appeared (it is
926     preserved in relay logs, making SHOW SLAVE STATUS able to print
927     coordinates of the event in the master's binlog). Note: when a
928     transaction is written by the master to its binlog (wrapped in
929     BEGIN/COMMIT) the log_pos of all the queries it contains is the
930     one of the BEGIN (this way, when one does SHOW SLAVE STATUS it
931     sees the offset of the BEGIN, which is logical as rollback may
932     occur), except the COMMIT query which has its real offset.
933   */
934   my_off_t log_pos;
935   /*
936      A temp buffer for read_log_event; it is later analysed according to the
937      event's type, and its content is distributed in the event-specific fields.
938   */
939   char *temp_buf;
940   /*
941     Timestamp on the master(for debugging and replication of
942     NOW()/TIMESTAMP).  It is important for queries and LOAD DATA
943     INFILE. This is set at the event's creation time, except for Query
944     and Load (et al.) events where this is set at the query's
945     execution time, which guarantees good replication (otherwise, we
946     could have a query and its event with different timestamps).
947   */
948   time_t when;
949   /* The number of seconds the query took to run on the master. */
950   ulong exec_time;
951   /* Number of bytes written by write() function */
952   ulong data_written;
953 
954   /*
955     The master's server id (is preserved in the relay log; used to
956     prevent from infinite loops in circular replication).
957   */
958   uint32 server_id;
959 
960   /**
961     Some 16 flags. See the definitions above for LOG_EVENT_TIME_F,
962     LOG_EVENT_FORCED_ROTATE_F, LOG_EVENT_THREAD_SPECIFIC_F, and
963     LOG_EVENT_SUPPRESS_USE_F for notes.
964   */
965   uint16 flags;
966 
967   /*
968     Defines the type of the cache, if any, where the event will be
969     stored before being flushed to disk.
970   */
971   uint16 cache_type;
972 
973   /**
974     A storage to cache the global system variable's value.
975     Handling of a separate event will be governed its member.
976   */
977   ulong slave_exec_mode;
978 
979 #ifdef MYSQL_SERVER
980   THD* thd;
981 
982   Log_event();
983   Log_event(THD* thd_arg, uint16 flags_arg, bool is_transactional);
984   /*
985     read_log_event() functions read an event from a binlog or relay
986     log; used by SHOW BINLOG EVENTS, the binlog_dump thread on the
987     master (reads master's binlog), the slave IO thread (reads the
988     event sent by binlog_dump), the slave SQL thread (reads the event
989     from the relay log).  If mutex is 0, the read will proceed without
990     mutex.  We need the description_event to be able to parse the
991     event (to know the post-header's size); in fact in read_log_event
992     we detect the event's type, then call the specific event's
993     constructor and pass description_event as an argument.
994   */
995   static Log_event* read_log_event(IO_CACHE* file,
996                                    mysql_mutex_t* log_lock,
997                                    const Format_description_log_event
998                                    *description_event);
999 
1000   /**
1001     Reads an event from a binlog or relay log. Used by the dump thread
1002     this method reads the event into a raw buffer without parsing it.
1003 
1004     @Note If mutex is 0, the read will proceed without mutex.
1005 
1006     @Note If a log name is given than the method will check if the
1007     given binlog is still active.
1008 
1009     @param[in]  file                log file to be read
1010     @param[out] packet              packet to hold the event
1011     @param[in]  lock                the lock to be used upon read
1012     @param[in]  log_file_name_arg   the log's file name
1013     @param[out] is_binlog_active    is the current log still active
1014 
1015     @retval 0                   success
1016     @retval LOG_READ_EOF        end of file, nothing was read
1017     @retval LOG_READ_BOGUS      malformed event
1018     @retval LOG_READ_IO         io error while reading
1019     @retval LOG_READ_MEM        packet memory allocation failed
1020     @retval LOG_READ_TRUNC      only a partial event could be read
1021     @retval LOG_READ_TOO_LARGE  event too large
1022    */
1023   static int read_log_event(IO_CACHE* file, String* packet,
1024                             mysql_mutex_t* log_lock,
1025                             const char *log_file_name_arg= NULL,
1026                             bool* is_binlog_active= NULL);
1027   /*
1028     init_show_field_list() prepares the column names and types for the
1029     output of SHOW BINLOG EVENTS; it is used only by SHOW BINLOG
1030     EVENTS.
1031   */
1032   static void init_show_field_list(List<Item>* field_list);
1033 #ifdef HAVE_REPLICATION
1034   int net_send(Protocol *protocol, const char* log_name, my_off_t pos);
1035 
1036   /*
1037     pack_info() is used by SHOW BINLOG EVENTS; as print() it prepares and sends
1038     a string to display to the user, so it resembles print().
1039   */
1040 
1041   virtual void pack_info(Protocol *protocol);
1042 
1043 #endif /* HAVE_REPLICATION */
get_db()1044   virtual const char* get_db()
1045   {
1046     return thd ? thd->db : 0;
1047   }
1048 #else
Log_event()1049   Log_event() : temp_buf(0), flags(0) {}
1050     /* avoid having to link mysqlbinlog against libpthread */
1051   static Log_event* read_log_event(IO_CACHE* file,
1052                                    const Format_description_log_event
1053                                    *description_event);
1054   /* print*() functions are used by mysqlbinlog */
1055   virtual void print(FILE* file, PRINT_EVENT_INFO* print_event_info) = 0;
1056   void print_timestamp(IO_CACHE* file, time_t *ts = 0);
1057   void print_header(IO_CACHE* file, PRINT_EVENT_INFO* print_event_info,
1058                     bool is_more);
1059   void print_base64(IO_CACHE* file, PRINT_EVENT_INFO* print_event_info,
1060                     bool is_more);
1061 #endif
1062 
new(size_t size)1063   static void *operator new(size_t size)
1064   {
1065     return (void*) my_malloc((uint)size, MYF(MY_WME|MY_FAE));
1066   }
1067 
delete(void * ptr,size_t)1068   static void operator delete(void *ptr, size_t)
1069   {
1070     my_free(ptr);
1071   }
1072 
1073   /* Placement version of the above operators */
new(size_t,void * ptr)1074   static void *operator new(size_t, void* ptr) { return ptr; }
delete(void *,void *)1075   static void operator delete(void*, void*) { }
1076 
1077 #ifdef MYSQL_SERVER
1078   bool write_header(IO_CACHE* file, ulong data_length);
write(IO_CACHE * file)1079   virtual bool write(IO_CACHE* file)
1080   {
1081     return (write_header(file, get_data_size()) ||
1082             write_data_header(file) ||
1083             write_data_body(file));
1084   }
write_data_header(IO_CACHE * file)1085   virtual bool write_data_header(IO_CACHE* file)
1086   { return 0; }
write_data_body(IO_CACHE * file)1087   virtual bool write_data_body(IO_CACHE* file __attribute__((unused)))
1088   { return 0; }
get_time()1089   inline time_t get_time()
1090   {
1091     THD *tmp_thd;
1092     if (when)
1093       return when;
1094     if (thd)
1095       return thd->start_time;
1096     if ((tmp_thd= current_thd))
1097       return tmp_thd->start_time;
1098     return my_time(0);
1099   }
1100 #endif
1101   virtual Log_event_type get_type_code() = 0;
1102   virtual bool is_valid() const = 0;
set_artificial_event()1103   void set_artificial_event() { flags |= LOG_EVENT_ARTIFICIAL_F; }
set_relay_log_event()1104   void set_relay_log_event() { flags |= LOG_EVENT_RELAY_LOG_F; }
is_artificial_event()1105   bool is_artificial_event() const { return flags & LOG_EVENT_ARTIFICIAL_F; }
is_relay_log_event()1106   bool is_relay_log_event() const { return flags & LOG_EVENT_RELAY_LOG_F; }
use_trans_cache()1107   inline bool use_trans_cache() const
1108   {
1109     return (cache_type == Log_event::EVENT_TRANSACTIONAL_CACHE);
1110   }
set_direct_logging()1111   inline void set_direct_logging()
1112   {
1113     cache_type = Log_event::EVENT_NO_CACHE;
1114   }
use_direct_logging()1115   inline bool use_direct_logging()
1116   {
1117     return (cache_type == Log_event::EVENT_NO_CACHE);
1118   }
1119   Log_event(const char* buf, const Format_description_log_event
1120             *description_event);
~Log_event()1121   virtual ~Log_event() { free_temp_buf();}
register_temp_buf(char * buf)1122   void register_temp_buf(char* buf) { temp_buf = buf; }
free_temp_buf()1123   void free_temp_buf()
1124   {
1125     if (temp_buf)
1126     {
1127       my_free(temp_buf);
1128       temp_buf = 0;
1129     }
1130   }
1131   /*
1132     Get event length for simple events. For complicated events the length
1133     is calculated during write()
1134   */
get_data_size()1135   virtual int get_data_size() { return 0;}
1136   static Log_event* read_log_event(const char* buf, uint event_len,
1137 				   const char **error,
1138                                    const Format_description_log_event
1139                                    *description_event);
1140   /**
1141     Returns the human readable name of the given event type.
1142   */
1143   static const char* get_type_str(Log_event_type type);
1144   /**
1145     Returns the human readable name of this event's type.
1146   */
1147   const char* get_type_str();
1148 
1149   /* Return start of query time or current time */
1150 
1151 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
1152 public:
1153 
1154   /**
1155      Apply the event to the database.
1156 
1157      This function represents the public interface for applying an
1158      event.
1159 
1160      @see do_apply_event
1161    */
apply_event(Relay_log_info const * rli)1162   int apply_event(Relay_log_info const *rli)
1163   {
1164     return do_apply_event(rli);
1165   }
1166 
1167 
1168   /**
1169      Update the relay log position.
1170 
1171      This function represents the public interface for "stepping over"
1172      the event and will update the relay log information.
1173 
1174      @see do_update_pos
1175    */
update_pos(Relay_log_info * rli)1176   int update_pos(Relay_log_info *rli)
1177   {
1178     return do_update_pos(rli);
1179   }
1180 
1181   /**
1182      Decide if the event shall be skipped, and the reason for skipping
1183      it.
1184 
1185      @see do_shall_skip
1186    */
shall_skip(Relay_log_info * rli)1187   enum_skip_reason shall_skip(Relay_log_info *rli)
1188   {
1189     return do_shall_skip(rli);
1190   }
1191 
1192 protected:
1193 
1194   /**
1195      Helper function to ignore an event w.r.t. the slave skip counter.
1196 
1197      This function can be used inside do_shall_skip() for functions
1198      that cannot end a group. If the slave skip counter is 1 when
1199      seeing such an event, the event shall be ignored, the counter
1200      left intact, and processing continue with the next event.
1201 
1202      A typical usage is:
1203      @code
1204      enum_skip_reason do_shall_skip(Relay_log_info *rli) {
1205        return continue_group(rli);
1206      }
1207      @endcode
1208 
1209      @return Skip reason
1210    */
1211   enum_skip_reason continue_group(Relay_log_info *rli);
1212 
1213   /**
1214     Primitive to apply an event to the database.
1215 
1216     This is where the change to the database is made.
1217 
1218     @note The primitive is protected instead of private, since there
1219     is a hierarchy of actions to be performed in some cases.
1220 
1221     @see Format_description_log_event::do_apply_event()
1222 
1223     @param rli Pointer to relay log info structure
1224 
1225     @retval 0     Event applied successfully
1226     @retval errno Error code if event application failed
1227   */
do_apply_event(Relay_log_info const * rli)1228   virtual int do_apply_event(Relay_log_info const *rli)
1229   {
1230     return 0;                /* Default implementation does nothing */
1231   }
1232 
1233 
1234   /**
1235      Advance relay log coordinates.
1236 
1237      This function is called to advance the relay log coordinates to
1238      just after the event.  It is essential that both the relay log
1239      coordinate and the group log position is updated correctly, since
1240      this function is used also for skipping events.
1241 
1242      Normally, each implementation of do_update_pos() shall:
1243 
1244      - Update the event position to refer to the position just after
1245        the event.
1246 
1247      - Update the group log position to refer to the position just
1248        after the event <em>if the event is last in a group</em>
1249 
1250      @param rli Pointer to relay log info structure
1251 
1252      @retval 0     Coordinates changed successfully
1253      @retval errno Error code if advancing failed (usually just
1254                    1). Observe that handler errors are returned by the
1255                    do_apply_event() function, and not by this one.
1256    */
1257   virtual int do_update_pos(Relay_log_info *rli);
1258 
1259 
1260   /**
1261      Decide if this event shall be skipped or not and the reason for
1262      skipping it.
1263 
1264      The default implementation decide that the event shall be skipped
1265      if either:
1266 
1267      - the server id of the event is the same as the server id of the
1268        server and <code>rli->replicate_same_server_id</code> is true,
1269        or
1270 
1271      - if <code>rli->slave_skip_counter</code> is greater than zero.
1272 
1273      @see do_apply_event
1274      @see do_update_pos
1275 
1276      @retval Log_event::EVENT_SKIP_NOT
1277      The event shall not be skipped and should be applied.
1278 
1279      @retval Log_event::EVENT_SKIP_IGNORE
1280      The event shall be skipped by just ignoring it, i.e., the slave
1281      skip counter shall not be changed. This happends if, for example,
1282      the originating server id of the event is the same as the server
1283      id of the slave.
1284 
1285      @retval Log_event::EVENT_SKIP_COUNT
1286      The event shall be skipped because the slave skip counter was
1287      non-zero. The caller shall decrease the counter by one.
1288    */
1289   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli);
1290 #endif
1291 };
1292 
1293 
1294 /*
1295    One class for each type of event.
1296    Two constructors for each class:
1297    - one to create the event for logging (when the server acts as a master),
1298    called after an update to the database is done,
1299    which accepts parameters like the query, the database, the options for LOAD
1300    DATA INFILE...
1301    - one to create the event from a packet (when the server acts as a slave),
1302    called before reproducing the update, which accepts parameters (like a
1303    buffer). Used to read from the master, from the relay log, and in
1304    mysqlbinlog. This constructor must be format-tolerant.
1305 */
1306 
1307 /**
1308   @class Query_log_event
1309 
1310   A @c Query_log_event is created for each query that modifies the
1311   database, unless the query is logged row-based.
1312 
1313   @section Query_log_event_binary_format Binary format
1314 
1315   See @ref Log_event_binary_format "Binary format for log events" for
1316   a general discussion and introduction to the binary format of binlog
1317   events.
1318 
1319   The Post-Header has five components:
1320 
1321   <table>
1322   <caption>Post-Header for Query_log_event</caption>
1323 
1324   <tr>
1325     <th>Name</th>
1326     <th>Format</th>
1327     <th>Description</th>
1328   </tr>
1329 
1330   <tr>
1331     <td>slave_proxy_id</td>
1332     <td>4 byte unsigned integer</td>
1333     <td>An integer identifying the client thread that issued the
1334     query.  The id is unique per server.  (Note, however, that two
1335     threads on different servers may have the same slave_proxy_id.)
1336     This is used when a client thread creates a temporary table local
1337     to the client.  The slave_proxy_id is used to distinguish
1338     temporary tables that belong to different clients.
1339     </td>
1340   </tr>
1341 
1342   <tr>
1343     <td>exec_time</td>
1344     <td>4 byte unsigned integer</td>
1345     <td>The time from when the query started to when it was logged in
1346     the binlog, in seconds.</td>
1347   </tr>
1348 
1349   <tr>
1350     <td>db_len</td>
1351     <td>1 byte integer</td>
1352     <td>The length of the name of the currently selected database.</td>
1353   </tr>
1354 
1355   <tr>
1356     <td>error_code</td>
1357     <td>2 byte unsigned integer</td>
1358     <td>Error code generated by the master.  If the master fails, the
1359     slave will fail with the same error code, except for the error
1360     codes ER_DB_CREATE_EXISTS == 1007 and ER_DB_DROP_EXISTS == 1008.
1361     </td>
1362   </tr>
1363 
1364   <tr>
1365     <td>status_vars_len</td>
1366     <td>2 byte unsigned integer</td>
1367     <td>The length of the status_vars block of the Body, in bytes. See
1368     @ref query_log_event_status_vars "below".
1369     </td>
1370   </tr>
1371   </table>
1372 
1373   The Body has the following components:
1374 
1375   <table>
1376   <caption>Body for Query_log_event</caption>
1377 
1378   <tr>
1379     <th>Name</th>
1380     <th>Format</th>
1381     <th>Description</th>
1382   </tr>
1383 
1384   <tr>
1385     <td>@anchor query_log_event_status_vars status_vars</td>
1386     <td>status_vars_len bytes</td>
1387     <td>Zero or more status variables.  Each status variable consists
1388     of one byte identifying the variable stored, followed by the value
1389     of the variable.  The possible variables are listed separately in
1390     the table @ref Table_query_log_event_status_vars "below".  MySQL
1391     always writes events in the order defined below; however, it is
1392     capable of reading them in any order.  </td>
1393   </tr>
1394 
1395   <tr>
1396     <td>db</td>
1397     <td>db_len+1</td>
1398     <td>The currently selected database, as a null-terminated string.
1399 
1400     (The trailing zero is redundant since the length is already known;
1401     it is db_len from Post-Header.)
1402     </td>
1403   </tr>
1404 
1405   <tr>
1406     <td>query</td>
1407     <td>variable length string without trailing zero, extending to the
1408     end of the event (determined by the length field of the
1409     Common-Header)
1410     </td>
1411     <td>The SQL query.</td>
1412   </tr>
1413   </table>
1414 
1415   The following table lists the status variables that may appear in
1416   the status_vars field.
1417 
1418   @anchor Table_query_log_event_status_vars
1419   <table>
1420   <caption>Status variables for Query_log_event</caption>
1421 
1422   <tr>
1423     <th>Status variable</th>
1424     <th>1 byte identifier</th>
1425     <th>Format</th>
1426     <th>Description</th>
1427   </tr>
1428 
1429   <tr>
1430     <td>flags2</td>
1431     <td>Q_FLAGS2_CODE == 0</td>
1432     <td>4 byte bitfield</td>
1433     <td>The flags in @c thd->options, binary AND-ed with @c
1434     OPTIONS_WRITTEN_TO_BIN_LOG.  The @c thd->options bitfield contains
1435     options for "SELECT".  @c OPTIONS_WRITTEN identifies those options
1436     that need to be written to the binlog (not all do).  Specifically,
1437     @c OPTIONS_WRITTEN_TO_BIN_LOG equals (@c OPTION_AUTO_IS_NULL | @c
1438     OPTION_NO_FOREIGN_KEY_CHECKS | @c OPTION_RELAXED_UNIQUE_CHECKS |
1439     @c OPTION_NOT_AUTOCOMMIT), or 0x0c084000 in hex.
1440 
1441     These flags correspond to the SQL variables SQL_AUTO_IS_NULL,
1442     FOREIGN_KEY_CHECKS, UNIQUE_CHECKS, and AUTOCOMMIT, documented in
1443     the "SET Syntax" section of the MySQL Manual.
1444 
1445     This field is always written to the binlog in version >= 5.0, and
1446     never written in version < 5.0.
1447     </td>
1448   </tr>
1449 
1450   <tr>
1451     <td>sql_mode</td>
1452     <td>Q_SQL_MODE_CODE == 1</td>
1453     <td>8 byte bitfield</td>
1454     <td>The @c sql_mode variable.  See the section "SQL Modes" in the
1455     MySQL manual, and see sql_priv.h for a list of the possible
1456     flags. Currently (2007-10-04), the following flags are available:
1457     <pre>
1458     MODE_REAL_AS_FLOAT==0x1
1459     MODE_PIPES_AS_CONCAT==0x2
1460     MODE_ANSI_QUOTES==0x4
1461     MODE_IGNORE_SPACE==0x8
1462     MODE_NOT_USED==0x10
1463     MODE_ONLY_FULL_GROUP_BY==0x20
1464     MODE_NO_UNSIGNED_SUBTRACTION==0x40
1465     MODE_NO_DIR_IN_CREATE==0x80
1466     MODE_POSTGRESQL==0x100
1467     MODE_ORACLE==0x200
1468     MODE_MSSQL==0x400
1469     MODE_DB2==0x800
1470     MODE_MAXDB==0x1000
1471     MODE_NO_KEY_OPTIONS==0x2000
1472     MODE_NO_TABLE_OPTIONS==0x4000
1473     MODE_NO_FIELD_OPTIONS==0x8000
1474     MODE_MYSQL323==0x10000
1475     MODE_MYSQL323==0x20000
1476     MODE_MYSQL40==0x40000
1477     MODE_ANSI==0x80000
1478     MODE_NO_AUTO_VALUE_ON_ZERO==0x100000
1479     MODE_NO_BACKSLASH_ESCAPES==0x200000
1480     MODE_STRICT_TRANS_TABLES==0x400000
1481     MODE_STRICT_ALL_TABLES==0x800000
1482     MODE_NO_ZERO_IN_DATE==0x1000000
1483     MODE_NO_ZERO_DATE==0x2000000
1484     MODE_INVALID_DATES==0x4000000
1485     MODE_ERROR_FOR_DIVISION_BY_ZERO==0x8000000
1486     MODE_TRADITIONAL==0x10000000
1487     MODE_NO_AUTO_CREATE_USER==0x20000000
1488     MODE_HIGH_NOT_PRECEDENCE==0x40000000
1489     MODE_PAD_CHAR_TO_FULL_LENGTH==0x80000000
1490     </pre>
1491     All these flags are replicated from the server.  However, all
1492     flags except @c MODE_NO_DIR_IN_CREATE are honored by the slave;
1493     the slave always preserves its old value of @c
1494     MODE_NO_DIR_IN_CREATE.  For a rationale, see comment in
1495     @c Query_log_event::do_apply_event in @c log_event.cc.
1496 
1497     This field is always written to the binlog.
1498     </td>
1499   </tr>
1500 
1501   <tr>
1502     <td>catalog</td>
1503     <td>Q_CATALOG_NZ_CODE == 6</td>
1504     <td>Variable-length string: the length in bytes (1 byte) followed
1505     by the characters (at most 255 bytes)
1506     </td>
1507     <td>Stores the client's current catalog.  Every database belongs
1508     to a catalog, the same way that every table belongs to a
1509     database.  Currently, there is only one catalog, "std".
1510 
1511     This field is written if the length of the catalog is > 0;
1512     otherwise it is not written.
1513     </td>
1514   </tr>
1515 
1516   <tr>
1517     <td>auto_increment</td>
1518     <td>Q_AUTO_INCREMENT == 3</td>
1519     <td>two 2 byte unsigned integers, totally 2+2=4 bytes</td>
1520 
1521     <td>The two variables auto_increment_increment and
1522     auto_increment_offset, in that order.  For more information, see
1523     "System variables" in the MySQL manual.
1524 
1525     This field is written if auto_increment > 1.  Otherwise, it is not
1526     written.
1527     </td>
1528   </tr>
1529 
1530   <tr>
1531     <td>charset</td>
1532     <td>Q_CHARSET_CODE == 4</td>
1533     <td>three 2 byte unsigned integers, totally 2+2+2=6 bytes</td>
1534     <td>The three variables character_set_client,
1535     collation_connection, and collation_server, in that order.
1536     character_set_client is a code identifying the character set and
1537     collation used by the client to encode the query.
1538     collation_connection identifies the character set and collation
1539     that the master converts the query to when it receives it; this is
1540     useful when comparing literal strings.  collation_server is the
1541     default character set and collation used when a new database is
1542     created.
1543 
1544     See also "Connection Character Sets and Collations" in the MySQL
1545     5.1 manual.
1546 
1547     All three variables are codes identifying a (character set,
1548     collation) pair.  To see which codes map to which pairs, run the
1549     query "SELECT id, character_set_name, collation_name FROM
1550     COLLATIONS".
1551 
1552     Cf. Q_CHARSET_DATABASE_CODE below.
1553 
1554     This field is always written.
1555     </td>
1556   </tr>
1557 
1558   <tr>
1559     <td>time_zone</td>
1560     <td>Q_TIME_ZONE_CODE == 5</td>
1561     <td>Variable-length string: the length in bytes (1 byte) followed
1562     by the characters (at most 255 bytes).
1563     <td>The time_zone of the master.
1564 
1565     See also "System Variables" and "MySQL Server Time Zone Support"
1566     in the MySQL manual.
1567 
1568     This field is written if the length of the time zone string is >
1569     0; otherwise, it is not written.
1570     </td>
1571   </tr>
1572 
1573   <tr>
1574     <td>lc_time_names_number</td>
1575     <td>Q_LC_TIME_NAMES_CODE == 7</td>
1576     <td>2 byte integer</td>
1577     <td>A code identifying a table of month and day names.  The
1578     mapping from codes to languages is defined in @c sql_locale.cc.
1579 
1580     This field is written if it is not 0, i.e., if the locale is not
1581     en_US.
1582     </td>
1583   </tr>
1584 
1585   <tr>
1586     <td>charset_database_number</td>
1587     <td>Q_CHARSET_DATABASE_CODE == 8</td>
1588     <td>2 byte integer</td>
1589 
1590     <td>The value of the collation_database system variable (in the
1591     source code stored in @c thd->variables.collation_database), which
1592     holds the code for a (character set, collation) pair as described
1593     above (see Q_CHARSET_CODE).
1594 
1595     collation_database was used in old versions (???WHEN).  Its value
1596     was loaded when issuing a "use db" query and could be changed by
1597     issuing a "SET collation_database=xxx" query.  It used to affect
1598     the "LOAD DATA INFILE" and "CREATE TABLE" commands.
1599 
1600     In newer versions, "CREATE TABLE" has been changed to take the
1601     character set from the database of the created table, rather than
1602     the character set of the current database.  This makes a
1603     difference when creating a table in another database than the
1604     current one.  "LOAD DATA INFILE" has not yet changed to do this,
1605     but there are plans to eventually do it, and to make
1606     collation_database read-only.
1607 
1608     This field is written if it is not 0.
1609     </td>
1610   </tr>
1611   <tr>
1612     <td>table_map_for_update</td>
1613     <td>Q_TABLE_MAP_FOR_UPDATE_CODE == 9</td>
1614     <td>8 byte integer</td>
1615 
1616     <td>The value of the table map that is to be updated by the
1617     multi-table update query statement. Every bit of this variable
1618     represents a table, and is set to 1 if the corresponding table is
1619     to be updated by this statement.
1620 
1621     The value of this variable is set when executing a multi-table update
1622     statement and used by slave to apply filter rules without opening
1623     all the tables on slave. This is required because some tables may
1624     not exist on slave because of the filter rules.
1625     </td>
1626   </tr>
1627   </table>
1628 
1629   @subsection Query_log_event_notes_on_previous_versions Notes on Previous Versions
1630 
1631   * Status vars were introduced in version 5.0.  To read earlier
1632   versions correctly, check the length of the Post-Header.
1633 
1634   * The status variable Q_CATALOG_CODE == 2 existed in MySQL 5.0.x,
1635   where 0<=x<=3.  It was identical to Q_CATALOG_CODE, except that the
1636   string had a trailing '\0'.  The '\0' was removed in 5.0.4 since it
1637   was redundant (the string length is stored before the string).  The
1638   Q_CATALOG_CODE will never be written by a new master, but can still
1639   be understood by a new slave.
1640 
1641   * See Q_CHARSET_DATABASE_CODE in the table above.
1642 
1643   * When adding new status vars, please don't forget to update the
1644   MAX_SIZE_LOG_EVENT_STATUS, and update function code_name
1645 
1646 */
1647 class Query_log_event: public Log_event
1648 {
1649   LEX_STRING user;
1650   LEX_STRING host;
1651 protected:
1652   Log_event::Byte* data_buf;
1653 public:
1654   const char* query;
1655   const char* catalog;
1656   const char* db;
1657   /*
1658     If we already know the length of the query string
1659     we pass it with q_len, so we would not have to call strlen()
1660     otherwise, set it to 0, in which case, we compute it with strlen()
1661   */
1662   uint32 q_len;
1663   uint32 db_len;
1664   uint16 error_code;
1665   ulong thread_id;
1666   /*
1667     For events created by Query_log_event::do_apply_event (and
1668     Load_log_event::do_apply_event()) we need the *original* thread
1669     id, to be able to log the event with the original (=master's)
1670     thread id (fix for BUG#1686).
1671   */
1672   ulong slave_proxy_id;
1673 
1674   /*
1675     Binlog format 3 and 4 start to differ (as far as class members are
1676     concerned) from here.
1677   */
1678 
1679   uint catalog_len;			// <= 255 char; 0 means uninited
1680 
1681   /*
1682     We want to be able to store a variable number of N-bit status vars:
1683     (generally N=32; but N=64 for SQL_MODE) a user may want to log the number
1684     of affected rows (for debugging) while another does not want to lose 4
1685     bytes in this.
1686     The storage on disk is the following:
1687     status_vars_len is part of the post-header,
1688     status_vars are in the variable-length part, after the post-header, before
1689     the db & query.
1690     status_vars on disk is a sequence of pairs (code, value) where 'code' means
1691     'sql_mode', 'affected' etc. Sometimes 'value' must be a short string, so
1692     its first byte is its length. For now the order of status vars is:
1693     flags2 - sql_mode - catalog - autoinc - charset
1694     We should add the same thing to Load_log_event, but in fact
1695     LOAD DATA INFILE is going to be logged with a new type of event (logging of
1696     the plain text query), so Load_log_event would be frozen, so no need. The
1697     new way of logging LOAD DATA INFILE would use a derived class of
1698     Query_log_event, so automatically benefit from the work already done for
1699     status variables in Query_log_event.
1700  */
1701   uint16 status_vars_len;
1702 
1703   /*
1704     'flags2' is a second set of flags (on top of those in Log_event), for
1705     session variables. These are thd->options which is & against a mask
1706     (OPTIONS_WRITTEN_TO_BIN_LOG).
1707     flags2_inited helps make a difference between flags2==0 (3.23 or 4.x
1708     master, we don't know flags2, so use the slave server's global options) and
1709     flags2==0 (5.0 master, we know this has a meaning of flags all down which
1710     must influence the query).
1711   */
1712   bool flags2_inited;
1713   bool sql_mode_inited;
1714   bool charset_inited;
1715 
1716   uint32 flags2;
1717   /* In connections sql_mode is 32 bits now but will be 64 bits soon */
1718   ulong sql_mode;
1719   ulong auto_increment_increment, auto_increment_offset;
1720   char charset[6];
1721   uint time_zone_len; /* 0 means uninited */
1722   const char *time_zone_str;
1723   uint lc_time_names_number; /* 0 means en_US */
1724   uint charset_database_number;
1725   /*
1726     map for tables that will be updated for a multi-table update query
1727     statement, for other query statements, this will be zero.
1728   */
1729   ulonglong table_map_for_update;
1730   /*
1731     Holds the original length of a Query_log_event that comes from a
1732     master of version < 5.0 (i.e., binlog_version < 4). When the IO
1733     thread writes the relay log, it augments the Query_log_event with a
1734     Q_MASTER_DATA_WRITTEN_CODE status_var that holds the original event
1735     length. This field is initialized to non-zero in the SQL thread when
1736     it reads this augmented event. SQL thread does not write
1737     Q_MASTER_DATA_WRITTEN_CODE to the slave's server binlog.
1738   */
1739   uint32 master_data_written;
1740 
1741 #ifdef MYSQL_SERVER
1742 
1743   Query_log_event(THD* thd_arg, const char* query_arg, ulong query_length,
1744                   bool using_trans, bool direct, bool suppress_use, int error);
get_db()1745   const char* get_db() { return db; }
1746 #ifdef HAVE_REPLICATION
1747   void pack_info(Protocol* protocol);
1748 #endif /* HAVE_REPLICATION */
1749 #else
1750   void print_query_header(IO_CACHE* file, PRINT_EVENT_INFO* print_event_info);
1751   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
1752 #endif
1753 
1754   Query_log_event();
1755   Query_log_event(const char* buf, uint event_len,
1756                   const Format_description_log_event *description_event,
1757                   Log_event_type event_type);
~Query_log_event()1758   ~Query_log_event()
1759   {
1760     if (data_buf)
1761       my_free(data_buf);
1762   }
get_type_code()1763   Log_event_type get_type_code() { return QUERY_EVENT; }
1764 #ifdef MYSQL_SERVER
1765   bool write(IO_CACHE* file);
write_post_header_for_derived(IO_CACHE * file)1766   virtual bool write_post_header_for_derived(IO_CACHE* file) { return FALSE; }
1767 #endif
is_valid()1768   bool is_valid() const { return query != 0; }
1769 
1770   /*
1771     Returns number of bytes additionaly written to post header by derived
1772     events (so far it is only Execute_load_query event).
1773   */
get_post_header_size_for_derived()1774   virtual ulong get_post_header_size_for_derived() { return 0; }
1775   /* Writes derived event-specific part of post header. */
1776 
1777 public:        /* !!! Public in this patch to allow old usage */
1778 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
1779   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli);
1780   virtual int do_apply_event(Relay_log_info const *rli);
1781   virtual int do_update_pos(Relay_log_info *rli);
1782 
1783   int do_apply_event(Relay_log_info const *rli,
1784                        const char *query_arg,
1785                        uint32 q_len_arg);
1786 #endif /* HAVE_REPLICATION */
1787   /*
1788     If true, the event always be applied by slave SQL thread or be printed by
1789     mysqlbinlog
1790    */
is_trans_keyword()1791   bool is_trans_keyword()
1792   {
1793     /*
1794       Before the patch for bug#50407, The 'SAVEPOINT and ROLLBACK TO'
1795       queries input by user was written into log events directly.
1796       So the keywords can be written in both upper case and lower case
1797       together, strncasecmp is used to check both cases. they also could be
1798       binlogged with comments in the front of these keywords. for examples:
1799         / * bla bla * / SAVEPOINT a;
1800         / * bla bla * / ROLLBACK TO a;
1801       but we don't handle these cases and after the patch, both quiries are
1802       binlogged in upper case with no comments.
1803      */
1804     return !strncmp(query, "BEGIN", q_len) ||
1805       !strncmp(query, "COMMIT", q_len) ||
1806       !strncasecmp(query, "SAVEPOINT", 9) ||
1807       !strncasecmp(query, "ROLLBACK", 8);
1808   }
1809 };
1810 
1811 
1812 #ifdef HAVE_REPLICATION
1813 
1814 /**
1815   @class Slave_log_event
1816 
1817   Note that this class is currently not used at all; no code writes a
1818   @c Slave_log_event (though some code in @c repl_failsafe.cc reads @c
1819   Slave_log_event).  So it's not a problem if this code is not
1820   maintained.
1821 
1822   @section Slave_log_event_binary_format Binary Format
1823 
1824   This event type has no Post-Header. The Body has the following
1825   four components.
1826 
1827   <table>
1828   <caption>Body for Slave_log_event</caption>
1829 
1830   <tr>
1831     <th>Name</th>
1832     <th>Format</th>
1833     <th>Description</th>
1834   </tr>
1835 
1836   <tr>
1837     <td>master_pos</td>
1838     <td>8 byte integer</td>
1839     <td>???TODO
1840     </td>
1841   </tr>
1842 
1843   <tr>
1844     <td>master_port</td>
1845     <td>2 byte integer</td>
1846     <td>???TODO</td>
1847   </tr>
1848 
1849   <tr>
1850     <td>master_host</td>
1851     <td>null-terminated string</td>
1852     <td>???TODO</td>
1853   </tr>
1854 
1855   <tr>
1856     <td>master_log</td>
1857     <td>null-terminated string</td>
1858     <td>???TODO</td>
1859   </tr>
1860   </table>
1861 */
1862 class Slave_log_event: public Log_event
1863 {
1864 protected:
1865   char* mem_pool;
1866   void init_from_mem_pool(int data_size);
1867 public:
1868   my_off_t master_pos;
1869   char* master_host;
1870   char* master_log;
1871   int master_host_len;
1872   int master_log_len;
1873   uint16 master_port;
1874 
1875 #ifdef MYSQL_SERVER
1876   Slave_log_event(THD* thd_arg, Relay_log_info* rli);
1877   void pack_info(Protocol* protocol);
1878 #else
1879   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
1880 #endif
1881 
1882   Slave_log_event(const char* buf,
1883                   uint event_len,
1884                   const Format_description_log_event *description_event);
1885   ~Slave_log_event();
1886   int get_data_size();
is_valid()1887   bool is_valid() const { return master_host != 0; }
get_type_code()1888   Log_event_type get_type_code() { return SLAVE_EVENT; }
1889 #ifdef MYSQL_SERVER
1890   bool write(IO_CACHE* file);
1891 #endif
1892 
1893 private:
1894 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
1895   virtual int do_apply_event(Relay_log_info const* rli);
1896 #endif
1897 };
1898 
1899 #endif /* HAVE_REPLICATION */
1900 
1901 
1902 /**
1903   @class Load_log_event
1904 
1905   This log event corresponds to a "LOAD DATA INFILE" SQL query on the
1906   following form:
1907 
1908   @verbatim
1909    (1)    USE db;
1910    (2)    LOAD DATA [CONCURRENT] [LOCAL] INFILE 'file_name'
1911    (3)    [REPLACE | IGNORE]
1912    (4)    INTO TABLE 'table_name'
1913    (5)    [FIELDS
1914    (6)      [TERMINATED BY 'field_term']
1915    (7)      [[OPTIONALLY] ENCLOSED BY 'enclosed']
1916    (8)      [ESCAPED BY 'escaped']
1917    (9)    ]
1918   (10)    [LINES
1919   (11)      [TERMINATED BY 'line_term']
1920   (12)      [LINES STARTING BY 'line_start']
1921   (13)    ]
1922   (14)    [IGNORE skip_lines LINES]
1923   (15)    (field_1, field_2, ..., field_n)@endverbatim
1924 
1925   @section Load_log_event_binary_format Binary Format
1926 
1927   The Post-Header consists of the following six components.
1928 
1929   <table>
1930   <caption>Post-Header for Load_log_event</caption>
1931 
1932   <tr>
1933     <th>Name</th>
1934     <th>Format</th>
1935     <th>Description</th>
1936   </tr>
1937 
1938   <tr>
1939     <td>slave_proxy_id</td>
1940     <td>4 byte unsigned integer</td>
1941     <td>An integer identifying the client thread that issued the
1942     query.  The id is unique per server.  (Note, however, that two
1943     threads on different servers may have the same slave_proxy_id.)
1944     This is used when a client thread creates a temporary table local
1945     to the client.  The slave_proxy_id is used to distinguish
1946     temporary tables that belong to different clients.
1947     </td>
1948   </tr>
1949 
1950   <tr>
1951     <td>exec_time</td>
1952     <td>4 byte unsigned integer</td>
1953     <td>The time from when the query started to when it was logged in
1954     the binlog, in seconds.</td>
1955   </tr>
1956 
1957   <tr>
1958     <td>skip_lines</td>
1959     <td>4 byte unsigned integer</td>
1960     <td>The number on line (14) above, if present, or 0 if line (14)
1961     is left out.
1962     </td>
1963   </tr>
1964 
1965   <tr>
1966     <td>table_name_len</td>
1967     <td>1 byte unsigned integer</td>
1968     <td>The length of 'table_name' on line (4) above.</td>
1969   </tr>
1970 
1971   <tr>
1972     <td>db_len</td>
1973     <td>1 byte unsigned integer</td>
1974     <td>The length of 'db' on line (1) above.</td>
1975   </tr>
1976 
1977   <tr>
1978     <td>num_fields</td>
1979     <td>4 byte unsigned integer</td>
1980     <td>The number n of fields on line (15) above.</td>
1981   </tr>
1982   </table>
1983 
1984   The Body contains the following components.
1985 
1986   <table>
1987   <caption>Body of Load_log_event</caption>
1988 
1989   <tr>
1990     <th>Name</th>
1991     <th>Format</th>
1992     <th>Description</th>
1993   </tr>
1994 
1995   <tr>
1996     <td>sql_ex</td>
1997     <td>variable length</td>
1998 
1999     <td>Describes the part of the query on lines (3) and
2000     (5)&ndash;(13) above.  More precisely, it stores the five strings
2001     (on lines) field_term (6), enclosed (7), escaped (8), line_term
2002     (11), and line_start (12); as well as a bitfield indicating the
2003     presence of the keywords REPLACE (3), IGNORE (3), and OPTIONALLY
2004     (7).
2005 
2006     The data is stored in one of two formats, called "old" and "new".
2007     The type field of Common-Header determines which of these two
2008     formats is used: type LOAD_EVENT means that the old format is
2009     used, and type NEW_LOAD_EVENT means that the new format is used.
2010     When MySQL writes a Load_log_event, it uses the new format if at
2011     least one of the five strings is two or more bytes long.
2012     Otherwise (i.e., if all strings are 0 or 1 bytes long), the old
2013     format is used.
2014 
2015     The new and old format differ in the way the five strings are
2016     stored.
2017 
2018     <ul>
2019     <li> In the new format, the strings are stored in the order
2020     field_term, enclosed, escaped, line_term, line_start. Each string
2021     consists of a length (1 byte), followed by a sequence of
2022     characters (0-255 bytes).  Finally, a boolean combination of the
2023     following flags is stored in 1 byte: REPLACE_FLAG==0x4,
2024     IGNORE_FLAG==0x8, and OPT_ENCLOSED_FLAG==0x2.  If a flag is set,
2025     it indicates the presence of the corresponding keyword in the SQL
2026     query.
2027 
2028     <li> In the old format, we know that each string has length 0 or
2029     1.  Therefore, only the first byte of each string is stored.  The
2030     order of the strings is the same as in the new format.  These five
2031     bytes are followed by the same 1 byte bitfield as in the new
2032     format.  Finally, a 1 byte bitfield called empty_flags is stored.
2033     The low 5 bits of empty_flags indicate which of the five strings
2034     have length 0.  For each of the following flags that is set, the
2035     corresponding string has length 0; for the flags that are not set,
2036     the string has length 1: FIELD_TERM_EMPTY==0x1,
2037     ENCLOSED_EMPTY==0x2, LINE_TERM_EMPTY==0x4, LINE_START_EMPTY==0x8,
2038     ESCAPED_EMPTY==0x10.
2039     </ul>
2040 
2041     Thus, the size of the new format is 6 bytes + the sum of the sizes
2042     of the five strings.  The size of the old format is always 7
2043     bytes.
2044     </td>
2045   </tr>
2046 
2047   <tr>
2048     <td>field_lens</td>
2049     <td>num_fields 1 byte unsigned integers</td>
2050     <td>An array of num_fields integers representing the length of
2051     each field in the query.  (num_fields is from the Post-Header).
2052     </td>
2053   </tr>
2054 
2055   <tr>
2056     <td>fields</td>
2057     <td>num_fields null-terminated strings</td>
2058     <td>An array of num_fields null-terminated strings, each
2059     representing a field in the query.  (The trailing zero is
2060     redundant, since the length are stored in the num_fields array.)
2061     The total length of all strings equals to the sum of all
2062     field_lens, plus num_fields bytes for all the trailing zeros.
2063     </td>
2064   </tr>
2065 
2066   <tr>
2067     <td>table_name</td>
2068     <td>null-terminated string of length table_len+1 bytes</td>
2069     <td>The 'table_name' from the query, as a null-terminated string.
2070     (The trailing zero is actually redundant since the table_len is
2071     known from Post-Header.)
2072     </td>
2073   </tr>
2074 
2075   <tr>
2076     <td>db</td>
2077     <td>null-terminated string of length db_len+1 bytes</td>
2078     <td>The 'db' from the query, as a null-terminated string.
2079     (The trailing zero is actually redundant since the db_len is known
2080     from Post-Header.)
2081     </td>
2082   </tr>
2083 
2084   <tr>
2085     <td>file_name</td>
2086     <td>variable length string without trailing zero, extending to the
2087     end of the event (determined by the length field of the
2088     Common-Header)
2089     </td>
2090     <td>The 'file_name' from the query.
2091     </td>
2092   </tr>
2093 
2094   </table>
2095 
2096   @subsection Load_log_event_notes_on_previous_versions Notes on Previous Versions
2097 
2098   This event type is understood by current versions, but only
2099   generated by MySQL 3.23 and earlier.
2100 */
2101 class Load_log_event: public Log_event
2102 {
2103 private:
2104 protected:
2105   int copy_log_event(const char *buf, ulong event_len,
2106                      int body_offset,
2107                      const Format_description_log_event* description_event);
2108 
2109 public:
2110   uint get_query_buffer_length();
2111   void print_query(bool need_db, const char *cs, char *buf, char **end,
2112                    char **fn_start, char **fn_end);
2113   ulong thread_id;
2114   ulong slave_proxy_id;
2115   uint32 table_name_len;
2116   /*
2117     No need to have a catalog, as these events can only come from 4.x.
2118     TODO: this may become false if Dmitri pushes his new LOAD DATA INFILE in
2119     5.0 only (not in 4.x).
2120   */
2121   uint32 db_len;
2122   uint32 fname_len;
2123   uint32 num_fields;
2124   const char* fields;
2125   const uchar* field_lens;
2126   uint32 field_block_len;
2127 
2128   const char* table_name;
2129   const char* db;
2130   const char* fname;
2131   uint32 skip_lines;
2132   sql_ex_info sql_ex;
2133   bool local_fname;
2134   /**
2135     Indicates that this event corresponds to LOAD DATA CONCURRENT,
2136 
2137     @note Since Load_log_event event coming from the binary log
2138           lacks information whether LOAD DATA on master was concurrent
2139           or not, this flag is only set to TRUE for an auxiliary
2140           Load_log_event object which is used in mysql_load() to
2141           re-construct LOAD DATA statement from function parameters,
2142           for logging.
2143   */
2144   bool is_concurrent;
2145 
2146   /* fname doesn't point to memory inside Log_event::temp_buf  */
set_fname_outside_temp_buf(const char * afname,uint alen)2147   void set_fname_outside_temp_buf(const char *afname, uint alen)
2148   {
2149     fname= afname;
2150     fname_len= alen;
2151     local_fname= TRUE;
2152   }
2153   /* fname doesn't point to memory inside Log_event::temp_buf  */
check_fname_outside_temp_buf()2154   int  check_fname_outside_temp_buf()
2155   {
2156     return local_fname;
2157   }
2158 
2159 #ifdef MYSQL_SERVER
2160   String field_lens_buf;
2161   String fields_buf;
2162 
2163   Load_log_event(THD* thd, sql_exchange* ex, const char* db_arg,
2164 		 const char* table_name_arg,
2165 		 List<Item>& fields_arg,
2166                  bool is_concurrent_arg,
2167                  enum enum_duplicates handle_dup, bool ignore,
2168 		 bool using_trans);
2169   void set_fields(const char* db, List<Item> &fields_arg,
2170                   Name_resolution_context *context);
get_db()2171   const char* get_db() { return db; }
2172 #ifdef HAVE_REPLICATION
2173   void pack_info(Protocol* protocol);
2174 #endif /* HAVE_REPLICATION */
2175 #else
2176   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2177   void print(FILE* file, PRINT_EVENT_INFO* print_event_info, bool commented);
2178 #endif
2179 
2180   /*
2181     Note that for all the events related to LOAD DATA (Load_log_event,
2182     Create_file/Append/Exec/Delete, we pass description_event; however as
2183     logging of LOAD DATA is going to be changed in 4.1 or 5.0, this is only used
2184     for the common_header_len (post_header_len will not be changed).
2185   */
2186   Load_log_event(const char* buf, uint event_len,
2187                  const Format_description_log_event* description_event);
~Load_log_event()2188   ~Load_log_event()
2189   {}
get_type_code()2190   Log_event_type get_type_code()
2191   {
2192     return sql_ex.new_format() ? NEW_LOAD_EVENT: LOAD_EVENT;
2193   }
2194 #ifdef MYSQL_SERVER
2195   bool write_data_header(IO_CACHE* file);
2196   bool write_data_body(IO_CACHE* file);
2197 #endif
is_valid()2198   bool is_valid() const { return table_name != 0; }
get_data_size()2199   int get_data_size()
2200   {
2201     return (table_name_len + db_len + 2 + fname_len
2202 	    + LOAD_HEADER_LEN
2203 	    + sql_ex.data_size() + field_block_len + num_fields);
2204   }
2205 
2206 public:        /* !!! Public in this patch to allow old usage */
2207 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
do_apply_event(Relay_log_info const * rli)2208   virtual int do_apply_event(Relay_log_info const* rli)
2209   {
2210     return do_apply_event(thd->slave_net,rli,0);
2211   }
2212 
2213   int do_apply_event(NET *net, Relay_log_info const *rli,
2214                      bool use_rli_only_for_errors);
2215 #endif
2216 };
2217 
2218 extern char server_version[SERVER_VERSION_LENGTH];
2219 
2220 /**
2221   @class Start_log_event_v3
2222 
2223   Start_log_event_v3 is the Start_log_event of binlog format 3 (MySQL 3.23 and
2224   4.x).
2225 
2226   Format_description_log_event derives from Start_log_event_v3; it is
2227   the Start_log_event of binlog format 4 (MySQL 5.0), that is, the
2228   event that describes the other events' Common-Header/Post-Header
2229   lengths. This event is sent by MySQL 5.0 whenever it starts sending
2230   a new binlog if the requested position is >4 (otherwise if ==4 the
2231   event will be sent naturally).
2232 
2233   @section Start_log_event_v3_binary_format Binary Format
2234 */
2235 class Start_log_event_v3: public Log_event
2236 {
2237 public:
2238   /*
2239     If this event is at the start of the first binary log since server
2240     startup 'created' should be the timestamp when the event (and the
2241     binary log) was created.  In the other case (i.e. this event is at
2242     the start of a binary log created by FLUSH LOGS or automatic
2243     rotation), 'created' should be 0.  This "trick" is used by MySQL
2244     >=4.0.14 slaves to know whether they must drop stale temporary
2245     tables and whether they should abort unfinished transaction.
2246 
2247     Note that when 'created'!=0, it is always equal to the event's
2248     timestamp; indeed Start_log_event is written only in log.cc where
2249     the first constructor below is called, in which 'created' is set
2250     to 'when'.  So in fact 'created' is a useless variable. When it is
2251     0 we can read the actual value from timestamp ('when') and when it
2252     is non-zero we can read the same value from timestamp
2253     ('when'). Conclusion:
2254      - we use timestamp to print when the binlog was created.
2255      - we use 'created' only to know if this is a first binlog or not.
2256      In 3.23.57 we did not pay attention to this identity, so mysqlbinlog in
2257      3.23.57 does not print 'created the_date' if created was zero. This is now
2258      fixed.
2259   */
2260   time_t created;
2261   uint16 binlog_version;
2262   char server_version[ST_SERVER_VER_LEN];
2263   /*
2264     We set this to 1 if we don't want to have the created time in the log,
2265     which is the case when we rollover to a new log.
2266   */
2267   bool dont_set_created;
2268 
2269 #ifdef MYSQL_SERVER
2270   Start_log_event_v3();
2271 #ifdef HAVE_REPLICATION
2272   void pack_info(Protocol* protocol);
2273 #endif /* HAVE_REPLICATION */
2274 #else
Start_log_event_v3()2275   Start_log_event_v3() {}
2276   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2277 #endif
2278 
2279   Start_log_event_v3(const char* buf, uint event_len,
2280                      const Format_description_log_event* description_event);
~Start_log_event_v3()2281   ~Start_log_event_v3() {}
get_type_code()2282   Log_event_type get_type_code() { return START_EVENT_V3;}
2283 #ifdef MYSQL_SERVER
2284   bool write(IO_CACHE* file);
2285 #endif
is_valid()2286   bool is_valid() const { return server_version[0] != 0; }
get_data_size()2287   int get_data_size()
2288   {
2289     return START_V3_HEADER_LEN; //no variable-sized part
2290   }
2291 
2292 protected:
2293 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2294   virtual int do_apply_event(Relay_log_info const *rli);
do_shall_skip(Relay_log_info *)2295   virtual enum_skip_reason do_shall_skip(Relay_log_info*)
2296   {
2297     /*
2298       Events from ourself should be skipped, but they should not
2299       decrease the slave skip counter.
2300      */
2301     if (this->server_id == ::server_id)
2302       return Log_event::EVENT_SKIP_IGNORE;
2303     else
2304       return Log_event::EVENT_SKIP_NOT;
2305   }
2306 #endif
2307 };
2308 
2309 
2310 /**
2311   @class Format_description_log_event
2312 
2313   For binlog version 4.
2314   This event is saved by threads which read it, as they need it for future
2315   use (to decode the ordinary events).
2316 
2317   @section Format_description_log_event_binary_format Binary Format
2318 */
2319 
2320 class Format_description_log_event: public Start_log_event_v3
2321 {
2322 public:
2323   /*
2324      The size of the fixed header which _all_ events have
2325      (for binlogs written by this version, this is equal to
2326      LOG_EVENT_HEADER_LEN), except FORMAT_DESCRIPTION_EVENT and ROTATE_EVENT
2327      (those have a header of size LOG_EVENT_MINIMAL_HEADER_LEN).
2328   */
2329   uint8 common_header_len;
2330   uint8 number_of_event_types;
2331   /* The list of post-headers' lengthes */
2332   uint8 *post_header_len;
2333   uchar server_version_split[3];
2334   const uint8 *event_type_permutation;
2335 
2336   Format_description_log_event(uint8 binlog_ver, const char* server_ver=0);
2337   Format_description_log_event(const char* buf, uint event_len,
2338                                const Format_description_log_event
2339                                *description_event);
~Format_description_log_event()2340   ~Format_description_log_event()
2341   {
2342     my_free(post_header_len);
2343   }
get_type_code()2344   Log_event_type get_type_code() { return FORMAT_DESCRIPTION_EVENT;}
2345 #ifdef MYSQL_SERVER
2346   bool write(IO_CACHE* file);
2347 #endif
header_is_valid()2348   bool header_is_valid() const
2349   {
2350     return ((common_header_len >= ((binlog_version==1) ? OLD_HEADER_LEN :
2351                                    LOG_EVENT_MINIMAL_HEADER_LEN)) &&
2352             (post_header_len != NULL));
2353   }
2354 
version_is_valid()2355   bool version_is_valid() const
2356   {
2357     /* It is invalid only when all version numbers are 0 */
2358     return !(server_version_split[0] == 0 &&
2359              server_version_split[1] == 0 &&
2360              server_version_split[2] == 0);
2361   }
2362 
is_valid()2363   bool is_valid() const
2364   {
2365     return header_is_valid() && version_is_valid();
2366   }
2367 
get_data_size()2368   int get_data_size()
2369   {
2370     /*
2371       The vector of post-header lengths is considered as part of the
2372       post-header, because in a given version it never changes (contrary to the
2373       query in a Query_log_event).
2374     */
2375     return FORMAT_DESCRIPTION_HEADER_LEN;
2376   }
2377 
2378   void calc_server_version_split();
2379 
2380 protected:
2381 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2382   virtual int do_apply_event(Relay_log_info const *rli);
2383   virtual int do_update_pos(Relay_log_info *rli);
2384   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli);
2385 #endif
2386 };
2387 
2388 
2389 /**
2390   @class Intvar_log_event
2391 
2392   An Intvar_log_event will be created just before a Query_log_event,
2393   if the query uses one of the variables LAST_INSERT_ID or INSERT_ID.
2394   Each Intvar_log_event holds the value of one of these variables.
2395 
2396   @section Intvar_log_event_binary_format Binary Format
2397 
2398   The Post-Header for this event type is empty.  The Body has two
2399   components:
2400 
2401   <table>
2402   <caption>Body for Intvar_log_event</caption>
2403 
2404   <tr>
2405     <th>Name</th>
2406     <th>Format</th>
2407     <th>Description</th>
2408   </tr>
2409 
2410   <tr>
2411     <td>type</td>
2412     <td>1 byte enumeration</td>
2413     <td>One byte identifying the type of variable stored.  Currently,
2414     two identifiers are supported:  LAST_INSERT_ID_EVENT==1 and
2415     INSERT_ID_EVENT==2.
2416     </td>
2417   </tr>
2418 
2419   <tr>
2420     <td>value</td>
2421     <td>8 byte unsigned integer</td>
2422     <td>The value of the variable.</td>
2423   </tr>
2424 
2425   </table>
2426 */
2427 class Intvar_log_event: public Log_event
2428 {
2429 public:
2430   ulonglong val;
2431   uchar type;
2432 
2433 #ifdef MYSQL_SERVER
Intvar_log_event(THD * thd_arg,uchar type_arg,ulonglong val_arg)2434   Intvar_log_event(THD* thd_arg,uchar type_arg, ulonglong val_arg)
2435     :Log_event(thd_arg,0,0),val(val_arg),type(type_arg)
2436   {}
2437 #ifdef HAVE_REPLICATION
2438   void pack_info(Protocol* protocol);
2439 #endif /* HAVE_REPLICATION */
2440 #else
2441   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2442 #endif
2443 
2444   Intvar_log_event(const char* buf,
2445                    const Format_description_log_event *description_event);
~Intvar_log_event()2446   ~Intvar_log_event() {}
get_type_code()2447   Log_event_type get_type_code() { return INTVAR_EVENT;}
2448   const char* get_var_type_name();
get_data_size()2449   int get_data_size() { return  9; /* sizeof(type) + sizeof(val) */;}
2450 #ifdef MYSQL_SERVER
2451   bool write(IO_CACHE* file);
2452 #endif
is_valid()2453   bool is_valid() const { return 1; }
2454 
2455 private:
2456 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2457   virtual int do_apply_event(Relay_log_info const *rli);
2458   virtual int do_update_pos(Relay_log_info *rli);
2459   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli);
2460 #endif
2461 };
2462 
2463 
2464 /**
2465   @class Rand_log_event
2466 
2467   Logs random seed used by the next RAND(), and by PASSWORD() in 4.1.0.
2468   4.1.1 does not need it (it's repeatable again) so this event needn't be
2469   written in 4.1.1 for PASSWORD() (but the fact that it is written is just a
2470   waste, it does not cause bugs).
2471 
2472   The state of the random number generation consists of 128 bits,
2473   which are stored internally as two 64-bit numbers.
2474 
2475   @section Rand_log_event_binary_format Binary Format
2476 
2477   The Post-Header for this event type is empty.  The Body has two
2478   components:
2479 
2480   <table>
2481   <caption>Body for Rand_log_event</caption>
2482 
2483   <tr>
2484     <th>Name</th>
2485     <th>Format</th>
2486     <th>Description</th>
2487   </tr>
2488 
2489   <tr>
2490     <td>seed1</td>
2491     <td>8 byte unsigned integer</td>
2492     <td>64 bit random seed1.</td>
2493   </tr>
2494 
2495   <tr>
2496     <td>seed2</td>
2497     <td>8 byte unsigned integer</td>
2498     <td>64 bit random seed2.</td>
2499   </tr>
2500   </table>
2501 */
2502 
2503 class Rand_log_event: public Log_event
2504 {
2505  public:
2506   ulonglong seed1;
2507   ulonglong seed2;
2508 
2509 #ifdef MYSQL_SERVER
Rand_log_event(THD * thd_arg,ulonglong seed1_arg,ulonglong seed2_arg)2510   Rand_log_event(THD* thd_arg, ulonglong seed1_arg, ulonglong seed2_arg)
2511     :Log_event(thd_arg,0,0),seed1(seed1_arg),seed2(seed2_arg)
2512   {}
2513 #ifdef HAVE_REPLICATION
2514   void pack_info(Protocol* protocol);
2515 #endif /* HAVE_REPLICATION */
2516 #else
2517   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2518 #endif
2519 
2520   Rand_log_event(const char* buf,
2521                  const Format_description_log_event *description_event);
~Rand_log_event()2522   ~Rand_log_event() {}
get_type_code()2523   Log_event_type get_type_code() { return RAND_EVENT;}
get_data_size()2524   int get_data_size() { return 16; /* sizeof(ulonglong) * 2*/ }
2525 #ifdef MYSQL_SERVER
2526   bool write(IO_CACHE* file);
2527 #endif
is_valid()2528   bool is_valid() const { return 1; }
2529 
2530 private:
2531 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2532   virtual int do_apply_event(Relay_log_info const *rli);
2533   virtual int do_update_pos(Relay_log_info *rli);
2534   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli);
2535 #endif
2536 };
2537 
2538 /**
2539   @class Xid_log_event
2540 
2541   Logs xid of the transaction-to-be-committed in the 2pc protocol.
2542   Has no meaning in replication, slaves ignore it.
2543 
2544   @section Xid_log_event_binary_format Binary Format
2545 */
2546 #ifdef MYSQL_CLIENT
2547 typedef ulonglong my_xid; // this line is the same as in handler.h
2548 #endif
2549 
2550 class Xid_log_event: public Log_event
2551 {
2552  public:
2553    my_xid xid;
2554 
2555 #ifdef MYSQL_SERVER
Xid_log_event(THD * thd_arg,my_xid x)2556   Xid_log_event(THD* thd_arg, my_xid x): Log_event(thd_arg, 0, TRUE), xid(x) {}
2557 #ifdef HAVE_REPLICATION
2558   void pack_info(Protocol* protocol);
2559 #endif /* HAVE_REPLICATION */
2560 #else
2561   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2562 #endif
2563 
2564   Xid_log_event(const char* buf,
2565                 const Format_description_log_event *description_event);
~Xid_log_event()2566   ~Xid_log_event() {}
get_type_code()2567   Log_event_type get_type_code() { return XID_EVENT;}
get_data_size()2568   int get_data_size() { return sizeof(xid); }
2569 #ifdef MYSQL_SERVER
2570   bool write(IO_CACHE* file);
2571 #endif
is_valid()2572   bool is_valid() const { return 1; }
2573 
2574 private:
2575 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2576   virtual int do_apply_event(Relay_log_info const *rli);
2577   enum_skip_reason do_shall_skip(Relay_log_info *rli);
2578 #endif
2579 };
2580 
2581 /**
2582   @class User_var_log_event
2583 
2584   Every time a query uses the value of a user variable, a User_var_log_event is
2585   written before the Query_log_event, to set the user variable.
2586 
2587   @section User_var_log_event_binary_format Binary Format
2588 */
2589 
2590 class User_var_log_event: public Log_event
2591 {
2592 public:
2593   enum {
2594     UNDEF_F= 0,
2595     UNSIGNED_F= 1
2596   };
2597   char *name;
2598   uint name_len;
2599   char *val;
2600   ulong val_len;
2601   Item_result type;
2602   uint charset_number;
2603   bool is_null;
2604   uchar flags;
2605 #ifdef MYSQL_SERVER
2606   bool deferred;
2607   query_id_t query_id;
User_var_log_event(THD * thd_arg,char * name_arg,uint name_len_arg,char * val_arg,ulong val_len_arg,Item_result type_arg,uint charset_number_arg,uchar flags_arg)2608   User_var_log_event(THD* thd_arg, char *name_arg, uint name_len_arg,
2609                      char *val_arg, ulong val_len_arg, Item_result type_arg,
2610 		     uint charset_number_arg, uchar flags_arg)
2611     :Log_event(thd_arg,0,0), name(name_arg), name_len(name_len_arg), val(val_arg),
2612     val_len(val_len_arg), type(type_arg), charset_number(charset_number_arg),
2613     flags(flags_arg), deferred(false)
2614     { is_null= !val; }
2615   void pack_info(Protocol* protocol);
2616 #else
2617   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2618 #endif
2619 
2620   User_var_log_event(const char* buf, uint event_len,
2621                      const Format_description_log_event *description_event);
~User_var_log_event()2622   ~User_var_log_event() {}
get_type_code()2623   Log_event_type get_type_code() { return USER_VAR_EVENT;}
2624 #ifdef MYSQL_SERVER
2625   bool write(IO_CACHE* file);
2626   /*
2627      Getter and setter for deferred User-event.
2628      Returns true if the event is not applied directly
2629      and which case the applier adjusts execution path.
2630   */
is_deferred()2631   bool is_deferred() { return deferred; }
2632   /*
2633     In case of the deffered applying the variable instance is flagged
2634     and the parsing time query id is stored to be used at applying time.
2635   */
set_deferred(query_id_t qid)2636   void set_deferred(query_id_t qid) { deferred= true; query_id= qid; }
2637 #endif
is_valid()2638   bool is_valid() const { return name != 0; }
2639 
2640 private:
2641 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2642   virtual int do_apply_event(Relay_log_info const *rli);
2643   virtual int do_update_pos(Relay_log_info *rli);
2644   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli);
2645 #endif
2646 };
2647 
2648 
2649 /**
2650   @class Stop_log_event
2651 
2652   @section Stop_log_event_binary_format Binary Format
2653 
2654   The Post-Header and Body for this event type are empty; it only has
2655   the Common-Header.
2656 */
2657 class Stop_log_event: public Log_event
2658 {
2659 public:
2660 #ifdef MYSQL_SERVER
Stop_log_event()2661   Stop_log_event() :Log_event()
2662   {}
2663 #else
2664   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2665 #endif
2666 
Stop_log_event(const char * buf,const Format_description_log_event * description_event)2667   Stop_log_event(const char* buf,
2668                  const Format_description_log_event *description_event):
2669     Log_event(buf, description_event)
2670   {}
~Stop_log_event()2671   ~Stop_log_event() {}
get_type_code()2672   Log_event_type get_type_code() { return STOP_EVENT;}
is_valid()2673   bool is_valid() const { return 1; }
2674 
2675 private:
2676 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2677   virtual int do_update_pos(Relay_log_info *rli);
do_shall_skip(Relay_log_info * rli)2678   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli)
2679   {
2680     /*
2681       Events from ourself should be skipped, but they should not
2682       decrease the slave skip counter.
2683      */
2684     if (this->server_id == ::server_id)
2685       return Log_event::EVENT_SKIP_IGNORE;
2686     else
2687       return Log_event::EVENT_SKIP_NOT;
2688   }
2689 #endif
2690 };
2691 
2692 /**
2693   @class Rotate_log_event
2694 
2695   This will be deprecated when we move to using sequence ids.
2696 
2697   @section Rotate_log_event_binary_format Binary Format
2698 
2699   The Post-Header has one component:
2700 
2701   <table>
2702   <caption>Post-Header for Rotate_log_event</caption>
2703 
2704   <tr>
2705     <th>Name</th>
2706     <th>Format</th>
2707     <th>Description</th>
2708   </tr>
2709 
2710   <tr>
2711     <td>position</td>
2712     <td>8 byte integer</td>
2713     <td>The position within the binlog to rotate to.</td>
2714   </tr>
2715 
2716   </table>
2717 
2718   The Body has one component:
2719 
2720   <table>
2721   <caption>Body for Rotate_log_event</caption>
2722 
2723   <tr>
2724     <th>Name</th>
2725     <th>Format</th>
2726     <th>Description</th>
2727   </tr>
2728 
2729   <tr>
2730     <td>new_log</td>
2731     <td>variable length string without trailing zero, extending to the
2732     end of the event (determined by the length field of the
2733     Common-Header)
2734     </td>
2735     <td>Name of the binlog to rotate to.</td>
2736   </tr>
2737 
2738   </table>
2739 */
2740 
2741 class Rotate_log_event: public Log_event
2742 {
2743 public:
2744   enum {
2745     DUP_NAME= 2, // if constructor should dup the string argument
2746     RELAY_LOG=4  // rotate event for relay log
2747   };
2748   const char* new_log_ident;
2749   ulonglong pos;
2750   uint ident_len;
2751   uint flags;
2752 #ifdef MYSQL_SERVER
2753   Rotate_log_event(const char* new_log_ident_arg,
2754 		   uint ident_len_arg,
2755 		   ulonglong pos_arg, uint flags);
2756 #ifdef HAVE_REPLICATION
2757   void pack_info(Protocol* protocol);
2758 #endif /* HAVE_REPLICATION */
2759 #else
2760   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2761 #endif
2762 
2763   Rotate_log_event(const char* buf, uint event_len,
2764                    const Format_description_log_event* description_event);
~Rotate_log_event()2765   ~Rotate_log_event()
2766   {
2767     if (flags & DUP_NAME)
2768       my_free((void*) new_log_ident);
2769   }
get_type_code()2770   Log_event_type get_type_code() { return ROTATE_EVENT;}
get_data_size()2771   int get_data_size() { return  ident_len + ROTATE_HEADER_LEN;}
is_valid()2772   bool is_valid() const { return new_log_ident != 0; }
2773 #ifdef MYSQL_SERVER
2774   bool write(IO_CACHE* file);
2775 #endif
2776 
2777 private:
2778 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2779   virtual int do_update_pos(Relay_log_info *rli);
2780   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli);
2781 #endif
2782 };
2783 
2784 
2785 /* the classes below are for the new LOAD DATA INFILE logging */
2786 
2787 /**
2788   @class Create_file_log_event
2789 
2790   @section Create_file_log_event_binary_format Binary Format
2791 */
2792 
2793 class Create_file_log_event: public Load_log_event
2794 {
2795 protected:
2796   /*
2797     Pretend we are Load event, so we can write out just
2798     our Load part - used on the slave when writing event out to
2799     SQL_LOAD-*.info file
2800   */
2801   bool fake_base;
2802 public:
2803   uchar* block;
2804   const char *event_buf;
2805   uint block_len;
2806   uint file_id;
2807   bool inited_from_old;
2808 
2809 #ifdef MYSQL_SERVER
2810   Create_file_log_event(THD* thd, sql_exchange* ex, const char* db_arg,
2811 			const char* table_name_arg,
2812 			List<Item>& fields_arg,
2813                         bool is_concurrent_arg,
2814 			enum enum_duplicates handle_dup, bool ignore,
2815 			uchar* block_arg, uint block_len_arg,
2816 			bool using_trans);
2817 #ifdef HAVE_REPLICATION
2818   void pack_info(Protocol* protocol);
2819 #endif /* HAVE_REPLICATION */
2820 #else
2821   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2822   void print(FILE* file, PRINT_EVENT_INFO* print_event_info,
2823              bool enable_local);
2824 #endif
2825 
2826   Create_file_log_event(const char* buf, uint event_len,
2827                         const Format_description_log_event* description_event);
~Create_file_log_event()2828   ~Create_file_log_event()
2829   {
2830     my_free((void*) event_buf);
2831   }
2832 
get_type_code()2833   Log_event_type get_type_code()
2834   {
2835     return fake_base ? Load_log_event::get_type_code() : CREATE_FILE_EVENT;
2836   }
get_data_size()2837   int get_data_size()
2838   {
2839     return (fake_base ? Load_log_event::get_data_size() :
2840 	    Load_log_event::get_data_size() +
2841 	    4 + 1 + block_len);
2842   }
is_valid()2843   bool is_valid() const { return inited_from_old || block != 0; }
2844 #ifdef MYSQL_SERVER
2845   bool write_data_header(IO_CACHE* file);
2846   bool write_data_body(IO_CACHE* file);
2847   /*
2848     Cut out Create_file extentions and
2849     write it as Load event - used on the slave
2850   */
2851   bool write_base(IO_CACHE* file);
2852 #endif
2853 
2854 private:
2855 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2856   virtual int do_apply_event(Relay_log_info const *rli);
2857 #endif
2858 };
2859 
2860 
2861 /**
2862   @class Append_block_log_event
2863 
2864   @section Append_block_log_event_binary_format Binary Format
2865 */
2866 
2867 class Append_block_log_event: public Log_event
2868 {
2869 public:
2870   uchar* block;
2871   uint block_len;
2872   uint file_id;
2873   /*
2874     'db' is filled when the event is created in mysql_load() (the
2875     event needs to have a 'db' member to be well filtered by
2876     binlog-*-db rules). 'db' is not written to the binlog (it's not
2877     used by Append_block_log_event::write()), so it can't be read in
2878     the Append_block_log_event(const char* buf, int event_len)
2879     constructor.  In other words, 'db' is used only for filtering by
2880     binlog-*-db rules.  Create_file_log_event is different: it's 'db'
2881     (which is inherited from Load_log_event) is written to the binlog
2882     and can be re-read.
2883   */
2884   const char* db;
2885 
2886 #ifdef MYSQL_SERVER
2887   Append_block_log_event(THD* thd, const char* db_arg, uchar* block_arg,
2888 			 uint block_len_arg, bool using_trans);
2889 #ifdef HAVE_REPLICATION
2890   void pack_info(Protocol* protocol);
2891   virtual int get_create_or_append() const;
2892 #endif /* HAVE_REPLICATION */
2893 #else
2894   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2895 #endif
2896 
2897   Append_block_log_event(const char* buf, uint event_len,
2898                          const Format_description_log_event
2899                          *description_event);
~Append_block_log_event()2900   ~Append_block_log_event() {}
get_type_code()2901   Log_event_type get_type_code() { return APPEND_BLOCK_EVENT;}
get_data_size()2902   int get_data_size() { return  block_len + APPEND_BLOCK_HEADER_LEN ;}
is_valid()2903   bool is_valid() const { return block != 0; }
2904 #ifdef MYSQL_SERVER
2905   bool write(IO_CACHE* file);
get_db()2906   const char* get_db() { return db; }
2907 #endif
2908 
2909 private:
2910 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2911   virtual int do_apply_event(Relay_log_info const *rli);
2912 #endif
2913 };
2914 
2915 
2916 /**
2917   @class Delete_file_log_event
2918 
2919   @section Delete_file_log_event_binary_format Binary Format
2920 */
2921 
2922 class Delete_file_log_event: public Log_event
2923 {
2924 public:
2925   uint file_id;
2926   const char* db; /* see comment in Append_block_log_event */
2927 
2928 #ifdef MYSQL_SERVER
2929   Delete_file_log_event(THD* thd, const char* db_arg, bool using_trans);
2930 #ifdef HAVE_REPLICATION
2931   void pack_info(Protocol* protocol);
2932 #endif /* HAVE_REPLICATION */
2933 #else
2934   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2935   void print(FILE* file, PRINT_EVENT_INFO* print_event_info,
2936              bool enable_local);
2937 #endif
2938 
2939   Delete_file_log_event(const char* buf, uint event_len,
2940                         const Format_description_log_event* description_event);
~Delete_file_log_event()2941   ~Delete_file_log_event() {}
get_type_code()2942   Log_event_type get_type_code() { return DELETE_FILE_EVENT;}
get_data_size()2943   int get_data_size() { return DELETE_FILE_HEADER_LEN ;}
is_valid()2944   bool is_valid() const { return file_id != 0; }
2945 #ifdef MYSQL_SERVER
2946   bool write(IO_CACHE* file);
get_db()2947   const char* get_db() { return db; }
2948 #endif
2949 
2950 private:
2951 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2952   virtual int do_apply_event(Relay_log_info const *rli);
2953 #endif
2954 };
2955 
2956 
2957 /**
2958   @class Execute_load_log_event
2959 
2960   @section Delete_file_log_event_binary_format Binary Format
2961 */
2962 
2963 class Execute_load_log_event: public Log_event
2964 {
2965 public:
2966   uint file_id;
2967   const char* db; /* see comment in Append_block_log_event */
2968 
2969 #ifdef MYSQL_SERVER
2970   Execute_load_log_event(THD* thd, const char* db_arg, bool using_trans);
2971 #ifdef HAVE_REPLICATION
2972   void pack_info(Protocol* protocol);
2973 #endif /* HAVE_REPLICATION */
2974 #else
2975   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
2976 #endif
2977 
2978   Execute_load_log_event(const char* buf, uint event_len,
2979                          const Format_description_log_event
2980                          *description_event);
~Execute_load_log_event()2981   ~Execute_load_log_event() {}
get_type_code()2982   Log_event_type get_type_code() { return EXEC_LOAD_EVENT;}
get_data_size()2983   int get_data_size() { return  EXEC_LOAD_HEADER_LEN ;}
is_valid()2984   bool is_valid() const { return file_id != 0; }
2985 #ifdef MYSQL_SERVER
2986   bool write(IO_CACHE* file);
get_db()2987   const char* get_db() { return db; }
2988 #endif
2989 
2990 private:
2991 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
2992   virtual int do_apply_event(Relay_log_info const *rli);
2993 #endif
2994 };
2995 
2996 
2997 /**
2998   @class Begin_load_query_log_event
2999 
3000   Event for the first block of file to be loaded, its only difference from
3001   Append_block event is that this event creates or truncates existing file
3002   before writing data.
3003 
3004   @section Begin_load_query_log_event_binary_format Binary Format
3005 */
3006 class Begin_load_query_log_event: public Append_block_log_event
3007 {
3008 public:
3009 #ifdef MYSQL_SERVER
3010   Begin_load_query_log_event(THD* thd_arg, const char *db_arg,
3011                              uchar* block_arg, uint block_len_arg,
3012                              bool using_trans);
3013 #ifdef HAVE_REPLICATION
3014   Begin_load_query_log_event(THD* thd);
3015   int get_create_or_append() const;
3016 #endif /* HAVE_REPLICATION */
3017 #endif
3018   Begin_load_query_log_event(const char* buf, uint event_len,
3019                              const Format_description_log_event
3020                              *description_event);
~Begin_load_query_log_event()3021   ~Begin_load_query_log_event() {}
get_type_code()3022   Log_event_type get_type_code() { return BEGIN_LOAD_QUERY_EVENT; }
3023 private:
3024 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3025   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli);
3026 #endif
3027 };
3028 
3029 
3030 /*
3031   Elements of this enum describe how LOAD DATA handles duplicates.
3032 */
3033 enum enum_load_dup_handling { LOAD_DUP_ERROR= 0, LOAD_DUP_IGNORE,
3034                               LOAD_DUP_REPLACE };
3035 
3036 /**
3037   @class Execute_load_query_log_event
3038 
3039   Event responsible for LOAD DATA execution, it similar to Query_log_event
3040   but before executing the query it substitutes original filename in LOAD DATA
3041   query with name of temporary file.
3042 
3043   @section Execute_load_query_log_event_binary_format Binary Format
3044 */
3045 class Execute_load_query_log_event: public Query_log_event
3046 {
3047 public:
3048   uint file_id;       // file_id of temporary file
3049   uint fn_pos_start;  // pointer to the part of the query that should
3050                       // be substituted
3051   uint fn_pos_end;    // pointer to the end of this part of query
3052   /*
3053     We have to store type of duplicate handling explicitly, because
3054     for LOAD DATA it also depends on LOCAL option. And this part
3055     of query will be rewritten during replication so this information
3056     may be lost...
3057   */
3058   enum_load_dup_handling dup_handling;
3059 
3060 #ifdef MYSQL_SERVER
3061   Execute_load_query_log_event(THD* thd, const char* query_arg,
3062                                ulong query_length, uint fn_pos_start_arg,
3063                                uint fn_pos_end_arg,
3064                                enum_load_dup_handling dup_handling_arg,
3065                                bool using_trans, bool direct,
3066                                bool suppress_use, int errcode);
3067 #ifdef HAVE_REPLICATION
3068   void pack_info(Protocol* protocol);
3069 #endif /* HAVE_REPLICATION */
3070 #else
3071   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
3072   /* Prints the query as LOAD DATA LOCAL and with rewritten filename */
3073   void print(FILE* file, PRINT_EVENT_INFO* print_event_info,
3074 	     const char *local_fname);
3075 #endif
3076   Execute_load_query_log_event(const char* buf, uint event_len,
3077                                const Format_description_log_event
3078                                *description_event);
~Execute_load_query_log_event()3079   ~Execute_load_query_log_event() {}
3080 
get_type_code()3081   Log_event_type get_type_code() { return EXECUTE_LOAD_QUERY_EVENT; }
is_valid()3082   bool is_valid() const { return Query_log_event::is_valid() && file_id != 0; }
3083 
3084   ulong get_post_header_size_for_derived();
3085 #ifdef MYSQL_SERVER
3086   bool write_post_header_for_derived(IO_CACHE* file);
3087 #endif
3088 
3089 private:
3090 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3091   virtual int do_apply_event(Relay_log_info const *rli);
3092 #endif
3093 };
3094 
3095 
3096 #ifdef MYSQL_CLIENT
3097 /**
3098   @class Unknown_log_event
3099 
3100   @section Unknown_log_event_binary_format Binary Format
3101 */
3102 class Unknown_log_event: public Log_event
3103 {
3104 public:
3105   /*
3106     Even if this is an unknown event, we still pass description_event to
3107     Log_event's ctor, this way we can extract maximum information from the
3108     event's header (the unique ID for example).
3109   */
Unknown_log_event(const char * buf,const Format_description_log_event * description_event)3110   Unknown_log_event(const char* buf,
3111                     const Format_description_log_event *description_event):
3112     Log_event(buf, description_event)
3113   {}
~Unknown_log_event()3114   ~Unknown_log_event() {}
3115   void print(FILE* file, PRINT_EVENT_INFO* print_event_info);
get_type_code()3116   Log_event_type get_type_code() { return UNKNOWN_EVENT;}
is_valid()3117   bool is_valid() const { return 1; }
3118 };
3119 #endif
3120 char *str_to_hex(char *to, const char *from, uint len);
3121 
3122 /**
3123   @class Table_map_log_event
3124 
3125   In row-based mode, every row operation event is preceded by a
3126   Table_map_log_event which maps a table definition to a number.  The
3127   table definition consists of database name, table name, and column
3128   definitions.
3129 
3130   @section Table_map_log_event_binary_format Binary Format
3131 
3132   The Post-Header has the following components:
3133 
3134   <table>
3135   <caption>Post-Header for Table_map_log_event</caption>
3136 
3137   <tr>
3138     <th>Name</th>
3139     <th>Format</th>
3140     <th>Description</th>
3141   </tr>
3142 
3143   <tr>
3144     <td>table_id</td>
3145     <td>6 bytes unsigned integer</td>
3146     <td>The number that identifies the table.</td>
3147   </tr>
3148 
3149   <tr>
3150     <td>flags</td>
3151     <td>2 byte bitfield</td>
3152     <td>Reserved for future use; currently always 0.</td>
3153   </tr>
3154 
3155   </table>
3156 
3157   The Body has the following components:
3158 
3159   <table>
3160   <caption>Body for Table_map_log_event</caption>
3161 
3162   <tr>
3163     <th>Name</th>
3164     <th>Format</th>
3165     <th>Description</th>
3166   </tr>
3167 
3168   <tr>
3169     <td>database_name</td>
3170     <td>one byte string length, followed by null-terminated string</td>
3171     <td>The name of the database in which the table resides.  The name
3172     is represented as a one byte unsigned integer representing the
3173     number of bytes in the name, followed by length bytes containing
3174     the database name, followed by a terminating 0 byte.  (Note the
3175     redundancy in the representation of the length.)  </td>
3176   </tr>
3177 
3178   <tr>
3179     <td>table_name</td>
3180     <td>one byte string length, followed by null-terminated string</td>
3181     <td>The name of the table, encoded the same way as the database
3182     name above.</td>
3183   </tr>
3184 
3185   <tr>
3186     <td>column_count</td>
3187     <td>@ref packed_integer "Packed Integer"</td>
3188     <td>The number of columns in the table, represented as a packed
3189     variable-length integer.</td>
3190   </tr>
3191 
3192   <tr>
3193     <td>column_type</td>
3194     <td>List of column_count 1 byte enumeration values</td>
3195     <td>The type of each column in the table, listed from left to
3196     right.  Each byte is mapped to a column type according to the
3197     enumeration type enum_field_types defined in mysql_com.h.  The
3198     mapping of types to numbers is listed in the table @ref
3199     Table_table_map_log_event_column_types "below" (along with
3200     description of the associated metadata field).  </td>
3201   </tr>
3202 
3203   <tr>
3204     <td>metadata_length</td>
3205     <td>@ref packed_integer "Packed Integer"</td>
3206     <td>The length of the following metadata block</td>
3207   </tr>
3208 
3209   <tr>
3210     <td>metadata</td>
3211     <td>list of metadata for each column</td>
3212     <td>For each column from left to right, a chunk of data who's
3213     length and semantics depends on the type of the column.  The
3214     length and semantics for the metadata for each column are listed
3215     in the table @ref Table_table_map_log_event_column_types
3216     "below".</td>
3217   </tr>
3218 
3219   <tr>
3220     <td>null_bits</td>
3221     <td>column_count bits, rounded up to nearest byte</td>
3222     <td>For each column, a bit indicating whether data in the column
3223     can be NULL or not.  The number of bytes needed for this is
3224     int((column_count+7)/8).  The flag for the first column from the
3225     left is in the least-significant bit of the first byte, the second
3226     is in the second least significant bit of the first byte, the
3227     ninth is in the least significant bit of the second byte, and so
3228     on.  </td>
3229   </tr>
3230 
3231   </table>
3232 
3233   The table below lists all column types, along with the numerical
3234   identifier for it and the size and interpretation of meta-data used
3235   to describe the type.
3236 
3237   @anchor Table_table_map_log_event_column_types
3238   <table>
3239   <caption>Table_map_log_event column types: numerical identifier and
3240   metadata</caption>
3241   <tr>
3242     <th>Name</th>
3243     <th>Identifier</th>
3244     <th>Size of metadata in bytes</th>
3245     <th>Description of metadata</th>
3246   </tr>
3247 
3248   <tr>
3249     <td>MYSQL_TYPE_DECIMAL</td><td>0</td>
3250     <td>0</td>
3251     <td>No column metadata.</td>
3252   </tr>
3253 
3254   <tr>
3255     <td>MYSQL_TYPE_TINY</td><td>1</td>
3256     <td>0</td>
3257     <td>No column metadata.</td>
3258   </tr>
3259 
3260   <tr>
3261     <td>MYSQL_TYPE_SHORT</td><td>2</td>
3262     <td>0</td>
3263     <td>No column metadata.</td>
3264   </tr>
3265 
3266   <tr>
3267     <td>MYSQL_TYPE_LONG</td><td>3</td>
3268     <td>0</td>
3269     <td>No column metadata.</td>
3270   </tr>
3271 
3272   <tr>
3273     <td>MYSQL_TYPE_FLOAT</td><td>4</td>
3274     <td>1 byte</td>
3275     <td>1 byte unsigned integer, representing the "pack_length", which
3276     is equal to sizeof(float) on the server from which the event
3277     originates.</td>
3278   </tr>
3279 
3280   <tr>
3281     <td>MYSQL_TYPE_DOUBLE</td><td>5</td>
3282     <td>1 byte</td>
3283     <td>1 byte unsigned integer, representing the "pack_length", which
3284     is equal to sizeof(double) on the server from which the event
3285     originates.</td>
3286   </tr>
3287 
3288   <tr>
3289     <td>MYSQL_TYPE_NULL</td><td>6</td>
3290     <td>0</td>
3291     <td>No column metadata.</td>
3292   </tr>
3293 
3294   <tr>
3295     <td>MYSQL_TYPE_TIMESTAMP</td><td>7</td>
3296     <td>0</td>
3297     <td>No column metadata.</td>
3298   </tr>
3299 
3300   <tr>
3301     <td>MYSQL_TYPE_LONGLONG</td><td>8</td>
3302     <td>0</td>
3303     <td>No column metadata.</td>
3304   </tr>
3305 
3306   <tr>
3307     <td>MYSQL_TYPE_INT24</td><td>9</td>
3308     <td>0</td>
3309     <td>No column metadata.</td>
3310   </tr>
3311 
3312   <tr>
3313     <td>MYSQL_TYPE_DATE</td><td>10</td>
3314     <td>0</td>
3315     <td>No column metadata.</td>
3316   </tr>
3317 
3318   <tr>
3319     <td>MYSQL_TYPE_TIME</td><td>11</td>
3320     <td>0</td>
3321     <td>No column metadata.</td>
3322   </tr>
3323 
3324   <tr>
3325     <td>MYSQL_TYPE_DATETIME</td><td>12</td>
3326     <td>0</td>
3327     <td>No column metadata.</td>
3328   </tr>
3329 
3330   <tr>
3331     <td>MYSQL_TYPE_YEAR</td><td>13</td>
3332     <td>0</td>
3333     <td>No column metadata.</td>
3334   </tr>
3335 
3336   <tr>
3337     <td><i>MYSQL_TYPE_NEWDATE</i></td><td><i>14</i></td>
3338     <td>&ndash;</td>
3339     <td><i>This enumeration value is only used internally and cannot
3340     exist in a binlog.</i></td>
3341   </tr>
3342 
3343   <tr>
3344     <td>MYSQL_TYPE_VARCHAR</td><td>15</td>
3345     <td>2 bytes</td>
3346     <td>2 byte unsigned integer representing the maximum length of
3347     the string.</td>
3348   </tr>
3349 
3350   <tr>
3351     <td>MYSQL_TYPE_BIT</td><td>16</td>
3352     <td>2 bytes</td>
3353     <td>A 1 byte unsigned int representing the length in bits of the
3354     bitfield (0 to 64), followed by a 1 byte unsigned int
3355     representing the number of bytes occupied by the bitfield.  The
3356     number of bytes is either int((length+7)/8) or int(length/8).</td>
3357   </tr>
3358 
3359   <tr>
3360     <td>MYSQL_TYPE_NEWDECIMAL</td><td>246</td>
3361     <td>2 bytes</td>
3362     <td>A 1 byte unsigned int representing the precision, followed
3363     by a 1 byte unsigned int representing the number of decimals.</td>
3364   </tr>
3365 
3366   <tr>
3367     <td><i>MYSQL_TYPE_ENUM</i></td><td><i>247</i></td>
3368     <td>&ndash;</td>
3369     <td><i>This enumeration value is only used internally and cannot
3370     exist in a binlog.</i></td>
3371   </tr>
3372 
3373   <tr>
3374     <td><i>MYSQL_TYPE_SET</i></td><td><i>248</i></td>
3375     <td>&ndash;</td>
3376     <td><i>This enumeration value is only used internally and cannot
3377     exist in a binlog.</i></td>
3378   </tr>
3379 
3380   <tr>
3381     <td>MYSQL_TYPE_TINY_BLOB</td><td>249</td>
3382     <td>&ndash;</td>
3383     <td><i>This enumeration value is only used internally and cannot
3384     exist in a binlog.</i></td>
3385   </tr>
3386 
3387   <tr>
3388     <td><i>MYSQL_TYPE_MEDIUM_BLOB</i></td><td><i>250</i></td>
3389     <td>&ndash;</td>
3390     <td><i>This enumeration value is only used internally and cannot
3391     exist in a binlog.</i></td>
3392   </tr>
3393 
3394   <tr>
3395     <td><i>MYSQL_TYPE_LONG_BLOB</i></td><td><i>251</i></td>
3396     <td>&ndash;</td>
3397     <td><i>This enumeration value is only used internally and cannot
3398     exist in a binlog.</i></td>
3399   </tr>
3400 
3401   <tr>
3402     <td>MYSQL_TYPE_BLOB</td><td>252</td>
3403     <td>1 byte</td>
3404     <td>The pack length, i.e., the number of bytes needed to represent
3405     the length of the blob: 1, 2, 3, or 4.</td>
3406   </tr>
3407 
3408   <tr>
3409     <td>MYSQL_TYPE_VAR_STRING</td><td>253</td>
3410     <td>2 bytes</td>
3411     <td>This is used to store both strings and enumeration values.
3412     The first byte is a enumeration value storing the <i>real
3413     type</i>, which may be either MYSQL_TYPE_VAR_STRING or
3414     MYSQL_TYPE_ENUM.  The second byte is a 1 byte unsigned integer
3415     representing the field size, i.e., the number of bytes needed to
3416     store the length of the string.</td>
3417   </tr>
3418 
3419   <tr>
3420     <td>MYSQL_TYPE_STRING</td><td>254</td>
3421     <td>2 bytes</td>
3422     <td>The first byte is always MYSQL_TYPE_VAR_STRING (i.e., 253).
3423     The second byte is the field size, i.e., the number of bytes in
3424     the representation of size of the string: 3 or 4.</td>
3425   </tr>
3426 
3427   <tr>
3428     <td>MYSQL_TYPE_GEOMETRY</td><td>255</td>
3429     <td>1 byte</td>
3430     <td>The pack length, i.e., the number of bytes needed to represent
3431     the length of the geometry: 1, 2, 3, or 4.</td>
3432   </tr>
3433 
3434   </table>
3435 */
3436 class Table_map_log_event : public Log_event
3437 {
3438 public:
3439   /* Constants */
3440   enum
3441   {
3442     TYPE_CODE = TABLE_MAP_EVENT
3443   };
3444 
3445   /**
3446      Enumeration of the errors that can be returned.
3447    */
3448   enum enum_error
3449   {
3450     ERR_OPEN_FAILURE = -1,               /**< Failure to open table */
3451     ERR_OK = 0,                                 /**< No error */
3452     ERR_TABLE_LIMIT_EXCEEDED = 1,      /**< No more room for tables */
3453     ERR_OUT_OF_MEM = 2,                         /**< Out of memory */
3454     ERR_BAD_TABLE_DEF = 3,     /**< Table definition does not match */
3455     ERR_RBR_TO_SBR = 4  /**< daisy-chanining RBR to SBR not allowed */
3456   };
3457 
3458   enum enum_flag
3459   {
3460     /*
3461        Nothing here right now, but the flags support is there in
3462        preparation for changes that are coming.  Need to add a
3463        constant to make it compile under HP-UX: aCC does not like
3464        empty enumerations.
3465     */
3466     ENUM_FLAG_COUNT
3467   };
3468 
3469   typedef uint16 flag_set;
3470 
3471   /* Special constants representing sets of flags */
3472   enum
3473   {
3474     TM_NO_FLAGS = 0U,
3475     TM_BIT_LEN_EXACT_F = (1U << 0)
3476   };
3477 
get_flags(flag_set flag)3478   flag_set get_flags(flag_set flag) const { return m_flags & flag; }
3479 
3480 #ifdef MYSQL_SERVER
3481   Table_map_log_event(THD *thd, TABLE *tbl, ulong tid, bool is_transactional);
3482 #endif
3483 #ifdef HAVE_REPLICATION
3484   Table_map_log_event(const char *buf, uint event_len,
3485                       const Format_description_log_event *description_event);
3486 #endif
3487 
3488   ~Table_map_log_event();
3489 
3490 #ifdef MYSQL_CLIENT
create_table_def()3491   table_def *create_table_def()
3492   {
3493     return new table_def(m_coltype, m_colcnt, m_field_metadata,
3494                          m_field_metadata_size, m_null_bits, m_flags);
3495   }
3496 #endif
get_table_id()3497   ulong get_table_id() const        { return m_table_id; }
get_table_name()3498   const char *get_table_name() const { return m_tblnam; }
get_db_name()3499   const char *get_db_name() const    { return m_dbnam; }
3500 
get_type_code()3501   virtual Log_event_type get_type_code() { return TABLE_MAP_EVENT; }
is_valid()3502   virtual bool is_valid() const { return m_memory != NULL; /* we check malloc */ }
3503 
get_data_size()3504   virtual int get_data_size() { return (uint) m_data_size; }
3505 #ifdef MYSQL_SERVER
3506   virtual int save_field_metadata();
3507   virtual bool write_data_header(IO_CACHE *file);
3508   virtual bool write_data_body(IO_CACHE *file);
get_db()3509   virtual const char *get_db() { return m_dbnam; }
3510 #endif
3511 
3512 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3513   virtual void pack_info(Protocol *protocol);
3514 #endif
3515 
3516 #ifdef MYSQL_CLIENT
3517   virtual void print(FILE *file, PRINT_EVENT_INFO *print_event_info);
3518 #endif
3519 
3520 
3521 private:
3522 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3523   virtual int do_apply_event(Relay_log_info const *rli);
3524   virtual int do_update_pos(Relay_log_info *rli);
3525   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli);
3526 #endif
3527 
3528 #ifdef MYSQL_SERVER
3529   TABLE         *m_table;
3530 #endif
3531   char const    *m_dbnam;
3532   size_t         m_dblen;
3533   char const    *m_tblnam;
3534   size_t         m_tbllen;
3535   ulong          m_colcnt;
3536   uchar         *m_coltype;
3537 
3538   uchar         *m_memory;
3539   ulong          m_table_id;
3540   flag_set       m_flags;
3541 
3542   size_t         m_data_size;
3543 
3544   uchar          *m_field_metadata;        // buffer for field metadata
3545   /*
3546     The size of field metadata buffer set by calling save_field_metadata()
3547   */
3548   ulong          m_field_metadata_size;
3549   uchar         *m_null_bits;
3550   uchar         *m_meta_memory;
3551 };
3552 
3553 
3554 /**
3555   @class Rows_log_event
3556 
3557  Common base class for all row-containing log events.
3558 
3559  RESPONSIBILITIES
3560 
3561    Encode the common parts of all events containing rows, which are:
3562    - Write data header and data body to an IO_CACHE.
3563    - Provide an interface for adding an individual row to the event.
3564 
3565   @section Rows_log_event_binary_format Binary Format
3566 */
3567 
3568 
3569 class Rows_log_event : public Log_event
3570 {
3571 public:
3572   /**
3573      Enumeration of the errors that can be returned.
3574    */
3575   enum enum_error
3576   {
3577     ERR_OPEN_FAILURE = -1,               /**< Failure to open table */
3578     ERR_OK = 0,                                 /**< No error */
3579     ERR_TABLE_LIMIT_EXCEEDED = 1,      /**< No more room for tables */
3580     ERR_OUT_OF_MEM = 2,                         /**< Out of memory */
3581     ERR_BAD_TABLE_DEF = 3,     /**< Table definition does not match */
3582     ERR_RBR_TO_SBR = 4  /**< daisy-chanining RBR to SBR not allowed */
3583   };
3584 
3585   /*
3586     These definitions allow you to combine the flags into an
3587     appropriate flag set using the normal bitwise operators.  The
3588     implicit conversion from an enum-constant to an integer is
3589     accepted by the compiler, which is then used to set the real set
3590     of flags.
3591   */
3592   enum enum_flag
3593   {
3594     /* Last event of a statement */
3595     STMT_END_F = (1U << 0),
3596 
3597     /* Value of the OPTION_NO_FOREIGN_KEY_CHECKS flag in thd->options */
3598     NO_FOREIGN_KEY_CHECKS_F = (1U << 1),
3599 
3600     /* Value of the OPTION_RELAXED_UNIQUE_CHECKS flag in thd->options */
3601     RELAXED_UNIQUE_CHECKS_F = (1U << 2),
3602 
3603     /**
3604       Indicates that rows in this event are complete, that is contain
3605       values for all columns of the table.
3606      */
3607     COMPLETE_ROWS_F = (1U << 3)
3608   };
3609 
3610   typedef uint16 flag_set;
3611 
3612   /* Special constants representing sets of flags */
3613   enum
3614   {
3615       RLE_NO_FLAGS = 0U
3616   };
3617 
3618   virtual ~Rows_log_event();
3619 
set_flags(flag_set flags_arg)3620   void set_flags(flag_set flags_arg) { m_flags |= flags_arg; }
clear_flags(flag_set flags_arg)3621   void clear_flags(flag_set flags_arg) { m_flags &= ~flags_arg; }
get_flags(flag_set flags_arg)3622   flag_set get_flags(flag_set flags_arg) const { return m_flags & flags_arg; }
3623 
3624 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3625   virtual void pack_info(Protocol *protocol);
3626 #endif
3627 
3628 #ifdef MYSQL_CLIENT
3629   /* not for direct call, each derived has its own ::print() */
3630   virtual void print(FILE *file, PRINT_EVENT_INFO *print_event_info)= 0;
3631   void print_verbose(IO_CACHE *file,
3632                      PRINT_EVENT_INFO *print_event_info);
3633   size_t print_verbose_one_row(IO_CACHE *file, table_def *td,
3634                                PRINT_EVENT_INFO *print_event_info,
3635                                MY_BITMAP *cols_bitmap,
3636                                const uchar *ptr, const uchar *prefix);
3637 #endif
3638 
3639 #ifdef MYSQL_SERVER
add_row_data(uchar * data,size_t length)3640   int add_row_data(uchar *data, size_t length)
3641   {
3642     return do_add_row_data(data,length);
3643   }
3644 #endif
3645 
3646   /* Member functions to implement superclass interface */
3647   virtual int get_data_size();
3648 
get_cols()3649   MY_BITMAP const *get_cols() const { return &m_cols; }
get_width()3650   size_t get_width() const          { return m_width; }
get_table_id()3651   ulong get_table_id() const        { return m_table_id; }
3652 
3653 #ifdef MYSQL_SERVER
3654   virtual bool write_data_header(IO_CACHE *file);
3655   virtual bool write_data_body(IO_CACHE *file);
get_db()3656   virtual const char *get_db() { return m_table->s->db.str; }
3657 #endif
3658   /*
3659     Check that malloc() succeeded in allocating memory for the rows
3660     buffer and the COLS vector. Checking that an Update_rows_log_event
3661     is valid is done in the Update_rows_log_event::is_valid()
3662     function.
3663   */
is_valid()3664   virtual bool is_valid() const
3665   {
3666     return m_rows_buf && m_cols.bitmap;
3667   }
3668 
3669   uint     m_row_count;         /* The number of rows added to the event */
3670 
3671 protected:
3672   /*
3673      The constructors are protected since you're supposed to inherit
3674      this class, not create instances of this class.
3675   */
3676 #ifdef MYSQL_SERVER
3677   Rows_log_event(THD*, TABLE*, ulong table_id,
3678 		 MY_BITMAP const *cols, bool is_transactional);
3679 #endif
3680   Rows_log_event(const char *row_data, uint event_len,
3681 		 Log_event_type event_type,
3682 		 const Format_description_log_event *description_event);
3683 
3684 #ifdef MYSQL_CLIENT
3685   void print_helper(FILE *, PRINT_EVENT_INFO *, char const *const name);
3686 #endif
3687 
3688 #ifdef MYSQL_SERVER
3689   virtual int do_add_row_data(uchar *data, size_t length);
3690 #endif
3691 
3692 #ifdef MYSQL_SERVER
3693   TABLE *m_table;		/* The table the rows belong to */
3694 #endif
3695   ulong       m_table_id;	/* Table ID */
3696   MY_BITMAP   m_cols;		/* Bitmap denoting columns available */
3697   ulong       m_width;          /* The width of the columns bitmap */
3698   /*
3699     Bitmap for columns available in the after image, if present. These
3700     fields are only available for Update_rows events. Observe that the
3701     width of both the before image COLS vector and the after image
3702     COLS vector is the same: the number of columns of the table on the
3703     master.
3704   */
3705   MY_BITMAP   m_cols_ai;
3706 
3707   ulong       m_master_reclength; /* Length of record on master side */
3708 
3709   /* Bit buffers in the same memory as the class */
3710   uint32    m_bitbuf[128/(sizeof(uint32)*8)];
3711   uint32    m_bitbuf_ai[128/(sizeof(uint32)*8)];
3712 
3713   uchar    *m_rows_buf;		/* The rows in packed format */
3714   uchar    *m_rows_cur;		/* One-after the end of the data */
3715   uchar    *m_rows_end;		/* One-after the end of the allocated space */
3716 
3717   flag_set m_flags;		/* Flags for row-level events */
3718 
3719   /* helper functions */
3720 
3721 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3722   const uchar *m_curr_row;     /* Start of the row being processed */
3723   const uchar *m_curr_row_end; /* One-after the end of the current row */
3724   uchar    *m_key;      /* Buffer to keep key value during searches */
3725 
3726   int find_row(const Relay_log_info *const);
3727   int write_row(const Relay_log_info *const, const bool);
3728 
3729   // Unpack the current row into m_table->record[0]
unpack_current_row(const Relay_log_info * const rli)3730   int unpack_current_row(const Relay_log_info *const rli)
3731   {
3732     DBUG_ASSERT(m_table);
3733 
3734     ASSERT_OR_RETURN_ERROR(m_curr_row < m_rows_end, HA_ERR_CORRUPT_EVENT);
3735     return ::unpack_row(rli, m_table, m_width, m_curr_row, &m_cols,
3736                                    &m_curr_row_end, &m_master_reclength, m_rows_end);
3737   }
3738 
3739   /**
3740     Helper function to check whether there is an auto increment
3741     column on the table where the event is to be applied.
3742 
3743     @return true if there is an autoincrement field on the extra
3744             columns, false otherwise.
3745    */
is_auto_inc_in_extra_columns()3746   inline bool is_auto_inc_in_extra_columns()
3747   {
3748     DBUG_ASSERT(m_table);
3749     return (m_table->next_number_field &&
3750             m_table->next_number_field->field_index >= m_width);
3751   }
3752 #endif
3753 
3754 private:
3755 
3756 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3757   virtual int do_apply_event(Relay_log_info const *rli);
3758   virtual int do_update_pos(Relay_log_info *rli);
3759   virtual enum_skip_reason do_shall_skip(Relay_log_info *rli);
3760 
3761   /*
3762     Primitive to prepare for a sequence of row executions.
3763 
3764     DESCRIPTION
3765 
3766       Before doing a sequence of do_prepare_row() and do_exec_row()
3767       calls, this member function should be called to prepare for the
3768       entire sequence. Typically, this member function will allocate
3769       space for any buffers that are needed for the two member
3770       functions mentioned above.
3771 
3772     RETURN VALUE
3773 
3774       The member function will return 0 if all went OK, or a non-zero
3775       error code otherwise.
3776   */
3777   virtual
3778   int do_before_row_operations(const Slave_reporting_capability *const log) = 0;
3779 
3780   /*
3781     Primitive to clean up after a sequence of row executions.
3782 
3783     DESCRIPTION
3784 
3785       After doing a sequence of do_prepare_row() and do_exec_row(),
3786       this member function should be called to clean up and release
3787       any allocated buffers.
3788 
3789       The error argument, if non-zero, indicates an error which happened during
3790       row processing before this function was called. In this case, even if
3791       function is successful, it should return the error code given in the argument.
3792   */
3793   virtual
3794   int do_after_row_operations(const Slave_reporting_capability *const log,
3795                               int error) = 0;
3796 
3797   /*
3798     Primitive to do the actual execution necessary for a row.
3799 
3800     DESCRIPTION
3801       The member function will do the actual execution needed to handle a row.
3802       The row is located at m_curr_row. When the function returns,
3803       m_curr_row_end should point at the next row (one byte after the end
3804       of the current row).
3805 
3806     RETURN VALUE
3807       0 if execution succeeded, 1 if execution failed.
3808 
3809   */
3810   virtual int do_exec_row(const Relay_log_info *const rli) = 0;
3811 #endif /* defined(MYSQL_SERVER) && defined(HAVE_REPLICATION) */
3812 
3813   friend class Old_rows_log_event;
3814 };
3815 
3816 /**
3817   @class Write_rows_log_event
3818 
3819   Log row insertions and updates. The event contain several
3820   insert/update rows for a table. Note that each event contains only
3821   rows for one table.
3822 
3823   @section Write_rows_log_event_binary_format Binary Format
3824 */
3825 class Write_rows_log_event : public Rows_log_event
3826 {
3827 public:
3828   enum
3829   {
3830     /* Support interface to THD::binlog_prepare_pending_rows_event */
3831     TYPE_CODE = WRITE_ROWS_EVENT
3832   };
3833 
3834 #if defined(MYSQL_SERVER)
3835   Write_rows_log_event(THD*, TABLE*, ulong table_id,
3836 		       MY_BITMAP const *cols, bool is_transactional);
3837 #endif
3838 #ifdef HAVE_REPLICATION
3839   Write_rows_log_event(const char *buf, uint event_len,
3840                        const Format_description_log_event *description_event);
3841 #endif
3842 #if defined(MYSQL_SERVER)
binlog_row_logging_function(THD * thd,TABLE * table,bool is_transactional,MY_BITMAP * cols,uint fields,const uchar * before_record,const uchar * after_record)3843   static bool binlog_row_logging_function(THD *thd, TABLE *table,
3844                                           bool is_transactional,
3845                                           MY_BITMAP *cols,
3846                                           uint fields,
3847                                           const uchar *before_record
3848                                           __attribute__((unused)),
3849                                           const uchar *after_record)
3850   {
3851     return thd->binlog_write_row(table, is_transactional,
3852                                  cols, fields, after_record);
3853   }
3854 #endif
3855 
3856 private:
get_type_code()3857   virtual Log_event_type get_type_code() { return (Log_event_type)TYPE_CODE; }
3858 
3859 #ifdef MYSQL_CLIENT
3860   void print(FILE *file, PRINT_EVENT_INFO *print_event_info);
3861 #endif
3862 
3863 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3864   virtual int do_before_row_operations(const Slave_reporting_capability *const);
3865   virtual int do_after_row_operations(const Slave_reporting_capability *const,int);
3866   virtual int do_exec_row(const Relay_log_info *const);
3867 #endif
3868 };
3869 
3870 
3871 /**
3872   @class Update_rows_log_event
3873 
3874   Log row updates with a before image. The event contain several
3875   update rows for a table. Note that each event contains only rows for
3876   one table.
3877 
3878   Also note that the row data consists of pairs of row data: one row
3879   for the old data and one row for the new data.
3880 
3881   @section Update_rows_log_event_binary_format Binary Format
3882 */
3883 class Update_rows_log_event : public Rows_log_event
3884 {
3885 public:
3886   enum
3887   {
3888     /* Support interface to THD::binlog_prepare_pending_rows_event */
3889     TYPE_CODE = UPDATE_ROWS_EVENT
3890   };
3891 
3892 #ifdef MYSQL_SERVER
3893   Update_rows_log_event(THD*, TABLE*, ulong table_id,
3894 			MY_BITMAP const *cols_bi,
3895 			MY_BITMAP const *cols_ai,
3896                         bool is_transactional);
3897 
3898   Update_rows_log_event(THD*, TABLE*, ulong table_id,
3899 			MY_BITMAP const *cols,
3900                         bool is_transactional);
3901 
3902   void init(MY_BITMAP const *cols);
3903 #endif
3904 
3905   virtual ~Update_rows_log_event();
3906 
3907 #ifdef HAVE_REPLICATION
3908   Update_rows_log_event(const char *buf, uint event_len,
3909 			const Format_description_log_event *description_event);
3910 #endif
3911 
3912 #ifdef MYSQL_SERVER
binlog_row_logging_function(THD * thd,TABLE * table,bool is_transactional,MY_BITMAP * cols,uint fields,const uchar * before_record,const uchar * after_record)3913   static bool binlog_row_logging_function(THD *thd, TABLE *table,
3914                                           bool is_transactional,
3915                                           MY_BITMAP *cols,
3916                                           uint fields,
3917                                           const uchar *before_record,
3918                                           const uchar *after_record)
3919   {
3920     return thd->binlog_update_row(table, is_transactional,
3921                                   cols, fields, before_record, after_record);
3922   }
3923 #endif
3924 
is_valid()3925   virtual bool is_valid() const
3926   {
3927     return Rows_log_event::is_valid() && m_cols_ai.bitmap;
3928   }
3929 
3930 protected:
get_type_code()3931   virtual Log_event_type get_type_code() { return (Log_event_type)TYPE_CODE; }
3932 
3933 #ifdef MYSQL_CLIENT
3934   void print(FILE *file, PRINT_EVENT_INFO *print_event_info);
3935 #endif
3936 
3937 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
3938   virtual int do_before_row_operations(const Slave_reporting_capability *const);
3939   virtual int do_after_row_operations(const Slave_reporting_capability *const,int);
3940   virtual int do_exec_row(const Relay_log_info *const);
3941 #endif /* defined(MYSQL_SERVER) && defined(HAVE_REPLICATION) */
3942 };
3943 
3944 /**
3945   @class Delete_rows_log_event
3946 
3947   Log row deletions. The event contain several delete rows for a
3948   table. Note that each event contains only rows for one table.
3949 
3950   RESPONSIBILITIES
3951 
3952     - Act as a container for rows that has been deleted on the master
3953       and should be deleted on the slave.
3954 
3955   COLLABORATION
3956 
3957     Row_writer
3958       Create the event and add rows to the event.
3959     Row_reader
3960       Extract the rows from the event.
3961 
3962   @section Delete_rows_log_event_binary_format Binary Format
3963 */
3964 class Delete_rows_log_event : public Rows_log_event
3965 {
3966 public:
3967   enum
3968   {
3969     /* Support interface to THD::binlog_prepare_pending_rows_event */
3970     TYPE_CODE = DELETE_ROWS_EVENT
3971   };
3972 
3973 #ifdef MYSQL_SERVER
3974   Delete_rows_log_event(THD*, TABLE*, ulong,
3975 			MY_BITMAP const *cols, bool is_transactional);
3976 #endif
3977 #ifdef HAVE_REPLICATION
3978   Delete_rows_log_event(const char *buf, uint event_len,
3979 			const Format_description_log_event *description_event);
3980 #endif
3981 #ifdef MYSQL_SERVER
binlog_row_logging_function(THD * thd,TABLE * table,bool is_transactional,MY_BITMAP * cols,uint fields,const uchar * before_record,const uchar * after_record)3982   static bool binlog_row_logging_function(THD *thd, TABLE *table,
3983                                           bool is_transactional,
3984                                           MY_BITMAP *cols,
3985                                           uint fields,
3986                                           const uchar *before_record,
3987                                           const uchar *after_record
3988                                           __attribute__((unused)))
3989   {
3990     return thd->binlog_delete_row(table, is_transactional,
3991                                   cols, fields, before_record);
3992   }
3993 #endif
3994 
3995 protected:
get_type_code()3996   virtual Log_event_type get_type_code() { return (Log_event_type)TYPE_CODE; }
3997 
3998 #ifdef MYSQL_CLIENT
3999   void print(FILE *file, PRINT_EVENT_INFO *print_event_info);
4000 #endif
4001 
4002 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4003   virtual int do_before_row_operations(const Slave_reporting_capability *const);
4004   virtual int do_after_row_operations(const Slave_reporting_capability *const,int);
4005   virtual int do_exec_row(const Relay_log_info *const);
4006 #endif
4007 };
4008 
4009 
4010 #include "log_event_old.h"
4011 
4012 /**
4013   @class Incident_log_event
4014 
4015    Class representing an incident, an occurance out of the ordinary,
4016    that happened on the master.
4017 
4018    The event is used to inform the slave that something out of the
4019    ordinary happened on the master that might cause the database to be
4020    in an inconsistent state.
4021 
4022    <table id="IncidentFormat">
4023    <caption>Incident event format</caption>
4024    <tr>
4025      <th>Symbol</th>
4026      <th>Format</th>
4027      <th>Description</th>
4028    </tr>
4029    <tr>
4030      <td>INCIDENT</td>
4031      <td align="right">2</td>
4032      <td>Incident number as an unsigned integer</td>
4033    </tr>
4034    <tr>
4035      <td>MSGLEN</td>
4036      <td align="right">1</td>
4037      <td>Message length as an unsigned integer</td>
4038    </tr>
4039    <tr>
4040      <td>MESSAGE</td>
4041      <td align="right">MSGLEN</td>
4042      <td>The message, if present. Not null terminated.</td>
4043    </tr>
4044    </table>
4045 
4046   @section Delete_rows_log_event_binary_format Binary Format
4047 */
4048 class Incident_log_event : public Log_event {
4049 public:
4050 #ifdef MYSQL_SERVER
Incident_log_event(THD * thd_arg,Incident incident)4051   Incident_log_event(THD *thd_arg, Incident incident)
4052     : Log_event(thd_arg, 0, FALSE), m_incident(incident)
4053   {
4054     DBUG_ENTER("Incident_log_event::Incident_log_event");
4055     DBUG_PRINT("enter", ("m_incident: %d", m_incident));
4056     m_message.str= NULL;                    /* Just as a precaution */
4057     m_message.length= 0;
4058     set_direct_logging();
4059     DBUG_VOID_RETURN;
4060   }
4061 
Incident_log_event(THD * thd_arg,Incident incident,LEX_STRING const msg)4062   Incident_log_event(THD *thd_arg, Incident incident, LEX_STRING const msg)
4063     : Log_event(thd_arg, 0, FALSE), m_incident(incident)
4064   {
4065     DBUG_ENTER("Incident_log_event::Incident_log_event");
4066     DBUG_PRINT("enter", ("m_incident: %d", m_incident));
4067     m_message= msg;
4068     set_direct_logging();
4069     DBUG_VOID_RETURN;
4070   }
4071 #endif
4072 
4073 #ifdef MYSQL_SERVER
4074   void pack_info(Protocol*);
4075 #endif
4076 
4077   Incident_log_event(const char *buf, uint event_len,
4078                      const Format_description_log_event *descr_event);
4079 
4080   virtual ~Incident_log_event();
4081 
4082 #ifdef MYSQL_CLIENT
4083   virtual void print(FILE *file, PRINT_EVENT_INFO *print_event_info);
4084 #endif
4085 
4086 #if defined(MYSQL_SERVER) && defined(HAVE_REPLICATION)
4087   virtual int do_apply_event(Relay_log_info const *rli);
4088 #endif
4089 
4090   virtual bool write_data_header(IO_CACHE *file);
4091   virtual bool write_data_body(IO_CACHE *file);
4092 
get_type_code()4093   virtual Log_event_type get_type_code() { return INCIDENT_EVENT; }
4094 
is_valid()4095   virtual bool is_valid() const
4096   {
4097     return m_incident > INCIDENT_NONE && m_incident < INCIDENT_COUNT;
4098   }
get_data_size()4099   virtual int get_data_size() {
4100     return INCIDENT_HEADER_LEN + 1 + (uint) m_message.length;
4101   }
4102 
4103 private:
4104   const char *description() const;
4105 
4106   Incident m_incident;
4107   LEX_STRING m_message;
4108 };
4109 
copy_event_cache_to_file_and_reinit(IO_CACHE * cache,FILE * file)4110 static inline bool copy_event_cache_to_file_and_reinit(IO_CACHE *cache,
4111                                                        FILE *file)
4112 {
4113   return
4114     my_b_copy_to_file(cache, file) ||
4115     reinit_io_cache(cache, WRITE_CACHE, 0, FALSE, TRUE);
4116 }
4117 
4118 #ifdef MYSQL_SERVER
4119 /*****************************************************************************
4120 
4121   Heartbeat Log Event class
4122 
4123   Replication event to ensure to slave that master is alive.
4124   The event is originated by master's dump thread and sent straight to
4125   slave without being logged. Slave itself does not store it in relay log
4126   but rather uses a data for immediate checks and throws away the event.
4127 
4128   Two members of the class log_ident and Log_event::log_pos comprise
4129   @see the event_coordinates instance. The coordinates that a heartbeat
4130   instance carries correspond to the last event master has sent from
4131   its binlog.
4132 
4133  ****************************************************************************/
4134 class Heartbeat_log_event: public Log_event
4135 {
4136 public:
4137   Heartbeat_log_event(const char* buf, uint event_len,
4138                       const Format_description_log_event* description_event);
get_type_code()4139   Log_event_type get_type_code() { return HEARTBEAT_LOG_EVENT; }
is_valid()4140   bool is_valid() const
4141     {
4142       return (log_ident != NULL &&
4143               log_pos >= BIN_LOG_HEADER_SIZE);
4144     }
get_log_ident()4145   const char * get_log_ident() { return log_ident; }
get_ident_len()4146   uint get_ident_len() { return ident_len; }
4147 
4148 private:
4149   const char* log_ident;
4150   uint ident_len;
4151 };
4152 
4153 /**
4154    The function is called by slave applier in case there are
4155    active table filtering rules to force gathering events associated
4156    with Query-log-event into an array to execute
4157    them once the fate of the Query is determined for execution.
4158 */
4159 bool slave_execute_deferred_events(THD *thd);
4160 #endif
4161 
4162 int append_query_string(THD *thd, CHARSET_INFO *csinfo,
4163                         String const *from, String *to);
4164 
4165 #ifdef MYSQL_SERVER
4166 /*
4167   This is an utility function that adds a quoted identifier into the a buffer.
4168   This also escapes any existance of the quote string inside the identifier.
4169  */
4170 size_t my_strmov_quoted_identifier(THD *thd, char *buffer,
4171                                    const char* identifier,
4172                                    uint length);
4173 #else
4174 size_t my_strmov_quoted_identifier(char *buffer, const char* identifier);
4175 #endif
4176 size_t my_strmov_quoted_identifier_helper(int q, char *buffer,
4177                                           const char* identifier,
4178                                           uint length);
4179 
4180 
4181 /**
4182   @} (end of group Replication)
4183 */
4184 
4185 #endif /* _log_event_h */
4186