Skip to content

Commit

Permalink
Add support for NamedValueChecker interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Apr 27, 2023
1 parent 2a217b9 commit 4d7b547
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions array.go
Expand Up @@ -20,6 +20,7 @@ var typeSQLScanner = reflect.TypeOf((*sql.Scanner)(nil)).Elem()
//
// For example:
// db.Query(`SELECT * FROM t WHERE id = ANY($1)`, pq.Array([]int{235, 401}))
// db.Query(`SELECT * FROM t WHERE id = ANY($1)`, []int{235, 401}) // go1.9+
//
// var x []sql.NullInt64
// db.QueryRow(`SELECT ARRAY[235, 401]`).Scan(pq.Array(&x))
Expand Down
30 changes: 30 additions & 0 deletions conn_go19.go
@@ -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
}
71 changes: 71 additions & 0 deletions conn_go19_test.go
@@ -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

View workflow job for this annotation

GitHub Actions / test (10, 1.17)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (10, 1.15)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (11, 1.17)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (10, 1.14)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (11, 1.14)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (12, 1.14)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (12, 1.17)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (12, 1.15)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (13, 1.15)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (13, 1.16)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (9.6, 1.14)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (12, 1.16)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (9.6, 1.16)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (11, 1.16)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (11, 1.15)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (9.6, 1.15)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (13, 1.17)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (13, 1.14)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (10, 1.16)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (9.6, 1.17)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (11, 1.15)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (10, 1.14)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (11, 1.16)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (11, 1.14)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (10, 1.15)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (12, 1.15)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (11, 1.17)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (12, 1.14)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (10, 1.16)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (12, 1.17)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (10, 1.17)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (13, 1.14)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (13, 1.16)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (12, 1.16)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (13, 1.17)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (13, 1.15)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (9.6, 1.14)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (9.6, 1.16)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (9.6, 1.15)

undefined: reflect.Pointer

Check failure on line 56 in conn_go19_test.go

View workflow job for this annotation

GitHub Actions / test (9.6, 1.17)

undefined: reflect.Pointer
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)
}
})
}

}

0 comments on commit 4d7b547

Please sign in to comment.