This commit is contained in:
2019-02-06 00:49:12 +03:00
commit 8dbb1bb605
4796 changed files with 506072 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
docs/log_event.h
docs/log_event.ph
experiments/cpptokenizer.pl
experiments/try.pl
lib/Mysql/BinLog.pm
lib/Mysql/BinLog/Constants.pm
lib/Mysql/BinLog/Events.pm
lib/Mysql/BinLog/Header.pm
lib/Mysql/BinLog/Net.pm
lib/Mysql/tmp
Makefile.PL
MANIFEST

View File

@@ -0,0 +1,11 @@
^#
\bCVS\b
^MANIFEST\.
^Makefile$
~$
\.html$
\.old$
^blib/
_blib$
^MakeMaker-\d
^\.exists

View File

@@ -0,0 +1,33 @@
#!/usr/bin/perl
#
# Perl Makefile for MySQL-BinLog
# $Id: Makefile.PL,v 1.2 2004/11/17 01:45:16 marksmith Exp $
#
# Invoke with 'perl Makefile.PL'
#
# See ExtUtils::MakeMaker (3) for more information on how to influence
# the contents of the Makefile that is written
#
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => 'MySQL::BinLog',
VERSION_FROM => 'lib/Mysql/BinLog.pm', # finds $VERSION
AUTHOR => 'Michael Granger <ged@danga.com>',
ABSTRACT => 'MySQL Replication Binlog Reader Library',
PREREQ_PM => {
'Net::MySQL' => 0,
'Scalar::Util' => 0,
fields => 0,
},
dist => {
CI => "cvs commit",
RCS_LABEL => 'cvs tag RELEASE_$(VERSION_SYM)',
SUFFIX => ".bz2",
DIST_DEFAULT => 'all tardist',
COMPRESS => "bzip2",
},
);

View File

@@ -0,0 +1,795 @@
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifndef _log_event_h
#define _log_event_h
#ifdef __EMX__
#undef write // remove pthread.h macro definition, conflict with write() class member
#endif
#if defined(__GNUC__) && !defined(MYSQL_CLIENT)
#pragma interface /* gcc class implementation */
#endif
#define LOG_READ_EOF -1
#define LOG_READ_BOGUS -2
#define LOG_READ_IO -3
#define LOG_READ_MEM -5
#define LOG_READ_TRUNC -6
#define LOG_READ_TOO_LARGE -7
#define LOG_EVENT_OFFSET 4
#define BINLOG_VERSION 3
/*
We could have used SERVER_VERSION_LENGTH, but this introduces an
obscure dependency - if somebody decided to change SERVER_VERSION_LENGTH
this would have broke the replication protocol
*/
#define ST_SERVER_VER_LEN 50
#define DUMPFILE_FLAG 0x1
#define OPT_ENCLOSED_FLAG 0x2
#define REPLACE_FLAG 0x4
#define IGNORE_FLAG 0x8
#define FIELD_TERM_EMPTY 0x1
#define ENCLOSED_EMPTY 0x2
#define LINE_TERM_EMPTY 0x4
#define LINE_START_EMPTY 0x8
#define ESCAPED_EMPTY 0x10
struct old_sql_ex
{
char field_term;
char enclosed;
char line_term;
char line_start;
char escaped;
char opt_flags;
char empty_flags;
};
#define NUM_LOAD_DELIM_STRS 5
struct sql_ex_info
{
char* field_term;
char* enclosed;
char* line_term;
char* line_start;
char* escaped;
int cached_new_format;
uint8 field_term_len,enclosed_len,line_term_len,line_start_len, escaped_len;
char opt_flags;
char empty_flags;
// store in new format even if old is possible
void force_new_format() { cached_new_format = 1;}
int data_size()
{
return (new_format() ?
field_term_len + enclosed_len + line_term_len +
line_start_len + escaped_len + 6 : 7);
}
int write_data(IO_CACHE* file);
char* init(char* buf,char* buf_end,bool use_new_format);
bool new_format()
{
return ((cached_new_format != -1) ? cached_new_format :
(cached_new_format=(field_term_len > 1 ||
enclosed_len > 1 ||
line_term_len > 1 || line_start_len > 1 ||
escaped_len > 1)));
}
};
/*
Binary log consists of events. Each event has a fixed length header,
followed by possibly variable ( depending on the type of event) length
data body. The data body consists of an optional fixed length segment
(post-header), and an optional variable length segment. See #defines and
comments below for the format specifics
*/
/* event-specific post-header sizes */
#define LOG_EVENT_HEADER_LEN 19
#define OLD_HEADER_LEN 13
#define QUERY_HEADER_LEN (4 + 4 + 1 + 2)
#define LOAD_HEADER_LEN (4 + 4 + 4 + 1 +1 + 4)
#define START_HEADER_LEN (2 + ST_SERVER_VER_LEN + 4)
#define ROTATE_HEADER_LEN 8
#define CREATE_FILE_HEADER_LEN 4
#define APPEND_BLOCK_HEADER_LEN 4
#define EXEC_LOAD_HEADER_LEN 4
#define DELETE_FILE_HEADER_LEN 4
/* event header offsets */
#define EVENT_TYPE_OFFSET 4
#define SERVER_ID_OFFSET 5
#define EVENT_LEN_OFFSET 9
#define LOG_POS_OFFSET 13
#define FLAGS_OFFSET 17
/* start event post-header */
#define ST_BINLOG_VER_OFFSET 0
#define ST_SERVER_VER_OFFSET 2
#define ST_CREATED_OFFSET (ST_SERVER_VER_OFFSET + ST_SERVER_VER_LEN)
/* slave event post-header */
#define SL_MASTER_PORT_OFFSET 8
#define SL_MASTER_POS_OFFSET 0
#define SL_MASTER_HOST_OFFSET 10
/* query event post-header */
#define Q_THREAD_ID_OFFSET 0
#define Q_EXEC_TIME_OFFSET 4
#define Q_DB_LEN_OFFSET 8
#define Q_ERR_CODE_OFFSET 9
#define Q_DATA_OFFSET QUERY_HEADER_LEN
/* Intvar event post-header */
#define I_TYPE_OFFSET 0
#define I_VAL_OFFSET 1
/* Rand event post-header */
#define RAND_SEED1_OFFSET 0
#define RAND_SEED2_OFFSET 8
/* Load event post-header */
#define L_THREAD_ID_OFFSET 0
#define L_EXEC_TIME_OFFSET 4
#define L_SKIP_LINES_OFFSET 8
#define L_TBL_LEN_OFFSET 12
#define L_DB_LEN_OFFSET 13
#define L_NUM_FIELDS_OFFSET 14
#define L_SQL_EX_OFFSET 18
#define L_DATA_OFFSET LOAD_HEADER_LEN
/* Rotate event post-header */
#define R_POS_OFFSET 0
#define R_IDENT_OFFSET 8
#define CF_FILE_ID_OFFSET 0
#define CF_DATA_OFFSET CREATE_FILE_HEADER_LEN
#define AB_FILE_ID_OFFSET 0
#define AB_DATA_OFFSET APPEND_BLOCK_HEADER_LEN
#define EL_FILE_ID_OFFSET 0
#define DF_FILE_ID_OFFSET 0
#define QUERY_EVENT_OVERHEAD (LOG_EVENT_HEADER_LEN+QUERY_HEADER_LEN)
#define QUERY_DATA_OFFSET (LOG_EVENT_HEADER_LEN+QUERY_HEADER_LEN)
#define ROTATE_EVENT_OVERHEAD (LOG_EVENT_HEADER_LEN+ROTATE_HEADER_LEN)
#define LOAD_EVENT_OVERHEAD (LOG_EVENT_HEADER_LEN+LOAD_HEADER_LEN)
#define CREATE_FILE_EVENT_OVERHEAD (LOG_EVENT_HEADER_LEN+\
+LOAD_HEADER_LEN+CREATE_FILE_HEADER_LEN)
#define DELETE_FILE_EVENT_OVERHEAD (LOG_EVENT_HEADER_LEN+DELETE_FILE_HEADER_LEN)
#define EXEC_LOAD_EVENT_OVERHEAD (LOG_EVENT_HEADER_LEN+EXEC_LOAD_HEADER_LEN)
#define APPEND_BLOCK_EVENT_OVERHEAD (LOG_EVENT_HEADER_LEN+APPEND_BLOCK_HEADER_LEN)
#define BINLOG_MAGIC "\xfe\x62\x69\x6e"
#define LOG_EVENT_TIME_F 0x1
#define LOG_EVENT_FORCED_ROTATE_F 0x2
enum Log_event_type
{
UNKNOWN_EVENT = 0, START_EVENT = 1, QUERY_EVENT =2, STOP_EVENT=3,
ROTATE_EVENT = 4, INTVAR_EVENT=5, LOAD_EVENT=6, SLAVE_EVENT=7,
CREATE_FILE_EVENT=8, APPEND_BLOCK_EVENT=9, EXEC_LOAD_EVENT=10,
DELETE_FILE_EVENT=11, NEW_LOAD_EVENT=12, RAND_EVENT=13
};
enum Int_event_type
{
INVALID_INT_EVENT = 0, LAST_INSERT_ID_EVENT = 1, INSERT_ID_EVENT = 2
};
#ifndef MYSQL_CLIENT
class String;
class MYSQL_LOG;
class THD;
#endif
struct st_relay_log_info;
class Log_event
{
public:
my_off_t log_pos;
char *temp_buf;
time_t when;
ulong exec_time;
uint32 server_id;
uint cached_event_len;
uint16 flags;
bool cache_stmt;
#ifndef MYSQL_CLIENT
THD* thd;
Log_event(THD* thd_arg, uint16 flags_arg, bool cache_stmt);
Log_event();
// if mutex is 0, the read will proceed without mutex
static Log_event* read_log_event(IO_CACHE* file,
pthread_mutex_t* log_lock,
bool old_format);
static int read_log_event(IO_CACHE* file, String* packet,
pthread_mutex_t* log_lock);
void set_log_pos(MYSQL_LOG* log);
virtual void pack_info(String* packet);
int net_send(THD* thd, const char* log_name, my_off_t pos);
static void init_show_field_list(List<Item>* field_list);
virtual int exec_event(struct st_relay_log_info* rli);
virtual const char* get_db()
{
return thd ? thd->db : 0;
}
#else
// avoid having to link mysqlbinlog against libpthread
static Log_event* read_log_event(IO_CACHE* file, bool old_format);
virtual void print(FILE* file, bool short_form = 0, char* last_db = 0) = 0;
void print_timestamp(FILE* file, time_t *ts = 0);
void print_header(FILE* file);
#endif
static void *operator new(size_t size)
{
return (void*) my_malloc((uint)size, MYF(MY_WME|MY_FAE));
}
static void operator delete(void *ptr, size_t size)
{
my_free((gptr) ptr, MYF(MY_WME|MY_ALLOW_ZERO_PTR));
}
int write(IO_CACHE* file);
int write_header(IO_CACHE* file);
virtual int write_data(IO_CACHE* file)
{ return write_data_header(file) || write_data_body(file); }
virtual int write_data_header(IO_CACHE* file __attribute__((unused)))
{ return 0; }
virtual int write_data_body(IO_CACHE* file __attribute__((unused)))
{ return 0; }
virtual Log_event_type get_type_code() = 0;
virtual bool is_valid() = 0;
inline bool get_cache_stmt() { return cache_stmt; }
Log_event(const char* buf, bool old_format);
virtual ~Log_event() { free_temp_buf();}
void register_temp_buf(char* buf) { temp_buf = buf; }
void free_temp_buf()
{
if (temp_buf)
{
my_free(temp_buf, MYF(0));
temp_buf = 0;
}
}
virtual int get_data_size() { return 0;}
virtual int get_data_body_offset() { return 0; }
int get_event_len()
{
return (cached_event_len ? cached_event_len :
(cached_event_len = LOG_EVENT_HEADER_LEN + get_data_size()));
}
static Log_event* read_log_event(const char* buf, int event_len,
const char **error, bool old_format);
const char* get_type_str();
};
class Query_log_event: public Log_event
{
protected:
char* data_buf;
public:
const char* query;
const char* db;
/*
If we already know the length of the query string
we pass it with q_len, so we would not have to call strlen()
otherwise, set it to 0, in which case, we compute it with strlen()
*/
uint32 q_len;
uint32 db_len;
uint16 error_code;
ulong thread_id;
/*
For events created by Query_log_event::exec_event (and
Load_log_event::exec_event()) we need the *original* thread id, to be able
to log the event with the original (=master's) thread id (fix for
BUG#1686).
*/
ulong slave_proxy_id;
#ifndef MYSQL_CLIENT
Query_log_event(THD* thd_arg, const char* query_arg, ulong query_length,
bool using_trans);
const char* get_db() { return db; }
void pack_info(String* packet);
int exec_event(struct st_relay_log_info* rli);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
#endif
Query_log_event(const char* buf, int event_len, bool old_format);
~Query_log_event()
{
if (data_buf)
{
my_free((gptr) data_buf, MYF(0));
}
}
Log_event_type get_type_code() { return QUERY_EVENT; }
int write(IO_CACHE* file);
int write_data(IO_CACHE* file); // returns 0 on success, -1 on error
bool is_valid() { return query != 0; }
int get_data_size()
{
return (q_len + db_len + 2
+ 4 // thread_id
+ 4 // exec_time
+ 2 // error_code
);
}
};
class Slave_log_event: public Log_event
{
protected:
char* mem_pool;
void init_from_mem_pool(int data_size);
public:
my_off_t master_pos;
char* master_host;
char* master_log;
int master_host_len;
int master_log_len;
uint16 master_port;
#ifndef MYSQL_CLIENT
Slave_log_event(THD* thd_arg, struct st_relay_log_info* rli);
void pack_info(String* packet);
int exec_event(struct st_relay_log_info* rli);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
#endif
Slave_log_event(const char* buf, int event_len);
~Slave_log_event();
int get_data_size();
bool is_valid() { return master_host != 0; }
Log_event_type get_type_code() { return SLAVE_EVENT; }
int write_data(IO_CACHE* file );
};
class Load_log_event: public Log_event
{
protected:
int copy_log_event(const char *buf, ulong event_len, bool old_format);
public:
ulong thread_id;
ulong slave_proxy_id;
uint32 table_name_len;
uint32 db_len;
uint32 fname_len;
uint32 num_fields;
const char* fields;
const uchar* field_lens;
uint32 field_block_len;
const char* table_name;
const char* db;
const char* fname;
uint32 skip_lines;
sql_ex_info sql_ex;
bool local_fname;
/* fname doesn't point to memory inside Log_event::temp_buf */
void set_fname_outside_temp_buf(const char *afname, uint alen)
{
fname= afname;
fname_len= alen;
local_fname= true;
}
/* fname doesn't point to memory inside Log_event::temp_buf */
int check_fname_outside_temp_buf()
{
return local_fname;
}
#ifndef MYSQL_CLIENT
String field_lens_buf;
String fields_buf;
Load_log_event(THD* thd, sql_exchange* ex, const char* db_arg,
const char* table_name_arg,
List<Item>& fields_arg, enum enum_duplicates handle_dup,
bool using_trans);
void set_fields(List<Item> &fields_arg);
void pack_info(String* packet);
const char* get_db() { return db; }
int exec_event(struct st_relay_log_info* rli)
{
return exec_event(thd->slave_net,rli,0);
}
int exec_event(NET* net, struct st_relay_log_info* rli,
bool use_rli_only_for_errors);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
void print(FILE* file, bool short_form, char* last_db, bool commented);
#endif
Load_log_event(const char* buf, int event_len, bool old_format);
~Load_log_event()
{}
Log_event_type get_type_code()
{
return sql_ex.new_format() ? NEW_LOAD_EVENT: LOAD_EVENT;
}
int write_data_header(IO_CACHE* file);
int write_data_body(IO_CACHE* file);
bool is_valid() { return table_name != 0; }
int get_data_size()
{
return (table_name_len + 2 + db_len + 2 + fname_len
+ 4 // thread_id
+ 4 // exec_time
+ 4 // skip_lines
+ 4 // field block len
+ sql_ex.data_size() + field_block_len + num_fields);
}
int get_data_body_offset() { return LOAD_EVENT_OVERHEAD; }
};
extern char server_version[SERVER_VERSION_LENGTH];
class Start_log_event: public Log_event
{
public:
/*
If this event is at the start of the first binary log since server startup
'created' should be the timestamp when the event (and the binary log) was
created.
In the other case (i.e. this event is at the start of a binary log created
by FLUSH LOGS or automatic rotation), 'created' should be 0.
This "trick" is used by MySQL >=4.0.14 slaves to know if they must drop the
stale temporary tables or not.
Note that when 'created'!=0, it is always equal to the event's timestamp;
indeed Start_log_event is written only in log.cc where the first
constructor below is called, in which 'created' is set to 'when'.
So in fact 'created' is a useless variable. When it is 0
we can read the actual value from timestamp ('when') and when it is
non-zero we can read the same value from timestamp ('when'). Conclusion:
- we use timestamp to print when the binlog was created.
- we use 'created' only to know if this is a first binlog or not.
In 3.23.57 we did not pay attention to this identity, so mysqlbinlog in
3.23.57 does not print 'created the_date' if created was zero. This is now
fixed.
*/
time_t created;
uint16 binlog_version;
char server_version[ST_SERVER_VER_LEN];
#ifndef MYSQL_CLIENT
Start_log_event() :Log_event(), binlog_version(BINLOG_VERSION)
{
created = (time_t) when;
memcpy(server_version, ::server_version, ST_SERVER_VER_LEN);
}
void pack_info(String* packet);
int exec_event(struct st_relay_log_info* rli);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
#endif
Start_log_event(const char* buf, bool old_format);
~Start_log_event() {}
Log_event_type get_type_code() { return START_EVENT;}
int write_data(IO_CACHE* file);
bool is_valid() { return 1; }
int get_data_size()
{
return START_HEADER_LEN;
}
};
class Intvar_log_event: public Log_event
{
public:
ulonglong val;
uchar type;
#ifndef MYSQL_CLIENT
Intvar_log_event(THD* thd_arg,uchar type_arg, ulonglong val_arg)
:Log_event(thd_arg,0,0),val(val_arg),type(type_arg)
{}
void pack_info(String* packet);
int exec_event(struct st_relay_log_info* rli);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
#endif
Intvar_log_event(const char* buf, bool old_format);
~Intvar_log_event() {}
Log_event_type get_type_code() { return INTVAR_EVENT;}
const char* get_var_type_name();
int get_data_size() { return sizeof(type) + sizeof(val);}
int write_data(IO_CACHE* file);
bool is_valid() { return 1; }
};
/*****************************************************************************
*
* Rand log event class
*
****************************************************************************/
class Rand_log_event: public Log_event
{
public:
ulonglong seed1;
ulonglong seed2;
#ifndef MYSQL_CLIENT
Rand_log_event(THD* thd_arg, ulonglong seed1_arg, ulonglong seed2_arg)
:Log_event(thd_arg,0,0),seed1(seed1_arg),seed2(seed2_arg)
{}
void pack_info(String* packet);
int exec_event(struct st_relay_log_info* rli);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
#endif
Rand_log_event(const char* buf, bool old_format);
~Rand_log_event() {}
Log_event_type get_type_code() { return RAND_EVENT;}
int get_data_size() { return sizeof(ulonglong) * 2; }
int write_data(IO_CACHE* file);
bool is_valid() { return 1; }
};
class Stop_log_event: public Log_event
{
public:
#ifndef MYSQL_CLIENT
Stop_log_event() :Log_event()
{}
int exec_event(struct st_relay_log_info* rli);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
#endif
Stop_log_event(const char* buf, bool old_format):
Log_event(buf, old_format)
{}
~Stop_log_event() {}
Log_event_type get_type_code() { return STOP_EVENT;}
bool is_valid() { return 1; }
};
class Rotate_log_event: public Log_event
{
public:
const char* new_log_ident;
ulonglong pos;
uint ident_len;
bool alloced;
#ifndef MYSQL_CLIENT
Rotate_log_event(THD* thd_arg, const char* new_log_ident_arg,
uint ident_len_arg = 0,
ulonglong pos_arg = LOG_EVENT_OFFSET)
:Log_event(), new_log_ident(new_log_ident_arg),
pos(pos_arg),ident_len(ident_len_arg ? ident_len_arg :
(uint) strlen(new_log_ident_arg)), alloced(0)
{}
void pack_info(String* packet);
int exec_event(struct st_relay_log_info* rli);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
#endif
Rotate_log_event(const char* buf, int event_len, bool old_format);
~Rotate_log_event()
{
if (alloced)
my_free((gptr) new_log_ident, MYF(0));
}
Log_event_type get_type_code() { return ROTATE_EVENT;}
int get_data_size() { return ident_len + ROTATE_HEADER_LEN;}
bool is_valid() { return new_log_ident != 0; }
int write_data(IO_CACHE* file);
};
/* the classes below are for the new LOAD DATA INFILE logging */
class Create_file_log_event: public Load_log_event
{
protected:
/*
Pretend we are Load event, so we can write out just
our Load part - used on the slave when writing event out to
SQL_LOAD-*.info file
*/
bool fake_base;
public:
char* block;
const char *event_buf;
uint block_len;
uint file_id;
bool inited_from_old;
#ifndef MYSQL_CLIENT
Create_file_log_event(THD* thd, sql_exchange* ex, const char* db_arg,
const char* table_name_arg,
List<Item>& fields_arg,
enum enum_duplicates handle_dup,
char* block_arg, uint block_len_arg,
bool using_trans);
void pack_info(String* packet);
int exec_event(struct st_relay_log_info* rli);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
void print(FILE* file, bool short_form, char* last_db, bool enable_local);
#endif
Create_file_log_event(const char* buf, int event_len, bool old_format);
~Create_file_log_event()
{
my_free((char*) event_buf, MYF(MY_ALLOW_ZERO_PTR));
}
Log_event_type get_type_code()
{
return fake_base ? Load_log_event::get_type_code() : CREATE_FILE_EVENT;
}
int get_data_size()
{
return (fake_base ? Load_log_event::get_data_size() :
Load_log_event::get_data_size() +
4 + 1 + block_len);
}
int get_data_body_offset()
{
return (fake_base ? LOAD_EVENT_OVERHEAD:
LOAD_EVENT_OVERHEAD + CREATE_FILE_HEADER_LEN);
}
bool is_valid() { return inited_from_old || block != 0; }
int write_data_header(IO_CACHE* file);
int write_data_body(IO_CACHE* file);
/*
Cut out Create_file extentions and
write it as Load event - used on the slave
*/
int write_base(IO_CACHE* file);
};
class Append_block_log_event: public Log_event
{
public:
char* block;
uint block_len;
uint file_id;
/*
'db' is filled when the event is created in mysql_load() (the event needs to
have a 'db' member to be well filtered by binlog-*-db rules). 'db' is not
written to the binlog (it's not used by Append_block_log_event::write()), so
it can't be read in the Append_block_log_event(const char* buf, int
event_len) constructor.
In other words, 'db' is used only for filtering by binlog-*-db rules.
Create_file_log_event is different: its 'db' (which is inherited from
Load_log_event) is written to the binlog and can be re-read.
*/
const char* db;
#ifndef MYSQL_CLIENT
Append_block_log_event(THD* thd, const char* db_arg, char* block_arg,
uint block_len_arg, bool using_trans);
int exec_event(struct st_relay_log_info* rli);
void pack_info(String* packet);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
#endif
Append_block_log_event(const char* buf, int event_len);
~Append_block_log_event() {}
Log_event_type get_type_code() { return APPEND_BLOCK_EVENT;}
int get_data_size() { return block_len + APPEND_BLOCK_HEADER_LEN ;}
bool is_valid() { return block != 0; }
int write_data(IO_CACHE* file);
const char* get_db() { return db; }
};
class Delete_file_log_event: public Log_event
{
public:
uint file_id;
const char* db; /* see comment in Append_block_log_event */
#ifndef MYSQL_CLIENT
Delete_file_log_event(THD* thd, const char* db_arg, bool using_trans);
void pack_info(String* packet);
int exec_event(struct st_relay_log_info* rli);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
#endif
Delete_file_log_event(const char* buf, int event_len);
~Delete_file_log_event() {}
Log_event_type get_type_code() { return DELETE_FILE_EVENT;}
int get_data_size() { return DELETE_FILE_HEADER_LEN ;}
bool is_valid() { return file_id != 0; }
int write_data(IO_CACHE* file);
const char* get_db() { return db; }
};
class Execute_load_log_event: public Log_event
{
public:
uint file_id;
const char* db; /* see comment in Append_block_log_event */
#ifndef MYSQL_CLIENT
Execute_load_log_event(THD* thd, const char* db_arg, bool using_trans);
void pack_info(String* packet);
int exec_event(struct st_relay_log_info* rli);
#else
void print(FILE* file, bool short_form = 0, char* last_db = 0);
#endif
Execute_load_log_event(const char* buf, int event_len);
~Execute_load_log_event() {}
Log_event_type get_type_code() { return EXEC_LOAD_EVENT;}
int get_data_size() { return EXEC_LOAD_HEADER_LEN ;}
bool is_valid() { return file_id != 0; }
int write_data(IO_CACHE* file);
const char* get_db() { return db; }
};
#ifdef MYSQL_CLIENT
class Unknown_log_event: public Log_event
{
public:
Unknown_log_event(const char* buf, bool old_format):
Log_event(buf, old_format)
{}
~Unknown_log_event() {}
void print(FILE* file, bool short_form= 0, char* last_db= 0);
Log_event_type get_type_code() { return UNKNOWN_EVENT;}
bool is_valid() { return 1; }
};
#endif
#endif /* _log_event_h */

View File

@@ -0,0 +1,101 @@
#!/usr/bin/perl -w
#require '_h2ph_pre.ph';
unless(defined(&_log_event_h)) {
eval 'sub _log_event_h () {1;}' unless defined(&_log_event_h);
eval 'sub LOG_READ_EOF () {-1;}' unless defined(&LOG_READ_EOF);
eval 'sub LOG_READ_BOGUS () {-2;}' unless defined(&LOG_READ_BOGUS);
eval 'sub LOG_READ_IO () {-3;}' unless defined(&LOG_READ_IO);
eval 'sub LOG_READ_MEM () {-5;}' unless defined(&LOG_READ_MEM);
eval 'sub LOG_READ_TRUNC () {-6;}' unless defined(&LOG_READ_TRUNC);
eval 'sub LOG_READ_TOO_LARGE () {-7;}' unless defined(&LOG_READ_TOO_LARGE);
eval 'sub LOG_EVENT_OFFSET () {4;}' unless defined(&LOG_EVENT_OFFSET);
eval 'sub BINLOG_VERSION () {3;}' unless defined(&BINLOG_VERSION);
eval 'sub ST_SERVER_VER_LEN () {50;}' unless defined(&ST_SERVER_VER_LEN);
eval 'sub DUMPFILE_FLAG () {0x1;}' unless defined(&DUMPFILE_FLAG);
eval 'sub OPT_ENCLOSED_FLAG () {0x2;}' unless defined(&OPT_ENCLOSED_FLAG);
eval 'sub REPLACE_FLAG () {0x4;}' unless defined(&REPLACE_FLAG);
eval 'sub IGNORE_FLAG () {0x8;}' unless defined(&IGNORE_FLAG);
eval 'sub FIELD_TERM_EMPTY () {0x1;}' unless defined(&FIELD_TERM_EMPTY);
eval 'sub ENCLOSED_EMPTY () {0x2;}' unless defined(&ENCLOSED_EMPTY);
eval 'sub LINE_TERM_EMPTY () {0x4;}' unless defined(&LINE_TERM_EMPTY);
eval 'sub LINE_START_EMPTY () {0x8;}' unless defined(&LINE_START_EMPTY);
eval 'sub ESCAPED_EMPTY () {0x10;}' unless defined(&ESCAPED_EMPTY);
eval 'sub NUM_LOAD_DELIM_STRS () {5;}' unless defined(&NUM_LOAD_DELIM_STRS);
eval 'sub LOG_EVENT_HEADER_LEN () {19;}' unless defined(&LOG_EVENT_HEADER_LEN);
eval 'sub OLD_HEADER_LEN () {13;}' unless defined(&OLD_HEADER_LEN);
eval 'sub QUERY_HEADER_LEN () {(4+ 4+ 1+ 2);}' unless defined(&QUERY_HEADER_LEN);
eval 'sub LOAD_HEADER_LEN () {(4+ 4+ 4+ 1+1+ 4);}' unless defined(&LOAD_HEADER_LEN);
eval 'sub START_HEADER_LEN () {(2+ &ST_SERVER_VER_LEN + 4);}' unless defined(&START_HEADER_LEN);
eval 'sub ROTATE_HEADER_LEN () {8;}' unless defined(&ROTATE_HEADER_LEN);
eval 'sub CREATE_FILE_HEADER_LEN () {4;}' unless defined(&CREATE_FILE_HEADER_LEN);
eval 'sub APPEND_BLOCK_HEADER_LEN () {4;}' unless defined(&APPEND_BLOCK_HEADER_LEN);
eval 'sub EXEC_LOAD_HEADER_LEN () {4;}' unless defined(&EXEC_LOAD_HEADER_LEN);
eval 'sub DELETE_FILE_HEADER_LEN () {4;}' unless defined(&DELETE_FILE_HEADER_LEN);
eval 'sub EVENT_TYPE_OFFSET () {4;}' unless defined(&EVENT_TYPE_OFFSET);
eval 'sub SERVER_ID_OFFSET () {5;}' unless defined(&SERVER_ID_OFFSET);
eval 'sub EVENT_LEN_OFFSET () {9;}' unless defined(&EVENT_LEN_OFFSET);
eval 'sub LOG_POS_OFFSET () {13;}' unless defined(&LOG_POS_OFFSET);
eval 'sub FLAGS_OFFSET () {17;}' unless defined(&FLAGS_OFFSET);
eval 'sub ST_BINLOG_VER_OFFSET () {0;}' unless defined(&ST_BINLOG_VER_OFFSET);
eval 'sub ST_SERVER_VER_OFFSET () {2;}' unless defined(&ST_SERVER_VER_OFFSET);
eval 'sub ST_CREATED_OFFSET () {( &ST_SERVER_VER_OFFSET + &ST_SERVER_VER_LEN);}' unless defined(&ST_CREATED_OFFSET);
eval 'sub SL_MASTER_PORT_OFFSET () {8;}' unless defined(&SL_MASTER_PORT_OFFSET);
eval 'sub SL_MASTER_POS_OFFSET () {0;}' unless defined(&SL_MASTER_POS_OFFSET);
eval 'sub SL_MASTER_HOST_OFFSET () {10;}' unless defined(&SL_MASTER_HOST_OFFSET);
eval 'sub Q_THREAD_ID_OFFSET () {0;}' unless defined(&Q_THREAD_ID_OFFSET);
eval 'sub Q_EXEC_TIME_OFFSET () {4;}' unless defined(&Q_EXEC_TIME_OFFSET);
eval 'sub Q_DB_LEN_OFFSET () {8;}' unless defined(&Q_DB_LEN_OFFSET);
eval 'sub Q_ERR_CODE_OFFSET () {9;}' unless defined(&Q_ERR_CODE_OFFSET);
eval 'sub Q_DATA_OFFSET () { &QUERY_HEADER_LEN;}' unless defined(&Q_DATA_OFFSET);
eval 'sub I_TYPE_OFFSET () {0;}' unless defined(&I_TYPE_OFFSET);
eval 'sub I_VAL_OFFSET () {1;}' unless defined(&I_VAL_OFFSET);
eval 'sub RAND_SEED1_OFFSET () {0;}' unless defined(&RAND_SEED1_OFFSET);
eval 'sub RAND_SEED2_OFFSET () {8;}' unless defined(&RAND_SEED2_OFFSET);
eval 'sub L_THREAD_ID_OFFSET () {0;}' unless defined(&L_THREAD_ID_OFFSET);
eval 'sub L_EXEC_TIME_OFFSET () {4;}' unless defined(&L_EXEC_TIME_OFFSET);
eval 'sub L_SKIP_LINES_OFFSET () {8;}' unless defined(&L_SKIP_LINES_OFFSET);
eval 'sub L_TBL_LEN_OFFSET () {12;}' unless defined(&L_TBL_LEN_OFFSET);
eval 'sub L_DB_LEN_OFFSET () {13;}' unless defined(&L_DB_LEN_OFFSET);
eval 'sub L_NUM_FIELDS_OFFSET () {14;}' unless defined(&L_NUM_FIELDS_OFFSET);
eval 'sub L_SQL_EX_OFFSET () {18;}' unless defined(&L_SQL_EX_OFFSET);
eval 'sub L_DATA_OFFSET () { &LOAD_HEADER_LEN;}' unless defined(&L_DATA_OFFSET);
eval 'sub R_POS_OFFSET () {0;}' unless defined(&R_POS_OFFSET);
eval 'sub R_IDENT_OFFSET () {8;}' unless defined(&R_IDENT_OFFSET);
eval 'sub CF_FILE_ID_OFFSET () {0;}' unless defined(&CF_FILE_ID_OFFSET);
eval 'sub CF_DATA_OFFSET () { &CREATE_FILE_HEADER_LEN;}' unless defined(&CF_DATA_OFFSET);
eval 'sub AB_FILE_ID_OFFSET () {0;}' unless defined(&AB_FILE_ID_OFFSET);
eval 'sub AB_DATA_OFFSET () { &APPEND_BLOCK_HEADER_LEN;}' unless defined(&AB_DATA_OFFSET);
eval 'sub EL_FILE_ID_OFFSET () {0;}' unless defined(&EL_FILE_ID_OFFSET);
eval 'sub DF_FILE_ID_OFFSET () {0;}' unless defined(&DF_FILE_ID_OFFSET);
eval 'sub QUERY_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &QUERY_HEADER_LEN);}' unless defined(&QUERY_EVENT_OVERHEAD);
eval 'sub QUERY_DATA_OFFSET () {( &LOG_EVENT_HEADER_LEN+ &QUERY_HEADER_LEN);}' unless defined(&QUERY_DATA_OFFSET);
eval 'sub ROTATE_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &ROTATE_HEADER_LEN);}' unless defined(&ROTATE_EVENT_OVERHEAD);
eval 'sub LOAD_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &LOAD_HEADER_LEN);}' unless defined(&LOAD_EVENT_OVERHEAD);
eval 'sub CREATE_FILE_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ + &LOAD_HEADER_LEN+ &CREATE_FILE_HEADER_LEN);}' unless defined(&CREATE_FILE_EVENT_OVERHEAD);
eval 'sub DELETE_FILE_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &DELETE_FILE_HEADER_LEN);}' unless defined(&DELETE_FILE_EVENT_OVERHEAD);
eval 'sub EXEC_LOAD_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &EXEC_LOAD_HEADER_LEN);}' unless defined(&EXEC_LOAD_EVENT_OVERHEAD);
eval 'sub APPEND_BLOCK_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &APPEND_BLOCK_HEADER_LEN);}' unless defined(&APPEND_BLOCK_EVENT_OVERHEAD);
eval 'sub BINLOG_MAGIC () {"\\xfe\\x62\\x69\\x6e";}' unless defined(&BINLOG_MAGIC);
eval 'sub LOG_EVENT_TIME_F () {0x1;}' unless defined(&LOG_EVENT_TIME_F);
eval 'sub LOG_EVENT_FORCED_ROTATE_F () {0x2;}' unless defined(&LOG_EVENT_FORCED_ROTATE_F);
eval("sub UNKNOWN_EVENT () { 0; }") unless defined(&UNKNOWN_EVENT);
eval("sub START_EVENT () { 1; }") unless defined(&START_EVENT);
eval("sub QUERY_EVENT () { 2; }") unless defined(&QUERY_EVENT);
eval("sub STOP_EVENT () { 3; }") unless defined(&STOP_EVENT);
eval("sub ROTATE_EVENT () { 4; }") unless defined(&ROTATE_EVENT);
eval("sub INTVAR_EVENT () { 5; }") unless defined(&INTVAR_EVENT);
eval("sub LOAD_EVENT () { 6; }") unless defined(&LOAD_EVENT);
eval("sub SLAVE_EVENT () { 7; }") unless defined(&SLAVE_EVENT);
eval("sub CREATE_FILE_EVENT () { 8; }") unless defined(&CREATE_FILE_EVENT);
eval("sub APPEND_BLOCK_EVENT () { 9; }") unless defined(&APPEND_BLOCK_EVENT);
eval("sub EXEC_LOAD_EVENT () { 10; }") unless defined(&EXEC_LOAD_EVENT);
eval("sub DELETE_FILE_EVENT () { 11; }") unless defined(&DELETE_FILE_EVENT);
eval("sub NEW_LOAD_EVENT () { 12; }") unless defined(&NEW_LOAD_EVENT);
eval("sub RAND_EVENT () { 13; }") unless defined(&RAND_EVENT);
eval("sub INVALID_INT_EVENT () { 0; }") unless defined(&INVALID_INT_EVENT);
eval("sub LAST_INSERT_ID_EVENT () { 1; }") unless defined(&LAST_INSERT_ID_EVENT);
eval("sub INSERT_ID_EVENT () { 2; }") unless defined(&INSERT_ID_EVENT);
}
1;

View File

@@ -0,0 +1,38 @@
#!/usr/bin/perl -w
#
# Learning Text::CPP... conclusion: not what I need.
#
#
package cpptokenizer;
use Text::CPP;
use Data::Dumper;
$Data::Dumper::TERSE = 1;
$Data::Dumper::INDENT = 1;
my $reader = new Text::CPP ( Language => "GNUC99" );
my ( $text, $type, $prettytype, $flags );
foreach my $file ( @ARGV ) {
print "File: $file\n", '-' x 70, "\n";
$reader->read( $file );
#print join("\n", $reader->tokens);
while ( ($text, $type, $flags) = $reader->token ) {
$prettytype = $reader->type( $type );
chomp( $text );
#print "$prettytype: $text ($type) +$flags\n";
print Data::Dumper->Dumpxs( [$text,$type,$flags,$prettytype],
[qw{text type flags prettytype}] ), "\n";
print "---\n";
}
print "\n\n";
}

View File

@@ -0,0 +1,34 @@
#!/usr/bin/perl -w
package try;
use strict;
BEGIN {
use lib qw{lib};
use MySQL::BinLog;
}
my %connect_params = (
hostname => 'whitaker.lj',
database => 'livejournal',
user => 'slave',
password => 'm&s',
port => 3337,
debug => 1,
log_slave_id => 512,
);
sub handler {
my $ev = shift;
print( ('-' x 70), "\n",
">>> QUERY: ", $ev->query_data, "\n",
('-' x 70), "\n" );
}
my $filename = shift @ARGV;
my $log = MySQL::BinLog->open( $filename );
#my $log = MySQL::BinLog->connect( %connect_params );
my @res = $log->handle_events( \&handler, MySQL::QUERY_EVENT );

View File

@@ -0,0 +1,244 @@
#!/usr/bin/perl
##############################################################################
=head1 NAME
MySQL::BinLog - Binary log parser classes
=head1 SYNOPSIS
use MySQL::BinLog ();
my $log = MySQL::BinLog->open( "Foo-relay.bin.001" );
# -or-
die unless $MySQL::BinLog::HaveNet;
my $log = MySQL::BinLog->connect(
hostname => 'db.example.com',
database => 'sales',
user => 'salesapp',
password => '',
port => 3337,
log_name => '',
log_pos => 4,
log_slave_id => 10,
);
$log->handle_events( \&print_queries, MySQL::BinLog::QUERY_EVENT );
sub print_queries {
my $ev = shift;
print "Query: ", $ev->query_data, "\n";
}
=head1 REQUIRES
I<Token requires line>
=head1 DESCRIPTION
This is a collection of Perl classes for parsing a MySQL binlog.
=head1 AUTHOR
Michael Granger <ged@FaerieMUD.org>
Copyright (c) 2004 Danga Interactive. All rights reserved.
This module is free software. You may use, modify, and/or redistribute this
software under the terms of the Perl Artistic License. (See
http://language.perl.com/misc/Artistic.html)
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
FITNESS FOR A PARTICULAR PURPOSE.
=cut
##############################################################################
package MySQL::BinLog;
use strict;
use warnings qw{all};
BEGIN {
# Versioning stuff
use vars qw{$VERSION $RCSID};
$VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
$RCSID = q$Id: BinLog.pm,v 1.2 2004/11/17 21:58:39 marksmith Exp $;
use constant TRUE => 1;
use constant FALSE => 0;
# Subordinate classes
use MySQL::BinLog::Constants qw{};
use MySQL::BinLog::Events qw{};
use MySQL::BinLog::Header qw{};
# Try to load Net::MySQL, but no worries if we can't until they try to use
# ->connect.
use vars qw{$HaveNet $NetError};
$HaveNet = eval { require MySQL::BinLog::Net; 1 };
$NetError = $@;
use Carp qw{croak confess carp};
use IO::File qw{};
use Fcntl qw{O_RDONLY};
}
### (CONSTRUCTOR) METHOD: new
### Return a new generic MySQL::BinLog object.
sub new {
my $class = shift or confess "Cannot be used as a function";
return bless {
fh => undef,
type => undef,
}, $class;
}
### (CONSTRUCTOR) METHOD: open( $filename )
### Return a MySQL::BinLog object that will read events from the file specified
### by I<filename>.
sub open {
my $class = shift or confess "Cannot be used as a function";
my $filename = shift or croak "Missing argument: filename";
my $ifh = new IO::File $filename, O_RDONLY
or croak "open: $filename: $!";
$ifh->seek( 4, 0 );
my $self = $class->new;
$self->{fh} = $ifh;
return $self;
}
### (CONSTRUCTOR) METHOD: connect( %connect_params )
### Open a connection to a MySQL server over the network and read events from
### it. The connection parameters are the same as those passed to Net::MySQL. If
### Net::MySQL is not installed, this method will raise an exception.
sub connect {
my $class = shift or confess "Cannot be used as a function";
my %connect_params = @_;
croak "Net::MySQL not available: $NetError" unless $HaveNet;
my $self = $class->new;
my (
$logname,
$pos,
$slave_id,
);
$logname = delete $connect_params{log_name} || '';
$pos = delete $connect_params{log_pos} || 0;
$slave_id = delete $connect_params{log_slave_id} || 128;
$self->{net} = new MySQL::BinLog::Net ( %connect_params );
$self->{net}->start_binlog( $slave_id, $logname, $pos );
return $self;
}
#####################################################################
### I N S T A N C E M E T H O D S
#####################################################################
### METHOD: read_next_event()
### Read the next event from the registered source and return it.
sub read_next_event {
my $self = shift;
# :FIXME: This is some ugly inexcusably ugly shit, but I'm hacking all the
# IO into here to get something working, but it really should be made
# cleaner by separating out the socket IO routine into the ::Net class and
# the file IO into a new File class that reads from a file in an optimized
# fashion.
my $event_data;
# Reading from a file -- have to read the header, figure out the length of
# the rest of the event data, then read the rest.
if ( $self->{fh} ) {
$event_data = $self->readbytes( $self->{fh}, MySQL::LOG_EVENT_HEADER_LEN );
my $len = unpack( 'V', substr($event_data, 9, 4) );
$event_data .= $self->readbytes( $self->{fh},
$len - MySQL::LOG_EVENT_HEADER_LEN );
}
# Reading from a real master
elsif ( $self->{net} ) {
$event_data = $self->{net}->read_packet;
}
# An object without a reader
else {
croak "Cannot read without an event source.";
}
# Let the event class parse the event
return MySQL::BinLog::Event->read_event( $event_data );
}
### METHOD: handle_events( \&handler[, @types] )
### Start reading events from whatever source is registered, handling those of
### the types specified in I<types> with the given I<handler>. If no I<types>
### are given, all events will be sent to the I<handler>. Events are sent as
### instances of the MySQL::BinLog::Event classes.
sub handle_events {
my $self = shift or croak "Cannot be used as a function.";
my ( $handler, @types ) = @_;
my @rv = ();
while (( my $event = $self->read_next_event )) {
my $etype = $event->header->event_type;
next if @types && !grep { $etype == $_ } @types;
push @rv, $handler->( $event );
}
return @rv;
}
### FUNCTION: readbytes( $fh, $len )
### Read and return I<len> bytes from the specified I<fh>.
sub readbytes {
my ( $self, $fh, $len ) = @_;
my ( $buf, $rval, $bytes ) = ('', '', 0);
until ( length $rval == $len ) {
$bytes = $fh->read( $buf, $len - length $rval );
if ( !defined $bytes ) {
if ( $!{EAGAIN} ) { next }
die "Read error: $!";
} elsif ( !$bytes && $fh->eof ) {
die "EOF before reading $len bytes.\n";
}
$rval .= $buf;
}
return $rval;
}
### Destructors
DESTROY {}
END {}
1;

View File

@@ -0,0 +1,105 @@
package MySQL;
BEGIN {
no warnings 'redefine';
sub LOG_READ_EOF () {-1;}
sub LOG_READ_BOGUS () {-2;}
sub LOG_READ_IO () {-3;}
sub LOG_READ_MEM () {-5;}
sub LOG_READ_TRUNC () {-6;}
sub LOG_READ_TOO_LARGE () {-7;}
sub LOG_EVENT_OFFSET () {4;}
sub BINLOG_VERSION () {3;}
sub ST_SERVER_VER_LEN () {50;}
sub DUMPFILE_FLAG () {0x1;}
sub OPT_ENCLOSED_FLAG () {0x2;}
sub REPLACE_FLAG () {0x4;}
sub IGNORE_FLAG () {0x8;}
sub FIELD_TERM_EMPTY () {0x1;}
sub ENCLOSED_EMPTY () {0x2;}
sub LINE_TERM_EMPTY () {0x4;}
sub LINE_START_EMPTY () {0x8;}
sub ESCAPED_EMPTY () {0x10;}
sub NUM_LOAD_DELIM_STRS () {5;}
sub LOG_EVENT_HEADER_LEN () {19;}
sub OLD_HEADER_LEN () {13;}
sub QUERY_HEADER_LEN () {(4+ 4+ 1+ 2);}
sub LOAD_HEADER_LEN () {(4+ 4+ 4+ 1+1+ 4);}
sub START_HEADER_LEN () {(2+ &ST_SERVER_VER_LEN + 4);}
sub ROTATE_HEADER_LEN () {8;}
sub CREATE_FILE_HEADER_LEN () {4;}
sub APPEND_BLOCK_HEADER_LEN () {4;}
sub EXEC_LOAD_HEADER_LEN () {4;}
sub DELETE_FILE_HEADER_LEN () {4;}
sub EVENT_TYPE_OFFSET () {4;}
sub SERVER_ID_OFFSET () {5;}
sub EVENT_LEN_OFFSET () {9;}
sub LOG_POS_OFFSET () {13;}
sub FLAGS_OFFSET () {17;}
sub ST_BINLOG_VER_OFFSET () {0;}
sub ST_SERVER_VER_OFFSET () {2;}
sub ST_CREATED_OFFSET () {( &ST_SERVER_VER_OFFSET + &ST_SERVER_VER_LEN);}
sub SL_MASTER_PORT_OFFSET () {8;}
sub SL_MASTER_POS_OFFSET () {0;}
sub SL_MASTER_HOST_OFFSET () {10;}
sub Q_THREAD_ID_OFFSET () {0;}
sub Q_EXEC_TIME_OFFSET () {4;}
sub Q_DB_LEN_OFFSET () {8;}
sub Q_ERR_CODE_OFFSET () {9;}
sub Q_DATA_OFFSET () { &QUERY_HEADER_LEN;}
sub I_TYPE_OFFSET () {0;}
sub I_VAL_OFFSET () {1;}
sub RAND_SEED1_OFFSET () {0;}
sub RAND_SEED2_OFFSET () {8;}
sub L_THREAD_ID_OFFSET () {0;}
sub L_EXEC_TIME_OFFSET () {4;}
sub L_SKIP_LINES_OFFSET () {8;}
sub L_TBL_LEN_OFFSET () {12;}
sub L_DB_LEN_OFFSET () {13;}
sub L_NUM_FIELDS_OFFSET () {14;}
sub L_SQL_EX_OFFSET () {18;}
sub L_DATA_OFFSET () { &LOAD_HEADER_LEN;}
sub R_POS_OFFSET () {0;}
sub R_IDENT_OFFSET () {8;}
sub CF_FILE_ID_OFFSET () {0;}
sub CF_DATA_OFFSET () { &CREATE_FILE_HEADER_LEN;}
sub AB_FILE_ID_OFFSET () {0;}
sub AB_DATA_OFFSET () { &APPEND_BLOCK_HEADER_LEN;}
sub EL_FILE_ID_OFFSET () {0;}
sub DF_FILE_ID_OFFSET () {0;}
sub QUERY_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &QUERY_HEADER_LEN);}
sub QUERY_DATA_OFFSET () {( &LOG_EVENT_HEADER_LEN+ &QUERY_HEADER_LEN);}
sub ROTATE_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &ROTATE_HEADER_LEN);}
sub LOAD_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &LOAD_HEADER_LEN);}
sub CREATE_FILE_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ + &LOAD_HEADER_LEN+ &CREATE_FILE_HEADER_LEN);}
sub DELETE_FILE_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &DELETE_FILE_HEADER_LEN);}
sub EXEC_LOAD_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &EXEC_LOAD_HEADER_LEN);}
sub APPEND_BLOCK_EVENT_OVERHEAD () {( &LOG_EVENT_HEADER_LEN+ &APPEND_BLOCK_HEADER_LEN);}
sub BINLOG_MAGIC () {"\\xfe\\x62\\x69\\x6e";}
sub LOG_EVENT_TIME_F () {0x1;}
sub LOG_EVENT_FORCED_ROTATE_F () {0x2;}
sub UNKNOWN_EVENT () { 0; }
sub START_EVENT () { 1; }
sub QUERY_EVENT () { 2; }
sub STOP_EVENT () { 3; }
sub ROTATE_EVENT () { 4; }
sub INTVAR_EVENT () { 5; }
sub LOAD_EVENT () { 6; }
sub SLAVE_EVENT () { 7; }
sub CREATE_FILE_EVENT () { 8; }
sub APPEND_BLOCK_EVENT () { 9; }
sub EXEC_LOAD_EVENT () { 10; }
sub DELETE_FILE_EVENT () { 11; }
sub NEW_LOAD_EVENT () { 12; }
sub RAND_EVENT () { 13; }
sub INVALID_INT_EVENT () { 0; }
sub LAST_INSERT_ID_EVENT () { 1; }
sub INSERT_ID_EVENT () { 2; }
}
1;

View File

@@ -0,0 +1,793 @@
#!/usr/bin/perl
##############################################################################
=head1 NAME
MySQL::BinLog::Event - Event class for MySQL binlog parsing
=head1 SYNOPSIS
use MySQL::BinLog::Event qw();
my $event = MySQL::BinLog::Event->read_event( $header, $data );
=head1 REQUIRES
I<Token requires line>
=head1 DESCRIPTION
None yet.
=head1 AUTHOR
Michael Granger <ged@FaerieMUD.org>
Copyright (c) 2004 Danga Interactive. All rights reserved.
This module is free software. You may use, modify, and/or redistribute this
software under the terms of the Perl Artistic License. (See
http://language.perl.com/misc/Artistic.html)
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
FITNESS FOR A PARTICULAR PURPOSE.
=cut
##############################################################################
package MySQL::BinLog::Event;
use strict;
use warnings qw{all};
###############################################################################
### I N I T I A L I Z A T I O N
###############################################################################
BEGIN {
### Versioning stuff and custom includes
use vars qw{$VERSION $RCSID};
$VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
$RCSID = q$Id: Events.pm,v 1.2 2004/11/17 21:58:40 marksmith Exp $;
# MySQL classes
use MySQL::BinLog::Header qw{};
use Carp qw{croak confess carp};
use Scalar::Util qw{blessed};
use fields qw{header rawdata};
use base qw{fields};
}
our $AUTOLOAD;
# Maps an event type to a subclass
our @ClassMap = qw(
UnknownEvent
StartEvent
QueryEvent
StopEvent
RotateEvent
IntvarEvent
LoadEvent
SlaveEvent
CreateFileEvent
AppendBlockEvent
ExecLoadEvent
DeleteFileEvent
NewLoadEvent
RandEvent
UserVarEvent
);
### (FACTORY) METHOD: read_event( $fh )
### Read the next event from the given string I<str> and return it as a
### C<MySQL::BinLog::Event> object.
sub read_event {
my $class = shift;
my $rawdata = shift;
my @desired_types = @_;
my (
$hdata,
$header,
$datalen,
$event_data,
$reallen,
$event_class,
);
debugMsg( "Reading event from ", length $rawdata, " byes of raw data.\n" );
# Read the header data and create the header object
# :TODO: only handles "new" headers; old headers are shorter. Need to
# document which version this changed and mention this in the docs.
$hdata = substr( $rawdata, 0, MySQL::LOG_EVENT_HEADER_LEN, '' );
$header = new MySQL::BinLog::Header $hdata;
# Read the event data
$datalen = $header->{event_len} - MySQL::LOG_EVENT_HEADER_LEN;
debugMsg( "Event data is $header->{event_len} bytes long.\n" );
$event_data = substr( $rawdata, 0, $datalen, '' );
debugMsg( "Read ", length $event_data, " bytes of event data.\n" );
$reallen = length $event_data;
croak "Short read for event data ($reallen of $datalen bytes)"
unless $reallen == $datalen;
# Figure out which class implements the event type and create one with the
# header and data
$event_class = sprintf "MySQL::BinLog::%s", $ClassMap[ $header->{event_type} ];
return $event_class->new( $header, $event_data );
}
### (CONSTRUCTOR) METHOD: new( $header, $raw_data )
### Construct a new Event with the specified I<header> and I<raw_data>. This is
### only meant to the called from a subclass.
sub new {
my MySQL::BinLog::Event $self = shift;
my ( $header, $data ) = @_;
die "Instantiation of abstract class" unless ref $self;
$self->{header} = $header;
$self->{rawdata} = $data;
return $self;
}
# Accessor-generator
### (PROXY) METHOD: AUTOLOAD( @args )
### Proxy method to build (non-translucent) object accessors.
sub AUTOLOAD {
my MySQL::BinLog::Event $self = shift;
( my $name = $AUTOLOAD ) =~ s{.*::}{};
### Build an accessor for extant attributes
if ( blessed $self && exists $self->{$name} ) {
### Define an accessor for this attribute
my $method = sub {
my MySQL::BinLog::Event $closureSelf = shift;
$closureSelf->{$name} = shift if @_;
return $closureSelf->{$name};
};
### Install the new method in the symbol table
NO_STRICT_REFS: {
no strict 'refs';
*{$AUTOLOAD} = $method;
}
### Now jump to the new method after sticking the self-ref back onto the
### stack
unshift @_, $self;
goto &$AUTOLOAD;
}
### Try to delegate to our parent's version of the method
my $parentMethod = "SUPER::$name";
return $self->$parentMethod( @_ );
}
### Destructors
DESTROY {}
END {}
### Utility functions
### Debugging function -- switch the commented one for debugging or no.
sub debugMsg {}
#sub debugMsg { print STDERR @_ }
#####################################################################
### S T A R T E V E N T C L A S S
#####################################################################
package MySQL::BinLog::StartEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{binlog_ver server_ver created};
use constant PACK_TEMPLATE => 'va8a*';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new StartEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::StartEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
@{$self}{qw{binlog_ver server_ver created}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::StartEvent $self = shift;
return join( ':', @{$self}{qw{binlog_ver server_ver created}} );
}
#####################################################################
### Q U E R Y E V E N T C L A S S
#####################################################################
package MySQL::BinLog::QueryEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{thread_id exec_time db_len err_code dbname query_data};
# 4 + 4 + 1 + 2 + variable length data field.
use constant PACK_TEMPLATE => 'VVCva*';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new QueryEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::QueryEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
# The last bit needs further unpacking with a length that is in the data
# extracted via the first template. If db_len immediately preceded the
# query data it could all be done in one unpack with 'c/a' or something,
# but alas...
my $template = sprintf( 'a%da*', $fields[2] ); # $fields[2] = length of dbname
push @fields, unpack( $template, pop @fields );
@{$self}{qw{thread_id exec_time db_len err_code dbname query_data}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::QueryEvent $self = shift;
return join( ':', @{$self}{qw{thread_id exec_time db_len err_code dbname query_data}} );
}
#####################################################################
### S T O P E V E N T C L A S S
#####################################################################
package MySQL::BinLog::StopEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{}; # Stop event has no fields
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new StopEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::StopEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::StopEvent $self = shift;
return join( ':', @{$self}{qw{}} );
}
#####################################################################
### R O T A T E E V E N T C L A S S
#####################################################################
package MySQL::BinLog::RotateEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{pos ident};
use constant PACK_TEMPLATE => 'a8';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new RotateEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::RotateEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
@{$self}{qw{pos ident}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::RotateEvent $self = shift;
return join( ':', @{$self}{qw{pos ident}} );
}
#####################################################################
### I N T V A R E V E N T C L A S S
#####################################################################
package MySQL::BinLog::IntvarEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{type val};
use constant PACK_TEMPLATE => 'Ca8';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new IntvarEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::IntvarEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
@{$self}{qw{type val}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::IntvarEvent $self = shift;
return join( ':', @{$self}{qw{type val}} );
}
#####################################################################
### L O A D E V E N T C L A S S
#####################################################################
package MySQL::BinLog::LoadEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{thread_id exec_time skip_lines tbl_len db_len num_fields sql_ex ldata};
use constant PACK_TEMPLATE => 'VVVCCVa*';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new LoadEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::LoadEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
@{$self}{qw{thread_id exec_time skip_lines tbl_len db_len num_fields sql_ex ldata}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::LoadEvent $self = shift;
return join( ':', @{$self}{qw{thread_id exec_time skip_lines tbl_len db_len num_fields sql_ex ldata}} );
}
#####################################################################
### S L A V E E V E N T C L A S S
#####################################################################
package MySQL::BinLog::SlaveEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{master_pos master_port master_host};
use constant PACK_TEMPLATE => 'a8va*';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new SlaveEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::SlaveEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
@{$self}{qw{master_pos master_port master_host}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::SlaveEvent $self = shift;
return join( ':', @{$self}{qw{master_pos master_port master_host}} );
}
#####################################################################
### C R E A T E F I L E E V E N T C L A S S
#####################################################################
package MySQL::BinLog::CreateFileEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{thread_id exec_time skip_lines tbl_len db_len num_fields sql_ex ldata};
use constant PACK_TEMPLATE => 'VVVCCVa*';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new CreateFileEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::CreateFileEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
@{$self}{qw{thread_id exec_time skip_lines tbl_len db_len num_fields sql_ex ldata}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::CreateFileEvent $self = shift;
return join( ':', @{$self}{qw{thread_id exec_time skip_lines tbl_len db_len num_fields sql_ex ldata}} );
}
#####################################################################
### A P P E N D B L O C K E V E N T C L A S S
#####################################################################
package MySQL::BinLog::AppendBlockEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{file_id data};
use constant PACK_TEMPLATE => 'Va*';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new AppendBlockEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::AppendBlockEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
@{$self}{qw{file_id data}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::AppendBlockEvent $self = shift;
return join( ':', @{$self}{qw{file_id data}} );
}
#####################################################################
### E X E C L O A D E V E N T C L A S S
#####################################################################
package MySQL::BinLog::ExecLoadEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{file_id};
use constant PACK_TEMPLATE => 'V';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new ExecLoadEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::ExecLoadEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
@{$self}{qw{file_id}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::ExecLoadEvent $self = shift;
return join( ':', @{$self}{qw{file_id}} );
}
#####################################################################
### D E L E T E F I L E E V E N T C L A S S
#####################################################################
package MySQL::BinLog::DeleteFileEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{file_id};
use constant PACK_TEMPLATE => 'V';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new DeleteFileEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::DeleteFileEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
@{$self}{qw{file_id}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::DeleteFileEvent $self = shift;
return join( ':', @{$self}{qw{file_id}} );
}
#####################################################################
### N E W L O A D E V E N T C L A S S
#####################################################################
package MySQL::BinLog::NewLoadEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new NewLoadEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::NewLoadEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
# I don't think these have any data (?) -MG
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::NewLoadEvent $self = shift;
return '(New_load)';
}
#####################################################################
### R A N D E V E N T C L A S S
#####################################################################
package MySQL::BinLog::RandEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{seed1 seed2};
use constant PACK_TEMPLATE => 'a8a8';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new RandEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::RandEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
@{$self}{qw{seed1 seed2}} = @fields;
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::RandEvent $self = shift;
return join( ':', @{$self}{qw{seed1 seed2}} );
}
#####################################################################
### U S E R V A R E V E N T C L A S S
#####################################################################
# USER_VAR_EVENT
# o 4 bytes: the size of the name of the user variable.
# o variable-sized part: A concatenation. First is the name of the
# user variable. Second is one byte, non-zero if the content of the
# variable is the SQL value NULL, ASCII 0 otherwise. If this bytes was
# ASCII 0, then the following parts exist in the event. Third is one
# byte, the type of the user variable, which corresponds to elements of
# enum Item_result defined in `include/mysql_com.h'. Fourth is 4 bytes,
# the number of the character set of the user variable (needed for a
# string variable). Fifth is 4 bytes, the size of the user variable's
# value (corresponds to member val_len of class Item_string). Sixth is
# variable-sized: for a string variable it is the string, for a float or
# integer variable it is its value in 8 bytes.
package MySQL::BinLog::UserVarEvent;
use strict;
BEGIN {
use base 'MySQL::BinLog::Event';
use fields qw{varname value};
use constant PACK_TEMPLATE => 'V/aca*';
}
### (CONSTRUCTOR) METHOD: new( $header_obj, $raw_data )
### Create a new UserVarEvent object from the given I<raw_data> and I<header_obj>
### (a MySQL::BinLog::Header object).
sub new {
my MySQL::BinLog::UserVarEvent $self = shift;
$self = fields::new( $self );
$self->SUPER::new( @_ );
if ( $self->{rawdata} ) {
my @fields = unpack PACK_TEMPLATE, $self->{rawdata};
# If the the second field is null, the value is undef. Otherwise,
# unpack the value
if ( $fields[1] eq "\0" ) {
$fields[2] = undef;
} else {
my ( $type, $charset, $len, $data ) = unpack 'cVVa*', $fields[2];
$fields[2] = {
type => $type,
charset => $charset,
len => $len,
data => $data,
};
}
@{$self}{qw{varname value}} = @fields[0, 2];
}
return $self;
}
### METHOD: stringify()
### Return a representation of the event as a human-readable string.
sub stringify {
my MySQL::BinLog::UserVarEvent $self = shift;
# :FIXME: This will obviously have to take into account the fact that the
# value field is a complex datatype or undef.
return join( ':', @{$self}{qw{varname value}} );
}
1;

View File

@@ -0,0 +1,158 @@
#!/usr/bin/perl
##############################################################################
=head1 NAME
MySQL::BinLog::Header - Per-event MySQL binlog header class
=head1 SYNOPSIS
use MySQL::BinLog::Header qw();
use MySQL::Constants qw(LOG_EVENT_HEADER_LEN);
my $hdata = substr( $data, 0, LOG_EVENT_HEADER_LEN );
my $header = new MySQL::BinLog::Header $hdata;
$header->event_type;
$header->server_id;
$header->event_len;
$header->log_pos;
$header->flags;
=head1 REQUIRES
I<Token requires line>
=head1 DESCRIPTION
None yet.
=head1 AUTHOR
Michael Granger <ged@FaerieMUD.org>
Copyright (c) 2004 Danga Interactive. All rights reserved.
This module is free software. You may use, modify, and/or redistribute this
software under the terms of the Perl Artistic License. (See
http://language.perl.com/misc/Artistic.html)
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
FITNESS FOR A PARTICULAR PURPOSE.
=cut
##############################################################################
package MySQL::BinLog::Header;
use strict;
use warnings qw{all};
###############################################################################
### I N I T I A L I Z A T I O N
###############################################################################
BEGIN {
# Versioning stuff and custom includes
use vars qw{$VERSION $RCSID};
$VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
$RCSID = q$Id: Header.pm,v 1.2 2004/11/17 21:58:40 marksmith Exp $;
# Data format template and fields definition
use constant PACK_TEMPLATE => 'VcVVVv';
use fields qw{timestamp event_type server_id event_len log_pos flags};
use base qw{fields};
# MySQL modules
use MySQL::BinLog::Constants qw{:all};
# Other modules
use Data::Dumper;
use Scalar::Util qw{blessed};
}
our $AUTOLOAD;
### (CONSTRUCTOR) new( $data )
### Construct a new MySQL::BinLog::::Header object from the given header data.
sub new {
my MySQL::BinLog::Header $self = shift;
my $data = shift || '';
debugMsg( "Creating a new ", __PACKAGE__, " object for header: ",
hexdump($data), ".\n" );
die "Invalid header" unless length $data == MySQL::LOG_EVENT_HEADER_LEN;
$self = fields::new( $self ) unless ref $self;
# Extract the fields or provide defaults
my @fields = ();
if ( $data ) {
@fields = unpack PACK_TEMPLATE, $data;
debugMsg( "Unpacked fields are: ", Data::Dumper->Dumpxs([\@fields], [qw{fields}]), "\n" );
} else {
@fields = ( time, MySQL::UNKNOWN_EVENT, 0, 0, 0, 0 );
}
@{$self}{qw{timestamp event_type server_id event_len log_pos flags}} = @fields;
debugMsg( "Returning header: ", Data::Dumper->Dumpxs([$self]), ".\n" );
return $self;
}
# Accessor-generator
### (PROXY) METHOD: AUTOLOAD( @args )
### Proxy method to build (non-translucent) object accessors.
sub AUTOLOAD {
my MySQL::BinLog::Header $self = shift;
( my $name = $AUTOLOAD ) =~ s{.*::}{};
### Build an accessor for extant attributes
if ( blessed $self && exists $self->{$name} ) {
### Define an accessor for this attribute
my $method = sub {
my MySQL::BinLog::Header $closureSelf = shift;
$closureSelf->{$name} = shift if @_;
return $closureSelf->{$name};
};
### Install the new method in the symbol table
NO_STRICT_REFS: {
no strict 'refs';
*{$AUTOLOAD} = $method;
}
### Now jump to the new method after sticking the self-ref back onto the
### stack
unshift @_, $self;
goto &$AUTOLOAD;
}
### Try to delegate to our parent's version of the method
my $parentMethod = "SUPER::$name";
return $self->$parentMethod( @_ );
}
### Utility functions
#sub debugMsg { print STDERR @_ }
sub debugMsg {}
sub hexdump { return join( ' ', map {sprintf '%02x', ord($_)} split('', $_[0])) }
### Destructors
DESTROY {}
END {}
1;

View File

@@ -0,0 +1,207 @@
#!/usr/bin/perl
##############################################################################
=head1 NAME
MySQL::BinLog::Net - Read binlog events from a master server over the network.
=head1 SYNOPSIS
use MySQL::BinLog qw{};
my %connect_params = (
hostname => 'db.example.com',
database => 'sales',
user => 'salesapp',
password => 'bloo$shewz',
port => 3306,
);
my $log = MySQL::BinLog->connect( %connect_params )
or die "Couldn't connect.";
=head1 REQUIRES
I<Net::MySQL>, I<Carp>
=head1 DESCRIPTION
None yet.
=head1 AUTHOR
Michael Granger <ged@Danga.com>
Copyright (c) 2004 Danga Interactive. All rights reserved.
This module is free software. You may use, modify, and/or redistribute this
software under the terms of the Perl Artistic License. (See
http://language.perl.com/misc/Artistic.html)
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
FITNESS FOR A PARTICULAR PURPOSE.
=cut
##############################################################################
package MySQL::BinLog::Net;
use strict;
use warnings qw{all};
###############################################################################
### I N I T I A L I Z A T I O N
###############################################################################
BEGIN {
# Versioning stuff
use vars qw{$VERSION $RCSID};
$VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
$RCSID = q$Id: Net.pm,v 1.2 2004/11/17 21:58:40 marksmith Exp $;
use Net::MySQL qw{};
use Carp qw{carp croak confess};
use base qw{Net::MySQL};
use constant CHUNKSIZE => 16;
use constant PKTHEADER_LEN => (3 + 1 + 1);
}
### METHOD: start_binlog( $slave_id[, $logname, $position, $flags] )
### Contact the remote server and send the command to start reading binlog
### events from the given I<logname>, I<position>, I<slave_id>, and optional
### I<flags>.
sub start_binlog {
my $self = shift;
my ( $slave_server_id, $logname, $pos, $flags ) = @_;
# New log: no logname and position = 4
$logname ||= '';
$pos = 4 unless defined $pos && $pos > 4;
my (
$len,
$cmd,
$packet,
$mysql,
);
# Build the BINLOG_DUMP packet
$cmd = Net::MySQL::COMMAND_BINLOG_DUMP;
$flags ||= 0;
$len = 1 + 4 + 2 + 4 + length( $logname );
$packet = pack( 'VaVvVa*', $len, $cmd, $pos, $flags, $slave_server_id, $logname );
$mysql = $self->{socket};
# Send it
$mysql->send( $packet, 0 );
$self->_dump_packet( $packet ) if $self->debug;
# Receive the response
my $result = $self->read_packet;
# FIXME I broke error checking by switching to read_packet instead of using
# recv... but recv reads a full buffer's worth, which just gets tossed and
# causes subsequent read_packe calls to start at arbitrary positions and fail.
# real solution is to make read_packet set error flags and then have callers
# check them. eventually. oh, FYI, you have to reconstitute the packet before
# passing it on to _is_error and _set_error_by_packet, as those are in Net::MySQL
# and expect the whole packet, not just the payload that read_packet returns.
#return $self->_set_error_by_packet( $result ) if $self->_is_error( $result );
return 1;
}
### METHOD: read_packet( )
### Read a single packet from the connection and return its payload as a scalar.
sub read_packet {
my $self = shift;
my $pkt_header = $self->readbytes( PKTHEADER_LEN );
my $length = unpack( 'V', substr($pkt_header, 0, 3, '') . "\0" ) - 1;
my ( $pktno, $cmd ) = unpack( 'CC', $pkt_header );
my $pkt = $self->readbytes( $length );
$self->_dump_packet( $pkt ) if $self->debug;
return $pkt;
}
### FUNCTION: readbytes( $len )
### Read and return I<len> bytes from the connection.
sub readbytes {
my ( $self, $len ) = @_;
my ( $buf, $rval, $bytes ) = ('', '', 0);
my $sock = $self->{socket};
until ( length $rval == $len ) {
$bytes = $sock->read( $buf, $len - length $rval );
if ( !defined $bytes ) {
if ( $!{EAGAIN} ) { next }
die "Read error: $!";
} elsif ( !$bytes && $sock->eof ) {
die "EOF before reading $len bytes.\n";
}
$rval .= $buf;
}
return $rval;
}
### Utility/debugging methods (overridden).
sub hexdump { join ' ', map {sprintf "%02x", ord $_} grep {defined} @_ }
sub ascdump { join '', map {m/[\d \w\._]/ ? $_ : '.'} grep {defined} @_ }
sub _dump_packet {
my $self = shift;
my $packet = shift;
my (
$method_name,
@bytes,
@chunk,
$half,
$width,
$count,
);
$method_name = (caller(1))[3];
print "$method_name:\n";
@bytes = split //, $packet;
$count = 0;
while ( @bytes ) {
@chunk = grep { defined } splice( @bytes, 0, CHUNKSIZE );
$half = CHUNKSIZE / 2;
$width = $half * 3;
printf( " 0x%04x: %-${width}s %-${width}s |%-${half}s %-${half}s|\n",
$count,
hexdump( @chunk[0..($half-1)] ),
hexdump( @chunk[$half..$#chunk] ),
ascdump( @chunk[0..($half-1)] ),
ascdump( @chunk[$half..$#chunk] ) );
$count += CHUNKSIZE;
}
print "--\n";
}
### Destructors
DESTROY {}
END {}
1;