line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojo::SQLite::Results; |
2
|
6
|
|
|
6
|
|
40
|
use Mojo::Base -base; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
35
|
|
3
|
|
|
|
|
|
|
|
4
|
6
|
|
|
6
|
|
979
|
use Mojo::Collection; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
226
|
|
5
|
6
|
|
|
6
|
|
33
|
use Mojo::JSON 'from_json'; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
293
|
|
6
|
6
|
|
|
6
|
|
42
|
use Mojo::Util 'tablify'; |
|
6
|
|
|
|
|
24
|
|
|
6
|
|
|
|
|
5466
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '3.009'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has [qw(db sth)]; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
204
|
|
|
204
|
1
|
1639
|
my $self = shift->SUPER::new(@_); |
14
|
204
|
|
100
|
|
|
3525
|
($self->{sth}{private_mojo_refcount} //= 0)++; |
15
|
204
|
|
|
|
|
585
|
return $self; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub DESTROY { |
19
|
204
|
|
|
204
|
|
36979
|
my $self = shift; |
20
|
204
|
50
|
|
|
|
454
|
return() unless my $sth = $self->{sth}; |
21
|
204
|
100
|
|
|
|
3537
|
$sth->finish unless --$sth->{private_mojo_refcount}; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
78
|
|
|
78
|
1
|
909
|
sub array { ($_[0]->_expand($_[0]->sth->fetchrow_arrayref))[0] } |
25
|
|
|
|
|
|
|
|
26
|
7
|
|
|
7
|
1
|
22
|
sub arrays { _collect($_[0]->_expand(@{$_[0]->sth->fetchall_arrayref})) } |
|
7
|
|
|
|
|
22
|
|
27
|
|
|
|
|
|
|
|
28
|
8
|
|
|
8
|
1
|
26
|
sub columns { shift->sth->{NAME} } |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub expand { |
31
|
18
|
|
|
18
|
1
|
56
|
my ($self, %expands) = @_; |
32
|
18
|
|
|
|
|
45
|
for my $type (keys %expands) { |
33
|
18
|
100
|
|
|
|
51
|
my @cols = ref $expands{$type} eq 'ARRAY' ? @{$expands{$type}} : $expands{$type}; |
|
1
|
|
|
|
|
3
|
|
34
|
18
|
|
|
|
|
70
|
++$self->{expand}{$type}{$_} for @cols; |
35
|
|
|
|
|
|
|
} |
36
|
18
|
|
|
|
|
71
|
return $self; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
2
|
|
|
2
|
1
|
15
|
sub finish { shift->sth->finish } |
40
|
|
|
|
|
|
|
|
41
|
23
|
|
|
23
|
1
|
89
|
sub hash { ($_[0]->_expand($_[0]->sth->fetchrow_hashref))[0] } |
42
|
|
|
|
|
|
|
|
43
|
35
|
|
|
35
|
1
|
1056
|
sub hashes { _collect($_[0]->_expand(@{$_[0]->sth->fetchall_arrayref({})})) } |
|
35
|
|
|
|
|
84
|
|
44
|
|
|
|
|
|
|
|
45
|
2
|
|
50
|
2
|
1
|
40
|
sub last_insert_id { shift->{last_insert_id} // 0 } |
46
|
|
|
|
|
|
|
|
47
|
2
|
|
|
2
|
1
|
815
|
sub rows { shift->sth->rows } |
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
1
|
1
|
6
|
sub text { tablify shift->arrays } |
50
|
|
|
|
|
|
|
|
51
|
42
|
|
|
42
|
|
213
|
sub _collect { Mojo::Collection->new(@_) } |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _expand { |
54
|
143
|
|
|
143
|
|
4168
|
my ($self, @rows) = @_; |
55
|
|
|
|
|
|
|
|
56
|
143
|
100
|
100
|
|
|
701
|
return @rows unless $self->{expand} and $rows[0]; |
57
|
|
|
|
|
|
|
|
58
|
17
|
100
|
|
|
|
43
|
if (ref $rows[0] eq 'HASH') { |
59
|
11
|
|
|
|
|
14
|
my @json_names = keys %{$self->{expand}{json}}; |
|
11
|
|
|
|
|
32
|
|
60
|
11
|
|
|
|
|
22
|
for my $r (@rows) { $r->{$_} = from_json $r->{$_} for grep { $r->{$_} } @json_names } |
|
12
|
|
|
|
|
91
|
|
|
13
|
|
|
|
|
52
|
|
61
|
|
|
|
|
|
|
} else { |
62
|
6
|
|
|
|
|
18
|
my $cols = $self->columns; |
63
|
6
|
|
|
|
|
54
|
my @json_idxs = grep { $self->{expand}{json}{$cols->[$_]} } 0..$#$cols; |
|
7
|
|
|
|
|
26
|
|
64
|
6
|
|
|
|
|
14
|
for my $r (@rows) { $r->[$_] = from_json $r->[$_] for grep { $r->[$_] } @json_idxs } |
|
7
|
|
|
|
|
70
|
|
|
7
|
|
|
|
|
55
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
17
|
|
|
|
|
1032
|
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"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 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"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 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"sth"> 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 |