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

easier way to get number of rows in DELETE #264

Closed
kevinburke1 opened this issue Jan 12, 2020 · 3 comments · Fixed by #270
Closed

easier way to get number of rows in DELETE #264

kevinburke1 opened this issue Jan 12, 2020 · 3 comments · Fixed by #270
Labels
documentation Improvements or additions to documentation

Comments

@kevinburke1
Copy link
Contributor

If I issue a DELETE it would be nice to know whether 0, 1, or more rows were deleted, in an easy way.

I tried doing this, FYI

DELETE FROM queued_jobs
WHERE id = $1
RETURNING count(id) as rows_deleted;

And got this at runtime (not compile time)

pq: aggregate functions are not allowed in RETURNING

@eullerpereira94
Copy link
Contributor

Well this is a issue outside of sqlc, but that can be addressed trough code generation.
The Exec() method of the sql.DB type, from the database/sql package, returns two values: an sql.Result interface and an error.
The sql.Result interface has two methods: LastInsertId(), that is self-explanatory, and RowsAffected(), that returns the amount of rows that were inserted, updated, or deleted, and this is what you want.
My proposed solution is to have all generated methods that were marked with an exec tag in the generating sql script to also return a sql.Result in addition to the error.
If you can't wait for a change in the library like this, you can go use this Stack Overflow answer to solve your problem.

@eullerpereira94
Copy link
Contributor

Reading better the code, I've noticed that there is a execrows tag. After testing a little, this does provides the functionality you want.

@kevinburke1
Copy link
Contributor Author

Ah, forgot about one. Maybe the issue should be better documentation of what the meta fields do.

@kyleconroy kyleconroy added the documentation Improvements or additions to documentation label Jan 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants