/
Result.pm6
114 lines (76 loc) · 2.39 KB
/
Result.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
use DB::SQLite::Native;
use DB::Result;
sub sqlite-int($stmt, $i) { $stmt.int64($i) }
sub sqlite-double($stmt, $i) { $stmt.double($i) }
sub sqlite-text($stmt, $i) { $stmt.text($i) }
sub sqlite-blob($stmt, $i) { buf8.new($stmt.blob($i)[^$stmt.bytes($i)]) }
sub sqlite-null($stmt, $i) { Any }
my %convert = Map.new: map( { +SQLITE_TYPE::{.key} => .value}, (
SQLITE_INTEGER => &sqlite-int,
SQLITE_FLOAT => &sqlite-double,
SQLITE_TEXT => &sqlite-text,
SQLITE_BLOB => &sqlite-blob,
SQLITE_NULL => &sqlite-null
));
class DB::SQLite::Result does DB::Result
{
has $.stmt = $!sth.stmt;
has $.count = $!sth.count;
method names() { do for ^$!count { $!stmt.name($_) } }
method row()
{
given $!stmt.step()
{
when SQLITE_ROW
{
do for ^$!count -> $i
{
%convert{$!stmt.type($i)}($!stmt, $i);
}
}
when SQLITE_DONE
{
()
}
default
{
$!stmt.db.check($_)
}
}
}
}
=begin pod
=head1 NAME
DB::SQLite::Result -- Results from a SQLite query
=head1 SYNOPSIS
my $results = $sth.execute(1);
say $results.count; # Number of column fields
say $results.keys; # Array of column field keys
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::SQLite::Statement> execution that returns
results.
=head1 METHODS
=head2 B<count>()
Returns the number of fields in each row.
=head2 B<keys>()
Array of the names of the columns (fields) to be returned.
=head2 B<finish>()
Finish the database connection. This is only needed if the complete
database returns aren't consumed.
=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>()
Return a sequence of all rows as arrays.
=head2 B<hashes>()
Return a sequence of all rows as hashes.
=end pod