Skip to content

Commit 97aa985

Browse files
authored
mysql: Fix GetInt() with negative text result (#972)
1 parent 35db8be commit 97aa985

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

mysql/resultset.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,45 @@ func (r *Resultset) GetUintByName(row int, name string) (uint64, error) {
185185
}
186186

187187
func (r *Resultset) GetInt(row, column int) (int64, error) {
188-
v, err := r.GetUint(row, column)
188+
d, err := r.GetValue(row, column)
189189
if err != nil {
190190
return 0, err
191191
}
192192

193-
return int64(v), nil
193+
switch v := d.(type) {
194+
case int:
195+
return int64(v), nil
196+
case int8:
197+
return int64(v), nil
198+
case int16:
199+
return int64(v), nil
200+
case int32:
201+
return int64(v), nil
202+
case int64:
203+
return v, nil
204+
case uint:
205+
return int64(v), nil
206+
case uint8:
207+
return int64(v), nil
208+
case uint16:
209+
return int64(v), nil
210+
case uint32:
211+
return int64(v), nil
212+
case uint64:
213+
return int64(v), nil
214+
case float32:
215+
return int64(v), nil
216+
case float64:
217+
return int64(v), nil
218+
case string:
219+
return strconv.ParseInt(v, 10, 64)
220+
case []byte:
221+
return strconv.ParseInt(string(v), 10, 64)
222+
case nil:
223+
return 0, nil
224+
default:
225+
return 0, errors.Errorf("data type is %T", v)
226+
}
194227
}
195228

196229
func (r *Resultset) GetIntByName(row int, name string) (int64, error) {

mysql/resultset_test.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
package mysql
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
48

59
func TestColumnNumber(t *testing.T) {
610
r := NewResultReserveResultset(0)
711
// Make sure ColumnNumber doesn't panic when constructing a Result with 0
812
// columns. https://github.com/go-mysql-org/go-mysql/issues/964
913
r.ColumnNumber()
1014
}
15+
16+
// TestGetInt tests GetInt with a negative value
17+
func TestGetIntNeg(t *testing.T) {
18+
r := NewResultset(1)
19+
fv := NewFieldValue(FieldValueTypeString, 0, []uint8("-193"))
20+
r.Values = [][]FieldValue{{fv}}
21+
v, err := r.GetInt(0, 0)
22+
require.NoError(t, err)
23+
require.Equal(t, int64(-193), v)
24+
}

0 commit comments

Comments
 (0)