SimpleDBI/SubProcess.pm

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