lib/MySQL/Replication/Server.pm untested

filter: Statementstmt 0.0% 0 / 279 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 502 CC 62 SCAR 44.6
CC
62
Cov
0%
USER_VAR
272.0
BinlogReadHandler
132.0
_InitBinlogFh
90.0
CRAP 86.4
1 package MySQL::Replication::Server;
2  
3 use strict;
4 use warnings;
5  
6 use base qw{ Class::Accessor };
7  
8 use AnyEvent;
9 use Fcntl qw{ :seek };
10 use Scalar::Util qw{ weaken };
11 use MySQL::Replication::Command;
12  
13 my ( %HANDLERS, %COLLATIONS, $EXTRA_HEADER_LENGTH );
14  
15 BEGIN {
16 $EXTRA_HEADER_LENGTH = 0;
17  
18 use constant {
19 BINLOG_READ_INTERVAL => 1,
20  
21 # binlog header
22 BINLOG_MAGIC => "\xfe\x62\x69\x6e",
23 HEADER_LENGTH => 19,
24  
25 # binlog event typecodes
26 QUERY_EVENT => 2,
27 INTVAR_EVENT => 5,
28 RAND_EVENT => 13,
29 USER_VAR_EVENT => 14,
30 FORMAT_DESCRIPTION_EVENT => 15,
31 XID_EVENT => 16,
32  
33 # skip event typecodes
34 ROTATE_EVENT => 4,
35 TABLE_MAP_EVENT => 19,
36 PRE_GA_WRITE_ROWS_EVENT => 20,
37 PRE_GA_UPDATE_ROWS_EVENT => 21,
38 PRE_GA_DELETE_ROWS_EVENT => 22,
39 WRITE_ROWS_EVENT => 23,
40 UPDATE_ROWS_EVENT => 24,
41 DELETE_ROWS_EVENT => 25,
42 INCIDENT_EVENT => 26,
43  
44 # header offsets
45 OFFSET_TYPE_CODE => 4,
46 OFFSET_EVENT_LENGTH => 9,
47 OFFSET_NEXT_POSITION => 13,
48 OFFSET_HEADER_LENGTH => 75,
49  
50 # query event offsets
51 OFFSET_DB_LENGTH => 8,
52 OFFSET_STATUS_LENGTH => 11,
53 OFFSET_DATA_BLOCK => 13,
54  
55 # user variable types
56 STRING_RESULT => 0,
57 REAL_RESULT => 1,
58 INT_RESULT => 2,
59 ROW_RESULT => 3,
60 DECIMAL_RESULT => 4,
61 };
62  
63 %HANDLERS = (
64 QUERY_EVENT() => \&QUERY,
65 INTVAR_EVENT() => \&INTVAR,
66 RAND_EVENT() => \&RAND,
67 USER_VAR_EVENT() => \&USER_VAR,
68 XID_EVENT() => \&XID,
69 );
70  
71 __PACKAGE__->mk_accessors( qw{
72 BinlogPath
73 ServerId
74 StartLog
75 StartPos
76 EndLog
77 EndPos
78 CurrentLog
79 CurrentPos
80 NewBinlogExists
81 Headers
82 Query
83 BinlogFh
84 BinlogBuffer
85 BinlogReadTimer
86 CondVar
87 });
88  
89 %COLLATIONS = (
90 1 => { charset => "big5", collation => "big5_chinese_ci" },
91 2 => { charset => "latin2", collation => "latin2_czech_cs" },
92 3 => { charset => "dec8", collation => "dec8_swedish_ci" },
93 4 => { charset => "cp850", collation => "cp850_general_ci" },
94 5 => { charset => "latin1", collation => "latin1_german1_ci" },
95 6 => { charset => "hp8", collation => "hp8_english_ci" },
96 7 => { charset => "koi8r", collation => "koi8r_general_ci" },
97 8 => { charset => "latin1", collation => "latin1_swedish_ci" },
98 9 => { charset => "latin2", collation => "latin2_general_ci" },
99 10 => { charset => "swe7", collation => "swe7_swedish_ci" },
100 11 => { charset => "ascii", collation => "ascii_general_ci" },
101 12 => { charset => "ujis", collation => "ujis_japanese_ci" },
102 13 => { charset => "sjis", collation => "sjis_japanese_ci" },
103 14 => { charset => "cp1251", collation => "cp1251_bulgarian_ci" },
104 15 => { charset => "latin1", collation => "latin1_danish_ci" },
105 16 => { charset => "hebrew", collation => "hebrew_general_ci" },
106 18 => { charset => "tis620", collation => "tis620_thai_ci" },
107 19 => { charset => "euckr", collation => "euckr_korean_ci" },
108 20 => { charset => "latin7", collation => "latin7_estonian_cs" },
109 21 => { charset => "latin2", collation => "latin2_hungarian_ci" },
110 22 => { charset => "koi8u", collation => "koi8u_general_ci" },
111 23 => { charset => "cp1251", collation => "cp1251_ukrainian_ci" },
112 24 => { charset => "gb2312", collation => "gb2312_chinese_ci" },
113 25 => { charset => "greek", collation => "greek_general_ci" },
114 26 => { charset => "cp1250", collation => "cp1250_general_ci" },
115 27 => { charset => "latin2", collation => "latin2_croatian_ci" },
116 28 => { charset => "gbk", collation => "gbk_chinese_ci" },
117 29 => { charset => "cp1257", collation => "cp1257_lithuanian_ci" },
118 30 => { charset => "latin5", collation => "latin5_turkish_ci" },
119 31 => { charset => "latin1", collation => "latin1_german2_ci" },
120 32 => { charset => "armscii8", collation => "armscii8_general_ci" },
121 33 => { charset => "utf8", collation => "utf8_general_ci" },
122 34 => { charset => "cp1250", collation => "cp1250_czech_cs" },
123 35 => { charset => "ucs2", collation => "ucs2_general_ci" },
124 36 => { charset => "cp866", collation => "cp866_general_ci" },
125 37 => { charset => "keybcs2", collation => "keybcs2_general_ci" },
126 38 => { charset => "macce", collation => "macce_general_ci" },
127 39 => { charset => "macroman", collation => "macroman_general_ci" },
128 40 => { charset => "cp852", collation => "cp852_general_ci" },
129 41 => { charset => "latin7", collation => "latin7_general_ci" },
130 42 => { charset => "latin7", collation => "latin7_general_cs" },
131 43 => { charset => "macce", collation => "macce_bin" },
132 44 => { charset => "cp1250", collation => "cp1250_croatian_ci" },
133 47 => { charset => "latin1", collation => "latin1_bin" },
134 48 => { charset => "latin1", collation => "latin1_general_ci" },
135 49 => { charset => "latin1", collation => "latin1_general_cs" },
136 50 => { charset => "cp1251", collation => "cp1251_bin" },
137 51 => { charset => "cp1251", collation => "cp1251_general_ci" },
138 52 => { charset => "cp1251", collation => "cp1251_general_cs" },
139 53 => { charset => "macroman", collation => "macroman_bin" },
140 57 => { charset => "cp1256", collation => "cp1256_general_ci" },
141 58 => { charset => "cp1257", collation => "cp1257_bin" },
142 59 => { charset => "cp1257", collation => "cp1257_general_ci" },
143 63 => { charset => "binary", collation => "binary" },
144 64 => { charset => "armscii8", collation => "armscii8_bin" },
145 65 => { charset => "ascii", collation => "ascii_bin" },
146 66 => { charset => "cp1250", collation => "cp1250_bin" },
147 67 => { charset => "cp1256", collation => "cp1256_bin" },
148 68 => { charset => "cp866", collation => "cp866_bin" },
149 69 => { charset => "dec8", collation => "dec8_bin" },
150 70 => { charset => "greek", collation => "greek_bin" },
151 71 => { charset => "hebrew", collation => "hebrew_bin" },
152 72 => { charset => "hp8", collation => "hp8_bin" },
153 73 => { charset => "keybcs2", collation => "keybcs2_bin" },
154 74 => { charset => "koi8r", collation => "koi8r_bin" },
155 75 => { charset => "koi8u", collation => "koi8u_bin" },
156 77 => { charset => "latin2", collation => "latin2_bin" },
157 78 => { charset => "latin5", collation => "latin5_bin" },
158 79 => { charset => "latin7", collation => "latin7_bin" },
159 80 => { charset => "cp850", collation => "cp850_bin" },
160 81 => { charset => "cp852", collation => "cp852_bin" },
161 82 => { charset => "swe7", collation => "swe7_bin" },
162 83 => { charset => "utf8", collation => "utf8_bin" },
163 84 => { charset => "big5", collation => "big5_bin" },
164 85 => { charset => "euckr", collation => "euckr_bin" },
165 86 => { charset => "gb2312", collation => "gb2312_bin" },
166 87 => { charset => "gbk", collation => "gbk_bin" },
167 88 => { charset => "sjis", collation => "sjis_bin" },
168 89 => { charset => "tis620", collation => "tis620_bin" },
169 90 => { charset => "ucs2", collation => "ucs2_bin" },
170 91 => { charset => "ujis", collation => "ujis_bin" },
171 92 => { charset => "geostd8", collation => "geostd8_general_ci" },
172 93 => { charset => "geostd8", collation => "geostd8_bin" },
173 94 => { charset => "latin1", collation => "latin1_spanish_ci" },
174 95 => { charset => "cp932", collation => "cp932_japanese_ci" },
175 96 => { charset => "cp932", collation => "cp932_bin" },
176 97 => { charset => "eucjpms", collation => "eucjpms_japanese_ci" },
177 98 => { charset => "eucjpms", collation => "eucjpms_bin" },
178 128 => { charset => "ucs2", collation => "ucs2_unicode_ci" },
179 129 => { charset => "ucs2", collation => "ucs2_icelandic_ci" },
180 130 => { charset => "ucs2", collation => "ucs2_latvian_ci" },
181 131 => { charset => "ucs2", collation => "ucs2_romanian_ci" },
182 132 => { charset => "ucs2", collation => "ucs2_slovenian_ci" },
183 133 => { charset => "ucs2", collation => "ucs2_polish_ci" },
184 134 => { charset => "ucs2", collation => "ucs2_estonian_ci" },
185 135 => { charset => "ucs2", collation => "ucs2_spanish_ci" },
186 136 => { charset => "ucs2", collation => "ucs2_swedish_ci" },
187 137 => { charset => "ucs2", collation => "ucs2_turkish_ci" },
188 138 => { charset => "ucs2", collation => "ucs2_czech_ci" },
189 139 => { charset => "ucs2", collation => "ucs2_danish_ci" },
190 140 => { charset => "ucs2", collation => "ucs2_lithuanian_ci" },
191 141 => { charset => "ucs2", collation => "ucs2_slovak_ci" },
192 142 => { charset => "ucs2", collation => "ucs2_spanish2_ci" },
193 143 => { charset => "ucs2", collation => "ucs2_roman_ci" },
194 144 => { charset => "ucs2", collation => "ucs2_persian_ci" },
195 145 => { charset => "ucs2", collation => "ucs2_esperanto_ci" },
196 146 => { charset => "ucs2", collation => "ucs2_hungarian_ci" },
197 192 => { charset => "utf8", collation => "utf8_unicode_ci" },
198 193 => { charset => "utf8", collation => "utf8_icelandic_ci" },
199 194 => { charset => "utf8", collation => "utf8_latvian_ci" },
200 195 => { charset => "utf8", collation => "utf8_romanian_ci" },
201 196 => { charset => "utf8", collation => "utf8_slovenian_ci" },
202 197 => { charset => "utf8", collation => "utf8_polish_ci" },
203 198 => { charset => "utf8", collation => "utf8_estonian_ci" },
204 199 => { charset => "utf8", collation => "utf8_spanish_ci" },
205 200 => { charset => "utf8", collation => "utf8_swedish_ci" },
206 201 => { charset => "utf8", collation => "utf8_turkish_ci" },
207 202 => { charset => "utf8", collation => "utf8_czech_ci" },
208 203 => { charset => "utf8", collation => "utf8_danish_ci" },
209 204 => { charset => "utf8", collation => "utf8_lithuanian_ci" },
210 205 => { charset => "utf8", collation => "utf8_slovak_ci" },
211 206 => { charset => "utf8", collation => "utf8_spanish2_ci" },
212 207 => { charset => "utf8", collation => "utf8_roman_ci" },
213 208 => { charset => "utf8", collation => "utf8_persian_ci" },
214 209 => { charset => "utf8", collation => "utf8_esperanto_ci" },
215 210 => { charset => "utf8", collation => "utf8_hungarian_ci" },
216 );
217 }
218  
219 sub new {
220 my ( $Class, %Args ) = @_;
221  
222 foreach my $Required ( qw{ BinlogPath StartLog StartPos } ) {
223 next if $Args{$Required};
224 die "Required parameter '$Required' missing";
225 }
226  
227 if ( $Args{EndLog} and not $Args{EndPos} ) {
228 die "EndLog specified but EndPos missing";
229 }
230  
231 if ( $Args{EndPos} and not $Args{EndLog} ) {
232 die "EndPos specified but EndLog missing";
233 }
234  
235 my $Self = $Class->SUPER::new( \%Args );
236 $Self->CurrentLog( $Self->StartLog() );
237 $Self->CurrentPos( $Self->StartPos() );
238  
239 return $Self;
240 }
241  
242 sub GetQuery {
243 my ( $Self ) = @_;
244  
245 $Self->BinlogBuffer( '' );
246 $Self->Headers( {} );
247 $Self->Query( undef );
248  
249 weaken( my $Weak = $Self );
250  
251 $Self->BinlogReadTimer( AnyEvent->timer(
252 after => 0,
253 interval => BINLOG_READ_INTERVAL,
254 cb => sub { $Weak->BinlogReadHandler() },
255 )) or die "Error creating binlog read timer";
256  
257 $Self->CondVar( AnyEvent->condvar() )
258 or die "Error creating condition variable";
259  
260 $Self->CondVar()->recv();
261 return $Self->Query();
262 }
263  
264 sub BinlogReadHandler {
265 my ( $Self ) = @_;
266  
267 NEXT_EVENT:
268  
269 if ( not $Self->BinlogFh() ) {
270 return if not $Self->_InitBinlogFh();
271 }
272  
273 if ( not %{ $Self->Headers() || {} } ) {
274 return if not $Self->_ReadBinlog( HEADER_LENGTH + $EXTRA_HEADER_LENGTH );
275 return if $Self->_RequestComplete();
276  
277 $Self->_ParseHeaders();
278 }
279  
280 return if not $Self->_ReadBinlog( $Self->Headers()->{EventLength} - HEADER_LENGTH - $EXTRA_HEADER_LENGTH );
281 return if $Self->_RequestComplete();
282  
283 if ( $Self->ServerId() and ( $Self->Headers()->{ServerId} != $Self->ServerId() ) ) {
284 $Self->BinlogBuffer( '' );
285 $Self->Headers( {} );
286 goto NEXT_EVENT;
287 }
288  
289 if ( not $HANDLERS{ $Self->Headers()->{TypeCode} } ) {
290 my @SkipEvents = (
291 ROTATE_EVENT,
292 TABLE_MAP_EVENT,
293 PRE_GA_WRITE_ROWS_EVENT,
294 PRE_GA_UPDATE_ROWS_EVENT,
295 PRE_GA_DELETE_ROWS_EVENT,
296 WRITE_ROWS_EVENT,
297 UPDATE_ROWS_EVENT,
298 DELETE_ROWS_EVENT,
299 );
300  
301 foreach my $SkipEvent ( @SkipEvents ) {
302 next unless $Self->Headers()->{TypeCode} == $SkipEvent;
303  
304 $Self->BinlogBuffer( '' );
305 $Self->Headers( {} );
306 goto NEXT_EVENT;
307 }
308  
309 die sprintf( "Unsupported event '%s' in binlog '%s' at '%s'",
310 $Self->Headers()->{TypeCode},
311 $Self->_BinlogFilename(),
312 $Self->CurrentPos()
313 );
314 }
315  
316 $HANDLERS{ $Self->Headers()->{TypeCode} }->( $Self );
317  
318 $Self->BinlogReadTimer( undef );
319 $Self->CondVar()->send();
320 }
321  
322 sub _InitBinlogFh {
323 my ( $Self ) = @_;
324  
325 open my $BinlogFh, '<', $Self->_BinlogFilename()
326 or die sprintf( "Error opening binlog '%s'", $Self->_BinlogFilename() );
327  
328 my $Bytes = sysread( $BinlogFh, my $Buffer, length( BINLOG_MAGIC ) + OFFSET_HEADER_LENGTH + 1 );
329  
330 if ( not defined $Bytes ) {
331 die sprintf( "Error reading magic from binlog '%s' ($!)", $Self->_BinlogFilename() );
332 }
333  
334 if ( $Bytes == 0 ) {
335 $Self->_CheckForNewBinlog();
336 return;
337 }
338  
339 if ( $Bytes != length( BINLOG_MAGIC ) + OFFSET_HEADER_LENGTH + 1 ) {
340 return;
341 }
342  
343 my ( $Magic, $FormatDescriptionEvent, $HeaderLength ) = unpack(
344 sprintf( 'a4 a%d x%d C',
345 HEADER_LENGTH,
346 OFFSET_HEADER_LENGTH - HEADER_LENGTH
347 ),
348 $Buffer,
349 );
350  
351 if ( grep { not defined $_ } $Magic, $FormatDescriptionEvent, $HeaderLength ) {
352 die sprintf( "Invalid format description event in binlog '%s'", $Self->_BinlogFilename() );
353 }
354  
355 if ( $Magic ne BINLOG_MAGIC ) {
356 die sprintf( "Invalid magic in binlog '%s'", $Self->_BinlogFilename() );
357 }
358  
359 substr $Buffer, 0, length( BINLOG_MAGIC ), '';
360 $Self->BinlogBuffer( $Buffer );
361 $Self->_ParseHeaders();
362  
363 if ( $Self->Headers()->{TypeCode} != FORMAT_DESCRIPTION_EVENT ) {
364 die sprintf( "Format description event not found in binlog '%s'", $Self->_BinlogFilename() );
365 }
366  
367 $EXTRA_HEADER_LENGTH = $HeaderLength - HEADER_LENGTH;
368  
369 if ( not $Self->CurrentPos() ) {
370 $Self->CurrentPos( $Self->Headers()->{NextPosition} );
371 }
372  
373 my $Tell = sysseek( $BinlogFh, $Self->CurrentPos(), SEEK_SET );
374  
375 if ( not defined $Tell ) {
376 die sprintf( "Error seeking in binlog '%s' ($!)", $Self->_BinlogFilename() );
377 }
378  
379 $Self->BinlogFh( $BinlogFh );
380 $Self->BinlogBuffer( '' );
381 $Self->Headers( {} );
382 return 1;
383 }
384  
385 sub _CheckForNewBinlog {
386 my ( $Self ) = @_;
387  
388 if( $Self->NewBinlogExists() ) {
389 $Self->NewBinlogExists( 0 );
390 $Self->CurrentLog( $Self->CurrentLog() + 1 );
391 $Self->CurrentPos( 0 );
392 $Self->BinlogFh( undef );
393 $Self->BinlogBuffer( '' ),
394 $Self->Headers( {} );
395 return;
396 }
397  
398 if ( -e $Self->_BinlogFilename( 1 ) ) {
399 $Self->NewBinlogExists( 1 );
400 return;
401 }
402 }
403  
404 sub _ReadBinlog {
405 my ( $Self, $Length ) = @_;
406  
407 my $Bytes = sysread(
408 $Self->BinlogFh(),
409 $Self->{BinlogBuffer},
410 $Length - length( $Self->{BinlogBuffer} ),
411 length( $Self->{BinlogBuffer} )
412 );
413  
414 if( not defined $Bytes ) {
415 die sprintf( "Error reading from binlog '%s' ($!)", $Self->_BinlogFilename() );
416 }
417  
418 if ( $Bytes == 0 ) {
419 $Self->_CheckForNewBinlog();
420 return;
421 }
422  
423 if ( length( $Self->{BinlogBuffer} ) != $Length ) {
424 return;
425 }
426  
427 $Self->CurrentPos( $Self->CurrentPos() + $Length );
428 return 1;
429 }
430  
431 sub _RequestComplete {
432 my ( $Self ) = @_;
433  
434 return if not $Self->EndLog();
435 return if $Self->CurrentLog() < $Self->EndLog();
436 return if $Self->CurrentLog() == $Self->EndLog() and $Self->CurrentPos() < $Self->EndPos();
437  
438 $Self->Query( undef );
439 $Self->BinlogReadTimer( undef );
440 $Self->CondVar->send();
441  
442 return 1;
443 }
444  
445 sub _ParseHeaders {
446 my ( $Self ) = @_;
447  
448 my ( $Timestamp, $TypeCode, $ServerId, $EventLength, $NextPosition ) = unpack( 'L C L L L', $Self->{BinlogBuffer} );
449  
450 if ( grep { not defined $_ } $Timestamp, $TypeCode, $ServerId, $EventLength, $NextPosition ) {
451 die sprintf( "Invalid header in binlog '%s' at position %s", $Self->_BinlogFilename(), $Self->CurrentPos() );
452 }
453  
454 $Self->BinlogBuffer( '' );
455 $Self->Headers({
456 Timestamp => $Timestamp,
457 TypeCode => $TypeCode,
458 ServerId => $ServerId,
459 EventLength => $EventLength,
460 NextPosition => $NextPosition,
461 });
462 }
463  
464 sub _BinlogFilename {
465 my ( $Self, $Offset ) = @_;
466  
467 return sprintf( '%s.%06d',
468 $Self->BinlogPath(),
469 ( $Self->CurrentLog() + ( $Offset || 0 ) ),
470 );
471 }
472  
473 sub QUERY {
474 my ( $Self ) = @_;
475  
476 my ( $DbLength, $StatusBlockLength ) = unpack(
477 sprintf( 'x%d C x%d S',
478 OFFSET_DB_LENGTH,
479 OFFSET_STATUS_LENGTH - OFFSET_DB_LENGTH - 1
480 ),
481 $Self->{BinlogBuffer},
482 );
483  
484 if ( grep { not defined $_ } $DbLength, $StatusBlockLength ) {
485 die sprintf( "Error parsing QUERY event (static) in binlog '%' at %", $Self->_BinlogFilename(), $Self->CurrentPos() );
486 }
487  
488 my ( $DbName, $Query ) = unpack(
489 sprintf( 'x%d a%dx a%d',
490 OFFSET_DATA_BLOCK + $StatusBlockLength,
491 $DbLength,
492 length( $Self->{BinlogBuffer} ) - OFFSET_DATA_BLOCK + $StatusBlockLength + $DbLength,
493 ),
494 $Self->{BinlogBuffer},
495 );
496  
497 if ( grep { not defined $_ } $DbName, $Query ) {
498 die sprintf( "Error parsing QUERY event (dynamic) in binlog '%s' at %s", $Self->_BinlogFilename(), $Self->CurrentPos() );
499 }
500  
501 $Self->Query( MySQL::Replication::Command->new(
502 Command => 'QUERY',
503 Headers => {
504 ( $DbName ? ( Database => $DbName ) : () ),
505 Timestamp => $Self->Headers()->{Timestamp},
506 Log => $Self->CurrentLog(),
507 Pos => $Self->CurrentPos(),
508 Length => length( $Query ),
509 },
510 Body => $Query,
511 ));
512  
513 if ( not $Self->Query() ) {
514 die "Error creating QUERY query ($MySQL::Replication::Command::Errstr)";
515 }
516 }
517  
518 sub INTVAR {
519 my ( $Self ) = @_;
520  
521 my ( $IntType, $IntValueLow, $IntValueHigh ) = unpack( 'C L2', $Self->{BinlogBuffer} );
522  
523 if ( grep { not defined $_ } $IntType, $IntValueLow, $IntValueHigh ) {
524 die sprintf( "Error parsing INTVAR event in binlog '%s' at %s", $Self->_BinlogFilename(), $Self->CurrentPos() );
525 }
526  
527 use bigint;
528 my $IntValue = ( $IntValueHigh << 32 ) + $IntValueLow;
529  
530 my $Query = $IntType == 1
531 ? 'SET LAST_INSERT_ID=' . $IntValue
532 : 'SET INSERT_ID=' . $IntValue;
533  
534 $Self->Query( MySQL::Replication::Command->new(
535 Command => 'QUERY',
536 Headers => {
537 Timestamp => $Self->Headers()->{Timestamp},
538 Log => $Self->CurrentLog(),
539 Pos => $Self->CurrentPos(),
540 Length => length( $Query ),
541 },
542 Body => $Query,
543 ));
544  
545 if ( not $Self->Query() ) {
546 die "Error creating INTVAR query ($MySQL::Replication::Command::Errstr)";
547 }
548 }
549  
550 sub RAND {
551 my ( $Self ) = @_;
552  
553 my ( $RandSeed1Low, $RandSeed1High, $RandSeed2Low, $RandSeed2High ) = unpack( 'L4', $Self->{BinlogBuffer});
554  
555 if ( grep { not defined $_ } $RandSeed1Low, $RandSeed1High, $RandSeed1Low, $RandSeed2High ) {
556 die sprintf( "Error parsing RAND event in binlog '%s' at %s", $Self->_BinlogFilename(), $Self->CurrentPos() );
557 }
558  
559 use bigint;
560 my $RandSeed1 = ( $RandSeed1High << 32 ) + $RandSeed1Low;
561 my $RandSeed2 = ( $RandSeed2High << 32 ) + $RandSeed2Low;
562  
563 my $Query = sprintf( 'SET @@RAND_SEED1=%s, @@RAND_SEED2=%s', $RandSeed1, $RandSeed2 );
564  
565 $Self->Query( MySQL::Replication::Command->new(
566 Command => 'QUERY',
567 Headers => {
568 Timestamp => $Self->Headers()->{Timestamp},
569 Log => $Self->CurrentLog(),
570 Pos => $Self->CurrentPos(),
571 Length => length( $Query ),
572 },
573 Body => $Query,
574 ));
575  
576 if ( not $Self->Query() ) {
577 die "Error creating RAND query ($MySQL::Replication::Command::Errstr)";
578 }
579 }
580  
581 sub USER_VAR {
582 my ( $Self ) = @_;
583  
584 use bigint;
585 my ( $Value );
586  
587 my ( $Name, $IsNull ) = unpack( 'L/a c', $Self->{BinlogBuffer} );
588  
589 if ( grep { not defined $_ } $Name, $IsNull ) {
590 die sprintf( "Error parsing USER_VAR event (header) in binlog '%s' at %s", $Self->_BinlogFilename(), $Self->CurrentPos() );
591 }
592  
593 if ( $IsNull ) {
594 $Value = 'NULL';
595 }
596 else {
597 my ( $Type ) = unpack( sprintf( 'x%d c', length( $Name ) + 5 ), $Self->{BinlogBuffer} );
598  
599 if ( not defined $Type ) {
600 die sprintf( "Error parsing USER_VAR event (variable type) in binlog '%s' at %s",
601 $Self->_BinlogFilename(), $Self->CurrentPos() );
602 }
603  
604 if ( $Type == STRING_RESULT ) {
605 my ( $CollationId, $Size, @Values ) = unpack( sprintf( 'x%d L L c*', length( $Name ) + 6 ), $Self->{BinlogBuffer} );
606  
607 if ( grep { not defined $_ } $CollationId, $Size, @Values ) {
608 die sprintf( "Error parsing USER_VAR event (STRING_RESULT) in binlog '%s' at %s",
609 $Self->_BinlogFilename(), $Self->CurrentPos() );
610 }
611  
612 my $Charset = $COLLATIONS{$CollationId} ? "_$COLLATIONS{$CollationId}{charset} " : '';
613 my $Collation = $COLLATIONS{$CollationId} ? " COLLATE `$COLLATIONS{$CollationId}{collation}`" : '';
614 $Value = $Charset . '0x' . join( '', map { uc sprintf '%x', $_ } @Values ) . $Collation;
615 }
616 elsif ( grep { $Type == $_ } REAL_RESULT, INT_RESULT ) {
617 my ( $Low, $High ) = unpack( sprintf( 'x%d L2', length( $Name ) + 14 ), $Self->{BinlogBuffer} );
618  
619 if ( grep { not defined $_ } $Low, $High ) {
620 die sprintf( "Error parsing USER_VAR event ([REAL|INT]_RESULT) in binlog '%s' at %s",
621 $Self->_BinlogFilename(), $Self->CurrentPos() );
622 }
623  
624 $Value = ( $High << 32 ) + $Low;
625 }
626 elsif ( $Type == DECIMAL_RESULT ) {
627 my ( $PackedDecimal ) = unpack( sprintf( 'x%d L/a', length( $Name ) + 10 ), $Self->{BinlogBuffer} );
628  
629 if ( not defined $PackedDecimal ) {
630 die sprintf( "Error parsing USER_VAR event (DECIMAL_RESULT) in binlog '%s' at %s",
631 $Self->_BinlogFilename(), $Self->CurrentPos() );
632 }
633  
634 my ( $Precision, $Scale, @Parts ) = unpack( 'c2 C*', $PackedDecimal );
635  
636 if ( grep { not defined $_ } $Precision, $Scale, @Parts ) {
637 die sprintf( "Error parsing USER_VAR event (DECIMAL_RESULT precision) in binlog '%s' at %s",
638 $Self->_BinlogFilename(), $Self->CurrentPos() );
639 }
640  
641 my $Cut = 0;
642 while ( length( 256 ** $Cut ) < $Precision - $Scale ) {
643 $Cut++;
644 }
645 $Cut++;
646  
647 my @Left = splice @Parts, 0, $Cut;
648 $Left[0] ^= 0x80;
649 my $Negative = $Left[0] & 0x80 ? 1 : 0;
650  
651 my $Left = $Self->_UnpackDecimalPart( $Negative , 0, reverse @Left );
652 my $Right = $Self->_UnpackDecimalPart( $Negative , 1, @Parts );
653 $Value = ( $Negative ? '-' : '' ) . ( $Left || 0 ) . '.' . ( $Right || 0 );
654 }
655 else {
656 die sprintf( "Unknown user variable type '$Type' in binlog '%s' at %s'",
657 $Self->_BinlogFilename(), $Self->CurrentPos() );
658 }
659 }
660  
661 my $Query = "SET @`$Name`:=$Value";
662  
663 $Self->Query( MySQL::Replication::Command->new(
664 Command => 'QUERY',
665 Headers => {
666 Timestamp => $Self->Headers()->{Timestamp},
667 Log => $Self->CurrentLog(),
668 Pos => $Self->CurrentPos(),
669 Length => length( $Query ),
670 },
671 Body => $Query,
672 ));
673  
674 if ( not $Self->Query() ) {
675 die "Error creating USER_VAR query ($MySQL::Replication::Command::Errstr)";
676 }
677 }
678  
679 sub XID {
680 my ( $Self ) = @_;
681  
682 my $Query = 'COMMIT';
683  
684 $Self->Query( MySQL::Replication::Command->new(
685 Command => 'QUERY',
686 Headers => {
687 Timestamp => $Self->Headers()->{Timestamp},
688 Log => $Self->CurrentLog(),
689 Pos => $Self->CurrentPos(),
690 Length => length( $Query ),
691 },
692 Body => $Query,
693 ));
694  
695 if ( not $Self->Query() ) {
696 die "Error creating COMMIT query ($MySQL::Replication::Command::Errstr)";
697 }
698 }
699  
700 sub _UnpackDecimalPart {
701 my ( $Self, $Negative, $Reverse, @Parts ) = @_;
702  
703 if ( $Negative ) {
704 @Parts = map { unpack 'C', pack 'l', ~$_ } @Parts;
705  
706 if ( grep { not defined $_ } @Parts ) {
707 die sprintf( "Error parsing USER_VAR event (DECIMAL_RESULT sign) in binlog '%s' at %s",
708 $Self->_BinlogFilename(), $Self->CurrentPos() );
709 }
710 }
711  
712 my @Values;
713  
714 while ( @Parts ) {
715 my @Part = splice @Parts, 0, 4;
716 @Part = reverse @Part if $Reverse;
717  
718 $Part[0] ||= 0; $Part[1] ||= 0; $Part[2] ||= 0; $Part[3] ||= 0;
719  
720 my @Unpacked = unpack 'L', pack 'C*', @Part;
721  
722 if ( grep { not defined $_ } @Unpacked ) {
723 die sprintf( "Error parsing USER_VAR event (DECIMAL_RESULT parts) in binlog '%s' at %s",
724 $Self->_BinlogFilename(), $Self->CurrentPos() );
725 }
726  
727 unshift @Values, @Unpacked;
728 }
729  
730 @Values = reverse @Values if $Reverse;
731  
732 my $Value;
733  
734 for ( my $i = 0; $i < @Values; $i++ ) {
735 $Value .= ( $i == 0 or $i == @Values - 1 )
736 ? $Values[$i]
737 : sprintf "%09d", $Values[$i];
738 }
739  
740 return $Value;
741 }
742  
743 1;
744  
745 __END__