I'm trying to find a way of using DBI's internal knowlege
of how bind var's are managed to format a working query
for error messages.

I normally use placeholders with execute or selectall*.
This works wonderfully and saves quite a bit of hassles
trying to interpolate the queries.

Catch is that if the query fails our sysadmin's don't
want a placeholder-ized string with some values but
a "real" query they can cut+paste into the system to
see what happend.

A truncated example (multiple sub-queries removed) is:

        select
                foo,
                bar

        from
                some_table

        where
                name = ?
                and
                value = ?
                and
                date = ?


run as:


$sth->execute( $a, $a, $today );

Yes, $a is used twice, in one case it is compared to a
number, the other it used as a string.

Using the placeholders makes my life simpler since the
name and value are taken from the same variable but
DBI handles the stringy/numeric issues for itself. The
problem starts when admin's have to check why something
failed at 3am and don't know that the '?' are replaced
as '500' followed by a naked 500 (for $a) and then the
date in quotes.

What I need is something like:

my $string = $sth->interpolated( $sql, @bindlist );

which called as:

my $a = 500;

my $date = '11-Jul-1999';

$string = $sth->interpolated( $sql, $a, $a, $date )

gives me back:

        select
                foo,
                bar

        from
                some_table

        where
                name = "500"
                and
                value = 500
                and
                date = "11-Jul-1999"


The main issue is being able to walk the bind param. list and check if the columns are numeric (naked copy of $a + 0 inserted) or not (quoted copy of $a).

The alternative is having to sprintf every query I use
for each combination of values and $dbh->do() them for
large datasets in case any one of them fails (ugh!).

Looking throught he DBI-1.38 pod, the Catalog Methods
don't have anything quite like this since there is no
way to query what DBI thinks of the bound parameters
(vs. things like column_info which are about the returned
data set).

An ideal would be some sort of $dbh->blah that returned
the stringified version of whatever query was run last:

        die join "\n",
                'Bad news, boss:',
                $dbh->errstr,
                $dbh->last_query
        ;

If there is someplace w/in the SQL modules that has this
please warn me, so far wandering through CPAN hasn't
gotten me anywhere.

thanx

--
Steven Lembark                               2930 W. Palmer
Workhorse Computing                       Chicago, IL 60647
                                           +1 888 359 3508

Reply via email to