Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e949b19
Initial YDB support: SELECT logic, some convert tests, and YDB engine…
1NepuNep1 Apr 22, 2025
4e46daa
Added almost full INSERT support and basic DELETE (without ON) support
1NepuNep1 Apr 26, 2025
3a4f818
Added UPDATE and DELETE full logic. Updated examples in example/authors
1NepuNep1 Apr 28, 2025
e980d19
Added some examples and README for demo
1NepuNep1 Apr 28, 2025
3013c84
Supported some more YQL constructions:
1NepuNep1 May 17, 2025
e687cf6
ADDED DROP/ALTER USER/GROUP
1NepuNep1 May 19, 2025
0293f6a
Added Funcs support!
1NepuNep1 May 20, 2025
fc8c932
Added examples for funcs codegen
1NepuNep1 May 21, 2025
d0e1550
Upgraded jwt to 4.5.2
1NepuNep1 May 27, 2025
0b1752a
First ydb-go-sdk generation version (See #4) (#6)
1NepuNep1 Sep 2, 2025
4eb8ff5
Bump sqlc version in examples/authors/ydb to v1.30.0
Sep 3, 2025
14b60c5
Rewrited comments and testdata to eng
Sep 3, 2025
f2d7aea
Expanding YQL Syntax support in sqlc engine (Refactored SELECT (Suppo…
1NepuNep1 Sep 8, 2025
296cca7
Made new params building logic & got rid off .Query handle (#11)
1NepuNep1 Sep 9, 2025
1379e43
Rewrited convert to Visitor Interface from ANTLR4 + fixed some types …
1NepuNep1 Sep 23, 2025
a680b1b
Massive Optional + Functions update (#13)
1NepuNep1 Sep 26, 2025
a69429c
Added simple types in param builder and ydbType (#14)
1NepuNep1 Oct 8, 2025
816eda8
Ydb-go-sdk codegen: Containers support in ydb.ParamsBuilder() (#15)
1NepuNep1 Oct 9, 2025
2eaa5d0
Tests: Added a lot of ydb e2e tests + some engine and types fixes
1NepuNep1 Oct 27, 2025
bde9d5e
Merge branch 'main' into ydb
1NepuNep1 Oct 27, 2025
08424e0
Resolved go.mod && go.sum
1NepuNep1 Oct 27, 2025
be13a37
Fixed builtins test
1NepuNep1 Oct 27, 2025
683926c
Added docs and rewrited examples for ydb (#17)
1NepuNep1 Oct 27, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: build build-endtoend test test-ci test-examples test-endtoend start psql mysqlsh proto
.PHONY: build build-endtoend test test-ci test-examples test-endtoend start psql mysqlsh proto sqlc-dev ydb test-examples-ydb gen-examples-ydb

build:
go build ./...
Expand All @@ -18,13 +18,21 @@ vet:
test-examples:
go test --tags=examples ./...

ydb-examples: sqlc-dev ydb gen-examples-ydb test-examples-ydb

test-examples-ydb:
YDB_SERVER_URI=localhost:2136 go test -v ./examples/authors/ydb/... -count=1

gen-examples-ydb:
cd examples/authors/ && SQLCDEBUG=1 ~/bin/sqlc-dev generate && cd ../..

build-endtoend:
cd ./internal/endtoend/testdata && go build ./...

test-ci: test-examples build-endtoend vet

sqlc-dev:
go build -o ~/bin/sqlc-dev ./cmd/sqlc/
go build -x -v -o ~/bin/sqlc-dev ./cmd/sqlc/

sqlc-pg-gen:
go build -o ~/bin/sqlc-pg-gen ./internal/tools/sqlc-pg-gen
Expand All @@ -38,6 +46,9 @@ test-json-process-plugin:
start:
docker compose up -d

ydb:
docker compose up -d ydb

fmt:
go fmt ./...

Expand Down
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ services:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: mysecretpassword
POSTGRES_USER: postgres

ydb:
image: ydbplatform/local-ydb:latest
ports:
- "2135:2135"
- "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

234 changes: 234 additions & 0 deletions docs/tutorials/getting-started-ydb.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
# Getting started with YDB

This tutorial assumes that the latest version of sqlc is
[installed](../overview/install.md) and ready to use.

We'll generate Go code here, but other
[language plugins](../reference/language-support.rst) are available. You'll
naturally need the Go toolchain if you want to build and run a program with the
code sqlc generates, but sqlc itself has no dependencies.

At the end, you'll push your SQL queries to [sqlc
Cloud](https://dashboard.sqlc.dev/) for further insights and analysis.

## Setting up

Create a new directory called `sqlc-tutorial` and open it up.

Initialize a new Go module named `tutorial.sqlc.dev/app`:

```shell
go mod init tutorial.sqlc.dev/app
```

sqlc looks for either a `sqlc.(yaml|yml)` or `sqlc.json` file in the current
directory. In our new directory, create a file named `sqlc.yaml` with the
following contents:

```yaml
version: "2"
sql:
- engine: "ydb"
queries: "query.sql"
schema: "schema.sql"
gen:
go:
package: "tutorial"
out: "tutorial"
```

## Schema and queries

sqlc needs to know your database schema and queries in order to generate code.
In the same directory, create a file named `schema.sql` with the following
content:

```sql
CREATE TABLE authors (
id Serial,
name Text NOT NULL,
bio Text,
PRIMARY KEY (id)
);
```

Next, create a `query.sql` file with the following five queries:

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

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;

-- name: CreateOrUpdateAuthor :one
UPSERT INTO authors (name, bio)
VALUES (
$name, $bio
)
RETURNING *;

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

-- name: DropTable :exec
DROP TABLE IF EXISTS authors;
```

Note that YDB uses named parameters (`$id`, `$name`, `$bio`) rather than
positional parameters.

## Generating code

You are now ready to generate code. You shouldn't see any output when you run
the `generate` subcommand, unless something goes wrong:

```shell
sqlc generate
```

You should now have a `tutorial` subdirectory with three files containing Go
source code. These files comprise a Go package named `tutorial`:

```
├── go.mod
├── query.sql
├── schema.sql
├── sqlc.yaml
└── tutorial
├── db.go
├── models.go
└── query.sql.go
```

## Using generated code

You can use your newly-generated `tutorial` package from any Go program.
Create a file named `tutorial.go` and add the following contents:

```go
package main

import (
"context"
"log"

"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/query"

"tutorial.sqlc.dev/app/tutorial"
)

func ptr(s string) *string {
return &s
}

func run() error {
ctx := context.Background()

// Create YDB connection
// Replace with your actual YDB endpoint
db, err := ydb.Open(ctx, "grpcs://localhost:2136/local")
if err != nil {
return err
}
defer db.Close(ctx)

queries := tutorial.New(db.Query())

// list all authors
authors, err := queries.ListAuthors(ctx)
if err != nil {
return err
}
log.Println(authors)

// create an author
insertedAuthor, err := queries.CreateOrUpdateAuthor(ctx, tutorial.CreateOrUpdateAuthorParams{
Name: "Brian Kernighan",
Bio: ptr("Co-author of The C Programming Language and The Go Programming Language"),
}, query.WithIdempotent())
if err != nil {
return err
}
log.Println(insertedAuthor)

// get the author we just inserted
fetchedAuthor, err := queries.GetAuthor(ctx, insertedAuthor.ID)
if err != nil {
return err
}
log.Println(fetchedAuthor)
return nil
}

func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
```

Before this code will compile you'll need to fetch the relevant YDB driver:

```shell
go get github.com/ydb-platform/ydb-go-sdk/v3
go build ./...
```

The program should compile without errors. To make that possible, sqlc generates
readable, **idiomatic** Go code that you otherwise would've had to write
yourself. Take a look in `tutorial/query.sql.go`.

Of course for this program to run successfully you'll need
to compile after replacing the database connection parameters in the call to
`ydb.Open()` with the correct parameters for your database. And your
database must have the `authors` table as defined in `schema.sql`.

You should now have a working program using sqlc's generated Go source code,
and hopefully can see how you'd use sqlc in your own real-world applications.

## Query verification (Not supported for YDB yet)

[sqlc Cloud](https://dashboard.sqlc.dev) provides additional verification, catching subtle bugs. To get started, create a
[dashboard account](https://dashboard.sqlc.dev). Once you've signed in, create a
project and generate an auth token. Add your project's ID to the `cloud` block
to your sqlc.yaml.

```yaml
version: "2"
cloud:
# Replace <PROJECT_ID> with your project ID from the sqlc Cloud dashboard
project: "<PROJECT_ID>"
sql:
- engine: "ydb"
queries: "query.sql"
schema: "schema.sql"
gen:
go:
package: "tutorial"
out: "tutorial"
```

Replace `<PROJECT_ID>` with your project ID from the sqlc Cloud dashboard. It
will look something like `01HA8SZH31HKYE9RR3N3N3TSJM`.

And finally, set the `SQLC_AUTH_TOKEN` environment variable:

```shell
export SQLC_AUTH_TOKEN="<your sqlc auth token>"
```

```shell
$ sqlc push --tag tutorial
```

In the sidebar, go to the "Queries" section to see your published queries. Run
`verify` to ensure that previously published queries continue to work against
updated database schema.

```shell
$ sqlc verify --against tutorial
```
12 changes: 12 additions & 0 deletions examples/authors/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ sql:
go:
package: authors
out: sqlite
- name: ydb
schema: ydb/schema.sql
queries: ydb/query.sql
engine: ydb
gen:
go:
package: authors
out: ydb
emit_json_tags: true
sql_package: ydb-go-sdk


rules:
- name: postgresql-query-too-costly
message: "Too costly"
Expand Down
26 changes: 26 additions & 0 deletions examples/authors/ydb/db.go

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

53 changes: 53 additions & 0 deletions examples/authors/ydb/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

package authors

import (
"context"
"testing"

"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 {
return &s
}

func TestAuthors(t *testing.T) {
ctx := context.Background()
db := local.YDB(t, []string{"schema.sql"})
defer db.Close(ctx)

q := New(db.Query())

// list all authors
authors, err := q.ListAuthors(ctx)
if err != nil {
t.Fatal(err)
}
t.Log(authors)

// create an author
insertedAuthor, err := q.CreateOrUpdateAuthor(ctx, CreateOrUpdateAuthorParams{
Name: "Brian Kernighan",
Bio: ptr("Co-author of The C Programming Language and The Go Programming Language"),
}, query.WithIdempotent())
if err != nil {
t.Fatal(err)
}
t.Log(insertedAuthor)

// get the author we just inserted
fetchedAuthor, err := q.GetAuthor(ctx, insertedAuthor.ID)
if err != nil {
t.Fatal(err)
}
t.Log(fetchedAuthor)

// drop table
err = q.DropTable(ctx)
if err != nil {
t.Fatal(err)
}
}
Loading
Loading