On Tue, Apr 05, 2005 at 07:27:10PM -0700, Patrick Galbraith wrote:
> Tim,
> 
> I have a fix to the emulated code that throws an error if you try to 
> bind a non-numeric value as a numeric - is this the desired behaviour? 
> I tested my fix, and it works fine, but perhaps you have pointers to 
> make it even more elegant:
> 
> if (! looks_like_number(value))
>       if (sql_type == SQL_NUMERIC ||
>           sql_type == SQL_DECIMAL ||
>           sql_type == SQL_INTEGER ||
>           sql_type == SQL_SMALLINT ||
>           sql_type == SQL_FLOAT ||
>           sql_type == SQL_REAL ||
>           sql_type == SQL_DOUBLE)
>         do_error(sth, JW_ERR_ILLEGAL_PARAM_NUM,
>                        "Non-numeric value bound as numeric!");

I'd swap the order of the tests as looks_like_number is more
expensive than the sql_type tests.

> perl code to test:
> 
> $sth= $dbh->prepare("UPDATE bind SET string = ?, num = ? WHERE id = ?");
> $sth->bind_param(1, 'one', SQL_VARCHAR());
> $sth->bind_param(2, '1.#INF', SQL_DOUBLE());
> $sth->bind_param(3, 1, SQL_INTEGER());
> $sth->execute();
> 
> error message:
> 
> DBD::mysql::st bind_param failed: Non-numeric value bound as numeric! 
> at ./test_bind.pl line 44.

Error messages that don't include relevant information always bug me.
The message should include the placeholder name (number) and the
value being bound. For the value use the neatsvpv(sv,0) function,
which exists for exactly that purpose.

> Maybe there's a better way to do the equivalent to checking the SQL 
> type with the or's.  Also, I couldn't find an error to pass to do_error 
> (which just calls DBIh_EVENT2) (I need a ERROR_event for this 
> particular error perhaps?)

The DBIh_EVENT2 and ERROR_event stuff is all deprecated.
See http://search.cpan.org/~timb/DBI/lib/DBI/DBD.pm#The_dbd_drv_error_method

(all driver authors should reread that section and, if needed, update
their code to use the newish DBIh_SET_ERR_*(...) macros which
do-the-right-thing WRT set_err().)

Tim.

Reply via email to