Skip to content

Commit c840e4a

Browse files
authored
Merge pull request #1406 from ydb-platform/query-example
Changed query.Client API
2 parents 2e2103a + fe61f3c commit c840e4a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3107
-1340
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* Removed tx result from `query.Session.Execute` (tx can be obtained from `query.Session.Begin`)
2+
* Changed behaviour of `query.Session.Begin` to `noop` for lazy initialization with first call `query.TxActor.Execute`
3+
* Splitted experimental method `query.Client.Execute` to methods `query.Client.Exec` without result and `query.Client.Query` with result
4+
* Splitted experimental method `query.TxActor.Execute` to methods `query.TxActor.Exec` without result and `query.TxActor.Query` with result
5+
* Renamed experimental method `query.Client.ReadResultSet` to `query.Client.QueryResultSet`
6+
* Renamed experimental method `query.Client.ReadRow` to `query.Client.QueryRow`
7+
* Removed experimental methods `query.Session.ReadResultSet` and `query.Session.ReadRows`
8+
* Removed experimental methods `query.TxActor.ReadResultSet` and `query.TxActor.ReadRows`
9+
* Removed experimental method `query.Client.Stats`
10+
111
## v3.76.6
212
* Replaced requirements from go1.22 + experimantal flag to go1.23 for experimental range-over interface
313

example_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func Example_query() {
3535
err = db.Query().Do( // Do retry operation on errors with best effort
3636
ctx, // context manage exiting from Do
3737
func(ctx context.Context, s query.Session) (err error) { // retry operation
38-
_, res, err := s.Execute(ctx,
38+
streamResult, err := s.Query(ctx,
3939
`SELECT $id as myId, $str as myStr`,
4040
query.WithParameters(
4141
ydb.ParamsBuilder().
@@ -47,9 +47,9 @@ func Example_query() {
4747
if err != nil {
4848
return err // for auto-retry with driver
4949
}
50-
defer func() { _ = res.Close(ctx) }() // cleanup resources
51-
for { // iterate over result sets
52-
rs, err := res.NextResultSet(ctx)
50+
defer func() { _ = streamResult.Close(ctx) }() // cleanup resources
51+
for { // iterate over result sets
52+
rs, err := streamResult.NextResultSet(ctx)
5353
if err != nil {
5454
if errors.Is(err, io.EOF) {
5555
break

examples/basic/native/query/main.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"flag"
56
"fmt"
67
"os"
78
"path"
@@ -15,20 +16,18 @@ import (
1516

1617
func isYdbVersionHaveQueryService() error {
1718
minYdbVersion := strings.Split("24.1", ".")
18-
ydbVersion := strings.Split(os.Getenv("YDB_VERSION"), ".")
19-
for i, component := range ydbVersion {
19+
ydbVersion, has := os.LookupEnv("YDB_VERSION")
20+
if !has {
21+
return nil
22+
}
23+
versionComponents := strings.Split(ydbVersion, ".")
24+
for i, component := range versionComponents {
2025
if i < len(minYdbVersion) {
2126
if r := strings.Compare(component, minYdbVersion[i]); r < 0 {
2227
return fmt.Errorf("example '%s' run on minimal YDB version '%v', but current version is '%s'",
2328
os.Args[0],
2429
strings.Join(minYdbVersion, "."),
25-
func() string {
26-
if len(ydbVersion) > 0 && ydbVersion[0] != "" {
27-
return strings.Join(ydbVersion, ".")
28-
}
29-
30-
return "undefined"
31-
}(),
30+
ydbVersion,
3231
)
3332
} else if r > 0 {
3433
return nil
@@ -39,6 +38,8 @@ func isYdbVersionHaveQueryService() error {
3938
return nil
4039
}
4140

41+
var connectionString = flag.String("ydb", os.Getenv("YDB_CONNECTION_STRING"), "")
42+
4243
func main() {
4344
if err := isYdbVersionHaveQueryService(); err != nil {
4445
fmt.Println(err.Error())
@@ -49,13 +50,9 @@ func main() {
4950
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
5051
defer cancel()
5152

52-
dsn, exists := os.LookupEnv("YDB_CONNECTION_STRING")
53-
if !exists {
54-
panic("YDB_CONNECTION_STRING environment variable not defined")
55-
}
53+
flag.Parse()
5654

57-
db, err := ydb.Open(ctx,
58-
dsn,
55+
db, err := ydb.Open(ctx, *connectionString,
5956
environ.WithEnvironCredentials(),
6057
)
6158
if err != nil {

examples/basic/native/query/series.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
func read(ctx context.Context, c query.Client, prefix string) error {
1717
return c.Do(ctx,
1818
func(ctx context.Context, s query.Session) (err error) {
19-
_, result, err := s.Execute(ctx, fmt.Sprintf(`
19+
result, err := s.Query(ctx, fmt.Sprintf(`
2020
PRAGMA TablePathPrefix("%s");
2121
DECLARE $seriesID AS Uint64;
2222
SELECT
@@ -27,7 +27,6 @@ func read(ctx context.Context, c query.Client, prefix string) error {
2727
series
2828
`, prefix),
2929
query.WithTxControl(query.TxControl(query.BeginTx(query.WithOnlineReadOnly()))),
30-
query.WithStatsMode(query.StatsModeBasic),
3130
)
3231
if err != nil {
3332
return err
@@ -79,7 +78,7 @@ func fillTablesWithData(ctx context.Context, c query.Client, prefix string) erro
7978

8079
return c.Do(ctx,
8180
func(ctx context.Context, s query.Session) (err error) {
82-
_, _, err = s.Execute(ctx,
81+
return s.Exec(ctx,
8382
fmt.Sprintf(`
8483
PRAGMA TablePathPrefix("%s");
8584
@@ -138,16 +137,14 @@ func fillTablesWithData(ctx context.Context, c query.Client, prefix string) erro
138137
Build(),
139138
),
140139
)
141-
142-
return err
143140
},
144141
)
145142
}
146143

147144
func createTables(ctx context.Context, c query.Client, prefix string) error {
148145
return c.Do(ctx,
149146
func(ctx context.Context, s query.Session) error {
150-
_, _, err := s.Execute(ctx, fmt.Sprintf(`
147+
err := s.Exec(ctx, fmt.Sprintf(`
151148
CREATE TABLE IF NOT EXISTS %s (
152149
series_id Bytes,
153150
title Text,
@@ -164,7 +161,7 @@ func createTables(ctx context.Context, c query.Client, prefix string) error {
164161
return err
165162
}
166163

167-
_, _, err = s.Execute(ctx, fmt.Sprintf(`
164+
err = s.Exec(ctx, fmt.Sprintf(`
168165
CREATE TABLE IF NOT EXISTS %s (
169166
series_id Bytes,
170167
season_id Bytes,
@@ -181,7 +178,7 @@ func createTables(ctx context.Context, c query.Client, prefix string) error {
181178
return err
182179
}
183180

184-
_, _, err = s.Execute(ctx, fmt.Sprintf(`
181+
err = s.Exec(ctx, fmt.Sprintf(`
185182
CREATE TABLE IF NOT EXISTS %s (
186183
series_id Bytes,
187184
season_id Bytes,

examples/topic/topicreader/topic_reader_transaction.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func CommitMessagesToTransaction(ctx context.Context, db *ydb.Driver, reader *to
2626
return err
2727
}
2828

29-
_, err = tx.Execute(ctx, `
29+
err = tx.Exec(ctx, `
3030
$last = SELECT MAX(val) FROM table WHERE id=$id;
3131
UPSERT INTO t (id, val) VALUES($id, COALESCE($last, 0) + $value)
3232
`, query.WithParameters(
@@ -62,7 +62,7 @@ func PopWithTransaction(ctx context.Context, db *ydb.Driver, reader *topicreader
6262
return err
6363
}
6464

65-
_, err = tx.Execute(ctx, `
65+
err = tx.Exec(ctx, `
6666
$last = SELECT MAX(val) FROM table WHERE id=$id;
6767
UPSERT INTO t (id, val) VALUES($id, COALESCE($last, 0) + $value)
6868
`, query.WithParameters(
@@ -113,7 +113,7 @@ func PopWithTransactionRecreateReader(
113113
return err
114114
}
115115

116-
_, err = tx.Execute(ctx, `
116+
err = tx.Exec(ctx, `
117117
$last = SELECT MAX(val) FROM table WHERE id=$id;
118118
UPSERT INTO t (id, val) VALUES($id, COALESCE($last, 0) + $value)
119119
`,

0 commit comments

Comments
 (0)