Skip to content

Commit ae71b30

Browse files
committed
added a fetchrow_typedhash method to have a hash value with correct perl6 type instead of all Str
1 parent bb3fea5 commit ae71b30

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/DBDish.pm6

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ role DBDish::StatementHandle does DBDish::ErrorHandling {
5959
}
6060

6161
method fetchrow_hashref { $.fetchrow-hash }
62+
method fetchrow_typedhash { die "Your selected backend does not support/implement typed values" }
6263

6364
method fetchall-hash {
6465
my @names := self.column_names;

lib/DBDish/Pg.pm6

100644100755
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ constant %oid-to-type-name = (
136136

137137
).hash;
138138

139+
139140
constant CONNECTION_OK = 0;
140141
constant CONNECTION_BAD = 1;
141142

@@ -327,6 +328,26 @@ class DBDish::Pg::StatementHandle does DBDish::StatementHandle {
327328
return $results_ref;
328329
}
329330

331+
#Try to map pg type to perl type
332+
method fetchrow_typedhash {
333+
my Str @values = self.fetchrow_array;
334+
return Any if !@values.defined;
335+
my @names = self.column_names;
336+
my @types = self.column_oids;
337+
my %hash;
338+
my %p = 'f' => False, 't' => True;
339+
for 0..(@values.elems-1) -> $i {
340+
given (%oid-to-type-name{@types[$i]}) {
341+
%hash{@names[$i]} = @values[$i] when 'Str';
342+
%hash{@names[$i]} = @values[$i].Num when 'Num';
343+
%hash{@names[$i]} = @values[$i].Int when 'Int';
344+
%hash{@names[$i]} = %p{@values[$i]} when 'Bool';
345+
%hash{@names[$i]} = @values[$i].Real when 'Real';
346+
}
347+
}
348+
return %hash;
349+
}
350+
330351
method finish() {
331352
if defined($!result) {
332353
PQclear($!result);

0 commit comments

Comments
 (0)