in reply to Re: Does fetchall_arrayref() ever return a NULL?
in thread Does fetchall_arrayref() ever return a NULL?

My attempt to reproduce this has failed.

use strict; use warnings; use Test::More tests => 2; use DBI; use Data::Dumper; my $dbh = DBI->connect ('dbi:SQLite:dbname=:memory:', '', ''); my $sth = $dbh->prepare ('SELECT 1 = 1;'); $sth->execute; $sth->finish; ok ! $sth->{Active}, 'Handle is inactive'; my $res = $sth->fetchall_arrayref (); is $res, undef, 'Result is undef' or diag Dumper ($res); diag "DBI $DBI::VERSION";
$ perl inactive.t 1..2 ok 1 - Handle is inactive not ok 2 - Result is undef # Failed test 'Result is undef' # at /tmp/inactive2.t line 16. # got: 'ARRAY(0x2c41050)' # expected: undef # $VAR1 = []; # DBI 1.643 # Looks like you failed 1 test of 2.

On this apparently inactive handle it still returns an empty arrayref. Where am I going wrong?


🦛

Replies are listed 'Best First'.
Re^3: Does fetchall_arrayref() ever return a NULL?
by tangent (Parson) on May 06, 2022 at 20:01 UTC
    I tried your test using MySQL and it gave the same result. But I checked the handle for errors and it produced "fetch() without execute()". So perhaps returning an empty arrayref is correct, as there was an error. Interesting that SQLite did not throw that error.
Re^3: Does fetchall_arrayref() ever return a NULL?
by erix (Prior) on May 06, 2022 at 12:38 UTC

    You can't do any fetch after calling finish(), I don't think

      I'm not trying to perform a successful fetch - I'm trying to reproduce the documented behaviour. Hence calling finish first in order to ensure that the handle is inactive. And the fetch still doesn't return undef.


      🦛

        I didn't immediately spot that intention. And fair enough: the behaviour doesn't quite agree with the documented tekst.

        Then again, the DBI doc /also/ says that the "Active" attribute is vague at the moment, and means 'connected to database', with advise not to use it.

        It would seem that documentation could benefit from some polishing here and there

Re^3: Does fetchall_arrayref() ever return a NULL?
by bartender1382 (Beadle) on May 07, 2022 at 15:23 UTC

    Thank for trying all that.

    In the end, I decided it would just be easier if I checked for null in both places and move on.

    checking for undef in the return, and no records when I check the array size

      Another option is to enable RaiseError, and then you get an exception instead of undef, and cleaner code to wrap up errors.