Model/Driver/Queue/Q4M.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 164 CC 15 SCAR 36.2
CC
15
Cov
0%
queue_running
90.0
_create_arguments
12.0
queue_wait
12.0
CRAP 37.3
1 package Data::Model::Driver::Queue::Q4M;
2 use strict;
3 use warnings;
4 use base 'Data::Model::Driver::DBI';
5  
6 use Carp ();
7 $Carp::Internal{(__PACKAGE__)}++;
8  
9 sub timeout { $_[0]->{timeout} }
10  
11 sub _create_arguments {
12 my $arg_length = scalar(@_);
13 my $timeout;
14 my %callbacks;
15 my @queue_tables;
16 for (my $i = 0; $i < $arg_length; $i++) {
17 my($table, $value) = ($_[$i], $_[$i + 1]);
18 if (ref($value) eq 'CODE') {
19 # register callback
20 push @queue_tables, $table;
21 $callbacks{$table} = $value;
22  
23 } elsif ($table eq 'timeout' && $value =~ /\A[0-9]+\z/) {
24 # timeout
25 $timeout = $value;
26 }
27 $i++;
28 }
29 (\@queue_tables, \%callbacks, $timeout);
30 }
31  
32 sub queue_wait {
33 my($self, $timeout, @tables) = @_;
34  
35 my $dbh = $self->r_handle;
36 my $sql = sprintf 'SELECT queue_wait(%s)', join(', ', (('?') x (scalar(@tables) + 1)));
37 my $sth = $dbh->prepare_cached($sql);
38  
39 # bind params
40 my $i = 1;
41 for my $table (@tables) {
42 $sth->bind_param($i++, $table, undef);
43 }
44 $sth->bind_param($i, $timeout, undef);
45  
46 $sth->execute;
47 $sth->bind_columns(undef, \my $retcode);
48  
49 my $rv = $sth->fetch;
50 $sth->finish;
51 undef $sth;
52  
53 unless (defined $retcode) { # queue_wait return code is NULL is illegal table name
54 Carp::croak "no created queue table";
55 }
56 return 0 unless $rv && $retcode;
57 return $retcode;
58 }
59  
60 sub queue_abort {
61 my $self = shift;
62  
63 my $dbh = $self->r_handle;
64 my $sql = 'SELECT queue_abort()';
65 my $sth = $dbh->prepare($sql);
66 $sth->execute;
67 $self->{is_aborted} = 1;
68 }
69  
70 sub queue_end {
71 my $self = shift;
72  
73 my $dbh = $self->r_handle;
74 my $sql = 'SELECT queue_end()';
75 my $sth = $dbh->prepare($sql);
76 $sth->execute;
77 }
78  
79 sub queue_running {
80 my($self, $c) = (shift, shift);
81 $self->{is_aborted} = 0;
82 my $arg_length = scalar(@_);
83 Carp::croak 'illegal parameter' if $arg_length % 2;
84  
85 # create table attributes
86 my($queue_tables, $callbacks, $timeout) = _create_arguments(@_);
87 Carp::croak 'required is callback handler' unless @{ $queue_tables };
88  
89 my %schema = map { $_ => 1 } $c->schema_names;
90 for my $table (@{ $queue_tables }) {
91 my($name) = split /:/, $table;
92 Carp::croak "'$name' is missing model name" unless $schema{$name};
93 }
94  
95 $timeout ||= $self->timeout || 60;
96  
97 # queue_wait
98 my $table_id = $self->queue_wait($timeout, @{ $queue_tables });
99 return unless $table_id;
100  
101 # get record
102 my $running_table = $queue_tables->[$table_id - 1];
103 my($real_table) = split /:/, $running_table;
104 my($row) = $c->get( $real_table );
105 unless ($row) {
106 $self->queue_abort;
107 return;
108 }
109  
110 # running callback
111 eval {
112 $callbacks->{$running_table}->($row);
113 };
114 if ($@) {
115 $self->queue_abort unless $self->{is_aborted};
116 die $@; # throwing exception
117 }
118 return if $self->{is_aborted};
119  
120 $self->queue_end;
121 return $real_table;
122 }
123  
124 # for schema
125 sub _as_sql_hook {
126 my $self = shift;
127  
128 if ($_[1] eq 'get_table_attributes') {
129 my $ret = $self->dbd->_as_sql_hook(@_);
130 unless ($ret =~ s/(\A|\W)\s*ENGINE\s*=\s*\w+\s*(\z|\W)/${1}TYPE=QUEUE${2}/) {
131 $ret ||= 'ENGINE=QUEUE';
132 }
133 return $ret;
134 } else {
135 return $self->dbd->_as_sql_hook(@_);
136 }
137 }
138  
139 1;
140  
141  
142 __END__