Tim Bunce wrote:
> On Tue, Sep 30, 2003 at 09:39:03PM +0100, Andy Hassall wrote:
>> Tim Bunce wrote:
>>
>> Being able to write something like:
>>
>> $sth_insert->execute(1,2,3);
>> if ($dbh->err) {
>> if ($dbh->dbi_error_code == DBI::ERROR_PRIMARY_KEY_VIOLATION) {
>> // do an update instead
>> }
>> if ($dbh->dbi_error_code ==
>> DBI::ERROR_FOREIGN_KEY_VIOLATION_PARENT_NOT_FOUND) {
>> // add necessary parent records, retry insert
>> }
>> }
>>
>> ... and not worry about which DBD it's using could be useful,
>> certainly neat anyway (I can never remember the numeric codes to
>> check for when switching between Oracle and MySQL).
>>
>> What would need mapping? An initial list could be:
>>
>> Primary key violation
>> Unique key violation
>> Not null constraint violation
>> Check constraint violation
>> Foreign key violation - parent not found on insert
>> Foreign key violation - child records exist on update or delete
>
> All very wonderful in theory. But what about databases and drivers
> that can't provide that level of detail?
OK, I see your point; then you have to fall back to looking at DBI->errstr
as before, as it'd have to return some sort of 'unable to classify' error
value.
Just seemed a fairly neat idea that DBI could define a set of higher-level
errors that are common to 'databases in general' (whatever that really
means - hmm), and then have the DBD make a 'best effort' to map native
errors onto that set; since the DBD is the place that has most specific
knowledge about what a particular database is going to do? Some DBDs may not
have to the information available, but at least for those that were able,
you'd have this consistent set to check against first, before falling back
to database specific checks.
So perhaps it's not something for DBI then; could still perhaps be useful as
a module that takes a $dbh and tries to classify the current error, from
rules dependent on the $dbh->{Driver}->{Name}. I'll have a go at writing som
ething for Oracle and MySQL at least, since i've never really liked having
to check for numeric native codes from a readability point of view.
--
Andy Hassall ([EMAIL PROTECTED]) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)