Skip to content

Commit 5b9a61b

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

File tree

11 files changed

+427
-97
lines changed

11 files changed

+427
-97
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ require (
2424
github.com/tetratelabs/wazero v1.9.0
2525
github.com/wasilibs/go-pgquery v0.0.0-20250409022910-10ac41983c07
2626
github.com/xeipuuv/gojsonschema v1.2.0
27+
github.com/ydb-platform/ydb-go-sdk/v3 v3.108.0
28+
github.com/ydb-platform/yql-parsers v0.0.0-20250309001738-7d693911f333
2729
golang.org/x/sync v0.13.0
2830
google.golang.org/grpc v1.71.1
2931
google.golang.org/protobuf v1.36.6
@@ -35,6 +37,7 @@ require (
3537
cel.dev/expr v0.19.1 // indirect
3638
filippo.io/edwards25519 v1.1.0 // indirect
3739
github.com/dustin/go-humanize v1.0.1 // indirect
40+
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
3841
github.com/google/uuid v1.6.0 // indirect
3942
github.com/inconshreveable/mousetrap v1.1.0 // indirect
4043
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
@@ -45,6 +48,7 @@ require (
4548
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
4649
github.com/jackc/pgtype v1.14.0 // indirect
4750
github.com/jackc/puddle/v2 v2.2.2 // indirect
51+
github.com/jonboulle/clockwork v0.3.0 // indirect
4852
github.com/mattn/go-isatty v0.0.20 // indirect
4953
github.com/ncruces/go-strftime v0.1.9 // indirect
5054
github.com/pingcap/errors v0.11.5-0.20240311024730-e056997136bb // indirect
@@ -56,6 +60,7 @@ require (
5660
github.com/wasilibs/wazero-helpers v0.0.0-20240620070341-3dff1577cd52 // indirect
5761
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
5862
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
63+
github.com/ydb-platform/ydb-go-genproto v0.0.0-20241112172322-ea1f63298f77 // indirect
5964
go.uber.org/atomic v1.11.0 // indirect
6065
go.uber.org/multierr v1.11.0 // indirect
6166
go.uber.org/zap v1.27.0 // indirect

0 commit comments

Comments
 (0)