~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/log_event.h

MergedĀ inĀ trunk.

Show diffs side-by-side

added added

removed removed

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