| 1 | package Mojo::Pg::Results; | ||
| 2 | use Mojo::Base -base; | ||
| 3 | |||
| 4 | use Mojo::Collection; | ||
| 5 | use Mojo::JSON qw(from_json); | ||
| 6 | use Mojo::Util qw(tablify); | ||
| 7 | |||
| 8 | has [qw(db rv sth)]; | ||
| 9 | |||
| 10 | sub DESTROY { | ||
| 11 | my $self = shift; | ||
| 12 | return unless my $sth = $self->{sth}; | ||
| 13 | $self->finish unless --$sth->{private_mojo_results}; | ||
| 14 | } | ||
| 15 | |||
| 16 | sub array { ($_[0]->_expand($_[0]->sth->fetchrow_arrayref))[0] } | ||
| 17 | |||
| 18 | sub arrays { _collect($_[0]->_expand(@{$_[0]->sth->fetchall_arrayref})) } | ||
| 19 | |||
| 20 | sub columns { shift->sth->{NAME} } | ||
| 21 | |||
| 22 | sub hash { ($_[0]->_expand($_[0]->sth->fetchrow_hashref))[0] } | ||
| 23 | |||
| 24 | sub expand { ++$_[0]{expand} and return $_[0] } | ||
| 25 | |||
| 26 | sub finish { $_[0]->db->_finish_when_safe($_[0]->sth) } | ||
| 27 | |||
| 28 | sub hashes { _collect($_[0]->_expand(@{$_[0]->sth->fetchall_arrayref({})})) } | ||
| 29 | |||
| 30 | sub new { | ||
| 31 | my $self = shift->SUPER::new(@_); | ||
| 32 | ($self->{sth}{private_mojo_results} //= 0)++; | ||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub rows { shift->sth->rows } | ||
| 37 | |||
| 38 | sub text { tablify shift->arrays } | ||
| 39 | |||
| 40 | sub _collect { Mojo::Collection->new(@_) } | ||
| 41 | |||
| 42 | sub _expand { | ||
| 43 | my ($self, @rows) = @_; | ||
| 44 | |||
| 45 | return @rows unless $self->{expand} && $rows[0]; | ||
| 46 | my ($idx, $name) = @$self{qw(idx name)}; | ||
| 47 | unless ($idx) { | ||
| 48 | my $types = $self->sth->{pg_type}; | ||
| 49 | my @idx = grep { $types->[$_] eq 'json' || $types->[$_] eq 'jsonb' } 0 .. $#$types; | ||
| 50 | ($idx, $name) = @$self{qw(idx name)} = (\@idx, [@{$self->columns}[@idx]]); | ||
| 51 | } | ||
| 52 | |||
| 53 | return @rows unless @$idx; | ||
| 54 | if (ref $rows[0] eq 'HASH') { | ||
| 55 | for my $r (@rows) { $r->{$_} && ($r->{$_} = from_json $r->{$_}) for @$name } | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | for my $r (@rows) { $r->[$_] && ($r->[$_] = from_json $r->[$_]) for @$idx } | ||
| 59 | } | ||
| 60 | |||
| 61 | return @rows; | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; | ||
| 65 | |||
| 66 | =encoding utf8 | ||
| 67 | |||
| 68 | =head1 NAME | ||
| 69 | |||
| 70 | Mojo::Pg::Results - Results | ||
| 71 | |||
| 72 | =head1 SYNOPSIS | ||
| 73 | |||
| 74 | use Mojo::Pg::Results; | ||
| 75 | |||
| 76 | my $results = Mojo::Pg::Results->new(sth => $sth); | ||
| 77 | $results->hashes->map(sub { $_->{foo} })->shuffle->join("\n")->say; | ||
| 78 | |||
| 79 | =head1 DESCRIPTION | ||
| 80 | |||
| 81 | L<Mojo::Pg::Results> is a container for L<DBD::Pg> statement handles used by L<Mojo::Pg::Database>. | ||
| 82 | |||
| 83 | =head1 ATTRIBUTES | ||
| 84 | |||
| 85 | L<Mojo::Pg::Results> implements the following attributes. | ||
| 86 | |||
| 87 | =head2 db | ||
| 88 | |||
| 89 | my $db = $results->db; | ||
| 90 | $results = $results->db(Mojo::Pg::Database->new); | ||
| 91 | |||
| 92 | L<Mojo::Pg::Database> object these results belong to. | ||
| 93 | |||
| 94 | =head2 rv | ||
| 95 | |||
| 96 | my $rv = $results->rv; | ||
| 97 | $results = $results->rv($rv); | ||
| 98 | |||
| 99 | Return value from L<DBD::Pg> execute methods. | ||
| 100 | |||
| 101 | =head2 sth | ||
| 102 | |||
| 103 | my $sth = $results->sth; | ||
| 104 | $results = $results->sth($sth); | ||
| 105 | |||
| 106 | L<DBD::Pg> statement handle results are fetched from. | ||
| 107 | |||
| 108 | =head1 METHODS | ||
| 109 | |||
| 110 | L<Mojo::Pg::Results> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||
| 111 | |||
| 112 | =head2 array | ||
| 113 | |||
| 114 | my $array = $results->array; | ||
| 115 | |||
| 116 | Fetch one row from L</"sth"> and return it as an array reference. | ||
| 117 | |||
| 118 | =head2 arrays | ||
| 119 | |||
| 120 | my $collection = $results->arrays; | ||
| 121 | |||
| 122 | Fetch all rows from L</"sth"> and return them as a L<Mojo::Collection> object containing array references. | ||
| 123 | |||
| 124 | # Process all rows at once | ||
| 125 | say $results->arrays->reduce(sub { $a + $b->[3] }, 0); | ||
| 126 | |||
| 127 | =head2 columns | ||
| 128 | |||
| 129 | my $columns = $results->columns; | ||
| 130 | |||
| 131 | Return column names as an array reference. | ||
| 132 | |||
| 133 | # Names of all columns | ||
| 134 | say for @{$results->columns}; | ||
| 135 | |||
| 136 | =head2 expand | ||
| 137 | |||
| 138 | $results = $results->expand; | ||
| 139 | |||
| 140 | Decode C<json> and C<jsonb> fields automatically to Perl values for all rows. | ||
| 141 | |||
| 142 | # Expand JSON | ||
| 143 | $results->expand->hashes->map(sub { $_->{foo}{bar} })->join("\n")->say; | ||
| 144 | |||
| 145 | =head2 finish | ||
| 146 | |||
| 147 | $results->finish; | ||
| 148 | |||
| 149 | Indicate that you are finished with L</"sth"> and will not be fetching all the remaining rows. | ||
| 150 | |||
| 151 | =head2 hash | ||
| 152 | |||
| 153 | my $hash = $results->hash; | ||
| 154 | |||
| 155 | Fetch one row from L</"sth"> and return it as a hash reference. | ||
| 156 | |||
| 157 | =head2 hashes | ||
| 158 | |||
| 159 | my $collection = $results->hashes; | ||
| 160 | |||
| 161 | Fetch all rows from L</"sth"> and return them as a L<Mojo::Collection> object containing hash references. | ||
| 162 | |||
| 163 | # Process all rows at once | ||
| 164 | say $results->hashes->reduce(sub { $a + $b->{money} }, 0); | ||
| 165 | |||
| 166 | =head2 new | ||
| 167 | |||
| 168 | my $results = Mojo::Pg::Results->new; | ||
| 169 | my $results = Mojo::Pg::Results->new(sth => $sth); | ||
| 170 | my $results = Mojo::Pg::Results->new({sth => $sth}); | ||
| 171 | |||
| 172 | Construct a new L<Mojo::Pg::Results> object. | ||
| 173 | |||
| 174 | =head2 rows | ||
| 175 | |||
| 176 | my $num = $results->rows; | ||
| 177 | |||
| 178 | Number of rows. | ||
| 179 | |||
| 180 | =head2 text | ||
| 181 | |||
| 182 | my $text = $results->text; | ||
| 183 | |||
| 184 | Fetch all rows from L</"sth"> and turn them into a table with L<Mojo::Util/"tablify">. | ||
| 185 | |||
| 186 | =head1 SEE ALSO | ||
| 187 | |||
| 188 | L<Mojo::Pg>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||
| 189 | |||
| 190 | =cut |