Skip to content

Commit

Permalink
mysql: Fix GetInt() with negative text result
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed Jan 19, 2025
1 parent d02e79a commit b2e3ad1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
37 changes: 35 additions & 2 deletions mysql/resultset.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,45 @@ func (r *Resultset) GetUintByName(row int, name string) (uint64, error) {
}

func (r *Resultset) GetInt(row, column int) (int64, error) {
v, err := r.GetUint(row, column)
d, err := r.GetValue(row, column)
if err != nil {
return 0, err
}

return int64(v), nil
switch v := d.(type) {
case int:
return int64(v), nil
case int8:
return int64(v), nil
case int16:
return int64(v), nil
case int32:
return int64(v), nil
case int64:
return v, nil
case uint:
return int64(v), nil
case uint8:
return int64(v), nil
case uint16:
return int64(v), nil
case uint32:
return int64(v), nil
case uint64:
return int64(v), nil
case float32:
return int64(v), nil
case float64:
return int64(v), nil
case string:
return strconv.ParseInt(v, 10, 64)
case []byte:
return strconv.ParseInt(string(v), 10, 64)
case nil:
return 0, nil
default:
return 0, errors.Errorf("data type is %T", v)
}
}

func (r *Resultset) GetIntByName(row int, name string) (int64, error) {
Expand Down
16 changes: 15 additions & 1 deletion mysql/resultset_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
package mysql

import "testing"
import (
"testing"

"github.com/stretchr/testify/require"
)

func TestColumnNumber(t *testing.T) {
r := NewResultReserveResultset(0)
// Make sure ColumnNumber doesn't panic when constructing a Result with 0
// columns. https://github.com/go-mysql-org/go-mysql/issues/964
r.ColumnNumber()
}

// TestGetInt tests GetInt with a negative value
func TestGetIntNeg(t *testing.T) {
r := NewResultset(1)
fv := NewFieldValue(FieldValueTypeString, 0, []uint8("-193"))
r.Values = [][]FieldValue{{fv}}
v, err := r.GetInt(0, 0)
require.NoError(t, err)
require.Equal(t, int64(-193), v)
}

0 comments on commit b2e3ad1

Please sign in to comment.