Skip to content

Commit f29bef1

Browse files
authored
Merge pull request #1083 from ydb-platform/named-scanner
added implementation of named scanner
2 parents d909cd1 + d1acec4 commit f29bef1

File tree

3 files changed

+788
-1
lines changed

3 files changed

+788
-1
lines changed

internal/query/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
var (
88
ErrNotImplemented = errors.New("not implemented yet")
99
errWrongNextResultSetIndex = errors.New("wrong result set index")
10+
errColumnNotFoundByName = errors.New("column not found by name")
1011
errInterruptedStream = errors.New("interrupted stream")
1112
errClosedResult = errors.New("result closed early")
1213
errWrongResultSetIndex = errors.New("critical violation of the logic - wrong result set index")

internal/query/scanner_named.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package query
22

33
import (
4+
"fmt"
5+
"reflect"
6+
7+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
48
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
59
"github.com/ydb-platform/ydb-go-sdk/v3/query"
610
)
@@ -17,6 +21,45 @@ func newScannerNamed(data *scannerData) scannerNamed {
1721
}
1822
}
1923

24+
func (s scannerNamed) seekByName(name string) (
25+
value.Value,
26+
error,
27+
) {
28+
for i := range s.data.columns {
29+
if s.data.columns[i].GetName() == name {
30+
return value.FromYDB(s.data.columns[i].GetType(), s.data.values[i]), nil
31+
}
32+
}
33+
34+
return nil, xerrors.WithStackTrace(fmt.Errorf("'%s': %w", name, errColumnNotFoundByName))
35+
}
36+
2037
func (s scannerNamed) ScanNamed(dst ...query.NamedDestination) error {
21-
return xerrors.WithStackTrace(ErrNotImplemented)
38+
if len(dst) != len(s.data.columns) {
39+
return xerrors.WithStackTrace(errWrongArgumentsCount)
40+
}
41+
42+
for i := range dst {
43+
v, err := s.seekByName(dst[i].Name())
44+
if err != nil {
45+
return xerrors.WithStackTrace(err)
46+
}
47+
if err := value.CastTo(v, dst[i].Destination()); err != nil {
48+
to := reflect.ValueOf(dst[i].Destination())
49+
if to.Kind() != reflect.Pointer {
50+
return xerrors.WithStackTrace(
51+
fmt.Errorf("dst[%d].Destination() type is not a pointer ('%s')", i,
52+
to.Kind().String(),
53+
))
54+
}
55+
vv := reflect.ValueOf(v)
56+
if vv.CanConvert(to.Elem().Type()) {
57+
to.Elem().Set(vv.Convert(to.Elem().Type()))
58+
} else {
59+
return xerrors.WithStackTrace(err)
60+
}
61+
}
62+
}
63+
64+
return nil
2265
}

0 commit comments

Comments
 (0)