in reply to Auto-Increment and DBD Agnosticism
The obvious and simple fix is simply to perform a select right after the insert, calling up the auto-increment value that way. This mostly works, though I am concerned that it is going to result in my code executing twice as many SQL statements, and there is the further danger that the saved row is not unique.
This method is not safe in multi-process environments at all (CGI, mod_perl, etc) as the INSERT followed by the SELECT is not an atomic action. I would stay away from this kludge as it will only get you in trouble down the road.
On the other hand, I wonder what kind of overhead already exists as the result of the mysql_insertid's availability. Is DBD::mysql already transparently doing something along these lines, or is it somehow optimized?
No, there is no extra SQL call by DBD::Mysql (as best I know without looking at the source that is). I believe that in the client library for MySQL (which DBD::MySQL uses) the 'last_insert_id' is a function/variable/environmental-parameter of some kind. So its actually optimized in MySQL itself.
*snip a bunch of stuff* This still leaves a bitter taste in my mouth, but at least it would avoid the superfluity of executing a second statement that wasn't going to be of any value.
That whole idea leaves a bitter taste in my mouth as well. Anything that is assuming you don't have duplicate data (aside from the primary key) will get you in trouble.
So, what I would really like to know... Does some better, less hackish way exist to accomplish that which I have described, or am I stuck dealing with it in some way along the lines of what I have delineated herein?
Subclassing, subclassing, subclassing. There is no such thing as truely "vanilla" SQL (at least not that I have seen and which is usable in a real world situation), so the idea of complete DBD (therefore database) agnosticism is a fantasy. There really exists only one elegant solution, which is to subclass.
Also, keep in mind that all DBDs are not alike. Just because the DBI specification says it should do "A", doesn't mean the driver author has to actually implement that (or even better, have time to implement it). You would be surprised how minimal a DBD driver can be, and still "work" with DBI.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Auto-Increment and DBD Agnosticism
by skyknight (Hermit) on Jun 23, 2004 at 20:41 UTC | |
by stvn (Monsignor) on Jun 23, 2004 at 23:58 UTC | |
by skyknight (Hermit) on Jun 24, 2004 at 13:12 UTC | |
by stvn (Monsignor) on Jun 24, 2004 at 14:15 UTC | |
by skyknight (Hermit) on Jun 24, 2004 at 15:14 UTC | |
| |
by skyknight (Hermit) on Jun 24, 2004 at 12:44 UTC | |
by stvn (Monsignor) on Jun 24, 2004 at 13:59 UTC | |
by skyknight (Hermit) on Jun 24, 2004 at 15:23 UTC | |
|