lib/MySQL/Replication/Client.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 166 CC 20 SCAR 42.9
CC
20
Cov
0%
GetQueries
156.0
RequestQueries
30.0
ExecuteQueries
20.0
CRAP 73.3
1 package MySQL::Replication::Client;
2  
3 use strict;
4 use warnings;
5  
6 use base qw{ Class::Accessor };
7  
8 use DBI;
9 use MySQL::Replication::Command;
10  
11 BEGIN {
12 __PACKAGE__->mk_accessors( qw{
13 Dbh
14 DBName
15 RelaySocket
16 RelayBuffer
17 Host
18 Binlog
19 });
20 }
21  
22 sub new {
23 my ( $Class, %Args ) = @_;
24  
25 foreach my $Required ( qw{ Dbh DBName RelaySocket Host Binlog } ) {
26 if ( not $Args{$Required} ) {
27 die "Required parameter '$Required' missing";
28 }
29 }
30  
31 return $Class->SUPER::new( \%Args );
32 }
33  
34 sub RequestQueries {
35 my ( $Self ) = @_;
36  
37 my ( $StartLog, $StartPos ) = $Self->Dbh()->selectrow_array( sprintf( q{
38 SELECT Log, Pos
39 FROM %s.SourcePosition
40 WHERE Host = ?
41 AND Binlog = ?
42 }, $Self->DBName() ),
43 undef,
44 $Self->Host(),
45 $Self->Binlog(),
46 );
47  
48 if ( not defined $StartLog or not defined $StartPos ) {
49 die "Error reading source position ($DBI::errstr)" if $DBI::errstr;
50 die sprintf( "No source position for source (%s:%s)", $Self->Host(), $Self->Binlog() );
51 }
52  
53 my $Command = MySQL::Replication::Command->new(
54 Command => 'GET',
55 Headers => {
56 Host => $Self->Host(),
57 Binlog => $Self->Binlog(),
58 StartLog => $StartLog,
59 StartPos => $StartPos,
60 },
61 );
62  
63 if ( not $Command ) {
64 die "Error creating request ($MySQL::Replication::Command::Errstr)";
65 }
66  
67 if ( not $Command->SendToSocket( $Self->RelaySocket() ) ) {
68 die "Error sending request ($MySQL::Replication::Command::Errstr)";
69 }
70  
71 return 1;
72 }
73  
74 sub GetQueries {
75 my ( $Self ) = @_;
76  
77 my ( $InsideTransaction, @BufferedQueries );
78  
79 while ( 1 ) {
80 my $Query = MySQL::Replication::Command->NewFromSocket(
81 Socket => $Self->RelaySocket(),
82 Buffer => \$Self->{RelayBuffer},
83 );
84  
85 if ( not $Query ) {
86 die "Error reading response ($MySQL::Replication::Command::Errstr)";
87 }
88  
89 if ( $Query->Command() ne 'QUERY' ) {
90 die sprintf( "Invalid command '%s' in response", $Query->Command() );
91 }
92  
93 foreach my $Required ( qw{ Timestamp Log Pos Length } ) {
94 next if $Query->Headers()->{$Required};
95 die "Required header '$Required' missing in response";
96 }
97  
98 if ( $Query->Body() =~ /^\s*ROLLBACK\s*/i ) {
99 $InsideTransaction = undef;
100 @BufferedQueries = ();
101 next;
102 }
103  
104 if ( $Query->Body() =~ /^\s*(?:BEGIN|START\s+TRANSACTION)\s*/i ) {
105 $InsideTransaction = 1;
106 next;
107 }
108  
109 if ( $Query->Body() =~ /^\s*SET\s*/i ) {
110 push @BufferedQueries, $Query;
111 next;
112 }
113  
114 if ( $InsideTransaction ) {
115 if ( $Query->Body() =~ /^\s*COMMIT\s*/i ) {
116 if ( not @BufferedQueries ) {
117 $InsideTransaction = undef;
118 next;
119 }
120  
121 return @BufferedQueries;
122 }
123  
124 push @BufferedQueries, $Query;
125 next;
126 }
127  
128 if ( $Query->Body() =~ /^\s*COMMIT\s*/i ) {
129 return @BufferedQueries if @BufferedQueries;
130 next;
131 }
132  
133 return @BufferedQueries, $Query;
134 }
135 }
136  
137 sub ExecuteQueries {
138 my ( $Self, @Queries ) = @_;
139  
140 if ( not @Queries ) {
141 die "No queries to execute";
142 }
143  
144 $Self->Dbh()->do( 'SET SQL_LOG_BIN=0' )
145 or die "Error turning off binlogging ($DBI::errstr)";
146  
147 $Self->Dbh()->do( 'BEGIN' )
148 or die "Error starting transaction ($DBI::errstr)";
149  
150 my $Database = '';
151  
152 foreach my $Query ( @Queries ) {
153 if ( $Query->Headers()->{Database} and $Query->Headers()->{Database} ne $Database ) {
154 if ( $Query->Body() !~ /\A\s*(?:CREATE|DROP)\s*(?:DATABASE|SCHEMA)\s+/smi ) {
155 $Database = $Query->Headers()->{Database};
156 $Self->Dbh()->do( "USE $Database" )
157 or die "Error using database '$Database' ($DBI::errstr): " . $Query->Stringify();
158 }
159 }
160  
161 $Self->Dbh()->do( "SET TIMESTAMP=" . $Query->Headers()->{Timestamp} )
162 or die "Error setting timestamp ($DBI::errstr)";
163  
164 $Self->Dbh()->do( $Query->Body() )
165 or die "Error executing query ($DBI::errstr): " . $Query->Stringify();
166 }
167  
168 $Self->Dbh()->do( sprintf( q{
169 UPDATE %s.SourcePosition
170 SET Log = ?,
171 Pos = ?
172 WHERE Host = ?
173 AND Binlog = ?
174 }, $Self->DBName() ),
175 undef,
176 $Queries[-1]->Headers()->{Log},
177 $Queries[-1]->Headers()->{Pos},
178 $Self->Host(),
179 $Self->Binlog(),
180 ) or die "Error updating source position ($DBI::errstr)";
181  
182 $Self->Dbh()->do( 'COMMIT' )
183 or die "Error finishing transaction ($DBI::errstr)";
184  
185 return 1;
186 }
187  
188 1;
189  
190 __END__