Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ type mysqlResult struct {
}

func (res *mysqlResult) LastInsertId() (int64, error) {
if len(res.insertIds) == 0 {
return 0, nil
}
return res.insertIds[len(res.insertIds)-1], nil
}

func (res *mysqlResult) RowsAffected() (int64, error) {
if len(res.affectedRows) == 0 {
return 0, nil
}
return res.affectedRows[len(res.affectedRows)-1], nil
}

Expand Down
22 changes: 22 additions & 0 deletions result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
//
// Copyright 2026 The Go-MySQL-Driver Authors. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

package mysql

import "testing"

// Regression for #1733: indexing into an empty result slice used to panic.
func TestEmptyResult(t *testing.T) {
r := &mysqlResult{}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide reproducible steps for users.

if id, err := r.LastInsertId(); err != nil || id != 0 {
t.Errorf("LastInsertId() = (%d, %v), want (0, nil)", id, err)
}
if rows, err := r.RowsAffected(); err != nil || rows != 0 {
t.Errorf("RowsAffected() = (%d, %v), want (0, nil)", rows, err)
}
}
Loading