MySQL/Forwarder.pm

filter: Statementstmt 14.3% 49 / 341 Branchbran 0.0% 0 / 118 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 31.5% 12 / 38 Podpod 0.0% 0 / 6 totaltotal 11.8% 61 / 513 CC 72 SCAR 41.3
CC
72
Cov
11%
_forwarder_start
156.0
_read_resultset
137.2
_forwarder_client_input
132.0
CRAP 62.4
↑ Summary MySQL.pm »
1 package # ignore CPAN ..
2 POE::Component::Proxy::MySQL::Forwarder;
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MooseX::MethodAttributes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use strict; # for kwalitee
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings; # for kwalitee
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use POSIX qw(errno_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POE qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 Wheel::ReadWrite
13 Wheel::SocketFactory
14 Filter::Stream
15 1 );
16 1 ▶use MySQL::Packet qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 :encode
18 :decode
19 :COM
20 :CLIENT
21 :SERVER
22 :debug
23 1 );
24 1 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 my $VERSION = '0.01_01';
28  
29 1 has 'namespace' => (is => 'rw', isa => 'Str');
30 1 has 'packet_count' => (is => 'rw', isa => 'Int');
31  
32 1 has 'client_wheel' => (is => 'rw', isa => 'Any');
33 1 has 'server_wheel' => (is => 'rw', isa => 'Any');
34  
35 1 has 'server_heap' => (is => 'rw', isa => 'Any');
36  
37 1 has 'heap' => (is => 'rw', isa => 'Any');
38  
39 1 has 'client_input_data' => (is => 'rw', isa => 'Any');
40 1 has 'server_input_data' => (is => 'rw', isa => 'Any');
41  
42 1 has 'server_request_callback' => (is => 'rw', isa => 'Str');
43  
44 1 my @dispatchers;
45  
46 1 my $max_connections_per_child = 256;
47  
48 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
49 0 my ($self, $opt) = @_;
50  
51  
52 0 $self->server_heap($opt->{heap});
53
54 0 my $server_heap = $self->server_heap;
55 0 $server_heap->{active_connections}->{$$}++;
56 0 $server_heap->{connections_per_child}->{$$}++;
57
58 POE::Session->create(
59 object_states => [
60 $self => {
61 _start => '_forwarder_start',
62 _stop => '_forwarder_stop',
63 client_input => '_forwarder_client_input',
64 client_error => '_forwarder_client_error',
65 server_connect => '_forwarder_server_connect',
66 server_input => '_forwarder_server_input',
67 server_error => '_forwarder_server_error',
68 dispatch_input => '_dispatch_input',
69 },
70 ],
71 0 args => [$opt->{socket}, $opt->{peer_addr}, $opt->{peer_port}, $opt->{remote_addr}, $opt->{remote_port}, $opt->{heap}]
72 );
73  
74 }
75  
76  
77 0 ▶sub _forwarder_start {
Subroutine _forwarder_start: not called CC=12 SCAR=50.5
78 0 my ($self, $heap, $session, $socket, $peer_host, $peer_port, $remote_addr,
79 $remote_port)
80 = @_[OBJECT, HEAP, SESSION, ARG0, ARG1, ARG2, ARG3, ARG4, ARG5];
81
82 0 $heap->{log} = $session->ID;
83 0 $peer_host = inet_ntoa($peer_host);
84 0 $heap->{peer_host} = $peer_host;
85 0 $heap->{peer_port} = $peer_port;
86 0 $heap->{remote_addr} = $remote_addr;
87 0 $heap->{remote_port} = $remote_port;
88
89 0 $self->heap($heap);
90  
91 0 my @modules = findsubmod($self->namespace);
92
93 0 foreach my $module (@modules) {
94 0 ▶ next unless $module;
Branch: 1 branch
BranchTrueFalseTotal
unless $module 0 0 0
95  
96 0 eval('use '.$module); # to be replaced some day
97 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
98
99 0 foreach my $method ($module->meta->get_method_list) {
100 0 ▶ next unless $module->meta->get_method($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $module->meta->get_method($method) 0 0 0
101
102 0 my $module_prefix = $module;
103 0 $module_prefix =~ s/::/_/g;
104  
105
106 0 ref($self)->meta->add_method(
107 $module_prefix."_".$method,
108 $module->meta->get_method($method)
109 );
110
111 }
112  
113 0 $module->meta->apply( $self->meta );
114  
115 0 foreach my $method ($module->meta->get_method_list) {
116 0 ▶ next unless $module->meta->get_method($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $module->meta->get_method($method) 0 0 0
117
118 0 my $attrs;
119
120 0 eval {
121 0 $attrs = $module->meta->get_method($method)->attributes;
122 };
123
124 0 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
125
126 0 my $module_prefix = $module;
127 0 $module_prefix =~ s/::/_/g;
128
129 0 $_[KERNEL]->state( $module_prefix."_".$method, $self );
130
131 0 foreach my $attr (@{$attrs}) {
132
133 0 ▶ if ($attr =~ /Regexp\(('|")(.*)('|")\)/io) { # "
Branch: 2 branches
BranchTrueFalseTotal
if ($attr =~ /Regexp\(('|")(.*)('|")\)/io) { } 0 0 0
elsif ($attr =~ /Match\(('|")(.*)('|")\)/io) { } 0 0 0
134 0 my $eval_str = 'push @dispatchers, {
135 regexp => '.$2.',
136 method => $method,
137 class => $module,
138 };';
139 0 eval($eval_str);
140 }
141 elsif ($attr =~ /Match\(('|")(.*)('|")\)/io) { # "
142 0 push @dispatchers, {
143 match => $2,
144 method => $method,
145 class => $module,
146 };
147 }
148
149 }
150
151 }
152
153 }
154
155 0 $heap->{state} = 'connecting';
156 0 $heap->{queue} = [];
157
158 0 $heap->{driver} = POE::Driver::SysRW->new;
159 0 $heap->{filter} = POE::Filter::Stream->new;
160  
161 $heap->{client_wheel} = POE::Wheel::ReadWrite->new(
162 Handle => $socket,
163 Driver => $heap->{driver},
164 Filter => $heap->{filter},
165 0 InputEvent => 'client_input',
166 ErrorEvent => 'client_error',
167 );
168
169 0 $heap->{server_wheel} = POE::Wheel::SocketFactory->new(
170 RemoteAddress => $remote_addr,
171 RemotePort => $remote_port,
172 SuccessEvent => 'server_connect',
173 FailureEvent => 'server_error',
174 );
175  
176 0 my $server_heap = $self->server_heap;
177
178 0 $self->client_wheel($heap->{client_wheel});
179 0 $self->server_wheel($heap->{server_wheel});
180
181 }
182  
183 0 ▶sub _forwarder_stop {
Subroutine _forwarder_stop: not called CC=1 SCAR=6.9
184 0 my $heap = $_[HEAP];
185
186 0 $heap->{server_heap}->{active_connections}->{$$}--;
187 }
188  
189 0 ▶sub _forwarder_client_input {
Subroutine _forwarder_client_input: not called CC=11 SCAR=48.8
190 0 my ($self, $heap, $session, $kernel, $input) =
191 @_[OBJECT, HEAP, SESSION, KERNEL, ARG0];
192  
193 0 $self->client_input_data($input);
194
195 0 ▶ if ($heap->{state} eq 'connecting') {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{'state'} eq 'connecting') { } 0 0 0
196 0 push @{$heap->{queue}}, $input;
197 }
198 else {
199
200 0 $self->packet_count(unpack('C', substr($input, 3, 1)));
201
202 0 my $command = unpack('C', substr($input, 4, 1));
203
204 0 ▶ if ($command == COM_QUERY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command == 3) { } 0 0 0
205
206 0 my $query = $input;
207 0 $query = substr($query, 5);
208 0 chomp($query);
209
210 0 my $event;
211 my @placeholders;
212 0 my $dispatcher;
213
214 0 foreach my $tmp_dispatcher (@dispatchers) {
215 0 ▶ if (ref($tmp_dispatcher->{regexp}) eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $tmp_dispatcher->{'regexp'} eq 'Regexp') { } 0 0 0
elsif (exists $tmp_dispatcher->{'match'}) { } 0 0 0
216 0 ▶ if (@placeholders = $query =~ $tmp_dispatcher->{regexp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@placeholders = $query =~ /$tmp_dispatcher->{"regexp"}/) 0 0 0
217 0 $dispatcher = $tmp_dispatcher;
218 0 last;
219 }
220 }
221 elsif (exists($tmp_dispatcher->{match})) {
222 0 ▶ if ($query eq $tmp_dispatcher->{match}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query eq $tmp_dispatcher->{"match"}) 0 0 0
223 0 $dispatcher = $tmp_dispatcher;
224 0 last;
225 }
226 }
227 }
228
229 0 ▶ if ($dispatcher->{class} && $dispatcher->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatcher->{'class'} and $dispatcher->{'method'}) { } 0 0 0
Condition0%0/3
$dispatcher->{"class"} and $dispatcher->{"method"}
!ll&&!rl&&r
000
MC/DC0%0/2
$dispatcher->{"class"} and $dispatcher->{"method"}
$dispatcher->{"class"} $dispatcher->{"method"}
Truth table0%0/3
A and B
A= $dispatcher->{"class"}B= $dispatcher->{"method"}
ABresult
0X0
100
111
230  
231 0 my $specific_method = $dispatcher->{class};
232 0 $specific_method =~ s/::/_/g;
233 0 $specific_method = $specific_method.'_'.$dispatcher->{method};
234
235 0 $self->packet_count($self->packet_count + 1);
236 0 $kernel->call($_[SESSION], $specific_method, $query, \@placeholders);
237
238 0 ▶ print $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
239
240 }
241 else {
242 0 $self->release_client;
243 }
244
245 }
246 else {
247 0 $self->release_client;
248 }
249 }
250 }
251  
252  
253 0 ▶sub _forwarder_client_error {
Subroutine _forwarder_client_error: not called CC=1 SCAR=6.9
254 0 my ($self, $kernel, $heap, $operation, $errnum, $errstr) =
255 @_[OBJECT, KERNEL, HEAP, ARG0, ARG1, ARG2];
256  
257 0 my $server_heap = $self->server_heap;
258 0 $server_heap->{active_connections}->{$$}--;
259  
260 0 delete $heap->{client_wheel};
261 0 delete $heap->{server_wheel};
262  
263 0 $self->client_wheel(undef);
264 0 $self->server_wheel(undef);
265  
266 }
267  
268 0 ▶sub _forwarder_server_connect {
Subroutine _forwarder_server_connect: not called CC=2 SCAR=17.9
269 0 my ($self, $kernel, $session, $heap, $socket)
270 = @_[OBJECT, KERNEL, SESSION, HEAP, ARG0];
271
272 0 my ($local_port, $local_addr) = unpack_sockaddr_in(getsockname($socket));
273 0 $local_addr = inet_ntoa($local_addr);
274
275 0 $heap->{server_wheel} = POE::Wheel::ReadWrite->new(
276 Handle => $socket,
277 Driver => POE::Driver::SysRW->new( BlockSize => 4096 ),
278 Filter => POE::Filter::Stream->new,
279 InputEvent => 'server_input',
280 ErrorEvent => 'server_error',
281 );
282
283 0 $self->server_wheel($heap->{server_wheel});
284
285 0 $heap->{state} = 'connected';
286
287 0 foreach my $pending (@{$heap->{queue}}) {
288 0 $kernel->call($session, 'client_input', $pending);
289 }
290 0 $heap->{queue} = [];
291 }
292  
293 0 ▶sub _forwarder_server_input {
Subroutine _forwarder_server_input: not called CC=1 SCAR=6.9
294 0 my ($self, $heap, $input) = @_[OBJECT, HEAP, ARG0];
295  
296 0 $self->server_input_data($input);
297
298 0 $self->release_server;
299  
300 }
301  
302 0 ▶sub _forwarder_server_error {
Subroutine _forwarder_server_error: not called CC=1 SCAR=6.9
303 0 my ($kernel, $heap, $operation, $errnum, $errstr) =
304 @_[KERNEL, HEAP, ARG0, ARG1, ARG2];
305  
306 0 delete $heap->{client_wheel};
307 0 delete $heap->{server_wheel};
308 }
309  
310 0 ▶sub _server_send_query {
Subroutine _server_send_query: not called CC=3 SCAR=24.8
311 0 my ($self, $opt) = (@_);
312
313 0 my $wheel = $self->server_wheel;
314
315 0 ▶ unless (exists($opt->{callback})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt->{'callback'}) { } 0 0 0
316 0 $opt->{callback} = '';
317 }
318 else {
319 0 my ($package, $filename, $line) = caller;
320
321 0 $package =~ s/::/_/g;
322 0 $opt->{callback} = $package.'_'.$opt->{callback};
323 }
324
325 0 my $heap = $self->heap;
326
327 0 $heap->{server_request_callback} = $opt->{callback};
328  
329 0 ▶ if ($opt->{callback} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'callback'} ne '') { } 0 0 0
330 0 $wheel->event(InputEvent => 'dispatch_input');
331 }
332 else {
333 0 $wheel->event(InputEvent => 'server_input');
334 }
335
336 0 my $packet_body = mysql_encode_com_query($opt->{query});
337 0 my $packet_head = mysql_encode_header($packet_body);
338 0 $wheel->put($packet_head.$packet_body);
339
340 }
341  
342 0 ▶sub _dispatch_input {
Subroutine _dispatch_input: not called CC=7 SCAR=40.3
343 0 my ($self, $heap, $session, $kernel, $input) =
344 @_[OBJECT, HEAP, SESSION, KERNEL, ARG0];
345
346 0 my (@resultset, $result);
347
348 0 ▶ if (length($input) == 4096) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input == 4096) { } 0 0 0
349
350 0 (@resultset, $result) = $self->read_resultset($self->server_input_data.$input);
351
352 0 ▶ if ($result->{'field_count'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'field_count'}) { } 0 0 0
353 # OK
354
355 0 $heap->{'input'} = $input;
356
357 0 my $query;
358
359 0 my $wheel = $self->server_wheel;
360 0 $wheel->event(InputEvent => 'server_input');
361
362 0 ▶ if ($heap->{server_request_callback}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"server_request_callback"}) 0 0 0
363 0 ▶ if (ref($heap->{server_request_callback}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $heap->{'server_request_callback'} eq 'CODE') { } 0 0 0
364 0 my $code = $heap->{server_request_callback};
365 0 &$code;
366 }
367 else {
368 0 $kernel->call($session, $heap->{server_request_callback}, \@resultset, $result, $input);
369 }
370 }
371
372 0 $self->server_input_data('');
373
374 }
375 else {
376 0 $self->server_input_data($self->server_input_data.$input);
377 }
378
379 }
380 else {
381
382 0 (@resultset, $result) = $self->read_resultset($self->server_input_data.$input);
383
384 0 $heap->{'input'} = $input;
385
386 0 my $query;
387
388 0 my $wheel = $self->server_wheel;
389 0 $wheel->event(InputEvent => 'server_input');
390
391 0 ▶ if ($heap->{server_request_callback}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"server_request_callback"}) 0 0 0
392 0 ▶ if (ref($heap->{server_request_callback}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $heap->{'server_request_callback'} eq 'CODE') { } 0 0 0
393 0 my $code = $heap->{server_request_callback};
394 0 &$code;
395 }
396 else {
397 0 $kernel->call($session, $heap->{server_request_callback}, \@resultset, $result, $input);
398 }
399 }
400
401 0 $self->server_input_data('');
402
403 }
404  
405  
406  
407 }
408  
409  
410 0 ▶sub _read_resultset {
Subroutine _read_resultset: not called CC=12 SCAR=49.2
411 0 my ($self, $input) = (@_);
412
413 0 my $rc;
414 my $packet;
415
416 0 my $field_count;
417 0 my @headers;
418 0 my @resultset;
419
420 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
421
422 0 $rc = mysql_decode_header($packet, $input);
423
424 0 ▶ if ($rc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 0) { } 0 0 0
425 0 $input = substr($input, 4);
426 0 $rc = mysql_decode_result($packet, $input);
427 0 ▶ if ($rc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 0) 0 0 0
428 0 ▶ if ($packet->{field_count}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($packet->{'field_count'}) { } 0 0 0
elsif ($packet->{'error'}) { } 0 0 0
429
430 0 $field_count = $packet->{field_count};
431
432 0 $input = substr($input, $packet->{packet_size});
433
434 0 ▶ for (my $i = 1; $i <= $field_count; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $field_count) 0 0 0
435
436 0 $packet = ();
437 0 $rc = mysql_decode_header($packet, $input);
438 0 ▶ if ($rc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 0) 0 0 0
439 0 $input = substr($input, 4);
440 0 $rc = mysql_decode_field($packet, $input);
441 0 ▶ if ($rc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 0) 0 0 0
442 0 push @headers, $packet->{name};
443 0 $input = substr($input, $packet->{packet_size});
444 }
445 }
446 }
447
448 0 push @resultset, \@headers;
449
450 0 $input = substr($input, 9);
451
452 0 ▶ while (length($input) > 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $input > 4) 0 0 0
453
454 0 $packet = ();
455
456 0 $rc = mysql_decode_header($packet, $input);
457
458 0 $input = substr($input, 4);
459
460 0 ▶ last if mysql_test_end($packet, $input);
Branch: 1 branch
BranchTrueFalseTotal
if mysql_test_end($packet, $input) 0 0 0
461
462 0 $rc = mysql_decode_row($packet, $input);
463
464 0 ▶ if ($rc > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 0) 0 0 0
465
466 0 ▶ if ($packet->{'end'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet->{'end'}) { } 0 0 0
467 0 last;
468 }
469 else {
470 0 $input = substr($input, $packet->{packet_size});
471
472 0 push @resultset, $packet->{row};
473
474 }
475 }
476 }
477
478
479 }
480 elsif ($packet->{error}) {
481 0 print "mysql_decode_result error\n";
482 0 print Dumper($packet);
483 0 $self->client_send_error($packet->{message});
484 }
485 else {
486 0 return (@resultset, $packet);
487 }
488 }
489 }
490 else {
491 0 return (@resultset, $packet);
492 }
493
494 0 return (@resultset, {
495 field_count => $field_count,
496 });
497 }
498  
499  
500  
501 0 ▶sub client_send_ok {
Subroutine client_send_ok: not called CC=3 SCAR=24.8
Pod undocumented
502 0 my ($self, $message, $affected_rows, $insert_id, $printing_count) = @_;
503
504 0 my $data;
505
506 0 ▶ $affected_rows = 0 unless defined $affected_rows;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $affected_rows 0 0 0
507 0 ▶ $printing_count = 0 unless defined $printing_count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $printing_count 0 0 0
508
509 0 $data .= "\0";
510 0 $data .= _length_coded_binary($affected_rows);
511 0 $data .= _length_coded_binary($insert_id);
512 0 $data .= pack('v', SERVER_STATUS_AUTOCOMMIT);
513 0 $data .= pack('v', $printing_count);
514 0 $data .= _length_coded_string($message);
515
516 0 return $self->_write_to_client( $data, 1);
517 }
518  
519 0 ▶sub _add_header {
Subroutine _add_header: not called CC=2 SCAR=17.9
520 0 my ($self, $message, $reinit) = @_;
521
522 0 my $header;
523 0 $header .= substr(pack('V',length($message)),0,3);
524 0 $header .= chr($self->packet_count % 256);
525
526 0 ▶ if ($reinit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reinit) { } 0 0 0
527 0 $self->packet_count(0);
528 }
529 else {
530 0 $self->packet_count($self->packet_count + 1);
531 }
532
533 0 return $header.$message;
534 }
535  
536 0 ▶sub release_client {
Subroutine release_client: not called CC=1 SCAR=6.9
Pod undocumented
537 0 my ($self) = @_;
538
539 0 $self->_release($self->server_wheel, $self->client_input_data);
540 0 $self->client_input_data('');
541 }
542  
543 0 ▶sub release_server {
Subroutine release_server: not called CC=1 SCAR=6.9
Pod undocumented
544 0 my ($self) = @_;
545  
546 0 $self->_release($self->client_wheel, $self->server_input_data);
547 0 $self->server_input_data('');
548
549 }
550  
551 0 ▶sub _release {
Subroutine _release: not called CC=2 SCAR=17.9
552 0 my ($self, $wheel, $data) = @_;
553
554 0 ▶ defined($wheel) and $wheel->put($data);
Branch: 1 branch
BranchTrueFalseTotal
defined $wheel and $wheel->put($data) 0 0 0
555 }
556  
557  
558 0 ▶sub _write_to_client {
Subroutine _write_to_client: not called CC=1 SCAR=6.9
559 0 my ($self, $message, $reinit) = @_;
560  
561 0 $message = $self->_add_header($message, $reinit);
562
563 0 $self->client_wheel->put($message);
564
565 0 return $message;
566 }
567  
568 0 ▶sub _length_coded_string {
Subroutine _length_coded_string: not called CC=3 SCAR=24.8
569 0 my ($string) = @_;
570 0 ▶ return chr(0) if (not defined $string or $string eq '');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $string or $string eq "" 0 0 0
Condition0%0/3
not defined $string or $string eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $string or $string eq ""
not defined $string $string eq ""
Truth table0%0/3
not(A) or B
A= not defined $stringB= $string eq ""
ABresult
1X1
011
000
571 0 return _length_coded_binary(length($string)).$string;
572 }
573  
574 0 ▶sub _length_coded_binary {
Subroutine _length_coded_binary: not called CC=5 SCAR=34.0
575 0 my ($number) = @_;
576
577 0 ▶ if (not defined $number) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $number) { } 0 0 0
elsif ($number < 251) { } 0 0 0
elsif ($number < 65536) { } 0 0 0
elsif ($number < 16777216) { } 0 0 0
578 0 return chr(251);
579 }
580 elsif ($number < 251) {
581 0 return chr($number);
582 }
583 elsif ($number < 0x10000) {
584 0 return chr(252).pack('v', $number);
585 }
586 elsif ($number < 0x1000000) {
587 0 return chr(253).substr(pack('V', $number), 0, 3);
588 }
589 else {
590 0 return chr(254).pack('V', $number >> 32).pack('V', $number & 0xffffffff);
591 }
592 }
593  
594 0 ▶sub _send_eof {
Subroutine _send_eof: not called CC=5 SCAR=34.0
595 0 my ($self, $warning_count, $server_status, $reinit) = @_;
596  
597 0 my $payload;
598  
599 0 ▶ $server_status = SERVER_STATUS_AUTOCOMMIT |
Branch: 1 branch
BranchTrueFalseTotal
unless defined $server_status 0 0 0
600 SERVER_QUERY_NO_INDEX_USED if not defined $server_status;
601  
602 0 ▶ $warning_count = 0 if !defined $warning_count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $warning_count 0 0 0
603  
604 0 $payload .= chr(0xfe);
605 0 ▶ $payload .= pack('v', $warning_count) if defined $warning_count;
Branch: 1 branch
BranchTrueFalseTotal
if defined $warning_count 0 0 0
606 0 ▶ $payload .= pack('v', $server_status) if defined $server_status;
Branch: 1 branch
BranchTrueFalseTotal
if defined $server_status 0 0 0
607  
608 0 return $self->_write_to_client( $payload, $reinit);
609 }
610  
611  
612 0 ▶sub client_send_results {
Subroutine client_send_results: not called CC=2 SCAR=17.9
Pod undocumented
613 0 my ($self, $definitions, $data, $opt) = @_;
614  
615 0 ▶ unless (ref($data) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data eq "ARRAY") 0 0 0
616 0 $self->client_send_error('Internal error');
617 0 return;
618 }
619 0 $self->_send_header_packet(scalar(@{$definitions}), scalar(@{$data}));
620 0 $self->_send_definitions($definitions, $opt);
621 0 $self->_send_eof();
622 0 $self->_send_rows($data);
623 0 $self->_send_eof(undef,undef,1);
624 }
625  
626  
627  
628 0 ▶sub client_send_error {
Subroutine client_send_error: not called CC=4 SCAR=30.0
Pod undocumented
629 0 my ($self, $message, $errno, $sqlstate) = @_;
630
631 0 ▶ $message = 'Unknown MySQL error' if not defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
632 0 ▶ $errno = 2000 if not defined $errno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $errno 0 0 0
633 0 ▶ $sqlstate = 'HY000' if not defined $sqlstate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sqlstate 0 0 0
634
635 0 my $payload = chr(0xff);
636 0 $payload .= pack('v', $errno);
637 0 $payload .= '#';
638 0 $payload .= $sqlstate;
639 0 $payload .= $message."\0";
640
641 0 return $self->_write_to_client( $payload, 1);
642 }
643  
644 0 ▶sub _send_header_packet {
Subroutine _send_header_packet: not called CC=1 SCAR=6.9
645 0 my ($self, $n_fields, $n_rows) = @_;
646  
647 0 my $packet = _length_coded_binary($n_fields);
648  
649 0 $self->_write_to_client($packet);
650 }
651  
652 0 ▶sub _send_definitions {
Subroutine _send_definitions: not called CC=3 SCAR=24.8
653 0 my ($self, $definitions, $skip_envelope) = @_;
654  
655 0 my $last_send_result;
656
657 0 foreach my $definition (@{$definitions}) {
658 0 ▶ $definition = {
Branch: 1 branch
BranchTrueFalseTotal
unless ref $definition eq "HASH" 0 0 0
659 name => $definition,
660 type => 15,
661 } unless ref($definition) eq 'HASH';
662 0 $self->_send_definition($definition);
663 }
664  
665 }
666  
667 0 ▶sub _send_definition {
Subroutine _send_definition: not called CC=3 SCAR=21.1
668 0 my ($self, $definition) = @_;
669  
670 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
671  
672 0 my $payload = join('', map { _length_coded_string($_) } (
673 $definition->{catalog},
674 $definition->{db},
675 $definition->{table},
676 $definition->{org_table},
677 $definition->{name},
678 $definition->{org_name}
679 0 ));
680
681 0 $payload .= chr(0x0c);
682 0 $payload .= pack('v', 11);
683 0 $payload .= pack('V', $definition->{length});
684 0 $payload .= chr($definition->{type});
685 0 ▶ $payload .= defined $definition->{flags} ? pack('v', $definition->{flags}) : pack('v', 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $definition->{'flags'} ? : 0 0 0
686 0 ▶ $payload .= defined $definition->{decimals} ? chr($definition->{decimals}) : pack('v', 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $definition->{'decimals'} ? : 0 0 0
687 0 $payload .= pack('v', 0);
688  
689 0 $self->_write_to_client($payload);
690 }
691  
692  
693 0 ▶sub _send_rows {
Subroutine _send_rows: not called CC=9 SCAR=45.0
694 0 my ($self, $rows, $opt) = @_;
695  
696 0 foreach my $row (@$rows) {
697  
698 0 my $small_data;
699 0 ▶ if (ref($row) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $row eq 'ARRAY') { } 0 0 0
elsif (ref $row eq 'HASH') { } 0 0 0
700 0 foreach (@$row) {
701 0 ▶ if (not defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_) { } 0 0 0
702 0 $small_data .= chr(251);
703 }
704 else {
705 0 $small_data .= _length_coded_string($_);
706 }
707 }
708 }
709 elsif (ref($row) eq 'HASH') {
710 0 foreach (values %{ $row }) {
711 0 ▶ if (not defined $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_) { } 0 0 0
712 0 $small_data .= chr(251);
713 }
714 else {
715 0 $small_data .= _length_coded_string($_);
716 }
717 }
718 }
719  
720 0 ▶ if (defined $small_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $small_data) 0 0 0
721 0 $self->_write_to_client($small_data);
722 }
723 }
724
725 }
726  
727  
728 1 1;
↑ Summary MySQL.pm »