SQLite/Results.pm

filter: Statementstmt 100% 54 / 54 Branchbran 90.0% 9 / 10 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 18 / 18 Podpod 100% 11 / 11 totaltotal 97.1% 101 / 104 CC 15 SCAR 12.4
CC
15
Cov
97%
_expand
8.0
expand
4.0
DESTROY
3.0
CRAP 3.4
« SQLite/Database.pm ↑ Summary SQLite/Transaction.pm »
1 package Mojo::SQLite::Results;
2 6 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Mojo::JSON 'from_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Mojo::Util 'tablify';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = 'v4.0.0';
9  
10 6 has [qw(db sth)];
11  
12 204 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
13 204 my $self = shift->SUPER::new(@_);
14 204 ▶ ($self->{sth}{private_mojo_refcount} //= 0)++;
Condition100%2/2
$self->{"sth"}{"private_mojo_refcount"} //= 0
l!l
12678
MC/DC100%1/1
$self->{"sth"}{"private_mojo_refcount"} //= 0
$self->{"sth"}{"private_mojo_refcount"}
Truth table100%2/2
A
A= $self->{"sth"}{"private_mojo_refcount"}
Aresult
11
00
15 204 return $self;
16 }
17  
18 204 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.1
19 204 my $self = shift;
20 204 ▶ return() unless my $sth = $self->{sth};
Branch: 1 branch
BranchTrueFalseTotal
unless my $sth = $self->{"sth"} 0 204 204
21 204 ▶ $sth->finish unless --$sth->{private_mojo_refcount};
Branch: 1 branch
BranchTrueFalseTotal
unless --$sth->{"private_mojo_refcount"} 200 4 204
22 }
23  
24 78 ▶sub array { ($_[0]->_expand($_[0]->sth->fetchrow_arrayref))[0] }
Subroutine array: called CC=1 SCAR=0.0
Pod documented
25  
26 7 ▶sub arrays { _collect($_[0]->_expand(@{$_[0]->sth->fetchall_arrayref})) }
Subroutine arrays: called CC=1 SCAR=0.0
Pod documented
27  
28 8 ▶sub columns { shift->sth->{NAME} }
Subroutine columns: called CC=1 SCAR=0.0
Pod documented
29  
30 18 ▶sub expand {
Subroutine expand: called CC=4 SCAR=13.9
Pod documented
31 18 my ($self, %expands) = @_;
32 18 for my $type (keys %expands) {
33 18 ▶ my @cols = ref $expands{$type} eq 'ARRAY' ? @{$expands{$type}} : $expands{$type};
Branch: 1 branch
BranchTrueFalseTotal
ref $expands{$type} eq 'ARRAY' ? : 1 17 18
34 18 ++$self->{expand}{$type}{$_} for @cols;
35 }
36 18 return $self;
37 }
38  
39 2 ▶sub finish { shift->sth->finish }
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
40  
41 23 ▶sub hash { ($_[0]->_expand($_[0]->sth->fetchrow_hashref))[0] }
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
42  
43 35 ▶sub hashes { _collect($_[0]->_expand(@{$_[0]->sth->fetchall_arrayref({})})) }
Subroutine hashes: called CC=1 SCAR=0.0
Pod documented
44  
45 2 ▶sub last_insert_id { shift->{last_insert_id} // 0 }
Subroutine last_insert_id: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
+(shift())->{"last_insert_id"} // 0
l!l
20
MC/DC0%0/1
+(shift())->{"last_insert_id"} // 0
+(shift())->{"last_insert_id"}
Truth table50%1/2
A
A= +(shift())->{"last_insert_id"}
Aresult
11
00
46  
47 2 ▶sub rows { shift->sth->rows }
Subroutine rows: called CC=1 SCAR=0.0
Pod documented
48  
49 1 ▶sub text { tablify shift->arrays }
Subroutine text: called CC=1 SCAR=0.0
Pod documented
50  
51 42 ▶sub _collect { Mojo::Collection->new(@_) }
Subroutine _collect: called CC=1 SCAR=0.0
52  
53 143 ▶sub _expand {
Subroutine _expand: called CC=8 SCAR=20.8
54 143 my ($self, @rows) = @_;
55
56 143 ▶ return @rows unless $self->{expand} and $rows[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"expand"} and $rows[0] 126 17 143
Condition100%3/3
$self->{"expand"} and $rows[0]
!ll&&!rl&&r
125117
MC/DC100%2/2
$self->{"expand"} and $rows[0]
$self->{"expand"} $rows[0]
Truth table100%3/3
A and B
A= $self->{"expand"}B= $rows[0]
ABresult
0X0
100
111
57
58 17 ▶ if (ref $rows[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rows[0] eq 'HASH') { } 11 6 17
59 11 my @json_names = keys %{$self->{expand}{json}};
60 11 for my $r (@rows) { $r->{$_} = from_json $r->{$_} for grep { $r->{$_} } @json_names }
61 } else {
62 6 my $cols = $self->columns;
63 6 my @json_idxs = grep { $self->{expand}{json}{$cols->[$_]} } 0..$#$cols;
64 6 for my $r (@rows) { $r->[$_] = from_json $r->[$_] for grep { $r->[$_] } @json_idxs }
65 }
66
67 17 return @rows;
68 }
69  
70 6 1;
71  
72 =head1 NAME
73  
74 Mojo::SQLite::Results - Results
75  
76 =head1 SYNOPSIS
77  
78 use Mojo::SQLite::Results;
79  
80 my $results = Mojo::SQLite::Results->new(sth => $sth);
81 $results->hashes->map(sub { $_->{foo} })->shuffle->join("\n")->say;
82  
83 =head1 DESCRIPTION
84  
85 L<Mojo::SQLite::Results> is a container for L<DBD::SQLite> statement handles
86 used by L<Mojo::SQLite::Database>.
87  
88 =head1 ATTRIBUTES
89  
90 L<Mojo::SQLite::Results> implements the following attributes.
91  
92 =head2 db
93  
94 my $db = $results->db;
95 $results = $results->db(Mojo::SQLite::Database->new);
96  
97 L<Mojo::SQLite::Database> object these results belong to.
98  
99 =head2 sth
100  
101 my $sth = $results->sth;
102 $results = $results->sth($sth);
103  
104 L<DBD::SQLite> statement handle results are fetched from.
105  
106 =head1 METHODS
107  
108 L<Mojo::SQLite::Results> inherits all methods from L<Mojo::Base> and implements
109 the following new ones.
110  
111 =head2 new
112  
113 my $results = Mojo::SQLite::Results->new;
114 my $results = Mojo::SQLite::Results->new(sth => $sth);
115 my $results = Mojo::SQLite::Results->new({sth => $sth});
116  
117 Construct a new L<Mojo::SQLite::Results> object.
118  
119 =head2 array
120  
121 my $array = $results->array;
122  
123 Fetch next row from L</"sth"> and return it as an array reference. Note that
124 L</"finish"> needs to be called if you are not fetching all the possible rows.
125  
126 # Process one row at a time
127 while (my $next = $results->array) {
128 say $next->[3];
129 }
130  
131 =head2 arrays
132  
133 my $collection = $results->arrays;
134  
135 Fetch all rows from L</"sth"> and return them as a L<Mojo::Collection> object
136 containing array references.
137  
138 # Process all rows at once
139 say $results->arrays->reduce(sub { $a + $b->[3] }, 0);
140  
141 =head2 columns
142  
143 my $columns = $results->columns;
144  
145 Return column names as an array reference.
146  
147 # Names of all columns
148 say for @{$results->columns};
149  
150 =head2 expand
151  
152 $results = $results->expand(json => 'some_json');
153 $results = $results->expand(json => ['some_json','other_json']);
154  
155 Decode specified fields from a particular format to Perl values for all rows.
156 Currently only the C<json> text format is recognized. The names must exactly
157 match the column names as returned by L</"columns">; it is recommended to use
158 explicit aliases in the query for consistent column names.
159  
160 # Expand JSON
161 $results->expand(json => 'json_field')->hashes->map(sub { $_->{foo}{bar} })->join("\n")->say;
162  
163 =head2 finish
164  
165 $results->finish;
166  
167 Indicate that you are finished with L</"sth"> and will not be fetching all the
168 remaining rows.
169  
170 =head2 hash
171  
172 my $hash = $results->hash;
173  
174 Fetch next row from L</"sth"> and return it as a hash reference. Note that
175 L</"finish"> needs to be called if you are not fetching all the possible rows.
176  
177 # Process one row at a time
178 while (my $next = $results->hash) {
179 say $next->{money};
180 }
181  
182 =head2 hashes
183  
184 my $collection = $results->hashes;
185  
186 Fetch all rows from L</"sth"> and return them as a L<Mojo::Collection> object
187 containing hash references.
188  
189 # Process all rows at once
190 say $results->hashes->reduce(sub { $a + $b->{money} }, 0);
191  
192 =head2 last_insert_id
193  
194 my $id = $results->last_insert_id;
195  
196 Returns the L<rowid|https://www.sqlite.org/c3ref/last_insert_rowid.html> of the
197 most recent successful C<INSERT>.
198  
199 =head2 rows
200  
201 my $num = $results->rows;
202  
203 Number of rows. Note that for C<SELECT> statements, this count will not be
204 accurate until all rows have been fetched.
205  
206 =head2 text
207  
208 my $text = $results->text;
209  
210 Fetch all rows from L</"sth"> and turn them into a table with
211 L<Mojo::Util/"tablify">.
212  
213 =head1 BUGS
214  
215 Report any issues on the public bugtracker.
216  
217 =head1 AUTHOR
218  
219 Dan Book, C<dbook@cpan.org>
220  
221 =head1 COPYRIGHT AND LICENSE
222  
223 Copyright 2015, Dan Book.
224  
225 This library is free software; you may redistribute it and/or modify it under
226 the terms of the Artistic License version 2.0.
227  
228 =head1 SEE ALSO
229  
230 L<Mojo::SQLite>
« SQLite/Database.pm ↑ Summary SQLite/Transaction.pm »