Skip to content

Commit d1d51a3

Browse files
committed
fixes
1 parent ff0747d commit d1d51a3

File tree

2 files changed

+41
-56
lines changed

2 files changed

+41
-56
lines changed

example_test.go

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
//go:build go1.23
2+
13
package ydb_test
24

35
import (
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

internal/query/client.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,11 @@ func executeScript(ctx context.Context, //nolint:funlen
182182
Stats: stats.FromQueryStats(md.GetExecStats()),
183183
ResultSetsMeta: func() (
184184
resultSetsMeta []struct {
185-
Columns []struct {
186-
Name string
187-
Type query.Type
188-
}
189-
},
185+
Columns []struct {
186+
Name string
187+
Type query.Type
188+
}
189+
},
190190
) {
191191
for _, rs := range md.GetResultSetsMeta() {
192192
resultSetsMeta = append(resultSetsMeta, struct {
@@ -197,9 +197,9 @@ func executeScript(ctx context.Context, //nolint:funlen
197197
}{
198198
Columns: func() (
199199
columns []struct {
200-
Name string
201-
Type types.Type
202-
},
200+
Name string
201+
Type types.Type
202+
},
203203
) {
204204
for _, c := range rs.GetColumns() {
205205
columns = append(columns, struct {

0 commit comments

Comments
 (0)