Skip to content

Commit

Permalink
Support returning uint64 from Valuer in ConvertValue (#1143)
Browse files Browse the repository at this point in the history
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:
  • Loading branch information
ikesyo committed Aug 13, 2020
1 parent 73dc904 commit 076901a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -79,6 +79,7 @@ Reed Allman <rdallman10 at gmail.com>
Richard Wilkes <wilkes at me.com>
Robert Russell <robert at rrbrussell.com>
Runrioter Wung <runrioter at gmail.com>
Sho Ikeda <suicaicoca at gmail.com>
Shuode Li <elemount at qq.com>
Simon J Mudd <sjmudd at pobox.com>
Soroush Pour <me at soroushjp.com>
Expand Down
12 changes: 9 additions & 3 deletions statement.go
Expand Up @@ -154,10 +154,16 @@ func (c converter) ConvertValue(v interface{}) (driver.Value, error) {
if err != nil {
return nil, err
}
if !driver.IsValue(sv) {
return nil, fmt.Errorf("non-Value type %T returned from Value", sv)
if driver.IsValue(sv) {
return sv, nil
}
return sv, nil
// A value returend from the Valuer interface can be "a type handled by
// a database driver's NamedValueChecker interface" so we should accept
// uint64 here as well.
if u, ok := sv.(uint64); ok {
return u, nil
}
return nil, fmt.Errorf("non-Value type %T returned from Value", sv)
}
rv := reflect.ValueOf(v)
switch rv.Kind() {
Expand Down
10 changes: 10 additions & 0 deletions statement_test.go
Expand Up @@ -10,6 +10,7 @@ package mysql

import (
"bytes"
"database/sql/driver"
"encoding/json"
"testing"
)
Expand Down Expand Up @@ -96,13 +97,22 @@ func TestConvertSignedIntegers(t *testing.T) {
}
}

type myUint64 struct {
value uint64
}

func (u myUint64) Value() (driver.Value, error) {
return u.value, nil
}

func TestConvertUnsignedIntegers(t *testing.T) {
values := []interface{}{
uint8(42),
uint16(42),
uint32(42),
uint64(42),
uint(42),
myUint64{uint64(42)},
}

for _, value := range values {
Expand Down

0 comments on commit 076901a

Please sign in to comment.