~ubuntu-branches/ubuntu/utopic/mariadb-5.5/utopic-security

« back to all changes in this revision

Viewing changes to .pc/90_spelling.diff/sql/log_event.h

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2014-08-27 21:12:36 UTC
  • mfrom: (2.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20140827211236-se41hwfe4xy0hpef
* d/control: Removed Provides: libmysqlclient-dev (Closes: #759309)
* d/control: Removed Provides: libmysqld-dev with same motivation
* Re-introduced tha HPPA build patch as the upstream fix wasn't complete
* Fixed all kFreeBSD build and test suite issues
* Added Italian translation (Closes: #759813)

Show diffs side-by-side

added added

removed removed

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