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

Exec() now supports reporting results for multiple statements in a request (#1261) #1263

Closed
wants to merge 0 commits into from

Conversation

mherr-google
Copy link
Contributor

@mherr-google mherr-google commented Sep 1, 2021

Description

Exec() now provides access to the last inserted ID and number of affected rows for all statements, not just the last one. This is useful to execute batches of statements such as UPDATE with minimal roundtrips. Fixes #1261.

The approach taken is to track last insert id and affected rows using []int64 instead of a int64. Both are set in mysqlResult, and a new mysql.Result interface makes them accessible to callers calling Exec() via sql.Conn.Raw. For example:

conn.Raw(func(conn interface{}) error {
        ex := conn.(driver.Execer)
        res, err := ex.Exec(`
        UPDATE point SET x = 1 WHERE y = 2;
        UPDATE point SET x = 2 WHERE y = 3;
        `, nil)
        // Both slices have 2 elements.
        log.Print(res.(mysql.Result).AllRowsAffected())
        log.Print(res.(mysql.Result).AllLastInsertIds())
      })

Checklist

  • Code compiles correctly
  • Created tests which fail without the change (if possible)
  • All tests passing
  • Extended the README / documentation, if necessary
  • Added myself / the copyright holder to the AUTHORS file

@mherr-google mherr-google marked this pull request as ready for review September 1, 2021 03:28
@mherr-google mherr-google changed the title Exec() now supports reporting results for multiple statements in a request (#1261) Exec() now supports reporting results for multiple statements in a request (fixes #1261) Sep 1, 2021
@mherr-google mherr-google changed the title Exec() now supports reporting results for multiple statements in a request (fixes #1261) Exec() now supports reporting results for multiple statements in a request (#1261) Sep 1, 2021
@aforrest-google
Copy link

There is a memory loitering issue associated with this change. Details in this closed issue: github.com//issues/1304.

I have a fix that should join this PR before it is merged. mherr-google@ please contact me.

@mherr-google
Copy link
Contributor Author

The memory loitering issue described in #1304 is fixed, this is now ready for review.

@mherr-google
Copy link
Contributor Author

This is superceded by #1309, which is the final version of the change being used internally (revised to address memory loitering).

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

Successfully merging this pull request may close these issues.

Add support for accessing LastInsertId and RowsAffected for all statements in a batch
2 participants