SimpleDBI.pm

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