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

support custom types in query result scanning #1599

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Added support of custom types to row.ScanStruct using sql.Scanner interface

## v3.104.5
* Added query client session pool metrics: create_in_progress, in_use, waiters_queue
* Added pool item closing for not-alived item
Expand Down
22 changes: 22 additions & 0 deletions internal/value/cast.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package value

import (
"database/sql"
"database/sql/driver"

"github.com/google/uuid"
)

func CastTo(v Value, dst interface{}) error {
if dst == nil {
return errNilDestination
Expand All @@ -10,5 +17,20 @@ func CastTo(v Value, dst interface{}) error {
return nil
}

if _, ok := dst.(*uuid.UUID); ok {
Copy link
Member

@rekby rekby Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note for me - need check uuid behaviour to #1501

and is the pre solve issue: #1515

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this crutch, because uuid.UUID implements sql.Scanner, but in here we store object with type uuid.UUID as driver.Value, which is unexpected due to go-docs.

If you have suggestions how to fix it, i can make them)

return v.castTo(dst)
}

if scanner, has := dst.(sql.Scanner); has {
dv := new(driver.Value)

err := v.castTo(dv)
if err != nil {
return err
}

return scanner.Scan(*dv)
}

return v.castTo(dst)
}
21 changes: 21 additions & 0 deletions internal/value/cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package value

import (
"database/sql/driver"
"errors"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -32,6 +33,19 @@ func loadLocation(t *testing.T, name string) *time.Location {
return loc
}

type testStringSQLScanner string

func (s *testStringSQLScanner) Scan(value any) error {
ts, ok := value.(string)
if !ok {
return errors.New("can't cast from " + reflect.TypeOf(value).String() + " to string")
}

*s = testStringSQLScanner(ts)

return nil
}

func TestCastTo(t *testing.T) {
testsCases := []struct {
name string
Expand Down Expand Up @@ -428,6 +442,13 @@ func TestCastTo(t *testing.T) {
exp: DateValueFromTime(time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC)),
err: nil,
},
{
name: xtest.CurrentFileLine(),
value: TextValue("text-string"),
dst: ptr[testStringSQLScanner](),
exp: testStringSQLScanner("text-string"),
err: nil,
},
}
for _, tt := range testsCases {
t.Run(tt.name, func(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions internal/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,7 @@ func (v *listValue) castTo(dst any) error {
inner.Set(newSlice)

for i, item := range v.ListItems() {
if err := item.castTo(inner.Index(i).Addr().Interface()); err != nil {
if err := CastTo(item, inner.Index(i).Addr().Interface()); err != nil {
return xerrors.WithStackTrace(fmt.Errorf(
"%w '%s(%+v)' to '%T' destination",
ErrCannotCast, v.Type().Yql(), v, dstValue,
Expand Down Expand Up @@ -1649,7 +1649,7 @@ func (v *setValue) castTo(dst any) error {
inner.Set(newSlice)

for i, item := range v.items {
if err := item.castTo(inner.Index(i).Addr().Interface()); err != nil {
if err := CastTo(item, inner.Index(i).Addr().Interface()); err != nil {
return xerrors.WithStackTrace(fmt.Errorf(
"%w '%s(%+v)' to '%T' destination",
ErrCannotCast, v.Type().Yql(), v, dstValue,
Expand Down Expand Up @@ -1757,7 +1757,7 @@ func (v *optionalValue) castTo(dst any) error {
return nil
}

if err := v.value.castTo(ptr.Interface()); err != nil {
if err := CastTo(v.value, (ptr.Interface())); err != nil {
return xerrors.WithStackTrace(err)
}

Expand All @@ -1772,7 +1772,7 @@ func (v *optionalValue) castTo(dst any) error {

inner.Set(reflect.New(inner.Type().Elem()))

if err := v.value.castTo(inner.Interface()); err != nil {
if err := CastTo(v.value, inner.Interface()); err != nil {
return xerrors.WithStackTrace(err)
}

Expand Down Expand Up @@ -1853,7 +1853,7 @@ func (v *structValue) castTo(dst any) error {
}

for i, field := range v.fields {
if err := field.V.castTo(inner.Field(i).Addr().Interface()); err != nil {
if err := CastTo(field.V, inner.Field(i).Addr().Interface()); err != nil {
return xerrors.WithStackTrace(fmt.Errorf(
"scan error on struct field name '%s': %w",
field.Name, err,
Expand Down Expand Up @@ -2031,7 +2031,7 @@ func (v *tupleValue) TupleItems() []Value {

func (v *tupleValue) castTo(dst any) error {
if len(v.items) == 1 {
return v.items[0].castTo(dst)
return CastTo(v.items[0], dst)
}

switch dstValue := dst.(type) {
Expand Down
Loading