in reply to Can I tell if DBI's quote() was used?

You could possibly override $dbh->quote so that it always returns a string that's marked in some way (have it return a stringifiable object, maybe).

Probably not good for performance, but may be useful in your testing/bug-hunting scenario.

Replies are listed 'Best First'.
Re^2: Can I tell if DBI's quote() was used?
by kyle (Abbot) on Apr 06, 2008 at 19:44 UTC

    I love this idea! The object it returns could (hopefully) track its own usage and dump that information for debugging. That is, use caller to provide a log:

    1. File and line where the object was created.
    2. File and line for each place it was stringified.

    It could dump this on destruction or continuously during its lifetime. I wonder if it could detect when it was about to be double quoted. This seems useful enough that I wonder if it already exists.

      I wonder if it could detect when it was about to be double quoted. This seems useful enough that I wonder if it already exists.

      If you override overload stringification, you could log any call to "". I expect that would be more or less enough information. Keep in mind that you actually want to detect the strings that haven't been quote()ed, though :-)

        Yes, for detecting things that are not quoted, I can insert a sanity check everywhere. So, if I have this:

        $dbh->do( "DELETE FROM user WHERE name = $name" );

        ...I can do this:

        ensure_quoted( $name ); $dbh->do( "DELETE FROM user WHERE name = $name" ); sub ensure_quoted { return if ref $_[0] ne '' && $_[0]->isa( 'Magic::DBI::Quote' ); if ( $just_fix_it ) { # replace caller's variable $_[0] = $dbh->quote( $_[0] ); } elsif ( $raise_the_red_flag ) { die "Oh noes! Unquoted string '$_[0]'"; } }

        This is still considerable work, but at least I don't have to figure out where $name came from.