File Coverage

blib/lib/DBIx/Async/Handle.pm
Criterion Covered Total %
statement 9 40 22.5
branch 0 6 0.0
condition 0 6 0.0
subroutine 3 14 21.4
pod 6 6 100.0
total 18 72 25.0


line stmt bran cond sub pod time code
1             package DBIx::Async::Handle;
2             $DBIx::Async::Handle::VERSION = '0.003';
3 1     1   4 use strict;
  1         1  
  1         35  
4 1     1   4 use warnings;
  1         1  
  1         30  
5              
6             =head1 NAME
7              
8             DBIx::Async::Handle - statement handle for L
9              
10             =head1 VERSION
11              
12             version 0.003
13              
14             =head1 DESCRIPTION
15              
16             Some L methods (L for example)
17             return statement handles. Those statement handles are supposed to
18             behave something like L's statement handles. Where they don't,
19             this is either a limitation of the async interface or a bug. Please
20             report if the latter.
21              
22             =cut
23              
24 1     1   3 use Variable::Disposition qw(retain_future);
  1         1  
  1         337  
25              
26             =head1 METHODS
27              
28             =cut
29              
30             =head2 new
31              
32             Returns $self.
33              
34             =cut
35              
36 0     0 1   sub new { my $class = shift; bless { @_ }, $class }
  0            
37              
38             =head2 dbh
39              
40             Returns the database handle which created this statement handle.
41              
42             =cut
43              
44 0     0 1   sub dbh { shift->{dbh} }
45              
46             =head2 execute
47              
48             Executes this statement handle, takes an optional list of bind parameters.
49              
50             Returns a L which will resolve when the statement completes.
51              
52             =cut
53              
54             sub execute {
55 0     0 1   my $self = shift;
56 0           my @param = @_;
57             $self->{execute} = $self->{prepare}->then(sub {
58 0     0     my $id = shift->{id};
59 0           $self->dbh->queue({ op => 'execute', id => $id, param => \@param });
60 0           });
61             }
62              
63             =head2 finish
64              
65             Marks this statement handle as finished.
66              
67             Returns a L which will resolve when the statement is finished.
68              
69             =cut
70              
71             sub finish {
72 0     0 1   my $self = shift;
73 0           my @param = @_;
74 0 0 0       die "execute has not yet completed?" unless $self->{execute} && $self->{execute}->is_ready;
75              
76             retain_future $self->{execute}->then(sub {
77 0     0     my $id = shift->{id};
78 0           $self->dbh->queue({ op => 'finish', id => $id });
79 0           });
80             }
81              
82             =head2 fetchrow_hashref
83              
84             Fetch a single row, returning the results as a hashref.
85              
86             Since the data won't necessarily be ready immediately, this returns
87             a L which will resolve with the requested hashref.
88              
89             =cut
90              
91             sub fetchrow_hashref {
92 0     0 1   my $self = shift;
93 0 0         die "fetch on a handle which has not been executed" unless $self->{execute};
94             retain_future $self->{execute}->then(sub {
95 0     0     my $id = shift->{id};
96 0           $self->dbh->queue({
97             op => 'fetchrow_hashref',
98             id => $id
99             });
100 0   0 0     })->transform(done => sub { shift->{data} // () })
101 0           }
102              
103             =head2 iterate
104              
105             A helper method for iterating over results.
106              
107             Takes two parameters:
108              
109             =over 4
110              
111             =item * $method - the method to call, for example L
112              
113             =item * $code - the code to run for each result
114              
115             =back
116              
117             $sth->iterate(
118             fetchrow_hashref => sub {
119            
120             }
121             )
122              
123             Returns $self.
124              
125             =cut
126              
127             sub iterate {
128 0     0 1   my $self = shift;
129 0           my $method = shift;
130 0           my $code = shift;
131 0           my $f = $self->dbh->loop->new_future;
132 0           my $step;
133             $step = sub {
134 0 0   0     return $f->done unless @_;
135 0           $self->$method->on_done($step);
136 0           $code->(@_);
137 0           };
138 0           $self->$method->on_done($step);
139 0           retain_future $f;
140             }
141              
142             1;
143              
144             __END__