Poggy/DBI.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Poggy.pm ↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package DBIx::Poggy::DBI;
5 1 ▶use base 'DBI';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 DBIx::Poggy::DBI - DBI subclass
10  
11 =head2 DESCRIPTION
12  
13 Overrides several methods in L<DBI>. All queries are marked as async. See list of
14 supported methods below:
15  
16 =cut
17  
18 package DBIx::Poggy::DBI::db;
19 1 ▶use base 'DBI::db';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use DBD::Pg qw(:async);
Subroutine BEGIN: called CC=1 SCAR=2.6
23 use Promises qw(collect deferred);
24 use Scalar::Util qw(weaken blessed);
25 use Guard qw(guard);
26  
27 sub connected {
28 my $self= shift;
29 $self->{private_poggy_state} = {active => 0, queue => []};
30 return;
31 }
32  
33 =head2 METHODS
34  
35 =head3 supported
36  
37 These are supported: L<DBI/selectrow_array>, L<DBI/selectrow_arrayref>, L<DBI/selectrow_hashref>,
38 L<DBI/selectall_arrayref>, L<DBI/selectall_hashref> and L<DBI/do>.
39  
40 For example:
41  
42 $pool->take->selectrow_array(
43 "SELECT * FROM test LIMIT 1",
44 )->then(sub {
45 my @row = @_;
46 ...
47 });
48  
49 See L</Transactions> to learn about L</begin_work>, L</commit> and L</rollback>.
50  
51 =head3 not supported
52  
53 These are not supported, but will be when I need them or somebody will write a patch:
54 L<DBI/selectcol_arrayref>
55  
56 You don't use C<prepare>, C<bind*>, C<execute> or C<fetch*>. I have some ideas of making
57 these work, but don't think there is urgent need to pursue.
58  
59 =cut
60  
61 my %map = (
62 selectrow_array => ['fetchrow_array'],
63 selectrow_arrayref => ['fetchrow_arrayref'],
64 selectrow_hashref => ['fetchrow_hashref'],
65 selectall_arrayref => ['fetchall_arrayref', sub {
66 my $in = shift;
67 my ($query, $attrs) = splice @$in, 0, 2;
68 my @fetch_args;
69 @fetch_args = delete @{$attrs}{'Slice', 'MaxRows'} if $attrs;
70 return (\@fetch_args, $query, $attrs, $in);
71 } ],
72 selectall_hashref => ['fetchall_hashref', sub {
73 my $in = shift;
74 my ($query, $key_field, $attrs) = splice @$in, 0, 3;
75 my @fetch_args = $key_field;
76 return (\@fetch_args, $query, $attrs, $in);
77 } ],
78 do => [''],
79 );
80 while ( my ($method, $fetch_method) = each %map ) {
81 no strict 'refs';
82 *{$method} = sub {
83 my $self = shift;
84  
85 my $d = deferred;
86  
87 my @args = $fetch_method->[1]?
88 ($d, $fetch_method->[0], $fetch_method->[1]->(\@_))
89 : ($d, $fetch_method->[0], [], shift, shift, \@_)
90 ;
91  
92 my $state = $self->{private_poggy_state};
93 if ( $state->{active} ) {
94 push @{$state->{queue}}, \@args;
95 return $d->promise;
96 }
97 $self->_do_async( @args );
98 return $d->promise;
99 }
100 }
101  
102 sub _do_async {
103 my $self = shift;
104 my ($d, $fetch_method, $fetch_args, $query, $args, $binds) = @_;
105  
106 my $sth;
107  
108 my $done = sub {
109 my $method = shift;
110 my @res = @_;
111 if ( $method eq 'reject' ) {
112 unshift @res, $self->errobj;
113 }
114 if ( $sth ) {
115 $sth->finish;
116 $sth = undef;
117 }
118  
119 $d->$method( @res );
120  
121 return;
122 };
123  
124 $sth = $self->prepare($query, $args)
125 or return $done->( 'reject' );
126 $sth->execute( @$binds )
127 or return $done->( 'reject' );
128  
129 my $guard;
130 my $watcher = sub {
131 return unless $self->pg_ready;
132  
133 $guard = undef;
134 my $res = $self->pg_result or return $done->( 'reject' );
135 return $done->(resolve => $res) unless $fetch_method;
136 return $done->(resolve => $sth->$fetch_method( @$fetch_args ) );
137 };
138 $guard = AnyEvent->io( fh => $self->{pg_socket}, poll => 'r', cb => $watcher );
139 return;
140 }
141  
142 sub prepare {
143 my $self = shift;
144 my $args = ($_[1]||={});
145 $args->{pg_async} ||= 0;
146 $args->{pg_async} |= PG_ASYNC;
147  
148 my $sth = $self->SUPER::prepare( @_ );
149 return $sth unless $sth;
150  
151 my $state = $self->{private_poggy_state};
152  
153 $state->{active}++;
154  
155 my $wself = $self;
156 weaken $wself;
157 $sth->{private_poggy_guard} = guard {
158 --$state->{active};
159 unless ( @{ $state->{queue} } ) {
160 $state->{release_to}->release($wself)
161 if $wself && $state->{release_to} && !$state->{txn};
162 return;
163 }
164 unless ($wself) {
165 warn "still have pending sql queries, but dbh has gone away";
166 return;
167 }
168 $wself->_do_async( @{ shift @{$state->{queue}} } );
169 };
170 return $sth;
171 }
172  
173 =head3 Transactions
174  
175 This module wraps L</begin_work>, L</commit> and L</rollback> methods to
176 help handle transactions.
177  
178 B<NOTE> that behaviour is not yet defined when commiting or rolling back
179 a transaction with active query. I just havn't decided what to do in this
180 case. Now it's your job to make sure commit/rollback happens after all
181 queries on the handle.
182  
183 =head4 begin_work
184  
185 Returns a Promise that will be resolved once transaction is committed or
186 rejected on rollback or failed attempt to start the transaction.
187  
188 =cut
189  
190 sub begin_work {
191 my $self = shift;
192 my $d = deferred;
193 $self->SUPER::begin_work(@_)
194 or return $d->reject( $self->errobj )->promise;
195 $self->{private_poggy_state}{txn} = $d;
196 if ( my $pool = $self->{private_poggy_state}{release_to} ) {
197 $d->finally(sub { $pool->release( $self ) });
198 }
199 return $d->promise;
200 }
201  
202 =head4 commit
203  
204 Takes resolution value of the transaction, commits and resolves the promise returned
205 by L</begin_work>.
206  
207 =cut
208  
209 sub commit {
210 my $self = shift;
211 my $d = delete $self->{private_poggy_state}{txn} or die "No transaction in progress";
212 my $rv = $self->SUPER::commit();
213 unless ( $rv ) {
214 $d->reject($self->errobj);
215 return $rv;
216 }
217 $d->resolve(@_);
218 return $rv;
219 }
220  
221 =head4 rollback
222  
223 Takes rollback value of the transaction, commits and rejects the promise returned
224 by L</begin_work>.
225  
226 =cut
227  
228 sub rollback {
229 my $self = shift;
230 my $d = delete $self->{private_poggy_state}{txn} or die "No transaction in progress";
231 my $rv = $self->SUPER::rollback();
232 unless ( $rv ) {
233 $d->reject($self->errobj);
234 return $rv;
235 }
236 $d->reject(@_);
237 return $rv;
238 }
239  
240 sub errobj {
241 my $self = shift;
242 return DBIx::Poggy::Error->new( $self );
243 }
244  
245 package DBIx::Poggy::DBI::st;
246 use base 'DBI::st';
247  
248 1;
« Poggy.pm ↑ Summary