[CDBI] processing mysql errors
bsb at bereft.net
Mon Sep 5 05:42:21 BST 2005
> Thanks for this. Yes DBD::mysql makes some attributes available,
> including mysql_errno and mysql_error. I believe these are propagated up
> through DBI and Class::DBI. But the detailed information, which users
> want and which my attempt at automatic recovery needs, is buried in the
> MySQL mysql_error string, AFAIK. So it seems like I need to parse that
> and I can't believe I'm the first to want to do it :)
You're not the first. Although it shames me to admit it, I'm rewriting
the "Duplicate entry" key violation errors. I couldn't find a module to
do this, nor much guidance in the MySQL docs. The code therefore
depends on the formatting and other, possibly unstable, properties of
the error message.
It has worked ok for my modest needs but it's inherently flaky.
For instance, to rewrite a message like:
"Duplicate entry '123' for key 2"
You need to know what key 2 is and the only documentation I could find
is in the CREATE TABLE docs:
In the created table, a PRIMARY KEY is placed first, followed by all
UNIQUE indexes, and then the non-unique indexes. This helps the MySQL
optimizer to prioritize which index to use and also more quickly to
detect duplicated UNIQUE keys.
So I'm dependent on something which happens to suit the optimizer.
Furthmore, the '123' data string has dashes inserted between the items
for multi-column data. I vaguely recollect dashes in the data being
unescaped, so you may hit some ambiguity.
SHOW INDEX FROM $table
Have I scared you off this approach yet?
More information about the ClassDBI