Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

DBD::mysql warns *and* dies?

by Ovid (Cardinal)
on Jul 18, 2006 at 12:22 UTC ( [id://561989]=perlquestion: print w/replies, xml ) Need Help??

Ovid has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing some code which, amongst other things, utilities MySQL server-side prepare statements to ensure the validity of chunks of SQL. However, I have to wrap the prepare in an eval because it dies when it encounters bad SQL:

eval { $dbh->prepare( $statement, { mysql_server_prepare => 1 } ) }; if ( my $error = $@ ) { ... }

Well, that seems pretty reasonable, except for one tiny problem. I trap the exception, but it still gets sent to STDERR unless I do this:

eval { local $SIG{__WARN__} = sub {}; $dbh->prepare( $statement, { mysql_server_prepare => 1 } ) }; if ( my $error = $@ ) { ... }

Why the heck do I need to do that? Am I have a total brain-cramp and missing something obvious here? A complete example of how I'm doing this is below, though I think all of the relevant bits are above.

use DBI; use DBD::mysql 3.0002_1; # so we have have server-side prepares use Data::Record; use Regexp::Common; my $dbh = DBI->connect( "DBI:mysql:$database:$server", $user, $pass, { AutoCommit => 0, RaiseError => 1, } ); my $sql = <<'END_SQL'; SELECT 1; ALTER TABLET foo; END_SQL use Data::Dumper; my @errors = invalid_sql($dbh, $sql); print Dumper(\@errors); sub invalid_sql { my ( $dbh, $sql ) = @_; my @errors; foreach my $statement ( split_sql($sql) ) { next unless $statement =~ /\S/; local $SIG{__WARN__} = sub {}; # XXX Why do I need this? eval { $dbh->prepare( $statement, { mysql_server_prepare => 1 +} ) }; if ( my $error = $@ ) { push @errors => [ $statement => $error ]; } } return wantarray ? @errors : \@errors; } sub split_sql { my $sql = shift; my $record = Data::Record->new( { split => ';', unless => $RE{quoted}, } ); return $record->records($sql); }

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: DBD::mysql warns *and* dies?
by bart (Canon) on Jul 18, 2006 at 12:30 UTC
    Set PrintError (sibling to RaiseError) to false. I think the default value is true.

      Right in one! Thanks :) Curiously, the full code which demonstrates this error has PrintError set to false so there's something else going on, but I think this is on the right track.

      Cheers,
      Ovid

      New address of my CGI Course.

        PrintError is not affecting it, RaiseError is raising the warn, extract from perldoc for DBI:
        "If you turn RaiseError on then you'd normally turn PrintError off." Update: Please ignore that, it is twaddle, the heat is getting to me.
      There is a PrintWarn attribute too. I think it's needed to be turned off.

      I tried it on my own code—PrintError=>0 stopped the annoying warnings, PrintWarn=>0 did not change anything.


           s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print
Re: DBD::mysql warns *and* dies?
by shmem (Chancellor) on Jul 18, 2006 at 12:38 UTC
    Just a guess - most probably DBD::mysql (or DBI) is setting up a $SIG{__WARN__} handler.

    update hmm... line 972 DBI.pm

    local $SIG{__WARN__} = sub {}; $class->install_driver($driver);
    end update

    Along with this post, greetings to those having birthday today - perlcapt, one of our oldest monks (by age), and robsv, markmoon and dreadpiratepeter.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://561989]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-20 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found