Skip to content
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

MySQL/SQLite: support returning LastInsertId #1459

Closed
housingdreams opened this issue Mar 1, 2022 · 4 comments
Closed

MySQL/SQLite: support returning LastInsertId #1459

housingdreams opened this issue Mar 1, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@housingdreams
Copy link

housingdreams commented Mar 1, 2022

:exec only checks the error
:execresult returns the full sql.Result
:execrows returns the sql.Result.RowsAffected() (int64, error)

We just need one more return type to support the full interface:

:execlastinsertid would just be a matter of calling .LastInsertId() rather than .RowsAffected() on the sql.Result.

This will avoid sql.Result database information leaking across the boundary of the pkg.Querier interface. It's not good for the service code to be handling DB specific method calls. This is a MySQL/SQLite issue as Postgres uses RETURNING so you can use :one instead.

Solution: duplicate the template code used for :execrows

@housingdreams housingdreams changed the title MySQL support returning LastInsertId MySQL/SQL: support returning LastInsertId Mar 1, 2022
@housingdreams housingdreams changed the title MySQL/SQL: support returning LastInsertId MySQL/SQLite: support returning LastInsertId Mar 1, 2022
@lpintes
Copy link

lpintes commented Mar 9, 2022

Recent versions of MariaDB supports returning clause in insert statement. So perhaps fixing parser could be sufficient.

@housingdreams
Copy link
Author

Unfortunately, there are a lot of companies still running older MySQL installs. In addition, using sqlc with products like TiDB or https://vitess.io/ would also require this.

@Jille
Copy link
Contributor

Jille commented Mar 16, 2022

(I requested this before in #1200. I agree it would be nicer to have a :execinsertid)

@kyleconroy kyleconroy added the enhancement New feature or request label Mar 19, 2022
@kyleconroy
Copy link
Collaborator

This has been added as :execlastid and will ship in 1.14.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants