blib/lib/MySQL/Binlog_RBR_Data.pm

filter: Statementstmt 91.0% 71 / 78 Branchbran 75.0% 45 / 60 Conditioncond 50.0% 15 / 30 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 73.3% 143 / 195 CC 41 SCAR 33.6
CC
41
Cov
78%
__ANON__
38.5
parse
15.3
BEGIN
1.0
CRAP 28.7
↑ Summary
1 package MySQL::Binlog_RBR_Data;
2  
3 1 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # be as strict and verbose as possible
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # set version info
10 1 our $VERSION = "1.02";
11  
12 1 ▶use Carp qw( confess croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶sub parse {
Subroutine parse: called CC=13 SCAR=27.3
Pod documented
15 2 my ( $class, $handle, $start_position, @interesting_tables ) = @_;
16  
17 2 my %interesting_tables;
18 2 @interesting_tables{ @interesting_tables } = @_;
19  
20 2 my $pos;
21 my @transactions;
22 2 my $trans;
23  
24 # mysqlbinlog outputs the binlog data between two lines with:
25 # DELIMITER
26 # so start working after the first one
27 2 ▶ while ( <$handle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $handle)) 188 0 188
28 188 ▶ if ( substr( $_, 0, 5 ) eq '# at ' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_, 0, 5) eq "# at ") 0 188 188
29 0 $pos = 0 + substr( $_, 5 );
30 0 next;
31 }
32  
33 last
34 188 ▶ if substr( $_, 0, 9 ) eq 'DELIMITER';
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, 0, 9) eq "DELIMITER" 2 186 188
35 }
36  
37 # it's an error if we didn't get the DELIMITER, probably
38 # not a binlog file or a corrupted one
39 2 ▶ $_ && substr( $_, 0, 9 ) eq 'DELIMITER'
Branch: 1 branch
BranchTrueFalseTotal
$_ and substr($_, 0, 9) eq "DELIMITER" or croak("not a binlog file, didn't find DELIMITER") 2 0 2
Condition33%1/3
$_ and substr($_, 0, 9) eq "DELIMITER"
!ll&&!rl&&r
002
MC/DC0%0/2
$_ and substr($_, 0, 9) eq "DELIMITER"
$_ substr($_, 0, 9) eq "DELIMITER"
Truth table33%1/3
A and B
A= $_B= substr($_, 0, 9) eq "DELIMITER"
ABresult
0X0
100
111
40 or croak "not a binlog file, didn't find DELIMITER";
41  
42 # want to start at some position? mysqlbinlog seeks on it, but we
43 # must parse the full output until we get at that position
44 2 ▶ if ( $start_position ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_position) 1 1 2
45 1 ▶ while ( <$handle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $handle)) 43 0 43
46 43 ▶ if ( substr( $_, 0, 5 ) eq '# at ' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_, 0, 5) eq "# at ") 8 35 43
47 8 $pos = 0 + substr( $_, 5 );
48  
49 8 ▶ last if $pos >= $start_position;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $start_position 1 7 8
50 }
51  
52 # got to EOF?
53 last
54 42 ▶ if substr( $_, 0, 9 ) eq 'DELIMITER';
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, 0, 9) eq "DELIMITER" 0 42 42
55 }
56  
57 1 ▶ if ( $pos < $start_position ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pos < $start_position) { } 0 1 1
elsif ($pos > $start_position) { } 0 1 1
58 # no guarantee $. points to 'at'
59 0 croak "position $start_position not found in binlog, last one found was: $pos";
60 }
61 elsif ( $pos > $start_position ) {
62 0 croak "position $start_position not found in binlog, next one after was: $pos, at $.";
63 }
64 }
65  
66 2 my ( $new_row, $old_row, $row );
67 2 my $end_pos;
68  
69 9 ▶ return sub {
Subroutine __ANON__: called CC=29 SCAR=36.5
70 9 ▶ while ( <$handle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $handle)) 55 0 55
71 # position update
72 55 ▶ if ( substr( $_, 0, 5 ) eq '# at ' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_, 0, 5) eq "# at ") 11 44 55
73 11 $pos = 0 + substr( $_, 5 );
74 11 next;
75 }
76  
77 # EOF?
78 last
79 44 ▶ if substr( $_, 0, 9 ) eq 'DELIMITER';
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, 0, 9) eq "DELIMITER" 2 42 44
80  
81 # new transaction?
82 # transactions have a BEGIN and a COMMIT
83 42 ▶ if ( substr( $_, 0, 5 ) eq 'BEGIN' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_, 0, 5) eq "BEGIN") 7 35 42
84 7 my %transaction;
85  
86 7 ▶ while ( <$handle> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $handle)) 132 0 132
87 # EOT?
88 last
89 132 ▶ if substr( $_, 0, 6 ) eq 'COMMIT';
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, 0, 6) eq "COMMIT" 7 125 132
90  
91 # end of current record indicated by
92 # #<date and time at server> end_log_pos <position>
93 125 ▶ if ( substr( $_, 0, 2 ) eq '#1' # FIXME in 2020
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_, 0, 2) eq "#1" and /^#\d.*end_log_pos (\d+)/) 25 100 125
Condition66%2/3
substr($_, 0, 2) eq "#1" and /^#\d.*end_log_pos (\d+)/
!ll&&!rl&&r
100025
MC/DC50%1/2
substr($_, 0, 2) eq "#1" and /^#\d.*end_log_pos (\d+)/
substr($_, 0, 2) eq "#1" /^#\d.*end_log_pos (\d+)/
Truth table66%2/3
A and B
A= substr($_, 0, 2) eq "#1"B= /^#\d.*end_log_pos (\d+)/
ABresult
0X0
100
111
94 && /^#\d.*end_log_pos (\d+)/ ) {
95 25 $end_pos = $1;
96 25 next;
97 }
98  
99 # not RBR? not interested
100 100 ▶ substr( $_, 0, 4 ) eq '### '
Branch: 1 branch
BranchTrueFalseTotal
substr($_, 0, 4) eq "### " or next 68 32 100
101 or next;
102  
103 # RBR statements format:
104  
105 # INSERT INTO ...
106 # SET
107 # values...
108  
109 # UPDATE ...
110 # WHERE
111 # old values...
112 # SET
113 # new values...
114  
115 # DELETE FROM ...
116 # WHERE
117 # old values...
118  
119 # so... SET defines new values
120 # WHERE defines old values
121  
122 68 ▶ if ( $old_row && substr( $_, 4, 5 ) eq 'WHERE' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($old_row and substr($_, 4, 5) eq 'WHERE') { } 6 62 68
elsif ($new_row and substr($_, 4, 3) eq 'SET') { } 12 50 62
elsif (/^### INSERT INTO (\S+)/) { } 8 42 50
elsif (/^### DELETE FROM (\S+)/) { } 2 40 42
elsif (/^### UPDATE (\S+)/) { } 4 36 40
elsif ($row and /@(\d+)=(.+)/) { } 36 0 36
Condition100%3/3
$old_row and substr($_, 4, 5) eq "WHERE"
!ll&&!rl&&r
34286
MC/DC100%2/2
$old_row and substr($_, 4, 5) eq "WHERE"
$old_row substr($_, 4, 5) eq "WHERE"
Truth table100%3/3
A and B
A= $old_rowB= substr($_, 4, 5) eq "WHERE"
ABresult
0X0
100
111
123 6 $row = $old_row;
124 }
125 ▶ elsif ( $new_row && substr ( $_, 4, 3 ) eq 'SET' ) {
Condition100%3/3
$new_row and substr($_, 4, 3) eq "SET"
!ll&&!rl&&r
84212
MC/DC100%2/2
$new_row and substr($_, 4, 3) eq "SET"
$new_row substr($_, 4, 3) eq "SET"
Truth table100%3/3
A and B
A= $new_rowB= substr($_, 4, 3) eq "SET"
ABresult
0X0
100
111
126 12 $row = $new_row;
127 }
128 elsif ( /^### INSERT INTO (\S+)/ ) {
129 8 $old_row = $row = undef;
130  
131 8 ▶ if ( ! @interesting_tables
Branch: 1 branch
BranchTrueFalseTotal
if (not @interesting_tables or exists $interesting_tables{$1}) { } 8 0 8
Condition33%1/3
not @interesting_tables or exists $interesting_tables{$1}
l!l&&r!l&&!r
800
MC/DC0%0/2
not @interesting_tables or exists $interesting_tables{$1}
not @interesting_tables exists $interesting_tables{$1}
Truth table33%1/3
not(A) or B
A= not @interesting_tablesB= exists $interesting_tables{$1}
ABresult
1X1
011
000
132 || exists $interesting_tables{ $1 } ) {
133 8 $new_row = [];
134 8 push @{ $transaction{ $1 } }, [ $new_row ];
135 }
136 else {
137 0 $new_row = undef;
138 }
139 }
140 elsif ( /^### DELETE FROM (\S+)/ ) {
141 2 $new_row = $row = undef;
142  
143 2 ▶ if ( ! @interesting_tables
Branch: 1 branch
BranchTrueFalseTotal
if (not @interesting_tables or exists $interesting_tables{$1}) { } 2 0 2
Condition33%1/3
not @interesting_tables or exists $interesting_tables{$1}
l!l&&r!l&&!r
200
MC/DC0%0/2
not @interesting_tables or exists $interesting_tables{$1}
not @interesting_tables exists $interesting_tables{$1}
Truth table33%1/3
not(A) or B
A= not @interesting_tablesB= exists $interesting_tables{$1}
ABresult
1X1
011
000
144 || exists $interesting_tables{ $1 } ) {
145 2 $old_row = [];
146 2 push @{ $transaction{ $1 } }, [ undef, $old_row ];
147 }
148 else {
149 0 $old_row = undef;
150 }
151 }
152 elsif ( /^### UPDATE (\S+)/ ) {
153 4 $row = undef;
154  
155 4 ▶ if ( ! @interesting_tables
Branch: 1 branch
BranchTrueFalseTotal
if (not @interesting_tables or exists $interesting_tables{$1}) { } 4 0 4
Condition33%1/3
not @interesting_tables or exists $interesting_tables{$1}
l!l&&r!l&&!r
400
MC/DC0%0/2
not @interesting_tables or exists $interesting_tables{$1}
not @interesting_tables exists $interesting_tables{$1}
Truth table33%1/3
not(A) or B
A= not @interesting_tablesB= exists $interesting_tables{$1}
ABresult
1X1
011
000
156 || exists $interesting_tables{ $1 } ) {
157 4 $new_row = [];
158 4 $old_row = [];
159 4 push @{ $transaction{ $1 } }, [ $new_row, $old_row ];
160 }
161 else {
162 0 $new_row = $old_row = undef;
163 }
164 }
165 ▶ elsif ( $row && /@(\d+)=(.+)/ ) {
Condition33%1/3
$row and /@(\d+)=(.+)/
!ll&&!rl&&r
0036
MC/DC0%0/2
$row and /@(\d+)=(.+)/
$row /@(\d+)=(.+)/
Truth table33%1/3
A and B
A= $rowB= /@(\d+)=(.+)/
ABresult
0X0
100
111
166 36 $row->[ $1 - 1 ] = $2;
167 }
168 }
169  
170 # did actually get EOT?
171 7 ▶ $_ && substr( $_, 0, 6 ) eq 'COMMIT'
Branch: 1 branch
BranchTrueFalseTotal
$_ and substr($_, 0, 6) eq "COMMIT" or croak('truncated binlog file, at ' . $handle->input_line_number . ": $_") 7 0 7
Condition33%1/3
$_ and substr($_, 0, 6) eq "COMMIT"
!ll&&!rl&&r
007
MC/DC0%0/2
$_ and substr($_, 0, 6) eq "COMMIT"
$_ substr($_, 0, 6) eq "COMMIT"
Truth table33%1/3
A and B
A= $_B= substr($_, 0, 6) eq "COMMIT"
ABresult
0X0
100
111
172 or croak "truncated binlog file, at " . $handle->input_line_number() . ": $_";
173  
174 # last 'end_log_pos' seen
175 7 $transaction{ end_position } = $end_pos;
176 # first 'at' seen
177 7 $transaction{ start_position } = $pos;
178  
179 7 return \%transaction;
180 }
181 }
182  
183 # actually got EOF?
184 2 ▶ $_ && substr( $_, 0, 9 ) eq 'DELIMITER'
Branch: 1 branch
BranchTrueFalseTotal
$_ and substr($_, 0, 9) eq "DELIMITER" or croak('truncated binlog file, at ' . $handle->input_line_number . ": $_") 2 0 2
Condition33%1/3
$_ and substr($_, 0, 9) eq "DELIMITER"
!ll&&!rl&&r
002
MC/DC0%0/2
$_ and substr($_, 0, 9) eq "DELIMITER"
$_ substr($_, 0, 9) eq "DELIMITER"
Truth table33%1/3
A and B
A= $_B= substr($_, 0, 9) eq "DELIMITER"
ABresult
0X0
100
111
185 or croak "truncated binlog file, at " . $handle->input_line_number() . ": $_";
186  
187 2 return;
188 2 };
189 }
190  
191 1 1;
192  
193 __END__
↑ Summary