Async/Query.pm

filter: Statementstmt 24.4% 32 / 131 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 23.8% 10 / 42 Podpod 31.8% 7 / 22 totaltotal 17.8% 49 / 275 CC 24 SCAR 18.5
CC
24
Cov
17%
__ANON__
20.0
from
20.0
in
12.0
CRAP 6.4
« Async/Backoff/Exponential.pm ↑ Summary Async/ORM/Schema.pm »
1 package Database::Async::Query;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '0.019'; # VERSION
7  
8 =head1 NAME
9  
10 Database::Async::Query - represents a single database query
11  
12 =head1 SYNOPSIS
13  
14 my $query = Database::Async::Query->new(
15 db => Database::Async->new(...),
16 );
17  
18 =head1 DESCRIPTION
19  
20 A query:
21  
22 =over 4
23  
24 =item * has zero or more parameters
25  
26 =item * accepts zero or more input rows
27  
28 =item * returns zero or more output rows
29  
30 =item * can be prepared or direct
31  
32 =back
33  
34 =head2 Creating queries
35  
36 Queries are initiated from the L<Database::Async> instance; users
37 are not expected to instantiate L<Database::Async::Query> objects
38 directly.
39  
40 my $query = $db->query('select 1');
41  
42 =head2 Passing parameters
43  
44 Normally additional parameters for L<placeholders|http://bobby-tables.com/>
45 are passed when creating the query instance:
46  
47 my $query_with_parameters = $db->query('select name from users where id = ?', $id);
48  
49 For prepared statements, query parameters can be bound and passed for each
50 execution, see L</Prepared queries> for more details.
51  
52 =head2 Retrieving data
53  
54 Methods prefixed with C<row_> each provide a L<Ryu::Source> which
55 emits events for each row returned from the query:
56  
57 $db->query(q{select * from a_table})
58 ->row_hashrefs
59 ->each(sub {
60 say "Had ID $_->{id} with name $_->{name}"
61 })->await;
62  
63 =head2 Direct queries
64  
65 A direct query is of the form:
66  
67 $db->query(q{select 1})
68  
69 or
70  
71 $db->query(q{select * from some_table where id = ?}, $id)
72  
73 and has all the information required to start the query.
74  
75 =head2 Prepared queries
76  
77 When the same query needs to be executed multiple times with different
78 parameters, it may be worth using a prepared query. This involves sending
79 the SQL for the query to the server so that it can parse the text and
80 prepare a plan. Once complete, you can then send a set of parameters
81 and have the server execute and return any results.
82  
83 A prepared query involves two steps.
84  
85 First, the query is created:
86  
87 my $query = $db->query(q{select * from some_table where id = ?});
88  
89 Next, it will need to be prepared. This will fail if the query was
90 provided any parameters when initially constructed:
91  
92 $query->prepare(
93 statement => 'xyz'
94 );
95  
96 Some engines allow a C<statement> parameter, others will ignore it.
97  
98 After a call to L</prepare>, the query is marked as prepared and will
99 support the L</bind> and L</execute> methods. Once the query is prepared,
100 it is traditional to bind some variables to it:
101  
102 $query->bind(
103 $var1, $var2, ...
104 );
105  
106 after which it can be executed:
107  
108 $query->execute(
109 portal => 'abc'
110 );
111  
112 and any results can be extracted by the usual methods such as L</row_hashrefs>.
113  
114 Again, some engines support named portals, others will ignore the parameter.
115  
116 Since passing parameters is so common, you can combine the L</bind> and
117 L</execute> steps by passing an arrayref to L</execute>:
118  
119 $query->execute([ $first_value, ... ], portal => '');
120  
121 Streaming of values via L<Ryu::Source> is also supported:
122  
123 $query->execute($src, portal => '');
124  
125 Note that prepared queries will continue to emit values from the C<< row_* >>
126 source(s) until the query itself is discarded. The caller is expected to
127 keep track of any required mapping from input parameters to output rows.
128  
129 A full example might look something like this:
130  
131 async sub name_by_user_id {
132 my ($self, $id) = @_;
133 my $q = await $self->{name_by_user_id} //= do {
134 # Prepare the query on first call
135 my $q = $self->db->query(q{select name from "site"."user" where id = ?});
136 $q->prepare(
137 statement => 'name_by_user_id'
138 )
139 };
140 my ($name) = await $q->execute([ $id ])->single;
141 return $name;
142 }
143  
144 =head2 Custom engine features
145  
146 Different engines support additional features or events.
147  
148 Once a query is scheduled onto an engine, it will resolve the L</engine> L<Future>
149 instance:
150  
151 my $query = $db->query('select * from some_table');
152 my $engine = await $query->engine;
153 $engine->source('notification')
154 ->map('payload')
155 ->say;
156  
157 =head2 Cancelling queries
158  
159 In cases where you want to terminate a query early, use the L</cancel> method.
160 This will ask the engine to stop query execution if already scheduled. For a query
161 which has not yet been assigned to an engine, the L</cancel> method will cancel
162 the schedule request.
163  
164 =head2 Cursors
165  
166 Cursors are handled as normal SQL queries.
167  
168 $db->txn(async sub {
169 my ($txn) = @_;
170 await $txn->query(q{declare c cursor for select id from xyz})->void;
171 say while await $txn->query(q{fetch next from c})->single;
172 await $txn->query(q{close c})->void;
173 });
174  
175 =cut
176  
177 5 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
178  
179 5 ▶use Database::Async::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
180  
181 5 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 5 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 5 ▶use Ryu::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 5 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
185  
186 5 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 use overload
189 0 ▶ '""' => sub { my ($self) = @_; sprintf '%s[%s]', ref($self), $self->sql },
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 ▶ bool => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
191 5 fallback => 1;
192  
193 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
194 0 my ($class, %args) = @_;
195 0 Scalar::Util::weaken($args{db});
196 0 bless \%args, $class;
197 }
198  
199 =head2 in
200  
201 This is a L<Ryu::Sink> used for queries which stream data to the server.
202  
203 It's buffered internally.
204  
205 =cut
206  
207 0 ▶sub in {
Subroutine in: not called CC=3 SCAR=24.8
Pod documented
208 0 my ($self) = @_;
209 0 ▶ $self->{in} //= do {
Condition0%0/3
$self->{"in"} //= do { my $sink = $self->db->new_sink; die "already have streaming input but no original ->{in} sink" if $self->{"streaming_input"}; $sink->source->completed->on_ready(sub { $Database::Async::Query::log->debugf("Sink for %s completed with %s", $self, (shift())->state); } ); $self->{"streaming_input"} = $sink->source->buffer->pause; $self->ready_to_stream->on_done(sub { $Database::Async::Query::log->debugf("Ready to stream, resuming streaming input"); $self->streaming_input->resume; } ); $sink }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"in"} //= do { my $sink = $self->db->new_sink; die "already have streaming input but no original ->{in} sink" if $self->{"streaming_input"}; $sink->source->completed->on_ready(sub { $Database::Async::Query::log->debugf("Sink for %s completed with %s", $self, (shift())->state); } ); $self->{"streaming_input"} = $sink->source->buffer->pause; $self->ready_to_stream->on_done(sub { $Database::Async::Query::log->debugf("Ready to stream, resuming streaming input"); $self->streaming_input->resume; } ); $sink }
$self->{"in"} do { my $sink = $self->db->new_sink; die "already have streaming input but no original ->{in} sink" if $self->{"streaming_input"}; $sink->source->completed->on_ready(sub { $Database::Async::Query::log->debugf("Sink for %s completed with %s", $self, (shift())->state); } ); $self->{"streaming_input"} = $sink->source->buffer->pause; $self->ready_to_stream->on_done(sub { $Database::Async::Query::log->debugf("Ready to stream, resuming streaming input"); $self->streaming_input->resume; } ); $sink }
Truth table0%0/3
A //= B
A= $self->{"in"}B= do { my $sink = $self->db->new_sink; die "already have streaming input but no original ->{in} sink" if $self->{"streaming_input"}; $sink->source->completed->on_ready(sub { $Database::Async::Query::log->debugf("Sink for %s completed with %s", $self, (shift())->state); } ); $self->{"streaming_input"} = $sink->source->buffer->pause; $self->ready_to_stream->on_done(sub { $Database::Async::Query::log->debugf("Ready to stream, resuming streaming input"); $self->streaming_input->resume; } ); $sink }
ABresult
1X1
011
000
210 0 my $sink = $self->db->new_sink;
211 0 ▶ die 'already have streaming input but no original ->{in} sink' if $self->{streaming_input};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"streaming_input"} 0 0 0
212 0 ▶ $sink->source->completed->on_ready(sub { $log->debugf('Sink for %s completed with %s', $self, shift->state) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
213 0 $self->{streaming_input} = $sink->source->buffer->pause;
214 0 ▶ $self->ready_to_stream->on_done(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 0 $log->debugf('Ready to stream, resuming streaming input');
216 0 $self->streaming_input->resume;
217 0 });
218 0 $sink
219 }
220 }
221  
222 #sub {
223 # my ($self) = @_;
224 # my $engine = $self->{engine} or die 'needs a valid ::Engine instance';
225 # my $sink = $self->in or die 'had no valid sink for streaming input';
226 #
227 # my $src = $sink->buffer;
228 # $src->pause;
229 # $engine->stream_from($src);
230 #
231 # $self->ready_to_stream
232 # ->on_done(sub {
233 # $log->tracef('Ready to stream for %s', $sink);
234 # $src->resume;
235 # })->on_fail(sub {
236 # $src->completed->fail(@_) unless $sink->completed->is_ready;
237 # })->on_cancel(sub {
238 # $src->completed->cancel unless $sink->completed->is_ready;
239 # });
240 #}
241  
242 0 ▶sub streaming_input { shift->{streaming_input} // die '->in has not yet been called' }
Subroutine streaming_input: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
+(shift())->{"streaming_input"} // die("->in has not yet been called")
l!l
00
MC/DC0%0/1
+(shift())->{"streaming_input"} // die("->in has not yet been called")
+(shift())->{"streaming_input"}
Truth table0%0/2
A
A= +(shift())->{"streaming_input"}
Aresult
11
00
243  
244 0 ▶sub finish {
Subroutine finish: not called CC=2 SCAR=17.9
Pod undocumented
245 0 my ($self) = @_;
246 0 ▶ if($self->{in}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'in'}) { } 0 0 0
247 0 $self->input_stream->done;
248 } else {
249 0 $self->input_stream->cancel;
250 }
251 }
252  
253 =head2 db
254  
255 Accessor for the L<Database::Async> instance.
256  
257 =cut
258  
259 0 ▶sub db { shift->{db} }
Subroutine db: not called CC=1 SCAR=6.9
Pod documented
260  
261 =head2 sql
262  
263 The SQL string that this query would be running.
264  
265 =cut
266  
267 0 ▶sub sql { shift->{sql} }
Subroutine sql: not called CC=1 SCAR=6.9
Pod documented
268  
269 =head2 bind
270  
271 A list of bind parameters for this query, can be empty.
272  
273 =cut
274  
275 0 ▶sub bind { @{shift->{bind}} }
Subroutine bind: not called CC=1 SCAR=6.9
Pod documented
276  
277 0 ▶sub row_description {
Subroutine row_description: not called CC=1 SCAR=6.9
Pod undocumented
278 0 my ($self, $desc) = @_;
279 0 $log->tracef('Have row description %s', $desc);
280 0 my @names = map { $_->{name} } $desc->@*;
281 0 $self->{field_names} = \@names;
282 $self->{field_by_name} = {
283 # First column wins by default if we have multiple hits
284 0 map { $names[$_] => $_ } reverse 0..$#names
285 };
286 0 $self
287 }
288  
289 0 ▶sub row {
Subroutine row: not called CC=1 SCAR=6.9
Pod undocumented
290 0 my ($self, $row) = @_;
291 0 $log->tracef('Have row %s', $row);
292 0 $self->row_data->emit($row);
293 }
294  
295 0 ▶sub row_hashrefs {
Subroutine row_hashrefs: not called CC=2 SCAR=17.9
Pod undocumented
296 0 my ($self) = @_;
297 $self->{row_hashrefs} //= $self->row_data
298 0 ▶ ->map(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
299 0 my ($row) = @_;
300 +{
301 0 map {;
302 0 $self->{field_names}[$_] => $row->[$_]
303 } 0..$#$row
304 }
305 0 ▶ });
Condition0%0/3
$self->{"row_hashrefs"} //= $self->row_data->map(sub { my($row) = @_; +{map({$self->{"field_names"}[$_], $row->[$_];} 0 .. $row->$#*)}; } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"row_hashrefs"} //= $self->row_data->map(sub { my($row) = @_; +{map({$self->{"field_names"}[$_], $row->[$_];} 0 .. $row->$#*)}; } )
$self->{"row_hashrefs"} $self->row_data->map(sub { my($row) = @_; +{map({$self->{"field_names"}[$_], $row->[$_];} 0 .. $row->$#*)}; } )
Truth table0%0/3
A //= B
A= $self->{"row_hashrefs"}B= $self->row_data->map(sub { my($row) = @_; +{map({$self->{"field_names"}[$_], $row->[$_];} 0 .. $row->$#*)}; } )
ABresult
1X1
011
000
306 }
307  
308 0 ▶sub row_arrayrefs {
Subroutine row_arrayrefs: not called CC=2 SCAR=17.9
Pod undocumented
309 0 my ($self) = @_;
310 0 ▶ $self->{row_arrayrefs} //= $self->row_data;
Condition0%0/3
$self->{"row_arrayrefs"} //= $self->row_data
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"row_arrayrefs"} //= $self->row_data
$self->{"row_arrayrefs"} $self->row_data
Truth table0%0/3
A //= B
A= $self->{"row_arrayrefs"}B= $self->row_data
ABresult
1X1
011
000
311 }
312  
313 =head2 start
314  
315 Schedules this query for execution.
316  
317 =cut
318  
319 0 ▶sub start {
Subroutine start: not called CC=2 SCAR=17.9
Pod documented
320 0 my ($self) = @_;
321 0 ▶ $self->{queued} //= $self->db->queue_query($self);
Condition0%0/3
$self->{"queued"} //= $self->db->queue_query($self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"queued"} //= $self->db->queue_query($self)
$self->{"queued"} $self->db->queue_query($self)
Truth table0%0/3
A //= B
A= $self->{"queued"}B= $self->db->queue_query($self)
ABresult
1X1
011
000
322 }
323  
324 0 ▶sub run_on {
Subroutine run_on: not called CC=1 SCAR=6.9
Pod undocumented
325 0 my ($self, $engine) = @_;
326 0 $log->tracef('Running query %s on %s', $self, $engine);
327 0 $self->{engine} = $engine;
328 0 $engine->query(
329 $self->sql,
330 $self->bind
331 );
332 }
333  
334 =head2 rows
335  
336 Returns a L<Ryu::Source> which will emit the rows from this query.
337  
338 Each row is a L<Database::Async::Row> instance.
339  
340 Will call L</start> if required.
341  
342 =cut
343  
344 0 ▶sub row_data {
Subroutine row_data: not called CC=2 SCAR=17.9
Pod undocumented
345 0 my ($self) = @_;
346 0 ▶ $self->{row_data} //= do {
Condition0%0/3
$self->{"row_data"} //= do { my $row_data = $self->db->new_source; $self->completed->on_ready(sub { my($f) = @_; return if $row_data->is_ready; return $row_data->finish if $f->is_done; return if $f->is_cancelled; return $row_data->fail($f->failure); } ); $row_data->completed->on_ready(sub { my $f = $self->completed; (shift())->on_ready($f) unless $f->is_ready; } ); $self->start; $row_data }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"row_data"} //= do { my $row_data = $self->db->new_source; $self->completed->on_ready(sub { my($f) = @_; return if $row_data->is_ready; return $row_data->finish if $f->is_done; return if $f->is_cancelled; return $row_data->fail($f->failure); } ); $row_data->completed->on_ready(sub { my $f = $self->completed; (shift())->on_ready($f) unless $f->is_ready; } ); $self->start; $row_data }
$self->{"row_data"} do { my $row_data = $self->db->new_source; $self->completed->on_ready(sub { my($f) = @_; return if $row_data->is_ready; return $row_data->finish if $f->is_done; return if $f->is_cancelled; return $row_data->fail($f->failure); } ); $row_data->completed->on_ready(sub { my $f = $self->completed; (shift())->on_ready($f) unless $f->is_ready; } ); $self->start; $row_data }
Truth table0%0/3
A //= B
A= $self->{"row_data"}B= do { my $row_data = $self->db->new_source; $self->completed->on_ready(sub { my($f) = @_; return if $row_data->is_ready; return $row_data->finish if $f->is_done; return if $f->is_cancelled; return $row_data->fail($f->failure); } ); $row_data->completed->on_ready(sub { my $f = $self->completed; (shift())->on_ready($f) unless $f->is_ready; } ); $self->start; $row_data }
ABresult
1X1
011
000
347 0 my $row_data = $self->db->new_source;
348 0 ▶ $self->completed->on_ready(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
349 0 my ($f) = @_;
350 0 ▶ return if $row_data->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $row_data->is_ready 0 0 0
351 0 ▶ return $row_data->finish if $f->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_done 0 0 0
352 0 ▶ return if $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_cancelled 0 0 0
353 0 return $row_data->fail($f->failure);
354 0 });
355 0 ▶ $row_data->completed->on_ready(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
356 0 my $f = $self->completed;
357 0 ▶ shift->on_ready($f) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
358 0 });
359 0 $self->start;
360 0 $row_data;
361 };
362 }
363  
364 0 ▶sub completed {
Subroutine completed: not called CC=2 SCAR=17.9
Pod undocumented
365 0 my ($self) = @_;
366 0 ▶ $self->{completed} //= do {
Condition0%0/3
$self->{"completed"} //= do { my $f = $self->db->new_future; $self->start->on_fail(sub { $f->fail(@_) unless $f->is_ready; } ); $f }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"completed"} //= do { my $f = $self->db->new_future; $self->start->on_fail(sub { $f->fail(@_) unless $f->is_ready; } ); $f }
$self->{"completed"} do { my $f = $self->db->new_future; $self->start->on_fail(sub { $f->fail(@_) unless $f->is_ready; } ); $f }
Truth table0%0/3
A //= B
A= $self->{"completed"}B= do { my $f = $self->db->new_future; $self->start->on_fail(sub { $f->fail(@_) unless $f->is_ready; } ); $f }
ABresult
1X1
011
000
367 0 my $f = $self->db->new_future;
368 0 ▶ $self->start->on_fail(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
369 0 ▶ $f->fail(@_) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
370 0 });
371 0 $f
372 }
373 }
374  
375 0 ▶sub void {
Subroutine void: not called CC=1 SCAR=6.9
Pod undocumented
376 0 my ($self) = @_;
377 0 $self->start;
378 0 return $self->completed;
379 }
380  
381 0 ▶sub ready_to_stream {
Subroutine ready_to_stream: not called CC=2 SCAR=17.9
Pod undocumented
382 0 my ($self) = @_;
383 0 ▶ $self->{ready_to_stream} //= $self->db->new_future;
Condition0%0/3
$self->{"ready_to_stream"} //= $self->db->new_future
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ready_to_stream"} //= $self->db->new_future
$self->{"ready_to_stream"} $self->db->new_future
Truth table0%0/3
A //= B
A= $self->{"ready_to_stream"}B= $self->db->new_future
ABresult
1X1
011
000
384 }
385  
386 0 ▶sub input_stream {
Subroutine input_stream: not called CC=2 SCAR=17.9
Pod undocumented
387 0 my ($self) = @_;
388 0 ▶ $self->{input_stream} //= $self->db->new_future;
Condition0%0/3
$self->{"input_stream"} //= $self->db->new_future
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"input_stream"} //= $self->db->new_future
$self->{"input_stream"} $self->db->new_future
Truth table0%0/3
A //= B
A= $self->{"input_stream"}B= $self->db->new_future
ABresult
1X1
011
000
389 }
390  
391 0 ▶sub done {
Subroutine done: not called CC=2 SCAR=17.9
Pod undocumented
392 0 my ($self) = @_;
393 0 my $f = $self->completed;
394 0 ▶ if($f->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_ready) 0 0 0
395 0 $log->warnf('Calling ->done but we think our status is already %s', $f->state);
396 0 return $f;
397 }
398 # $self->in->completed->done unless $self->in->completed->is_ready;
399 0 $self->completed->done;
400 }
401  
402 0 ▶sub from {
Subroutine from: not called CC=4 SCAR=30.0
Pod undocumented
403 0 my ($self, $src) = @_;
404 0 ▶ if(ref($src) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $src eq "ARRAY") 0 0 0
405 0 $src = Ryu::Source->from($src);
406 }
407 0 ▶ die 'Invalid source' unless blessed($src) and $src->isa('Ryu::Source');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($src) and $src->isa("Ryu::Source") 0 0 0
Condition0%0/3
builtin::blessed($src) and $src->isa("Ryu::Source")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($src) and $src->isa("Ryu::Source")
builtin::blessed($src) $src->isa("Ryu::Source")
Truth table0%0/3
A and B
A= builtin::blessed($src)B= $src->isa("Ryu::Source")
ABresult
0X0
100
111
408  
409 0 $self->in->from($src);
410 0 $src->prepare_await;
411 0 $self;
412 }
413  
414 0 ▶sub rows {
Subroutine rows: not called CC=2 SCAR=17.9
Pod documented
415 0 my ($self) = @_;
416 $self->{rows} //= $self->row_data
417 0 ▶ ->map(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
418 0 my ($row) = @_;
419 Database::Async::Row->new(
420 0 index_by_name => +{ map { $row->[$_]->{description}->name => $_ } 0..$#$row },
421 data => $row
422 )
423 })
424 0 ▶}
Condition0%0/3
$self->{"rows"} //= $self->row_data->map(sub { my($row) = @_; "Database::Async::Row"->new("index_by_name", {map({$row->[$_]{"description"}->name, $_;} 0 .. $row->$#*)}, "data", $row); } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"rows"} //= $self->row_data->map(sub { my($row) = @_; "Database::Async::Row"->new("index_by_name", {map({$row->[$_]{"description"}->name, $_;} 0 .. $row->$#*)}, "data", $row); } )
$self->{"rows"} $self->row_data->map(sub { my($row) = @_; "Database::Async::Row"->new("index_by_name", {map({$row->[$_]{"description"}->name, $_;} 0 .. $row->$#*)}, "data", $row); } )
Truth table0%0/3
A //= B
A= $self->{"rows"}B= $self->row_data->map(sub { my($row) = @_; "Database::Async::Row"->new("index_by_name", {map({$row->[$_]{"description"}->name, $_;} 0 .. $row->$#*)}, "data", $row); } )
ABresult
1X1
011
000
425  
426 =head2 single
427  
428 Used to retrieve data for a query that's always going to return a single row.
429  
430 Defaults to all columns, provide a list of indices to select a subset:
431  
432 # should yield "a", "b" and "c" as the three results
433 print for await $db->query(q{select 'a', 'b', 'c'})->single->as_list;
434  
435 # should yield just the ID column from the first row
436 print for await $db->query(q{select id, * from some_table})->single('id')->as_list;
437  
438 Returns a L<Future> which will resolve to the list of items.
439  
440 =cut
441  
442 0 ▶sub single {
Subroutine single: not called CC=2 SCAR=17.9
Pod documented
443 0 my ($self, @id) = @_;
444 $self->{single} //= $self->row_data
445 ->first
446 0 ▶ ->flat_map(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
447 0 ▶ [ @id ? @{$_}{@id} : @$_ ]
Branch: 1 branch
BranchTrueFalseTotal
@id ? : 0 0 0
448 0 ▶ })->as_list;
Condition0%0/3
$self->{"single"} //= $self->row_data->first->flat_map(sub { [@id ? @{$_;}{@id} : @$_]; } )->as_list
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"single"} //= $self->row_data->first->flat_map(sub { [@id ? @{$_;}{@id} : @$_]; } )->as_list
$self->{"single"} $self->row_data->first->flat_map(sub { [@id ? @{$_;}{@id} : @$_]; } )->as_list
Truth table0%0/3
A //= B
A= $self->{"single"}B= $self->row_data->first->flat_map(sub { [@id ? @{$_;}{@id} : @$_]; } )->as_list
ABresult
1X1
011
000
449 }
450  
451 5 1;
452  
453 __END__
« Async/Backoff/Exponential.pm ↑ Summary Async/ORM/Schema.pm »