-
Notifications
You must be signed in to change notification settings - Fork 3
/
Results.pm6
166 lines (113 loc) · 3.26 KB
/
Results.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use DB::Pg::Native;
class DB::Pg::ArrayIterator does Iterator
{
has $.res;
has Bool $.finish;
has Bool $.hash;
has Int $.rows;
has Int $!row = 0;
method pull-one
{
if $!row == $!rows
{
$!res.finish if $!finish;
return IterationEnd
}
$!res.row($!row++, :$!hash)
}
}
class DB::Pg::Results
{
has Bool $.finish = False;
has $.sth handles <columns types>;
has PGresult $.result;
method DESTROY { self.clear }
method clear
{
.clear with $!result;
$!result = PGresult;
}
method finish
{
self.clear;
$!sth.finish
}
method rows { $!result.tuples }
method row(Int $row, Bool :$hash)
{
return () unless 0 ≤ $row < self.rows;
my @row = do for ^$!sth.columns.elems Z $!sth.types -> [$col, $type]
{
$!result.getisnull($row, $col)
?? $type
!! $!sth.db.converter.convert($type,
$!result.getvalue($row, $col))
}
$hash ?? %($!sth.columns Z=> @row) !! @row
}
method value
{
LEAVE self.finish if $!finish;
self.row(0)[0]
}
method array
{
LEAVE self.finish if $!finish;
self.row(0)
}
method hash
{
LEAVE self.finish if $!finish;
self.row(0, :hash) or Nil
}
method arrays
{
Seq.new: DB::Pg::ArrayIterator.new(res => self, :$!finish, :!hash,
rows => self.rows)
}
method hashes
{
Seq.new: DB::Pg::ArrayIterator.new(res => self, :$!finish, :hash,
rows => self.rows)
}
}
=begin pod
=head1 NAME
DB::Pg::Results -- Results from a PostgreSQL query
=head1 SYNOPSIS
my $results = $sth.execute(1);
say $results.rows; # Number of rows returned
say $results.columns; # Array of column (field) names
say $results.types; # Array of column Perl types
say $results.value; # A single scalar value
say $results.array; # A single array with one row
say $results.hash; # A single hash with one row
say $results.arrays; # A sequence of arrays with all rows
say $results.hashes; # A sequence of hashes with all rows
$results.finish # Only needed if results aren't consumed.
=head1 DESCRIPTION
Returned from a C<DB::Pg::Statement> execution that returns results.
=head1 METHODS
=head2 B<rows>()
Returns number of rows returned.
=head2 B<columns>()
Array of the names of the columns (fields) to be returned.
=head2 B<types>()
Array of the Perl types of the columns (fields) to be returned.
=head2 B<finish>()
Finish with the database connection. This is only needed if the complete
database returns aren't consumed.
=head2 B<row>(Int $row, Bool :hash)
Retrieves a specific row from the results, either as an array, or as a
Hash if :hash is True.
=head2 B<value>()
Return a single scalar value from the results.
=head2 B<array>()
Return a single row from the results as an array.
=head2 B<hash>()
Return a single row from the results as a hash.
=head2 B<arrays>()
Returns a sequence of all rows as arrays.
=head2 B<hashes>()
Returns a sequence of all rows as hashes.
=end pod