Skip to content

Commit f80c9fa

Browse files
committed
fix sql/driver.Valuer interface usage
1 parent 9a4b4f8 commit f80c9fa

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Fix - returned support of sql/driver.Valuer interfaces for params which passed to query using sql driver
2+
13
## v3.94.0
24
* Refactored golang types mapping into ydb types using `ydb.ParamsFromMap` and `database/sql` query arguments
35
* Small breaking change: type mapping for `ydb.ParamsFromMap` and `database/sql` type `uuid.UUID` changed from ydb type `Text` to ydb type `UUID`

internal/bind/params.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,17 @@ func toValue(v interface{}) (_ value.Value, err error) {
161161
return x, nil
162162
}
163163

164+
if valuer, ok := v.(driver.Valuer); ok {
165+
v, err = valuer.Value()
166+
if err != nil {
167+
return nil, fmt.Errorf("ydb: driver.Valuer error: %w", err)
168+
}
169+
}
170+
171+
if x, ok := asUUID(v); ok {
172+
return x, nil
173+
}
174+
164175
switch x := v.(type) {
165176
case nil:
166177
return value.VoidValue(), nil
@@ -337,16 +348,17 @@ func supportNewTypeLink(x interface{}) string {
337348
}
338349

339350
func toYdbParam(name string, value interface{}) (*params.Parameter, error) {
340-
if na, ok := value.(driver.NamedValue); ok {
341-
n, v := na.Name, na.Value
351+
switch tv := value.(type) {
352+
case driver.NamedValue:
353+
n, v := tv.Name, tv.Value
342354
if n != "" {
343355
name = n
344356
}
345357
value = v
358+
case *params.Parameter:
359+
return tv, nil
346360
}
347-
if v, ok := value.(*params.Parameter); ok {
348-
return v, nil
349-
}
361+
350362
v, err := toValue(value)
351363
if err != nil {
352364
return nil, xerrors.WithStackTrace(err)

internal/bind/params_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ import (
1717
"github.com/ydb-platform/ydb-go-sdk/v3/table"
1818
)
1919

20+
type testValuer struct {
21+
value driver.Value
22+
}
23+
24+
func (v testValuer) Value() (driver.Value, error) {
25+
return v.value, nil
26+
}
27+
2028
func TestToValue(t *testing.T) {
2129
for _, tt := range []struct {
2230
name string
@@ -601,6 +609,24 @@ func TestToValue(t *testing.T) {
601609
dst: nil,
602610
err: value.ErrIssue1501BadUUID,
603611
},
612+
{
613+
name: xtest.CurrentFileLine(),
614+
src: testValuer{value: "1234567890"},
615+
dst: value.TextValue("1234567890"),
616+
err: nil,
617+
},
618+
{
619+
name: xtest.CurrentFileLine(),
620+
src: testValuer{value: uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}},
621+
dst: value.Uuid(uuid.UUID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}),
622+
err: nil,
623+
},
624+
{
625+
name: xtest.CurrentFileLine(),
626+
src: testValuer{value: func() *string { return nil }()},
627+
dst: value.NullValue(types.Text),
628+
err: nil,
629+
},
604630
} {
605631
t.Run(tt.name, func(t *testing.T) {
606632
dst, err := toValue(tt.src)

tests/integration/database_sql_regression_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package integration
66
import (
77
"context"
88
"database/sql"
9+
"database/sql/driver"
910
"errors"
1011
"fmt"
1112
"math/rand"
@@ -435,3 +436,11 @@ func TestUUIDSerializationDatabaseSQLIssue1501(t *testing.T) {
435436
require.Equal(t, id.String(), res.String())
436437
})
437438
}
439+
440+
type testValuer struct {
441+
value driver.Value
442+
}
443+
444+
func (v *testValuer) Value() (driver.Value, error) {
445+
return v.value, nil
446+
}

0 commit comments

Comments
 (0)