Ok, per request I am showing my code. The first thing I edit was edit
DBI.pm,, version 1.19 and do a use Data::Dumper; print Dumper($dbh) in
DBD::_::db::selectall_arrayref -
sub selectall_arrayref {
my ($dbh, $stmt, $attr, @bind) = @_;
use Data::Dumper;
warn "SAR: ", Dumper($dbh);
my $sth = (ref $stmt) ? $stmt
: $dbh->prepare($stmt, $attr);
return unless $sth;
$sth->execute(@bind) || return;
my $slice = $attr->{dbi_fetchall_arrayref_attr}; # typically
undef
return $sth->fetchall_arrayref($slice);
}
now here is my file that results in a call to this module. Note the
prints of ref($dbh) immediately before calling selectall_arrayref....
they show that $dbh belongs to *my* subclass not to DBI's classes:
use DBI::Cache qw(MLDM::Sync::SDBM_File);
my $dbh;
my $sth;
$dbh = DBI::Cache->connect($connect,'postgres');
sub first_run {
my $select = 'select * from weather LIMIT 10';
$sth = $dbh->prepare($select); # 1st part of cache key is built
$sth->execute; # 2nd part of cache key is built
# execute will return a DBI::st or DBI::Cache::st based on whether
# a cached version of the query was found
# thus the fetches below are from either an in-memory array that was
# cached or via DBI::st::fetch_*
while (my $row = $sth->fetchrow_arrayref) {
warn "Row -> ", Dumper($row);
}
my $ref = ref($dbh);
warn "ABOUT TO SAR";
$dbh->selectall_arrayref($select); ##### PROBLEM LINE!!!!!!!!!!!!
}
my module is kinda long, I hope attachments work on this list. If they
dont, the bottom line is they create and inheritance hierarchy just like
in t/subclass.t... inline here I will just list the @ISA statements in
DBI/Cache.pm - my module. But will attach the whole thing
package DBI::Cache::db;
use strict;
our @ISA = qw(DBI::db);
package DBI::Cache::st;
our @ISA = qw(DBI::st);
package DBI::Cache;
require 5.005_62;
use strict;
use warnings;
use Data::Dumper;
our @ISA = qw(DBI);
use DBI;
package DBI::Cache;
require 5.005_62;
use strict;
use warnings;
use Data::Dumper;
our @ISA = qw(DBI);
our $VERSION = '0.01';
our $AUTOLOAD;
our $select;
our $execute;
our $joiner = '!!';
use vars qw(%cache $dbm);
%cache=();
$dbm = {};
sub import {
require MLDBM::Sync;
require Fcntl;
MLDBM::import(@_);
eval(<<EOE)
$dbm = tie %cache, 'MLDBM', 'testmldbm', O_CREAT|O_RDWR, 0640 or die $!;
EOE
}
sub select {
my $class = shift;
$select = shift;
}
sub execute {
my $class = shift;
$execute = "@_";
}
sub key { "$select$joiner$execute" }
sub flush {
my $class = shift;
my $data = shift;
my $key = key;
$cache{$key} = $data;
}
sub retrieve {
my $key = key;
warn "RETREIVE seeks $key";
$cache{$key};
}
sub dbi_cache_key_gen {
my ($statement, $attr) = @_;
my @attr_keys = ($attr) ? sort keys %$attr : ();
my $key = ($attr)
? join("~~", $statement, @attr_keys, @{$attr}{@attr_keys}) : $statement;
}
sub connect {
my $self = shift;
my $dbh = DBI->connect(@_);
bless $dbh, 'DBI::Cache::db';
}
1;
package DBI::Cache::db;
use strict;
our @ISA = qw(DBI::db);
use Data::Dumper;
sub prepare {
my $self = shift;
warn "::db->prepare", Dumper(\@_);
my $sth = $self->SUPER::prepare(@_);
if ($_[0] =~ /^select/i) {
DBI::Cache->select("@_");
bless $sth, 'DBI::Cache::st';
}
$sth;
}
1;
package DBI::Cache::st;
our @ISA = qw(DBI::st);
use Data::Dumper;
use Carp;
our $fetch;
our %fetch =
(
fetchrow_arrayref => sub { push @{$fetch->{$_[0]}}, $_[1] },
execute => sub { $fetch->{$_[0]} = $_[1] }
);
sub store_fetch_data {
my ($caller) = ((caller(1))[3] =~ /.*::(.*)/);
$fetch{$caller}->($caller,$_[0]);
}
sub execute {
my $sth = $_[0];
warn "::st->execute @_";
DBI::Cache->execute(@_[1..$#_]);
if (my $data = DBI::Cache->retrieve) {
warn "Found!";
# doesnt work as documented $sth->{'private_Cache'} = $data;
$sth->STORE('NUM_OF_FIELDS', scalar @{$data->[0]});
store_fetch_data $data;
bless $_[0], 'DBI::Cache::st::cached';
return 1;
} else {
shift;
my $retval = $sth->SUPER::execute(@_);
$retval;
}
}
sub flush {
not scalar keys %$fetch and return;
scalar keys %$fetch == 1
or die "more than one fetch executed without a flush: ",
Data::Dumper::Dumper($fetch);
for my $fetch_type (keys %$fetch) {
warn "FLUSH: ", DBI::Cache->key, Dumper($fetch->{$fetch_type});
DBI::Cache->flush($fetch->{$fetch_type});
}
undef $fetch;
}
sub select_error_msg {
sprintf
'
ERROR: select, selectall, etc are $dbh, not $sth methods.
perldoc DBI for details
', $_[1];
}
sub selectall_arrayref { croak select_error_msg }
# fetchrow_hashref is complimentary!
sub fetchrow_arrayref {
my($sth, @args) = @_;
my $row = $sth->SUPER::fetchrow_arrayref(@args);
if ($row) {
store_fetch_data [ @$row ];
my @new = map { lc $_ } @$row;
\@new;
}
}
END {
}
1;
package DBI::Cache::st::cached;
use Data::Dumper;
our @ISA = qw(DBI::st);
sub fetchrow_arrayref {
my $sth = shift;
my $data = $DBI::Cache::st::fetch->{execute};
my $row = shift @$data;
if (!$row) { return undef; }
if ($sth->FETCH('ChopBlanks')) {
map { $_ =~ s/\s+$//; } @$row;
}
return $sth->_set_fbav($row);
}
1;
__END__
# Below is stub documentation for your module. You better edit it!
=head1 NAME
DBI::Cache - transparent SQL SELECT caching in DBI programs via MLDBM::Sync
=head1 SYNOPSIS
use DBI::Cache;
my $connect = 'dbi:Pg:dbname=mydb';
my $dbh = DBI::Cache->connect($connect,'postgres');
my $sth = $dbh->prepare('select * from weather LIMIT 10');
$sth->execute;
while (my $row = $sth->fetchrow_arrayref) {
;
}
$sth->flush; # explicit caching of fetch results
$dbh->disconnect;
=head1 DESCRIPTION
Stub documentation for DBI::Cache, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.
Blah blah blah.
=head1 AUTHOR
T. M. Brannon, <[EMAIL PROTECTED]>
=head1 TO DO
=over 4
=item * automate flush
=item * clear execute
=item * test suite
=back
=head1 SEE ALSO
perl(1).
=cut
On Sunday, October 14, 2001, at 03:20 AM, Stephen Clouse wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Sat, Oct 13, 2001 at 05:15:14PM -0700, [EMAIL PROTECTED] wrote:
>> I have studied t/subclass.t and have created a subclass of DBI::st and
>> DBI::db.
>>
>> Something mystifies me though. Even though I have a database handle
>> blessed into my subclass, when this database handle calls
>> $dbh->selectall_arrayref, which is does not define and hence is
>> resolved
>> by calling DBI::db::selectall_arrayref , an immediate print of the $dbh
>> there shows it as blessed into DBI::db instead of the class of the
>> original $dbh.
>
> Mind showing some code that demonstrates the problem? There's nothing
> in DBI
> that reblesses the dbh, so it's hard to say what the problem is without
> being
> able to reproduce it.
>
> - --
> Stephen Clouse <[EMAIL PROTECTED]>
> Senior Programmer, IQ Coordinator Project Lead
> The IQ Group, Inc. <http://www.theiqgroup.com/>
>
> -----BEGIN PGP SIGNATURE-----
> Version: PGP 6.5.8
>
> iQA/AwUBO8lmzwOGqGs0PadnEQJrjgCgmHl15isOdQ6F6CkcVsBvJC+3i7YAoJWo
> fOdenpl26rBKxarnTc4fygSu
> =mwz9
> -----END PGP SIGNATURE-----
>