Skip to content

Commit 5b23375

Browse files
committed
Added UPDATE and DELETE full logic. Updated examples in example/authors
1 parent 2df7aad commit 5b23375

File tree

11 files changed

+394
-100
lines changed

11 files changed

+394
-100
lines changed

examples/authors/ydb/db_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ func TestAuthors(t *testing.T) {
4646

4747
t.Run("CreateOrUpdateAuthorReturningBio", func(t *testing.T) {
4848
newBio := "Обновленная биография автора"
49-
arg := CreateOrUpdateAuthorRetunringBioParams{
49+
arg := CreateOrUpdateAuthorReturningBioParams{
5050
P0: 3,
5151
P1: "Тестовый Автор",
5252
P2: &newBio,
5353
}
5454

55-
returnedBio, err := q.CreateOrUpdateAuthorRetunringBio(ctx, arg)
55+
returnedBio, err := q.CreateOrUpdateAuthorReturningBio(ctx, arg)
5656
if err != nil {
5757
t.Fatalf("failed to create or update author: %v", err)
5858
}
@@ -67,6 +67,24 @@ func TestAuthors(t *testing.T) {
6767
t.Logf("Author created or updated successfully with bio: %s", *returnedBio)
6868
})
6969

70+
t.Run("Update Author", func(t *testing.T) {
71+
arg := UpdateAuthorByIDParams{
72+
P0: "Максим Горький",
73+
P1: ptr("Обновленная биография"),
74+
P2: 10,
75+
}
76+
77+
singleAuthor, err := q.UpdateAuthorByID(ctx, arg)
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
bio := "Null"
82+
if singleAuthor.Bio != nil {
83+
bio = *singleAuthor.Bio
84+
}
85+
t.Logf("- ID: %d | Name: %s | Bio: %s", singleAuthor.ID, singleAuthor.Name, bio)
86+
})
87+
7088
t.Run("ListAuthors", func(t *testing.T) {
7189
authors, err := q.ListAuthors(ctx)
7290
if err != nil {
@@ -115,24 +133,6 @@ func TestAuthors(t *testing.T) {
115133
}
116134
})
117135

118-
t.Run("ListAuthorsWithIdModulo", func(t *testing.T) {
119-
authors, err := q.ListAuthorsWithIdModulo(ctx)
120-
if err != nil {
121-
t.Fatal(err)
122-
}
123-
if len(authors) == 0 {
124-
t.Fatal("expected at least one author with even ID, got none")
125-
}
126-
t.Log("Authors with even IDs:")
127-
for _, a := range authors {
128-
bio := "Null"
129-
if a.Bio != nil {
130-
bio = *a.Bio
131-
}
132-
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
133-
}
134-
})
135-
136136
t.Run("ListAuthorsWithNullBio", func(t *testing.T) {
137137
authors, err := q.ListAuthorsWithNullBio(ctx)
138138
if err != nil {

examples/authors/ydb/query.sql

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ SELECT * FROM authors;
55
SELECT * FROM authors
66
WHERE id = $p0;
77

8-
-- name: ListAuthorsWithIdModulo :many
9-
SELECT * FROM authors
10-
WHERE id % 2 = 0;
11-
128
-- name: GetAuthorsByName :many
139
SELECT * FROM authors
1410
WHERE name = $p0;
@@ -20,10 +16,11 @@ WHERE bio IS NULL;
2016
-- name: CreateOrUpdateAuthor :execresult
2117
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2);
2218

23-
-- name: CreateOrUpdateAuthorRetunringBio :one
19+
-- name: CreateOrUpdateAuthorReturningBio :one
2420
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2) RETURNING bio;
2521

2622
-- name: DeleteAuthor :exec
27-
DELETE FROM authors
28-
WHERE id = $p0;
23+
DELETE FROM authors WHERE id = $p0;
2924

25+
-- name: UpdateAuthorByID :one
26+
UPDATE authors SET name = $p0, bio = $p1 WHERE id = $p2 RETURNING *;

examples/authors/ydb/query.sql.go

Lines changed: 22 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ require (
2222
github.com/tetratelabs/wazero v1.8.2
2323
github.com/wasilibs/go-pgquery v0.0.0-20240606042535-c0843d6592cc
2424
github.com/xeipuuv/gojsonschema v1.2.0
25+
github.com/ydb-platform/ydb-go-sdk/v3 v3.108.0
26+
github.com/ydb-platform/yql-parsers v0.0.0-20250309001738-7d693911f333
2527
golang.org/x/sync v0.10.0
2628
google.golang.org/grpc v1.69.4
2729
google.golang.org/protobuf v1.36.2
@@ -33,6 +35,7 @@ require (
3335
cel.dev/expr v0.18.0 // indirect
3436
filippo.io/edwards25519 v1.1.0 // indirect
3537
github.com/dustin/go-humanize v1.0.1 // indirect
38+
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
3639
github.com/google/uuid v1.6.0 // indirect
3740
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
3841
github.com/inconshreveable/mousetrap v1.1.0 // indirect
@@ -44,6 +47,7 @@ require (
4447
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
4548
github.com/jackc/pgtype v1.14.0 // indirect
4649
github.com/jackc/puddle/v2 v2.2.2 // indirect
50+
github.com/jonboulle/clockwork v0.3.0 // indirect
4751
github.com/mattn/go-isatty v0.0.20 // indirect
4852
github.com/ncruces/go-strftime v0.1.9 // indirect
4953
github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb // indirect
@@ -55,12 +59,13 @@ require (
5559
github.com/wasilibs/wazero-helpers v0.0.0-20240604052452-61d7981e9a38 // indirect
5660
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
5761
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
62+
github.com/ydb-platform/ydb-go-genproto v0.0.0-20241112172322-ea1f63298f77 // indirect
5863
go.uber.org/atomic v1.11.0 // indirect
5964
go.uber.org/multierr v1.11.0 // indirect
6065
go.uber.org/zap v1.27.0 // indirect
6166
golang.org/x/crypto v0.31.0 // indirect
6267
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
63-
golang.org/x/net v0.30.0 // indirect
68+
golang.org/x/net v0.33.0 // indirect
6469
golang.org/x/sys v0.28.0 // indirect
6570
golang.org/x/text v0.21.0 // indirect
6671
google.golang.org/genproto/googleapis/api v0.0.0-20241015192408-796eee8c2d53 // indirect

0 commit comments

Comments
 (0)