Q4M.pm

filter: Statementstmt 36.0% 27 / 75 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 64.2% 9 / 14 Podpod n/a totaltotal 27.6% 36 / 130 CC 16 SCAR 33.3
CC
16
Cov
25%
connect
56.0
dbh
42.0
next
30.0
CRAP 27.8
↑ Summary Q4M/Status.pm »
1 # $Id: /mirror/coderepos/lang/perl/Queue-Q4M/trunk/lib/Queue/Q4M.pm 103794 2009-04-13T11:38:30.159603Z daisuke $
2 #
3 # Copyright (c) 2008 Daisuke Maki <daisuke@endeworks.jp>
4 # All rights reserved.
5  
6 package Queue::Q4M;
7 3 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Any::Moose '::Util::TypeConstraints';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Queue::Q4M::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 class_type 'Queue::Q4M::Result';
15  
16 has 'auto_reconnect' => (
17 is => 'rw',
18 isa => 'Bool',
19 required => 1,
20 default => 1,
21 );
22  
23 has 'owner_mode' => (
24 is => 'rw',
25 isa => 'Bool',
26 default => 0
27 );
28  
29 has '_connect_pid' => (
30 is => 'rw',
31 isa => 'Int'
32 );
33  
34 has 'connect_info' => (
35 is => 'rw',
36 isa => 'ArrayRef',
37 required => 1,
38 );
39  
40 has 'sql_maker' => (
41 is => 'rw',
42 isa => 'SQL::Abstract',
43 required => 1,
44 default => sub { SQL::Abstract->new }
45 );
46  
47 has '_dbh' => (
48 is => 'rw',
49 );
50  
51 has '__table' => (
52 is => 'rw',
53 );
54  
55 has '__res' => (
56 is => 'rw',
57 # isa => 'Maybe[Queue::Q4M::Result]'
58 );
59  
60 __PACKAGE__->meta->make_immutable;
61  
62 3 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 3 ▶no Any::Moose '::Util::TypeConstraints';
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 our $AUTHORITY = 'cpan:DMAKI';
66 our $VERSION = '0.00019';
67  
68 3 ▶use constant Q4M_MINIMUM_VERSION => '0.8';
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 sub connect
71 0 ▶{
Subroutine connect: not called CC=7 SCAR=40.3
72 0 my $self = shift;
73 0 ▶ if (! ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
74 0 $self = $self->new(@_);
75 }
76  
77 0 ▶ if (my $old = $self->_dbh()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old = $self->_dbh) 0 0 0
78 0 $old->disconnect();
79 }
80  
81 0 my $dbh = $self->_connect();
82 0 $self->_dbh( $dbh );
83  
84 # Make sure we have the minimum supported API version
85 # (or, a Q4M enabled mysql, for that matter)
86 0 my $version;
87 0 eval {
88 0 my $sth = $dbh->prepare(<<' EOSQL');
89 SELECT PLUGIN_VERSION from
90 information_schema.plugins
91 WHERE plugin_name = ?
92 EOSQL
93 0 $sth->execute('QUEUE');
94 0 $sth->bind_columns(\$version);
95 0 $sth->fetchrow_arrayref;
96 0 $sth->finish;
97 };
98 0 ▶ warn if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
99  
100 0 ▶ if (! $version || $version < Q4M_MINIMUM_VERSION) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $version or $version < "0.8") 0 0 0
Condition0%0/3
not $version or $version < "0.8"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $version or $version < "0.8"
not $version $version < "0.8"
Truth table0%0/3
not(A) or B
A= not $versionB= $version < "0.8"
ABresult
1X1
011
000
101 0 ▶ Carp::confess( "Connected database does not meet the minimum required q4m version (" . Q4M_MINIMUM_VERSION . "). Got version " . (defined $version ? $version : '(undef)' ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $version ? : 0 0 0
102 }
103  
104 0 $self;
105 }
106  
107 sub _connect
108 0 ▶{
Subroutine _connect: not called CC=1 SCAR=6.9
109 0 my $self = shift;
110  
111 0 return DBI->connect(@{ $self->connect_info });
112 }
113  
114 sub dbh
115 0 ▶{
Subroutine dbh: not called CC=6 SCAR=37.4
116 0 my $self = shift;
117 0 my $dbh = $self->_dbh;
118  
119 0 my $pid = $self->_connect_pid;
120 0 ▶ if ( ($pid || '') ne $$ || ! $dbh || ! $dbh->ping) {
Branch: 1 branch
BranchTrueFalseTotal
if (($pid || "") ne $$ or not $dbh or not $dbh->ping) 0 0 0
Condition0%0/8
($pid || "") ne $$ or not $dbh or not $dbh->ping
l!l&&r!l&&!r
000
($pid || "") ne $$ or not $dbh
l!l&&r!l&&!r
000
$pid || ''
l!l
00
MC/DC0%0/3
($pid || "") ne $$ or not $dbh or not $dbh->ping
$pid not $dbh not $dbh->ping
Truth table0%0/4
A or not(B) or not(C)
A= $pidB= not $dbhC= not $dbh->ping
ABCresult
1XX1
01X1
0011
0000
121 0 ▶ $self->auto_reconnect or die "not connect";
Branch: 1 branch
BranchTrueFalseTotal
$self->auto_reconnect or die "not connect" 0 0 0
122 0 $dbh = $self->_connect();
123 0 $self->_dbh( $dbh );
124 0 $self->_connect_pid($$);
125 }
126 0 return $dbh;
127 }
128  
129 sub next
130 0 ▶{
Subroutine next: not called CC=5 SCAR=34.0
131 0 my $self = shift;
132 0 my @args = @_;
133  
134 # First, undef any cached table name that we might have had
135 0 $self->__table(undef);
136  
137 my @tables =
138 0 grep { !/^\d+$/ }
139 map {
140 0 (my $v = $_) =~ s/:.*$//;
141 0 $v
142 }
143 @args
144 ;
145  
146 # Cache this statement handler so we don't unnecessarily create
147 # string or handles
148 0 my $dbh = $self->dbh;
149 0 my $sql = sprintf(
150 "SELECT queue_wait(%s)",
151 join(',', (('?') x scalar(@args)))
152 );
153 0 my ($index) = $dbh->selectrow_array($sql, undef, @args);
154  
155 0 ▶ my $table = defined $index && $index > 0 ? $tables[$index - 1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $index && $index > 0 ? : 0 0 0
Condition0%0/3
defined $index and $index > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $index and $index > 0
defined $index $index > 0
Truth table0%0/3
A and B
A= defined $indexB= $index > 0
ABresult
0X0
100
111
156 my $res = Queue::Q4M::Result->new(
157 rv => defined $table,
158 table => $table,
159 0 ▶ on_release => sub { $self->__table(undef) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 0 );
161  
162 0 ▶ if (defined $table) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $table) 0 0 0
163 0 $self->__table($table);
164 }
165 0 ▶ $self->__res($res) if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
166 0 $self->owner_mode(1);
167 0 return $res;
168 }
169  
170 *fetch = \&fetch_array;
171  
172 BEGIN
173 {
174 foreach my $type qw(array arrayref hashref) {
175 eval sprintf( <<'EOSUB', $type, $type );
176 sub fetch_%s {
177 my $self = shift;
178 my $table = shift;
179 $table ||= $self->__table;
180 if (Scalar::Util::blessed $table &&
181 $table->isa('Queue::Q4M::Result'))
182 {
183 $table = $table->[1];
184 }
185  
186 $table or die "no table";
187  
188 my ($sql, @bind) = $self->sql_maker->select($table, @_);
189 my $dbh = $self->dbh;
190 $self->owner_mode(0);
191 return $dbh->selectrow_%s($sql, undef, @bind);
192 }
193 EOSUB
194 die if $@;
195 }
196 }
197  
198 sub insert
199 {
200 my $self = shift;
201 my $table = shift;
202  
203 my ($sql, @bind) = $self->sql_maker->insert($table, @_);
204 my $dbh = $self->dbh;
205 my $sth = $dbh->prepare($sql);
206 my $rv = $sth->execute(@bind);
207 $sth->finish;
208 return $rv;
209 }
210  
211 sub disconnect
212 {
213 my $self = shift;
214 my $dbh = $self->dbh;
215 if ($dbh) {
216 $dbh->do("select queue_end()");
217 $dbh->disconnect;
218 $self->_dbh(undef);
219 }
220 }
221  
222 sub clear
223 {
224 my ($self, $table) = @_;
225 return $self->dbh->do("DELETE FROM $table");
226 }
227  
228 sub status {
229 return Queue::Q4M::Status->fetch( shift->dbh );
230 }
231  
232 sub DEMOLISH
233 {
234 my $self = shift;
235 local $@;
236 eval {
237 $self->dbh->do("SELECT queue_abort()") if $self->owner_mode;
238 $self->disconnect;
239 };
240 }
241  
242 package
243 Queue::Q4M::Result;
244 use overload
245 bool => \&as_bool,
246 '""' => \&as_string,
247 fallback => 1
248 ;
249 use Scope::Guard;
250  
251 sub new
252 {
253 my $class = shift;
254 my %args = @_;
255 return bless [ $args{rv}, $args{table}, Scope::Guard->new( $args{on_release} ) ], $class;
256 }
257  
258 sub as_bool { $_[0]->[0] }
259 sub as_string { $_[0]->[1] }
260 sub DESTROY { $_[0]->[2]->dismiss(1) if $_[0]->[2] }
261  
262 1;
263  
264 __END__
↑ Summary Q4M/Status.pm »