Problems with database handle attributes in error handle



Hi,

I have my own error handler in which I'd like to log as much detail about the
error as possible. In this handler I do something like:

sub _error_handler {
my ($msg, $h, $method_ret) = @_;
my $dbh = $h;
$dbh = $h->{Database} if ($h->{Type} eq "st");
print "DB: ". $dbh->{Name}.
", Username: ". $dbh->{Username};
print "Error: (handle type: ". $h->{Type}. ")" .
"(SQL:" . $h->{Statement} . ") (msg:".
$h->errstr . ")");
print Carp::longmess("DBI error trap")};
}

(vastly simplified).

This works fine when $h is type "st" and is mostly fine when $h is type "dr"
with one notable exception - when DBI->connect fails as I get:

Can't get DBI::dr=HASH(0x83cbbc4)->{Username}: unrecognised attribute name at
/usr/local/lib/perl5/site_perl/5.8.7/i686-linux/DBI.pm line 1329.

This is a warning (I always use - use warnings).

I cannot protect the $dbh->{Username} references by checking for existance
first e.g.

print "Username: " . $dbh->{Username} if (exists($dbh->{Username}));

as I get the same warning message. Is this correct behavior? I found:

/* finally check the actual hash just in case */
if (valuesv == Nullsv) {
valuesv = &sv_undef;
cacheit = 0;
svp = hv_fetch((HV*)SvRV(h), key, keylen, FALSE);
if (svp)
valuesv = newSVsv(*svp); /* take copy to mortalize */
else if (!( (*key=='H' && strEQ(key, "HandleError"))
|| (*key=='H' && strEQ(key, "HandleSetErr"))
|| (*key=='S' && strEQ(key, "Statement"))
|| (*key=='P' && strEQ(key, "ParamValues"))
|| (*key=='P' && strEQ(key, "Profile"))
|| (*key=='C' && strEQ(key, "CursorName"))
|| (*key=='C' && strEQ(key, "Callbacks"))
|| !isUPPER(*key) /* dbd_*, private_* etc */
))
warn("Can't get %s->{%s}: unrecognised attribute name",neatsvpv(h,0)
,key);
}

in DBI.xs and the pureperl DBI (which I'm not using) seems to be substantially
different in this area. I noticed Changes file for 1.37 says:

Fixed "Can't get dbh->{Statement}: unrecognised attribute" error in test
caused by change to perl internals in 5.8.0

and a diff of 1.36/1.37 seems to have added the strEQ test for "Statement"
(above). Is this a similar case perhaps?

Thanks.

Martin
--
Martin J. Evans
Easysoft Ltd, UK
http://www.easysoft.com

.