Skip to content

Commit 62b3351

Browse files
authored
Merge pull request #1591 from 4el0ve4ek/fix_sql_driver_Valuer
fix sql/driver.Valuer interface usage
2 parents cb3bf6b + fb2182e commit 62b3351

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Supported of `database/sql/driver.Valuer` interfaces for params which passed to query using sql driver
12
* Exposed `credentials/credentials.OAuth2Config` OAuth2 config
23

34
## v3.95.2

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"
@@ -451,3 +452,11 @@ func TestUUIDSerializationDatabaseSQLIssue1501(t *testing.T) {
451452
require.Equal(t, id.String(), res.String())
452453
})
453454
}
455+
456+
type testValuer struct {
457+
value driver.Value
458+
}
459+
460+
func (v *testValuer) Value() (driver.Value, error) {
461+
return v.value, nil
462+
}

0 commit comments

Comments
 (0)