blib/lib/Queue/Q4Pg/Lite.pm

filter: Statementstmt 26.1% 33 / 126 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.0% 8 / 20 Podpod 88.8% 8 / 9 totaltotal 26.0% 49 / 188 CC 16 SCAR 25.4
CC
16
Cov
21%
connect
30.0
next
30.0
dbh
20.0
CRAP 12.7
↑ Summary
1 package Queue::Q4Pg::Lite;
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 2 our $VERSION = '0.03';
6  
7 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 has 'auto_reconnect' => (
13 is => 'rw',
14 isa => 'Bool',
15 required => 1,
16 default => 1,
17 );
18  
19 2 has 'connect_info' => (
20 is => 'rw',
21 isa => 'ArrayRef',
22 required => 1,
23 );
24  
25 has 'sql_maker' => (
26 is => 'rw',
27 isa => 'SQL::Abstract',
28 required => 1,
29 0 ▶ default => sub { SQL::Abstract->new }
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 2 );
31  
32 2 has '_dbh' => (
33 is => 'rw',
34 );
35  
36 2 has '_res' => (
37 is => 'rw',
38 );
39  
40 2 has 'interval' => (
41 is => 'rw',
42 isa => 'Int',
43 default => 5,
44 );
45  
46 2 ▶use constant PG_ADVISORY_LOCK_SUPPORT_VERSION => 80200;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 0 ▶sub connect {
Subroutine connect: not called CC=5 SCAR=34.0
Pod documented
51 0 my $self = shift;
52 0 ▶ if (! ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 0 0
53 0 $self = $self->new(@_);
54 }
55  
56 0 ▶ if (my $old = $self->_dbh()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old = $self->_dbh) 0 0 0
57 0 $old->disconnect();
58 }
59  
60 0 my $dbh = $self->_connect();
61 0 $self->_dbh( $dbh );
62  
63 0 my $version = $dbh->{pg_server_version};
64 0 ▶ if ( $version < PG_ADVISORY_LOCK_SUPPORT_VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version < 80200) 0 0 0
65 0 ▶ Carp::confess( "Connected database does not support pg_advisory_lock(). required PostgreSQL version (" . PG_ADVISORY_LOCK_SUPPORT_VERSION . "). Got version " . (defined $version ? $version : '(undef)' ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $version ? : 0 0 0
66 }
67 0 $self;
68 }
69  
70 0 ▶sub _connect {
Subroutine _connect: not called CC=1 SCAR=6.9
71 0 my $self = shift;
72  
73 0 return DBI->connect(@{ $self->connect_info });
74 }
75  
76 0 ▶sub dbh {
Subroutine dbh: not called CC=4 SCAR=30.0
Pod documented
77 0 my $self = shift;
78 0 my $dbh = $self->_dbh;
79  
80 0 ▶ if ( ! $dbh || ! $dbh->ping ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh and $dbh->ping) 0 0 0
Condition0%0/3
$dbh and $dbh->ping
!ll&&!rl&&r
000
MC/DC0%0/2
$dbh and $dbh->ping
$dbh $dbh->ping
Truth table0%0/3
A and B
A= $dbhB= $dbh->ping
ABresult
0X0
100
111
81 0 ▶ $self->auto_reconnect or die "not connect";
Branch: 1 branch
BranchTrueFalseTotal
$self->auto_reconnect or die "not connect" 0 0 0
82 0 $dbh = $self->_connect();
83 0 $self->_dbh( $dbh );
84 }
85 0 return $dbh;
86 }
87  
88 0 ▶sub next {
Subroutine next: not called CC=5 SCAR=34.0
Pod documented
89 0 my $self = shift;
90 0 my $table = shift;
91 0 my ( $where ) = @_;
92  
93 0 ▶ if ( my $pre = $self->_res ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pre = $self->_res) 0 0 0
94 0 Carp::carp( 'abort not finished job. id='. $pre->{id} );
95 0 $self->abort;
96 }
97 0 my $dbh = $self->dbh;
98 0 my $sql = "SELECT * FROM $table";
99 0 my ($sql_where, @bind) = $self->sql_maker->where($where);
100 0 ▶ if ( $sql_where eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sql_where eq '') { } 0 0 0
101 0 $sql .= " WHERE pg_try_advisory_lock(tableoid::int, id)";
102 }
103 else {
104 0 (my $cond = $sql_where) =~ s/^\s+WHERE\s//i;
105 0 $sql .= " WHERE CASE WHEN $cond THEN pg_try_advisory_lock(tableoid::int, id) ELSE false END";
106 }
107 0 $sql .= " LIMIT 1";
108  
109 0 my $sth = $dbh->prepare($sql);
110  
111 0 ▶ while ( $sth->execute(@bind) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->execute(@bind)) 0 0 0
112 0 my $res = $sth->fetchrow_hashref;
113 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
114 0 $res->{_table} = $table;
115 0 $self->_res($res);
116 0 return $res;
117 }
118 0 sleep $self->interval;
119 }
120 0 return;
121 }
122  
123 2 *fetch = \&fetch_hashref;
124  
125 0 ▶sub fetch_hashref {
Subroutine fetch_hashref: not called CC=1 SCAR=6.9
Pod documented
126 0 my $self = shift;
127 0 return $self->_res;
128 }
129  
130 0 ▶sub abort {
Subroutine abort: not called CC=2 SCAR=17.9
Pod undocumented
131 0 my $self = shift;
132  
133 0 ▶ return unless $self->_res;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_res 0 0 0
134  
135 0 my $dbh = $self->_dbh;
136 0 my $res = $self->_res;
137 my $sql
138 = "SELECT pg_advisory_unlock(tableoid::int, ?) FROM "
139 0 . $res->{_table};
140 0 my $sth = $dbh->prepare($sql);
141 0 $sth->execute( $res->{id} );
142 0 my $r = $sth->fetchrow_arrayref;
143 0 $sth->finish;
144 0 return $r->[0];
145 }
146  
147 0 ▶sub ack {
Subroutine ack: not called CC=2 SCAR=17.9
Pod documented
148 0 my $self = shift;
149  
150 0 ▶ return unless $self->_res;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_res 0 0 0
151  
152 0 my $dbh = $self->_dbh;
153 0 my $res = $self->_res;
154  
155 my ($sql, @bind) = $self->sql_maker->delete(
156 $res->{_table},
157 { id => $res->{id} },
158 0 );
159 0 $sql .= " RETURNING pg_advisory_unlock(tableoid::int, id)";
160 0 my $sth = $dbh->prepare($sql);
161 0 $sth->execute(@bind);
162 0 my $r = $sth->fetchrow_arrayref;
163 0 $sth->finish;
164  
165 0 $self->_res(undef);
166 0 return $r->[0];
167 }
168  
169 0 ▶sub insert {
Subroutine insert: not called CC=1 SCAR=6.9
Pod documented
170 0 my $self = shift;
171 0 my $table = shift;
172  
173 0 my ($sql, @bind) = $self->sql_maker->insert($table, @_);
174 0 my $dbh = $self->dbh;
175 0 my $sth = $dbh->prepare($sql);
176 0 my $rv = $sth->execute(@bind);
177 0 $sth->finish;
178 0 return $rv;
179 }
180  
181 0 ▶sub disconnect {
Subroutine disconnect: not called CC=2 SCAR=17.9
Pod documented
182 0 my $self = shift;
183 0 ▶ $self->_dbh->disconnect if $self->_dbh;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dbh 0 0 0
184 0 $self->_dbh(undef);
185 0 $self->_res(undef);
186 0 1;
187 }
188  
189 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
190 0 my $self = shift;
191 0 my $table = shift;
192 0 my ($sql, @bind) = $self->sql_maker->delete(
193 $table,
194 { "pg_try_advisory_lock(tableoid::int, id)" => \"" },
195 );
196 0 $sql .= " RETURNING pg_advisory_unlock(tableoid::int, id)";
197 0 my $sth = $self->dbh->prepare($sql);
198 0 my $rows = $sth->execute();
199 0 $sth->finish();
200 0 return $rows;
201 }
202  
203 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
204 0 my $self = shift;
205 0 ▶ $self->abort if $self->_res;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_res 0 0 0
206 0 $self->disconnect;
207 }
208  
209 2 1;
210 __END__
↑ Summary