Skip to content

Commit

Permalink
The GLR of DBDIsh. $sth.allrows now returns Seq
Browse files Browse the repository at this point in the history
Fetching rows is now lazy, plus:
- named ':hash' of .row method is now implemented by the role.
- legacy '.fetchrow' requirement removed from drivers, a simple one
  implement by the role.
- Allows driver's ._row method return a simple list of typed values
  • Loading branch information
salortiz committed Mar 19, 2016
1 parent 0055e68 commit 3849814
Showing 1 changed file with 60 additions and 41 deletions.
101 changes: 60 additions & 41 deletions lib/DBDish/StatementHandle.pm6
Expand Up @@ -3,38 +3,42 @@ use v6;
=begin pod
=head2 role DBDish::StatementHandle
The Connection C<prepare> method returns a StatementHandle object that
mainly provides the C<execute> and C<finish> methods. It also has all the methods from C<DBDish::Role::ErrorHandling>.
should provides the C<execute> and C<finish> methods.
A Statetement handle should provide also the low-level C<_row> and C<_free>
methods.
It also has all the methods from C<DBDish::Role::ErrorHandling>.
=end pod

need DBDish::ErrorHandling;

unit role DBDish::StatementHandle does DBDish::ErrorHandling;

my role IntTrue { method Bool { self.defined } };

has Int $.Executed = 0;
has Bool $.Finished = True;
has Int $!affected_rows;
has @!column-name;
has @!column-type;

method dispose() {
self.finish unless $!Finished;
self._free;
my \id := self.WHICH.Str;
?($.parent.Statements{id}:delete);
}
#Avoid leaks if explicit dispose isn't used by the user.
submethod DESTROY() {
self.dispose;
}
# My defined interface
method execute(*@ --> IntTrue) { ... }
method finish(--> Bool) { ... }
method _row(--> Array) { ... }
method _free() { ... }

method !ftr() {
$.parent.last-sth-id = self.WHICH;
}

method !enter-execute() {
self.finish unless $!Finished;
$!affected_rows = Nil;
self!ftr;
}

method !done-execute(Int $rows, Bool $was-select) {
$!Executed++;
$!Finished = False;
Expand All @@ -48,20 +52,28 @@ method new(*%args) {
%args<parent>.Statements{sth.WHICH} = sth;
}

my role IntTrue { method Bool { self.defined } };
method dispose() {
self.finish unless $!Finished;
self._free;
my \id := self.WHICH.Str;
?($.parent.Statements{id}:delete);
}
#Avoid leaks if explicit dispose isn't used by the user.
submethod DESTROY() {
self.dispose;
}

method rows {
$!affected_rows but IntTrue;
}

method _free() { ... }
method finish(--> Bool) { ... }
method fetchrow() { ... }
method execute(*@ --> IntTrue) { ... }
method _row(:$hash) { ... }

method row(:$hash) {
self!ftr;
self._row(:$hash);
if my \r = self._row {
$hash ?? (@!column-name Z=> @(r)).hash !! r.Array;
} else {
$hash ?? % !! @;
}
}

method column-names {
Expand All @@ -73,37 +85,50 @@ method column-types {
}

multi method allrows(:$array-of-hash!) {
my @rows;
while self.row(:hash) -> %row {
@rows.push(%row);
gather {
while self.row(:hash) -> %r {
take %r;
}
}
@rows;
}

multi method allrows(:$hash-of-array!) {
my @names := @!column-name;
my %rows = @names Z=> [] xx *;
my %rows = @!column-name Z=> [] xx *;
while self.row -> @a {
for @a Z @names -> ($v, $n) {
for @a Z @!column-name -> ($v, $n) {
%rows{$n}.push: $v;
}
}
%rows;
}

multi method allrows() {
my @rows;
while self.row -> @r {
@rows.push(@r);
gather {
while self.row -> @r {
take @r;
}
}
@rows;
}

method fetchrow-hash() {
# Legacy
method fetchrow {
if my \r = self._row {
my @ = (r.map: { .defined ?? ~$_ !! Str });
} else { @ };
}

method fetchrow-hash {
hash @!column-name Z=> self.fetchrow;
}
method fetchrow_hashref { self.fetchrow-hash }

method fetchrow_hashref { $.fetchrow-hash }
method fetchall_hashref(Str $key) {
my %results;
while self.fetch-hash -> \h {
%results{h{$key}} = h;
}
%results;
}

method fetchall-hash {
my @names := @!column-name;
Expand All @@ -113,7 +138,7 @@ method fetchall-hash {
%res{$n}.push: $v;
}
}
return %res;
%res;
}

method fetchall-AoH {
Expand All @@ -133,13 +158,7 @@ method fetchall-array {
}

method fetchrow_array { self.fetchrow }

method fetchrow_arrayref {
$.fetchrow;
}

method fetch() {
$.fetchrow;
}
method fetchrow_arrayref { self.fetchrow; }
method fetch { self.fetchrow; }

method fetchall_arrayref { [ self.fetchall-array.eager ] }

0 comments on commit 3849814

Please sign in to comment.