Skip to content

Commit 76e07ce

Browse files
authored
Merge pull request #1130 from ydb-platform/begin-list
changed in `ydb.ParamsBuilder()` container type constructors from `List()...Build()` to `BeginList()...EndList()` (`Dict`, `Set`, `Optional`)
2 parents f1a46a6 + 43208d2 commit 76e07ce

File tree

18 files changed

+168
-96
lines changed

18 files changed

+168
-96
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Changed `List` constructor from `ydb.ParamsBuilder().List().Build().Build()` to `ydb.ParamsBuilder().BeginList().EndList().Build()`
2+
* Changed `Set` constructor from `ydb.ParamsBuilder().Set().Build().Build()` to `ydb.ParamsBuilder().BeginSet().EndSet().Build()`
3+
* Changed `Dict` constructor from `ydb.ParamsBuilder().Dict().Build().Build()` to `ydb.ParamsBuilder().BeginDict().EndDict().Build()`
4+
* Changed `Optional` constructor from `ydb.ParamsBuilder().Set().Build().Build()` to `ydb.ParamsBuilder().BeginOptional().EndOptional().Build()`
15
* Added events into `trace.Query` trace
26
* Rewrote `internal/pool` to buffered channel
37
* Added `internal/xcontext.WithDone()`

example_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ydb_test
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"fmt"
78
"io"
89
"log"
@@ -14,13 +15,77 @@ import (
1415
"github.com/ydb-platform/ydb-go-sdk/v3"
1516
"github.com/ydb-platform/ydb-go-sdk/v3/balancers"
1617
"github.com/ydb-platform/ydb-go-sdk/v3/config"
18+
"github.com/ydb-platform/ydb-go-sdk/v3/query"
1719
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
1820
"github.com/ydb-platform/ydb-go-sdk/v3/table"
1921
"github.com/ydb-platform/ydb-go-sdk/v3/table/result/named"
2022
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
2123
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions"
2224
)
2325

26+
//nolint:testableexamples, nonamedreturns
27+
func Example_query() {
28+
ctx := context.TODO()
29+
db, err := ydb.Open(ctx, "grpc://localhost:2136/local")
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
defer db.Close(ctx) // cleanup resources
34+
35+
err = db.Query().Do( // Do retry operation on errors with best effort
36+
ctx, // context manage exiting from Do
37+
func(ctx context.Context, s query.Session) (err error) { // retry operation
38+
_, res, err := s.Execute(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+
)
47+
if err != nil {
48+
return err // for auto-retry with driver
49+
}
50+
defer func() { _ = res.Close(ctx) }() // cleanup resources
51+
for { // iterate over result sets
52+
rs, err := res.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+
}
78+
}
79+
80+
return res.Err() // return finally result error for auto-retry with driver
81+
},
82+
query.WithIdempotent(),
83+
)
84+
if err != nil {
85+
log.Printf("unexpected error: %v", err)
86+
}
87+
}
88+
2489
//nolint:testableexamples, nonamedreturns
2590
func Example_table() {
2691
ctx := context.TODO()

examples/basic/native/query/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010

1111
environ "github.com/ydb-platform/ydb-go-sdk-auth-environ"
12-
"github.com/ydb-platform/ydb-go-sdk/v3"
12+
ydb "github.com/ydb-platform/ydb-go-sdk/v3"
1313
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
1414
)
1515

examples/basic/native/query/series.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"path"
1010
"time"
1111

12-
"github.com/ydb-platform/ydb-go-sdk/v3"
12+
ydb "github.com/ydb-platform/ydb-go-sdk/v3"
1313
"github.com/ydb-platform/ydb-go-sdk/v3/query"
1414
)
1515

@@ -130,9 +130,9 @@ func fillTablesWithData(ctx context.Context, c query.Client, prefix string) erro
130130
FROM AS_TABLE($episodesData);
131131
`, prefix),
132132
query.WithParameters(ydb.ParamsBuilder().
133-
Param("$seriesData").List().AddItems(series...).Build().
134-
Param("$seasonsData").List().AddItems(seasons...).Build().
135-
Param("$episodesData").List().AddItems(episodes...).Build().
133+
Param("$seriesData").BeginList().AddItems(series...).EndList().
134+
Param("$seasonsData").BeginList().AddItems(seasons...).EndList().
135+
Param("$episodesData").BeginList().AddItems(episodes...).EndList().
136136
Build(),
137137
),
138138
)

examples/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ require (
3232
github.com/golang/snappy v0.0.4 // indirect
3333
github.com/jackc/pgpassfile v1.0.0 // indirect
3434
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
35-
github.com/jackc/pgx/v5 v5.3.0 // indirect
35+
github.com/jackc/pgx/v5 v5.5.4 // indirect
36+
github.com/jackc/puddle/v2 v2.2.1 // indirect
3637
github.com/jinzhu/inflection v1.0.0 // indirect
3738
github.com/jinzhu/now v1.1.5 // indirect
3839
github.com/jonboulle/clockwork v0.4.0 // indirect

examples/go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -944,14 +944,17 @@ github.com/jackc/pgx/v4 v4.6.1-0.20200510190926-94ba730bb1e9/go.mod h1:t3/cdRQl6
944944
github.com/jackc/pgx/v4 v4.6.1-0.20200606145419-4e5062306904/go.mod h1:ZDaNWkt9sW1JMiNn0kdYBaLelIhw7Pg4qd+Vk6tw7Hg=
945945
github.com/jackc/pgx/v4 v4.11.0/go.mod h1:i62xJgdrtVDsnL3U8ekyrQXEwGNTRoG7/8r+CIdYfcc=
946946
github.com/jackc/pgx/v4 v4.12.0/go.mod h1:fE547h6VulLPA3kySjfnSG/e2D861g/50JlVUa/ub60=
947-
github.com/jackc/pgx/v5 v5.3.0 h1:/NQi8KHMpKWHInxXesC8yD4DhkXPrVhmnwYkjp9AmBA=
948947
github.com/jackc/pgx/v5 v5.3.0/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8=
948+
github.com/jackc/pgx/v5 v5.5.4 h1:Xp2aQS8uXButQdnCMWNmvx6UysWQQC+u1EoizjguY+8=
949+
github.com/jackc/pgx/v5 v5.5.4/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
949950
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
950951
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
951952
github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
952953
github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
953954
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
954955
github.com/jackc/puddle/v2 v2.2.0/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
956+
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
957+
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
955958
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
956959
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
957960
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
@@ -1954,7 +1957,6 @@ google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
19541957
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
19551958
google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
19561959
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
1957-
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
19581960
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
19591961
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
19601962
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=

internal/params/builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func TestBuilder(t *testing.T) {
361361
{
362362
name: xtest.CurrentFileLine(),
363363
builder: Builder{}.
364-
Param("$x").List().Build().
364+
Param("$x").BeginList().EndList().
365365
Build(),
366366
params: map[string]*Ydb.TypedValue{
367367
"$x": {

internal/params/dict.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type (
2121
}
2222
)
2323

24-
func (d *dict) Pair() *dictPair {
24+
func (d *dict) Add() *dictPair {
2525
return &dictPair{
2626
parent: d,
2727
}
@@ -407,7 +407,7 @@ func (d *dictValue) UUID(v [16]byte) *dict {
407407
return d.pair.parent
408408
}
409409

410-
func (d *dict) Build() Builder {
410+
func (d *dict) EndDict() Builder {
411411
d.parent.params = append(d.parent.params, &Parameter{
412412
parent: d.parent,
413413
name: d.name,

internal/params/dict_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,15 @@ func TestDict(t *testing.T) {
370370
a := allocator.New()
371371
defer a.Free()
372372

373-
item := Builder{}.Param("$x").Dict().Pair()
373+
item := Builder{}.Param("$x").BeginDict().Add()
374374

375375
addedKey, ok := xtest.CallMethod(item, key.method, key.args...)[0].(*dictValue)
376376
require.True(t, ok)
377377

378378
d, ok := xtest.CallMethod(addedKey, val.method, val.args...)[0].(*dict)
379379
require.True(t, ok)
380380

381-
params := d.Build().Build().ToYDB(a)
381+
params := d.EndDict().Build().ToYDB(a)
382382
require.Equal(t, paramsToJSON(
383383
map[string]*Ydb.TypedValue{
384384
"$x": {
@@ -420,7 +420,7 @@ func TestDict_AddPairs(t *testing.T) {
420420
},
421421
}
422422

423-
params := Builder{}.Param("$x").Dict().AddPairs(pairs...).Build().Build().ToYDB(a)
423+
params := Builder{}.Param("$x").BeginDict().AddPairs(pairs...).EndDict().Build().ToYDB(a)
424424

425425
require.Equal(t, paramsToJSON(
426426
map[string]*Ydb.TypedValue{

internal/params/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type (
1717
}
1818
)
1919

20-
func (l *list) AddItem() *listItem {
20+
func (l *list) Add() *listItem {
2121
return &listItem{
2222
parent: l,
2323
}
@@ -29,7 +29,7 @@ func (l *list) AddItems(items ...value.Value) *list {
2929
return l
3030
}
3131

32-
func (l *list) Build() Builder {
32+
func (l *list) EndList() Builder {
3333
l.parent.params = append(l.parent.params, &Parameter{
3434
parent: l.parent,
3535
name: l.name,

0 commit comments

Comments
 (0)