On Thu, Aug 06, 2009 at 02:13:57PM -0400, Jordan Macdonald wrote:
> What is the proper return value of $dbh->prepare() if the prepare fails?
> The primary documentation ([1]CPAN and perldoc DBI) don't say. The most
> seemingly authoritative
> [2]source I can find says that it should return undef, and this is buoyed
> out by multiple examples from
> various locations which test for the failure of my $sth =
> $dbh->prepare($sql); using if (!$sth) or if
> (!defined($sth)).
> However, the behaviour of the test program (attached) shows it returning
> what appears to be the
> statement handle for a null statement:
>
> DBD::DB2::db prepare failed: [IBM][CLI Driver][DB2/LINUX] SQL0204N
> "SCHEMA.TABLE" is an undefined
> name. SQLSTATE=42704
> All is well.
> $VAR1 = bless( {}, 'DBI::st' );
>
> What is the correct behaviour? And what is the correct way to test for the
> failure of my $sth =
> $dbh->prepare($sql);?
prepare() should return undef if it failed.
Having said that, returning a statement handle that will fail when
executed is valid behaviour, although unusual. (Many drivers can't
detect errors in the statement until it's executed.)
Tim.