-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
abstraction for obtaining last insert id #47
Comments
Hm, yeah that is a tough one. Especially when you consider that (unless you use transactions), there are no guarantees that two queries will even use the same connection (they are executed against an any-db connection pool). I've had a prototype for an extension system that allows engines to patch (and unpatch) query objects at runtime, this might be an appropriate use case for it. Would you be interested in working on a pull request that adds simulated 'returning' support to the MySQL and SQLite3 drivers? |
Hmm. Didn't consider the pooling of connections, that certainly complicates things. I was mostly trying to figure out if you would be interested in this feature, so yes, I'm happy to look into the simulated Thanks! |
I've just pushed up the engine-extensions branch with a (non-working) attempt at adding this functionality. Feel free to completely throw out my implementation, but it might serve as inspiration. |
Thanks, I'll play with this code and try to complete it. |
Plumbing the last ID for sqlite3 is going to be a challenge. The only node-sqlite3 function that returns the last insert id is Do you have any situations where any-db needs to look at the query string to know how to execute it? |
Currently there isn't, but there's no hard rule against it, the only thing any-db shouldn't do is modify the query text/params being sent to the backend. I'd definitely accept a pull request against any-db that makes use of |
If you have a moment please take a look at this commit on my forked any-db and let me know if something like this works for you. This would also be fairly easy to add to MySQL, though it's going to be harder for postgres. |
I commented on the commit, looks good!
I don't think any-db needs to add a workaround for Postgres doesn't include generated ids in normal query results, as |
Great, thanks for looking at it. I'll implement MySQL as well, since it is equally easy. Regarding Postgres, I agree it isn't necessary and will probably not worry about it, but it would have been nice to have all drivers return the same response, because then you don't need driver specific extensions in this project. |
Ah, that's a noble goal, but I want to support driver specific extensions in this project anyways; there's a number of SQL extensions that would be useful to support in the query building API. (The extensions system actually started as a way to add support for Postgres' common table expressions). |
It is unfortunate that different database engines implement the "get_last_insert_id()" function in very different ways. The only engine that currently supports this with the query generation API is Postgresql, via the
returning()
clause. For the others it is necessary to send a hardcoded SQL query, because it seems these cannot be generated via theselect()
function.Would you consider it within the scope of your project to create an engine agnostic function to retrieve this id?
I could come up with two possible implementations:
returning()
clause. Postgresql already has it, sqlite3 and MySQL will need this clause simulated with aSELECT
query.Let me know what you think!
The text was updated successfully, but these errors were encountered: