Skip to content

Commit d3a0b0f

Browse files
darrenmethane
authored andcommitted
pass unsigned int without converting it to string (go-sql-driver#838)
1 parent 8f4b98d commit d3a0b0f

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Hajime Nakagami <nakagami at gmail.com>
3535
Hanno Braun <mail at hannobraun.com>
3636
Henri Yandell <flamefew at gmail.com>
3737
Hirotaka Yamamoto <ymmt2005 at gmail.com>
38+
Huyiguang <hyg at webterren.com>
3839
ICHINOSE Shogo <shogo82148 at gmail.com>
3940
Ilia Cimpoes <ichimpoesh at gmail.com>
4041
INADA Naoki <songofacandy at gmail.com>

connection_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func TestCheckNamedValue(t *testing.T) {
7878
t.Fatal("uint64 high-bit not convertible", err)
7979
}
8080

81-
if value.Value != "18446744073709551615" {
82-
t.Fatalf("uint64 high-bit not converted, got %#v %T", value.Value, value.Value)
81+
if value.Value != ^uint64(0) {
82+
t.Fatalf("uint64 high-bit converted, got %#v %T", value.Value, value.Value)
8383
}
8484
}
8585

packets.go

+16
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,22 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
10111011
)
10121012
}
10131013

1014+
case uint64:
1015+
paramTypes[i+i] = byte(fieldTypeLongLong)
1016+
paramTypes[i+i+1] = 0x80 // type is unsigned
1017+
1018+
if cap(paramValues)-len(paramValues)-8 >= 0 {
1019+
paramValues = paramValues[:len(paramValues)+8]
1020+
binary.LittleEndian.PutUint64(
1021+
paramValues[len(paramValues)-8:],
1022+
uint64(v),
1023+
)
1024+
} else {
1025+
paramValues = append(paramValues,
1026+
uint64ToBytes(uint64(v))...,
1027+
)
1028+
}
1029+
10141030
case float64:
10151031
paramTypes[i+i] = byte(fieldTypeDouble)
10161032
paramTypes[i+i+1] = 0x00

statement.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"fmt"
1414
"io"
1515
"reflect"
16-
"strconv"
1716
)
1817

1918
type mysqlStmt struct {
@@ -164,14 +163,8 @@ func (c converter) ConvertValue(v interface{}) (driver.Value, error) {
164163
}
165164
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
166165
return rv.Int(), nil
167-
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
168-
return int64(rv.Uint()), nil
169-
case reflect.Uint64:
170-
u64 := rv.Uint()
171-
if u64 >= 1<<63 {
172-
return strconv.FormatUint(u64, 10), nil
173-
}
174-
return int64(u64), nil
166+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
167+
return rv.Uint(), nil
175168
case reflect.Float32, reflect.Float64:
176169
return rv.Float(), nil
177170
case reflect.Bool:

statement_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestConvertUnsignedIntegers(t *testing.T) {
110110
t.Fatalf("%T type not convertible %s", value, err)
111111
}
112112

113-
if output != int64(42) {
113+
if output != uint64(42) {
114114
t.Fatalf("%T type not converted, got %#v %T", value, output, output)
115115
}
116116
}
@@ -120,7 +120,7 @@ func TestConvertUnsignedIntegers(t *testing.T) {
120120
t.Fatal("uint64 high-bit not convertible", err)
121121
}
122122

123-
if output != "18446744073709551615" {
124-
t.Fatalf("uint64 high-bit not converted, got %#v %T", output, output)
123+
if output != ^uint64(0) {
124+
t.Fatalf("uint64 high-bit converted, got %#v %T", output, output)
125125
}
126126
}

0 commit comments

Comments
 (0)