Skip to content

Commit 2df7aad

Browse files
committed
Added almost full INSERT support and basic DELETE (without ON) support
1 parent 08121ec commit 2df7aad

File tree

13 files changed

+533
-80
lines changed

13 files changed

+533
-80
lines changed

examples/authors/sqlc.yaml

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,47 @@ version: '2'
22
cloud:
33
project: "01HAQMMECEYQYKFJN8MP16QC41"
44
sql:
5-
- name: postgresql
6-
schema: postgresql/schema.sql
7-
queries: postgresql/query.sql
8-
engine: postgresql
9-
database:
10-
uri: "${VET_TEST_EXAMPLES_POSTGRES_AUTHORS}"
11-
analyzer:
12-
database: false
13-
rules:
14-
- sqlc/db-prepare
15-
- postgresql-query-too-costly
16-
gen:
17-
go:
18-
package: authors
19-
sql_package: pgx/v5
20-
out: postgresql
21-
- name: mysql
22-
schema: mysql/schema.sql
23-
queries: mysql/query.sql
24-
engine: mysql
25-
database:
26-
uri: "${VET_TEST_EXAMPLES_MYSQL_AUTHORS}"
27-
rules:
28-
- sqlc/db-prepare
29-
# - mysql-query-too-costly
30-
gen:
31-
go:
32-
package: authors
33-
out: mysql
34-
- name: sqlite
35-
schema: sqlite/schema.sql
36-
queries: sqlite/query.sql
37-
engine: sqlite
38-
database:
39-
uri: file:authors?mode=memory&cache=shared
40-
rules:
41-
- sqlc/db-prepare
42-
gen:
43-
go:
44-
package: authors
45-
out: sqlite
5+
# - name: postgresql
6+
# schema: postgresql/schema.sql
7+
# queries: postgresql/query.sql
8+
# engine: postgresql
9+
# database:
10+
# uri: "${VET_TEST_EXAMPLES_POSTGRES_AUTHORS}"
11+
# analyzer:
12+
# database: false
13+
# rules:
14+
# - sqlc/db-prepare
15+
# - postgresql-query-too-costly
16+
# gen:
17+
# go:
18+
# package: authors
19+
# sql_package: pgx/v5
20+
# out: postgresql
21+
# - name: mysql
22+
# schema: mysql/schema.sql
23+
# queries: mysql/query.sql
24+
# engine: mysql
25+
# database:
26+
# uri: "${VET_TEST_EXAMPLES_MYSQL_AUTHORS}"
27+
# rules:
28+
# - sqlc/db-prepare
29+
# # - mysql-query-too-costly
30+
# gen:
31+
# go:
32+
# package: authors
33+
# out: mysql
34+
# - name: sqlite
35+
# schema: sqlite/schema.sql
36+
# queries: sqlite/query.sql
37+
# engine: sqlite
38+
# database:
39+
# uri: file:authors?mode=memory&cache=shared
40+
# rules:
41+
# - sqlc/db-prepare
42+
# gen:
43+
# go:
44+
# package: authors
45+
# out: sqlite
4646
- name: ydb
4747
schema: ydb/schema.sql
4848
queries: ydb/query.sql

examples/authors/ydb/db_test.go

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
_ "github.com/ydb-platform/ydb-go-sdk/v3"
99
)
1010

11+
func ptr(s string) *string {
12+
return &s
13+
}
14+
1115
func TestAuthors(t *testing.T) {
1216
ctx := context.Background()
1317

@@ -16,6 +20,53 @@ func TestAuthors(t *testing.T) {
1620

1721
q := New(test.DB)
1822

23+
t.Run("InsertAuthors", func(t *testing.T) {
24+
authorsToInsert := []CreateOrUpdateAuthorParams{
25+
{P0: 1, P1: "Лев Толстой", P2: ptr("Русский писатель, автор \"Война и мир\"")},
26+
{P0: 2, P1: "Александр Пушкин", P2: ptr("Автор \"Евгения Онегина\"")},
27+
{P0: 3, P1: "Александр Пушкин", P2: ptr("Русский поэт, драматург и прозаик")},
28+
{P0: 4, P1: "Фёдор Достоевский", P2: ptr("Автор \"Преступление и наказание\"")},
29+
{P0: 5, P1: "Николай Гоголь", P2: ptr("Автор \"Мёртвые души\"")},
30+
{P0: 6, P1: "Антон Чехов", P2: nil},
31+
{P0: 7, P1: "Иван Тургенев", P2: ptr("Автор \"Отцы и дети\"")},
32+
{P0: 8, P1: "Михаил Лермонтов", P2: nil},
33+
{P0: 9, P1: "Даниил Хармс", P2: ptr("Абсурдист, писатель и поэт")},
34+
{P0: 10, P1: "Максим Горький", P2: ptr("Автор \"На дне\"")},
35+
{P0: 11, P1: "Владимир Маяковский", P2: nil},
36+
{P0: 12, P1: "Сергей Есенин", P2: ptr("Русский лирик")},
37+
{P0: 13, P1: "Борис Пастернак", P2: ptr("Автор \"Доктор Живаго\"")},
38+
}
39+
40+
for _, author := range authorsToInsert {
41+
if _, err := q.CreateOrUpdateAuthor(ctx, author); err != nil {
42+
t.Fatalf("failed to insert author %q: %v", author.P1, err)
43+
}
44+
}
45+
})
46+
47+
t.Run("CreateOrUpdateAuthorReturningBio", func(t *testing.T) {
48+
newBio := "Обновленная биография автора"
49+
arg := CreateOrUpdateAuthorRetunringBioParams{
50+
P0: 3,
51+
P1: "Тестовый Автор",
52+
P2: &newBio,
53+
}
54+
55+
returnedBio, err := q.CreateOrUpdateAuthorRetunringBio(ctx, arg)
56+
if err != nil {
57+
t.Fatalf("failed to create or update author: %v", err)
58+
}
59+
60+
if returnedBio == nil {
61+
t.Fatal("expected non-nil bio, got nil")
62+
}
63+
if *returnedBio != newBio {
64+
t.Fatalf("expected bio %q, got %q", newBio, *returnedBio)
65+
}
66+
67+
t.Logf("Author created or updated successfully with bio: %s", *returnedBio)
68+
})
69+
1970
t.Run("ListAuthors", func(t *testing.T) {
2071
authors, err := q.ListAuthors(ctx)
2172
if err != nil {
@@ -26,9 +77,9 @@ func TestAuthors(t *testing.T) {
2677
}
2778
t.Log("Authors:")
2879
for _, a := range authors {
29-
bio := "NULL"
30-
if a.Bio.Valid {
31-
bio = a.Bio.String
80+
bio := "Null"
81+
if a.Bio != nil {
82+
bio = *a.Bio
3283
}
3384
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
3485
}
@@ -39,9 +90,9 @@ func TestAuthors(t *testing.T) {
3990
if err != nil {
4091
t.Fatal(err)
4192
}
42-
bio := "NULL"
43-
if singleAuthor.Bio.Valid {
44-
bio = singleAuthor.Bio.String
93+
bio := "Null"
94+
if singleAuthor.Bio != nil {
95+
bio = *singleAuthor.Bio
4596
}
4697
t.Logf("- ID: %d | Name: %s | Bio: %s", singleAuthor.ID, singleAuthor.Name, bio)
4798
})
@@ -56,9 +107,9 @@ func TestAuthors(t *testing.T) {
56107
}
57108
t.Log("Authors with this name:")
58109
for _, a := range authors {
59-
bio := "NULL"
60-
if a.Bio.Valid {
61-
bio = a.Bio.String
110+
bio := "Null"
111+
if a.Bio != nil {
112+
bio = *a.Bio
62113
}
63114
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
64115
}
@@ -74,9 +125,9 @@ func TestAuthors(t *testing.T) {
74125
}
75126
t.Log("Authors with even IDs:")
76127
for _, a := range authors {
77-
bio := "NULL"
78-
if a.Bio.Valid {
79-
bio = a.Bio.String
128+
bio := "Null"
129+
if a.Bio != nil {
130+
bio = *a.Bio
80131
}
81132
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
82133
}
@@ -92,11 +143,27 @@ func TestAuthors(t *testing.T) {
92143
}
93144
t.Log("Authors with NULL bio:")
94145
for _, a := range authors {
95-
bio := "NULL"
96-
if a.Bio.Valid {
97-
bio = a.Bio.String
146+
bio := "Null"
147+
if a.Bio != nil {
148+
bio = *a.Bio
98149
}
99150
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
100151
}
101152
})
153+
154+
t.Run("Delete All Authors", func(t *testing.T) {
155+
var i uint64
156+
for i = 1; i <= 13; i++ {
157+
if err := q.DeleteAuthor(ctx, i); err != nil {
158+
t.Fatalf("failed to delete authors: %v", err)
159+
}
160+
}
161+
authors, err := q.ListAuthors(ctx)
162+
if err != nil {
163+
t.Fatal(err)
164+
}
165+
if len(authors) != 0 {
166+
t.Fatalf("expected no authors, got %d", len(authors))
167+
}
168+
})
102169
}

examples/authors/ydb/models.go

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/authors/ydb/query.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ WHERE name = $p0;
1717
SELECT * FROM authors
1818
WHERE bio IS NULL;
1919

20+
-- name: CreateOrUpdateAuthor :execresult
21+
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2);
22+
23+
-- name: CreateOrUpdateAuthorRetunringBio :one
24+
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2) RETURNING bio;
25+
26+
-- name: DeleteAuthor :exec
27+
DELETE FROM authors
28+
WHERE id = $p0;
29+

examples/authors/ydb/query.sql.go

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

0 commit comments

Comments
 (0)