File Coverage

blib/lib/Mojo/SQLite/Results.pm
Criterion Covered Total %
statement 51 51 100.0
branch 9 10 90.0
condition 6 7 85.7
subroutine 18 18 100.0
pod 11 11 100.0
total 95 97 97.9


line stmt bran cond sub pod time code
1             package Mojo::SQLite::Results;
2 6     6   45 use Mojo::Base -base;
  6         15  
  6         41  
3              
4 6     6   1120 use Mojo::Collection;
  6         13  
  6         271  
5 6     6   35 use Mojo::JSON 'from_json';
  6         9  
  6         303  
6 6     6   36 use Mojo::Util 'tablify';
  6         14  
  6         6178  
7              
8             our $VERSION = '3.007';
9              
10             has [qw(db sth)];
11              
12             sub new {
13 198     198 1 1813 my $self = shift->SUPER::new(@_);
14 198   100     3788 ($self->{sth}{private_mojo_refcount} //= 0)++;
15 198         592 return $self;
16             }
17              
18             sub DESTROY {
19 198     198   75142 my $self = shift;
20 198 50       492 return() unless my $sth = $self->{sth};
21 198 100       3864 $sth->finish unless --$sth->{private_mojo_refcount};
22             }
23              
24 75     75 1 899 sub array { ($_[0]->_expand($_[0]->sth->fetchrow_arrayref))[0] }
25              
26 7     7 1 21 sub arrays { _collect($_[0]->_expand(@{$_[0]->sth->fetchall_arrayref})) }
  7         26  
27              
28 8     8 1 24 sub columns { shift->sth->{NAME} }
29              
30             sub expand {
31 18     18 1 64 my ($self, %expands) = @_;
32 18         48 for my $type (keys %expands) {
33 18 100       58 my @cols = ref $expands{$type} eq 'ARRAY' ? @{$expands{$type}} : $expands{$type};
  1         3  
34 18         96 ++$self->{expand}{$type}{$_} for @cols;
35             }
36 18         97 return $self;
37             }
38              
39 2     2 1 31 sub finish { shift->sth->finish }
40              
41 23     23 1 97 sub hash { ($_[0]->_expand($_[0]->sth->fetchrow_hashref))[0] }
42              
43 35     35 1 1572 sub hashes { _collect($_[0]->_expand(@{$_[0]->sth->fetchall_arrayref({})})) }
  35         112  
44              
45 2   50 2 1 40 sub last_insert_id { shift->{last_insert_id} // 0 }
46              
47 2     2 1 796 sub rows { shift->sth->rows }
48              
49 1     1 1 5 sub text { tablify shift->arrays }
50              
51 42     42   292 sub _collect { Mojo::Collection->new(@_) }
52              
53             sub _expand {
54 140     140   5110 my ($self, @rows) = @_;
55            
56 140 100 100     772 return @rows unless $self->{expand} and $rows[0];
57            
58 17 100       42 if (ref $rows[0] eq 'HASH') {
59 11         16 my @json_names = keys %{$self->{expand}{json}};
  11         38  
60 11         24 for my $r (@rows) { $r->{$_} = from_json $r->{$_} for grep { $r->{$_} } @json_names }
  12         86  
  13         55  
61             } else {
62 6         21 my $cols = $self->columns;
63 6         63 my @json_idxs = grep { $self->{expand}{json}{$cols->[$_]} } 0..$#$cols;
  7         34  
64 6         15 for my $r (@rows) { $r->[$_] = from_json $r->[$_] for grep { $r->[$_] } @json_idxs }
  7         72  
  7         28  
65             }
66            
67 17         1127 return @rows;
68             }
69              
70             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 is a container for L statement handles
86             used by L.
87              
88             =head1 ATTRIBUTES
89              
90             L 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 object these results belong to.
98              
99             =head2 sth
100              
101             my $sth = $results->sth;
102             $results = $results->sth($sth);
103              
104             L statement handle results are fetched from.
105              
106             =head1 METHODS
107              
108             L inherits all methods from L 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 object.
118              
119             =head2 array
120              
121             my $array = $results->array;
122              
123             Fetch next row from L and return it as an array reference. Note that
124             L 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 and return them as a L 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 text format is recognized. The names must exactly
157             match the column names as returned by L; 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 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 and return it as a hash reference. Note that
175             L 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 and return them as a L 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 of the
197             most recent successful C.
198              
199             =head2 rows
200              
201             my $num = $results->rows;
202              
203             Number of rows. Note that for C
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 and turn them into a table with
211             L.
212              
213             =head1 BUGS
214              
215             Report any issues on the public bugtracker.
216              
217             =head1 AUTHOR
218              
219             Dan Book, C
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