Skip to content

mysql: Fix GetInt() with negative NEWDECIMAL result #972

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

Merged
merged 2 commits into from
Jan 20, 2025
Merged
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
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)
}
Loading