1Mojo::Pg::Results(3)  User Contributed Perl Documentation Mojo::Pg::Results(3)
2
3
4

NAME

6       Mojo::Pg::Results - Results
7

SYNOPSIS

9         use Mojo::Pg::Results;
10
11         my $results = Mojo::Pg::Results->new(sth => $sth);
12         $results->hashes->map(sub { $_->{foo} })->shuffle->join("\n")->say;
13

DESCRIPTION

15       Mojo::Pg::Results is a container for DBD::Pg statement handles used by
16       Mojo::Pg::Database.
17

ATTRIBUTES

19       Mojo::Pg::Results implements the following attributes.
20
21   db
22         my $db   = $results->db;
23         $results = $results->db(Mojo::Pg::Database->new);
24
25       Mojo::Pg::Database object these results belong to.
26
27   sth
28         my $sth  = $results->sth;
29         $results = $results->sth($sth);
30
31       DBD::Pg statement handle results are fetched from.
32

METHODS

34       Mojo::Pg::Results inherits all methods from Mojo::Base and implements
35       the following new ones.
36
37   array
38         my $array = $results->array;
39
40       Fetch next row from "sth" and return it as an array reference. Note
41       that "finish" needs to be called if you are not fetching all the
42       possible rows.
43
44         # Process one row at a time
45         while (my $next = $results->array) {
46           say $next->[3];
47         }
48
49   arrays
50         my $collection = $results->arrays;
51
52       Fetch all rows from "sth" and return them as a Mojo::Collection object
53       containing array references.
54
55         # Process all rows at once
56         say $results->arrays->reduce(sub { $a + $b->[3] }, 0);
57
58   columns
59         my $columns = $results->columns;
60
61       Return column names as an array reference.
62
63         # Names of all columns
64         say for @{$results->columns};
65
66   expand
67         $results = $results->expand;
68
69       Decode "json" and "jsonb" fields automatically to Perl values for all
70       rows.
71
72         # Expand JSON
73         $results->expand->hashes->map(sub { $_->{foo}{bar} })->join("\n")->say;
74
75   finish
76         $results->finish;
77
78       Indicate that you are finished with "sth" and will not be fetching all
79       the remaining rows.
80
81   hash
82         my $hash = $results->hash;
83
84       Fetch next row from "sth" and return it as a hash reference. Note that
85       "finish" needs to be called if you are not fetching all the possible
86       rows.
87
88         # Process one row at a time
89         while (my $next = $results->hash) {
90           say $next->{money};
91         }
92
93   hashes
94         my $collection = $results->hashes;
95
96       Fetch all rows from "sth" and return them as a Mojo::Collection object
97       containing hash references.
98
99         # Process all rows at once
100         say $results->hashes->reduce(sub { $a + $b->{money} }, 0);
101
102   new
103         my $results = Mojo::Pg::Results->new;
104         my $results = Mojo::Pg::Results->new(sth => $sth);
105         my $results = Mojo::Pg::Results->new({sth => $sth});
106
107       Construct a new Mojo::Pg::Results object.
108
109   rows
110         my $num = $results->rows;
111
112       Number of rows.
113
114   text
115         my $text = $results->text;
116
117       Fetch all rows from "sth" and turn them into a table with "tablify" in
118       Mojo::Util.
119

SEE ALSO

121       Mojo::Pg, Mojolicious::Guides, <https://mojolicious.org>.
122
123
124
125perl v5.28.0                      2018-05-08              Mojo::Pg::Results(3)
Impressum