DBI.pm

filter: Statementstmt 12.8% 31 / 242 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 21.2% 7 / 33 Podpod 0.0% 0 / 20 totaltotal 10.3% 38 / 369 CC 35 SCAR 28.0
CC
35
Cov
10%
enqueue
56.0
exception_list
42.0
job_status_list
20.0
CRAP 16.4
↑ Summary DBI/DBD.pm »
1 package Qudo::Driver::DBI;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.03';
7  
8 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Qudo::Driver::DBI::DBD;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub init_driver {
Subroutine init_driver: not called CC=2 SCAR=17.9
Pod undocumented
14 0 my ($class, $master) = @_;
15  
16 0 for my $database (@{$master->{databases}}) {
17 0 my $connection = bless {
18 database => $database,
19 dbh => '',
20 dbd => '',
21 }, $class;
22 0 $connection->_connect();
23  
24 0 my $dbd_type = $connection->{dbh}->{Driver}->{Name};
25 0 $connection->{dbd} = Qudo::Driver::DBI::DBD->new($dbd_type);
26  
27 0 $master->set_connection($database->{dsn}, $connection);
28 }
29 }
30  
31 0 ▶sub _connect {
Subroutine _connect: not called CC=2 SCAR=17.9
32 0 my $self = shift;
33
34 $self->{dbh} = DBI->connect(
35 $self->{database}->{dsn},
36 $self->{database}->{username},
37 $self->{database}->{password},
38 0 ▶ { RaiseError => 1, PrintError => 0, AutoCommit => 1, %{ $self->{database}->{connect_options} || {} } }
Condition0%0/2
$self->{'database'}{'connect_options'} || {}
l!l
00
MC/DC0%0/1
$self->{'database'}{'connect_options'} || {}
$self->{'database'}{'connect_options'}
Truth table0%0/2
A
A= $self->{'database'}{'connect_options'}
Aresult
11
00
39 );
40 }
41  
42 0 ▶sub dbh{
Subroutine dbh: not called CC=1 SCAR=6.9
Pod undocumented
43 0 my $self = shift;
44  
45 0 return $self->{dbh};
46 }
47  
48 0 ▶sub job_status_list {
Subroutine job_status_list: not called CC=4 SCAR=30.0
Pod undocumented
49 0 my ($self, $args) = @_;
50  
51 0 my $sql = q{
52 SELECT
53 job_status.id,
54 job_status.func_id,
55 job_status.arg,
56 job_status.uniqkey,
57 job_status.status,
58 job_status.job_start_time,
59 job_status.job_end_time
60 FROM
61 job_status
62 };
63  
64 0 my @funcs;
65 0 ▶ if( $args->{funcs} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"funcs"}) 0 0 0
66 0 ▶ if( ref($args->{funcs}) eq 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'funcs'} eq 'ARRAY') { } 0 0 0
67 0 @funcs = @{$args->{funcs}};
68 }
69 else{
70 0 push @funcs , $args->{funcs};
71 }
72  
73 $sql .= sprintf( q{
74 INNER JOIN
75 func
76 ON
77 job_status.func_id = func.id
78 0 WHERE (func.name IN (%s) )}, join(',',map{'?'} @funcs) );
79 }
80  
81 0 $sql .= q{ LIMIT ? OFFSET ? };
82  
83 my $sth = $self->_execute(
84 $sql,
85 0 [ @funcs , $args->{limit} ,$args->{offset} ]
86 );
87  
88 0 my @job_status_list;
89 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
90 0 push @job_status_list, $row;
91 }
92 0 return \@job_status_list;
93 }
94  
95 0 ▶sub job_count {
Subroutine job_count: not called CC=4 SCAR=30.0
Pod undocumented
96 0 my ($self , $funcs) = @_;
97  
98 0 my @bind;
99 0 ▶ if( ref $funcs eq 'ARRAY' ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $funcs eq 'ARRAY') { } 0 0 0
elsif (defined $funcs) { } 0 0 0
100 0 @bind = @{$funcs};
101 }
102 elsif( defined $funcs ){
103 0 push @bind , $funcs;
104 }
105  
106 0 my $sql = q{
107 SELECT
108 COUNT(job.id) AS count
109 FROM
110 job
111 };
112  
113 0 ▶ if( scalar @bind ){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @bind) 0 0 0
114 0 $sql .= q{
115 INNER JOIN
116 func ON job.func_id = func.id
117 WHERE
118 };
119 0 $sql .= $self->_join_func_name( \@bind );
120 }
121  
122 0 my $sth = $self->_execute(
123 $sql,
124 \@bind
125 );
126  
127 0 my $ret = $sth->fetchrow_hashref();
128 0 return $ret->{count};
129 }
130  
131 0 ▶sub job_list {
Subroutine job_list: not called CC=3 SCAR=24.8
Pod undocumented
132 0 my ($self, $limit, $funcs) = @_;
133  
134 0 my $sql = $self->_search_job_sql();
135 0 $sql .= q{
136 WHERE
137 job.grabbed_until <= ?
138 AND
139 job.run_after <= ?
140 };
141 0 my @bind = $self->get_server_time;
142 0 push @bind, $self->get_server_time;
143  
144 # func.name
145 0 ▶ if( $funcs ){
Branch: 1 branch
BranchTrueFalseTotal
if ($funcs) 0 0 0
146 0 $sql .= q{ AND }. $self->_join_func_name($funcs);
147 0 push @bind , @{$funcs};
148 }
149  
150 # limit
151 0 $sql .= q{LIMIT ?};
152 0 push @bind , $limit;
153  
154 0 my $sth = $self->_execute(
155 $sql,
156 \@bind
157 );
158  
159 0 my $code = $self->_get_job_data( $sth );
160  
161 0 my @jobs;
162 0 while (1) {
163 0 my $row = $code->();
164 0 ▶ last unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
165 0 push @jobs, $row;
166 }
167 0 return \@jobs;
168 }
169  
170 0 ▶sub exception_list {
Subroutine exception_list: not called CC=6 SCAR=37.4
Pod undocumented
171 0 my ($self, $args) = @_;
172  
173 0 my @bind = ();
174 0 my $limit = $args->{limit};
175 0 my $offset = $args->{offset};
176 0 ▶ my $funcs = $args->{funcs} || '';
Condition0%0/2
$args->{'funcs'} || ''
l!l
00
MC/DC0%0/1
$args->{'funcs'} || ''
$args->{'funcs'}
Truth table0%0/2
A
A= $args->{'funcs'}
Aresult
11
00
177 0 my $sql = q{
178 SELECT
179 exception_log.id,
180 exception_log.func_id,
181 exception_log.exception_time,
182 exception_log.message,
183 exception_log.uniqkey,
184 exception_log.arg,
185 exception_log.retried
186 FROM
187 exception_log
188 };
189  
190 # funcs
191 0 ▶ if ($funcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($funcs) 0 0 0
192 0 $sql .= q{
193 INNER JOIN
194 func
195 ON
196 exception_log.func_id = func.id
197 WHERE
198 };
199 0 $sql .= $self->_join_func_name($funcs);
200 0 push @bind , @{$funcs};
201 }
202  
203 # limit
204 0 ▶ if( $limit ){
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) 0 0 0
205 0 $sql .= q{ LIMIT ? };
206 0 push @bind , $limit;
207 }
208  
209 #offset
210 0 ▶ if( $offset ){
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 0 0 0
211 0 $sql .= q{OFFSET ?};
212 0 push @bind , $offset;
213 }
214  
215 0 my $sth = $self->_execute(
216 $sql,
217 \@bind
218 );
219  
220 0 my @exception_list;
221 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
222 0 push @exception_list, $row;
223 }
224 0 return \@exception_list;
225 }
226  
227  
228 0 ▶sub lookup_job {
Subroutine lookup_job: not called CC=2 SCAR=17.9
Pod undocumented
229 0 my ($self, $job_id) = @_;
230  
231 0 my $sql = $self->_search_job_sql();
232  
233 0 my @bind;
234 # func.name
235 0 ▶ if( $job_id ){
Branch: 1 branch
BranchTrueFalseTotal
if ($job_id) 0 0 0
236 0 $sql .= q{ WHERE job.id = ?};
237 0 push @bind , $job_id;
238 }
239  
240 # limit
241 0 $sql .= q{ LIMIT 1};
242  
243 0 my $sth = $self->_execute(
244 $sql,
245 \@bind
246 );
247  
248 0 return $self->_get_job_data( $sth );
249 }
250  
251 0 ▶sub find_job {
Subroutine find_job: not called CC=2 SCAR=17.9
Pod undocumented
252 0 my ($self, $limit, $func_ids) = @_;
253  
254 0 my $sql = $self->_search_job_sql();
255 0 $sql .= q{
256 WHERE
257 job.grabbed_until <= ?
258 AND
259 job.run_after <= ?
260 };
261 0 my @bind = $self->get_server_time;
262 0 push @bind, $self->get_server_time;
263  
264 # func.name
265 0 ▶ if( $func_ids ){
Branch: 1 branch
BranchTrueFalseTotal
if ($func_ids) 0 0 0
266 0 $sql .= q{ AND }. $self->_join_func_ids($func_ids);
267 0 push @bind , @{$func_ids};
268 }
269  
270 # priority
271 0 $sql .= q{ ORDER BY job.priority DESC };
272  
273 # limit
274 0 $sql .= q{ LIMIT ? };
275 0 push @bind , $limit;
276  
277 0 my $sth = $self->_execute(
278 $sql,
279 \@bind
280 );
281  
282 0 return $self->_get_job_data( $sth );
283 }
284  
285 0 ▶sub _search_job_sql {
Subroutine _search_job_sql: not called CC=1 SCAR=6.9
286 0 q{
287 SELECT
288 job.id AS id,
289 job.arg AS arg,
290 job.uniqkey AS uniqkey,
291 job.func_id AS func_id,
292 job.grabbed_until AS grabbed_until,
293 job.retry_cnt AS retry_cnt,
294 job.priority AS priority
295 FROM job
296 };
297 }
298  
299 0 ▶sub _get_job_data {
Subroutine _get_job_data: not called CC=1 SCAR=6.9
300 0 my ($self, $sth) = @_;
301 0 ▶ sub{
Subroutine __ANON__: not called CC=2 SCAR=17.9
302 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
303 return +{
304 job_id => $row->{id},
305 job_arg => $row->{arg},
306 job_uniqkey => $row->{uniqkey},
307 job_grabbed_until => $row->{grabbed_until},
308 job_retry_cnt => $row->{retry_cnt},
309 job_priority => $row->{priority},
310 func_id => $row->{func_id},
311 0 };
312 }
313 0 return;
314 0 };
315 }
316  
317 0 ▶sub grab_a_job {
Subroutine grab_a_job: not called CC=1 SCAR=6.9
Pod undocumented
318 0 my ($self, %args) = @_;
319  
320 0 my $sql = q{
321 UPDATE
322 job
323 SET
324 grabbed_until = ?
325 WHERE
326 id = ?
327 AND
328 grabbed_until = ?
329 };
330  
331 my @bind = (
332 $args{grabbed_until},
333 $args{job_id},
334 $args{old_grabbed_until},
335 0 );
336  
337 0 my $sth = $self->_execute(
338 $sql,
339 \@bind
340 );
341  
342 0 return $sth->rows;
343 }
344  
345 0 ▶sub logging_exception {
Subroutine logging_exception: not called CC=1 SCAR=6.9
Pod undocumented
346 0 my ($self, $args) = @_;
347  
348 0 my $sql = q{
349 INSERT INTO exception_log
350 ( func_id , message , uniqkey, arg, exception_time, retried)
351 VALUES
352 ( ? , ? , ?, ?, ?, ?)
353 };
354 my @bind = (
355 $args->{func_id} ,
356 $args->{message} ,
357 $args->{uniqkey} ,
358 $args->{arg} ,
359 0 time(),
360 0,
361 );
362  
363 0 my $sth = $self->_execute(
364 $sql,
365 \@bind
366 );
367  
368 0 return;
369 }
370  
371 0 ▶sub set_job_status{
Subroutine set_job_status: not called CC=1 SCAR=6.9
Pod undocumented
372 0 my ($self, $args) = @_;
373  
374 0 my @column = keys %{$args};
375 0 my $sql = $self->_build_insert_sql(
376 'job_status',
377 \@column
378 );
379  
380 0 my @bind = map {$args->{$_}} @column;
381  
382 0 $self->_execute(
383 $sql,
384 \@bind
385 );
386  
387 0 return;
388 }
389  
390 0 ▶sub get_server_time {
Subroutine get_server_time: not called CC=2 SCAR=17.9
Pod undocumented
391 0 my $self = shift;
392  
393 0 my $unixtime_sql = $self->{dbd}->sql_for_unixtime;
394 0 my $time;
395 0 eval {
396 0 $time = $self->{dbh}->selectrow_array("SELECT $unixtime_sql");
397 };
398 0 ▶ if ($@) { $time = time }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
399 0 return $time;
400 }
401  
402 0 ▶sub enqueue {
Subroutine enqueue: not called CC=7 SCAR=40.3
Pod undocumented
403 0 my ($self, $args) = @_;
404  
405 0 ▶ $args->{enqueue_time} ||= time;
Condition0%0/2
$args->{"enqueue_time"} ||= time
l!l
00
MC/DC0%0/1
$args->{"enqueue_time"} ||= time
$args->{"enqueue_time"}
Truth table0%0/2
A
A= $args->{"enqueue_time"}
Aresult
11
00
406 0 ▶ $args->{grabbed_until} ||= 0;
Condition0%0/2
$args->{"grabbed_until"} ||= 0
l!l
00
MC/DC0%0/1
$args->{"grabbed_until"} ||= 0
$args->{"grabbed_until"}
Truth table0%0/2
A
A= $args->{"grabbed_until"}
Aresult
11
00
407 0 ▶ $args->{retry_cnt} ||= 0;
Condition0%0/2
$args->{"retry_cnt"} ||= 0
l!l
00
MC/DC0%0/1
$args->{"retry_cnt"} ||= 0
$args->{"retry_cnt"}
Truth table0%0/2
A
A= $args->{"retry_cnt"}
Aresult
11
00
408 0 ▶ $args->{run_after} = time + ($args->{run_after}||0);
Condition0%0/2
$args->{'run_after'} || 0
l!l
00
MC/DC0%0/1
$args->{'run_after'} || 0
$args->{'run_after'}
Truth table0%0/2
A
A= $args->{'run_after'}
Aresult
11
00
409 0 ▶ $args->{priority} ||= 0;
Condition0%0/2
$args->{"priority"} ||= 0
l!l
00
MC/DC0%0/1
$args->{"priority"} ||= 0
$args->{"priority"}
Truth table0%0/2
A
A= $args->{"priority"}
Aresult
11
00
410  
411 0 my @column = keys %{$args};
412 0 my $sql = $self->_build_insert_sql(
413 'job',
414 \@column
415 );
416 0 my @bind = map {$args->{$_}} @column;
417  
418 0 my $sth_ins = $self->_execute(
419 $sql,
420 \@bind
421 );
422  
423  
424 0 my $id = $self->{dbd}->last_insert_id($self->{dbh}, $sth_ins);
425 0 my $sth_sel = $self->_execute(
426 q{SELECT * FROM job WHERE id = ?} ,
427 [ $id ]
428 );
429  
430 0 my $ret_sel = $sth_sel->fetchrow_hashref();
431 0 ▶ return $ret_sel ? $ret_sel->{id} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ret_sel ? : 0 0 0
432 }
433  
434 0 ▶sub reenqueue {
Subroutine reenqueue: not called CC=2 SCAR=17.9
Pod undocumented
435 0 my ($self, $job_id, $args) = @_;
436  
437 0 my $sql = q{
438 UPDATE
439 job
440 SET
441 enqueue_time = ?,
442 run_after = ?,
443 retry_cnt = ?,
444 grabbed_until = ?
445 WHERE
446 id = ?
447 };
448  
449 my @bind = (
450 time,
451 (time + ($args->{retry_delay}||0) ),
452 $args->{retry_cnt},
453 $args->{grabbed_until},
454 0 ▶ $job_id,
Condition0%0/2
$args->{'retry_delay'} || 0
l!l
00
MC/DC0%0/1
$args->{'retry_delay'} || 0
$args->{'retry_delay'}
Truth table0%0/2
A
A= $args->{'retry_delay'}
Aresult
11
00
455 );
456  
457 0 my $sth = $self->_execute(
458 $sql,
459 \@bind
460 );
461  
462 0 return $sth->rows;
463 }
464  
465  
466 0 ▶sub dequeue {
Subroutine dequeue: not called CC=1 SCAR=6.9
Pod undocumented
467 0 my ($self, $args) = @_;
468  
469 my $sth = $self->_execute(
470 q{DELETE FROM job WHERE id = ?} ,
471 0 [ $args->{id} ]
472 );
473  
474 0 return $sth->rows;
475 }
476  
477  
478 0 ▶sub get_func_id {
Subroutine get_func_id: not called CC=3 SCAR=24.8
Pod undocumented
479 0 my ($self, $funcname) = @_;
480
481 0 my $sth_sel = $self->_execute(
482 q{SELECT * FROM func WHERE name = ?} ,
483 [ $funcname ]
484 );
485  
486 0 my $func_id;
487 0 my $ret_hashref = $sth_sel->fetchrow_hashref();
488 0 ▶ if ( $ret_hashref ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_hashref) { } 0 0 0
489 0 $func_id = $ret_hashref->{id};
490 }
491 else{
492 0 my $sth_ins = $self->_execute(
493 q{INSERT INTO func ( name ) VALUES ( ? )} ,
494 [ $funcname ]
495 );
496  
497 0 $sth_sel->execute( $funcname );
498 0 my $ret_hashref = $sth_sel->fetchrow_hashref();
499 0 ▶ if ( $ret_hashref ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_hashref) 0 0 0
500 0 $func_id = $ret_hashref->{id};
501 }
502 }
503  
504 0 return $func_id;
505 }
506  
507 0 ▶sub get_func_name {
Subroutine get_func_name: not called CC=2 SCAR=17.9
Pod undocumented
508 0 my ($self, $funcid) = @_;
509  
510 0 my $sth = $self->_execute(
511 q{SELECT * FROM func WHERE id = ?} ,
512 [ $funcid ]
513 );
514  
515 0 my $ret_hashref = $sth->fetchrow_hashref();
516 0 ▶ return $ret_hashref ? $ret_hashref->{name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ret_hashref ? : 0 0 0
517 }
518  
519 0 ▶sub retry_from_exception_log {
Subroutine retry_from_exception_log: not called CC=1 SCAR=6.9
Pod undocumented
520 0 my ($self, $exception_log_id) = @_;
521  
522 0 $self->_execute(
523 q{UPDATE exception_log SET retried = 1 WHERE id = ?},
524 [$exception_log_id]
525 );
526 }
527  
528 0 ▶sub _execute {
Subroutine _execute: not called CC=2 SCAR=17.9
529 0 my ($self, $sql, $bind) = @_;
530  
531 0 my $sth;
532 0 eval {
533 0 $sth = $self->{dbh}->prepare($sql);
534 0 $sth->execute(@{$bind});
535 };
536 0 ▶ if ($@) { croak $@ }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
537 0 $sth;
538 }
539  
540 0 ▶sub _join_func_ids{
Subroutine _join_func_ids: not called CC=1 SCAR=6.9
541 0 my ($self , $func_ids ) = @_;
542  
543 my $func_in_sql = sprintf(
544 q{ job.func_id IN (%s) } ,
545 0 join(',', map { '?' } @{$func_ids} )
546 );
547  
548 0 return $func_in_sql;
549 }
550  
551 3 ▶sub _join_func_name{
Subroutine _join_func_name: called CC=1 SCAR=0.0
552 3 my ($self , $funcs ) = @_;
553  
554 my $func_name = sprintf(
555 q{ func.name IN (%s) } ,
556 3 join(',', map { '?' } @{$funcs} )
557 );
558  
559 3 return $func_name;
560 }
561  
562 2 ▶sub _build_insert_sql{
Subroutine _build_insert_sql: called CC=1 SCAR=0.0
563 2 my( $self , $table , $column_ary_ref ) = @_;
564  
565 2 my $sql = qq{ INSERT INTO $table ( };
566 2 $sql .= join ' , ' , @{$column_ary_ref};
567 2 $sql .= ' ) VALUES ( ';
568 2 $sql .= join( ' , ', ('?') x @{$column_ary_ref} );
569 2 $sql .= ' )';
570  
571 2 return $sql;
572 }
573  
574 0 ▶sub func_from_name {
Subroutine func_from_name: not called CC=4 SCAR=30.0
Pod undocumented
575 0 my ($self, $funcname) = @_;
576  
577 0 my $result;
578 0 ▶ while ( ! $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
579 0 my $sth = $self->_execute(q{
580 SELECT id, name FROM func WHERE name = ? LIMIT 1
581 }, [$funcname]);
582 0 my $couner = 0;
583 0 ▶ while ( my $row = $sth->fetchrow_hashref() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
584 0 $result = $row;
585 }
586 0 ▶ if ( ! $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
587 0 $self->_execute(q{
588 INSERT INTO func (name) VALUES (?)
589 }, [ $funcname ]);
590 }
591 }
592 0 return $result;
593 }
594  
595 0 ▶sub func_from_id {
Subroutine func_from_id: not called CC=2 SCAR=17.9
Pod undocumented
596 0 my ($self, $funcid) = @_;
597 0 my $result;
598 0 my $sth = $self->_execute(q{
599 SELECT id, name FROM func WHERE id = ? LIMIT 1
600 }, [$funcid]);
601 0 my $couner = 0;
602 0 ▶ while ( my $row = $sth->fetchrow_hashref() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
603 0 $result = $row;
604 }
605 0 return $result;
606 }
607  
608 2 1;
609  
610 __END__
↑ Summary DBI/DBD.pm »