Skip to content

Commit 076901a

Browse files
authored
Support returning uint64 from Valuer in ConvertValue (#1143)
https://golang.org/pkg/database/sql/driver/#Value says: > Value is a value that drivers must be able to handle. It is either nil, a type handled by a database driver's NamedValueChecker interface, or an instance of one of these types:
1 parent 73dc904 commit 076901a

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Reed Allman <rdallman10 at gmail.com>
7979
Richard Wilkes <wilkes at me.com>
8080
Robert Russell <robert at rrbrussell.com>
8181
Runrioter Wung <runrioter at gmail.com>
82+
Sho Ikeda <suicaicoca at gmail.com>
8283
Shuode Li <elemount at qq.com>
8384
Simon J Mudd <sjmudd at pobox.com>
8485
Soroush Pour <me at soroushjp.com>

statement.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,16 @@ func (c converter) ConvertValue(v interface{}) (driver.Value, error) {
154154
if err != nil {
155155
return nil, err
156156
}
157-
if !driver.IsValue(sv) {
158-
return nil, fmt.Errorf("non-Value type %T returned from Value", sv)
157+
if driver.IsValue(sv) {
158+
return sv, nil
159159
}
160-
return sv, nil
160+
// A value returend from the Valuer interface can be "a type handled by
161+
// a database driver's NamedValueChecker interface" so we should accept
162+
// uint64 here as well.
163+
if u, ok := sv.(uint64); ok {
164+
return u, nil
165+
}
166+
return nil, fmt.Errorf("non-Value type %T returned from Value", sv)
161167
}
162168
rv := reflect.ValueOf(v)
163169
switch rv.Kind() {

statement_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package mysql
1010

1111
import (
1212
"bytes"
13+
"database/sql/driver"
1314
"encoding/json"
1415
"testing"
1516
)
@@ -96,13 +97,22 @@ func TestConvertSignedIntegers(t *testing.T) {
9697
}
9798
}
9899

100+
type myUint64 struct {
101+
value uint64
102+
}
103+
104+
func (u myUint64) Value() (driver.Value, error) {
105+
return u.value, nil
106+
}
107+
99108
func TestConvertUnsignedIntegers(t *testing.T) {
100109
values := []interface{}{
101110
uint8(42),
102111
uint16(42),
103112
uint32(42),
104113
uint64(42),
105114
uint(42),
115+
myUint64{uint64(42)},
106116
}
107117

108118
for _, value := range values {

0 commit comments

Comments
 (0)