blib/lib/POE/Component/EasyDBI.pm

filter: Statementstmt 59.8% 235 / 393 Branchbran 44.1% 90 / 204 Conditioncond 26.6% 20 / 75 MC/DCmcdc 6.3% 3 / 47 Subroutinesub 85.7% 30 / 35 Podpod 5.2% 1 / 19 totaltotal 49.0% 379 / 773 SCAR 77.0
CC
134
Cov
51%
db_handler
261.6
new
94.4
child_STDOUT
93.6
CRAP 2202.9
« blib/lib/POE/Component/EasyDBI/SubProcess.pm ↑ Summary Build »
1 package POE::Component::EasyDBI;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings FATAL =>'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Initialize our version
7 2 our $VERSION = '1.28';
8  
9 # Import what we need from the POE namespace
10 2 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use POE::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use POE::Filter::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use POE::Wheel::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use POE::Component::EasyDBI::SubProcess;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Params::Util qw( _ARRAY _HASH );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 # Miscellaneous modules
22 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 our %COMMANDS = map { $_ => 1 } qw(
26 commit
27 rollback
28 begin_work
29 func
30 method
31 insert
32 do
33 single
34 quote
35 arrayhash
36 hashhash
37 hasharray
38 array
39 arrayarray
40 hash
41 keyvalhash
42 shutdown
43 combo
44 );
45  
46 sub AUTOLOAD {
47 13 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=6 SCAR=18.7
48 13 my $method = $AUTOLOAD;
49 13 $method =~ s/.*:://;
50 13 ▶ my $call = ( $method =~ s/^_// ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$method =~ s/^_// ? : 0 13 13
51
52 13 ▶ return unless $method =~ /[^A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /[^A-Z]/ 0 13 13
53
54 13 $method = lc( $method );
55
56 croak "EasyDBI command $method does not exist"
57 13 ▶ unless ( exists( $COMMANDS{ $method } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $POE::Component::EasyDBI::COMMANDS{$method} 0 13 13
58  
59 13 ▶ my @args = ( $method eq 'shutdown' ) ? @_ : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'shutdown' ? : 1 12 13
60 13 ▶ if ( $call ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call) { } 0 13 13
61 0 $poe_kernel->call($self->{ID} => $method => @args);
62 } else {
63 13 $poe_kernel->post($self->{ID} => $method => @args);
64 }
65 }
66  
67 # TODO use constants?
68 sub MAX_RETRIES () { 5 }
69 sub DEBUG () { 0 }
70  
71 # Autoflush on STDOUT
72 # Select returns last selected handle
73 # So, reselect it after selecting STDOUT and setting Autoflush
74 #select((select(STDOUT), $| = 1)[0]);
75  
76 sub spawn {
77 1 ▶ my ($self,$session) = &new;
Subroutine spawn: called CC=1 SCAR=0.0
Pod undocumented
78 1 return $session;
79 }
80  
81 sub create {
82 0 ▶ &spawn;
Subroutine create: not called CC=1 SCAR=6.9
Pod undocumented
83 }
84  
85 sub new {
86 2 ▶ my $class = shift;
Subroutine new: called CC=27 SCAR=45.5
Pod undocumented
87  
88 # The options hash
89 2 my %opt;
90  
91 # Support passing in a hash ref or a regular hash
92 2 ▶ if ((@_ & 1) && _HASH($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ & 1 and &_HASH($_[0])) { } 0 2 2
Condition33%1/3
@_ & 1 and &_HASH($_[0])
!ll&&!rl&&r
200
MC/DC0%0/2
@_ & 1 and &_HASH($_[0])
@_ & 1 &_HASH($_[0])
Truth table33%1/3
A and B
A= @_ & 1B= &_HASH($_[0])
ABresult
0X0
100
111
93 0 %opt = %{$_[0]};
94 } else {
95 # Sanity checking
96 2 ▶ if (@_ & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ & 1) 0 2 2
97 0 croak('POE::Component::EasyDBI requires an even number of options '
98 .'passed to new()/spawn() call');
99 }
100 2 %opt = @_;
101 }
102  
103 # lowercase keys
104 2 %opt = map { lc($_) => $opt{$_} } keys %opt;
105
106 2 my @valid = qw(
107 dsn
108 username
109 password
110 options
111 alias
112 max_retries
113 ping_timeout
114 use_cancel
115 no_connect_failures
116 connect_error
117 reconnect_wait
118 connected
119 no_warnings
120 sig_ignore_off
121 no_cache
122 alt_fork
123 stopwatch
124 query_logger
125 );
126
127 # check the DSN
128 # username/password/port other options
129 # should be part of the DSN
130 2 ▶ if (!exists($opt{dsn})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"dsn"}) 0 2 2
131 0 croak('DSN is required to spawn a new POE::Component::EasyDBI '
132 .'instance!');
133 }
134  
135 # check the USERNAME
136 2 ▶ if (!exists($opt{username})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"username"}) 0 2 2
137 0 croak('username is required to spawn a new POE::Component::EasyDBI '
138 .'instance!');
139 }
140  
141 # check the PASSWORD
142 2 ▶ if (!exists($opt{password})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"password"}) 0 2 2
143 0 croak('password is required to spawn a new POE::Component::EasyDBI '
144 .'instance!');
145 }
146  
147 # check the reconnect wait time
148 2 ▶ if (exists($opt{reconnect_wait}) && $opt{reconnect_wait} < 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opt{'reconnect_wait'} and $opt{'reconnect_wait'} < 1) { } 0 2 2
elsif (not $opt{'reconnect_wait'}) { } 2 0 2
Condition33%1/3
exists $opt{"reconnect_wait"} and $opt{"reconnect_wait"} < 1
!ll&&!rl&&r
200
MC/DC0%0/2
exists $opt{"reconnect_wait"} and $opt{"reconnect_wait"} < 1
exists $opt{"reconnect_wait"} $opt{"reconnect_wait"} < 1
Truth table33%1/3
A and B
A= exists $opt{"reconnect_wait"}B= $opt{"reconnect_wait"} < 1
ABresult
0X0
100
111
149 0 warn('A reconnect_wait of less than 1 second is NOT recommended, '
150 .'continuing anyway');
151 } elsif (!$opt{reconnect_wait}) {
152 2 $opt{reconnect_wait} = 2;
153 }
154  
155 # check the session alias
156 2 ▶ if (!exists($opt{alias})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"alias"}) 0 2 2
157 # Debugging info...
158 0 DEBUG && warn 'Using default Alias EasyDBI';
159  
160 # Set the default
161 0 $opt{alias} = 'EasyDBI';
162 }
163
164 # check for connect error event
165 2 ▶ if (exists($opt{connect_error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"connect_error"}) 2 0 2
166 2 ▶ if (_ARRAY($opt{connect_error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_ARRAY($opt{'connect_error'})) { } 2 0 2
167 2 ▶ unless ($#{$opt{connect_error}} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#{$opt{"connect_error"};} > 0) 0 2 2
168 0 warn('connect_error must be an array reference that contains '
169 .'at least a session and event, ignoring');
170 0 delete $opt{connect_error};
171 }
172 } else {
173 0 warn('connect_error must be an array reference that contains at '
174 .'least a session and event, ignoring');
175 0 delete $opt{connect_error};
176 }
177 }
178
179 # check for connect event
180 2 ▶ if (exists($opt{connected})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"connected"}) 2 0 2
181 2 ▶ if (_ARRAY($opt{connected})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_ARRAY($opt{'connected'})) { } 2 0 2
182 2 ▶ unless ($#{$opt{connected}} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($#{$opt{"connected"};} > 0) 0 2 2
183 0 warn('connected must be an array reference that contains '
184 .'at least a session and event, ignoring');
185 0 delete $opt{connected};
186 }
187 } else {
188 0 warn('connected must be an array reference that contains at '
189 .'least a session and event, ignoring');
190 0 delete $opt{connected};
191 }
192 }
193  
194 2 ▶ if (exists($opt{options})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"options"}) 0 2 2
195 0 ▶ unless (_HASH($opt{options})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_HASH($opt{"options"})) 0 0 0
196 0 warn('options must be a hash ref, ignoring');
197 0 delete $opt{options};
198 }
199 }
200  
201 2 ▶ if ($opt{stopwatch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"stopwatch"}) 2 0 2
202 2 ▶ eval "use Time::Stopwatch";
Subroutine BEGIN: called CC=1 SCAR=0.0
203 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
204 0 warn('cannot use stopwatch (ignored), Time::Stopwatch not installed? ');
205 0 delete $opt{stopwatch};
206 }
207 }
208
209 2 my $keep = { map { $_ => delete $opt{$_} } @valid };
210  
211 # Anything left over is unrecognized
212 2 ▶ if (keys %opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %opt) 0 2 2
213 0 croak('Unrecognized keys/options ('.join(',',(keys %opt))
214 .') were present in new() call to POE::Component::EasyDBI!');
215 }
216
217 2 ▶ my $self = bless($keep,ref $class || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
218  
219 # Create a new session for ourself
220 my $session = POE::Session->create(
221 # Our subroutines
222 'object_states' => [
223 $self => {
224 # Maintenance events
225 '_start' => 'start',
226 '_stop' => 'stop',
227 'setup_wheel' => 'setup_wheel',
228 'sig_child' => 'sig_child',
229
230 # child events
231 'child_error' => 'child_error',
232 'child_closed' => 'child_closed',
233 'child_STDOUT' => 'child_STDOUT',
234 'child_STDERR' => 'child_STDERR',
235
236 # database events
237 36 ▶ (map { $_ => 'db_handler', uc($_) => 'db_handler' } keys %COMMANDS),
Condition50%2/4
$keep->{'max_retries'} || 5
l!l
02
$keep->{'no_connect_failures'} || 0
l!l
02
MC/DC0%0/1
$keep->{'max_retries'} || 5
$keep->{'max_retries'}
MC/DC0%0/1
$keep->{'no_connect_failures'} || 0
$keep->{'no_connect_failures'}
Truth table50%1/2
A
A= $keep->{'max_retries'}
Aresult
11
00
Truth table50%1/2
A
A= $keep->{'no_connect_failures'}
Aresult
11
00
238
239 # redefine
240 'combo' => 'combo_query',
241 'COMBO' => 'combo_query',
242 'shutdown' => 'shutdown_poco',
243 'SHUTDOWN' => 'shutdown_poco',
244
245
246 # Queue handling
247 'send_query' => 'send_query',
248 'check_queue' => 'check_queue',
249 'print_queue' => 'print_queue',
250 },
251 ],
252  
253 # Set up the heap for ourself
254 'heap' => {
255 # The queue of DBI calls
256 'queue' => [],
257 'idcounter' => 0,
258  
259 # The Wheel::Run object placeholder
260 'wheel' => undef,
261  
262 # How many times have we re-created the wheel?
263 'retries' => 0,
264  
265 # Are we shutting down?
266 'shutdown' => 0,
267  
268 # Valid options
269 'opts' => $keep,
270  
271 # The alia/s we will run under
272 'alias' => $keep->{alias},
273  
274 # Number of times to retry connection
275 # (if connection failures option is off)
276 'max_retries' => $keep->{max_retries} || MAX_RETRIES,
277  
278 # Connection failure option
279 2 ▶ 'no_connect_failures' => $keep->{no_connect_failures} || 0,
Branch: 1 branch
BranchTrueFalseTotal
my $session = "POE::Session"->create("object_states", [$self, {"_start", "start", "_stop", "stop", "setup_wheel", "setup_wheel", "sig_child", "sig_child", "child_error", "child_error", "child_closed", "child_closed", "child_STDOUT", "child_STDOUT", "child_STDERR", "child_STDERR", map({$_, "db_handler", uc $_, "db_handler";} keys %POE::Component::EasyDBI::COMMANDS), "combo", "combo_query", "COMBO", "combo_query", "shutdown", "shutdown_poco", "SHUTDOWN", "shutdown_poco", "send_query", "send_query", "check_queue", "check_queue", "print_queue", "print_queue"}], "heap", {"queue", [], "idcounter", 0, "wheel", undef, "retries", 0, "shutdown", 0, "opts", $keep, "alias", $keep->{"alias"}, "max_retries", $keep->{"max_retries"} || 5, "no_connect_failures", $keep->{"no_connect_failures"} || 0, "action_params", {"commit", [], "rollback", [], "begin_work", [], "func", ["args"], "method", ["function", "args"], "single", ["separator"], "insert", ["insert", "hash", "table", "last_insert_id"], "array", ["chunked", "separator"], "arrayarray", ["chunked"], "keyvalhash", ["primary_key", "chunked"], "hashhash", ["primary_key", "chunked"], "hasharray", ["primary_key", "chunked"], "arrayhash", ["chunked"]}}) or die "Unable to create a new session!" 0 2 2
280  
281 # extra params for actions
282 action_params => {
283 commit => [],
284 rollback => [],
285 begin_work => [],
286 func => [qw( args )],
287 method => [qw( function args )],
288 single => [qw( separator )],
289 insert => [qw( insert hash table last_insert_id )],
290 array => [qw( chunked separator )],
291 arrayarray => [qw( chunked )],
292 keyvalhash => [qw( primary_key chunked )],
293 hashhash => [qw( primary_key chunked )],
294 hasharray => [qw( primary_key chunked )],
295 arrayhash => [qw( chunked )],
296 },
297 },
298 ) or die 'Unable to create a new session!';
299
300 # save the session id
301 2 $self->{ID} = $session->ID;
302
303 2 ▶ return wantarray ? ($self,$session) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
304 }
305  
306 # This subroutine handles shutdown signals
307 sub shutdown_poco {
308 2 ▶ my ($kernel, $heap) = @_[KERNEL,HEAP];
Subroutine shutdown_poco: called CC=10 SCAR=41.1
Pod undocumented
309
310 # Check for duplicate shutdown signals
311 2 ▶ if ($heap->{shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"shutdown"}) 0 2 2
312 # Okay, let's see what's going on
313 0 ▶ if ($heap->{shutdown} == 1 && ! defined $_[ARG0]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($heap->{'shutdown'} == 1 and not defined $_[10]) { } 0 0 0
elsif ($heap->{'shutdown'} == 2) { } 0 0 0
Condition0%0/3
$heap->{"shutdown"} == 1 and not defined $_[10]
!ll&&!rl&&r
000
MC/DC0%0/2
$heap->{"shutdown"} == 1 and not defined $_[10]
$heap->{"shutdown"} == 1 not defined $_[10]
Truth table0%0/3
A and not(B)
A= $heap->{"shutdown"} == 1B= not defined $_[10]
ABresult
0X0
100
111
314 # Duplicate shutdown events
315 0 DEBUG && warn 'Duplicate shutdown event fired!';
316 0 return;
317 } elsif ($heap->{shutdown} == 2) {
318 # Tried to shutdown_NOW again...
319 0 DEBUG && warn 'Duplicate shutdown NOW fired!';
320 0 return;
321 }
322 }
323  
324 # Check if we got "NOW"
325 2 ▶ if (defined($_[ARG0]) && uc($_[ARG0]) eq 'NOW') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[10] and uc $_[10] eq 'NOW') { } 0 2 2
Condition33%1/3
defined $_[10] and uc $_[10] eq "NOW"
!ll&&!rl&&r
200
MC/DC0%0/2
defined $_[10] and uc $_[10] eq "NOW"
defined $_[10] uc $_[10] eq "NOW"
Truth table33%1/3
A and B
A= defined $_[10]B= uc $_[10] eq "NOW"
ABresult
0X0
100
111
326 # Actually shut down!
327 0 $heap->{shutdown} = 2;
328  
329 0 ▶ if ($heap->{wheel}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"wheel"}) 0 0 0
330 # KILL our subprocess
331 0 $heap->{wheel}->kill(9);
332 }
333  
334 # Delete the wheel, so we have nothing to keep
335 # the GC from destructing us...
336 0 delete $heap->{wheel};
337  
338 # Go over our queue, and do some stuff
339 0 foreach my $queue (@{$heap->{queue}}) {
340 # Skip the special EXIT actions we might have put on the queue
341 0 ▶ if ($queue->{action} eq 'EXIT') { next }
Branch: 1 branch
BranchTrueFalseTotal
if ($queue->{"action"} eq "EXIT") 0 0 0
342  
343 # Post a failure event to all the queries on the Queue
344 # informing them that we have been shutdown...
345 0 $queue->{error} = 'POE::Component::EasyDBI was '
346 .'shut down forcibly!';
347 0 $kernel->post( $queue->{session}, $queue->{event}, $queue);
348  
349 # Argh, decrement the refcount
350 0 $kernel->refcount_decrement( $queue->{session}, 'EasyDBI' );
351 }
352  
353 # Tell the kernel to kill us!
354 0 $kernel->signal( $_[SESSION], 'KILL' );
355 } else {
356 # Gracefully shut down...
357 2 $heap->{shutdown} = 1;
358
359 # Put into the queue EXIT for the child
360 2 $kernel->yield( 'send_query', {
361 action => 'EXIT',
362 sql => undef,
363 placeholders => undef,
364 }
365 );
366 }
367 }
368  
369 sub combo_query {
370 2 ▶ my ($kernel, $heap, $args) = @_[KERNEL,HEAP,ARG0];
Subroutine combo_query: called CC=11 SCAR=25.9
Pod undocumented
371  
372 # Get the arguments
373 2 ▶ unless (_HASH($args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_HASH($args)) 0 2 2
374 0 $args = { @_[ ARG0 .. $#_ ] };
375 }
376
377 # Add some stuff to the args
378 # defaults to sender, but can be specified
379 2 ▶ unless (defined($args->{session})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"session"}) 2 0 2
380 2 $args->{session} = $_[SENDER]->ID();
381 2 DEBUG && print "setting session to $args->{session}\n";
382 }
383
384 2 $args->{action} = $_[STATE];
385  
386 2 ▶ if (!exists($args->{event})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args->{'event'}) { } 0 2 2
387 # Nothing much we can do except drop this quietly...
388 0 warn "Did not receive an event argument from caller ".$_[SENDER]->ID
389 ." State: " . $_[STATE] . " Args: " . join('',%$args);
390 0 return;
391 } else {
392 2 my $a = ref($args->{event});
393 2 ▶ unless (!ref($a) || $a =~ m/postback/i || $a =~ m/callback/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not ref $a or $a =~ /postback/i or $a =~ /callback/i) 0 2 2
Condition33%2/6
not ref $a or $a =~ /postback/i or $a =~ /callback/i
l!l&&r!l&&!r
200
not ref $a or $a =~ /postback/i
l!l&&r!l&&!r
200
MC/DC0%0/3
not ref $a or $a =~ /postback/i or $a =~ /callback/i
not ref $a $a =~ /postback/i $a =~ /callback/i
Truth table25%1/4
not(A) or B or C
A= not ref $aB= $a =~ /postback/iC= $a =~ /callback/i
ABCresult
1XX1
01X1
0011
0000
394 0 warn "Received an malformed event argument from caller"
395 ." (only postbacks, callbacks and scalar allowed) "
396 .$_[SENDER]->ID." -> State: " . $_[STATE] . " Event: $args->{event}"
397 ." Args: " . %$args;
398 0 return;
399 }
400 }
401  
402 2 my @res;
403 my $handle = sub {
404 4 ▶ push(@res, shift);
Subroutine __ANON__: called CC=2 SCAR=6.9
405 4 ▶ $poe_kernel->post( $args->{session} => $args->{event} => @res ) if (delete $res[ -1 ]->{__last});
Branch: 1 branch
BranchTrueFalseTotal
if delete $res[-1]{"__last"} 2 2 4
406 2 };
407  
408 2 foreach my $i ( 0 .. $#{ $args->{queries} } ) {
409 4 my ($type, $arg) = %{ $args->{queries}->[ $i ] };
410
411 # Copy pass-through options
412 4 for my $key ( keys %{ $args } ) {
413 16 ▶ next if defined $arg->{$key} || $key eq 'queries';
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg->{$key} or $key eq "queries" 4 12 16
Condition66%2/3
defined $arg->{$key} or $key eq "queries"
l!l&&r!l&&!r
0412
MC/DC50%1/2
defined $arg->{$key} or $key eq "queries"
defined $arg->{$key} $key eq "queries"
Truth table66%2/3
A or B
A= defined $arg->{$key}B= $key eq "queries"
ABresult
1X1
011
000
414 12 $arg->{$key} = $args->{$key};
415 }
416
417 4 $arg->{event} = $handle;
418 4 $arg->{__last} = ( $i == $#{ $args->{queries} } );
419 4 $kernel->call( $_[SESSION] => $type => $arg );
420 }
421  
422 2 return;
423 }
424  
425 # This subroutine handles queries
426 sub db_handler {
427 27 ▶ my ($kernel, $heap) = @_[KERNEL,HEAP];
Subroutine db_handler: called CC=29 SCAR=55.7
Pod undocumented
428  
429 # Get the arguments
430 27 my $args;
431 27 ▶ if (_HASH($_[ARG0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_HASH($_[10])) { } 27 0 27
432 27 $args = $_[ARG0];
433 } else {
434 0 warn "first parameter must be a hash ref, trying to adjust. "
435 ."(fix this to get rid of this message)";
436 0 $args = { @_[ARG0 .. $#_ ] };
437 }
438  
439 # fix a stupid spelling mistake
440 27 ▶ if ($args->{seperator}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"seperator"}) 0 27 27
441 0 $args->{separator} = $args->{seperator};
442 }
443
444 # Add some stuff to the args
445 # defaults to sender, but can be specified
446 27 ▶ unless (defined($args->{session})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"session"}) 23 4 27
447 23 $args->{session} = $_[SENDER]->ID();
448 23 DEBUG && print "setting session to $args->{session}\n";
449 }
450
451 27 $args->{action} = $_[STATE];
452  
453 27 ▶ if (!exists($args->{event})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args->{'event'}) { } 0 27 27
454 # Nothing much we can do except drop this quietly...
455 0 warn "Did not receive an event argument from caller ".$_[SENDER]->ID
456 ." State: " . $_[STATE] . " Args: " . join(',',%$args);
457 0 return;
458 } else {
459 27 my $a = ref($args->{event});
460 27 ▶ unless (!ref($a) || $a =~ m/postback/i || $a =~ m/callback/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not ref $a or $a =~ /postback/i or $a =~ /callback/i) 0 27 27
Condition33%2/6
not ref $a or $a =~ /postback/i or $a =~ /callback/i
l!l&&r!l&&!r
2700
not ref $a or $a =~ /postback/i
l!l&&r!l&&!r
2700
MC/DC0%0/3
not ref $a or $a =~ /postback/i or $a =~ /callback/i
not ref $a $a =~ /postback/i $a =~ /callback/i
Truth table25%1/4
not(A) or B or C
A= not ref $aB= $a =~ /postback/iC= $a =~ /callback/i
ABCresult
1XX1
01X1
0011
0000
461 0 warn "Received an malformed event argument from caller"
462 ." (only postbacks, callbacks and scalar allowed) "
463 .$_[SENDER]->ID." -> State: " . $_[STATE] . " Event: $args->{event}"
464 ." Args: " . %$args;
465 0 return;
466 }
467 }
468  
469 27 ▶ if (!defined($args->{sql})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args->{'sql'}) { } 6 21 27
470 6 ▶ unless ($args->{action} =~ m/^(insert|func|method|commit|rollback|begin_work)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"action"} =~ /^(insert|func|method|commit|rollback|begin_work)$/i) 0 6 6
471 0 $args->{error} = 'sql is not defined!';
472 # Okay, send the error to the Failure Event
473 0 $kernel->post($args->{session}, $args->{event}, $args);
474 0 return;
475 }
476 } else {
477 21 ▶ if (ref($args->{sql})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{"sql"}) 0 21 21
478 0 $args->{error} = 'sql is not a scalar!';
479 0 ▶ if ( reftype( $args->{event} ) && reftype( $args->{event} ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($args->{'event'}) and builtin::reftype($args->{'event'}) eq 'CODE') { } 0 0 0
Condition0%0/3
builtin::reftype($args->{"event"}) and builtin::reftype($args->{"event"}) eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($args->{"event"}) and builtin::reftype($args->{"event"}) eq "CODE"
builtin::reftype($args->{"event"}) builtin::reftype($args->{"event"}) eq "CODE"
Truth table0%0/3
A and B
A= builtin::reftype($args->{"event"})B= builtin::reftype($args->{"event"}) eq "CODE"
ABresult
0X0
100
111
480 0 my $callback = delete $args->{event};
481 0 $callback->($args);
482 } else {
483 # send the error to the Failure Event
484 0 $kernel->post($args->{session}, $args->{event}, $args);
485 }
486 0 return;
487 }
488 }
489  
490 # Check for placeholders
491 27 ▶ if (!defined($args->{placeholders})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args->{'placeholders'}) { } 23 4 27
492 # Create our own empty placeholders
493 23 $args->{placeholders} = [];
494 } else {
495 4 ▶ unless (_ARRAY($args->{placeholders})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_ARRAY($args->{"placeholders"})) 0 4 4
496 0 $args->{error} = 'placeholders is not an array!';
497 0 ▶ if ( reftype( $args->{event} ) && reftype( $args->{event} ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($args->{'event'}) and builtin::reftype($args->{'event'}) eq 'CODE') { } 0 0 0
Condition0%0/3
builtin::reftype($args->{"event"}) and builtin::reftype($args->{"event"}) eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($args->{"event"}) and builtin::reftype($args->{"event"}) eq "CODE"
builtin::reftype($args->{"event"}) builtin::reftype($args->{"event"}) eq "CODE"
Truth table0%0/3
A and B
A= builtin::reftype($args->{"event"})B= builtin::reftype($args->{"event"}) eq "CODE"
ABresult
0X0
100
111
498 0 my $callback = delete $args->{event};
499 0 $callback->($args);
500 } else {
501 # Okay, send the error to the Failure Event
502 0 $kernel->post($args->{session}, $args->{event}, $args);
503 }
504 0 return;
505 }
506 }
507  
508 # Check for primary_key on HASHHASH or ARRAYHASH queries
509 27 ▶ if ($args->{action} eq 'HASHHASH' || $args->{action} eq 'HASHARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"action"} eq "HASHHASH" or $args->{"action"} eq "HASHARRAY") 0 27 27
Condition33%1/3
$args->{"action"} eq "HASHHASH" or $args->{"action"} eq "HASHARRAY"
l!l&&r!l&&!r
0027
MC/DC0%0/2
$args->{"action"} eq "HASHHASH" or $args->{"action"} eq "HASHARRAY"
$args->{"action"} eq "HASHHASH" $args->{"action"} eq "HASHARRAY"
Truth table33%1/3
A or B
A= $args->{"action"} eq "HASHHASH"B= $args->{"action"} eq "HASHARRAY"
ABresult
1X1
011
000
510 0 ▶ if (!defined($args->{primary_key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args->{'primary_key'}) { } 0 0 0
511 0 $args->{error} = 'primary_key is not defined! It must '
512 .'be a column name or a 1 based index of a column';
513 0 ▶ if ( reftype( $args->{event} ) && reftype( $args->{event} ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($args->{'event'}) and builtin::reftype($args->{'event'}) eq 'CODE') { } 0 0 0
Condition0%0/3
builtin::reftype($args->{"event"}) and builtin::reftype($args->{"event"}) eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($args->{"event"}) and builtin::reftype($args->{"event"}) eq "CODE"
builtin::reftype($args->{"event"}) builtin::reftype($args->{"event"}) eq "CODE"
Truth table0%0/3
A and B
A= builtin::reftype($args->{"event"})B= builtin::reftype($args->{"event"}) eq "CODE"
ABresult
0X0
100
111
514 0 my $callback = delete $args->{event};
515 0 $callback->($args);
516 } else {
517 0 $kernel->post($args->{session}, $args->{event}, $args);
518 }
519 0 return;
520 } else {
521 0 $args->{error} = 'primary_key is not a scalar!';
522 0 ▶ if (ref($args->{sql})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{"sql"}) 0 0 0
523 # send the error to the Failure Event
524 0 ▶ if ( reftype( $args->{event} ) && reftype( $args->{event} ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($args->{'event'}) and builtin::reftype($args->{'event'}) eq 'CODE') { } 0 0 0
Condition0%0/3
builtin::reftype($args->{"event"}) and builtin::reftype($args->{"event"}) eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($args->{"event"}) and builtin::reftype($args->{"event"}) eq "CODE"
builtin::reftype($args->{"event"}) builtin::reftype($args->{"event"}) eq "CODE"
Truth table0%0/3
A and B
A= builtin::reftype($args->{"event"})B= builtin::reftype($args->{"event"}) eq "CODE"
ABresult
0X0
100
111
525 0 my $callback = delete $args->{event};
526 0 $callback->($args);
527 } else {
528 0 $kernel->post($args->{session}, $args->{event}, $args);
529 }
530 0 return;
531 }
532 }
533 }
534  
535 # Check if we have shutdown or not
536 27 ▶ if ($heap->{shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"shutdown"}) 0 27 27
537 0 $args->{error} = 'POE::Component::EasyDBI is shutting'
538 .' down now, requests are not accepted!';
539 # Do not accept this query
540 0 ▶ if ( reftype( $args->{event} ) && reftype( $args->{event} ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($args->{'event'}) and builtin::reftype($args->{'event'}) eq 'CODE') { } 0 0 0
Condition0%0/3
builtin::reftype($args->{"event"}) and builtin::reftype($args->{"event"}) eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($args->{"event"}) and builtin::reftype($args->{"event"}) eq "CODE"
builtin::reftype($args->{"event"}) builtin::reftype($args->{"event"}) eq "CODE"
Truth table0%0/3
A and B
A= builtin::reftype($args->{"event"})B= builtin::reftype($args->{"event"}) eq "CODE"
ABresult
0X0
100
111
541 0 my $callback = delete $args->{event};
542 0 $callback->($args);
543 } else {
544 0 $kernel->post($args->{session}, $args->{event}, $args);
545 }
546 0 return;
547 }
548  
549 # Increment the refcount for the session that is sending us this query
550 27 $kernel->refcount_increment($args->{session}, 'EasyDBI');
551
552 27 ▶ if ($args->{session} ne $_[SENDER]->ID()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"session"} ne $_[5]->ID) 4 23 27
553 4 $kernel->refcount_increment($_[SENDER]->ID(), 'EasyDBI');
554 4 $args->{sendersession} = $_[SENDER]->ID();
555 }
556  
557 # Okay, fire off this query!
558 27 $kernel->call($_[SESSION] => 'send_query' => $args);
559
560 27 return;
561 }
562  
563 # This subroutine starts the process of sending a query
564 sub send_query {
565 29 ▶ my ($kernel, $heap, $args) = @_[KERNEL,HEAP,ARG0];
Subroutine send_query: called CC=3 SCAR=11.8
Pod undocumented
566
567 # Validate that we have something
568 29 ▶ if (!defined($args) || !_HASH($args) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args and &_HASH($args)) 0 29 29
Condition33%1/3
defined $args and &_HASH($args)
!ll&&!rl&&r
0029
MC/DC0%0/2
defined $args and &_HASH($args)
defined $args &_HASH($args)
Truth table33%1/3
A and B
A= defined $argsB= &_HASH($args)
ABresult
0X0
100
111
569 0 return;
570 }
571  
572 # Add the ID to the query
573 29 $args->{id} = $heap->{idcounter}++;
574  
575 # Add this query to the queue
576 29 push(@{ $heap->{queue} }, $args);
577  
578 # Send the query!
579 29 $kernel->call($_[SESSION], 'check_queue');
580
581 29 return;
582 }
583  
584 # This subroutine is the meat - sends queries to the subprocess
585 sub check_queue {
586 58 ▶ my ($kernel, $heap) = @_[KERNEL,HEAP];
Subroutine check_queue: called CC=7 SCAR=19.5
Pod undocumented
587
588 # Check if the subprocess is currently active
589 58 ▶ return unless (!$heap->{active});
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"active"} 2 56 58
590  
591 # Check if we have a query in the queue
592 56 ▶ return unless (scalar(@{ $heap->{queue} }) > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$heap->{"queue"};} > 0 27 29 56
593
594 # shutting down?
595 29 ▶ return unless ($heap->{shutdown} != 2);
Branch: 1 branch
BranchTrueFalseTotal
unless $heap->{"shutdown"} != 2 0 29 29
596
597 29 ▶ if ($heap->{opts}{stopwatch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"opts"}{"stopwatch"}) 29 0 29
598 29 tie $heap->{queue}->[0]->{stopwatch}, 'Time::Stopwatch';
599 }
600  
601 # Copy what we need from the top of the queue
602 29 my %queue;
603 29 foreach (
604 qw(id sql action placeholders no_cache begin_work commit method)
605 29 ,@{$heap->{action_params}->{$heap->{queue}->[0]->{action}}}
606 ) {
607 266 ▶ next unless (defined($heap->{queue}->[0]->{$_}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $heap->{"queue"}[0]{$_} 144 122 266
608 122 $queue{$_} = $heap->{queue}->[0]->{$_};
609 }
610  
611 # Send data only if we are not shutting down...
612 # Set the child to 'active'
613 29 $heap->{active} = 1;
614  
615 # Put it in the wheel
616 29 $heap->{wheel}->put(\%queue);
617  
618 29 return;
619 }
620  
621 sub print_queue {
622 0 ▶ my ($kernel, $heap) = @_[KERNEL,HEAP];
Subroutine print_queue: not called CC=1 SCAR=6.9
Pod undocumented
623 0 return scalar @{$heap->{queue}};
624 }
625  
626 # This starts the EasyDBI
627 sub start {
628 2 ▶ my ($kernel, $heap) = @_[KERNEL,HEAP];
Subroutine start: called CC=2 SCAR=7.0
Pod undocumented
629
630 # Set up the alias for ourself
631 2 ▶ $kernel->alias_set($heap->{alias}) if ($heap->{alias} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"alias"} ne "" 2 0 2
632
633 # Create the wheel
634 2 $kernel->call( $_[SESSION] => 'setup_wheel' );
635
636 2 return;
637 }
638  
639 # This sets up the WHEEL
640 sub setup_wheel {
641 2 ▶ my ($kernel, $heap) = @_[KERNEL,HEAP];
Subroutine setup_wheel: called CC=10 SCAR=29.6
Pod undocumented
642
643 # Are we shutting down?
644 2 ▶ if ($heap->{shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"shutdown"}) 0 2 2
645 # if ($heap->{wheel}) {
646 # $heap->{wheel}->kill();
647 # }
648 # Do not re-create the wheel...
649 0 delete $heap->{wheel};
650 0 return;
651 }
652  
653 # Check if we should set up the wheel
654 2 ▶ if ($heap->{retries} == $heap->{max_retries}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"retries"} == $heap->{"max_retries"}) 0 2 2
655 die 'POE::Component::EasyDBI tried '.$heap->{max_retries}
656 0 .' times to create a Wheel and is giving up...';
657 }
658  
659 my %prog = (
660 'Program' => \&POE::Component::EasyDBI::SubProcess::main,
661 2 'ProgramArgs' => [ $heap->{opts} ],
662 );
663  
664 2 ▶ if ($heap->{opts}{alt_fork} && $^O ne 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"opts"}{"alt_fork"} and $^O ne "MSWin32") 0 2 2
Condition33%1/3
$heap->{"opts"}{"alt_fork"} and $^O ne "MSWin32"
!ll&&!rl&&r
200
MC/DC0%0/2
$heap->{"opts"}{"alt_fork"} and $^O ne "MSWin32"
$heap->{"opts"}{"alt_fork"} $^O ne "MSWin32"
Truth table33%1/3
A and B
A= $heap->{"opts"}{"alt_fork"}B= $^O ne "MSWin32"
ABresult
0X0
100
111
665 0 ▶ my $X = $heap->{opts}{alt_fork} ne "1" ? $heap->{opts}{alt_fork} : $^X;
Branch: 1 branch
BranchTrueFalseTotal
$heap->{'opts'}{'alt_fork'} ne '1' ? : 0 0 0
666 0 %prog = (
667 'Program' => "$X -MPOE::Component::EasyDBI::SubProcess -I".join(' -I',@INC)
668 ." -e 'POE::Component::EasyDBI::SubProcess::main(1)'",
669 );
670 }
671  
672 # $kernel->sig_child( $heap->{wheel_pid} )
673 # if ( $heap->{wheel_pid} );
674
675 # Set up the SubProcess we communicate with
676 2 ▶ $heap->{wheel} = POE::Wheel::Run->new(
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 2 2
677 # What we will run in the separate process
678 %prog,
679  
680 # Kill off existing FD's unless we're running in HELL^H^H^H^HMSWin32
681 'CloseOnCall' => ($^O eq 'MSWin32' ? 0 : 1),
682  
683 # Redirect errors to our error routine
684 'ErrorEvent' => 'child_error',
685  
686 # Send child died to our child routine
687 'CloseEvent' => 'child_closed',
688  
689 # Send input from child
690 'StdoutEvent' => 'child_STDOUT',
691  
692 # Send input from child STDERR
693 'StderrEvent' => 'child_STDERR',
694  
695 # Set our filters
696 # Communicate with child via Storable::nfreeze
697 'StdinFilter' => POE::Filter::Reference->new(),
698 # Receive input via Storable::nfreeze
699 'StdoutFilter' => POE::Filter::Reference->new(),
700 # Plain ol' error lines
701 'StderrFilter' => POE::Filter::Line->new(),
702 );
703  
704 2 $heap->{wheel_pid} = $heap->{wheel}->PID();
705
706 2 ▶ if ( $kernel->can( "sig_child" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kernel->can('sig_child')) { } 2 0 2
707 2 $kernel->sig_child( $heap->{wheel_pid} => 'sig_child' );
708 } else {
709 0 $kernel->sig( 'CHLD', 'sig_child' );
710 }
711  
712 # Check for errors
713 2 ▶ if (! defined $heap->{wheel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $heap->{'wheel'}) { } 0 2 2
714 0 die 'Unable to create a new wheel!';
715 } else {
716 # Increment our retry count
717 2 $heap->{retries}++;
718  
719 # Set the wheel to inactive
720 2 $heap->{active} = 0;
721  
722 2 ▶ if ($heap->{opts}{alt_fork}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"opts"}{"alt_fork"}) 0 2 2
723 0 $heap->{wheel}->put($heap->{opts});
724 }
725  
726 # Check for queries
727 2 $kernel->call($_[SESSION], 'check_queue');
728 }
729
730 2 return;
731 }
732  
733 ▶sub stop {
Subroutine stop: called CC=1 SCAR=0.0
Pod undocumented
734 # nothing to see here, move along
735 }
736  
737 # Deletes a query from the queue, if it is not active
738 sub delete_query {
739 0 ▶ my ($kernel, $heap) = @_[KERNEL,HEAP];
Subroutine delete_query: not called CC=6 SCAR=37.4
Pod undocumented
740 # ARG0 = ID
741 0 my $id = $_[ARG0];
742  
743 # Validation
744 0 ▶ if (!defined($id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
745 # Debugging
746 0 DEBUG && warn 'In Delete_Query event with no arguments!';
747 0 return;
748 }
749  
750 # Check if the id exists + not at the top of the queue :)
751 0 ▶ if (defined($heap->{queue}->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $heap->{"queue"}[0]) 0 0 0
752 0 ▶ if ($heap->{queue}->[0]->{id} eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{'queue'}[0]{'id'} eq $id) { } 0 0 0
753 # Query is still active, nothing we can do...
754 0 return undef;
755 } else {
756 # Search through the rest of the queue and see what we get
757 0 foreach my $count (@{ $heap->{queue} }) {
758 0 ▶ if ($heap->{queue}->[$count]->{id} eq $id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"queue"}[$count]{"id"} eq $id) 0 0 0
759 # Found a match, delete it!
760 0 splice(@{ $heap->{queue} }, $count, 1);
761  
762 # Return success
763 0 return 1;
764 }
765 }
766 }
767 }
768  
769 # If we got here, we didn't find anything
770 0 return undef;
771 }
772  
773 # Handles child DIE'ing
774 sub child_closed {
775 0 ▶ my ($kernel, $heap) = @_[KERNEL,HEAP];
Subroutine child_closed: not called CC=2 SCAR=17.9
Pod undocumented
776
777 0 DEBUG && warn 'POE::Component::EasyDBI\'s Wheel closed';
778 0 ▶ if ($heap->{shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"shutdown"}) 0 0 0
779 # if ($heap->{wheel}) {
780 # $heap->{wheel}->kill();
781 # }
782 0 delete $heap->{wheel};
783 0 return;
784 }
785  
786 # Emit debugging information
787 0 DEBUG && warn 'Restarting it...';
788  
789 # Create the wheel again
790 0 delete $heap->{wheel};
791 0 $kernel->call($_[SESSION], 'setup_wheel');
792
793 0 return;
794 }
795  
796 # Handles child error
797 sub child_error {
798 2 ▶ my $heap = $_[HEAP];
Subroutine child_error: called CC=3 SCAR=11.2
Pod undocumented
799
800 # Emit warnings only if debug is on
801 2 DEBUG && do {
802 # Copied from POE::Wheel::Run manpage
803 my ($operation, $errnum, $errstr) = @_[ARG0 .. ARG2];
804 warn "POE::Component::EasyDBI got an $operation error $errnum from Subprocess: '$errstr' shutdown: $heap->{shutdown}\n";
805 };
806
807 2 ▶ if ($heap->{shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"shutdown"}) 2 0 2
808 2 ▶ if ($heap->{wheel}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"wheel"}) 2 0 2
809 2 $heap->{wheel}->kill();
810 }
811 2 delete $heap->{wheel};
812 2 return;
813 }
814 }
815  
816 # Handles child STDOUT output
817 sub child_STDOUT {
818 29 ▶ my ($self, $kernel, $heap, $data) = @_[OBJECT,KERNEL,HEAP,ARG0];
Subroutine child_STDOUT: called CC=22 SCAR=45.4
Pod undocumented
819
820 # Validate the argument
821 29 ▶ unless ( _HASH($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_HASH($data)) 0 29 29
822 0 warn "POE::Component::EasyDBI did not get a hash from the child ( $data )";
823 0 return;
824 }
825  
826  
827 # DEBUG && do {
828 # require Data::Dumper;
829 # print Data::Dumper->Dump([$data,$heap->{queue}[0]]);
830 # };
831  
832 # Check for special DB messages with ID of 'DBI'
833 29 ▶ if ($data->{id} eq 'DBI') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"id"} eq "DBI") 0 29 29
834 # Okay, we received a DBI error -> error in connection...
835  
836 0 ▶ if ($heap->{no_connect_failures}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"no_connect_failures"}) 0 0 0
837 0 my $qc = {};
838 0 ▶ if (defined($heap->{queue}->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $heap->{"queue"}[0]) 0 0 0
839 0 $qc = $heap->{queue}[0];
840 }
841 0 $qc->{error} = $data->{error};
842 0 ▶ if (_ARRAY($heap->{opts}{connect_error})) {
Branch: 2 branches
BranchTrueFalseTotal
if (&_ARRAY($heap->{'opts'}{'connect_error'})) { } 0 0 0
elsif ($qc->{'session'} and $qc->{'event'}) { } 0 0 0
843 0 $kernel->post(@{$heap->{opts}{connect_error}}, $qc);
844 0 delete $heap->{queue}[0]->{error};
845 ▶ } elsif ($qc->{session} && $qc->{event}) {
Condition0%0/3
$qc->{"session"} and $qc->{"event"}
!ll&&!rl&&r
000
MC/DC0%0/2
$qc->{"session"} and $qc->{"event"}
$qc->{"session"} $qc->{"event"}
Truth table0%0/3
A and B
A= $qc->{"session"}B= $qc->{"event"}
ABresult
0X0
100
111
846 0 ▶ if ( reftype( $qc->{event} ) && reftype( $qc->{event} ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($qc->{'event'}) and builtin::reftype($qc->{'event'}) eq 'CODE') { } 0 0 0
Condition0%0/3
builtin::reftype($qc->{"event"}) and builtin::reftype($qc->{"event"}) eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::reftype($qc->{"event"}) and builtin::reftype($qc->{"event"}) eq "CODE"
builtin::reftype($qc->{"event"}) builtin::reftype($qc->{"event"}) eq "CODE"
Truth table0%0/3
A and B
A= builtin::reftype($qc->{"event"})B= builtin::reftype($qc->{"event"}) eq "CODE"
ABresult
0X0
100
111
847 0 my $callback = delete $qc->{event};
848 0 $callback->($qc);
849 } else {
850 0 $kernel->post($qc->{session}, $qc->{event}, $qc);
851 }
852 } else {
853 0 warn "No connect_error defined and no queries in the queue while "
854 ."error occurred: $data->{error}";
855 }
856 # print "DBI error: $data->{error}, retrying\n";
857 0 return;
858 }
859
860 # Shutdown ourself!
861 0 $kernel->call($_[SESSION], 'shutdown', 'NOW');
862  
863 # Too bad that we have to die...
864 0 croak("Could not connect to DBI or database went away: $data->{error}");
865 }
866  
867 29 ▶ if ($data->{id} eq 'DBI-CONNECTED') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"id"} eq "DBI-CONNECTED") 2 27 29
868 2 ▶ if (_ARRAY($heap->{opts}{connected})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_ARRAY($heap->{"opts"}{"connected"})) 2 0 2
869 2 my $query_copy = {};
870 2 ▶ if (defined($heap->{queue}->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $heap->{"queue"}[0]) 0 2 2
871 0 $query_copy = { %{$heap->{queue}[0]} };
872 }
873 2 $kernel->post(@{$heap->{opts}{connected}}, $query_copy);
874 }
875 2 return;
876 }
877  
878 27 my $query;
879 27 my $refcount_decrement = 0;
880
881 27 ▶ if (exists($data->{chunked})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'chunked'}) { } 0 27 27
882 # Get the query from the queue
883 0 ▶ if (exists($data->{last_chunk})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'last_chunk'}) { } 0 0 0
884 # last chunk, delete it out of the queue
885 0 $query = shift(@{ $heap->{queue} });
886 0 $refcount_decrement = 1;
887 } else {
888 0 $query = $heap->{queue}->[0];
889 }
890 } else {
891 # Check to see if the ID matches with the top of the queue
892 27 ▶ if ($data->{id} ne $heap->{queue}->[0]->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"id"} ne $heap->{"queue"}[0]{"id"}) 0 27 27
893 0 die "Internal error in queue/child consistency! ( CHILD: $data->{id} "
894 ."QUEUE: $heap->{queue}->[0]->{id} )";
895 }
896 # Get the query from the top of the queue
897 27 $query = shift(@{ $heap->{queue} });
898 27 $refcount_decrement = 1;
899 }
900  
901 # copy the query data, so we don't clobber the
902 # stored data when using chunks
903 #my $query_copy = { %{ $query } };
904
905 # marry data from the child to the data from the queue
906 #%$query_copy = (%$query_copy, %$data);
907
908 # my $query_copy = { (%$query, %$data) };
909
910 # my $query_copy = $query;
911 # foreach (keys %$data) { $query_copy->{$_} = $data->{$_}; }
912  
913 27 my $query_copy = { %$query, %$data };
914
915 # undocumented
916 $poe_kernel->call( $self->{query_logger} => _log => $query_copy )
917 27 ▶ if ( $self->{query_logger} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"query_logger"} 0 27 27
918  
919 # my ($ses,$evt) = ("$query_copy->{session}", "$query_copy->{event}");
920
921 # $kernel->call($ses => $evt => $query_copy);
922
923 #undef $query;
924 #foreach my $k (keys %$data) {
925 # $query_copy->{$k} = $data->{$k};
926 #}
927
928 27 ▶ if ( reftype( $query_copy->{event} ) && reftype( $query_copy->{event} ) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($query_copy->{'event'}) and builtin::reftype($query_copy->{'event'}) eq 'CODE') { } 7 20 27
Condition66%2/3
builtin::reftype($query_copy->{"event"}) and builtin::reftype($query_copy->{"event"}) eq "CODE"
!ll&&!rl&&r
2007
MC/DC50%1/2
builtin::reftype($query_copy->{"event"}) and builtin::reftype($query_copy->{"event"}) eq "CODE"
builtin::reftype($query_copy->{"event"}) builtin::reftype($query_copy->{"event"}) eq "CODE"
Truth table66%2/3
A and B
A= builtin::reftype($query_copy->{"event"})B= builtin::reftype($query_copy->{"event"}) eq "CODE"
ABresult
0X0
100
111
929 7 DEBUG && print "calling callback\n";
930 7 my $callback = delete $query_copy->{event};
931 7 $callback->($query_copy);
932 } else {
933 #DEBUG && print "calling event $query->{event} in session $query->{session} from our session ".$_[SESSION]->ID."\n";
934 20 $kernel->call($query_copy->{session} => $query_copy->{event} => $query_copy);
935 }
936  
937 # Decrement the refcount for the session that sent us a query
938 27 ▶ if ($refcount_decrement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refcount_decrement) 27 0 27
939 27 $heap->{active} = 0;
940 27 $kernel->refcount_decrement($query_copy->{session}, 'EasyDBI');
941
942 27 ▶ if (defined($query_copy->{sendersession}) && $query_copy->{sendersession} ne $query_copy->{session}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query_copy->{"sendersession"} and $query_copy->{"sendersession"} ne $query_copy->{"session"}) 4 23 27
Condition66%2/3
defined $query_copy->{"sendersession"} and $query_copy->{"sendersession"} ne $query_copy->{"session"}
!ll&&!rl&&r
2304
MC/DC50%1/2
defined $query_copy->{"sendersession"} and $query_copy->{"sendersession"} ne $query_copy->{"session"}
defined $query_copy->{"sendersession"} $query_copy->{"sendersession"} ne $query_copy->{"session"}
Truth table66%2/3
A and B
A= defined $query_copy->{"sendersession"}B= $query_copy->{"sendersession"} ne $query_copy->{"session"}
ABresult
0X0
100
111
943 4 $kernel->refcount_decrement($query_copy->{sendersession}, 'EasyDBI');
944 }
945  
946 # Now, that we have got a result, check if we need to send another query
947 27 $kernel->call($_[SESSION], 'check_queue');
948 }
949  
950 27 return;
951 }
952  
953 # Handles child STDERR output
954 sub child_STDERR {
955 0 ▶ my $input = $_[ARG0];
Subroutine child_STDERR: not called CC=4 SCAR=30.0
Pod undocumented
956  
957 # Skip empty lines as the POE::Filter::Line manpage says...
958 0 ▶ if ($input eq '') { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq "") 0 0 0
959  
960 0 ▶ return if ($_[HEAP]->{opts}->{no_warnings} && !DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if 0 0 0
Condition0%0/2
$_[3]{"opts"}{"no_warnings"} and
!ll
00
MC/DC0%0/1
$_[3]{"opts"}{"no_warnings"} and
$_[3]{"opts"}{"no_warnings"}
Truth table0%0/2
A
A= $_[3]{"opts"}{"no_warnings"}
Aresult
00
11
961  
962 0 warn "$input\n";
963 }
964  
965 sub sig_child {
966 2 ▶ my ($kernel, $heap) = @_[KERNEL, HEAP];
Subroutine sig_child: called CC=3 SCAR=11.2
Pod undocumented
967
968 2 delete $heap->{wheel_pid};
969 2 $kernel->sig_handled();
970
971 2 ▶ if ( $heap->{shutdown} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"shutdown"}) 2 0 2
972 # Remove our alias so we can be properly terminated
973 2 ▶ $kernel->alias_remove($heap->{alias}) if ($heap->{alias} ne '');
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"alias"} ne "" 2 0 2
974 # and the child
975 2 $kernel->sig( 'CHLD' );
976 }
977 }
978  
979 # ----------------
980 # Object methods
981  
982 sub ID {
983 1 ▶ my $self = shift;
Subroutine ID: called CC=1 SCAR=0.0
Pod documented
984 1 return $self->{ID};
985 }
986  
987 # not called directly
988 sub DESTROY {
989 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=3 SCAR=14.2
990 1 ▶ if (ref($self) && $self->ID) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and $self->ID) { } 1 0 1
Condition33%1/3
ref $self and $self->ID
!ll&&!rl&&r
001
MC/DC0%0/2
ref $self and $self->ID
ref $self $self->ID
Truth table33%1/3
A and B
A= ref $selfB= $self->ID
ABresult
0X0
100
111
991 # $poe_kernel->post($self->ID => 'shutdown' => @_);
992 } else {
993 0 return undef;
994 }
995 }
996  
997 # End of module
998 2 1;
999  
1000 # egg: I like Sealab 2021. This is why you can see lots of 2021 refences in my code.
1001  
1002 #=item C<use_cancel>
1003 #
1004 #Optional. If set to a true value, it will install a signal handler that will
1005 #call $sth->cancel when a INT signal is received by the sub-process. You may
1006 #want to read the docs on your driver to see if this is supported.
1007  
1008 __END__
« blib/lib/POE/Component/EasyDBI/SubProcess.pm ↑ Summary Build »