Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ services:
- "2136:2136"
- "8765:8765"
restart: always
hostname: localhost
environment:
- YDB_USE_IN_MEMORY_PDISKS=true
- GRPC_TLS_PORT=2135
- GRPC_PORT=2136
- MON_PORT=8765

1 change: 1 addition & 0 deletions examples/authors/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ sql:
package: authors
out: ydb
emit_json_tags: true
sql_package: ydb-go-sdk


rules:
Expand Down
17 changes: 6 additions & 11 deletions examples/authors/ydb/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 13 additions & 82 deletions examples/authors/ydb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/sqlc-dev/sqlc/internal/sqltest/local"
_ "github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/query"
)

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

test := local.YDB(t, []string{"schema.sql"})
defer test.DB.Close()
db := local.YDB(t, []string{"schema.sql"})
defer db.Close(ctx)

q := New(test.DB)
q := New(db.Query())

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

for _, author := range authorsToInsert {
if _, err := q.CreateOrUpdateAuthor(ctx, author); err != nil {
if err := q.CreateOrUpdateAuthor(ctx, author, query.WithIdempotent()); err != nil {
t.Fatalf("failed to insert author %q: %v", author.P1, err)
}
}
})

t.Run("CreateOrUpdateAuthorReturningBio", func(t *testing.T) {
newBio := "Обновленная биография автора"
arg := CreateOrUpdateAuthorReturningBioParams{
P0: 3,
P1: "Тестовый Автор",
P2: &newBio,
}

returnedBio, err := q.CreateOrUpdateAuthorReturningBio(ctx, arg)
if err != nil {
t.Fatalf("failed to create or update author: %v", err)
}

if returnedBio == nil {
t.Fatal("expected non-nil bio, got nil")
}
if *returnedBio != newBio {
t.Fatalf("expected bio %q, got %q", newBio, *returnedBio)
}

t.Logf("Author created or updated successfully with bio: %s", *returnedBio)
})

t.Run("Update Author", func(t *testing.T) {
arg := UpdateAuthorByIDParams{
P0: "Максим Горький",
P1: ptr("Обновленная биография"),
P2: 10,
}

singleAuthor, err := q.UpdateAuthorByID(ctx, arg)
if err != nil {
t.Fatal(err)
}
bio := "Null"
if singleAuthor.Bio != nil {
bio = *singleAuthor.Bio
}
t.Logf("- ID: %d | Name: %s | Bio: %s", singleAuthor.ID, singleAuthor.Name, bio)
})

t.Run("ListAuthors", func(t *testing.T) {
authors, err := q.ListAuthors(ctx)
if err != nil {
Expand Down Expand Up @@ -115,46 +75,10 @@ func TestAuthors(t *testing.T) {
t.Logf("- ID: %d | Name: %s | Bio: %s", singleAuthor.ID, singleAuthor.Name, bio)
})

t.Run("GetAuthorByName", func(t *testing.T) {
authors, err := q.GetAuthorsByName(ctx, "Александр Пушкин")
if err != nil {
t.Fatal(err)
}
if len(authors) == 0 {
t.Fatal("expected at least one author with this name, got none")
}
t.Log("Authors with this name:")
for _, a := range authors {
bio := "Null"
if a.Bio != nil {
bio = *a.Bio
}
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
}
})

t.Run("ListAuthorsWithNullBio", func(t *testing.T) {
authors, err := q.ListAuthorsWithNullBio(ctx)
if err != nil {
t.Fatal(err)
}
if len(authors) == 0 {
t.Fatal("expected at least one author with NULL bio, got none")
}
t.Log("Authors with NULL bio:")
for _, a := range authors {
bio := "Null"
if a.Bio != nil {
bio = *a.Bio
}
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
}
})

t.Run("Delete All Authors", func(t *testing.T) {
var i uint64
for i = 1; i <= 13; i++ {
if err := q.DeleteAuthor(ctx, i); err != nil {
if err := q.DeleteAuthor(ctx, i, query.WithIdempotent()); err != nil {
t.Fatalf("failed to delete authors: %v", err)
}
}
Expand All @@ -166,4 +90,11 @@ func TestAuthors(t *testing.T) {
t.Fatalf("expected no authors, got %d", len(authors))
}
})

t.Run("Drop Table Authors", func(t *testing.T) {
err := q.DropTable(ctx)
if err != nil {
t.Fatal(err)
}
})
}
29 changes: 6 additions & 23 deletions examples/authors/ydb/query.sql
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
-- name: ListAuthors :many
SELECT * FROM authors;

-- name: GetAuthor :one
SELECT * FROM authors
WHERE id = $p0;
WHERE id = $p0 LIMIT 1;

-- name: GetAuthorsByName :many
SELECT * FROM authors
WHERE name = $p0;

-- name: ListAuthorsWithNullBio :many
SELECT * FROM authors
WHERE bio IS NULL;

-- name: Count :one
SELECT COUNT(*) FROM authors;

-- name: COALESCE :many
SELECT id, name, COALESCE(bio, 'Null value!') FROM authors;
-- name: ListAuthors :many
SELECT * FROM authors ORDER BY name;

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

-- name: CreateOrUpdateAuthorReturningBio :one
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2) RETURNING bio;

-- name: DeleteAuthor :exec
DELETE FROM authors WHERE id = $p0;

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