Skip to content

Commit 08121ec

Browse files
committed
Initial YDB support: SELECT logic, some convert tests, and YDB engine integration
- Added reserved keywords and parsing logic - CREATE TABLE support and tests - SELECT initial support and tests - Initial YDB engine integration and examples
1 parent da09201 commit 08121ec

File tree

22 files changed

+3345
-0
lines changed

22 files changed

+3345
-0
lines changed

docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ services:
1919
POSTGRES_DB: postgres
2020
POSTGRES_PASSWORD: mysecretpassword
2121
POSTGRES_USER: postgres
22+
23+
ydb:
24+
image: ydbplatform/local-ydb:latest
25+
ports:
26+
- "2135:2135"
27+
- "2136:2136"
28+
- "8765:8765"
29+
restart: always
30+
environment:
31+
- YDB_USE_IN_MEMORY_PDISKS=true
32+
- GRPC_TLS_PORT=2135
33+
- GRPC_PORT=2136
34+
- MON_PORT=8765

examples/authors/sqlc.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ sql:
4343
go:
4444
package: authors
4545
out: sqlite
46+
- name: ydb
47+
schema: ydb/schema.sql
48+
queries: ydb/query.sql
49+
engine: ydb
50+
gen:
51+
go:
52+
package: authors
53+
out: ydb
54+
55+
4656
rules:
4757
- name: postgresql-query-too-costly
4858
message: "Too costly"

examples/authors/ydb/db.go

Lines changed: 31 additions & 0 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: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package authors
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/sqlc-dev/sqlc/internal/sqltest/local"
8+
_ "github.com/ydb-platform/ydb-go-sdk/v3"
9+
)
10+
11+
func TestAuthors(t *testing.T) {
12+
ctx := context.Background()
13+
14+
test := local.YDB(t, []string{"schema.sql"})
15+
defer test.DB.Close()
16+
17+
q := New(test.DB)
18+
19+
t.Run("ListAuthors", func(t *testing.T) {
20+
authors, err := q.ListAuthors(ctx)
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
if len(authors) == 0 {
25+
t.Fatal("expected at least one author, got none")
26+
}
27+
t.Log("Authors:")
28+
for _, a := range authors {
29+
bio := "NULL"
30+
if a.Bio.Valid {
31+
bio = a.Bio.String
32+
}
33+
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
34+
}
35+
})
36+
37+
t.Run("GetAuthor", func(t *testing.T) {
38+
singleAuthor, err := q.GetAuthor(ctx, 10)
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
bio := "NULL"
43+
if singleAuthor.Bio.Valid {
44+
bio = singleAuthor.Bio.String
45+
}
46+
t.Logf("- ID: %d | Name: %s | Bio: %s", singleAuthor.ID, singleAuthor.Name, bio)
47+
})
48+
49+
t.Run("GetAuthorByName", func(t *testing.T) {
50+
authors, err := q.GetAuthorsByName(ctx, "Александр Пушкин")
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
if len(authors) == 0 {
55+
t.Fatal("expected at least one author with this name, got none")
56+
}
57+
t.Log("Authors with this name:")
58+
for _, a := range authors {
59+
bio := "NULL"
60+
if a.Bio.Valid {
61+
bio = a.Bio.String
62+
}
63+
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
64+
}
65+
})
66+
67+
t.Run("ListAuthorsWithIdModulo", func(t *testing.T) {
68+
authors, err := q.ListAuthorsWithIdModulo(ctx)
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
if len(authors) == 0 {
73+
t.Fatal("expected at least one author with even ID, got none")
74+
}
75+
t.Log("Authors with even IDs:")
76+
for _, a := range authors {
77+
bio := "NULL"
78+
if a.Bio.Valid {
79+
bio = a.Bio.String
80+
}
81+
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
82+
}
83+
})
84+
85+
t.Run("ListAuthorsWithNullBio", func(t *testing.T) {
86+
authors, err := q.ListAuthorsWithNullBio(ctx)
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
if len(authors) == 0 {
91+
t.Fatal("expected at least one author with NULL bio, got none")
92+
}
93+
t.Log("Authors with NULL bio:")
94+
for _, a := range authors {
95+
bio := "NULL"
96+
if a.Bio.Valid {
97+
bio = a.Bio.String
98+
}
99+
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
100+
}
101+
})
102+
}

examples/authors/ydb/models.go

Lines changed: 15 additions & 0 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- name: ListAuthors :many
2+
SELECT * FROM authors;
3+
4+
-- name: GetAuthor :one
5+
SELECT * FROM authors
6+
WHERE id = $p0;
7+
8+
-- name: ListAuthorsWithIdModulo :many
9+
SELECT * FROM authors
10+
WHERE id % 2 = 0;
11+
12+
-- name: GetAuthorsByName :many
13+
SELECT * FROM authors
14+
WHERE name = $p0;
15+
16+
-- name: ListAuthorsWithNullBio :many
17+
SELECT * FROM authors
18+
WHERE bio IS NULL;
19+

examples/authors/ydb/query.sql.go

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

examples/authors/ydb/schema.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE authors (
2+
id Uint64,
3+
name Utf8 NOT NULL,
4+
bio Utf8,
5+
PRIMARY KEY (id)
6+
);

internal/codegen/golang/go_type.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func goInnerType(req *plugin.GenerateRequest, options *opts.Options, col *plugin
8686
return postgresType(req, options, col)
8787
case "sqlite":
8888
return sqliteType(req, options, col)
89+
case "ydb":
90+
return YDBType(req, options, col)
8991
default:
9092
return "interface{}"
9193
}

0 commit comments

Comments
 (0)