Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Return integer and floating-point values as int64 and float64, not []…

…byte
  • Loading branch information...
commit cf316d2848d70cd3898e169ff17217dc16134af2 1 parent 2ed1d69
Andy Balholm authored
Showing with 30 additions and 3 deletions.
  1. +13 −3 conn_test.go
  2. +17 −0 encode.go
16 conn_test.go
View
@@ -150,7 +150,9 @@ func TestEncodeDecode(t *testing.T) {
'foobar'::text,
NULL::integer,
'2000-1-1 01:02:03.04-7'::timestamptz,
- 0::boolean
+ 0::boolean,
+ 123,
+ 3.14::float8
WHERE
'\x000102'::bytea = $1
AND 'foobar'::text = $2
@@ -178,9 +180,9 @@ func TestEncodeDecode(t *testing.T) {
var got2 string
var got3 = sql.NullInt64{Valid: true}
var got4 time.Time
- var got5 interface{}
+ var got5, got6, got7 interface{}
- err = r.Scan(&got1, &got2, &got3, &got4, &got5)
+ err = r.Scan(&got1, &got2, &got3, &got4, &got5, &got6, &got7)
if err != nil {
t.Fatal(err)
}
@@ -204,6 +206,14 @@ func TestEncodeDecode(t *testing.T) {
if got5 != false {
t.Fatalf("expected false, got %q", got5)
}
+
+ if got6 != int64(123) {
+ t.Fatalf("expected 123, got %d", got6)
+ }
+
+ if got7 != float64(3.14) {
+ t.Fatalf("expected 3.14, got %f", got7)
+ }
}
func TestNoData(t *testing.T) {
17 encode.go
View
@@ -4,6 +4,7 @@ import (
"database/sql/driver"
"encoding/hex"
"fmt"
+ "strconv"
"time"
)
@@ -52,6 +53,22 @@ func decode(s []byte, typ int) interface{} {
return mustParse("2006-01-02", s)
case t_bool:
return s[0] == 't'
+ case t_int8, t_int2, t_int4:
+ i, err := strconv.ParseInt(string(s), 10, 64)
+ if err != nil {
+ errorf("%s", err)
+ }
+ return i
+ case t_float4, t_float8:
+ bits := 64
+ if typ == t_float4 {
+ bits = 32
+ }
+ f, err := strconv.ParseFloat(string(s), bits)
+ if err != nil {
+ errorf("%s", err)
+ }
+ return f
}
return s
Please sign in to comment.
Something went wrong with that request. Please try again.