Skip to content

incorrect value from keys( %{$sth->{ParamValues}} ) after using $DBI::lasth when using prepare_cached() #173

@sherrardb

Description

@sherrardb

good day,
please forgive me if this has already been covered or if i have misread/misunderstood something in the docs.

context:
in order to debug an unrelated problem, i was inserting code of the form:

warn $sth->{Statement};
warn join( ', ', map { $sth->{ParamValues}->{$_} } sort { $a <=> $b } keys( %{$sth->{ParamValues}} ) );

alone, this works as expected.

but there are other sections of code where i don't have easy, direct access to the statement handle, so thought i'd use $DBI::lasth to access the statement handle from the caller:

my $lasth = $DBI::lasth;
warn $lasth->{Statement};
warn join( ', ', map { $lasth->{ParamValues}->{$_} } sort { $a <=> $b } keys( %{$last->{ParamValues}} ) );

but after this section is triggered, the previous idiom produces an empty list for calls to

keys( %{$sth->{ParamValues}} ) )

the culprit seems to be the combination of triggering keys( $DBI::lasth->{ParamValues} ) and using $dbh->prepare_cached(). removing either seems to make the code work as expected.

MRE:

#! /bin/perl

$|++;

use strict;
use warnings;

use DBI qw();
use Data::Dumper qw();

my $dbh = DBI->connect( 'DBI:mysql:<USER>:<HOST>', '<DBNAME>', '<DBPASS>' );

my $TABLE_NAME = '_test_dbi_lasth';

$dbh->do( qq[CREATE TABLE $TABLE_NAME ( foo CHAR(5) )] );

{
  my $sth = $dbh->prepare_cached( qq[SELECT foo FROM $TABLE_NAME WHERE foo = ?] );
  $sth->bind_param( 1, 'bar' );
  $sth->execute();

  DBI->trace( '2' );
  warn join( ', ', sort( { $a <=> $b } keys( %{$sth->{ParamValues}} ) ) );
  DBI->trace( '0' );
  $sth->finish();
}

my $lasth = $DBI::lasth;
DBI->trace( '2' );
keys( %{$lasth->{ParamValues}} );
DBI->trace( '0' );

{
  my $sth = $dbh->prepare_cached( qq[SELECT foo FROM $TABLE_NAME WHERE foo = ?] );
  $sth->bind_param( 1, 'bar' );
  $sth->execute();

  DBI->trace( '2' );
  warn join( ', ', sort( { $a <=> $b } keys( %{$sth->{ParamValues}} ) ) );
  DBI->trace( '0' );
  $sth->finish();
}

$dbh->do( qq[DROP TABLE $TABLE_NAME] );

the telling line from the trace output appears to be

    <> FETCH= ( HASH(0x563d576ce810)0keys ) [1 items] ('ParamValues' from cache) at dbi-lasth-bug.pl line 41

which differs from the working case

    <- FETCH= ( HASH(0x558f9f73fd50)1keys ) [1 items] at dbi-lasth-bug.pl line 41

we are stuck using an older version of for reasons that are not easy to address

~$ perl -MDBI -we 'print $DBI::VERSION."\n";'
1.643

so please forgive me if this has been addressed in the current version of the module.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions