| 1 | package Mojo::MySQL5::Results; | ||
| 2 | use Mojo::Base -base; | ||
| 3 | |||
| 4 | use Mojo::Collection; | ||
| 5 | use Mojo::Util 'tablify'; | ||
| 6 | |||
| 7 | has _columns => sub { [] }; | ||
| 8 | has _results => sub { [] }; | ||
| 9 | has _result => 0; | ||
| 10 | has _pos => 0; | ||
| 11 | |||
| 12 | sub _current_columns { $_[0]->_columns->[$_[0]->_result] } | ||
| 13 | |||
| 14 | sub _current_results { $_[0]->_results->[$_[0]->_result] // [] } | ||
| 15 | |||
| 16 | sub _hash { | ||
| 17 | my ($self, $pos) = @_; | ||
| 18 | return { | ||
| 19 | map { $self->_current_columns->[$_]->{name} => $self->_current_results->[$pos]->[$_] } | ||
| 20 | (0 .. scalar @{ $self->_current_columns } - 1) | ||
| 21 | }; | ||
| 22 | } | ||
| 23 | |||
| 24 | |||
| 25 | sub more_results { | ||
| 26 | my $self = shift; | ||
| 27 | return undef unless $self->_pos >= $self->rows; | ||
| 28 | $self->{_pos} = 0; | ||
| 29 | $self->{_result} ++; | ||
| 30 | return $self->_result < scalar @{ $self->_results }; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub array { | ||
| 34 | my $self = shift; | ||
| 35 | return undef if $self->_pos >= $self->rows; | ||
| 36 | my $array = $self->_current_results->[$self->_pos]; | ||
| 37 | $self->{_pos}++; | ||
| 38 | return $array; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub arrays { | ||
| 42 | my $self = shift; | ||
| 43 | my $arrays = Mojo::Collection->new( | ||
| 44 | map { $self->_current_results->[$_] } ($self->_pos .. $self->rows - 1) ); | ||
| 45 | $self->{_pos} = $self->rows; | ||
| 46 | return $arrays; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub columns { [ map { $_->{name} } @{ shift->_current_columns } ] } | ||
| 50 | |||
| 51 | sub hash { | ||
| 52 | my $self = shift; | ||
| 53 | return undef if $self->_pos >= $self->rows; | ||
| 54 | my $hash = $self->_hash($self->_pos); | ||
| 55 | $self->{_pos}++; | ||
| 56 | return $hash; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub hashes { | ||
| 60 | my $self = shift; | ||
| 61 | my $hashes = Mojo::Collection->new( | ||
| 62 | map { $self->_hash($_) } ($self->_pos .. $self->rows - 1) ); | ||
| 63 | $self->{_pos} = $self->rows; | ||
| 64 | return $hashes; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub rows { scalar @{ shift->_current_results } } | ||
| 68 | |||
| 69 | sub affected_rows { shift->{affected_rows} } | ||
| 70 | |||
| 71 | sub warnings_count { shift->{warnings_count} } | ||
| 72 | |||
| 73 | sub last_insert_id { shift->{last_insert_id} } | ||
| 74 | |||
| 75 | sub err { shift->{error_code} } | ||
| 76 | |||
| 77 | sub errstr { shift->{error_message} } | ||
| 78 | |||
| 79 | sub state { shift->{sql_state} // '' } | ||
| 80 | |||
| 81 | sub text { tablify shift->arrays } | ||
| 82 | |||
| 83 | 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 |