1+ //go:build go1.23
2+
13package ydb_test
24
35import (
46 "context"
57 "database/sql"
6- "errors"
78 "fmt"
89 "io"
910 "log"
@@ -28,61 +29,45 @@ func Example_query() {
2829 ctx := context .TODO ()
2930 db , err := ydb .Open (ctx , "grpc://localhost:2136/local" )
3031 if err != nil {
31- log . Fatal (err )
32+ panic (err )
3233 }
3334 defer db .Close (ctx ) // cleanup resources
3435
35- err = db .Query ().Do ( // Do retry operation on errors with best effort
36+ materializedResult , err : = db .Query ().Query ( // Do retry operation on errors with best effort
3637 ctx , // context manage exiting from Do
37- func (ctx context.Context , s query.Session ) (err error ) { // retry operation
38- streamResult , err := s .Query (ctx ,
39- `SELECT $id as myId, $str as myStr` ,
40- query .WithParameters (
41- ydb .ParamsBuilder ().
42- Param ("$id" ).Uint64 (42 ).
43- Param ("$str" ).Text ("my string" ).
44- Build (),
45- ),
46- )
38+ `SELECT $id as myId, $str as myStr` ,
39+ query .WithParameters (
40+ ydb .ParamsBuilder ().
41+ Param ("$id" ).Uint64 (42 ).
42+ Param ("$str" ).Text ("my string" ).
43+ Build (),
44+ ),
45+ query .WithIdempotent (),
46+ )
47+ if err != nil {
48+ panic (err )
49+ }
50+ defer func () { _ = materializedResult .Close (ctx ) }() // cleanup resources
51+ for rs , err := range materializedResult .ResultSets (ctx ) {
52+ if err != nil {
53+ panic (err )
54+ }
55+ for row , err := range rs .Rows (ctx ) {
4756 if err != nil {
48- return err // for auto-retry with driver
57+ panic ( err )
4958 }
50- defer func () { _ = streamResult .Close (ctx ) }() // cleanup resources
51- for { // iterate over result sets
52- rs , err := streamResult .NextResultSet (ctx )
53- if err != nil {
54- if errors .Is (err , io .EOF ) {
55- break
56- }
57-
58- return err
59- }
60- for { // iterate over rows
61- row , err := rs .NextRow (ctx )
62- if err != nil {
63- if errors .Is (err , io .EOF ) {
64- break
65- }
66-
67- return err
68- }
69- type myStruct struct {
70- ID uint64 `sql:"id"`
71- Str string `sql:"myStr"`
72- }
73- var s myStruct
74- if err = row .ScanStruct (& s ); err != nil {
75- return err // generally scan error not retryable, return it for driver check error
76- }
77- }
59+ type myStruct struct {
60+ ID uint64 `sql:"id"`
61+ Str string `sql:"myStr"`
7862 }
79-
80- return nil
81- },
82- query .WithIdempotent (),
83- )
63+ var s myStruct
64+ if err = row .ScanStruct (& s ); err != nil {
65+ panic (err )
66+ }
67+ }
68+ }
8469 if err != nil {
85- log . Printf ( "unexpected error: %v" , err )
70+ panic ( err )
8671 }
8772}
8873
0 commit comments