blib/lib/POE/Component/SimpleDBI.pm

filter: Statementstmt 6.2% 28 / 446 Branchbran 0.7% 2 / 286 Conditioncond 0.0% 0 / 78 MC/DCmcdc 0.0% 0 / 44 Subroutinesub 35.7% 10 / 28 Podpod 11.7% 2 / 17 totaltotal 4.6% 42 / 899 SCAR 102.8
CC
180
Cov
4%
DB_CONNECT
1406.0
DB_ATOMIC
1056.0
DB_HANDLE
756.0
CRAP 29111.7
↑ Summary blib/lib/POE/Component/SimpleDBI/SubProcess.pm »
1 #
2 # This file is part of POE-Component-SimpleDBI
3 #
4 # This software is copyright (c) 2011 by Apocalypse.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10 package POE::Component::SimpleDBI;
11 BEGIN {
12 1 ▶ $POE::Component::SimpleDBI::VERSION = '1.30';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14 BEGIN {
15 1 ▶ $POE::Component::SimpleDBI::AUTHORITY = 'cpan:APOCAL';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 }
17  
18 # ABSTRACT: Asynchronous non-blocking DBI calls in POE made simple
19  
20 # Import what we need from the POE namespace
21 1 ▶use POE; # For the constants
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use POE::Session; # To create our own :)
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use POE::Filter::Reference; # For communicating with the subprocess
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use POE::Filter::Line; # For subprocess STDERR messages
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use POE::Wheel::Run; # For the nitty-gritty details of 'fork'
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # Set some constants
28 BEGIN {
29 # Debug fun!
30 1 ▶ if ( ! defined &DEBUG ) { *DEBUG = sub () { 0 } }
Subroutine BEGIN: called CC=3 SCAR=11.4
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &POE::Component::SimpleDBI::DEBUG) 1 0 1
31  
32 # Our own definition of the max retries
33 1 ▶ if ( ! defined &MAX_RETRIES ) { *MAX_RETRIES = sub () { 5 } }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &POE::Component::SimpleDBI::MAX_RETRIES) 1 0 1
34 }
35  
36 # Set things in motion!
37 sub new {
38 # Get our arguments
39 0 ▶ my( $type, $ALIAS, $PREPARE_CACHED, $SYNCHRO_MODE ) = @_;
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
40  
41 # Get the session alias
42 0 ▶ if ( ! defined $ALIAS ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ALIAS) 0 0 0
43 # Debugging info...
44 0 if ( DEBUG ) {
45 warn 'Using default ALIAS = SimpleDBI';
46 }
47  
48 # Set the default
49 0 $ALIAS = 'SimpleDBI';
50 }
51  
52 # Should we disable prepare_cached?
53 0 ▶ if ( ! defined $PREPARE_CACHED ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $PREPARE_CACHED) { } 0 0 0
54 # Debugging info...
55 0 if ( DEBUG ) {
56 warn 'Setting PREPARE_CACHED = 1 by default';
57 }
58  
59 0 $PREPARE_CACHED = 1;
60 } else {
61 0 ▶ if ( $PREPARE_CACHED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PREPARE_CACHED) { } 0 0 0
62 0 $PREPARE_CACHED = 1;
63 } else {
64 0 $PREPARE_CACHED = 0;
65 }
66 }
67  
68 # Create a new session for ourself
69 0 ▶ POE::Session->create(
Branch: 1 branch
BranchTrueFalseTotal
"POE::Session"->create("inline_states", {"_start", \&POE::Component::SimpleDBI::Start, "_stop", \&POE::Component::SimpleDBI::Stop, "Setup_Wheel", \&POE::Component::SimpleDBI::Setup_Wheel, "shutdown", \&POE::Component::SimpleDBI::Shutdown, "ChildError", \&POE::Component::SimpleDBI::ChildError, "ChildClosed", \&POE::Component::SimpleDBI::ChildClosed, "Got_STDOUT", \&POE::Component::SimpleDBI::Got_STDOUT, "Got_STDERR", \&POE::Component::SimpleDBI::Got_STDERR, "Got_CHLD", \&POE::Component::SimpleDBI::Got_CHLD, "DO", \&POE::Component::SimpleDBI::DB_HANDLE, "SINGLE", \&POE::Component::SimpleDBI::DB_HANDLE, "MULTIPLE", \&POE::Component::SimpleDBI::DB_HANDLE, "QUOTE", \&POE::Component::SimpleDBI::DB_HANDLE, "CONNECT", \&POE::Component::SimpleDBI::DB_CONNECT, "DISCONNECT", \&POE::Component::SimpleDBI::DB_DISCONNECT, "ATOMIC", \&POE::Component::SimpleDBI::DB_ATOMIC, "Check_Queue", \&POE::Component::SimpleDBI::Check_Queue, "Clear_Queue", \&POE::Component::SimpleDBI::Clear_Queue, "Delete_Query", \&POE::Component::SimpleDBI::Delete_Query}, "heap", {"QUEUE", [], "IDCounter", 0, "WHEEL", undef, "Retries", 0, "SHUTDOWN", 0, "CONNECTED", 0, "ACTIVE", 0, "ALIAS", $ALIAS, "PREPARE_CACHED", $PREPARE_CACHED, "SYNCHRO", $SYNCHRO_MODE}) or die "Unable to create a new session!" 0 0 0
70 # Our subroutines
71 'inline_states' => {
72 # Maintenance events
73 '_start' => \&Start,
74 '_stop' => \&Stop,
75 'Setup_Wheel' => \&Setup_Wheel,
76  
77 # Shutdown stuff
78 'shutdown' => \&Shutdown,
79  
80 # IO events
81 'ChildError' => \&ChildError,
82 'ChildClosed' => \&ChildClosed,
83 'Got_STDOUT' => \&Got_STDOUT,
84 'Got_STDERR' => \&Got_STDERR,
85 'Got_CHLD' => \&Got_CHLD,
86  
87 # DB events
88 'DO' => \&DB_HANDLE,
89 'SINGLE' => \&DB_HANDLE,
90 'MULTIPLE' => \&DB_HANDLE,
91 'QUOTE' => \&DB_HANDLE,
92 'CONNECT' => \&DB_CONNECT,
93 'DISCONNECT' => \&DB_DISCONNECT,
94 'ATOMIC' => \&DB_ATOMIC,
95  
96 # Queue stuff
97 'Check_Queue' => \&Check_Queue,
98 'Clear_Queue' => \&Clear_Queue,
99 'Delete_Query' => \&Delete_Query,
100 },
101  
102 # Set up the heap for ourself
103 'heap' => {
104 # The queue of DBI calls
105 'QUEUE' => [],
106 'IDCounter' => 0,
107  
108 # The Wheel::Run object
109 'WHEEL' => undef,
110  
111 # How many times have we re-created the wheel?
112 'Retries' => 0,
113  
114 # Are we shutting down?
115 'SHUTDOWN' => 0,
116  
117 # Are we connected?
118 'CONNECTED' => 0,
119 'ACTIVE' => 0,
120  
121 # The alias we will run under
122 'ALIAS' => $ALIAS,
123  
124 # Cache sql statements?
125 'PREPARE_CACHED'=> $PREPARE_CACHED,
126  
127 # Synchronous mode?
128 'SYNCHRO' => $SYNCHRO_MODE,
129 },
130 ) or die 'Unable to create a new session!';
131  
132 # Return success
133 0 return 1;
134 }
135  
136 # This subroutine handles shutdown signals
137 sub Shutdown {
138 # Extensive debugging...
139 0 ▶ if ( DEBUG ) {
Subroutine Shutdown: not called CC=8 SCAR=42.8
Pod undocumented
140 warn 'Initiating shutdown procedure!';
141 }
142  
143 # Check for duplicate shutdown signals
144 0 ▶ if ( $_[HEAP]->{'SHUTDOWN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{'SHUTDOWN'}) { } 0 0 0
145 # Okay, let's see what's going on
146 0 ▶ if ( $_[HEAP]->{'SHUTDOWN'} == 1 && ! defined $_[ARG0] ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[3]{'SHUTDOWN'} == 1 and not defined $_[10]) { } 0 0 0
elsif ($_[3]{'SHUTDOWN'} == 2) { } 0 0 0
Condition0%0/3
$_[3]{"SHUTDOWN"} == 1 and not defined $_[10]
!ll&&!rl&&r
000
MC/DC0%0/2
$_[3]{"SHUTDOWN"} == 1 and not defined $_[10]
$_[3]{"SHUTDOWN"} == 1 not defined $_[10]
Truth table0%0/3
A and not(B)
A= $_[3]{"SHUTDOWN"} == 1B= not defined $_[10]
ABresult
0X0
100
111
147 # Duplicate shutdown events
148 0 if ( DEBUG ) {
149 warn 'Duplicate shutdown event was posted to SimpleDBI!';
150 }
151 0 return;
152 } elsif ( $_[HEAP]->{'SHUTDOWN'} == 2 ) {
153 # Tried to shutdown_NOW again...
154 0 if ( DEBUG ) {
155 warn 'Duplicate shutdown_NOW event was posted to SimpleDBI!';
156 }
157 0 return;
158 }
159 } else {
160 # Remove our alias so we can be properly terminated
161 0 $_[KERNEL]->alias_remove( $_[HEAP]->{'ALIAS'} );
162 }
163  
164 # Check if we got "NOW"
165 0 ▶ if ( defined $_[ARG0] and $_[ARG0] eq 'NOW' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[10] and $_[10] eq 'NOW') { } 0 0 0
Condition0%0/3
defined $_[10] and $_[10] eq "NOW"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[10] and $_[10] eq "NOW"
defined $_[10] $_[10] eq "NOW"
Truth table0%0/3
A and B
A= defined $_[10]B= $_[10] eq "NOW"
ABresult
0X0
100
111
166 # Actually shut down!
167 0 $_[HEAP]->{'SHUTDOWN'} = 2;
168  
169 # KILL our subprocess
170 0 $_[HEAP]->{'WHEEL'}->kill( -9 );
171  
172 # Delete the wheel, so we have nothing to keep the GC from destructing us...
173 0 delete $_[HEAP]->{'WHEEL'};
174  
175 # Clean up the queue
176 0 $_[KERNEL]->call( $_[SESSION], 'Clear_Queue', 'SimpleDBI is shutting down now' );
177  
178 # Tell the kernel to kill us!
179 0 $_[KERNEL]->signal( $_[SESSION], 'KILL' );
180 } else {
181 # Gracefully shut down...
182 0 $_[HEAP]->{'SHUTDOWN'} = 1;
183  
184 # Put into the queue EXIT for the child
185 0 push( @{ $_[HEAP]->{'QUEUE'} }, { 'ACTION' => 'EXIT', 'ID' => $_[HEAP]->{'IDCounter'}++ } );
186  
187 # Check if the subprocess is not active
188 0 ▶ if ( ! $_[HEAP]->{'ACTIVE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[3]{"ACTIVE"}) 0 0 0
189 # Send the query!
190 0 $_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
191 }
192 }
193  
194 0 return;
195 }
196  
197 # This subroutine handles MULTIPLE + SINGLE + DO + QUOTE queries
198 sub DB_HANDLE {
199 # Get the arguments
200 0 ▶ my %args = @_[ARG0 .. $#_ ];
Subroutine DB_HANDLE: not called CC=27 SCAR=66.3
Pod undocumented
201  
202 # Check for unknown args
203 0 foreach my $key ( keys %args ) {
204 0 ▶ if ( $key !~ /^(?:SQL|PLACEHOLDERS|BAGGAGE|EVENT|SESSION|PREPARE_CACHED|INSERT_ID)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^(?:SQL|PLACEHOLDERS|BAGGAGE|EVENT|SESSION|PREPARE_CACHED|INSERT_ID)$/) 0 0 0
205 0 if ( DEBUG ) {
206 warn "Unknown argument to $_[STATE] -> $key";
207 }
208 0 delete $args{ $key };
209 }
210 }
211  
212 # Add some stuff to the args
213 0 $args{'ACTION'} = $_[STATE];
214 0 ▶ if ( ! exists $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"SESSION"}) 0 0 0
215 0 $args{'SESSION'} = $_[SENDER]->ID();
216 }
217  
218 # Check for Session
219 0 ▶ if ( ! defined $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{'SESSION'}) { } 0 0 0
220 # Nothing much we can do except drop this quietly...
221 0 if ( DEBUG ) {
222 warn "Did not receive a SESSION argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
223 }
224 0 return;
225 } else {
226 0 ▶ if ( ref $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"SESSION"}) 0 0 0
227 0 ▶ if ( UNIVERSAL::isa( $args{'SESSION'}, 'POE::Session') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($args{'SESSION'}, 'POE::Session')) { } 0 0 0
228 # Convert it!
229 0 $args{'SESSION'} = $args{'SESSION'}->ID();
230 } else {
231 0 if ( DEBUG ) {
232 warn "Received malformed SESSION argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
233 }
234 0 return;
235 }
236 }
237 }
238  
239 # Check for Event
240 0 ▶ if ( ! exists $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{'EVENT'}) { } 0 0 0
241 # Nothing much we can do except drop this quietly...
242 0 if ( DEBUG ) {
243 warn "Did not receive an EVENT argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
244 }
245 0 return;
246 } else {
247 0 ▶ if ( ref $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"EVENT"}) 0 0 0
248 # Same quietness...
249 0 if ( DEBUG ) {
250 warn "Received a malformed EVENT argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
251 }
252 0 return;
253 }
254 }
255  
256 # Check for SQL
257 0 ▶ if ( ! exists $args{'SQL'} or ! defined $args{'SQL'} or ref $args{'SQL'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{"SQL"} && defined $args{"SQL"} or ref $args{"SQL"}) 0 0 0
Condition0%0/6
not exists $args{"SQL"} && defined $args{"SQL"} or ref $args{"SQL"}
l!l&&r!l&&!r
000
exists $args{'SQL'} && defined $args{'SQL'}
!ll&&!rl&&r
000
MC/DC0%0/3
not exists $args{"SQL"} && defined $args{"SQL"} or ref $args{"SQL"}
exists $args{'SQL'} defined $args{'SQL'} ref $args{"SQL"}
Truth table0%0/4
not(A && B) or C
A= exists $args{'SQL'}B= defined $args{'SQL'}C= ref $args{"SQL"}
ABCresult
0XX1
10X1
1111
1100
258 # Extensive debug
259 0 if ( DEBUG ) {
260 warn 'Did not receive/malformed SQL string!';
261 }
262  
263 # Okay, send the error to the Event
264 $_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
265 ( exists $args{'SQL'} ? ( 'SQL' => $args{'SQL'} ) : () ),
266 ( exists $args{'PLACEHOLDERS'} ? ( 'PLACEHOLDERS' => $args{'PLACEHOLDERS'} ) : () ),
267 ( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
268 'ERROR' => 'Received an empty/malformed SQL string!',
269 'ACTION' => $args{'ACTION'},
270 'EVENT' => $args{'EVENT'},
271 0 ▶ 'SESSION' => $args{'SESSION'},
Branch: 3 branches
BranchTrueFalseTotal
exists $args{'SQL'} ? : 0 0 0
exists $args{'PLACEHOLDERS'} ? : 0 0 0
exists $args{'BAGGAGE'} ? : 0 0 0
272 }
273 );
274 0 return;
275 }
276  
277 # Check for placeholders
278 0 ▶ if ( exists $args{'PLACEHOLDERS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"PLACEHOLDERS"}) 0 0 0
279 0 ▶ if ( ! ref $args{'PLACEHOLDERS'} or ref( $args{'PLACEHOLDERS'} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $args{"PLACEHOLDERS"} or ref $args{"PLACEHOLDERS"} ne "ARRAY") 0 0 0
Condition0%0/3
not ref $args{"PLACEHOLDERS"} or ref $args{"PLACEHOLDERS"} ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $args{"PLACEHOLDERS"} or ref $args{"PLACEHOLDERS"} ne "ARRAY"
not ref $args{"PLACEHOLDERS"} ref $args{"PLACEHOLDERS"} ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not ref $args{"PLACEHOLDERS"}B= ref $args{"PLACEHOLDERS"} ne "ARRAY"
ABresult
1X1
011
000
280 # Extensive debug
281 0 if ( DEBUG ) {
282 warn 'PLACEHOLDERS was not a ref to an ARRAY!';
283 }
284  
285 # Okay, send the error to the Event
286 $_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
287 'SQL' => $args{'SQL'},
288 'PLACEHOLDERS' => $args{'PLACEHOLDERS'},
289 ( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
290 'ERROR' => 'PLACEHOLDERS is not an array!',
291 'ACTION' => $args{'ACTION'},
292 'EVENT' => $args{'EVENT'},
293 0 ▶ 'SESSION' => $args{'SESSION'},
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'BAGGAGE'} ? : 0 0 0
294 }
295 );
296 0 return;
297 }
298 }
299  
300 # check for INSERT_ID
301 0 ▶ if ( exists $args{'INSERT_ID'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'INSERT_ID'}) { } 0 0 0
302 0 ▶ if ( $args{'INSERT_ID'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'INSERT_ID'}) { } 0 0 0
303 0 $args{'INSERT_ID'} = 1;
304 } else {
305 0 $args{'INSERT_ID'} = 0;
306 }
307 } else {
308 # set default
309 0 $args{'INSERT_ID'} = 1;
310 }
311  
312 # check for PREPARE_CACHED
313 0 ▶ if ( exists $args{'PREPARE_CACHED'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'PREPARE_CACHED'}) { } 0 0 0
314 0 ▶ if ( $args{'PREPARE_CACHED'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'PREPARE_CACHED'}) { } 0 0 0
315 0 $args{'PREPARE_CACHED'} = 1;
316 } else {
317 0 $args{'PREPARE_CACHED'} = 0;
318 }
319 } else {
320 # What does our global setting say?
321 0 $args{'PREPARE_CACHED'} = $_[HEAP]->{'PREPARE_CACHED'};
322 }
323  
324 # Check if we have shutdown or not
325 0 ▶ if ( $_[HEAP]->{'SHUTDOWN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"SHUTDOWN"}) 0 0 0
326 # Extensive debug
327 0 if ( DEBUG ) {
328 warn 'Denied query due to SHUTDOWN';
329 }
330  
331 # Do not accept this query
332 $_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
333 'SQL' => $args{'SQL'},
334 ( exists $args{'PLACEHOLDERS'} ? ( 'PLACEHOLDERS' => $args{'PLACEHOLDERS'} ) : () ),
335 ( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
336 'ERROR' => 'POE::Component::SimpleDBI is shutting down now, requests are not accepted!',
337 'ACTION' => $args{'ACTION'},
338 'EVENT' => $args{'EVENT'},
339 0 ▶ 'SESSION' => $args{'SESSION'},
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'PLACEHOLDERS'} ? : 0 0 0
exists $args{'BAGGAGE'} ? : 0 0 0
340 }
341 );
342 0 return;
343 }
344  
345 # Increment the refcount for the session that is sending us this query
346 0 $_[KERNEL]->refcount_increment( $args{'SESSION'}, 'SimpleDBI' );
347  
348 # Add the ID to the query
349 0 $args{'ID'} = $_[HEAP]->{'IDCounter'}++;
350  
351 # Add this query to the queue
352 0 push( @{ $_[HEAP]->{'QUEUE'} }, \%args );
353  
354 # Check if the subprocess is not active
355 0 ▶ if ( ! $_[HEAP]->{'ACTIVE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[3]{"ACTIVE"}) 0 0 0
356 # Send the query!
357 0 $_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
358 }
359  
360 # Return the ID for interested parties :)
361 0 return $args{'ID'};
362 }
363  
364 # This subroutine handles ATOMIC queries
365 sub DB_ATOMIC {
366 # Get the arguments
367 0 ▶ my %args = @_[ARG0 .. $#_ ];
Subroutine DB_ATOMIC: not called CC=32 SCAR=69.6
Pod undocumented
368  
369 # Check for unknown args
370 0 foreach my $key ( keys %args ) {
371 0 ▶ if ( $key !~ /^(?:SQL|PLACEHOLDERS|BAGGAGE|EVENT|SESSION|PREPARE_CACHED)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^(?:SQL|PLACEHOLDERS|BAGGAGE|EVENT|SESSION|PREPARE_CACHED)$/) 0 0 0
372 0 if ( DEBUG ) {
373 warn "Unknown argument to $_[STATE] -> $key";
374 }
375 0 delete $args{ $key };
376 }
377 }
378  
379 # Add some stuff to the args
380 0 $args{'ACTION'} = $_[STATE];
381 0 ▶ if ( ! exists $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"SESSION"}) 0 0 0
382 0 $args{'SESSION'} = $_[SENDER]->ID();
383 }
384  
385 # Check for Session
386 0 ▶ if ( ! defined $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{'SESSION'}) { } 0 0 0
387 # Nothing much we can do except drop this quietly...
388 0 if ( DEBUG ) {
389 warn "Did not receive a SESSION argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
390 }
391 0 return;
392 } else {
393 0 ▶ if ( ref $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"SESSION"}) 0 0 0
394 0 ▶ if ( UNIVERSAL::isa( $args{'SESSION'}, 'POE::Session') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($args{'SESSION'}, 'POE::Session')) { } 0 0 0
395 # Convert it!
396 0 $args{'SESSION'} = $args{'SESSION'}->ID();
397 } else {
398 0 if ( DEBUG ) {
399 warn "Received malformed SESSION argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
400 }
401 0 return;
402 }
403 }
404 }
405  
406 # Check for Event
407 0 ▶ if ( ! exists $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{'EVENT'}) { } 0 0 0
408 # Nothing much we can do except drop this quietly...
409 0 if ( DEBUG ) {
410 warn "Did not receive an EVENT argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
411 }
412 0 return;
413 } else {
414 0 ▶ if ( ref $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"EVENT"}) 0 0 0
415 # Same quietness...
416 0 if ( DEBUG ) {
417 warn "Received a malformed EVENT argument -> State: $_[STATE] Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
418 }
419 0 return;
420 }
421 }
422  
423 # Check for SQL
424 0 ▶ if ( ! exists $args{'SQL'} or ! defined $args{'SQL'} or ! ref $args{'SQL'} or ref( $args{'SQL'} ) ne 'ARRAY' or scalar @{ $args{'SQL'} } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{"SQL"} && defined $args{"SQL"} && ref $args{"SQL"} or ref $args{"SQL"} ne "ARRAY" or scalar @{$args{"SQL"};} == 0) 0 0 0
Condition0%0/12
not exists $args{"SQL"} && defined $args{"SQL"} && ref $args{"SQL"} or ref $args{"SQL"} ne "ARRAY" or scalar @{$args{"SQL"};} == 0
l!l&&r!l&&!r
000
not exists $args{"SQL"} && defined $args{"SQL"} && ref $args{"SQL"} or ref $args{"SQL"} ne "ARRAY"
l!l&&r!l&&!r
000
exists $args{'SQL'} && defined $args{'SQL'} && ref $args{'SQL'}
!ll&&!rl&&r
000
exists $args{"SQL"} and defined $args{"SQL"}
!ll&&!rl&&r
000
MC/DC0%0/5
not exists $args{"SQL"} && defined $args{"SQL"} && ref $args{"SQL"} or ref $args{"SQL"} ne "ARRAY" or scalar @{$args{"SQL"};} == 0
exists $args{"SQL"} defined $args{"SQL"} ref $args{'SQL'} ref $args{"SQL"} ne "ARRAY" scalar @{$args{"SQL"};} == 0
Truth table0%0/6
not(A and B && C) or D or E
A= exists $args{"SQL"}B= defined $args{"SQL"}C= ref $args{'SQL'}D= ref $args{"SQL"} ne "ARRAY"E= scalar @{$args{"SQL"};} == 0
ABCDEresult
0XXXX1
10XXX1
110XX1
1111X1
111011
111000
425 0 if ( DEBUG ) {
426 warn 'Did not receive/malformed SQL array!';
427 }
428  
429 # Okay, send the error to the Event
430 $_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
431 ( exists $args{'SQL'} ? ( 'SQL' => $args{'SQL'} ) : () ),
432 ( exists $args{'PLACEHOLDERS'} ? ( 'PLACEHOLDERS' => $args{'PLACEHOLDERS'} ) : () ),
433 ( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
434 'ERROR' => 'Received an empty/malformed SQL array!',
435 'ACTION' => $args{'ACTION'},
436 'EVENT' => $args{'EVENT'},
437 0 ▶ 'SESSION' => $args{'SESSION'},
Branch: 3 branches
BranchTrueFalseTotal
exists $args{'SQL'} ? : 0 0 0
exists $args{'PLACEHOLDERS'} ? : 0 0 0
exists $args{'BAGGAGE'} ? : 0 0 0
438 }
439 );
440 0 return;
441 }
442  
443 # Check for placeholders
444 0 ▶ if ( exists $args{'PLACEHOLDERS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"PLACEHOLDERS"}) 0 0 0
445 0 ▶ if ( ! ref $args{'PLACEHOLDERS'} or ref( $args{'PLACEHOLDERS'} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $args{"PLACEHOLDERS"} or ref $args{"PLACEHOLDERS"} ne "ARRAY") 0 0 0
Condition0%0/3
not ref $args{"PLACEHOLDERS"} or ref $args{"PLACEHOLDERS"} ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $args{"PLACEHOLDERS"} or ref $args{"PLACEHOLDERS"} ne "ARRAY"
not ref $args{"PLACEHOLDERS"} ref $args{"PLACEHOLDERS"} ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not ref $args{"PLACEHOLDERS"}B= ref $args{"PLACEHOLDERS"} ne "ARRAY"
ABresult
1X1
011
000
446 0 if ( DEBUG ) {
447 warn 'PLACEHOLDERS was not a ref to an ARRAY!';
448 }
449  
450 # Okay, send the error to the Event
451 $_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
452 'SQL' => $args{'SQL'},
453 'PLACEHOLDERS' => $args{'PLACEHOLDERS'},
454 ( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
455 'ERROR' => 'PLACEHOLDERS is not an array!',
456 'ACTION' => $args{'ACTION'},
457 'EVENT' => $args{'EVENT'},
458 0 ▶ 'SESSION' => $args{'SESSION'},
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'BAGGAGE'} ? : 0 0 0
459 }
460 );
461 0 return;
462 }
463  
464 # again, make sure it's an AoA
465 0 foreach my $ph ( @{ $args{'PLACEHOLDERS'} } ) {
466 0 ▶ if ( defined $ph and ( ! ref $ph or ref( $ph ) ne 'ARRAY' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ph and !ref($ph) || ref $ph ne "ARRAY") 0 0 0
Condition0%0/6
defined $ph and !ref($ph) || ref $ph ne "ARRAY"
!ll&&!rl&&r
000
not ref $ph or ref $ph ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ph and !ref($ph) || ref $ph ne "ARRAY"
defined $ph not ref $ph ref $ph ne "ARRAY"
Truth table0%0/4
A and not(B) or C
A= defined $phB= not ref $phC= ref $ph ne "ARRAY"
ABCresult
0XX0
1000
11X1
1011
467 0 if ( DEBUG ) {
468 warn 'PLACEHOLDERS was not a proper AoA!';
469 }
470  
471 # Okay, send the error to the Event
472 $_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
473 'SQL' => $args{'SQL'},
474 'PLACEHOLDERS' => $args{'PLACEHOLDERS'},
475 ( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
476 'ERROR' => 'PLACEHOLDERS is not an AoA!',
477 'ACTION' => $args{'ACTION'},
478 'EVENT' => $args{'EVENT'},
479 0 ▶ 'SESSION' => $args{'SESSION'},
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'BAGGAGE'} ? : 0 0 0
480 }
481 );
482 0 return;
483 }
484 }
485 }
486  
487 # check for PREPARE_CACHED
488 0 ▶ if ( exists $args{'PREPARE_CACHED'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'PREPARE_CACHED'}) { } 0 0 0
489 0 ▶ if ( $args{'PREPARE_CACHED'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'PREPARE_CACHED'}) { } 0 0 0
490 0 $args{'PREPARE_CACHED'} = 1;
491 } else {
492 0 $args{'PREPARE_CACHED'} = 0;
493 }
494 } else {
495 # What does our global setting say?
496 0 $args{'PREPARE_CACHED'} = $_[HEAP]->{'PREPARE_CACHED'};
497 }
498  
499 # Check if we have shutdown or not
500 0 ▶ if ( $_[HEAP]->{'SHUTDOWN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"SHUTDOWN"}) 0 0 0
501 # Extensive debug
502 0 if ( DEBUG ) {
503 warn 'Denied query due to SHUTDOWN';
504 }
505  
506 # Do not accept this query
507 $_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
508 'SQL' => $args{'SQL'},
509 ( exists $args{'PLACEHOLDERS'} ? ( 'PLACEHOLDERS' => $args{'PLACEHOLDERS'} ) : () ),
510 ( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
511 'ERROR' => __PACKAGE__ . ' is shutting down now, requests are not accepted!',
512 'ACTION' => $args{'ACTION'},
513 'EVENT' => $args{'EVENT'},
514 0 ▶ 'SESSION' => $args{'SESSION'},
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'PLACEHOLDERS'} ? : 0 0 0
exists $args{'BAGGAGE'} ? : 0 0 0
515 }
516 );
517 0 return;
518 }
519  
520 # Increment the refcount for the session that is sending us this query
521 0 $_[KERNEL]->refcount_increment( $args{'SESSION'}, 'SimpleDBI' );
522  
523 # Add the ID to the query
524 0 $args{'ID'} = $_[HEAP]->{'IDCounter'}++;
525  
526 # Add this query to the queue
527 0 push( @{ $_[HEAP]->{'QUEUE'} }, \%args );
528  
529 # Check if the subprocess is not active
530 0 ▶ if ( ! $_[HEAP]->{'ACTIVE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[3]{"ACTIVE"}) 0 0 0
531 # Send the query!
532 0 $_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
533 }
534  
535 # Return the ID for interested parties :)
536 0 return $args{'ID'};
537 }
538  
539 # This subroutine connects to the DB
540 sub DB_CONNECT {
541 # Get the arguments
542 0 ▶ my %args = @_[ARG0 .. $#_ ];
Subroutine DB_CONNECT: not called CC=37 SCAR=72.5
Pod undocumented
543  
544 # If we got no arguments, assume that we will use the old connection
545 0 ▶ if ( keys %args == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args == 0) 0 0 0
546 0 ▶ if ( ! exists $_[HEAP]->{'DB_DSN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_[3]{"DB_DSN"}) 0 0 0
547 # How should we connect?
548 0 if ( DEBUG ) {
549 warn 'Got CONNECT event but no arguments/did not have a cached copy of connect args';
550 }
551 0 return;
552 }
553 }
554  
555 # Check for unknown args
556 0 foreach my $key ( keys %args ) {
557 0 ▶ if ( $key !~ /^(?:SESSION|EVENT|DSN|USERNAME|PASSWORD|NOW|CLEAR|AUTO_COMMIT|BAGGAGE|CACHEDKIDS)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^(?:SESSION|EVENT|DSN|USERNAME|PASSWORD|NOW|CLEAR|AUTO_COMMIT|BAGGAGE|CACHEDKIDS)$/) 0 0 0
558 0 if ( DEBUG ) {
559 warn "Unknown argument to CONNECT -> $key";
560 }
561 0 delete $args{ $key };
562 }
563 }
564  
565 # Add the cached copy if applicable
566 0 foreach my $key ( qw( DSN USERNAME PASSWORD SESSION EVENT AUTO_COMMIT CACHEDKIDS ) ) {
567 0 ▶ if ( ! exists $args{ $key } and exists $_[HEAP]->{ 'DB_' . $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{$key} and exists $_[3]{'DB_' . $key}) 0 0 0
Condition0%0/3
not exists $args{$key} and exists $_[3]{'DB_' . $key}
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $args{$key} and exists $_[3]{'DB_' . $key}
not exists $args{$key} exists $_[3]{'DB_' . $key}
Truth table0%0/3
not(A) and B
A= not exists $args{$key}B= exists $_[3]{'DB_' . $key}
ABresult
0X0
100
111
568 0 $args{ $key } = $_[HEAP]->{ 'DB_' . $key };
569 }
570 }
571  
572 # Add some stuff to the args
573 0 $args{'ACTION'} = 'CONNECT';
574 0 ▶ if ( ! exists $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"SESSION"}) 0 0 0
575 0 $args{'SESSION'} = $_[SENDER]->ID();
576 }
577  
578 # set default AutoCommit = 1
579 0 ▶ if ( ! exists $args{'AUTO_COMMIT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{'AUTO_COMMIT'}) { } 0 0 0
580 0 $args{'AUTO_COMMIT'} = 1;
581 } else {
582 0 ▶ if ( $args{'AUTO_COMMIT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'AUTO_COMMIT'}) { } 0 0 0
583 0 $args{'AUTO_COMMIT'} = 1;
584 } else {
585 0 $args{'AUTO_COMMIT'} = 0;
586 }
587 }
588  
589 # set default CACHEDKIDS = undef
590 0 ▶ if ( ! exists $args{'CACHEDKIDS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"CACHEDKIDS"}) 0 0 0
591 0 $args{'CACHEDKIDS'} = undef;
592 }
593  
594 # Check for Session
595 0 ▶ if ( ! defined $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{'SESSION'}) { } 0 0 0
596 # Nothing much we can do except drop this quietly...
597 0 if ( DEBUG ) {
598 warn "Did not receive a SESSION argument -> State: CONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
599 }
600 0 return;
601 } else {
602 0 ▶ if ( ref $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"SESSION"}) 0 0 0
603 0 ▶ if ( UNIVERSAL::isa( $args{'SESSION'}, 'POE::Session') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($args{'SESSION'}, 'POE::Session')) { } 0 0 0
604 # Convert it!
605 0 $args{'SESSION'} = $args{'SESSION'}->ID();
606 } else {
607 0 if ( DEBUG ) {
608 warn "Received malformed SESSION argument -> State: CONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
609 }
610 0 return;
611 }
612 }
613 }
614  
615 # Check for Event
616 0 ▶ if ( ! exists $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{'EVENT'}) { } 0 0 0
617 # Nothing much we can do except drop this quietly...
618 0 if ( DEBUG ) {
619 warn "Did not receive an EVENT argument -> State: CONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
620 }
621 0 return;
622 } else {
623 0 ▶ if ( ref $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"EVENT"}) 0 0 0
624 # Same quietness...
625 0 if ( DEBUG ) {
626 warn "Received a malformed EVENT argument -> State: CONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
627 }
628 0 return;
629 }
630 }
631  
632 # Check the 3 things we are interested in
633 0 foreach my $key ( qw( DSN USERNAME PASSWORD ) ) {
634 # Check for it!
635 0 ▶ if ( ! exists $args{ $key } or ! defined $args{ $key } or ref $args{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{$key} && defined $args{$key} or ref $args{$key}) 0 0 0
Condition0%0/6
not exists $args{$key} && defined $args{$key} or ref $args{$key}
l!l&&r!l&&!r
000
exists $args{$key} && defined $args{$key}
!ll&&!rl&&r
000
MC/DC0%0/3
not exists $args{$key} && defined $args{$key} or ref $args{$key}
exists $args{$key} defined $args{$key} ref $args{$key}
Truth table0%0/4
not(A && B) or C
A= exists $args{$key}B= defined $args{$key}C= ref $args{$key}
ABCresult
0XX1
10X1
1111
1100
636 # Extensive debug
637 0 if ( DEBUG ) {
638 warn "Did not receive/malformed $key!";
639 }
640  
641 # Okay, send the error to the Event
642 $_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
643 ( exists $args{'DSN'} ? ( 'DSN' => $args{'DSN'} ) : () ),
644 ( exists $args{'USERNAME'} ? ( 'USERNAME' => $args{'USERNAME'} ) : () ),
645 ( exists $args{'PASSWORD'} ? ( 'PASSWORD' => $args{'PASSWORD'} ) : () ),
646 'ERROR' => "Cannot connect without the $key!",
647 'ACTION' => 'CONNECT',
648 'EVENT' => $args{'EVENT'},
649 'SESSION' => $args{'SESSION'},
650 0 ▶ ( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
Branch: 4 branches
BranchTrueFalseTotal
exists $args{'DSN'} ? : 0 0 0
exists $args{'USERNAME'} ? : 0 0 0
exists $args{'PASSWORD'} ? : 0 0 0
exists $args{'BAGGAGE'} ? : 0 0 0
651 }
652 );
653 0 return;
654 }
655 }
656  
657 # Some sanity
658 0 ▶ if ( exists $args{'NOW'} and $args{'NOW'} and $_[HEAP]->{'CONNECTED'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"NOW"} and $args{"NOW"} and $_[3]{"CONNECTED"}) 0 0 0
Condition0%0/6
exists $args{"NOW"} and $args{"NOW"} and $_[3]{"CONNECTED"}
!ll&&!rl&&r
000
exists $args{"NOW"} and $args{"NOW"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $args{"NOW"} and $args{"NOW"} and $_[3]{"CONNECTED"}
exists $args{"NOW"} $args{"NOW"} $_[3]{"CONNECTED"}
Truth table0%0/4
A and B and C
A= exists $args{"NOW"}B= $args{"NOW"}C= $_[3]{"CONNECTED"}
ABCresult
0XX0
10X0
1100
1111
659 # Okay, send the error to the Event
660 $_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
661 'DSN' => $args{'DSN'},
662 'USERNAME' => $args{'USERNAME'},
663 'PASSWORD' => $args{'PASSWORD'},
664 'ERROR' => "Cannot CONNECT NOW when we are already connected!",
665 'ACTION' => 'CONNECT',
666 'EVENT' => $args{'EVENT'},
667 'SESSION' => $args{'SESSION'},
668 0 ▶ ( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'BAGGAGE'} ? : 0 0 0
669 }
670 );
671 0 return;
672 }
673  
674 # If we got CLEAR, empty the queue
675 0 ▶ if ( exists $args{'CLEAR'} and $args{'CLEAR'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"CLEAR"} and $args{"CLEAR"}) 0 0 0
Condition0%0/3
exists $args{"CLEAR"} and $args{"CLEAR"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"CLEAR"} and $args{"CLEAR"}
exists $args{"CLEAR"} $args{"CLEAR"}
Truth table0%0/3
A and B
A= exists $args{"CLEAR"}B= $args{"CLEAR"}
ABresult
0X0
100
111
676 0 $_[KERNEL]->call( $_[SESSION], 'Clear_Queue', 'The request queue was cleared via CONNECT' );
677 }
678  
679 # Increment the refcount for the session that is sending us this query
680 0 $_[KERNEL]->refcount_increment( $args{'SESSION'}, 'SimpleDBI' );
681  
682 # Add the ID to the query
683 0 $args{'ID'} = $_[HEAP]->{'IDCounter'}++;
684  
685 # Are we connecting now?
686 0 ▶ if ( exists $args{'NOW'} and $args{'NOW'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'NOW'} and $args{'NOW'}) { } 0 0 0
Condition0%0/3
exists $args{"NOW"} and $args{"NOW"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"NOW"} and $args{"NOW"}
exists $args{"NOW"} $args{"NOW"}
Truth table0%0/3
A and B
A= exists $args{"NOW"}B= $args{"NOW"}
ABresult
0X0
100
111
687 # Add this query to the top of the queue
688 0 unshift( @{ $_[HEAP]->{'QUEUE'} }, \%args );
689 } else {
690 # Add this to the bottom of the queue
691 0 push( @{ $_[HEAP]->{'QUEUE'} }, \%args );
692 }
693  
694 # Asynchronous mode.
695 0 ▶ if ( ! defined $_[HEAP]->{'SYNCHRO'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[3]{'SYNCHRO'}) { } 0 0 0
696 # Do we have the wheel running?
697 0 ▶ if ( ! defined $_[HEAP]->{'WHEEL'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[3]{"WHEEL"}) 0 0 0
698 # Create the wheel
699 0 $_[KERNEL]->call( $_[SESSION], 'Setup_Wheel' );
700 }
701 } else {
702 0 require POE::Component::SimpleDBI::SubProcess;
703 }
704  
705 # Check if the subprocess is not active
706 0 ▶ if ( ! $_[HEAP]->{'ACTIVE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[3]{"ACTIVE"}) 0 0 0
707 # Send the query!
708 0 $_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
709 }
710  
711 # Save the connection info
712 0 foreach my $key ( qw( DSN USERNAME PASSWORD SESSION EVENT AUTO_COMMIT CACHEDKIDS ) ) {
713 0 $_[HEAP]->{ 'DB_' . $key } = $args{ $key };
714 }
715  
716 # Return the ID for interested parties :)
717 0 return $args{'ID'};
718 }
719  
720 # This subroutine disconnects from the DB
721 sub DB_DISCONNECT {
722 # Get the arguments
723 0 ▶ my %args = @_[ARG0 .. $#_ ];
Subroutine DB_DISCONNECT: not called CC=20 SCAR=60.4
Pod undocumented
724  
725 # Check for unknown args
726 0 foreach my $key ( keys %args ) {
727 0 ▶ if ( $key !~ /^(?:SESSION|EVENT|NOW|CLEAR|BAGGAGE)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /^(?:SESSION|EVENT|NOW|CLEAR|BAGGAGE)$/) 0 0 0
728 0 if ( DEBUG ) {
729 warn "Unknown argument to DISCONNECT -> $key";
730 }
731 0 delete $args{ $key };
732 }
733 }
734  
735 # Add some stuff to the args
736 0 $args{'ACTION'} = 'DISCONNECT';
737 0 ▶ if ( ! exists $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"SESSION"}) 0 0 0
738 0 $args{'SESSION'} = $_[SENDER]->ID();
739 }
740  
741 # Check for Session
742 0 ▶ if ( ! defined $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{'SESSION'}) { } 0 0 0
743 # Nothing much we can do except drop this quietly...
744 0 if ( DEBUG ) {
745 warn "Did not receive a SESSION argument -> State: DISCONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
746 }
747 0 return;
748 } else {
749 0 ▶ if ( ref $args{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"SESSION"}) 0 0 0
750 0 ▶ if ( UNIVERSAL::isa( $args{'SESSION'}, 'POE::Session') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($args{'SESSION'}, 'POE::Session')) { } 0 0 0
751 # Convert it!
752 0 $args{'SESSION'} = $args{'SESSION'}->ID();
753 } else {
754 0 if ( DEBUG ) {
755 warn "Received malformed SESSION argument -> State: DISCONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
756 }
757 0 return;
758 }
759 }
760 }
761  
762 # Check for Event
763 0 ▶ if ( ! exists $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{'EVENT'}) { } 0 0 0
764 # Nothing much we can do except drop this quietly...
765 0 if ( DEBUG ) {
766 warn "Did not receive an EVENT argument -> State: DISCONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
767 }
768 0 return;
769 } else {
770 0 ▶ if ( ref $args{'EVENT'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{"EVENT"}) 0 0 0
771 # Same quietness...
772 0 if ( DEBUG ) {
773 warn "Received a malformed EVENT argument -> State: DISCONNECT Args: " . join( ' / ', %args ) . " Caller: " . $_[CALLER_FILE] . ' -> ' . $_[CALLER_LINE];
774 }
775 0 return;
776 }
777 }
778  
779 # Some sanity
780 0 ▶ if ( exists $args{'NOW'} and $args{'NOW'} and ! $_[HEAP]->{'CONNECTED'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"NOW"} and $args{"NOW"} and not $_[3]{"CONNECTED"}) 0 0 0
Condition0%0/6
exists $args{"NOW"} and $args{"NOW"} and not $_[3]{"CONNECTED"}
!ll&&!rl&&r
000
exists $args{"NOW"} and $args{"NOW"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $args{"NOW"} and $args{"NOW"} and not $_[3]{"CONNECTED"}
exists $args{"NOW"} $args{"NOW"} not $_[3]{"CONNECTED"}
Truth table0%0/4
A and B and not(C)
A= exists $args{"NOW"}B= $args{"NOW"}C= not $_[3]{"CONNECTED"}
ABCresult
0XX0
10X0
1100
1111
781 # Okay, send the error to the Event
782 $_[KERNEL]->post( $args{'SESSION'}, $args{'EVENT'}, {
783 'ERROR' => "Cannot DISCONNECT NOW when we are already disconnected!",
784 'ACTION' => 'DISCONNECT',
785 'EVENT' => $args{'EVENT'},
786 'SESSION' => $args{'SESSION'},
787 0 ▶ ( exists $args{'BAGGAGE'} ? ( 'BAGGAGE' => $args{'BAGGAGE'} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'BAGGAGE'} ? : 0 0 0
788 }
789 );
790 0 return;
791 }
792  
793 # If we got CLEAR, empty the queue
794 0 ▶ if ( exists $args{'CLEAR'} and $args{'CLEAR'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"CLEAR"} and $args{"CLEAR"}) 0 0 0
Condition0%0/3
exists $args{"CLEAR"} and $args{"CLEAR"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"CLEAR"} and $args{"CLEAR"}
exists $args{"CLEAR"} $args{"CLEAR"}
Truth table0%0/3
A and B
A= exists $args{"CLEAR"}B= $args{"CLEAR"}
ABresult
0X0
100
111
795 0 $_[KERNEL]->call( $_[SESSION], 'Clear_Queue', 'The request queue was cleared via DISCONNECT' );
796 }
797  
798 # Increment the refcount for the session that is sending us this query
799 0 $_[KERNEL]->refcount_increment( $args{'SESSION'}, 'SimpleDBI' );
800  
801 # Add the ID to the query
802 0 $args{'ID'} = $_[HEAP]->{'IDCounter'}++;
803  
804 # Are we disconnecting now?
805 0 ▶ if ( exists $args{'NOW'} and $args{'NOW'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{'NOW'} and $args{'NOW'}) { } 0 0 0
Condition0%0/3
exists $args{"NOW"} and $args{"NOW"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"NOW"} and $args{"NOW"}
exists $args{"NOW"} $args{"NOW"}
Truth table0%0/3
A and B
A= exists $args{"NOW"}B= $args{"NOW"}
ABresult
0X0
100
111
806 # Add this query to the top of the queue
807  
808 # Do we need to save the running query?
809 0 ▶ if ( $_[HEAP]->{'ACTIVE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{'ACTIVE'}) { } 0 0 0
810 # Save the old top query
811 0 my $oldquery = shift( @{ $_[HEAP]->{'QUEUE'} } );
812  
813 # Add it to the top!
814 0 unshift( @{ $_[HEAP]->{'QUEUE'} }, \%args );
815  
816 # Put the old one back on top if it was there
817 0 ▶ if ( defined $oldquery ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oldquery) { } 0 0 0
818 0 unshift( @{ $_[HEAP]->{'QUEUE'} }, $oldquery );
819 } else {
820 0 die "Internal error - ACTIVE with no query!";
821 }
822 } else {
823 # Just shove it on top
824 0 unshift( @{ $_[HEAP]->{'QUEUE'} }, \%args );
825 }
826 } else {
827 # Add this to the bottom of the queue
828 0 push( @{ $_[HEAP]->{'QUEUE'} }, \%args );
829 }
830  
831 # Check if the subprocess is not active
832 0 ▶ if ( ! $_[HEAP]->{'ACTIVE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[3]{"ACTIVE"}) 0 0 0
833 # Send the query!
834 0 $_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
835 }
836  
837 # Return the ID for interested parties :)
838 0 return $args{'ID'};
839 }
840  
841 # This subroutine clears the queue
842 sub Clear_Queue {
843 # Get the error string
844 0 ▶ my $err = $_[ARG0];
Subroutine Clear_Queue: not called CC=11 SCAR=48.8
Pod documented
845  
846 # If it is not defined, make it the default
847 0 ▶ if ( ! defined $err ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $err) 0 0 0
848 0 $err = 'Cleared the queue';
849 }
850  
851 # Is the SubProcess active?
852 0 my $activequeue = undef;
853 0 ▶ if ( $_[HEAP]->{'ACTIVE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"ACTIVE"}) 0 0 0
854 0 $activequeue = shift( @{ $_[HEAP]->{'QUEUE'} } );
855 }
856  
857 # Go over our queue, and do some stuff
858 ## no critic ( ProhibitAccessOfPrivateData )
859 0 foreach my $queue ( @{ $_[HEAP]->{'QUEUE'} } ) {
860 # Skip the special EXIT actions we might have put on the queue
861 0 ▶ if ( $queue->{'ACTION'} eq 'EXIT' ) { next }
Branch: 1 branch
BranchTrueFalseTotal
if ($queue->{"ACTION"} eq "EXIT") 0 0 0
862  
863 # Construct the response
864 my $ret = {
865 'ERROR' => $err,
866 'ACTION' => $queue->{'ACTION'},
867 'EVENT' => $queue->{'EVENT'},
868 'SESSION' => $queue->{'SESSION'},
869 0 'ID' => $queue->{'ID'},
870 };
871  
872 # Add needed fields
873 0 ▶ if ( $queue->{'ACTION'} eq 'CONNECT' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($queue->{'ACTION'} eq 'CONNECT') { } 0 0 0
elsif ($queue->{'ACTION'} ne 'DISCONNECT') { } 0 0 0
874 0 $ret->{'DSN'} = $queue->{'DSN'};
875 0 $ret->{'USERNAME'} = $queue->{'USERNAME'};
876 0 $ret->{'PASSWORD'} = $queue->{'PASSWORD'};
877 0 $ret->{'AUTO_COMMIT'} = $queue->{'AUTO_COMMIT'};
878  
879 0 ▶ if ( defined $queue->{'CACHEDKIDS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $queue->{"CACHEDKIDS"}) 0 0 0
880 0 $ret->{'CACHEDKIDS'} = $queue->{'CACHEDKIDS'};
881 }
882 } elsif ( $queue->{'ACTION'} ne 'DISCONNECT' ) {
883 0 $ret->{'SQL'} = $queue->{'SQL'};
884  
885 0 ▶ if ( exists $queue->{'PLACEHOLDERS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $queue->{"PLACEHOLDERS"}) 0 0 0
886 0 $ret->{'PLACEHOLDERS'} = $queue->{'PLACEHOLDERS'};
887 }
888  
889 0 ▶ if ( exists $queue->{'BAGGAGE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $queue->{"BAGGAGE"}) 0 0 0
890 0 $ret->{'BAGGAGE'} = $queue->{'BAGGAGE'};
891 }
892 }
893  
894 # Post a failure event to all the queries on the Queue, informing them that we have been shutdown...
895 0 $_[KERNEL]->post( $queue->{'SESSION'}, $queue->{'EVENT'}, $ret );
896  
897 # Argh, decrement the refcount
898 0 $_[KERNEL]->refcount_decrement( $queue->{'SESSION'}, 'SimpleDBI' );
899 }
900  
901 # Clear the queue!
902 0 $_[HEAP]->{'QUEUE'} = [];
903  
904 # Reinstate the active request if it exists
905 0 ▶ if ( defined $activequeue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $activequeue) 0 0 0
906 0 push( @{ $_[HEAP]->{'QUEUE'} }, $activequeue );
907 }
908  
909 # All done!
910 0 return 1;
911 }
912  
913 # This subroutine does the meat - sends queries to the subprocess
914 sub Check_Queue {
915 # Extensive debug
916 0 ▶ if ( DEBUG ) {
Subroutine Check_Queue: not called CC=12 SCAR=50.5
Pod undocumented
917 warn 'Checking the queue for events to process';
918 }
919  
920 # Check if the subprocess is currently active
921 0 ▶ if ( ! $_[HEAP]->{'ACTIVE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[3]{'ACTIVE'}) { } 0 0 0
922 # Check if we have a query in the queue
923 0 ▶ if ( scalar( @{ $_[HEAP]->{'QUEUE'} } ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$_[3]{'QUEUE'};} > 0) { } 0 0 0
924 # Are we connected?
925 0 ▶ if ( ! $_[HEAP]->{'CONNECTED'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[3]{"CONNECTED"}) 0 0 0
926 # Hmpf, maybe this query is the CONNECT one?
927 0 ▶ if ( $_[HEAP]->{'QUEUE'}->[0]->{'ACTION'} ne 'CONNECT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"QUEUE"}[0]{"ACTION"} ne "CONNECT") 0 0 0
928 # WE ARE DEADLOCKED
929 0 if ( DEBUG ) {
930 warn "DEADLOCK -> Not connected, but $_[HEAP]->{'QUEUE'}->[0]->{'ACTION'} is on top of queue!";
931 }
932 0 return;
933 }
934 }
935  
936 # Extensive debug
937 0 if ( DEBUG ) {
938 warn "Sending a $_[HEAP]->{'QUEUE'}->[0]->{'ACTION'} query to the SubProcess";
939 }
940  
941 # Copy what we need from the top of the queue
942 0 my %queue;
943 0 $queue{'ID'} = $_[HEAP]->{'QUEUE'}->[0]->{'ID'};
944 0 $queue{'ACTION'} = $_[HEAP]->{'QUEUE'}->[0]->{'ACTION'};
945  
946 # CONNECT event?
947 0 ▶ if ( $queue{'ACTION'} eq 'CONNECT' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($queue{'ACTION'} eq 'CONNECT') { } 0 0 0
elsif ($queue{'ACTION'} ne 'DISCONNECT') { } 0 0 0
948 0 $queue{'DSN'} = $_[HEAP]->{'QUEUE'}->[0]->{'DSN'};
949 0 $queue{'USERNAME'} = $_[HEAP]->{'QUEUE'}->[0]->{'USERNAME'};
950 0 $queue{'PASSWORD'} = $_[HEAP]->{'QUEUE'}->[0]->{'PASSWORD'};
951 0 $queue{'AUTO_COMMIT'} = $_[HEAP]->{'QUEUE'}->[0]->{'AUTO_COMMIT'};
952  
953 0 ▶ if ( defined $_[HEAP]->{'QUEUE'}->[0]->{'CACHEDKIDS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3]{"QUEUE"}[0]{"CACHEDKIDS"}) 0 0 0
954 0 $queue{'CACHEDKIDS'} = $_[HEAP]->{'QUEUE'}->[0]->{'CACHEDKIDS'};
955 }
956 } elsif ( $queue{'ACTION'} ne 'DISCONNECT' ) {
957 0 $queue{'SQL'} = $_[HEAP]->{'QUEUE'}->[0]->{'SQL'};
958  
959 0 ▶ if ( exists $_[HEAP]->{'QUEUE'}->[0]->{'PLACEHOLDERS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{"QUEUE"}[0]{"PLACEHOLDERS"}) 0 0 0
960 0 $queue{'PLACEHOLDERS'} = $_[HEAP]->{'QUEUE'}->[0]->{'PLACEHOLDERS'};
961 }
962  
963 0 $queue{'PREPARE_CACHED'} = $_[HEAP]->{'QUEUE'}->[0]->{'PREPARE_CACHED'};
964  
965 0 ▶ if ( exists $_[HEAP]->{'QUEUE'}->[0]->{'INSERT_ID'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{"QUEUE"}[0]{"INSERT_ID"}) 0 0 0
966 0 $queue{'INSERT_ID'} = $_[HEAP]->{'QUEUE'}->[0]->{'INSERT_ID'};
967 }
968 }
969  
970 # Set the child to 'active'
971 0 $_[HEAP]->{'ACTIVE'} = 1;
972  
973 # Put it in the wheel
974 0 ▶ if ( defined $_[HEAP]->{'SYNCHRO'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3]{'SYNCHRO'}) { } 0 0 0
975 0 my $output = (
976 POE::Component::SimpleDBI::SubProcess::process_request(\%queue)
977 );
978 0 ▶ $_[KERNEL]->call($_[SESSION], 'Got_STDOUT', $output) if $output;
Branch: 1 branch
BranchTrueFalseTotal
if $output 0 0 0
979 } else {
980 0 $_[HEAP]->{'WHEEL'}->put( \%queue );
981 }
982 } else {
983 0 if ( DEBUG ) {
984 warn 'Check_Queue was called but nothing in the queue!';
985 }
986 }
987 } else {
988 0 if ( DEBUG ) {
989 warn 'Check_Queue was called but the SubProcess is active!';
990 }
991 }
992  
993 0 return;
994 }
995  
996 # This subroutine deletes a query from the queue
997 sub Delete_Query {
998 # ARG0 = ID
999 0 ▶ my $id = $_[ARG0];
Subroutine Delete_Query: not called CC=6 SCAR=37.4
Pod documented
1000  
1001 # Validation
1002 0 ▶ if ( ! defined $id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
1003 # Debugging
1004 0 if ( DEBUG ) {
1005 warn 'Got a Delete_Query event with no arguments!';
1006 }
1007 0 return;
1008 }
1009  
1010 # Check if the id exists + not at the top of the queue :)
1011 0 ▶ if ( defined @{ $_[HEAP]->{'QUEUE'} }[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined @{$_[3]{"QUEUE"};}[0]) 0 0 0
1012 0 ▶ if ( $_[HEAP]->{'QUEUE'}->[0]->{'ID'} eq $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{'QUEUE'}[0]{'ID'} eq $id) { } 0 0 0
1013 # Extensive debug
1014 0 if ( DEBUG ) {
1015 warn 'Could not delete query as it is being processed by the SubProcess!';
1016 }
1017  
1018 # Query is still active, nothing we can do...
1019 0 return 0;
1020 } else {
1021 # Search through the rest of the queue and see what we get
1022 0 foreach my $count ( @{ $_[HEAP]->{'QUEUE'} } ) {
1023 0 ▶ if ( $_[HEAP]->{'QUEUE'}->[ $count ]->{'ID'} eq $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"QUEUE"}[$count]{"ID"} eq $id) 0 0 0
1024 # Found a match, delete it!
1025 0 splice( @{ $_[HEAP]->{'QUEUE'} }, $count, 1 );
1026  
1027 # Return success
1028 0 return 1;
1029 }
1030 }
1031 }
1032 }
1033  
1034 # If we got here, we didn't find anything
1035 0 return;
1036 }
1037  
1038 # This starts the SimpleDBI
1039 sub Start {
1040 # Extensive debug
1041 0 ▶ if ( DEBUG ) {
Subroutine Start: not called CC=1 SCAR=6.9
Pod undocumented
1042 warn 'Starting up SimpleDBI!';
1043 }
1044  
1045 # Set up the alias for ourself
1046 0 $_[KERNEL]->alias_set( $_[HEAP]->{'ALIAS'} );
1047  
1048 0 return;
1049 }
1050  
1051 # This sets up the WHEEL
1052 sub Setup_Wheel {
1053 # Extensive debug
1054 0 ▶ if ( DEBUG ) {
Subroutine Setup_Wheel: not called CC=7 SCAR=40.3
Pod undocumented
1055 warn 'Attempting creation of SubProcess wheel now...';
1056 }
1057  
1058 # Are we shutting down?
1059 0 ▶ if ( $_[HEAP]->{'SHUTDOWN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"SHUTDOWN"}) 0 0 0
1060 # Do not re-create the wheel...
1061 0 if ( DEBUG ) {
1062 warn 'Hmm, we are shutting down but got Setup_Wheel event...';
1063 }
1064 0 return;
1065 }
1066  
1067 # Check if we should set up the wheel
1068 0 ▶ if ( $_[HEAP]->{'Retries'} == MAX_RETRIES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"Retries"} == 5) 0 0 0
1069 0 die __PACKAGE__ . ' tried ' . MAX_RETRIES . ' times to create a Wheel and is giving up...';
1070 }
1071  
1072 # Add the windows method
1073 0 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
1074 # Thanks RT #23851
1075 0 require POE::Component::SimpleDBI::SubProcess;
1076  
1077 # Set up the SubProcess we communicate with
1078 $_[HEAP]->{'WHEEL'} = POE::Wheel::Run->new(
1079 # What we will run in the separate process
1080 # Thanks RT #42890
1081 0 ▶ 'Program' => sub { POE::Component::SimpleDBI::SubProcess::main() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1082  
1083 # Kill off existing FD's
1084 0 'CloseOnCall' => 0,
1085  
1086 # Redirect errors to our error routine
1087 'ErrorEvent' => 'ChildError',
1088  
1089 # Send child died to our child routine
1090 'CloseEvent' => 'ChildClosed',
1091  
1092 # Send input from child
1093 'StdoutEvent' => 'Got_STDOUT',
1094  
1095 # Send input from child STDERR
1096 'StderrEvent' => 'Got_STDERR',
1097  
1098 # Set our filters
1099 'StdinFilter' => POE::Filter::Reference->new(), # Communicate with child via Storable::nfreeze
1100 'StdoutFilter' => POE::Filter::Reference->new(), # Receive input via Storable::nfreeze
1101 'StderrFilter' => POE::Filter::Line->new(), # Plain ol' error lines
1102 );
1103 } else {
1104 # Set up the SubProcess we communicate with
1105 0 $_[HEAP]->{'WHEEL'} = POE::Wheel::Run->new(
1106 # What we will run in the separate process
1107 'Program' => "$^X -MPOE::Component::SimpleDBI::SubProcess -e 'POE::Component::SimpleDBI::SubProcess::main()'",
1108  
1109 # Kill off existing FD's
1110 'CloseOnCall' => 1,
1111  
1112 # Redirect errors to our error routine
1113 'ErrorEvent' => 'ChildError',
1114  
1115 # Send child died to our child routine
1116 'CloseEvent' => 'ChildClosed',
1117  
1118 # Send input from child
1119 'StdoutEvent' => 'Got_STDOUT',
1120  
1121 # Send input from child STDERR
1122 'StderrEvent' => 'Got_STDERR',
1123  
1124 # Set our filters
1125 'StdinFilter' => POE::Filter::Reference->new(), # Communicate with child via Storable::nfreeze
1126 'StdoutFilter' => POE::Filter::Reference->new(), # Receive input via Storable::nfreeze
1127 'StderrFilter' => POE::Filter::Line->new(), # Plain ol' error lines
1128 );
1129 }
1130  
1131 # Check for errors
1132 0 ▶ if ( ! defined $_[HEAP]->{'WHEEL'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[3]{'WHEEL'}) { } 0 0 0
1133 0 die 'Unable to create a new wheel!';
1134 } else {
1135 # smart CHLD handling
1136 0 ▶ if ( $_[KERNEL]->can( "sig_child" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]->can('sig_child')) { } 0 0 0
1137 0 $_[KERNEL]->sig_child( $_[HEAP]->{'WHEEL'}->PID => 'Got_CHLD' );
1138 } else {
1139 0 $_[KERNEL]->sig( 'CHLD', 'Got_CHLD' );
1140 }
1141  
1142 # Increment our retry count
1143 0 $_[HEAP]->{'Retries'}++;
1144  
1145 # We are not active...
1146 0 $_[HEAP]->{'ACTIVE'} = 0;
1147  
1148 # Since we created a new wheel, we have to tell it to connect if we already have the data
1149 0 ▶ if ( defined $_[HEAP]->{'DB_DSN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3]{"DB_DSN"}) 0 0 0
1150 # Connect NOW!
1151 0 $_[KERNEL]->call( $_[SESSION], 'CONNECT', 'NOW' => 1 );
1152 }
1153 }
1154  
1155 0 return;
1156 }
1157  
1158 # Got a CHLD event!
1159 sub Got_CHLD {
1160 0 ▶ $_[KERNEL]->sig_handled();
Subroutine Got_CHLD: not called CC=1 SCAR=6.9
Pod undocumented
1161 0 return;
1162 }
1163  
1164 # Stops everything we have
1165 sub Stop {
1166 # Hmpf, what should I put in here?
1167 0 ▶ return;
Subroutine Stop: not called CC=1 SCAR=6.9
Pod undocumented
1168 }
1169  
1170 # Handles child DIE'ing
1171 sub ChildClosed {
1172 # Emit debugging information
1173 0 ▶ if ( DEBUG ) {
Subroutine ChildClosed: not called CC=3 SCAR=24.8
Pod undocumented
1174 warn __PACKAGE__ . "'s Wheel died!";
1175 }
1176  
1177 # Get rid of the wheel
1178 0 undef $_[HEAP]->{'WHEEL'};
1179  
1180 # We are not active...
1181 0 $_[HEAP]->{'ACTIVE'} = 0;
1182  
1183 # Should we create it again?
1184 0 ▶ if ( ! $_[HEAP]->{'SHUTDOWN'} and $_[HEAP]->{'CONNECTED'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[3]{'SHUTDOWN'} and $_[3]{'CONNECTED'}) { } 0 0 0
Condition0%0/3
not $_[3]{"SHUTDOWN"} and $_[3]{"CONNECTED"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $_[3]{"SHUTDOWN"} and $_[3]{"CONNECTED"}
not $_[3]{"SHUTDOWN"} $_[3]{"CONNECTED"}
Truth table0%0/3
not(A) and B
A= not $_[3]{"SHUTDOWN"}B= $_[3]{"CONNECTED"}
ABresult
0X0
100
111
1185 # Create the wheel again
1186 0 $_[HEAP]->{'CONNECTED'} = 0;
1187 0 $_[KERNEL]->call( $_[SESSION], 'Setup_Wheel' );
1188 } else {
1189 0 $_[HEAP]->{'CONNECTED'} = 0;
1190 }
1191  
1192 0 return;
1193 }
1194  
1195 # Handles child error
1196 sub ChildError {
1197 # Emit warnings only if debug is on
1198 0 ▶ if ( DEBUG ) {
Subroutine ChildError: not called CC=1 SCAR=6.9
Pod undocumented
1199 # Copied from POE::Wheel::Run manpage
1200 my ( $operation, $errnum, $errstr ) = @_[ ARG0 .. ARG2 ];
1201 warn __PACKAGE__ . " got an $operation error $errnum: $errstr\n";
1202 }
1203  
1204 0 return;
1205 }
1206  
1207 # Handles child STDOUT output
1208 sub Got_STDOUT {
1209 # The data!
1210 0 ▶ my $data = $_[ARG0];
Subroutine Got_STDOUT: not called CC=20 SCAR=60.4
Pod undocumented
1211  
1212 ## no critic ( ProhibitAccessOfPrivateData )
1213  
1214 # Validate the argument
1215 0 ▶ if ( ! ref $data or ref( $data ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $data or ref $data ne "HASH") 0 0 0
Condition0%0/3
not ref $data or ref $data ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $data or ref $data ne "HASH"
not ref $data ref $data ne "HASH"
Truth table0%0/3
not(A) or B
A= not ref $dataB= ref $data ne "HASH"
ABresult
1X1
011
000
1216 0 warn __PACKAGE__ . " did not get a hash from the SubProcess ( $data )";
1217 0 return;
1218 }
1219  
1220 # Special sysread error is here
1221 0 ▶ if ( $data->{'ID'} eq 'SYSREAD' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"ID"} eq "SYSREAD") 0 0 0
1222 0 if ( DEBUG ) {
1223 warn "The backend got a SYSREAD error: $data->{'ERROR'}";
1224 }
1225 0 return;
1226 }
1227  
1228 # allow debugging
1229 0 ▶ if ( $data->{'ID'} eq 'DEBUG' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"ID"} eq "DEBUG") 0 0 0
1230 0 if ( DEBUG ) {
1231 require Data::Dumper;
1232 warn Data::Dumper::Dumper( $data->{'RESULT'} );
1233 }
1234 0 return;
1235 }
1236  
1237 # Special server discon error is here
1238 0 ▶ if ( $data->{'ID'} eq 'GONE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"ID"} eq "GONE") 0 0 0
1239 0 if ( DEBUG ) {
1240 warn "The backend failed the \$dbh->ping test and couldn't reconnect!";
1241 }
1242  
1243 # We are now disconnected
1244 0 $_[HEAP]->{'CONNECTED'} = 0;
1245 0 $_[HEAP]->{'ACTIVE'} = 0;
1246  
1247 # Construct the response
1248 my $ret = {
1249 'ERROR' => $data->{'ERROR'},
1250 'GONE' => 1,
1251 'DSN' => $_[HEAP]->{'DB_DSN'},
1252 'USERNAME' => $_[HEAP]->{'DB_USERNAME'},
1253 'PASSWORD' => $_[HEAP]->{'DB_PASSWORD'},
1254 'AUTO_COMMIT' => $_[HEAP]->{'DB_AUTO_COMMIT'},
1255 0 'CACHEDKIDS' => $_[HEAP]->{'DB_CACHEDKIDS'},
1256 };
1257  
1258 # Okay, we have to inform the session it failed and couldn't reconnect
1259 0 $_[KERNEL]->post( $_[HEAP]->{'DB_SESSION'}, $_[HEAP]->{'DB_EVENT'}, $ret );
1260  
1261 # All done!
1262 0 return;
1263 }
1264  
1265 # Check to see if the ID matches with the top of the queue
1266 0 ▶ if ( $data->{'ID'} ne $_[HEAP]->{'QUEUE'}->[0]->{'ID'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"ID"} ne $_[3]{"QUEUE"}[0]{"ID"}) 0 0 0
1267 0 die "Internal error in queue/child consistency! ( CHILD: $data->{'ID'} QUEUE: $_[HEAP]->{'QUEUE'}->[0]->{'ID'}";
1268 }
1269  
1270 # Get the query from the top of the queue
1271 0 my $query = shift( @{ $_[HEAP]->{'QUEUE'} } );
1272  
1273 # If this was a connect/disconnect, update our status accordingly
1274 0 ▶ if ( $query->{'ACTION'} eq 'CONNECT' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($query->{'ACTION'} eq 'CONNECT') { } 0 0 0
elsif ($query->{'ACTION'} eq 'DISCONNECT') { } 0 0 0
1275 # Did it succeed?
1276 0 ▶ if ( ! exists $data->{'ERROR'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $data->{"ERROR"}) 0 0 0
1277 0 $_[HEAP]->{'CONNECTED'} = 1;
1278 }
1279 } elsif ( $query->{'ACTION'} eq 'DISCONNECT' ) {
1280 # Did it succeed?
1281 0 ▶ if ( ! exists $data->{'ERROR'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $data->{"ERROR"}) 0 0 0
1282 0 $_[HEAP]->{'CONNECTED'} = 0;
1283 }
1284 }
1285  
1286 # Build the return hash
1287 my $ret = {
1288 'ACTION' => $query->{'ACTION'},
1289 'ID' => $data->{'ID'},
1290 'EVENT' => $query->{'EVENT'},
1291 0 'SESSION' => $query->{'SESSION'},
1292 };
1293  
1294 # Was this an error?
1295 0 ▶ if ( exists $data->{'ERROR'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'ERROR'}) { } 0 0 0
1296 0 $ret->{'ERROR'} = $data->{'ERROR'};
1297 } else {
1298 0 $ret->{'RESULT'} = $data->{'DATA'};
1299 0 ▶ if ( exists $data->{'INSERTID'} and defined $data->{'INSERTID'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"INSERTID"} and defined $data->{"INSERTID"}) 0 0 0
Condition0%0/3
exists $data->{"INSERTID"} and defined $data->{"INSERTID"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"INSERTID"} and defined $data->{"INSERTID"}
exists $data->{"INSERTID"} defined $data->{"INSERTID"}
Truth table0%0/3
A and B
A= exists $data->{"INSERTID"}B= defined $data->{"INSERTID"}
ABresult
0X0
100
111
1300 0 $ret->{'INSERTID'} = $data->{'INSERTID'};
1301 }
1302 }
1303  
1304 # Add the extra fields
1305 0 ▶ if ( $query->{'ACTION'} eq 'CONNECT' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($query->{'ACTION'} eq 'CONNECT') { } 0 0 0
elsif ($query->{'ACTION'} ne 'DISCONNECT') { } 0 0 0
1306 0 $ret->{'DSN'} = $query->{'DSN'};
1307 0 $ret->{'USERNAME'} = $query->{'USERNAME'};
1308 0 $ret->{'PASSWORD'} = $query->{'PASSWORD'};
1309 0 $ret->{'AUTO_COMMIT'} = $query->{'AUTO_COMMIT'};
1310 0 ▶ $ret->{'CACHEDKIDS'} = $query->{'CACHEDKIDS'} if defined $query->{'CACHEDKIDS'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $query->{"CACHEDKIDS"} 0 0 0
1311 } elsif ( $query->{'ACTION'} ne 'DISCONNECT' ) {
1312 0 $ret->{'SQL'} = $query->{'SQL'};
1313  
1314 0 ▶ if ( exists $query->{'PLACEHOLDERS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $query->{"PLACEHOLDERS"}) 0 0 0
1315 0 $ret->{'PLACEHOLDERS'} = $query->{'PLACEHOLDERS'};
1316 }
1317 }
1318  
1319 0 ▶ if ( exists $query->{'BAGGAGE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $query->{"BAGGAGE"}) 0 0 0
1320 0 $ret->{'BAGGAGE'} = $query->{'BAGGAGE'};
1321 }
1322  
1323 # Send the data to the appropriate place
1324 0 $_[KERNEL]->post( $query->{'SESSION'}, $query->{'EVENT'}, $ret );
1325  
1326 # Decrement the refcount for the session that sent us a query
1327 0 $_[KERNEL]->refcount_decrement( $query->{'SESSION'}, 'SimpleDBI' );
1328  
1329 # Now, that we have got a result, check if we need to send another query
1330 0 $_[HEAP]->{'ACTIVE'} = 0;
1331 0 ▶ if ( scalar( @{ $_[HEAP]->{'QUEUE'} } ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$_[3]{"QUEUE"};} > 0) 0 0 0
1332 0 $_[KERNEL]->call( $_[SESSION], 'Check_Queue' );
1333 }
1334  
1335 0 return;
1336 }
1337  
1338 # Handles child STDERR output
1339 sub Got_STDERR {
1340 0 ▶ my $input = $_[ARG0];
Subroutine Got_STDERR: not called CC=2 SCAR=17.9
Pod undocumented
1341  
1342 # Skip empty lines as the POE::Filter::Line manpage says...
1343 0 ▶ if ( $input eq '' ) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq "") 0 0 0
1344  
1345 0 warn __PACKAGE__ . " Got STDERR from child, which should never happen ( $input )";
1346  
1347 0 return;
1348 }
1349  
1350 1 1;
1351  
1352  
1353 __END__
↑ Summary blib/lib/POE/Component/SimpleDBI/SubProcess.pm »