blib/lib/POE/Component/SimpleDBI/SubProcess.pm

filter: Statementstmt 8.2% 22 / 267 Branchbran 0.0% 0 / 116 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 13.7% 7 / 51 Podpod 0.0% 0 / 12 totaltotal 5.6% 29 / 515 SCAR 84.9
CC
75
Cov
5%
process_request
110.0
DB_DO
90.0
main
42.0
CRAP 4870.9
« blib/lib/POE/Component/SimpleDBI.pm ↑ Summary Build »
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::SubProcess;
11 BEGIN {
12 1 ▶ $POE::Component::SimpleDBI::SubProcess::VERSION = '1.30';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14 BEGIN {
15 1 ▶ $POE::Component::SimpleDBI::SubProcess::AUTHORITY = 'cpan:APOCAL';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 }
17  
18 # ABSTRACT: Backend of POE::Component::SimpleDBI
19  
20 # Use Error.pm's try/catch semantics
21 1 ▶use Error 0.15 qw( :try );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # We pass in data to POE::Filter::Reference
24 1 ▶use POE::Filter::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # We run the actual DB connection here
27 1 ▶use DBI 1.30;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Our Filter object
30 1 my $filter = POE::Filter::Reference->new();
31  
32 # Our DBI handle
33 1 my $DB = undef;
34  
35 # Save the connect struct for future use
36 1 my $CONN = undef;
37  
38 # Sysread error hits
39 1 my $sysreaderr = 0;
40  
41 # Shut up Perl::Critic!
42 ## no critic ( ProhibitAccessOfPrivateData )
43  
44 # This is the subroutine that will get executed upon the fork() call by our parent
45 sub main {
46 # Autoflush to avoid weirdness
47 0 ▶ $|++;
Subroutine main: not called CC=6 SCAR=37.4
Pod undocumented
48  
49 # set binmode, thanks RT #43442
50 0 binmode( STDIN );
51 0 binmode( STDOUT );
52  
53 # Okay, now we listen for commands from our parent :)
54 0 ▶ while ( sysread( STDIN, my $buffer = '', 1024 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread STDIN, my $buffer = "", 1024) 0 0 0
55 # Feed the line into the filter
56 0 my $data = $filter->get( [ $buffer ] );
57  
58 # INPUT STRUCTURE IS:
59 # $d->{'ACTION'} = SCALAR -> WHAT WE SHOULD DO
60 # $d->{'SQL'} = SCALAR -> THE ACTUAL SQL
61 # $d->{'SQL'} = ARRAY -> THE ACTUAL SQL ( in case of ATOMIC )
62 # $d->{'PLACEHOLDERS'} = ARRAY -> PLACEHOLDERS WE WILL USE
63 # $d->{'PREPARE_CACHED'}= BOOLEAN -> USE CACHED QUERIES?
64 # $d->{'ID'} = SCALAR -> THE QUERY ID ( FOR PARENT TO KEEP TRACK OF WHAT IS WHAT )
65  
66 # $d->{'DSN'} = SCALAR -> DSN for CONNECT
67 # $d->{'USERNAME'} = SCALAR -> USERNAME for CONNECT
68 # $d->{'PASSWORD'} = SCALAR -> PASSWORD for CONNECT
69  
70 # Process each data structure
71 0 foreach my $input ( @$data ) {
72 0 output( process_request( $input ) );
73 0 ▶ return if $input->{'ACTION'} eq 'EXIT';
Branch: 1 branch
BranchTrueFalseTotal
if $input->{"ACTION"} eq "EXIT" 0 0 0
74 }
75 }
76  
77 # Arrived here due to error in sysread/etc
78 0 output( Make_Error( 'SYSREAD', $! ) );
79  
80 # If we got more than 5 sysread errors, abort!
81 0 ▶ if ( ++$sysreaderr == 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$sysreaderr == 5) { } 0 0 0
82 0 ▶ if ( defined $DB ) { $DB->disconnect() }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $DB) 0 0 0
83 0 return;
84 } else {
85 0 goto &main;
86 }
87  
88 0 return;
89 }
90  
91 sub process_request {
92 0 ▶ my $input = shift;
Subroutine process_request: not called CC=10 SCAR=47.0
Pod undocumented
93  
94 # Now, we do the actual work depending on what kind of query it was
95 0 ▶ if ( $input->{'ACTION'} eq 'CONNECT' ) {
Branch: 8 branches
BranchTrueFalseTotal
if ($input->{'ACTION'} eq 'CONNECT') { } 0 0 0
elsif ($input->{'ACTION'} eq 'DISCONNECT') { } 0 0 0
elsif ($input->{'ACTION'} eq 'DO') { } 0 0 0
elsif ($input->{'ACTION'} eq 'SINGLE') { } 0 0 0
elsif ($input->{'ACTION'} eq 'MULTIPLE') { } 0 0 0
elsif ($input->{'ACTION'} eq 'QUOTE') { } 0 0 0
elsif ($input->{'ACTION'} eq 'ATOMIC') { } 0 0 0
elsif ($input->{'ACTION'} eq 'EXIT') { } 0 0 0
96 # Connect!
97 0 my ($success, $output) = DB_CONNECT($input, 0);
98 0 return $output;
99 } elsif ( $input->{'ACTION'} eq 'DISCONNECT' ) {
100 # Disconnect!
101 0 return DB_DISCONNECT( $input );
102 } elsif ( $input->{'ACTION'} eq 'DO' ) {
103 # Fire off the SQL and return success/failure + rows affected
104 0 return DB_DO( $input );
105 } elsif ( $input->{'ACTION'} eq 'SINGLE' ) {
106 # Return a single result
107 0 return DB_SINGLE( $input );
108 } elsif ( $input->{'ACTION'} eq 'MULTIPLE' ) {
109 # Get many results, then return them all at the same time
110 0 return DB_MULTIPLE( $input );
111 } elsif ( $input->{'ACTION'} eq 'QUOTE' ) {
112 0 return DB_QUOTE( $input );
113 } elsif ( $input->{'ACTION'} eq 'ATOMIC' ) {
114 0 return DB_ATOMIC( $input );
115 } elsif ( $input->{'ACTION'} eq 'EXIT' ) {
116 # Cleanly disconnect from the DB
117 0 ▶ if ( defined $DB ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $DB) 0 0 0
118 0 $DB->disconnect();
119 0 undef $DB;
120 }
121  
122 # EXIT!
123 0 return;
124 }
125  
126 # Unrecognized action!
127 0 return( Make_Error( $input->{'ID'}, 'Unknown action sent from parent' ) );
128 }
129  
130 # Connects to the DB
131 sub DB_CONNECT {
132 # Get the input structure
133 0 ▶ my $data = shift;
Subroutine DB_CONNECT: not called CC=5 SCAR=34.0
Pod undocumented
134  
135 # Our output structure
136 0 my $output = undef;
137  
138 # Are we reconnecting?
139 0 my $reconn = shift;
140  
141 # Are we already connected?
142 0 ▶ if ( defined $DB and $DB->ping() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $DB and $DB->ping) { } 0 0 0
Condition0%0/3
defined $DB and $DB->ping
!ll&&!rl&&r
000
MC/DC0%0/2
defined $DB and $DB->ping
defined $DB $DB->ping
Truth table0%0/3
A and B
A= defined $DBB= $DB->ping
ABresult
0X0
100
111
143 # Output success
144 $output = {
145 0 'ID' => $data->{'ID'},
146 };
147 } else {
148 # Actually make the connection :)
149 try {
150 $DB = DBI->connect(
151 # The DSN we just set up
152 $data->{'DSN'},
153  
154 # Username
155 $data->{'USERNAME'},
156  
157 # Password
158 $data->{'PASSWORD'},
159  
160 # We set some configuration stuff here
161 {
162 # We do not want users seeing 'spam' on the commandline...
163 'PrintError' => 0,
164 'PrintWarn' => 0,
165  
166 # Automatically raise errors so we can catch them with try/catch
167 'RaiseError' => 1,
168  
169 # Disable the DBI tracing
170 'TraceLevel' => 0,
171  
172 # AutoCommit our stuff?
173 0 ▶ 'AutoCommit' => $data->{'AUTO_COMMIT'},
Subroutine __ANON__: not called CC=4 SCAR=30.0
174 }
175 );
176  
177 # Check for undefined-ness
178 0 ▶ if ( ! defined $DB ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $DB) { } 0 0 0
179 0 die "Error Connecting: $DBI::errstr";
180 } else {
181 # Did we request a custom cache module?
182 0 ▶ if ( defined $data->{'CACHEDKIDS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{"CACHEDKIDS"}) 0 0 0
183 0 eval "require $data->{'CACHEDKIDS'}->[0]";
184 0 ▶ die "Unable to load custom caching module: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
185  
186 # code lifted from DBI's POD, thanks!
187 0 my $cache;
188 0 tie %$cache, @{ $data->{'CACHEDKIDS'} };
189 0 $DB->{'CachedKids'} = $cache;
190 }
191  
192 # Output success
193 $output = {
194 0 'ID' => $data->{'ID'},
195 };
196  
197 # Save this!
198 0 $CONN = $data;
199 }
200 } catch Error with {
201 # Get the error
202 0 ▶ my $e = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
203  
204 # Declare it!
205 0 $output = Make_Error( $data->{'ID'}, $e );
206 0 };
207 }
208  
209 # All done!
210 0 ▶ if ( ! defined $reconn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $reconn) { } 0 0 0
211 0 return (1, $output);
212 } else {
213 # Reconnect attempt, was it successful?
214 0 ▶ if ( ! exists $output->{'ERROR'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $output->{"ERROR"}) 0 0 0
215 0 return (1, $output);
216 }
217 }
218  
219 0 return (0, $output);
220 }
221  
222 # Disconnects from the DB
223 sub DB_DISCONNECT {
224 # Get the input structure
225 0 ▶ my $data = shift;
Subroutine DB_DISCONNECT: not called CC=2 SCAR=17.9
Pod undocumented
226  
227 # Our output structure
228 0 my $output = undef;
229  
230 # Are we already disconnected?
231 0 ▶ if ( ! defined $DB ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $DB) { } 0 0 0
232 # Output success
233 $output = {
234 0 'ID' => $data->{'ID'},
235 };
236 } else {
237 # Disconnect from the DB
238 try {
239 0 ▶ $DB->disconnect();
Subroutine __ANON__: not called CC=1 SCAR=6.9
240 0 undef $DB;
241  
242 # Output success
243 $output = {
244 0 'ID' => $data->{'ID'},
245 };
246 } catch Error with {
247 # Get the error
248 0 ▶ my $e = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
249  
250 # Declare it!
251 0 $output = Make_Error( $data->{'ID'}, $e );
252 0 };
253 }
254  
255 # All done!
256 0 return $output;
257 }
258  
259 # This subroutine does a DB QUOTE
260 sub DB_QUOTE {
261 # Get the input structure
262 0 ▶ my $data = shift;
Subroutine DB_QUOTE: not called CC=5 SCAR=34.0
Pod undocumented
263  
264 # The result
265 0 my $quoted = undef;
266 0 my $output = undef;
267  
268 # Check if we are connected
269 0 ▶ if ( ! defined $DB or ! $DB->ping() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $DB and $DB->ping) 0 0 0
Condition0%0/3
defined $DB and $DB->ping
!ll&&!rl&&r
000
MC/DC0%0/2
defined $DB and $DB->ping
defined $DB $DB->ping
Truth table0%0/3
A and B
A= defined $DBB= $DB->ping
ABresult
0X0
100
111
270 # Automatically try to reconnect
271 0 ▶ if ( ! ( DB_CONNECT( $CONN, 'RECONNECT' ) )[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((DB_CONNECT($CONN, "RECONNECT"))[0]) 0 0 0
272 0 return Make_Error( 'GONE', 'Lost connection to the database server.' );
273 }
274 }
275  
276 # Quote it!
277 try {
278 0 ▶ $quoted = $DB->quote( $data->{'SQL'} );
Subroutine __ANON__: not called CC=1 SCAR=6.9
279 } catch Error with {
280 # Get the error
281 0 ▶ my $e = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
282  
283 0 $output = Make_Error( $data->{'ID'}, $e );
284 0 };
285  
286 # Check for errors
287 0 ▶ if ( ! defined $output ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output) 0 0 0
288 # Make output include the results
289 0 $output = {};
290 0 $output->{'DATA'} = $quoted;
291 0 $output->{'ID'} = $data->{'ID'};
292 }
293  
294 # All done!
295 0 return $output;
296 }
297  
298 # This subroutine runs a 'SELECT' style query on the db
299 sub DB_MULTIPLE {
300 # Get the input structure
301 0 ▶ my $data = shift;
Subroutine DB_MULTIPLE: not called CC=6 SCAR=37.4
Pod undocumented
302  
303 # Variables we use
304 0 my $output = undef;
305 0 my $sth = undef;
306 0 my $result = [];
307  
308 # Check if we are connected
309 0 ▶ if ( ! defined $DB or ! $DB->ping() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $DB and $DB->ping) 0 0 0
Condition0%0/3
defined $DB and $DB->ping
!ll&&!rl&&r
000
MC/DC0%0/2
defined $DB and $DB->ping
defined $DB $DB->ping
Truth table0%0/3
A and B
A= defined $DBB= $DB->ping
ABresult
0X0
100
111
310 # Automatically try to reconnect
311 0 ▶ if ( ! ( DB_CONNECT( $CONN, 'RECONNECT' ) )[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((DB_CONNECT($CONN, "RECONNECT"))[0]) 0 0 0
312 0 return( Make_Error( 'GONE', 'Lost connection to the database server.' ) );
313 }
314 }
315  
316 # Catch any errors :)
317 try {
318 # Make a new statement handler and prepare the query
319 0 ▶ if ( $data->{'PREPARE_CACHED'} ) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'PREPARE_CACHED'}) { } 0 0 0
320 0 $sth = $DB->prepare_cached( $data->{'SQL'} );
321 } else {
322 0 $sth = $DB->prepare( $data->{'SQL'} );
323 }
324  
325 # Check for undef'ness
326 0 ▶ if ( ! defined $sth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sth) { } 0 0 0
327 0 die "Did not get sth: $DBI::errstr";
328 } else {
329 # Execute the query
330 try {
331 # Put placeholders?
332 0 ▶ if ( exists $data->{'PLACEHOLDERS'} and defined $data->{'PLACEHOLDERS'} ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'PLACEHOLDERS'} and defined $data->{'PLACEHOLDERS'}) { } 0 0 0
Condition0%0/3
exists $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"}
exists $data->{"PLACEHOLDERS"} defined $data->{"PLACEHOLDERS"}
Truth table0%0/3
A and B
A= exists $data->{"PLACEHOLDERS"}B= defined $data->{"PLACEHOLDERS"}
ABresult
0X0
100
111
333 0 $sth->execute( @{ $data->{'PLACEHOLDERS'} } );
334 } else {
335 0 $sth->execute();
336 }
337 } catch Error with {
338 0 ▶ die $sth->errstr;
Subroutine __ANON__: not called CC=1 SCAR=6.9
339 0 };
340 }
341  
342 # The result hash
343 0 my $newdata;
344  
345 # Bind the columns
346 try {
347 0 ▶ $sth->bind_columns( \( @$newdata{ @{ $sth->{'NAME_lc'} } } ) );
Subroutine __ANON__: not called CC=1 SCAR=6.9
348 } catch Error with {
349 0 ▶ die $sth->errstr;
Subroutine __ANON__: not called CC=1 SCAR=6.9
350 0 };
351  
352 # Actually do the query!
353 try {
354 0 ▶ while ( $sth->fetch() ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) 0 0 0
355 # Copy the data, and push it into the array
356 0 push( @{ $result }, { %{ $newdata } } );
357 }
358 } catch Error with {
359 0 ▶ die $sth->errstr;
Subroutine __ANON__: not called CC=1 SCAR=6.9
360 0 };
361  
362 # Check for any errors that might have terminated the loop early
363 0 ▶ if ( $sth->err() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->err) 0 0 0
364 # Premature termination!
365 0 die $sth->errstr;
366 }
367 } catch Error with {
368 # Get the error
369 0 ▶ my $e = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
370  
371 0 $output = Make_Error( $data->{'ID'}, $e );
372 0 };
373  
374 # Check if we got any errors
375 0 ▶ if ( ! defined $output ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output) 0 0 0
376 # Make output include the results
377 0 $output = {};
378 0 $output->{'DATA'} = $result;
379 0 $output->{'ID'} = $data->{'ID'};
380 }
381  
382 # Finally, we clean up this statement handle
383 0 ▶ if ( defined $sth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sth) 0 0 0
384 0 $sth->finish();
385  
386 # Make sure the object is gone, thanks Sjors!
387 0 undef $sth;
388 }
389  
390 # Return the data structure
391 0 return $output;
392 }
393  
394 # This subroutine runs a 'SELECT ... LIMIT 1' style query on the db
395 sub DB_SINGLE {
396 # Get the input structure
397 0 ▶ my $data = shift;
Subroutine DB_SINGLE: not called CC=6 SCAR=37.4
Pod undocumented
398  
399 # Variables we use
400 0 my $output = undef;
401 0 my $sth = undef;
402 0 my $result = undef;
403  
404 # Check if we are connected
405 0 ▶ if ( ! defined $DB or ! $DB->ping() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $DB and $DB->ping) 0 0 0
Condition0%0/3
defined $DB and $DB->ping
!ll&&!rl&&r
000
MC/DC0%0/2
defined $DB and $DB->ping
defined $DB $DB->ping
Truth table0%0/3
A and B
A= defined $DBB= $DB->ping
ABresult
0X0
100
111
406 # Automatically try to reconnect
407 0 ▶ if ( ! ( DB_CONNECT( $CONN, 'RECONNECT' ) )[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((DB_CONNECT($CONN, "RECONNECT"))[0]) 0 0 0
408 0 return Make_Error( 'GONE', 'Lost connection to the database server.' );
409 }
410 }
411  
412 # Catch any errors :)
413 try {
414 # Make a new statement handler and prepare the query
415 0 ▶ if ( $data->{'PREPARE_CACHED'} ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'PREPARE_CACHED'}) { } 0 0 0
416 0 $sth = $DB->prepare_cached( $data->{'SQL'} );
417 } else {
418 0 $sth = $DB->prepare( $data->{'SQL'} );
419 }
420  
421 # Check for undef'ness
422 0 ▶ if ( ! defined $sth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sth) { } 0 0 0
423 0 die "Did not get sth: $DBI::errstr";
424 } else {
425 # Execute the query
426 try {
427 # Put placeholders?
428 0 ▶ if ( exists $data->{'PLACEHOLDERS'} and defined $data->{'PLACEHOLDERS'} ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'PLACEHOLDERS'} and defined $data->{'PLACEHOLDERS'}) { } 0 0 0
Condition0%0/3
exists $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"}
exists $data->{"PLACEHOLDERS"} defined $data->{"PLACEHOLDERS"}
Truth table0%0/3
A and B
A= exists $data->{"PLACEHOLDERS"}B= defined $data->{"PLACEHOLDERS"}
ABresult
0X0
100
111
429 0 $sth->execute( @{ $data->{'PLACEHOLDERS'} } );
430 } else {
431 0 $sth->execute();
432 }
433 } catch Error with {
434 0 ▶ die $sth->errstr;
Subroutine __ANON__: not called CC=1 SCAR=6.9
435 0 };
436 }
437  
438 # Actually do the query!
439 try {
440 0 ▶ $result = $sth->fetchrow_hashref();
Subroutine __ANON__: not called CC=1 SCAR=6.9
441 } catch Error with {
442 0 ▶ die $sth->errstr;
Subroutine __ANON__: not called CC=1 SCAR=6.9
443 0 };
444 } catch Error with {
445 # Get the error
446 0 ▶ my $e = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
447  
448 0 $output = Make_Error( $data->{'ID'}, $e );
449 0 };
450  
451 # Check if we got any errors
452 0 ▶ if ( ! defined $output ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output) 0 0 0
453 # Make output include the results
454 0 $output = {};
455 0 $output->{'DATA'} = $result;
456 0 $output->{'ID'} = $data->{'ID'};
457 }
458  
459 # Finally, we clean up this statement handle
460 0 ▶ if ( defined $sth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sth) 0 0 0
461 0 $sth->finish();
462  
463 # Make sure the object is gone, thanks Sjors!
464 0 undef $sth;
465 }
466  
467 # Return the data structure
468 0 return $output;
469 }
470  
471 # This subroutine runs a 'DO' style query on the db
472 sub DB_DO {
473 # Get the input structure
474 0 ▶ my $data = shift;
Subroutine DB_DO: not called CC=9 SCAR=45.0
Pod undocumented
475  
476 # Variables we use
477 0 my $output = undef;
478 0 my $sth = undef;
479 0 my $rows_affected = undef;
480 0 my $last_id = undef;
481  
482 # Check if we are connected
483 0 ▶ if ( ! defined $DB or ! $DB->ping() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $DB and $DB->ping) 0 0 0
Condition0%0/3
defined $DB and $DB->ping
!ll&&!rl&&r
000
MC/DC0%0/2
defined $DB and $DB->ping
defined $DB $DB->ping
Truth table0%0/3
A and B
A= defined $DBB= $DB->ping
ABresult
0X0
100
111
484 # Automatically try to reconnect
485 0 ▶ if ( ! ( DB_CONNECT( $CONN, 'RECONNECT' ) )[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((DB_CONNECT($CONN, "RECONNECT"))[0]) 0 0 0
486 0 return Make_Error( 'GONE', 'Lost connection to the database server.' );
487 }
488 }
489  
490 # Catch any errors :)
491 try {
492 # Make a new statement handler and prepare the query
493 0 ▶ if ( $data->{'PREPARE_CACHED'} ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'PREPARE_CACHED'}) { } 0 0 0
494 0 $sth = $DB->prepare_cached( $data->{'SQL'} );
495 } else {
496 0 $sth = $DB->prepare( $data->{'SQL'} );
497 }
498  
499 # Check for undef'ness
500 0 ▶ if ( ! defined $sth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sth) { } 0 0 0
501 0 die "Did not get sth: $DBI::errstr";
502 } else {
503 # Execute the query
504 try {
505 # Put placeholders?
506 0 ▶ if ( exists $data->{'PLACEHOLDERS'} and defined $data->{'PLACEHOLDERS'} ) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'PLACEHOLDERS'} and defined $data->{'PLACEHOLDERS'}) { } 0 0 0
Condition0%0/3
exists $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"}
exists $data->{"PLACEHOLDERS"} defined $data->{"PLACEHOLDERS"}
Truth table0%0/3
A and B
A= exists $data->{"PLACEHOLDERS"}B= defined $data->{"PLACEHOLDERS"}
ABresult
0X0
100
111
507 0 $rows_affected = $sth->execute( @{ $data->{'PLACEHOLDERS'} } );
508 } else {
509 0 $rows_affected = $sth->execute();
510 }
511  
512 # Should we even attempt this?
513 0 ▶ if ( $data->{'INSERT_ID'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"INSERT_ID"}) 0 0 0
514 try {
515 # Get the last insert id ( make this portable! )
516 0 ▶ $last_id = $DB->last_insert_id( undef, undef, undef, undef );
Subroutine __ANON__: not called CC=1 SCAR=6.9
517 ▶ } catch Error with {
Subroutine __ANON__: not called CC=1 SCAR=0.0
518 # Ignore this error!
519 0 };
520 }
521 } catch Error with {
522 0 ▶ die $sth->errstr;
Subroutine __ANON__: not called CC=1 SCAR=6.9
523 0 };
524 }
525 } catch Error with {
526 # Get the error
527 0 ▶ my $e = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
528  
529 0 $output = Make_Error( $data->{'ID'}, $e );
530 0 };
531  
532 # If rows_affected is not undef, that means we were successful
533 0 ▶ if ( defined $rows_affected && ! defined $output ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rows_affected and not defined $output) { } 0 0 0
elsif (not defined $rows_affected || defined $output) { } 0 0 0
Condition0%0/3
defined $rows_affected and not defined $output
!ll&&!rl&&r
000
MC/DC0%0/2
defined $rows_affected and not defined $output
defined $rows_affected not defined $output
Truth table0%0/3
A and not(B)
A= defined $rows_affectedB= not defined $output
ABresult
0X0
100
111
534 # Make the data structure
535 0 $output = {};
536 0 $output->{'DATA'} = $rows_affected;
537 0 $output->{'ID'} = $data->{'ID'};
538 0 $output->{'INSERTID'} = $last_id;
539 ▶ } elsif ( ! defined $rows_affected && ! defined $output ) {
Condition0%0/3
defined $rows_affected || defined $output
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $rows_affected || defined $output
defined $rows_affected defined $output
Truth table0%0/3
A || B
A= defined $rows_affectedB= defined $output
ABresult
1X1
011
000
540 # Internal error...
541 0 die 'Internal Error in DB_DO';
542 }
543  
544 # Finally, we clean up this statement handle
545 0 ▶ if ( defined $sth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sth) 0 0 0
546 0 $sth->finish();
547  
548 # Make sure the object is gone, thanks Sjors!
549 0 undef $sth;
550 }
551  
552 # Return the data structure
553 0 return $output;
554 }
555  
556 # This subroutine runs a 'DO' style query on the db in a transaction
557 sub DB_ATOMIC {
558 # Get the input structure
559 0 ▶ my $data = shift;
Subroutine DB_ATOMIC: not called CC=6 SCAR=37.4
Pod undocumented
560  
561 # Variables we use
562 0 my $output = undef;
563 0 my $sth = undef;
564  
565 # Check if we are connected
566 0 ▶ if ( ! defined $DB or ! $DB->ping() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $DB and $DB->ping) 0 0 0
Condition0%0/3
defined $DB and $DB->ping
!ll&&!rl&&r
000
MC/DC0%0/2
defined $DB and $DB->ping
defined $DB $DB->ping
Truth table0%0/3
A and B
A= defined $DBB= $DB->ping
ABresult
0X0
100
111
567 # Automatically try to reconnect
568 0 ▶ if ( ! ( DB_CONNECT( $CONN, 'RECONNECT' ) )[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((DB_CONNECT($CONN, "RECONNECT"))[0]) 0 0 0
569 0 return Make_Error( 'GONE', 'Lost connection to the database server.' );
570 }
571 }
572  
573 # Catch any errors :)
574 try {
575 # start the transaction
576 0 ▶ $DB->begin_work if $DB->{'AutoCommit'};
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
if $DB->{"AutoCommit"} 0 0 0
577  
578 # process each query
579 0 for my $idx ( 0 .. $#{ $data->{'SQL'} } ) {
580 0 ▶ if ( $data->{'PREPARE_CACHED'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{'PREPARE_CACHED'}) { } 0 0 0
581 0 $sth = $DB->prepare_cached( $data->{'SQL'}->[ $idx ] );
582 } else {
583 0 $sth = $DB->prepare( $data->{'SQL'}->[ $idx ] );
584 }
585  
586 # Check for undef'ness
587 0 ▶ if ( ! defined $sth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sth) { } 0 0 0
588 0 die "Did not get sth: $DBI::errstr";
589 } else {
590 # actually execute it!
591 try {
592 0 ▶ if ( exists $data->{'PLACEHOLDERS'} and defined $data->{'PLACEHOLDERS'} and defined $data->{'PLACEHOLDERS'}->[ $idx ] ) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'PLACEHOLDERS'} and defined $data->{'PLACEHOLDERS'} and defined $data->{'PLACEHOLDERS'}[$idx]) { } 0 0 0
Condition0%0/6
exists $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"}[$idx]
!ll&&!rl&&r
000
exists $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"} and defined $data->{"PLACEHOLDERS"}[$idx]
exists $data->{"PLACEHOLDERS"} defined $data->{"PLACEHOLDERS"} defined $data->{"PLACEHOLDERS"}[$idx]
Truth table0%0/4
A and B and C
A= exists $data->{"PLACEHOLDERS"}B= defined $data->{"PLACEHOLDERS"}C= defined $data->{"PLACEHOLDERS"}[$idx]
ABCresult
0XX0
10X0
1100
1111
593 0 $sth->execute( @{ $data->{'PLACEHOLDERS'}->[ $idx ] } );
594 } else {
595 0 $sth->execute;
596 }
597 } catch Error with {
598 0 ▶ die $sth->errstr;
Subroutine __ANON__: not called CC=1 SCAR=6.9
599 0 };
600  
601 # Finally, we clean up this statement handle
602 0 $sth->finish();
603  
604 # Make sure the object is gone, thanks Sjors!
605 0 undef $sth;
606 }
607 }
608  
609 # done with transaction!
610 0 $DB->commit;
611 } catch Error with {
612 # Get the error
613 0 ▶ my $e = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
614  
615 # rollback the changes!
616 try {
617 0 ▶ $DB->rollback;
Subroutine __ANON__: not called CC=1 SCAR=6.9
618 } catch Error with {
619 # Get the error
620 0 ▶ my $error = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
621  
622 0 $output = Make_Error( $data->{'ID'}, 'ROLLBACK_FAILURE: ' . $error . ' on query error: ' . $e );
623 0 };
624  
625 # did we rollback fine?
626 0 ▶ if ( ! defined $output ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $output) 0 0 0
627 0 $output = Make_Error( $data->{'ID'}, 'COMMIT_FAILURE: ' . $e );
628 }
629 0 };
630  
631 # Finally, we clean up this statement handle
632 0 ▶ if ( defined $sth ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sth) 0 0 0
633 0 $sth->finish();
634  
635 # Make sure the object is gone, thanks Sjors!
636 0 undef $sth;
637 }
638  
639 # If we got no output, we did it!
640 0 ▶ if ( ! defined $output ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output) 0 0 0
641 # Make the data structure
642 0 $output = {};
643 0 $output->{'DATA'} = 'SUCCESS';
644 0 $output->{'ID'} = $data->{'ID'};
645 }
646  
647 # Return the data structure
648 0 return $output;
649 }
650  
651 # This subroutine makes a generic error structure
652 sub Make_Error {
653 # Make the structure
654 0 ▶ my $data = {};
Subroutine Make_Error: not called CC=3 SCAR=24.8
Pod undocumented
655 0 $data->{'ID'} = shift;
656  
657 # Get the error, and stringify it in case of Error::Simple objects
658 0 my $error = shift;
659  
660 0 ▶ if ( ref $error and ref( $error ) eq 'Error::Simple' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $error and ref $error eq 'Error::Simple') { } 0 0 0
Condition0%0/3
ref $error and ref $error eq "Error::Simple"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $error and ref $error eq "Error::Simple"
ref $error ref $error eq "Error::Simple"
Truth table0%0/3
A and B
A= ref $errorB= ref $error eq "Error::Simple"
ABresult
0X0
100
111
661 0 $data->{'ERROR'} = $error->text;
662 } else {
663 0 $data->{'ERROR'} = $error;
664 }
665  
666 # All done!
667 0 return $data;
668 }
669  
670 # This subroutine makes a generic DEBUG structure
671 sub Make_DEBUG {
672 # Make the structure
673 0 ▶ my $data = {};
Subroutine Make_DEBUG: not called CC=1 SCAR=6.9
Pod undocumented
674 0 $data->{'ID'} = 'DEBUG';
675  
676 # Get the data, and shove it in the hash
677 0 my @debug = @_;
678 0 $data->{'RESULT'} = \@debug;
679  
680 # All done!
681 0 return $data;
682 }
683  
684 # Prints any output to STDOUT
685 sub output {
686 # Get the data
687 0 ▶ my $data = shift;
Subroutine output: not called CC=1 SCAR=6.9
Pod undocumented
688  
689 # Freeze it!
690 0 my $output = $filter->put( [ $data ] );
691  
692 # Print it!
693 0 print STDOUT @$output;
694 0 return;
695 }
696  
697 1 1;
698  
699  
700 __END__
« blib/lib/POE/Component/SimpleDBI.pm ↑ Summary Build »