Mojo/mysql/Results.pm

filter: Statementstmt 21.2% 14 / 66 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 13.7% 4 / 29 Podpod 100% 16 / 16 totaltotal 21.7% 34 / 156 CC 22 SCAR 30.9
CC
22
Cov
14%
_expand
90.0
DESTROY
20.0
expand
12.0
CRAP 21.9
« Mojo/mysql/Database.pm ↑ Summary Mojo/mysql.pm »
1 package Mojo::mysql::Results;
2 18 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 18 ▶use Mojo::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use Mojo::JSON 'from_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use Mojo::Util 'tablify';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 has [qw(db sth)];
9  
10 0 ▶sub array { ($_[0]->_expand({list => 0, type => 'array'}))[0] }
Subroutine array: not called CC=1 SCAR=6.9
Pod documented
11  
12 0 ▶sub arrays { _c($_[0]->_expand({list => 1, type => 'array'})) }
Subroutine arrays: not called CC=1 SCAR=6.9
Pod documented
13  
14 0 ▶sub columns { shift->sth->{NAME} }
Subroutine columns: not called CC=1 SCAR=6.9
Pod documented
15  
16 0 ▶sub expand { $_[0]{expand} = defined $_[1] ? 2 : 1 and return $_[0] }
Subroutine expand: not called CC=3 SCAR=24.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
$_[0]{"expand"} = defined $_[1] ? 2 : 1 and return $_[0] 0 0 0
defined $_[1] ? : 0 0 0
17  
18 0 ▶sub finish { shift->sth->finish }
Subroutine finish: not called CC=1 SCAR=6.9
Pod documented
19  
20 0 ▶sub hash { ($_[0]->_expand({list => 0, type => 'hash'}))[0] }
Subroutine hash: not called CC=1 SCAR=6.9
Pod documented
21  
22 0 ▶sub hashes { _c($_[0]->_expand({list => 1, type => 'hash'})) }
Subroutine hashes: not called CC=1 SCAR=6.9
Pod documented
23  
24 0 ▶sub rows { shift->sth->rows }
Subroutine rows: not called CC=1 SCAR=6.9
Pod documented
25  
26 0 ▶sub text { tablify shift->arrays }
Subroutine text: not called CC=1 SCAR=6.9
Pod documented
27  
28 0 ▶sub more_results { shift->sth->more_results }
Subroutine more_results: not called CC=1 SCAR=6.9
Pod documented
29  
30 0 ▶sub affected_rows { shift->{affected_rows} }
Subroutine affected_rows: not called CC=1 SCAR=6.9
Pod documented
31  
32 0 ▶sub err { shift->sth->err }
Subroutine err: not called CC=1 SCAR=6.9
Pod documented
33  
34 0 ▶sub errstr { shift->sth->errstr }
Subroutine errstr: not called CC=1 SCAR=6.9
Pod documented
35  
36 0 ▶sub last_insert_id { shift->_sth_attr('mysql_insertid') }
Subroutine last_insert_id: not called CC=1 SCAR=6.9
Pod documented
37  
38 0 ▶sub state { shift->sth->state }
Subroutine state: not called CC=1 SCAR=6.9
Pod documented
39  
40 0 ▶sub warnings_count { shift->_sth_attr('mysql_warning_count') }
Subroutine warnings_count: not called CC=1 SCAR=6.9
Pod documented
41  
42 0 ▶sub _c { Mojo::Collection->new(@_) }
Subroutine _c: not called CC=1 SCAR=6.9
43  
44 sub _expand {
45 0 ▶ my ($self, $to) = @_;
Subroutine _expand: not called CC=9 SCAR=45.0
46  
47 # Get field names and types, needs to be done before reading from sth
48 0 ▶ my $mode = $self->{expand} || 0;
Condition0%0/2
$self->{'expand'} || 0
l!l
00
MC/DC0%0/1
$self->{'expand'} || 0
$self->{'expand'}
Truth table0%0/2
A
A= $self->{'expand'}
Aresult
11
00
49 0 ▶ my ($idx, $names) = $mode == 1 ? $self->_types : ();
Branch: 1 branch
BranchTrueFalseTotal
$mode == 1 ? : 0 0 0
50  
51 # Fetch sql data
52 0 my $hash = $to->{type} eq 'hash';
53 my $sql_data
54 = $to->{list} && $hash ? $self->sth->fetchall_arrayref({})
55 0 ▶ : $to->{list} ? $self->sth->fetchall_arrayref
Branch: 3 branches
BranchTrueFalseTotal
$to->{'list'} && $hash ? : 0 0 0
$to->{'list'} ? : 0 0 0
$hash ? : 0 0 0
Condition0%0/3
$to->{"list"} and $hash
!ll&&!rl&&r
000
MC/DC0%0/2
$to->{"list"} and $hash
$to->{"list"} $hash
Truth table0%0/3
A and B
A= $to->{"list"}B= $hash
ABresult
0X0
100
111
56 : $hash ? [$self->sth->fetchrow_hashref]
57 : [$self->sth->fetchrow_arrayref];
58  
59 # Optionally expand
60 0 ▶ if ($mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode) 0 0 0
61 0 my $from_json = __PACKAGE__->can(sprintf '_from_json_mode_%s_%s', $mode, $to->{type});
62 0 $from_json->($_, $idx, $names) for @$sql_data;
63 }
64  
65 0 return @$sql_data;
66 }
67  
68 sub _from_json_mode_1_array {
69 0 ▶ my ($r, $idx, $names) = @_;
Subroutine _from_json_mode_1_array: not called CC=2 SCAR=17.9
70 0 $r->[$_] = from_json $r->[$_] for grep { defined $r->[$_] } @$idx;
71 }
72  
73 sub _from_json_mode_1_hash {
74 0 ▶ my ($r, $idx, $names) = @_;
Subroutine _from_json_mode_1_hash: not called CC=2 SCAR=17.9
75 0 $r->{$_} = from_json $r->{$_} for grep { defined $r->{$_} } @$names;
76 }
77  
78 sub _from_json_mode_2_array {
79 0 ▶ my ($r, $idx, $names) = @_;
Subroutine _from_json_mode_2_array: not called CC=3 SCAR=24.8
80 0 ▶ $_ = from_json $_ for grep defined && /^[\[{].*[}\]]$/, @$r;
Condition0%0/3
defined $_ && /^[\[{].*[}\]]$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && /^[\[{].*[}\]]$/u
defined $_ /^[\[{].*[}\]]$/u
Truth table0%0/3
A && B
A= defined $_B= /^[\[{].*[}\]]$/u
ABresult
0X0
100
111
81 }
82  
83 sub _from_json_mode_2_hash {
84 0 ▶ my ($r, $idx, $names) = @_;
Subroutine _from_json_mode_2_hash: not called CC=3 SCAR=24.8
85 0 ▶ $_ = from_json $_ for grep defined && /^[\[{].*[}\]]$/, values %$r;
Condition0%0/3
defined $_ && /^[\[{].*[}\]]$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && /^[\[{].*[}\]]$/u
defined $_ /^[\[{].*[}\]]$/u
Truth table0%0/3
A && B
A= defined $_B= /^[\[{].*[}\]]$/u
ABresult
0X0
100
111
86 }
87  
88 sub _sth_attr {
89 0 ▶ my ($self, $name) = @_;
Subroutine _sth_attr: not called CC=1 SCAR=6.9
90 0 $name =~ s!^mysql!{lc $self->db->dbh->{Driver}{Name}}!e;
91 0 return $self->sth->{$name};
92 }
93  
94 sub _types {
95 0 ▶ my $self = shift;
Subroutine _types: not called CC=3 SCAR=24.8
96 0 ▶ return @$self{qw(idx names)} if $self->{idx};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"idx"} 0 0 0
97  
98 0 my $types = $self->_sth_attr('mysql_type');
99 0 ▶ my @idx = grep { $types->[$_] == 245 or $types->[$_] == 252 } 0 .. $#$types; # 245 = MySQL, 252 = MariaDB
Branch: 1 branch
BranchTrueFalseTotal
$types->[$_] == 245 or $types->[$_] == 252 0 0 0
100  
101 0 return ($self->{idx} = \@idx, $self->{names} = [@{$self->columns}[@idx]]);
102 }
103  
104 sub DESTROY {
105 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=4 SCAR=30.0
106 0 ▶ return unless my $db = $self->{db} and my $sth = $self->{sth};
Branch: 1 branch
BranchTrueFalseTotal
unless my $db = $self->{"db"} and my $sth = $self->{"sth"} 0 0 0
Condition0%0/3
my $db = $self->{"db"} and my $sth = $self->{"sth"}
!ll&&!rl&&r
000
MC/DC0%0/2
my $db = $self->{"db"} and my $sth = $self->{"sth"}
my $db = $self->{"db"} my $sth = $self->{"sth"}
Truth table0%0/3
A and B
A= my $db = $self->{"db"}B= my $sth = $self->{"sth"}
ABresult
0X0
100
111
107 0 ▶ push @{$db->{done_sth}}, $sth unless $self->{is_blocking};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"is_blocking"} 0 0 0
108 }
109  
110 18 1;
111  
112 =encoding utf8
113  
114 =head1 NAME
115  
116 Mojo::mysql::Results - Results
117  
118 =head1 SYNOPSIS
119  
120 use Mojo::mysql::Results;
121  
122 my $results = Mojo::mysql::Results->new(db => $db, sth => $sth);
123  
124 =head1 DESCRIPTION
125  
126 L<Mojo::mysql::Results> is a container for statement handles used by
127 L<Mojo::mysql::Database>.
128  
129 =head1 ATTRIBUTES
130  
131 L<Mojo::mysql::Results> implements the following attributes.
132  
133 =head2 db
134  
135 my $db = $results->db;
136 $results = $results->db(Mojo::mysql::Database->new);
137  
138 L<Mojo::mysql::Database> object these results belong to.
139  
140 =head2 sth
141  
142 my $sth = $results->sth;
143 $results = $results->sth($sth);
144  
145 Statement handle results are fetched from.
146  
147 =head1 METHODS
148  
149 L<Mojo::mysql::Results> inherits all methods from L<Mojo::Base> and implements
150 the following new ones.
151  
152 =head2 array
153  
154 my $array = $results->array;
155  
156 Fetch next row from L</"sth"> and return it as an array reference. Note that
157 L</"finish"> needs to be called if you are not fetching all the possible rows.
158  
159 # Process one row at a time
160 while (my $next = $results->array) {
161 say $next->[3];
162 }
163  
164 =head2 arrays
165  
166 my $collection = $results->arrays;
167  
168 Fetch all rows and return them as a L<Mojo::Collection> object containing
169 array references.
170  
171 # Process all rows at once
172 say $results->arrays->reduce(sub { $a->[3] + $b->[3] });
173  
174 =head2 columns
175  
176 my $columns = $results->columns;
177  
178 Return column names as an array reference.
179  
180 =head2 expand
181  
182 $results = $results->expand;
183 $results = $results->expand(1)
184  
185 Decode C<json> fields automatically to Perl values for all rows. Passing in "1"
186 as an argument will force expanding all columns that looks like a JSON array or
187 object.
188  
189 # Expand JSON
190 $results->expand->hashes->map(sub { $_->{foo}{bar} })->join("\n")->say;
191  
192 Note that this method is EXPERIMENTAL.
193  
194 See also L<https://dev.mysql.com/doc/refman/8.0/en/json.html> for more details
195 on how to work with JSON in MySQL.
196  
197 =head2 finish
198  
199 $results->finish;
200  
201 Indicate that you are finished with L</"sth"> and will not be fetching all the
202 remaining rows.
203  
204 =head2 hash
205  
206 my $hash = $results->hash;
207  
208 Fetch next row from L</"sth"> and return it as a hash reference. Note that
209 L</"finish"> needs to be called if you are not fetching all the possible rows.
210  
211 # Process one row at a time
212 while (my $next = $results->hash) {
213 say $next->{money};
214 }
215  
216 =head2 hashes
217  
218 my $collection = $results->hashes;
219  
220 Fetch all rows and return them as a L<Mojo::Collection> object containing hash
221 references.
222  
223 # Process all rows at once
224 say $results->hashes->reduce(sub { $a->{money} + $b->{money} });
225  
226 =head2 new
227  
228 my $results = Mojo::mysql::Results->new(db => $db, sth => $sth);
229 my $results = Mojo::mysql::Results->new({db => $db, sth => $sth});
230  
231 Construct a new L<Mojo::mysql::Results> object.
232  
233 =head2 rows
234  
235 my $num = $results->rows;
236  
237 Number of rows.
238  
239 =head2 text
240  
241 my $text = $results->text;
242  
243 Fetch all rows and turn them into a table with L<Mojo::Util/"tablify">.
244  
245 =head2 more_results
246  
247 do {
248 my $columns = $results->columns;
249 my $arrays = $results->arrays;
250 } while ($results->more_results);
251  
252 Handle multiple results.
253  
254 =head2 affected_rows
255  
256 my $affected = $results->affected_rows;
257  
258 Number of affected rows by the query. The number reported is dependant from
259 C<mysql_client_found_rows> or C<mariadb_client_found_rows> option in
260 L<Mojo::mysql>. For example
261  
262 UPDATE $table SET id = 1 WHERE id = 1
263  
264 would return 1 if C<mysql_client_found_rows> or L<mariadb_client_found_rows> is
265 set, and 0 otherwise.
266  
267 =head2 last_insert_id
268  
269 my $last_id = $results->last_insert_id;
270  
271 That value of C<AUTO_INCREMENT> column if executed query was C<INSERT> in a table with
272 C<AUTO_INCREMENT> column.
273  
274 =head2 warnings_count
275  
276 my $warnings = $results->warnings_count;
277  
278 Number of warnings raised by the executed query.
279  
280 =head2 err
281  
282 my $err = $results->err;
283  
284 Error code receieved.
285  
286 =head2 state
287  
288 my $state = $results->state;
289  
290 Error state receieved.
291  
292 =head2 errstr
293  
294 my $errstr = $results->errstr;
295  
296 Error message receieved.
297  
298 =head1 SEE ALSO
299  
300 L<Mojo::mysql>.
301  
302 =cut
« Mojo/mysql/Database.pm ↑ Summary Mojo/mysql.pm »