Skip to content

Commit fad3140

Browse files
committed
changed in ydb.ParamsBuilder() container type constructors from List()...Build() to BeginList()...EndList() (Dict, Set, Optional)
1 parent f1a46a6 commit fad3140

File tree

12 files changed

+152
-87
lines changed

12 files changed

+152
-87
lines changed

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()

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,

internal/params/list_test.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestList(t *testing.T) {
2020
}{
2121
{
2222
name: xtest.CurrentFileLine(),
23-
builder: Builder{}.Param("$x").List().AddItem().Uint64(123).Build(),
23+
builder: Builder{}.Param("$x").BeginList().Add().Uint64(123).EndList(),
2424
params: map[string]*Ydb.TypedValue{
2525
"$x": {
2626
Type: &Ydb.Type{
@@ -48,7 +48,7 @@ func TestList(t *testing.T) {
4848
},
4949
{
5050
name: xtest.CurrentFileLine(),
51-
builder: Builder{}.Param("$x").List().AddItem().Int64(123).Build(),
51+
builder: Builder{}.Param("$x").BeginList().Add().Int64(123).EndList(),
5252
params: map[string]*Ydb.TypedValue{
5353
"$x": {
5454
Type: &Ydb.Type{
@@ -76,7 +76,7 @@ func TestList(t *testing.T) {
7676
},
7777
{
7878
name: xtest.CurrentFileLine(),
79-
builder: Builder{}.Param("$x").List().AddItem().Uint32(123).Build(),
79+
builder: Builder{}.Param("$x").BeginList().Add().Uint32(123).EndList(),
8080
params: map[string]*Ydb.TypedValue{
8181
"$x": {
8282
Type: &Ydb.Type{
@@ -104,7 +104,7 @@ func TestList(t *testing.T) {
104104
},
105105
{
106106
name: xtest.CurrentFileLine(),
107-
builder: Builder{}.Param("$x").List().AddItem().Int32(123).Build(),
107+
builder: Builder{}.Param("$x").BeginList().Add().Int32(123).EndList(),
108108
params: map[string]*Ydb.TypedValue{
109109
"$x": {
110110
Type: &Ydb.Type{
@@ -132,7 +132,7 @@ func TestList(t *testing.T) {
132132
},
133133
{
134134
name: xtest.CurrentFileLine(),
135-
builder: Builder{}.Param("$x").List().AddItem().Uint16(123).Build(),
135+
builder: Builder{}.Param("$x").BeginList().Add().Uint16(123).EndList(),
136136
params: map[string]*Ydb.TypedValue{
137137
"$x": {
138138
Type: &Ydb.Type{
@@ -160,7 +160,7 @@ func TestList(t *testing.T) {
160160
},
161161
{
162162
name: xtest.CurrentFileLine(),
163-
builder: Builder{}.Param("$x").List().AddItem().Int16(123).Build(),
163+
builder: Builder{}.Param("$x").BeginList().Add().Int16(123).EndList(),
164164
params: map[string]*Ydb.TypedValue{
165165
"$x": {
166166
Type: &Ydb.Type{
@@ -188,7 +188,7 @@ func TestList(t *testing.T) {
188188
},
189189
{
190190
name: xtest.CurrentFileLine(),
191-
builder: Builder{}.Param("$x").List().AddItem().Uint8(123).Build(),
191+
builder: Builder{}.Param("$x").BeginList().Add().Uint8(123).EndList(),
192192
params: map[string]*Ydb.TypedValue{
193193
"$x": {
194194
Type: &Ydb.Type{
@@ -216,7 +216,7 @@ func TestList(t *testing.T) {
216216
},
217217
{
218218
name: xtest.CurrentFileLine(),
219-
builder: Builder{}.Param("$x").List().AddItem().Int8(123).Build(),
219+
builder: Builder{}.Param("$x").BeginList().Add().Int8(123).EndList(),
220220
params: map[string]*Ydb.TypedValue{
221221
"$x": {
222222
Type: &Ydb.Type{
@@ -244,7 +244,7 @@ func TestList(t *testing.T) {
244244
},
245245
{
246246
name: xtest.CurrentFileLine(),
247-
builder: Builder{}.Param("$x").List().AddItem().Bool(true).Build(),
247+
builder: Builder{}.Param("$x").BeginList().Add().Bool(true).EndList(),
248248
params: map[string]*Ydb.TypedValue{
249249
"$x": {
250250
Type: &Ydb.Type{
@@ -272,7 +272,7 @@ func TestList(t *testing.T) {
272272
},
273273
{
274274
name: xtest.CurrentFileLine(),
275-
builder: Builder{}.Param("$x").List().AddItem().Text("test").Build(),
275+
builder: Builder{}.Param("$x").BeginList().Add().Text("test").EndList(),
276276
params: map[string]*Ydb.TypedValue{
277277
"$x": {
278278
Type: &Ydb.Type{
@@ -300,7 +300,7 @@ func TestList(t *testing.T) {
300300
},
301301
{
302302
name: xtest.CurrentFileLine(),
303-
builder: Builder{}.Param("$x").List().AddItem().Bytes([]byte("test")).Build(),
303+
builder: Builder{}.Param("$x").BeginList().Add().Bytes([]byte("test")).EndList(),
304304
params: map[string]*Ydb.TypedValue{
305305
"$x": {
306306
Type: &Ydb.Type{
@@ -328,7 +328,7 @@ func TestList(t *testing.T) {
328328
},
329329
{
330330
name: xtest.CurrentFileLine(),
331-
builder: Builder{}.Param("$x").List().AddItem().Float(123).Build(),
331+
builder: Builder{}.Param("$x").BeginList().Add().Float(123).EndList(),
332332
params: map[string]*Ydb.TypedValue{
333333
"$x": {
334334
Type: &Ydb.Type{
@@ -356,7 +356,7 @@ func TestList(t *testing.T) {
356356
},
357357
{
358358
name: xtest.CurrentFileLine(),
359-
builder: Builder{}.Param("$x").List().AddItem().Double(123).Build(),
359+
builder: Builder{}.Param("$x").BeginList().Add().Double(123).EndList(),
360360
params: map[string]*Ydb.TypedValue{
361361
"$x": {
362362
Type: &Ydb.Type{
@@ -384,7 +384,7 @@ func TestList(t *testing.T) {
384384
},
385385
{
386386
name: xtest.CurrentFileLine(),
387-
builder: Builder{}.Param("$x").List().AddItem().Interval(time.Second).Build(),
387+
builder: Builder{}.Param("$x").BeginList().Add().Interval(time.Second).EndList(),
388388
params: map[string]*Ydb.TypedValue{
389389
"$x": {
390390
Type: &Ydb.Type{
@@ -412,7 +412,7 @@ func TestList(t *testing.T) {
412412
},
413413
{
414414
name: xtest.CurrentFileLine(),
415-
builder: Builder{}.Param("$x").List().AddItem().Datetime(time.Unix(123456789, 456)).Build(),
415+
builder: Builder{}.Param("$x").BeginList().Add().Datetime(time.Unix(123456789, 456)).EndList(),
416416
params: map[string]*Ydb.TypedValue{
417417
"$x": {
418418
Type: &Ydb.Type{
@@ -440,7 +440,7 @@ func TestList(t *testing.T) {
440440
},
441441
{
442442
name: xtest.CurrentFileLine(),
443-
builder: Builder{}.Param("$x").List().AddItem().Date(time.Unix(123456789, 456)).Build(),
443+
builder: Builder{}.Param("$x").BeginList().Add().Date(time.Unix(123456789, 456)).EndList(),
444444
params: map[string]*Ydb.TypedValue{
445445
"$x": {
446446
Type: &Ydb.Type{
@@ -468,7 +468,7 @@ func TestList(t *testing.T) {
468468
},
469469
{
470470
name: xtest.CurrentFileLine(),
471-
builder: Builder{}.Param("$x").List().AddItem().Timestamp(time.Unix(123456789, 456)).Build(),
471+
builder: Builder{}.Param("$x").BeginList().Add().Timestamp(time.Unix(123456789, 456)).EndList(),
472472
params: map[string]*Ydb.TypedValue{
473473
"$x": {
474474
Type: &Ydb.Type{
@@ -496,7 +496,7 @@ func TestList(t *testing.T) {
496496
},
497497
{
498498
name: xtest.CurrentFileLine(),
499-
builder: Builder{}.Param("$x").List().AddItem().Decimal([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}, 22, 9).Build(), //nolint:lll
499+
builder: Builder{}.Param("$x").BeginList().Add().Decimal([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}, 22, 9).EndList(), //nolint:lll
500500
params: map[string]*Ydb.TypedValue{
501501
"$x": {
502502
Type: &Ydb.Type{
@@ -528,7 +528,7 @@ func TestList(t *testing.T) {
528528
},
529529
{
530530
name: xtest.CurrentFileLine(),
531-
builder: Builder{}.Param("$x").List().AddItem().JSON(`{"a": 1,"b": "B"}`).Build(),
531+
builder: Builder{}.Param("$x").BeginList().Add().JSON(`{"a": 1,"b": "B"}`).EndList(),
532532
params: map[string]*Ydb.TypedValue{
533533
"$x": {
534534
Type: &Ydb.Type{
@@ -556,7 +556,7 @@ func TestList(t *testing.T) {
556556
},
557557
{
558558
name: xtest.CurrentFileLine(),
559-
builder: Builder{}.Param("$x").List().AddItem().JSONDocument(`{"a": 1,"b": "B"}`).Build(),
559+
builder: Builder{}.Param("$x").BeginList().Add().JSONDocument(`{"a": 1,"b": "B"}`).EndList(),
560560
params: map[string]*Ydb.TypedValue{
561561
"$x": {
562562
Type: &Ydb.Type{
@@ -584,7 +584,7 @@ func TestList(t *testing.T) {
584584
},
585585
{
586586
name: xtest.CurrentFileLine(),
587-
builder: Builder{}.Param("$x").List().AddItem().YSON([]byte(`[ 1; 2; 3; 4; 5 ]`)).Build(),
587+
builder: Builder{}.Param("$x").BeginList().Add().YSON([]byte(`[ 1; 2; 3; 4; 5 ]`)).EndList(),
588588
params: map[string]*Ydb.TypedValue{
589589
"$x": {
590590
Type: &Ydb.Type{
@@ -612,8 +612,8 @@ func TestList(t *testing.T) {
612612
},
613613
{
614614
name: xtest.CurrentFileLine(),
615-
builder: Builder{}.Param("$x").List().AddItem().
616-
UUID([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}).Build(),
615+
builder: Builder{}.Param("$x").BeginList().Add().
616+
UUID([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}).EndList(),
617617
params: map[string]*Ydb.TypedValue{
618618
"$x": {
619619
Type: &Ydb.Type{
@@ -642,7 +642,7 @@ func TestList(t *testing.T) {
642642
},
643643
{
644644
name: xtest.CurrentFileLine(),
645-
builder: Builder{}.Param("$x").List().AddItems(value.Uint64Value(123), value.Uint64Value(321)).Build(),
645+
builder: Builder{}.Param("$x").BeginList().AddItems(value.Uint64Value(123), value.Uint64Value(321)).EndList(),
646646
params: map[string]*Ydb.TypedValue{
647647
"$x": {
648648
Type: &Ydb.Type{

internal/params/optional.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type (
1717
}
1818
)
1919

20-
func (b *optionalBuilder) Build() Builder {
20+
func (b *optionalBuilder) EndOptional() Builder {
2121
b.opt.parent.params = append(b.opt.parent.params, &Parameter{
2222
parent: b.opt.parent,
2323
name: b.opt.name,

0 commit comments

Comments
 (0)