Permalink
Browse files

each driver statement handle should implement fetchrow; the other arr…

…ay functions can be implemented only once with roles
  • Loading branch information...
1 parent 2f555a1 commit 5543e9437907ca16aa37e9576e69882733433533 @moritz moritz committed Apr 27, 2012
Showing with 19 additions and 101 deletions.
  1. +15 −0 lib/DBDish.pm6
  2. +2 −36 lib/DBDish/Pg.pm6
  3. +1 −12 lib/DBDish/SQLite.pm6
  4. +1 −53 lib/DBDish/mysql.pm6
View
@@ -51,7 +51,22 @@ mainly provides the C<execute> and C<finish> methods. It also has all the method
role DBDish::StatementHandle does DBDish::ErrorHandling {
method finish() { ... }
+ method fetchrow() { ... }
method execute(*@) { ... }
+
+ method allrows {
+ gather while self.fetchrow -> @row {
+ take @row.item;
+ }
+ }
+ method fetchrow_array { self.fetchrow }
+ method fetchrow_arrayref {
+ $.fetchrow;
+ }
+ method fetch() {
+ $.fetchrow;
+ }
+ method fetchall_arrayref { [ self.allrows.eager ] }
}
=begin pod
View
@@ -175,7 +175,7 @@ class DBDish::Pg::StatementHandle does DBDish::StatementHandle {
}
}
- method fetchrow_array() {
+ method fetchrow() {
my @row_array;
return if $!current_row >= $!row_count;
@@ -184,7 +184,7 @@ class DBDish::Pg::StatementHandle does DBDish::StatementHandle {
}
if defined $!result {
- self!errstr = Any;
+ self!reset_errstr;
for ^$!field_count {
@row_array.push(PQgetvalue($!result, $!current_row, $_));
@@ -197,40 +197,6 @@ class DBDish::Pg::StatementHandle does DBDish::StatementHandle {
return @row_array;
}
- method fetchrow_arrayref() {
- my $row_arrayref;
-
- return if $!current_row >= $!row_count;
-
- unless defined $!field_count {
- $!field_count = PQnfields($!result);
- }
- if defined $!result {
- self!reset_errstr;
-
- my @row = self!get_row();
-
- my $errstr = PQresultErrorMessage ($!result);
- if $errstr ne '' {
- self!set_errstr($errstr);
- }
-
- if @row {
- $row_arrayref = @row;
- }
- else { self.finish; }
- }
- return $row_arrayref;
- }
- method fetch() { self.fetchrow_arrayref(); } # alias according to perldoc DBI
- method fetchall_arrayref() {
- my @res;
- while self.fetchrow_arrayref -> $a {
- @res.push: $a;
- }
- @res.item
- }
-
method fetchrow_hashref () {
my $row_hashref;
my %row_hash;
View
@@ -119,7 +119,7 @@ class DBDish::SQLite::StatementHandle does DBDish::StatementHandle {
$rows == 0 ?? '0E0' !! $rows;
}
- method fetchrow_array {
+ method fetchrow {
my @row;
die 'fetchrow_array without prior execute' unless $!row_status.defined;
return @row if $!row_status == SQLITE_DONE;
@@ -132,17 +132,6 @@ class DBDish::SQLite::StatementHandle does DBDish::StatementHandle {
@row;
}
- method fetchrow_arrayref {
- self.fetchrow_array.item;
- }
- method fetch() { self.fetchrow_arrayref }
- method fetchall_arrayref {
- my @rows;
- while self.fetchrow_arrayref -> $r {
- @rows.push: $r;
- }
- @rows.item;
- }
method finish() {
sqlite3_finalize($!statement_handle) if $!statement_handle.defined;
View
@@ -166,7 +166,7 @@ class DBDish::mysql::StatementHandle does DBDish::StatementHandle {
}
}
- method fetchrow_array() {
+ method fetchrow() {
my @row_array;
unless defined $!result_set {
@@ -193,58 +193,6 @@ class DBDish::mysql::StatementHandle does DBDish::StatementHandle {
return @row_array;
}
- method fetchrow_arrayref() {
- my $row_arrayref;
- unless defined $!result_set {
- $!result_set = mysql_use_result( $!mysql_client);
- $!field_count = mysql_field_count($!mysql_client);
- }
- if defined $!result_set {
- # warn "fetching a row";
- self!reset_errstr();
- my $native_row = mysql_fetch_row($!result_set); # can return NULL
- my $errstr = mysql_error( $!mysql_client );
- if $errstr ne '' { self!set_errstr($errstr); }
- if $native_row {
- my @row_array;
- loop ( my $i=0; $i < $!field_count; $i++ ) {
- @row_array[$i] = $native_row[$i];
- }
- $row_arrayref = @row_array;
- }
- else { self.finish; }
- }
- return $row_arrayref;
- }
- method fetch() { self.fetchrow_arrayref(); } # alias according to perldoc DBI
- method fetchall_arrayref() {
- my $all_arrayref;
- unless defined $!result_set {
- $!result_set = mysql_use_result( $!mysql_client);
- $!field_count = mysql_field_count($!mysql_client);
- }
- if defined $!result_set {
- self!reset_errstr();
- my @all_array;
- while ! self.errstr && my $native_row = mysql_fetch_row($!result_set) { # can return NULL
- my $row_arrayref;
- my $errstr = mysql_error( $!mysql_client );
- if $errstr ne '' { self!set_errstr($errstr); }
- if $native_row {
- my @row_array;
- loop ( my $i=0; $i < $!field_count; $i++ ) {
- @row_array[$i] = $native_row[$i];
- }
- $row_arrayref = @row_array;
- push @all_array, $row_arrayref;
- }
- else { self.finish; }
- }
- $all_arrayref = @all_array;
- }
- return $all_arrayref;
- }
-
method fetchrow_hashref () {
my $row_hashref;
my %row_hash;

0 comments on commit 5543e94

Please sign in to comment.