Skip to content

Commit 7108b1d

Browse files
dveedenlance6716
andauthored
mysql: Fix ColumnNumber() panic with nil Resultset (#968)
* mysql: Fix ColumnNumber() panic with nil Resultset * Add more comments * Fix comments --------- Co-authored-by: lance6716 <[email protected]>
1 parent 14069d3 commit 7108b1d

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

mysql/resultset.go

+9
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,23 @@ func (r *Resultset) Reset(fieldsCount int) {
7979
}
8080
}
8181

82+
// RowNumber is returning the number of rows in the [Resultset].
83+
//
84+
// For a nil [Resultset] 0 is returned.
8285
func (r *Resultset) RowNumber() int {
8386
if r == nil {
8487
return 0
8588
}
8689
return len(r.Values)
8790
}
8891

92+
// ColumnNumber is returning the number of fields in the [Resultset].
93+
//
94+
// For a nil [Resultset] 0 is returned.
8995
func (r *Resultset) ColumnNumber() int {
96+
if r == nil {
97+
return 0
98+
}
9099
return len(r.Fields)
91100
}
92101

mysql/resultset_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package mysql
2+
3+
import "testing"
4+
5+
func TestColumnNumber(t *testing.T) {
6+
r := Result{}
7+
// Make sure ColumnNumber doesn't panic if ResultSet is nil
8+
// https://github.com/go-mysql-org/go-mysql/issues/964
9+
r.ColumnNumber()
10+
}

0 commit comments

Comments
 (0)