Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NamedValueChecker interface #1125

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions conn_go19.go
@@ -0,0 +1,35 @@
//go:build go1.9
// +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 compatibility with pq.Array().
return driver.ErrSkip
}

// Ignoring []byte / []uint8.
if _, ok := nv.Value.([]uint8); ok {
return driver.ErrSkip
}

v := reflect.ValueOf(nv.Value)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() == reflect.Slice {
var err error
nv.Value, err = Array(v.Interface()).Value()
return err
}

return driver.ErrSkip
}
83 changes: 83 additions & 0 deletions conn_go19_test.go
@@ -0,0 +1,83 @@
//go:build go1.9
// +build go1.9

package pq

import (
"fmt"
"reflect"
"testing"
)

func TestArrayArg(t *testing.T) {
db := openTestConn(t)
defer db.Close()

for _, tc := range []struct {
pgType string
in, out interface{}
}{
{
pgType: "int[]",
in: []int{245, 231},
out: []int64{245, 231},
},
{
pgType: "int[]",
in: &[]int{245, 231},
out: []int64{245, 231},
},
{
pgType: "int[]",
in: []int64{245, 231},
},
{
pgType: "int[]",
in: &[]int64{245, 231},
out: []int64{245, 231},
},
{
pgType: "varchar[]",
in: []string{"hello", "world"},
},
{
pgType: "varchar[]",
in: &[]string{"hello", "world"},
out: []string{"hello", "world"},
},
} {
if tc.out == nil {
tc.out = tc.in
}
t.Run(fmt.Sprintf("%#v", tc.in), func(t *testing.T) {
r, err := db.Query(fmt.Sprintf("SELECT $1::%s", tc.pgType), tc.in)
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")
}
}()

got := reflect.New(reflect.TypeOf(tc.out))
if err := r.Scan(Array(got.Interface())); err != nil {
tamird marked this conversation as resolved.
Show resolved Hide resolved
t.Fatal(err)
}

if !reflect.DeepEqual(tc.out, got.Elem().Interface()) {
t.Errorf("got %v, want %v", got, tc.out)
}
})
}

}