log_event.h 52.1 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
unknown's avatar
unknown committed
2

unknown's avatar
unknown committed
3 4 5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
unknown's avatar
unknown committed
7

unknown's avatar
unknown committed
8 9 10 11
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
unknown's avatar
unknown committed
12

unknown's avatar
unknown committed
13 14 15 16 17
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


18 19
#ifndef _log_event_h
#define _log_event_h
unknown's avatar
unknown committed
20

21 22 23 24
#ifdef __EMX__
#undef write  // remove pthread.h macro definition, conflict with write() class member
#endif

unknown's avatar
unknown committed
25 26 27 28 29 30 31 32
#if defined(__GNUC__) && !defined(MYSQL_CLIENT)
#pragma interface			/* gcc class implementation */
#endif

#define LOG_READ_EOF    -1
#define LOG_READ_BOGUS  -2
#define LOG_READ_IO     -3
#define LOG_READ_MEM    -5
unknown's avatar
unknown committed
33
#define LOG_READ_TRUNC  -6
unknown's avatar
unknown committed
34
#define LOG_READ_TOO_LARGE -7
unknown's avatar
unknown committed
35 36

#define LOG_EVENT_OFFSET 4
unknown's avatar
unknown committed
37 38

/*
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
   3 is MySQL 4.x; 4 is MySQL 5.0.0.
   Compared to version 3, version 4 has:
   - a different Start_log_event, which includes info about the binary log
   (sizes of headers); this info is included for better compatibility if the
   master's MySQL version is different from the slave's.
   - all events have a unique ID (the triplet (server_id, timestamp at server
   start, other) to be sure an event is not executed more than once in a
   multimaster setup, example:
                M1
              /   \
             v     v
             M2    M3
             \     /
              v   v
                S
   if a query is run on M1, it will arrive twice on S, so we need that S
   remembers the last unique ID it has processed, to compare and know if the
   event should be skipped or not. Example of ID: we already have the server id
   (4 bytes), plus:
   timestamp_when_the_master_started (4 bytes), a counter (a sequence number
   which increments every time we write an event to the binlog) (3 bytes).
   Q: how do we handle when the counter is overflowed and restarts from 0 ?
unknown's avatar
unknown committed
61

62
   - Query and Load (Create or Execute) events may have a more precise timestamp
unknown's avatar
unknown committed
63
   (with microseconds), number of matched/affected/warnings rows
64 65
   and fields of session variables: SQL_MODE,
   FOREIGN_KEY_CHECKS, UNIQUE_CHECKS, SQL_AUTO_IS_NULL, the collations and
unknown's avatar
unknown committed
66
   charsets, the PASSWORD() version (old/new/...).
67 68
*/
#define BINLOG_VERSION    4
69

70 71 72
/*
 We could have used SERVER_VERSION_LENGTH, but this introduces an
 obscure dependency - if somebody decided to change SERVER_VERSION_LENGTH
73
 this would break the replication protocol
74 75 76
*/
#define ST_SERVER_VER_LEN 50

77 78 79 80 81
/*
  These are flags and structs to handle all the LOAD DATA INFILE options (LINES
  TERMINATED etc).
*/

82 83 84 85 86 87
/*
  These are flags and structs to handle all the LOAD DATA INFILE options (LINES
  TERMINATED etc).
  DUMPFILE_FLAG is probably useless (DUMPFILE is a clause of SELECT, not of LOAD
  DATA).
*/
88 89 90 91
#define DUMPFILE_FLAG		0x1
#define OPT_ENCLOSED_FLAG	0x2
#define REPLACE_FLAG		0x4
#define IGNORE_FLAG		0x8
92

93 94 95 96 97
#define FIELD_TERM_EMPTY	0x1
#define ENCLOSED_EMPTY		0x2
#define LINE_TERM_EMPTY		0x4
#define LINE_START_EMPTY	0x8
#define ESCAPED_EMPTY		0x10
98

99 100 101 102 103
/*****************************************************************************

  old_sql_ex struct

 ****************************************************************************/
104
struct old_sql_ex
105 106 107 108 109 110 111 112 113
{
  char field_term;
  char enclosed;
  char line_term;
  char line_start;
  char escaped;
  char opt_flags;
  char empty_flags;
};
114

unknown's avatar
unknown committed
115 116
#define NUM_LOAD_DELIM_STRS 5

117 118 119 120 121
/*****************************************************************************

  sql_ex_info struct

 ****************************************************************************/
122
struct sql_ex_info
123 124 125 126 127 128 129 130
{
  char* field_term;
  char* enclosed;
  char* line_term;
  char* line_start;
  char* escaped;
  int cached_new_format;
  uint8 field_term_len,enclosed_len,line_term_len,line_start_len, escaped_len;
unknown's avatar
unknown committed
131
  char opt_flags;
132
  char empty_flags;
unknown's avatar
unknown committed
133

134
  // store in new format even if old is possible
unknown's avatar
unknown committed
135
  void force_new_format() { cached_new_format = 1;}
136 137 138 139 140 141
  int data_size()
  {
    return (new_format() ?
	    field_term_len + enclosed_len + line_term_len +
	    line_start_len + escaped_len + 6 : 7);
  }
142
  bool write_data(IO_CACHE* file);
143 144 145 146 147 148 149 150 151 152
  char* init(char* buf,char* buf_end,bool use_new_format);
  bool new_format()
  {
    return ((cached_new_format != -1) ? cached_new_format :
	    (cached_new_format=(field_term_len > 1 ||
				enclosed_len > 1 ||
				line_term_len > 1 || line_start_len > 1 ||
				escaped_len > 1)));
  }
};
153

154 155 156 157 158 159 160 161 162 163 164 165
/*****************************************************************************

  MySQL Binary Log

  This log consists of events.  Each event has a fixed-length header,
  possibly followed by a variable length data body.

  The data body consists of an optional fixed length segment (post-header)
  and  an optional variable length segment.

  See the #defines below for the format specifics.

unknown's avatar
unknown committed
166 167 168 169 170 171
  The events which really update data are Query_log_event,
  Execute_load_query_log_event and old Load_log_event and
  Execute_load_log_event events (Execute_load_query is used together with
  Begin_load_query and Append_block events to replicate LOAD DATA INFILE.
  Create_file/Append_block/Execute_load (which includes Load_log_event)
  were used to replicate LOAD DATA before the 5.0.3).
172

173
 ****************************************************************************/
174

175 176
#define LOG_EVENT_HEADER_LEN 19     /* the fixed header length */
#define OLD_HEADER_LEN       13     /* the fixed header length in 3.23 */
unknown's avatar
unknown committed
177
/*
178 179 180 181 182 183
   Fixed header length, where 4.x and 5.0 agree. That is, 5.0 may have a longer
   header (it will for sure when we have the unique event's ID), but at least
   the first 19 bytes are the same in 4.x and 5.0. So when we have the unique
   event's ID, LOG_EVENT_HEADER_LEN will be something like 26, but
   LOG_EVENT_MINIMAL_HEADER_LEN will remain 19.
*/
unknown's avatar
unknown committed
184 185
#define LOG_EVENT_MINIMAL_HEADER_LEN 19

186
/* event-specific post-header sizes */
187 188 189
// where 3.23, 4.x and 5.0 agree
#define QUERY_HEADER_MINIMAL_LEN     (4 + 4 + 1 + 2)
// where 5.0 differs: 2 for len of N-bytes vars.
unknown's avatar
unknown committed
190
#define QUERY_HEADER_LEN     (QUERY_HEADER_MINIMAL_LEN + 2)
191
#define LOAD_HEADER_LEN      (4 + 4 + 4 + 1 +1 + 4)
192 193
#define START_V3_HEADER_LEN     (2 + ST_SERVER_VER_LEN + 4)
#define ROTATE_HEADER_LEN    8 // this is FROZEN (the Rotate post-header is frozen)
194 195 196 197
#define CREATE_FILE_HEADER_LEN 4
#define APPEND_BLOCK_HEADER_LEN 4
#define EXEC_LOAD_HEADER_LEN   4
#define DELETE_FILE_HEADER_LEN 4
198
#define FORMAT_DESCRIPTION_HEADER_LEN (START_V3_HEADER_LEN+1+LOG_EVENT_TYPES)
unknown's avatar
unknown committed
199 200
#define EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN (4 + 4 + 4 + 1)
#define EXECUTE_LOAD_QUERY_HEADER_LEN  (QUERY_HEADER_LEN + EXECUTE_LOAD_QUERY_EXTRA_HEADER_LEN)
201

unknown's avatar
unknown committed
202 203
/*
   Event header offsets;
204 205
   these point to places inside the fixed header.
*/
unknown's avatar
unknown committed
206

207
#define EVENT_TYPE_OFFSET    4
208 209
#define SERVER_ID_OFFSET     5
#define EVENT_LEN_OFFSET     9
210
#define LOG_POS_OFFSET       13
211 212
#define FLAGS_OFFSET         17

213
/* start event post-header (for v3 and v4) */
214 215 216 217

#define ST_BINLOG_VER_OFFSET  0
#define ST_SERVER_VER_OFFSET  2
#define ST_CREATED_OFFSET     (ST_SERVER_VER_OFFSET + ST_SERVER_VER_LEN)
218
#define ST_COMMON_HEADER_LEN_OFFSET (ST_CREATED_OFFSET + 4)
219

220
/* slave event post-header (this event is never written) */
221

unknown's avatar
unknown committed
222 223 224
#define SL_MASTER_PORT_OFFSET   8
#define SL_MASTER_POS_OFFSET    0
#define SL_MASTER_HOST_OFFSET   10
225 226 227

/* query event post-header */

228 229 230 231
#define Q_THREAD_ID_OFFSET	0
#define Q_EXEC_TIME_OFFSET	4
#define Q_DB_LEN_OFFSET		8
#define Q_ERR_CODE_OFFSET	9
232
#define Q_STATUS_VARS_LEN_OFFSET 11
233
#define Q_DATA_OFFSET		QUERY_HEADER_LEN
234 235 236
/* these are codes, not offsets; not more than 256 values (1 byte). */
#define Q_FLAGS2_CODE           0
#define Q_SQL_MODE_CODE         1
237 238 239 240
/*
  Q_CATALOG_CODE is catalog with end zero stored; it is used only by MySQL
  5.0.x where 0<=x<=3.
*/
241
#define Q_CATALOG_CODE          2
242
#define Q_AUTO_INCREMENT	3
243
#define Q_CHARSET_CODE          4
244
#define Q_TIME_ZONE_CODE        5
245 246 247 248 249 250 251 252
/*
  Q_CATALOG_NZ_CODE is catalog withOUT end zero stored; it is used by MySQL
  5.0.x where x>=4. Saves one byte in every Query_log_event in binlog,
  compared to Q_CATALOG_CODE. The reason we didn't simply re-use
  Q_CATALOG_CODE is that then a 5.0.3 slave of this 5.0.x (x>=4) master would
  crash (segfault etc) because it would expect a 0 when there is none.
*/
#define Q_CATALOG_NZ_CODE       6
253 254 255 256 257 258

/* Intvar event post-header */

#define I_TYPE_OFFSET        0
#define I_VAL_OFFSET         1

unknown's avatar
unknown committed
259 260 261 262 263
/* Rand event post-header */

#define RAND_SEED1_OFFSET 0
#define RAND_SEED2_OFFSET 8

unknown's avatar
unknown committed
264 265 266 267 268 269 270 271
/* User_var event post-header */

#define UV_VAL_LEN_SIZE        4
#define UV_VAL_IS_NULL         1
#define UV_VAL_TYPE_SIZE       1
#define UV_NAME_LEN_SIZE       4
#define UV_CHARSET_NUMBER_SIZE 4

272 273 274 275 276
/* Load event post-header */

#define L_THREAD_ID_OFFSET   0
#define L_EXEC_TIME_OFFSET   4
#define L_SKIP_LINES_OFFSET  8
unknown's avatar
unknown committed
277 278
#define L_TBL_LEN_OFFSET     12
#define L_DB_LEN_OFFSET      13
279
#define L_NUM_FIELDS_OFFSET  14
280
#define L_SQL_EX_OFFSET      18
281
#define L_DATA_OFFSET        LOAD_HEADER_LEN
282

283 284 285 286
/* Rotate event post-header */

#define R_POS_OFFSET       0
#define R_IDENT_OFFSET     8
287

288 289 290
/* CF to DF handle LOAD DATA INFILE */

/* CF = "Create File" */
291 292 293
#define CF_FILE_ID_OFFSET  0
#define CF_DATA_OFFSET     CREATE_FILE_HEADER_LEN

294
/* AB = "Append Block" */
295 296 297
#define AB_FILE_ID_OFFSET  0
#define AB_DATA_OFFSET     APPEND_BLOCK_HEADER_LEN

298
/* EL = "Execute Load" */
299 300
#define EL_FILE_ID_OFFSET  0

301
/* DF = "Delete File" */
302
#define DF_FILE_ID_OFFSET  0
303

unknown's avatar
unknown committed
304 305 306 307 308 309
/* ELQ = "Execute Load Query" */
#define ELQ_FILE_ID_OFFSET QUERY_HEADER_LEN
#define ELQ_FN_POS_START_OFFSET ELQ_FILE_ID_OFFSET + 4
#define ELQ_FN_POS_END_OFFSET ELQ_FILE_ID_OFFSET + 8
#define ELQ_DUP_HANDLING_OFFSET ELQ_FILE_ID_OFFSET + 12

310
/* 4 bytes which all binlogs should begin with */
unknown's avatar
unknown committed
311
#define BINLOG_MAGIC        "\xfe\x62\x69\x6e"
312

313 314 315 316 317 318 319 320
/*
  The 2 flags below were useless :
  - the first one was never set
  - the second one was set in all Rotate events on the master, but not used for
  anything useful.
  So they are now removed and their place may later be reused for other
  flags. Then one must remember that Rotate events in 4.x have
  LOG_EVENT_FORCED_ROTATE_F set, so one should not rely on the value of the
unknown's avatar
unknown committed
321
  replacing flag when reading a Rotate event.
322 323 324
  I keep the defines here just to remember what they were.
*/
#ifdef TO_BE_REMOVED
unknown's avatar
unknown committed
325
#define LOG_EVENT_TIME_F            0x1
unknown's avatar
unknown committed
326
#define LOG_EVENT_FORCED_ROTATE_F   0x2
327
#endif
328 329

/*
330 331 332 333 334 335 336 337
   This flag only makes sense for Format_description_log_event. It is set
   when the event is written, and *reset* when a binlog file is
   closed (yes, it's the only case when MySQL modifies already written
   part of binlog).  Thus it is a reliable indicator that binlog was
   closed correctly.  (Stop_log_event is not enough, there's always a
   small chance that mysqld crashes in the middle of insert and end of
   the binlog would look like a Stop_log_event).

338 339 340 341 342 343 344 345
   This flag is used to detect a restart after a crash, and to provide
   "unbreakable" binlog. The problem is that on a crash storage engines
   rollback automatically, while binlog does not.  To solve this we use this
   flag and automatically append ROLLBACK to every non-closed binlog (append
   virtually, on reading, file itself is not changed). If this flag is found,
   mysqlbinlog simply prints "ROLLBACK" Replication master does not abort on
   binlog corruption, but takes it as EOF, and replication slave forces a
   rollback in this case.
346 347 348

   Note, that old binlogs does not have this flag set, so we get a
   a backward-compatible behaviour.
349 350
*/

351 352
#define LOG_EVENT_BINLOG_IN_USE_F       0x1

unknown's avatar
unknown committed
353
/*
354 355 356 357 358
   If the query depends on the thread (for example: TEMPORARY TABLE).
   Currently this is used by mysqlbinlog to know it must print
   SET @@PSEUDO_THREAD_ID=xx; before the query (it would not hurt to print it
   for every query but this would be slow).
*/
unknown's avatar
unknown committed
359
#define LOG_EVENT_THREAD_SPECIFIC_F 0x4
360

361 362 363 364 365 366 367 368 369 370 371 372 373
/*
  Suppress the generation of 'USE' statements before the actual
  statement. This flag should be set for any events that does not need
  the current database set to function correctly. Most notable cases
  are 'CREATE DATABASE' and 'DROP DATABASE'.

  This flags should only be used in exceptional circumstances, since
  it introduce a significant change in behaviour regarding the
  replication logic together with the flags --binlog-do-db and
  --replicated-do-db.
 */
#define LOG_EVENT_SUPPRESS_USE_F    0x8

unknown's avatar
unknown committed
374
/*
375 376 377 378 379 380 381
   OPTIONS_WRITTEN_TO_BIN_LOG are the bits of thd->options which must be
   written to the binlog. OPTIONS_WRITTEN_TO_BINLOG could be written
   into the Format_description_log_event, so that if later we don't want
   to replicate a variable we did replicate, or the contrary, it's
   doable. But it should not be too hard to decide once for all of what
   we replicate and what we don't, among the fixed 32 bits of
   thd->options.
382
   I (Guilhem) have read through every option's usage, and it looks like
383 384 385 386
   OPTION_AUTO_IS_NULL and OPTION_NO_FOREIGN_KEYS are the only ones
   which alter how the query modifies the table. It's good to replicate
   OPTION_RELAXED_UNIQUE_CHECKS too because otherwise, the slave may
   insert data slower than the master, in InnoDB.
387
   OPTION_BIG_SELECTS is not needed (the slave thread runs with
388 389 390
   max_join_size=HA_POS_ERROR) and OPTION_BIG_TABLES is not needed
   either, as the manual says (because a too big in-memory temp table is
   automatically written to disk).
unknown's avatar
unknown committed
391
*/
392 393 394
#define OPTIONS_WRITTEN_TO_BIN_LOG (OPTION_AUTO_IS_NULL | \
OPTION_NO_FOREIGN_KEY_CHECKS | OPTION_RELAXED_UNIQUE_CHECKS)

395 396
enum Log_event_type
{
397 398
  /*
    Every time you update this enum (when you add a type), you have to
unknown's avatar
unknown committed
399
    fix Format_description_log_event::Format_description_log_event().
400 401 402 403 404
  */
  UNKNOWN_EVENT= 0, START_EVENT_V3, QUERY_EVENT, STOP_EVENT, ROTATE_EVENT,
  INTVAR_EVENT, LOAD_EVENT, SLAVE_EVENT, CREATE_FILE_EVENT,
  APPEND_BLOCK_EVENT, EXEC_LOAD_EVENT, DELETE_FILE_EVENT,
  /*
unknown's avatar
unknown committed
405 406 407
    NEW_LOAD_EVENT is like LOAD_EVENT except that it has a longer
    sql_ex, allowing multibyte TERMINATED BY etc; both types share the
    same class (Load_log_event)
408
  */
unknown's avatar
unknown committed
409
  NEW_LOAD_EVENT,
410
  RAND_EVENT, USER_VAR_EVENT,
unknown's avatar
unknown committed
411
  FORMAT_DESCRIPTION_EVENT,
unknown's avatar
unknown committed
412
  XID_EVENT,
unknown's avatar
unknown committed
413 414
  BEGIN_LOAD_QUERY_EVENT,
  EXECUTE_LOAD_QUERY_EVENT,
unknown's avatar
unknown committed
415 416 417 418 419 420

  /*
    add new events here - right above this comment!
    existing events should never change their numbers
  */

421
  ENUM_END_EVENT /* end marker */
422 423
};

unknown's avatar
unknown committed
424
/*
425 426 427 428 429 430
   The number of types we handle in Format_description_log_event (UNKNOWN_EVENT
   is not to be handled, it does not exist in binlogs, it does not have a
   format).
*/
#define LOG_EVENT_TYPES (ENUM_END_EVENT-1)

431 432 433 434 435
enum Int_event_type
{
  INVALID_INT_EVENT = 0, LAST_INSERT_ID_EVENT = 1, INSERT_ID_EVENT = 2
};

unknown's avatar
unknown committed
436 437 438

#ifndef MYSQL_CLIENT
class String;
439 440
class MYSQL_LOG;
class THD;
unknown's avatar
unknown committed
441 442
#endif

443 444
class Format_description_log_event;

445
struct st_relay_log_info;
446

447 448 449 450 451 452 453 454 455 456 457 458 459
#ifdef MYSQL_CLIENT
/*
  A structure for mysqlbinlog to remember the last db, flags2, sql_mode etc; it
  is passed to events' print() methods, so that they print only the necessary
  USE and SET commands.
*/
typedef struct st_last_event_info
{
  // TODO: have the last catalog here ??
  char db[FN_REFLEN+1]; // TODO: make this a LEX_STRING when thd->db is
  bool flags2_inited;
  uint32 flags2;
  bool sql_mode_inited;
unknown's avatar
unknown committed
460
  ulong sql_mode;		/* must be same as THD.variables.sql_mode */
461
  ulong auto_increment_increment, auto_increment_offset;
462 463
  bool charset_inited;
  char charset[6]; // 3 variables, each of them storable in 2 bytes
464
  char time_zone_str[MAX_TIME_ZONE_NAME_LENGTH];
465
  st_last_event_info()
466 467
    :flags2_inited(0), sql_mode_inited(0),
     auto_increment_increment(1),auto_increment_offset(1), charset_inited(0)
468
    {
469 470 471 472 473 474 475
      /*
        Currently we only use static LAST_EVENT_INFO objects, so zeroed at
        program's startup, but these explicit bzero() is for the day someone
        creates dynamic instances.
      */
      bzero(db, sizeof(db));
      bzero(charset, sizeof(charset));
476
      bzero(time_zone_str, sizeof(time_zone_str));
477 478 479 480 481
    }
} LAST_EVENT_INFO;
#endif


482 483 484 485 486 487 488
/*****************************************************************************

  Log_event class

  This is the abstract base class for binary log events.

 ****************************************************************************/
unknown's avatar
unknown committed
489 490 491
class Log_event
{
public:
unknown's avatar
unknown committed
492
  /*
493 494 495 496 497 498 499 500
    The offset in the log where this event originally appeared (it is
    preserved in relay logs, making SHOW SLAVE STATUS able to print
    coordinates of the event in the master's binlog). Note: when a
    transaction is written by the master to its binlog (wrapped in
    BEGIN/COMMIT) the log_pos of all the queries it contains is the
    one of the BEGIN (this way, when one does SHOW SLAVE STATUS it
    sees the offset of the BEGIN, which is logical as rollback may
    occur), except the COMMIT query which has its real offset.
501
  */
502
  my_off_t log_pos;
unknown's avatar
unknown committed
503
  /*
504 505 506
     A temp buffer for read_log_event; it is later analysed according to the
     event's type, and its content is distributed in the event-specific fields.
  */
unknown's avatar
unknown committed
507
  char *temp_buf;
508
  /*
509 510 511 512 513 514
    Timestamp on the master(for debugging and replication of
    NOW()/TIMESTAMP).  It is important for queries and LOAD DATA
    INFILE. This is set at the event's creation time, except for Query
    and Load (et al.) events where this is set at the query's
    execution time, which guarantees good replication (otherwise, we
    could have a query and its event with different timestamps).
515
  */
unknown's avatar
unknown committed
516
  time_t when;
517
  /* The number of seconds the query took to run on the master. */
unknown's avatar
unknown committed
518
  ulong exec_time;
519 520 521
  /* Number of bytes written by write() function */
  ulong data_written;

unknown's avatar
unknown committed
522
  /*
523 524
    The master's server id (is preserved in the relay log; used to
    prevent from infinite loops in circular replication).
525
  */
526
  uint32 server_id;
527 528

  /*
529 530 531
    Some 16 flags. Look above for LOG_EVENT_TIME_F,
    LOG_EVENT_FORCED_ROTATE_F, LOG_EVENT_THREAD_SPECIFIC_F, and
    LOG_EVENT_SUPPRESS_USE_F for notes.
532
  */
533
  uint16 flags;
534

535
  bool cache_stmt;
536

537
#ifndef MYSQL_CLIENT
538
  THD* thd;
539

540
  Log_event();
541
  Log_event(THD* thd_arg, uint16 flags_arg, bool cache_stmt);
542
  /*
543 544 545 546 547 548 549 550 551
    read_log_event() functions read an event from a binlog or relay
    log; used by SHOW BINLOG EVENTS, the binlog_dump thread on the
    master (reads master's binlog), the slave IO thread (reads the
    event sent by binlog_dump), the slave SQL thread (reads the event
    from the relay log).  If mutex is 0, the read will proceed without
    mutex.  We need the description_event to be able to parse the
    event (to know the post-header's size); in fact in read_log_event
    we detect the event's type, then call the specific event's
    constructor and pass description_event as an argument.
552
  */
553 554
  static Log_event* read_log_event(IO_CACHE* file,
				   pthread_mutex_t* log_lock,
555
                                   const Format_description_log_event *description_event);
556 557
  static int read_log_event(IO_CACHE* file, String* packet,
			    pthread_mutex_t* log_lock);
558
  /*
559 560 561
    init_show_field_list() prepares the column names and types for the
    output of SHOW BINLOG EVENTS; it is used only by SHOW BINLOG
    EVENTS.
562
  */
563
  static void init_show_field_list(List<Item>* field_list);
unknown's avatar
SCRUM  
unknown committed
564
#ifdef HAVE_REPLICATION
unknown's avatar
unknown committed
565
  int net_send(Protocol *protocol, const char* log_name, my_off_t pos);
566 567 568 569
  /*
    pack_info() is used by SHOW BINLOG EVENTS; as print() it prepares and sends
    a string to display to the user, so it resembles print().
  */
unknown's avatar
unknown committed
570
  virtual void pack_info(Protocol *protocol);
571 572 573 574
  /*
    The SQL slave thread calls exec_event() to execute the event; this is where
    the slave's data is modified.
  */
575
  virtual int exec_event(struct st_relay_log_info* rli);
unknown's avatar
SCRUM  
unknown committed
576
#endif /* HAVE_REPLICATION */
577 578 579 580 581
  virtual const char* get_db()
  {
    return thd ? thd->db : 0;
  }
#else
582
  Log_event() : temp_buf(0) {}
583
    /* avoid having to link mysqlbinlog against libpthread */
584 585
  static Log_event* read_log_event(IO_CACHE* file,
                                   const Format_description_log_event *description_event);
586
  /* print*() functions are used by mysqlbinlog */
587
  virtual void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0) = 0;
588 589
  void print_timestamp(FILE* file, time_t *ts = 0);
  void print_header(FILE* file);
unknown's avatar
unknown committed
590
#endif
unknown's avatar
unknown committed
591

592 593 594 595 596 597
  static void *operator new(size_t size)
  {
    return (void*) my_malloc((uint)size, MYF(MY_WME|MY_FAE));
  }
  static void operator delete(void *ptr, size_t size)
  {
598
    my_free((gptr) ptr, MYF(MY_WME|MY_ALLOW_ZERO_PTR));
599
  }
unknown's avatar
unknown committed
600

601
#ifndef MYSQL_CLIENT
602 603 604 605 606 607 608 609
  bool write_header(IO_CACHE* file, ulong data_length);
  virtual bool write(IO_CACHE* file)
  {
    return (write_header(file, get_data_size()) ||
            write_data_header(file) ||
            write_data_body(file));
  }
  virtual bool write_data_header(IO_CACHE* file)
610
  { return 0; }
611
  virtual bool write_data_body(IO_CACHE* file __attribute__((unused)))
612
  { return 0; }
613
#endif
unknown's avatar
unknown committed
614
  virtual Log_event_type get_type_code() = 0;
unknown's avatar
unknown committed
615
  virtual bool is_valid() const = 0;
616
  virtual bool is_artificial_event() { return 0; }
617
  inline bool get_cache_stmt() { return cache_stmt; }
618
  Log_event(const char* buf, const Format_description_log_event* description_event);
619 620 621
  virtual ~Log_event() { free_temp_buf();}
  void register_temp_buf(char* buf) { temp_buf = buf; }
  void free_temp_buf()
622 623
  {
    if (temp_buf)
624
    {
625 626
      my_free(temp_buf, MYF(0));
      temp_buf = 0;
627
    }
628
  }
629 630 631 632
  /*
    Get event length for simple events. For complicated events the length
    is calculated during write()
  */
633
  virtual int get_data_size() { return 0;}
634 635 636
  static Log_event* read_log_event(const char* buf, uint event_len,
				   const char **error,
                                   const Format_description_log_event
unknown's avatar
unknown committed
637
                                   *description_event);
638
  /* returns the human readable name of the event's type */
unknown's avatar
unknown committed
639
  const char* get_type_str();
unknown's avatar
unknown committed
640 641
};

unknown's avatar
unknown committed
642
/*
643 644 645 646 647 648 649 650 651 652 653
   One class for each type of event.
   Two constructors for each class:
   - one to create the event for logging (when the server acts as a master),
   called after an update to the database is done,
   which accepts parameters like the query, the database, the options for LOAD
   DATA INFILE...
   - one to create the event from a packet (when the server acts as a slave),
   called before reproducing the update, which accepts parameters (like a
   buffer). Used to read from the master, from the relay log, and in
   mysqlbinlog. This constructor must be format-tolerant.
*/
unknown's avatar
unknown committed
654

655 656 657 658 659 660 661
/*****************************************************************************

  Query Log Event class

  Logs SQL queries

 ****************************************************************************/
unknown's avatar
unknown committed
662 663 664 665 666 667
class Query_log_event: public Log_event
{
protected:
  char* data_buf;
public:
  const char* query;
668
  const char* catalog;
unknown's avatar
unknown committed
669
  const char* db;
670 671 672 673 674 675
  /*
    If we already know the length of the query string
    we pass it with q_len, so we would not have to call strlen()
    otherwise, set it to 0, in which case, we compute it with strlen()
  */
  uint32 q_len;
unknown's avatar
unknown committed
676
  uint32 db_len;
677
  uint16 error_code;
678
  ulong thread_id;
unknown's avatar
unknown committed
679
  /*
680 681 682 683
    For events created by Query_log_event::exec_event (and
    Load_log_event::exec_event()) we need the *original* thread id, to be able
    to log the event with the original (=master's) thread id (fix for
    BUG#1686).
unknown's avatar
unknown committed
684 685
  */
  ulong slave_proxy_id;
unknown's avatar
unknown committed
686 687

  /*
688 689
    Binlog format 3 and 4 start to differ (as far as class members are
    concerned) from here.
690
  */
unknown's avatar
unknown committed
691

692
  uint catalog_len;			// <= 255 char; 0 means uninited
unknown's avatar
unknown committed
693

694 695
  /*
    We want to be able to store a variable number of N-bit status vars:
696 697 698
    (generally N=32; but N=64 for SQL_MODE) a user may want to log the number
    of affected rows (for debugging) while another does not want to lose 4
    bytes in this.
699 700 701 702 703
    The storage on disk is the following:
    status_vars_len is part of the post-header,
    status_vars are in the variable-length part, after the post-header, before
    the db & query.
    status_vars on disk is a sequence of pairs (code, value) where 'code' means
704 705
    'sql_mode', 'affected' etc. Sometimes 'value' must be a short string, so
    its first byte is its length. For now the order of status vars is:
706
    flags2 - sql_mode - catalog - autoinc - charset
707 708 709 710 711 712 713 714
    We should add the same thing to Load_log_event, but in fact
    LOAD DATA INFILE is going to be logged with a new type of event (logging of
    the plain text query), so Load_log_event would be frozen, so no need. The
    new way of logging LOAD DATA INFILE would use a derived class of
    Query_log_event, so automatically benefit from the work already done for
    status variables in Query_log_event.
 */
  uint16 status_vars_len;
unknown's avatar
unknown committed
715

716 717 718 719 720 721 722 723 724
  /*
    'flags2' is a second set of flags (on top of those in Log_event), for
    session variables. These are thd->options which is & against a mask
    (OPTIONS_WRITTEN_TO_BINLOG).
    flags2_inited helps make a difference between flags2==0 (3.23 or 4.x
    master, we don't know flags2, so use the slave server's global options) and
    flags2==0 (5.0 master, we know this has a meaning of flags all down which
    must influence the query).
  */
unknown's avatar
unknown committed
725
  bool flags2_inited;
726
  bool sql_mode_inited;
727
  bool charset_inited;
unknown's avatar
unknown committed
728 729

  uint32 flags2;
730
  /* In connections sql_mode is 32 bits now but will be 64 bits soon */
unknown's avatar
unknown committed
731
  ulong sql_mode;
732
  ulong auto_increment_increment, auto_increment_offset;
733
  char charset[6];
734 735
  uint time_zone_len; /* 0 means uninited */
  const char *time_zone_str;
unknown's avatar
unknown committed
736

737
#ifndef MYSQL_CLIENT
738

739
  Query_log_event(THD* thd_arg, const char* query_arg, ulong query_length,
740
		  bool using_trans, bool suppress_use);
741
  const char* get_db() { return db; }
unknown's avatar
SCRUM  
unknown committed
742
#ifdef HAVE_REPLICATION
743
  void pack_info(Protocol* protocol);
744
  int exec_event(struct st_relay_log_info* rli);
unknown's avatar
unknown committed
745 746
  int exec_event(struct st_relay_log_info* rli, const char *query_arg,
                 uint32 q_len_arg);
unknown's avatar
SCRUM  
unknown committed
747
#endif /* HAVE_REPLICATION */
748
#else
unknown's avatar
unknown committed
749
  void print_query_header(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
750
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
unknown's avatar
unknown committed
751 752
#endif

753
  Query_log_event(const char* buf, uint event_len,
unknown's avatar
unknown committed
754 755
                  const Format_description_log_event *description_event,
                  Log_event_type event_type);
unknown's avatar
unknown committed
756 757 758
  ~Query_log_event()
  {
    if (data_buf)
759
      my_free((gptr) data_buf, MYF(0));
unknown's avatar
unknown committed
760 761
  }
  Log_event_type get_type_code() { return QUERY_EVENT; }
762
#ifndef MYSQL_CLIENT
763
  bool write(IO_CACHE* file);
764 765
  virtual bool write_post_header_for_derived(IO_CACHE* file) { return FALSE; }
#endif
unknown's avatar
unknown committed
766
  bool is_valid() const { return query != 0; }
unknown's avatar
unknown committed
767 768 769 770 771 772 773

  /*
    Returns number of bytes additionaly written to post header by derived
    events (so far it is only Execute_load_query event).
  */
  virtual ulong get_post_header_size_for_derived() { return 0; }
  /* Writes derived event-specific part of post header. */
unknown's avatar
unknown committed
774 775
};

unknown's avatar
SCRUM  
unknown committed
776
#ifdef HAVE_REPLICATION
777

778 779 780
/*****************************************************************************

  Slave Log Event class
781 782
  Note that this class is currently not used at all; no code writes a
  Slave_log_event (though some code in repl_failsafe.cc reads Slave_log_event).
783
  So it's not a problem if this code is not maintained.
784 785

 ****************************************************************************/
786 787 788 789 790 791
class Slave_log_event: public Log_event
{
protected:
  char* mem_pool;
  void init_from_mem_pool(int data_size);
public:
unknown's avatar
unknown committed
792
  my_off_t master_pos;
793 794
  char* master_host;
  char* master_log;
unknown's avatar
unknown committed
795
  int master_host_len;
796
  int master_log_len;
unknown's avatar
unknown committed
797
  uint16 master_port;
798

unknown's avatar
unknown committed
799
#ifndef MYSQL_CLIENT
800
  Slave_log_event(THD* thd_arg, struct st_relay_log_info* rli);
801
  void pack_info(Protocol* protocol);
802
  int exec_event(struct st_relay_log_info* rli);
803
#else
804
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
unknown's avatar
unknown committed
805
#endif
806

807
  Slave_log_event(const char* buf, uint event_len);
808 809
  ~Slave_log_event();
  int get_data_size();
unknown's avatar
unknown committed
810
  bool is_valid() const { return master_host != 0; }
811
  Log_event_type get_type_code() { return SLAVE_EVENT; }
812
#ifndef MYSQL_CLIENT
813
  bool write(IO_CACHE* file);
814
#endif
815 816
};

unknown's avatar
SCRUM  
unknown committed
817
#endif /* HAVE_REPLICATION */
818

819 820 821 822 823 824

/*****************************************************************************

  Load Log Event class

 ****************************************************************************/
unknown's avatar
unknown committed
825 826
class Load_log_event: public Log_event
{
unknown's avatar
unknown committed
827 828 829 830
private:
  uint get_query_buffer_length();
  void print_query(bool need_db, char *buf, char **end,
                   char **fn_start, char **fn_end);
unknown's avatar
unknown committed
831
protected:
unknown's avatar
unknown committed
832
  int copy_log_event(const char *buf, ulong event_len,
833
                     int body_offset, const Format_description_log_event* description_event);
834

unknown's avatar
unknown committed
835
public:
836
  ulong thread_id;
unknown's avatar
unknown committed
837
  ulong slave_proxy_id;
unknown's avatar
unknown committed
838
  uint32 table_name_len;
839 840 841 842 843
  /*
    No need to have a catalog, as these events can only come from 4.x.
    TODO: this may become false if Dmitri pushes his new LOAD DATA INFILE in
    5.0 only (not in 4.x).
  */
unknown's avatar
unknown committed
844 845 846
  uint32 db_len;
  uint32 fname_len;
  uint32 num_fields;
unknown's avatar
unknown committed
847 848
  const char* fields;
  const uchar* field_lens;
unknown's avatar
unknown committed
849
  uint32 field_block_len;
unknown's avatar
unknown committed
850 851 852 853

  const char* table_name;
  const char* db;
  const char* fname;
unknown's avatar
unknown committed
854
  uint32 skip_lines;
unknown's avatar
unknown committed
855
  sql_ex_info sql_ex;
856
  bool local_fname;
857

858 859
  /* fname doesn't point to memory inside Log_event::temp_buf  */
  void set_fname_outside_temp_buf(const char *afname, uint alen)
unknown's avatar
unknown committed
860 861 862
  {
    fname= afname;
    fname_len= alen;
unknown's avatar
unknown committed
863
    local_fname= TRUE;
unknown's avatar
unknown committed
864
  }
865 866
  /* fname doesn't point to memory inside Log_event::temp_buf  */
  int  check_fname_outside_temp_buf()
unknown's avatar
unknown committed
867
  {
868
    return local_fname;
unknown's avatar
unknown committed
869
  }
870

871
#ifndef MYSQL_CLIENT
unknown's avatar
unknown committed
872 873
  String field_lens_buf;
  String fields_buf;
unknown's avatar
unknown committed
874

875
  Load_log_event(THD* thd, sql_exchange* ex, const char* db_arg,
876
		 const char* table_name_arg,
877
		 List<Item>& fields_arg, enum enum_duplicates handle_dup, bool ignore,
878
		 bool using_trans);
unknown's avatar
unknown committed
879
  void set_fields(const char* db, List<Item> &fields_arg);
880
  const char* get_db() { return db; }
unknown's avatar
SCRUM  
unknown committed
881
#ifdef HAVE_REPLICATION
unknown's avatar
unknown committed
882
  void pack_info(Protocol* protocol);
883
  int exec_event(struct st_relay_log_info* rli)
884
  {
885
    return exec_event(thd->slave_net,rli,0);
886
  }
unknown's avatar
unknown committed
887
  int exec_event(NET* net, struct st_relay_log_info* rli,
888
		 bool use_rli_only_for_errors);
unknown's avatar
SCRUM  
unknown committed
889
#endif /* HAVE_REPLICATION */
890
#else
891 892
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info = 0);
  void print(FILE* file, bool short_form, LAST_EVENT_INFO* last_event_info, bool commented);
unknown's avatar
unknown committed
893 894
#endif

895 896 897 898 899 900 901
  /*
    Note that for all the events related to LOAD DATA (Load_log_event,
    Create_file/Append/Exec/Delete, we pass description_event; however as
    logging of LOAD DATA is going to be changed in 4.1 or 5.0, this is only used
    for the common_header_len (post_header_len will not be changed).
  */
  Load_log_event(const char* buf, uint event_len,
unknown's avatar
unknown committed
902
                 const Format_description_log_event* description_event);
unknown's avatar
unknown committed
903
  ~Load_log_event()
904
  {}
905 906 907 908
  Log_event_type get_type_code()
  {
    return sql_ex.new_format() ? NEW_LOAD_EVENT: LOAD_EVENT;
  }
909
#ifndef MYSQL_CLIENT
910 911
  bool write_data_header(IO_CACHE* file);
  bool write_data_body(IO_CACHE* file);
912
#endif
unknown's avatar
unknown committed
913
  bool is_valid() const { return table_name != 0; }
unknown's avatar
unknown committed
914 915
  int get_data_size()
  {
916 917
    return (table_name_len + db_len + 2 + fname_len
	    + LOAD_HEADER_LEN
918
	    + sql_ex.data_size() + field_block_len + num_fields);
unknown's avatar
unknown committed
919 920 921
  }
};

unknown's avatar
unknown committed
922
extern char server_version[SERVER_VERSION_LENGTH];
unknown's avatar
unknown committed
923

924 925
/*****************************************************************************

926
  Start Log Event_v3 class
927

928 929 930 931 932 933 934
  Start_log_event_v3 is the Start_log_event of binlog format 3 (MySQL 3.23 and
  4.x).
  Format_description_log_event derives from Start_log_event_v3; it is the
  Start_log_event of binlog format 4 (MySQL 5.0), that is, the event that
  describes the other events' header/postheader lengths. This event is sent by
  MySQL 5.0 whenever it starts sending a new binlog if the requested position
  is >4 (otherwise if ==4 the event will be sent naturally).
unknown's avatar
unknown committed
935

936
 ****************************************************************************/
937

938
class Start_log_event_v3: public Log_event
unknown's avatar
unknown committed
939 940
{
public:
unknown's avatar
unknown committed
941
  /*
942 943 944 945 946
    If this event is at the start of the first binary log since server
    startup 'created' should be the timestamp when the event (and the
    binary log) was created.  In the other case (i.e. this event is at
    the start of a binary log created by FLUSH LOGS or automatic
    rotation), 'created' should be 0.  This "trick" is used by MySQL
947 948
    >=4.0.14 slaves to know whether they must drop stale temporary
    tables and whether they should abort unfinished transaction.
949 950 951 952 953 954 955 956

    Note that when 'created'!=0, it is always equal to the event's
    timestamp; indeed Start_log_event is written only in log.cc where
    the first constructor below is called, in which 'created' is set
    to 'when'.  So in fact 'created' is a useless variable. When it is
    0 we can read the actual value from timestamp ('when') and when it
    is non-zero we can read the same value from timestamp
    ('when'). Conclusion:
957 958 959 960 961
     - we use timestamp to print when the binlog was created.
     - we use 'created' only to know if this is a first binlog or not.
     In 3.23.57 we did not pay attention to this identity, so mysqlbinlog in
     3.23.57 does not print 'created the_date' if created was zero. This is now
     fixed.
962
  */
963
  time_t created;
964
  uint16 binlog_version;
965
  char server_version[ST_SERVER_VER_LEN];
966 967 968 969 970 971
  /*
    artifical_event is 1 in the case where this is a generated event that
    should not case any cleanup actions. We handle this in the log by
    setting log_event == 0 (for now).
  */
  bool artificial_event;
972 973

#ifndef MYSQL_CLIENT
974
  Start_log_event_v3();
unknown's avatar
SCRUM  
unknown committed
975
#ifdef HAVE_REPLICATION
976
  void pack_info(Protocol* protocol);
977
  int exec_event(struct st_relay_log_info* rli);
unknown's avatar
SCRUM  
unknown committed
978
#endif /* HAVE_REPLICATION */
979
#else
980 981
  Start_log_event_v3() {}
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
unknown's avatar
unknown committed
982
#endif
983

984 985 986 987
  Start_log_event_v3(const char* buf,
                     const Format_description_log_event* description_event);
  ~Start_log_event_v3() {}
  Log_event_type get_type_code() { return START_EVENT_V3;}
988
#ifndef MYSQL_CLIENT
989
  bool write(IO_CACHE* file);
990
#endif
unknown's avatar
unknown committed
991
  bool is_valid() const { return 1; }
992 993
  int get_data_size()
  {
994 995
    return START_V3_HEADER_LEN; //no variable-sized part
  }
996
  virtual bool is_artificial_event() { return artificial_event; }
997 998
};

999

unknown's avatar
unknown committed
1000
/*
1001 1002 1003 1004 1005 1006 1007 1008
   For binlog version 4.
   This event is saved by threads which read it, as they need it for future
   use (to decode the ordinary events).
*/

class Format_description_log_event: public Start_log_event_v3
{
public:
unknown's avatar
unknown committed
1009
  /*
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
     The size of the fixed header which _all_ events have
     (for binlogs written by this version, this is equal to
     LOG_EVENT_HEADER_LEN), except FORMAT_DESCRIPTION_EVENT and ROTATE_EVENT
     (those have a header of size LOG_EVENT_MINIMAL_HEADER_LEN).
  */
  uint8 common_header_len;
  uint8 number_of_event_types;
  /* The list of post-headers' lengthes */
  uint8 *post_header_len;

  Format_description_log_event(uint8 binlog_ver, const char* server_ver=0);

#ifndef MYSQL_CLIENT
#ifdef HAVE_REPLICATION
  int exec_event(struct st_relay_log_info* rli);
#endif /* HAVE_REPLICATION */
unknown's avatar
unknown committed
1026 1027
#endif

1028 1029 1030 1031
  Format_description_log_event(const char* buf, uint event_len,
                               const Format_description_log_event* description_event);
  ~Format_description_log_event() { my_free((gptr)post_header_len, MYF(0)); }
  Log_event_type get_type_code() { return FORMAT_DESCRIPTION_EVENT;}
1032
#ifndef MYSQL_CLIENT
1033
  bool write(IO_CACHE* file);
1034
#endif
unknown's avatar
unknown committed
1035
  bool is_valid() const
1036
  {
1037 1038 1039
    return ((common_header_len >= ((binlog_version==1) ? OLD_HEADER_LEN :
                                   LOG_EVENT_MINIMAL_HEADER_LEN)) &&
            (post_header_len != NULL));
1040 1041 1042 1043 1044 1045 1046 1047 1048
  }
  int get_data_size()
  {
    /*
      The vector of post-header lengths is considered as part of the
      post-header, because in a given version it never changes (contrary to the
      query in a Query_log_event).
    */
    return FORMAT_DESCRIPTION_HEADER_LEN;
1049
  }
unknown's avatar
unknown committed
1050 1051
};

1052

1053 1054 1055 1056 1057 1058 1059
/*****************************************************************************

  Intvar Log Event class

  Logs special variables such as auto_increment values

 ****************************************************************************/
1060

unknown's avatar
unknown committed
1061 1062 1063 1064 1065
class Intvar_log_event: public Log_event
{
public:
  ulonglong val;
  uchar type;
1066

unknown's avatar
unknown committed
1067
#ifndef MYSQL_CLIENT
1068
  Intvar_log_event(THD* thd_arg,uchar type_arg, ulonglong val_arg)
1069
    :Log_event(thd_arg,0,0),val(val_arg),type(type_arg)
unknown's avatar
unknown committed
1070
  {}
unknown's avatar
SCRUM  
unknown committed
1071
#ifdef HAVE_REPLICATION
1072
  void pack_info(Protocol* protocol);
1073
  int exec_event(struct st_relay_log_info* rli);
unknown's avatar
SCRUM  
unknown committed
1074
#endif /* HAVE_REPLICATION */
1075
#else
1076
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
unknown's avatar
unknown committed
1077
#endif
1078

1079
  Intvar_log_event(const char* buf, const Format_description_log_event* description_event);
unknown's avatar
unknown committed
1080 1081
  ~Intvar_log_event() {}
  Log_event_type get_type_code() { return INTVAR_EVENT;}
unknown's avatar
unknown committed
1082
  const char* get_var_type_name();
1083
  int get_data_size() { return  9; /* sizeof(type) + sizeof(val) */;}
1084
#ifndef MYSQL_CLIENT
1085
  bool write(IO_CACHE* file);
1086
#endif
unknown's avatar
unknown committed
1087
  bool is_valid() const { return 1; }
unknown's avatar
unknown committed
1088 1089
};

1090

unknown's avatar
unknown committed
1091
/*****************************************************************************
1092 1093 1094

  Rand Log Event class

unknown's avatar
unknown committed
1095
  Logs random seed used by the next RAND(), and by PASSWORD() in 4.1.0.
1096 1097 1098
  4.1.1 does not need it (it's repeatable again) so this event needn't be
  written in 4.1.1 for PASSWORD() (but the fact that it is written is just a
  waste, it does not cause bugs).
1099

unknown's avatar
unknown committed
1100
 ****************************************************************************/
1101

unknown's avatar
unknown committed
1102 1103 1104 1105 1106 1107 1108 1109
class Rand_log_event: public Log_event
{
 public:
  ulonglong seed1;
  ulonglong seed2;

#ifndef MYSQL_CLIENT
  Rand_log_event(THD* thd_arg, ulonglong seed1_arg, ulonglong seed2_arg)
1110
    :Log_event(thd_arg,0,0),seed1(seed1_arg),seed2(seed2_arg)
unknown's avatar
unknown committed
1111
  {}
unknown's avatar
SCRUM  
unknown committed
1112
#ifdef HAVE_REPLICATION
1113
  void pack_info(Protocol* protocol);
unknown's avatar
unknown committed
1114
  int exec_event(struct st_relay_log_info* rli);
unknown's avatar
SCRUM  
unknown committed
1115
#endif /* HAVE_REPLICATION */
unknown's avatar
unknown committed
1116
#else
1117
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
unknown's avatar
unknown committed
1118 1119
#endif

1120
  Rand_log_event(const char* buf, const Format_description_log_event* description_event);
unknown's avatar
unknown committed
1121 1122
  ~Rand_log_event() {}
  Log_event_type get_type_code() { return RAND_EVENT;}
1123
  int get_data_size() { return 16; /* sizeof(ulonglong) * 2*/ }
1124
#ifndef MYSQL_CLIENT
1125
  bool write(IO_CACHE* file);
1126
#endif
unknown's avatar
unknown committed
1127
  bool is_valid() const { return 1; }
unknown's avatar
unknown committed
1128 1129
};

1130 1131 1132 1133 1134 1135 1136 1137 1138
/*****************************************************************************

  Xid Log Event class

  Logs xid of the transaction-to-be-committed in the 2pc protocol.
  Has no meaning in replication, slaves ignore it.

 ****************************************************************************/
#ifdef MYSQL_CLIENT
1139
typedef ulonglong my_xid; // this line is the same as in handler.h
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
#endif

class Xid_log_event: public Log_event
{
 public:
   my_xid xid;

#ifndef MYSQL_CLIENT
  Xid_log_event(THD* thd_arg, my_xid x): Log_event(thd_arg,0,0), xid(x) {}
#ifdef HAVE_REPLICATION
  void pack_info(Protocol* protocol);
  int exec_event(struct st_relay_log_info* rli);
#endif /* HAVE_REPLICATION */
#else
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
#endif

  Xid_log_event(const char* buf, const Format_description_log_event* description_event);
  ~Xid_log_event() {}
  Log_event_type get_type_code() { return XID_EVENT;}
  int get_data_size() { return sizeof(xid); }
1161
#ifndef MYSQL_CLIENT
1162
  bool write(IO_CACHE* file);
1163
#endif
1164 1165
  bool is_valid() const { return 1; }
};
1166

unknown's avatar
unknown committed
1167 1168 1169 1170
/*****************************************************************************

  User var Log Event class

1171 1172 1173
  Every time a query uses the value of a user variable, a User_var_log_event is
  written before the Query_log_event, to set the user variable.

1174 1175 1176
  Every time a query uses the value of a user variable, a User_var_log_event is
  written before the Query_log_event, to set the user variable.

unknown's avatar
unknown committed
1177
 ****************************************************************************/
1178

unknown's avatar
unknown committed
1179 1180 1181 1182 1183 1184 1185 1186 1187
class User_var_log_event: public Log_event
{
public:
  char *name;
  uint name_len;
  char *val;
  ulong val_len;
  Item_result type;
  uint charset_number;
1188
  bool is_null;
unknown's avatar
unknown committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
#ifndef MYSQL_CLIENT
  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)
    :Log_event(), name(name_arg), name_len(name_len_arg), val(val_arg),
    val_len(val_len_arg), type(type_arg), charset_number(charset_number_arg)
    { is_null= !val; }
  void pack_info(Protocol* protocol);
  int exec_event(struct st_relay_log_info* rli);
#else
1199
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
unknown's avatar
unknown committed
1200 1201
#endif

1202
  User_var_log_event(const char* buf, const Format_description_log_event* description_event);
unknown's avatar
unknown committed
1203 1204
  ~User_var_log_event() {}
  Log_event_type get_type_code() { return USER_VAR_EVENT;}
1205
#ifndef MYSQL_CLIENT
1206
  bool write(IO_CACHE* file);
1207
#endif
unknown's avatar
unknown committed
1208
  bool is_valid() const { return 1; }
unknown's avatar
unknown committed
1209
};
1210

1211

1212 1213 1214 1215 1216
/*****************************************************************************

  Stop Log Event class

 ****************************************************************************/
unknown's avatar
unknown committed
1217 1218 1219
class Stop_log_event: public Log_event
{
public:
unknown's avatar
unknown committed
1220
#ifndef MYSQL_CLIENT
1221
  Stop_log_event() :Log_event()
unknown's avatar
unknown committed
1222
  {}
1223 1224
  int exec_event(struct st_relay_log_info* rli);
#else
1225
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
unknown's avatar
unknown committed
1226
#endif
1227

1228 1229
  Stop_log_event(const char* buf, const Format_description_log_event* description_event):
    Log_event(buf, description_event)
1230
  {}
unknown's avatar
unknown committed
1231 1232
  ~Stop_log_event() {}
  Log_event_type get_type_code() { return STOP_EVENT;}
unknown's avatar
unknown committed
1233
  bool is_valid() const { return 1; }
unknown's avatar
unknown committed
1234 1235
};

1236 1237 1238 1239
/*****************************************************************************

  Rotate Log Event class

1240
  This will be deprecated when we move to using sequence ids.
1241 1242

 ****************************************************************************/
1243

unknown's avatar
unknown committed
1244 1245 1246 1247
class Rotate_log_event: public Log_event
{
public:
  const char* new_log_ident;
1248
  ulonglong pos;
unknown's avatar
unknown committed
1249
  uint ident_len;
unknown's avatar
unknown committed
1250
  bool alloced;
unknown's avatar
unknown committed
1251
#ifndef MYSQL_CLIENT
1252
  Rotate_log_event(THD* thd_arg, const char* new_log_ident_arg,
1253 1254
		   uint ident_len_arg = 0,
		   ulonglong pos_arg = LOG_EVENT_OFFSET)
1255
    :Log_event(), new_log_ident(new_log_ident_arg),
unknown's avatar
unknown committed
1256 1257
    pos(pos_arg),ident_len(ident_len_arg ? ident_len_arg :
			   (uint) strlen(new_log_ident_arg)), alloced(0)
unknown's avatar
unknown committed
1258
  {}
unknown's avatar
SCRUM  
unknown committed
1259
#ifdef HAVE_REPLICATION
1260
  void pack_info(Protocol* protocol);
1261
  int exec_event(struct st_relay_log_info* rli);
unknown's avatar
SCRUM  
unknown committed
1262
#endif /* HAVE_REPLICATION */
1263
#else
1264
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
1265 1266
#endif

1267 1268
  Rotate_log_event(const char* buf, uint event_len,
                   const Format_description_log_event* description_event);
unknown's avatar
unknown committed
1269 1270 1271 1272 1273 1274
  ~Rotate_log_event()
  {
    if (alloced)
      my_free((gptr) new_log_ident, MYF(0));
  }
  Log_event_type get_type_code() { return ROTATE_EVENT;}
1275
  int get_data_size() { return  ident_len + ROTATE_HEADER_LEN;}
unknown's avatar
unknown committed
1276
  bool is_valid() const { return new_log_ident != 0; }
1277
#ifndef MYSQL_CLIENT
1278
  bool write(IO_CACHE* file);
1279
#endif
1280 1281
};

1282

1283 1284
/* the classes below are for the new LOAD DATA INFILE logging */

1285 1286 1287
/*****************************************************************************
  Create File Log Event class
 ****************************************************************************/
1288

1289
class Create_file_log_event: public Load_log_event
1290
{
1291
protected:
1292 1293 1294 1295 1296
  /*
    Pretend we are Load event, so we can write out just
    our Load part - used on the slave when writing event out to
    SQL_LOAD-*.info file
  */
unknown's avatar
unknown committed
1297
  bool fake_base;
1298 1299
public:
  char* block;
1300
  const char *event_buf;
1301 1302
  uint block_len;
  uint file_id;
unknown's avatar
unknown committed
1303
  bool inited_from_old;
1304

1305 1306
#ifndef MYSQL_CLIENT
  Create_file_log_event(THD* thd, sql_exchange* ex, const char* db_arg,
1307 1308
			const char* table_name_arg,
			List<Item>& fields_arg,
1309
			enum enum_duplicates handle_dup, bool ignore,
1310 1311
			char* block_arg, uint block_len_arg,
			bool using_trans);
unknown's avatar
SCRUM  
unknown committed
1312
#ifdef HAVE_REPLICATION
1313
  void pack_info(Protocol* protocol);
1314
  int exec_event(struct st_relay_log_info* rli);
unknown's avatar
SCRUM  
unknown committed
1315
#endif /* HAVE_REPLICATION */
1316
#else
1317 1318
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
  void print(FILE* file, bool short_form, LAST_EVENT_INFO* last_event_info, bool enable_local);
unknown's avatar
unknown committed
1319 1320
#endif

1321 1322
  Create_file_log_event(const char* buf, uint event_len,
                        const Format_description_log_event* description_event);
1323 1324 1325 1326
  ~Create_file_log_event()
  {
    my_free((char*) event_buf, MYF(MY_ALLOW_ZERO_PTR));
  }
1327 1328

  Log_event_type get_type_code()
1329
  {
1330 1331 1332 1333 1334 1335 1336 1337
    return fake_base ? Load_log_event::get_type_code() : CREATE_FILE_EVENT;
  }
  int get_data_size()
  {
    return (fake_base ? Load_log_event::get_data_size() :
	    Load_log_event::get_data_size() +
	    4 + 1 + block_len);
  }
unknown's avatar
unknown committed
1338
  bool is_valid() const { return inited_from_old || block != 0; }
1339
#ifndef MYSQL_CLIENT
1340 1341
  bool write_data_header(IO_CACHE* file);
  bool write_data_body(IO_CACHE* file);
1342 1343 1344 1345
  /*
    Cut out Create_file extentions and
    write it as Load event - used on the slave
  */
1346
  bool write_base(IO_CACHE* file);
1347
#endif
1348 1349
};

1350

1351 1352 1353 1354 1355
/*****************************************************************************

  Append Block Log Event class

 ****************************************************************************/
1356

1357 1358 1359 1360 1361 1362
class Append_block_log_event: public Log_event
{
public:
  char* block;
  uint block_len;
  uint file_id;
unknown's avatar
unknown committed
1363
  /*
1364 1365 1366 1367 1368 1369 1370 1371 1372
    'db' is filled when the event is created in mysql_load() (the
    event needs to have a 'db' member to be well filtered by
    binlog-*-db rules). 'db' is not written to the binlog (it's not
    used by Append_block_log_event::write()), so it can't be read in
    the Append_block_log_event(const char* buf, int event_len)
    constructor.  In other words, 'db' is used only for filtering by
    binlog-*-db rules.  Create_file_log_event is different: it's 'db'
    (which is inherited from Load_log_event) is written to the binlog
    and can be re-read.
unknown's avatar
unknown committed
1373 1374 1375
  */
  const char* db;

1376
#ifndef MYSQL_CLIENT
unknown's avatar
unknown committed
1377
  Append_block_log_event(THD* thd, const char* db_arg, char* block_arg,
1378
			 uint block_len_arg, bool using_trans);
unknown's avatar
SCRUM  
unknown committed
1379
#ifdef HAVE_REPLICATION
1380
  int exec_event(struct st_relay_log_info* rli);
1381
  void pack_info(Protocol* protocol);
1382
  virtual int get_create_or_append() const;
unknown's avatar
SCRUM  
unknown committed
1383
#endif /* HAVE_REPLICATION */
1384
#else
1385
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
1386
#endif
unknown's avatar
unknown committed
1387 1388

  Append_block_log_event(const char* buf, uint event_len,
1389
                         const Format_description_log_event* description_event);
1390
  ~Append_block_log_event() {}
1391 1392
  Log_event_type get_type_code() { return APPEND_BLOCK_EVENT;}
  int get_data_size() { return  block_len + APPEND_BLOCK_HEADER_LEN ;}
unknown's avatar
unknown committed
1393
  bool is_valid() const { return block != 0; }
1394
#ifndef MYSQL_CLIENT
1395
  bool write(IO_CACHE* file);
1396
#endif
unknown's avatar
unknown committed
1397
  const char* get_db() { return db; }
1398 1399
};

1400

1401
/*****************************************************************************
1402

1403 1404 1405
  Delete File Log Event class

 ****************************************************************************/
1406

1407 1408 1409 1410
class Delete_file_log_event: public Log_event
{
public:
  uint file_id;
unknown's avatar
unknown committed
1411
  const char* db; /* see comment in Append_block_log_event */
unknown's avatar
unknown committed
1412

1413
#ifndef MYSQL_CLIENT
unknown's avatar
unknown committed
1414
  Delete_file_log_event(THD* thd, const char* db_arg, bool using_trans);
unknown's avatar
SCRUM  
unknown committed
1415
#ifdef HAVE_REPLICATION
1416
  void pack_info(Protocol* protocol);
1417
  int exec_event(struct st_relay_log_info* rli);
unknown's avatar
SCRUM  
unknown committed
1418
#endif /* HAVE_REPLICATION */
1419
#else
1420 1421
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
  void print(FILE* file, bool short_form, LAST_EVENT_INFO* last_event_info, bool enable_local);
unknown's avatar
unknown committed
1422 1423
#endif

1424 1425
  Delete_file_log_event(const char* buf, uint event_len,
                        const Format_description_log_event* description_event);
1426
  ~Delete_file_log_event() {}
1427 1428
  Log_event_type get_type_code() { return DELETE_FILE_EVENT;}
  int get_data_size() { return DELETE_FILE_HEADER_LEN ;}
unknown's avatar
unknown committed
1429
  bool is_valid() const { return file_id != 0; }
1430
#ifndef MYSQL_CLIENT
1431
  bool write(IO_CACHE* file);
1432
#endif
unknown's avatar
unknown committed
1433
  const char* get_db() { return db; }
1434 1435
};

1436

1437 1438 1439 1440 1441
/*****************************************************************************

  Execute Load Log Event class

 ****************************************************************************/
1442

1443 1444 1445 1446
class Execute_load_log_event: public Log_event
{
public:
  uint file_id;
unknown's avatar
unknown committed
1447
  const char* db; /* see comment in Append_block_log_event */
unknown's avatar
unknown committed
1448

1449
#ifndef MYSQL_CLIENT
unknown's avatar
unknown committed
1450
  Execute_load_log_event(THD* thd, const char* db_arg, bool using_trans);
unknown's avatar
SCRUM  
unknown committed
1451
#ifdef HAVE_REPLICATION
1452
  void pack_info(Protocol* protocol);
1453
  int exec_event(struct st_relay_log_info* rli);
unknown's avatar
SCRUM  
unknown committed
1454
#endif /* HAVE_REPLICATION */
1455
#else
1456
  void print(FILE* file, bool short_form = 0, LAST_EVENT_INFO* last_event_info= 0);
unknown's avatar
unknown committed
1457 1458 1459
#endif

  Execute_load_log_event(const char* buf, uint event_len,
1460
                         const Format_description_log_event* description_event);
1461
  ~Execute_load_log_event() {}
1462 1463
  Log_event_type get_type_code() { return EXEC_LOAD_EVENT;}
  int get_data_size() { return  EXEC_LOAD_HEADER_LEN ;}
unknown's avatar
unknown committed
1464
  bool is_valid() const { return file_id != 0; }
1465
#ifndef MYSQL_CLIENT
1466
  bool write(IO_CACHE* file);
1467
#endif
unknown's avatar
unknown committed
1468
  const char* get_db() { return db; }
unknown's avatar
unknown committed
1469 1470
};

1471

unknown's avatar
unknown committed
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
/***************************************************************************

  Begin load query Log Event class

  Event for the first block of file to be loaded, its only difference from
  Append_block event is that this event creates or truncates existing file
  before writing data.

****************************************************************************/
class Begin_load_query_log_event: public Append_block_log_event
{
public:
#ifndef MYSQL_CLIENT
  Begin_load_query_log_event(THD* thd_arg, const char *db_arg,
                             char* block_arg, uint block_len_arg,
                             bool using_trans);
#ifdef HAVE_REPLICATION
  Begin_load_query_log_event(THD* thd);
1490
  int get_create_or_append() const;
unknown's avatar
unknown committed
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
#endif /* HAVE_REPLICATION */
#endif
  Begin_load_query_log_event(const char* buf, uint event_len,
                             const Format_description_log_event* description_event);
  ~Begin_load_query_log_event() {}
  Log_event_type get_type_code() { return BEGIN_LOAD_QUERY_EVENT; }
};


/*
  Elements of this enum describe how LOAD DATA handles duplicates.
*/
enum enum_load_dup_handling { LOAD_DUP_ERROR= 0, LOAD_DUP_IGNORE,
                              LOAD_DUP_REPLACE };

/****************************************************************************

  Execute load query Log Event class

  Event responsible for LOAD DATA execution, it similar to Query_log_event
  but before executing the query it substitutes original filename in LOAD DATA
  query with name of temporary file.

****************************************************************************/
class Execute_load_query_log_event: public Query_log_event
{
public:
  uint file_id;       // file_id of temporary file
  uint fn_pos_start;  // pointer to the part of the query that should
                      // be substituted
  uint fn_pos_end;    // pointer to the end of this part of query
  /*
    We have to store type of duplicate handling explicitly, because
    for LOAD DATA it also depends on LOCAL option. And this part
    of query will be rewritten during replication so this information
    may be lost...
  */
  enum_load_dup_handling dup_handling;

#ifndef MYSQL_CLIENT
  Execute_load_query_log_event(THD* thd, const char* query_arg,
                       ulong query_length, uint fn_pos_start_arg,
                       uint fn_pos_end_arg,
                       enum_load_dup_handling dup_handling_arg,
                       bool using_trans, bool suppress_use);
#ifdef HAVE_REPLICATION
  void pack_info(Protocol* protocol);
  int exec_event(struct st_relay_log_info* rli);
#endif /* HAVE_REPLICATION */
#else
  void print(FILE* file, bool short_form = 0,
             LAST_EVENT_INFO* last_event_info= 0);
  /* Prints the query as LOAD DATA LOCAL and with rewritten filename */
  void print(FILE* file, bool short_form, LAST_EVENT_INFO* last_event_info,
             const char *local_fname);
#endif
  Execute_load_query_log_event(const char* buf, uint event_len,
                               const Format_description_log_event *description_event);
  ~Execute_load_query_log_event() {}

  Log_event_type get_type_code() { return EXECUTE_LOAD_QUERY_EVENT; }
  bool is_valid() const { return Query_log_event::is_valid() && file_id != 0; }

  ulong get_post_header_size_for_derived();
1555
#ifndef MYSQL_CLIENT
unknown's avatar
unknown committed
1556
  bool write_post_header_for_derived(IO_CACHE* file);
1557
#endif
unknown's avatar
unknown committed
1558 1559 1560
 };


1561 1562 1563 1564
#ifdef MYSQL_CLIENT
class Unknown_log_event: public Log_event
{
public:
1565 1566 1567 1568 1569 1570 1571
  /*
    Even if this is an unknown event, we still pass description_event to
    Log_event's ctor, this way we can extract maximum information from the
    event's header (the unique ID for example).
  */
  Unknown_log_event(const char* buf, const Format_description_log_event* description_event):
    Log_event(buf, description_event)
1572 1573
  {}
  ~Unknown_log_event() {}
1574
  void print(FILE* file, bool short_form= 0, LAST_EVENT_INFO* last_event_info= 0);
1575
  Log_event_type get_type_code() { return UNKNOWN_EVENT;}
unknown's avatar
unknown committed
1576
  bool is_valid() const { return 1; }
1577
};
unknown's avatar
unknown committed
1578
#endif
1579

1580
#endif /* _log_event_h */