| 1 | package Mojo::MySQL5::Results; | ||||||||||||
| 2 | 7 | ▶ | use Mojo::Base -base; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | 7 | ▶ | use Mojo::Collection; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 7 | ▶ | use Mojo::Util 'tablify'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 0 | ▶ | has _columns => sub { [] }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 8 | 0 | ▶ | has _results => sub { [] }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 9 | 7 | has _result => 0; | |||||||||||
| 10 | 7 | has _pos => 0; | |||||||||||
| 11 | |||||||||||||
| 12 | 0 | ▶ | sub _current_columns { $_[0]->_columns->[$_[0]->_result] } | ||||||||||
|
Subroutine
_current_columns: not called CC=1 SCAR=6.9
| |||||||||||||
| 13 | |||||||||||||
| 14 | 0 | ▶ | sub _current_results { $_[0]->_results->[$_[0]->_result] // [] } | ||||||||||
|
Subroutine
_current_results: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0]->_results->[$_[0]->_result] // []
MC/DC0%0/1
$_[0]->_results->[$_[0]->_result] // []
$_[0]->_results->[$_[0]->_result]
Truth table0%0/2
A
A= $_[0]->_results->[$_[0]->_result]
| |||||||||||||
| 15 | |||||||||||||
| 16 | 0 | ▶ | sub _hash { | ||||||||||
|
Subroutine
_hash: not called CC=1 SCAR=6.9
| |||||||||||||
| 17 | 0 | my ($self, $pos) = @_; | |||||||||||
| 18 | return { | ||||||||||||
| 19 | 0 | map { $self->_current_columns->[$_]->{name} => $self->_current_results->[$pos]->[$_] } | |||||||||||
| 20 | 0 | (0 .. scalar @{ $self->_current_columns } - 1) | |||||||||||
| 21 | }; | ||||||||||||
| 22 | } | ||||||||||||
| 23 | |||||||||||||
| 24 | |||||||||||||
| 25 | 0 | ▶ | sub more_results { | ||||||||||
|
Subroutine
more_results: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 26 | 0 | my $self = shift; | |||||||||||
| 27 | 0 | ▶ | return undef unless $self->_pos >= $self->rows; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 0 | $self->{_pos} = 0; | |||||||||||
| 29 | 0 | $self->{_result} ++; | |||||||||||
| 30 | 0 | return $self->_result < scalar @{ $self->_results }; | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | 0 | ▶ | sub array { | ||||||||||
|
Subroutine
array: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 34 | 0 | my $self = shift; | |||||||||||
| 35 | 0 | ▶ | return undef if $self->_pos >= $self->rows; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 0 | my $array = $self->_current_results->[$self->_pos]; | |||||||||||
| 37 | 0 | $self->{_pos}++; | |||||||||||
| 38 | 0 | return $array; | |||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | 0 | ▶ | sub arrays { | ||||||||||
|
Subroutine
arrays: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 42 | 0 | my $self = shift; | |||||||||||
| 43 | my $arrays = Mojo::Collection->new( | ||||||||||||
| 44 | 0 | map { $self->_current_results->[$_] } ($self->_pos .. $self->rows - 1) ); | |||||||||||
| 45 | 0 | $self->{_pos} = $self->rows; | |||||||||||
| 46 | 0 | return $arrays; | |||||||||||
| 47 | } | ||||||||||||
| 48 | |||||||||||||
| 49 | 0 | ▶ | sub columns { [ map { $_->{name} } @{ shift->_current_columns } ] } | ||||||||||
|
Subroutine
columns: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 50 | |||||||||||||
| 51 | 0 | ▶ | sub hash { | ||||||||||
|
Subroutine
hash: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 52 | 0 | my $self = shift; | |||||||||||
| 53 | 0 | ▶ | return undef if $self->_pos >= $self->rows; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 54 | 0 | my $hash = $self->_hash($self->_pos); | |||||||||||
| 55 | 0 | $self->{_pos}++; | |||||||||||
| 56 | 0 | return $hash; | |||||||||||
| 57 | } | ||||||||||||
| 58 | |||||||||||||
| 59 | 0 | ▶ | sub hashes { | ||||||||||
|
Subroutine
hashes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 60 | 0 | my $self = shift; | |||||||||||
| 61 | my $hashes = Mojo::Collection->new( | ||||||||||||
| 62 | 0 | map { $self->_hash($_) } ($self->_pos .. $self->rows - 1) ); | |||||||||||
| 63 | 0 | $self->{_pos} = $self->rows; | |||||||||||
| 64 | 0 | return $hashes; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 0 | ▶ | sub rows { scalar @{ shift->_current_results } } | ||||||||||
|
Subroutine
rows: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | sub affected_rows { shift->{affected_rows} } | ||||||||||
|
Subroutine
affected_rows: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 70 | |||||||||||||
| 71 | 0 | ▶ | sub warnings_count { shift->{warnings_count} } | ||||||||||
|
Subroutine
warnings_count: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 72 | |||||||||||||
| 73 | 0 | ▶ | sub last_insert_id { shift->{last_insert_id} } | ||||||||||
|
Subroutine
last_insert_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 74 | |||||||||||||
| 75 | 0 | ▶ | sub err { shift->{error_code} } | ||||||||||
|
Subroutine
err: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 76 | |||||||||||||
| 77 | 0 | ▶ | sub errstr { shift->{error_message} } | ||||||||||
|
Subroutine
errstr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 78 | |||||||||||||
| 79 | 0 | ▶ | sub state { shift->{sql_state} // '' } | ||||||||||
|
Subroutine
state: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
+(shift())->{"sql_state"} // ""
MC/DC0%0/1
+(shift())->{"sql_state"} // ""
+(shift())->{"sql_state"}
Truth table0%0/2
A
A= +(shift())->{"sql_state"}
| |||||||||||||
| 80 | |||||||||||||
| 81 | 0 | ▶ | sub text { tablify shift->arrays } | ||||||||||
|
Subroutine
text: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 82 | |||||||||||||
| 83 | 7 | 1; | |||||||||||
| 84 | |||||||||||||
| 85 | =encoding utf8 | ||||||||||||
| 86 | |||||||||||||
| 87 | =head1 NAME | ||||||||||||
| 88 | |||||||||||||
| 89 | Mojo::MySQL5::Results - Results | ||||||||||||
| 90 | |||||||||||||
| 91 | =head1 SYNOPSIS | ||||||||||||
| 92 | |||||||||||||
| 93 | use Mojo::MySQL5::Results; | ||||||||||||
| 94 | |||||||||||||
| 95 | my $results = Mojo::MySQL5::Results->new(db => $db); | ||||||||||||
| 96 | |||||||||||||
| 97 | =head1 DESCRIPTION | ||||||||||||
| 98 | |||||||||||||
| 99 | L<Mojo::MySQL5::Results> is a container for results returned by call to | ||||||||||||
| 100 | $db->L<Mojo::MySQL5::Database/"query">. | ||||||||||||
| 101 | |||||||||||||
| 102 | =head1 METHODS | ||||||||||||
| 103 | |||||||||||||
| 104 | L<Mojo::MySQL5::Results> inherits all methods from L<Mojo::Base> and implements | ||||||||||||
| 105 | the following ones. | ||||||||||||
| 106 | |||||||||||||
| 107 | =head2 array | ||||||||||||
| 108 | |||||||||||||
| 109 | my $array = $results->array; | ||||||||||||
| 110 | |||||||||||||
| 111 | Fetch one row and return it as an array reference. | ||||||||||||
| 112 | |||||||||||||
| 113 | # Process one row at a time | ||||||||||||
| 114 | while (my $next = $results->array) { | ||||||||||||
| 115 | say $next->[3]; | ||||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | =head2 arrays | ||||||||||||
| 119 | |||||||||||||
| 120 | my $collection = $results->arrays; | ||||||||||||
| 121 | |||||||||||||
| 122 | Fetch all rows and return them as a L<Mojo::Collection> object containing | ||||||||||||
| 123 | array references. | ||||||||||||
| 124 | |||||||||||||
| 125 | # Process all rows at once | ||||||||||||
| 126 | say $results->arrays->reduce(sub { $a->[3] + $b->[3] }); | ||||||||||||
| 127 | |||||||||||||
| 128 | =head2 columns | ||||||||||||
| 129 | |||||||||||||
| 130 | my $columns = $results->columns; | ||||||||||||
| 131 | |||||||||||||
| 132 | Return column names as an array reference. | ||||||||||||
| 133 | |||||||||||||
| 134 | =head2 hash | ||||||||||||
| 135 | |||||||||||||
| 136 | my $hash = $results->hash; | ||||||||||||
| 137 | |||||||||||||
| 138 | Fetch one row and return it as a hash reference. | ||||||||||||
| 139 | |||||||||||||
| 140 | # Process one row at a time | ||||||||||||
| 141 | while (my $next = $results->hash) { | ||||||||||||
| 142 | say $next->{money}; | ||||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | =head2 hashes | ||||||||||||
| 146 | |||||||||||||
| 147 | my $collection = $results->hashes; | ||||||||||||
| 148 | |||||||||||||
| 149 | Fetch all rows and return them as a L<Mojo::Collection> object containing hash | ||||||||||||
| 150 | references. | ||||||||||||
| 151 | |||||||||||||
| 152 | # Process all rows at once | ||||||||||||
| 153 | say $results->hashes->reduce(sub { $a->{money} + $b->{money} }); | ||||||||||||
| 154 | |||||||||||||
| 155 | =head2 rows | ||||||||||||
| 156 | |||||||||||||
| 157 | my $num = $results->rows; | ||||||||||||
| 158 | |||||||||||||
| 159 | Number of rows. | ||||||||||||
| 160 | |||||||||||||
| 161 | =head2 text | ||||||||||||
| 162 | |||||||||||||
| 163 | my $text = $results->text; | ||||||||||||
| 164 | |||||||||||||
| 165 | Fetch all rows and turn them into a table with L<Mojo::Util/"tablify">. | ||||||||||||
| 166 | |||||||||||||
| 167 | =head2 more_results | ||||||||||||
| 168 | |||||||||||||
| 169 | do { | ||||||||||||
| 170 | my $columns = $results->columns; | ||||||||||||
| 171 | my $arrays = $results->arrays; | ||||||||||||
| 172 | } while ($results->more_results); | ||||||||||||
| 173 | |||||||||||||
| 174 | Handle multiple results. | ||||||||||||
| 175 | |||||||||||||
| 176 | =head2 affected_rows | ||||||||||||
| 177 | |||||||||||||
| 178 | my $affected = $results->affected_rows; | ||||||||||||
| 179 | |||||||||||||
| 180 | Number of affected rows by the query. | ||||||||||||
| 181 | The number reported is dependant from C<found_rows> option in L<Mojo::MySQL5>. | ||||||||||||
| 182 | For example | ||||||||||||
| 183 | |||||||||||||
| 184 | UPDATE $table SET id = 1 WHERE id = 1 | ||||||||||||
| 185 | |||||||||||||
| 186 | would return 1 if C<found_rows> is set, and 0 otherwise. | ||||||||||||
| 187 | |||||||||||||
| 188 | =head2 last_insert_id | ||||||||||||
| 189 | |||||||||||||
| 190 | my $last_id = $results->last_insert_id; | ||||||||||||
| 191 | |||||||||||||
| 192 | That value of C<AUTO_INCREMENT> column if executed query was C<INSERT> in a table with | ||||||||||||
| 193 | C<AUTO_INCREMENT> column. | ||||||||||||
| 194 | |||||||||||||
| 195 | =head2 warnings_count | ||||||||||||
| 196 | |||||||||||||
| 197 | my $warnings = $results->warnings_count; | ||||||||||||
| 198 | |||||||||||||
| 199 | Number of warnings raised by the executed query. | ||||||||||||
| 200 | |||||||||||||
| 201 | =head2 err | ||||||||||||
| 202 | |||||||||||||
| 203 | my $err = $results->err; | ||||||||||||
| 204 | |||||||||||||
| 205 | Error code receieved. | ||||||||||||
| 206 | |||||||||||||
| 207 | =head2 state | ||||||||||||
| 208 | |||||||||||||
| 209 | my $state = $results->state; | ||||||||||||
| 210 | |||||||||||||
| 211 | Error state receieved. | ||||||||||||
| 212 | |||||||||||||
| 213 | =head2 errstr | ||||||||||||
| 214 | |||||||||||||
| 215 | my $errstr = $results->errstr; | ||||||||||||
| 216 | |||||||||||||
| 217 | Error message receieved. | ||||||||||||
| 218 | |||||||||||||
| 219 | =head1 SEE ALSO | ||||||||||||
| 220 | |||||||||||||
| 221 | L<Mojo::MySQL5>, L<Mojo::MySQL5::Database>. | ||||||||||||
| 222 | |||||||||||||
| 223 | =cut | ||||||||||||