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

Add support for accessing LastInsertId and RowsAffected for all statements in a batch #1261

Closed
mherr-google opened this issue Aug 31, 2021 · 4 comments
Milestone

Comments

@mherr-google
Copy link
Contributor

Issue description

When calling Exec() with multiple statements, we'd like to be able to access LastInsertId and RowsAffected for all statements, not just the last one.

The motivating use case is performing batches of UPDATE statements with minimal roundtrips, while determining the result of each separately.

Example code

My PR to implement this exposes additional methods on mysqlResult, which is accessible when querying with sql.Conn.Raw() using a new mysql.Result interface:

      conn, _ := db.Conn(ctx)
      conn.Raw(func(conn interface{}) error {
        ex := conn.(driver.Execer)
        res, _ := ex.Exec(`
        UPDATE point SET x = 1 WHERE y = 2;
        UPDATE point SET x = 2 WHERE y = 3;
        `, nil)
        log.Print(res.(mysql.Result).AllRowsAffected()) // eg. {5, 10}
     })

It looks like exposing the same functionality for sql.Stmt isn't possible yet, since it has no equivalent to sql.Conn.Raw().

@mherr-google
Copy link
Contributor Author

I have a PR for this, will mail shortly.

@jaysonhurd
Copy link

I am also having the same issue with v1.6.0. Both RowsAffected() and LastInsertId() return 0 even if the update statement succeeds. Is there any workaround for this?

@gbenroscience
Copy link

I am also having the same issue with v1.6.0. Both RowsAffected() and LastInsertId() return 0 even if the update statement succeeds. Is there any workaround for this?

I just observed this issue now in RowsAffected() v1.6.0 also. In my own case, it occurred on a PreparedStatement, and the statements were not batched

@methane methane added this to the v1.8.0 milestone May 24, 2023
@methane
Copy link
Member

methane commented Jun 17, 2023

Fixed by #1309

@methane methane closed this as completed Jun 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants