Skip to content

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

Closed
@mherr-google

Description

@mherr-google

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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions