ok, in this module, the standard handle attribute NUM_OF_FIELDS does
work perfectly fine for STORE-ing/FETCH-ing $sth fields.... however,
when I try to create a private attribute and fetch it (via the lines
$sth->FETCH) i get the standard field but not my private one....
also, I am aware that I should not be inlining large parts of DBI.pm in
my subclass, but per my previous post, I don't know why my objects
get-reblessed into the superclass when method resolution discovers the
method in a DBI class as opposed to my subclass.
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 trim {
$_[0] =~ s/^\s+//;
$_[0] =~ s/\s+$//;
$_[0];
}
sub select {
my $class = shift;
my @arg = grep { $_ } @_;
warn "arg: @arg";
$select = trim "@arg";
warn "SELECT:$select:";
}
ub execute {
my $class = shift;
my @arg = grep { $_ } @_;
$execute = trim "@arg";
warn "EXEC:$execute:";
}
sub key { "$joiner$select$joiner$execute$joiner" }
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(\@_);
DBI::Cache::st->flush;
my $sth = $self->SUPER::prepare(@_);
if ($_[0] =~ /^select/i) {
warn "prepare is caching on", join ':', @_;
DBI::Cache->select(@_);
bless $sth, 'DBI::Cache::st';
}
$sth;
}
sub prepare_cached {
my ($dbh, $statement, $attr, $allow_active) = @_;
# Needs support at dbh level to clear cache before complaining about
# active children. The XS template code does this. Drivers not using
# the template must handle clearing the cache themselves.
my $cache = $dbh->FETCH('CachedKids');
$dbh->STORE('CachedKids', $cache = {}) unless $cache;
my @attr_keys = ($attr) ? sort keys %$attr : ();
my $key = ($attr) ? join("~~", $statement, @attr_keys,
@{$attr}{@attr_keys}) \
: $statement;
my $sth = $cache->{$key};
if ($sth) {
if ($sth->FETCH('Active')) {
Carp::carp("prepare_cached($statement) statement handle $sth was
still ac\
tive")
if !$allow_active;
$sth->finish;
}
return $sth;
}
$sth = $dbh->prepare($statement, $attr);
$cache->{$key} = $sth if $sth;
return $sth;
}
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);
}
sub selectall_hashref {
my ($dbh, $stmt, $attr, @bind) = @_;
my $sth = (ref $stmt) ? $stmt
: $dbh->prepare($stmt, $attr);
return unless $sth;
$sth->execute(@bind) || return;
my ($row, @rows);
push @rows, $row while ($row = $sth->fetchrow_hashref());
return \@rows;
}
sub do {
my($dbh, $statement, $attr, @params) = @_;
my $sth = $dbh->prepare($statement, $attr) or return undef;
$sth->execute(@params) or return undef;
my $rows = $sth->rows;
($rows == 0) ? "0E0" : $rows;
}
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]);
}
ub 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]});
$sth->STORE('private_personal_data', 'yo ho ho and bottle of rum');
store_fetch_data $data;
bless $_[0], 'DBI::Cache::st::cached';
return 1;
} else {
shift;
my $retval = $sth->SUPER::execute(@_);
$retval;
}
}
stores selected and fetched data to MLDBM cache
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) = @_;
warn "FETCH ATTEMPT ", $sth->FETCH('NUM_OF_FIELDS');
warn "FETCH ATTEMPT+", $sth->FETCH('private_personal_data');
my $row = $sth->SUPER::fetchrow_arrayref(@args);
if ($row) {
store_fetch_data [ @$row ];
my @new = map { lc $_ } @$row;
\@new;
}
}
END {
flush;
}
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;