~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/log_event.h

  • Committer: Monty Taylor
  • Date: 2008-10-14 21:20:42 UTC
  • mto: (511.1.4 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081014212042-tef3njx3368b6lwt
Override copy ctr and op= because we have pointer members.

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