Skip to content

Commit a075a83

Browse files
committed
codegen/golang: bind named placeholders with sql.Named, and run the new engines' examples
SQL Server and Spanner name a parameter @name in the query text and ClickHouse's server-side placeholder is {name:Type}; their drivers take such an argument as sql.Named rather than by position. When every parameter of a query is named that way, pass each as sql.Named with the name the query uses. A query written with ? keeps positional arguments. Add an authors example for ClickHouse, SQL Server and Spanner beside the existing ones, and one for DuckDB in a module of its own, since its driver is cgo and the main module's builds keep cgo off. Test helpers create a database per test on the server CLICKHOUSE_SERVER_URI, MSSQL_SERVER_URI or SPANNER_SERVER_URI names and skip when none is; the DuckDB example opens a database in memory. CI runs ClickHouse and SQL Server as services, builds the end-to-end module with cgo for the DuckDB driver, and tests the DuckDB module; docker-compose gains a ClickHouse service beside the SQL Server and Spanner Omni ones for local runs. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015rfiHyC4iRLyRWc3UmYEtT
1 parent 32194c9 commit a075a83

43 files changed

Lines changed: 1635 additions & 44 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/ci.yml‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ jobs:
2424
GOARCH: ${{ matrix.goarch }}
2525
test:
2626
runs-on: ubuntu-24.04
27+
services:
28+
clickhouse:
29+
image: clickhouse/clickhouse-server:25.8
30+
ports:
31+
- 9000:9000
32+
- 8123:8123
33+
mssql:
34+
image: mcr.microsoft.com/mssql/server:2025-latest
35+
ports:
36+
- 1433:1433
37+
env:
38+
ACCEPT_EULA: "Y"
39+
MSSQL_SA_PASSWORD: "Mysecretpassword1!"
2740
steps:
2841
- uses: actions/checkout@v7
2942
- uses: actions/setup-go@v7
@@ -44,11 +57,11 @@ jobs:
4457
env:
4558
CGO_ENABLED: "0"
4659

60+
# The DuckDB driver the generated code imports is cgo, so this build
61+
# keeps cgo on.
4762
- name: build internal/endtoend
4863
run: go build ./...
4964
working-directory: internal/endtoend/testdata
50-
env:
51-
CGO_ENABLED: "0"
5265

5366
- name: install databases
5467
run: go run ./cmd/sqlc-test-setup install
@@ -64,8 +77,16 @@ jobs:
6477
SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }}
6578
POSTGRESQL_SERVER_URI: "postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable"
6679
MYSQL_SERVER_URI: "root:mysecretpassword@tcp(127.0.0.1:3306)/mysql?multiStatements=true&parseTime=true"
80+
CLICKHOUSE_SERVER_URI: "clickhouse://default:@127.0.0.1:9000"
81+
MSSQL_SERVER_URI: "sqlserver://sa:Mysecretpassword1!@127.0.0.1:1433?encrypt=disable"
6782
CGO_ENABLED: "0"
6883

84+
# The DuckDB example is its own module: its driver is cgo, which the
85+
# main module's builds keep off.
86+
- name: test examples/duckdb
87+
run: go test --tags=examples ./...
88+
working-directory: examples/duckdb
89+
6990
vuln_check:
7091
runs-on: ubuntu-24.04
7192
timeout-minutes: 5

‎Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ vet:
1717

1818
test-examples:
1919
go test --tags=examples ./...
20+
cd ./examples/duckdb && go test --tags=examples ./...
2021

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

‎docker-compose.yml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ services:
2020
POSTGRES_PASSWORD: mysecretpassword
2121
POSTGRES_USER: postgres
2222

23+
clickhouse:
24+
image: "clickhouse/clickhouse-server:25.8"
25+
ports:
26+
- "8123:8123"
27+
- "9000:9000"
28+
restart: always
29+
environment:
30+
CLICKHOUSE_USER: default
31+
CLICKHOUSE_PASSWORD: ""
32+
2333
mssql:
2434
image: "mcr.microsoft.com/mssql/server:2025-latest"
2535
ports:

‎docs/reference/language-support.md‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
# Database and language support
22

3-
| Language | Plugin | MySQL | PostgreSQL | SQLite |
4-
| ---------- | ---------------------------------------------------------------------- | ------ | ---------- | --------------- |
5-
| Go | (built-in) | Stable | Stable | Beta |
6-
| Go | [sqlc-gen-go](https://github.com/sqlc-dev/sqlc-gen-go) | Stable | Stable | Beta |
7-
| Kotlin | [sqlc-gen-kotlin](https://github.com/sqlc-dev/sqlc-gen-kotlin) | Beta | Beta | Not implemented |
8-
| Python | [sqlc-gen-python](https://github.com/sqlc-dev/sqlc-gen-python) | Beta | Beta | Not implemented |
9-
| TypeScript | [sqlc-gen-typescript](https://github.com/sqlc-dev/sqlc-gen-typescript) | Beta | Beta | Not implemented |
3+
| Language | Plugin | MySQL | PostgreSQL | SQLite | ClickHouse | DuckDB | Spanner | SQL Server |
4+
| ---------- | ---------------------------------------------------------------------- | ------ | ---------- | --------------- | --------------- | --------------- | --------------- | --------------- |
5+
| Go | (built-in) | Stable | Stable | Beta | Beta | Beta | Beta | Beta |
6+
| Go | [sqlc-gen-go](https://github.com/sqlc-dev/sqlc-gen-go) | Stable | Stable | Beta | Not implemented | Not implemented | Not implemented | Not implemented |
7+
| Kotlin | [sqlc-gen-kotlin](https://github.com/sqlc-dev/sqlc-gen-kotlin) | Beta | Beta | Not implemented | Not implemented | Not implemented | Not implemented | Not implemented |
8+
| Python | [sqlc-gen-python](https://github.com/sqlc-dev/sqlc-gen-python) | Beta | Beta | Not implemented | Not implemented | Not implemented | Not implemented | Not implemented |
9+
| TypeScript | [sqlc-gen-typescript](https://github.com/sqlc-dev/sqlc-gen-typescript) | Beta | Beta | Not implemented | Not implemented | Not implemented | Not implemented | Not implemented |
10+
11+
The built-in Go generator targets `database/sql` for ClickHouse, DuckDB,
12+
Spanner and SQL Server, with the types their drivers hand back:
13+
[clickhouse-go](https://github.com/ClickHouse/clickhouse-go),
14+
[duckdb-go](https://github.com/duckdb/duckdb-go),
15+
[go-sql-spanner](https://github.com/googleapis/go-sql-spanner) and
16+
[go-mssqldb](https://github.com/microsoft/go-mssqldb). Enums and alias types
17+
are not yet resolved for these engines and come out as `any`.
1018

1119
## Community language support
1220

‎examples/authors/clickhouse/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.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//go:build examples
2+
3+
package authors
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
"testing"
9+
10+
_ "github.com/ClickHouse/clickhouse-go/v2"
11+
12+
"github.com/sqlc-dev/sqlc/internal/sqltest/local"
13+
)
14+
15+
func TestAuthors(t *testing.T) {
16+
ctx := context.Background()
17+
uri := local.ClickHouse(t, []string{"schema.sql"})
18+
sdb, err := sql.Open("clickhouse", uri)
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
defer sdb.Close()
23+
24+
db := New(sdb)
25+
26+
// list all authors
27+
authors, err := db.ListAuthors(ctx)
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
t.Log(authors)
32+
33+
// create an author
34+
err = db.CreateAuthor(ctx, CreateAuthorParams{
35+
ID: 1,
36+
Name: "Brian Kernighan",
37+
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
38+
})
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
// get the author we just inserted
44+
fetchedAuthor, err := db.GetAuthor(ctx, 1)
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
if fetchedAuthor.Name != "Brian Kernighan" || !fetchedAuthor.Bio.Valid {
49+
t.Fatalf("unexpected author: %+v", fetchedAuthor)
50+
}
51+
t.Log(fetchedAuthor)
52+
53+
// delete the author
54+
if err := db.DeleteAuthor(ctx, 1); err != nil {
55+
t.Fatal(err)
56+
}
57+
}

‎examples/authors/clickhouse/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.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- name: GetAuthor :one
2+
SELECT * FROM authors
3+
WHERE id = ? LIMIT 1;
4+
5+
-- name: ListAuthors :many
6+
SELECT * FROM authors
7+
ORDER BY name;
8+
9+
-- name: CreateAuthor :exec
10+
INSERT INTO authors (
11+
id, name, bio
12+
) VALUES (
13+
?, ?, ?
14+
);
15+
16+
-- name: DeleteAuthor :exec
17+
DELETE FROM authors
18+
WHERE id = ?;

‎examples/authors/clickhouse/query.sql.go‎

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE authors (
2+
id UInt64,
3+
name String,
4+
bio Nullable(String)
5+
) ENGINE = MergeTree ORDER BY id;

0 commit comments

Comments
 (0)