Skip to content

Commit 0b1752a

Browse files
1NepuNep1Viktor Pentyukhov
authored andcommitted
First ydb-go-sdk generation version (See #4) (#6)
This PR adds native YDB Go SDK support to sqlc for YDB database engine, moving from the standard database/sql interface to the YDB-specific SDK. The implementation includes code generation templates, configuration updates, and example adaptations. Adds YDB Go SDK as a new SQL package option (ydb-go-sdk) Implements YDB-specific code generation templates for queries, interfaces, and database connections Updates configuration schema to support YDB as an engine option
1 parent d0e1550 commit 0b1752a

File tree

19 files changed

+446
-337
lines changed

19 files changed

+446
-337
lines changed

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ services:
2727
- "2136:2136"
2828
- "8765:8765"
2929
restart: always
30+
hostname: localhost
3031
environment:
3132
- YDB_USE_IN_MEMORY_PDISKS=true
3233
- GRPC_TLS_PORT=2135
3334
- GRPC_PORT=2136
3435
- MON_PORT=8765
36+

examples/authors/sqlc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ sql:
5252
package: authors
5353
out: ydb
5454
emit_json_tags: true
55+
sql_package: ydb-go-sdk
5556

5657

5758
rules:

examples/authors/ydb/db.go

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

examples/authors/ydb/db_test.go

Lines changed: 13 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/sqlc-dev/sqlc/internal/sqltest/local"
88
_ "github.com/ydb-platform/ydb-go-sdk/v3"
9+
"github.com/ydb-platform/ydb-go-sdk/v3/query"
910
)
1011

1112
func ptr(s string) *string {
@@ -15,10 +16,10 @@ func ptr(s string) *string {
1516
func TestAuthors(t *testing.T) {
1617
ctx := context.Background()
1718

18-
test := local.YDB(t, []string{"schema.sql"})
19-
defer test.DB.Close()
19+
db := local.YDB(t, []string{"schema.sql"})
20+
defer db.Close(ctx)
2021

21-
q := New(test.DB)
22+
q := New(db.Query())
2223

2324
t.Run("InsertAuthors", func(t *testing.T) {
2425
authorsToInsert := []CreateOrUpdateAuthorParams{
@@ -38,53 +39,12 @@ func TestAuthors(t *testing.T) {
3839
}
3940

4041
for _, author := range authorsToInsert {
41-
if _, err := q.CreateOrUpdateAuthor(ctx, author); err != nil {
42+
if err := q.CreateOrUpdateAuthor(ctx, author, query.WithIdempotent()); err != nil {
4243
t.Fatalf("failed to insert author %q: %v", author.P1, err)
4344
}
4445
}
4546
})
4647

47-
t.Run("CreateOrUpdateAuthorReturningBio", func(t *testing.T) {
48-
newBio := "Обновленная биография автора"
49-
arg := CreateOrUpdateAuthorReturningBioParams{
50-
P0: 3,
51-
P1: "Тестовый Автор",
52-
P2: &newBio,
53-
}
54-
55-
returnedBio, err := q.CreateOrUpdateAuthorReturningBio(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-
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-
8848
t.Run("ListAuthors", func(t *testing.T) {
8949
authors, err := q.ListAuthors(ctx)
9050
if err != nil {
@@ -115,46 +75,10 @@ func TestAuthors(t *testing.T) {
11575
t.Logf("- ID: %d | Name: %s | Bio: %s", singleAuthor.ID, singleAuthor.Name, bio)
11676
})
11777

118-
t.Run("GetAuthorByName", func(t *testing.T) {
119-
authors, err := q.GetAuthorsByName(ctx, "Александр Пушкин")
120-
if err != nil {
121-
t.Fatal(err)
122-
}
123-
if len(authors) == 0 {
124-
t.Fatal("expected at least one author with this name, got none")
125-
}
126-
t.Log("Authors with this name:")
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-
136-
t.Run("ListAuthorsWithNullBio", func(t *testing.T) {
137-
authors, err := q.ListAuthorsWithNullBio(ctx)
138-
if err != nil {
139-
t.Fatal(err)
140-
}
141-
if len(authors) == 0 {
142-
t.Fatal("expected at least one author with NULL bio, got none")
143-
}
144-
t.Log("Authors with NULL bio:")
145-
for _, a := range authors {
146-
bio := "Null"
147-
if a.Bio != nil {
148-
bio = *a.Bio
149-
}
150-
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
151-
}
152-
})
153-
15478
t.Run("Delete All Authors", func(t *testing.T) {
15579
var i uint64
15680
for i = 1; i <= 13; i++ {
157-
if err := q.DeleteAuthor(ctx, i); err != nil {
81+
if err := q.DeleteAuthor(ctx, i, query.WithIdempotent()); err != nil {
15882
t.Fatalf("failed to delete authors: %v", err)
15983
}
16084
}
@@ -166,4 +90,11 @@ func TestAuthors(t *testing.T) {
16690
t.Fatalf("expected no authors, got %d", len(authors))
16791
}
16892
})
93+
94+
t.Run("Drop Table Authors", func(t *testing.T) {
95+
err := q.DropTable(ctx)
96+
if err != nil {
97+
t.Fatal(err)
98+
}
99+
})
169100
}

examples/authors/ydb/query.sql

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
1-
-- name: ListAuthors :many
2-
SELECT * FROM authors;
3-
41
-- name: GetAuthor :one
52
SELECT * FROM authors
6-
WHERE id = $p0;
3+
WHERE id = $p0 LIMIT 1;
74

8-
-- name: GetAuthorsByName :many
9-
SELECT * FROM authors
10-
WHERE name = $p0;
11-
12-
-- name: ListAuthorsWithNullBio :many
13-
SELECT * FROM authors
14-
WHERE bio IS NULL;
15-
16-
-- name: Count :one
17-
SELECT COUNT(*) FROM authors;
18-
19-
-- name: COALESCE :many
20-
SELECT id, name, COALESCE(bio, 'Null value!') FROM authors;
5+
-- name: ListAuthors :many
6+
SELECT * FROM authors ORDER BY name;
217

22-
-- name: CreateOrUpdateAuthor :execresult
8+
-- name: CreateOrUpdateAuthor :exec
239
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2);
2410

25-
-- name: CreateOrUpdateAuthorReturningBio :one
26-
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2) RETURNING bio;
27-
2811
-- name: DeleteAuthor :exec
2912
DELETE FROM authors WHERE id = $p0;
3013

31-
-- name: UpdateAuthorByID :one
32-
UPDATE authors SET name = $p0, bio = $p1 WHERE id = $p2 RETURNING *;
14+
-- name: DropTable :exec
15+
DROP TABLE IF EXISTS authors;

0 commit comments

Comments
 (0)