-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
Added in 1.9: https://go.dev/doc/go1.9#minor_library_changes. Close #640.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// +build go1.9 | ||
|
||
package pq | ||
|
||
import ( | ||
"database/sql/driver" | ||
"reflect" | ||
) | ||
|
||
var _ driver.NamedValueChecker = (*conn)(nil) | ||
|
||
func (c *conn) CheckNamedValue(nv *driver.NamedValue) error { | ||
if _, ok := nv.Value.(driver.Valuer); ok { | ||
// Ignore Valuer, for backward compatiblity with pq.Array() | ||
return driver.ErrSkip | ||
} | ||
|
||
// Ignoring []byte / []uint8 | ||
if _, ok := nv.Value.([]uint8); ok { | ||
return driver.ErrSkip | ||
} | ||
|
||
if k := reflect.ValueOf(nv.Value).Kind(); k == reflect.Array || k == reflect.Slice { | ||
var err error | ||
nv.Value, err = Array(nv.Value).Value() | ||
return err | ||
} | ||
|
||
return driver.ErrSkip | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// +build go1.9 | ||
|
||
package pq | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestArrayArg(t *testing.T) { | ||
db := openTestConn(t) | ||
defer db.Close() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
want interface{} | ||
}{ | ||
{ | ||
name: "array-value", | ||
want: [...]int64{245, 231}, | ||
}, | ||
{ | ||
name: "slice-value", | ||
want: []int64{245, 231}, | ||
}, | ||
{ | ||
name: "array-pointer", | ||
want: &[...]int64{245, 231}, | ||
}, | ||
{ | ||
name: "slice-pointer", | ||
want: &[]int64{245, 231}, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
r, err := db.Query("SELECT $1::int[]", Array(tc.want)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer r.Close() | ||
|
||
if !r.Next() { | ||
if r.Err() != nil { | ||
t.Fatal(r.Err()) | ||
} | ||
t.Fatal("expected row") | ||
} | ||
|
||
defer func() { | ||
if r.Next() { | ||
t.Fatal("unexpected row") | ||
} | ||
}() | ||
|
||
rt := reflect.TypeOf(tc.want) | ||
if rt.Kind() == reflect.Pointer { | ||
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (10, 1.17)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (10, 1.15)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (11, 1.17)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (10, 1.14)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (11, 1.14)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (12, 1.14)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (12, 1.17)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (12, 1.15)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (13, 1.15)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (13, 1.16)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (9.6, 1.14)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (12, 1.16)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (9.6, 1.16)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (11, 1.16)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (11, 1.15)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (9.6, 1.15)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (13, 1.17)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (13, 1.14)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (10, 1.16)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (9.6, 1.17)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (11, 1.15)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (10, 1.14)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (11, 1.16)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (11, 1.14)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (10, 1.15)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (12, 1.15)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (11, 1.17)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (12, 1.14)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (10, 1.16)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (12, 1.17)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (10, 1.17)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (13, 1.14)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (13, 1.16)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (12, 1.16)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (13, 1.17)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (13, 1.15)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (9.6, 1.14)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (9.6, 1.16)
Check failure on line 56 in conn_go19_test.go GitHub Actions / test (9.6, 1.15)
|
||
rt = rt.Elem() | ||
} | ||
|
||
got := reflect.New(rt).Interface() | ||
if err := r.Scan(Array(got)); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !reflect.DeepEqual(tc.want, got) { | ||
t.Errorf("got %v, want %v", got, tc.want) | ||
} | ||
}) | ||
} | ||
|
||
} |