Skip to content

Commit ace37d0

Browse files
committed
lazy transaction example
1 parent 46e02f2 commit ace37d0

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* Fixed lazy transaction example for `godoc`
2+
13
## v3.42.0
24
* Added retry policy options for topics: `topic/topicoptions.WithReaderCheckRetryErrorFunction`, `topic/topicoptions.WithReaderStartTimeout`, `topic/topicoptions.WithWriterCheckRetryErrorFunction`, `topic/topicoptions.WithWriterStartTimeout`
35
* Refactored `internal/conn` middlewares

table/example_test.go

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package table_test
33
import (
44
"context"
55
"fmt"
6+
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
67
"path"
78
"time"
89

@@ -200,41 +201,44 @@ func Example_lazyTransaction() {
200201
),
201202
)
202203
if err != nil {
203-
panic(err)
204+
return err
204205
}
205206
defer tx.Rollback(ctx)
206207
defer result.Close()
207-
if result.NextResultSet(ctx) {
208-
if result.NextRow() {
209-
var (
210-
id uint64
211-
title string
212-
description string
213-
)
214-
if err = result.ScanNamed(
215-
named.OptionalWithDefault("id", &id),
216-
named.OptionalWithDefault("title", &title),
217-
named.OptionalWithDefault("description", &description),
218-
); err != nil {
219-
panic(err)
220-
}
221-
fmt.Println(id, title, description)
222-
_, err = tx.Execute(ctx,
223-
"DECLARE $id AS Uint64; "+
224-
"DECLARE $description AS Text; "+
225-
"UPSERT INTO `path/to/mytable` "+
226-
"(id, description) "+
227-
"VALUES ($id, $description);",
228-
table.NewQueryParameters(
229-
table.ValueParam("$id", types.Uint64Value(1)),
230-
table.ValueParam("$description", types.TextValue("changed description")),
231-
),
232-
options.WithCommit(),
233-
)
234-
if err != nil {
235-
return err
236-
}
237-
}
208+
if !result.NextResultSet(ctx) {
209+
return retry.RetryableError(fmt.Errorf("no result sets"))
210+
}
211+
if !result.NextRow() {
212+
return retry.RetryableError(fmt.Errorf("no rows"))
213+
}
214+
var (
215+
id uint64
216+
title string
217+
description string
218+
)
219+
if err = result.ScanNamed(
220+
named.OptionalWithDefault("id", &id),
221+
named.OptionalWithDefault("title", &title),
222+
named.OptionalWithDefault("description", &description),
223+
); err != nil {
224+
return err
225+
}
226+
fmt.Println(id, title, description)
227+
// execute query with commit transaction
228+
_, err = tx.Execute(ctx,
229+
"DECLARE $id AS Uint64; "+
230+
"DECLARE $description AS Text; "+
231+
"UPSERT INTO `path/to/mytable` "+
232+
"(id, description) "+
233+
"VALUES ($id, $description);",
234+
table.NewQueryParameters(
235+
table.ValueParam("$id", types.Uint64Value(1)),
236+
table.ValueParam("$description", types.TextValue("changed description")),
237+
),
238+
options.WithCommit(),
239+
)
240+
if err != nil {
241+
return err
238242
}
239243
return result.Err()
240244
},

0 commit comments

Comments
 (0)