Skip to content

Commit 367dbda

Browse files
1NepuNep1asmyasnikov
authored andcommitted
Added UPDATE and DELETE full logic. Updated examples in example/authors
1 parent 4a55329 commit 367dbda

9 files changed

Lines changed: 271 additions & 95 deletions

File tree

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.

0 commit comments

Comments
 (0)