Skip to content

Commit a868eb2

Browse files
committed
feat: add emit_models and models_package settings
1 parent 5ab6799 commit a868eb2

33 files changed

Lines changed: 181 additions & 38 deletions

File tree

bin/sqlc-gen-python.wasm

3.03 KB
Binary file not shown.

internal/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ type Config struct {
1010
EmitStrEnum bool `json:"emit_str_enum"`
1111
QueryParameterLimit *int32 `json:"query_parameter_limit"`
1212
InflectionExcludeTableNames []string `json:"inflection_exclude_table_names"`
13+
EmitModels *bool `json:"emit_models"`
14+
ModelsPackage string `json:"models_package"`
15+
}
16+
17+
// ShouldEmitModels returns true unless emit_models was explicitly set to false.
18+
func (c Config) ShouldEmitModels() bool {
19+
return c.EmitModels == nil || *c.EmitModels
1320
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.30.0
4+
# source: query.sql
5+
from typing import Iterator, Optional
6+
7+
import sqlalchemy
8+
9+
from src.db.generated import models
10+
11+
12+
CREATE_BOOK = """-- name: create_book \\:one
13+
INSERT INTO books (
14+
title, status
15+
) VALUES (
16+
:p1, :p2
17+
) RETURNING id, title, status
18+
"""
19+
20+
21+
DELETE_BOOK = """-- name: delete_book \\:exec
22+
DELETE FROM books
23+
WHERE id = :p1
24+
"""
25+
26+
27+
GET_BOOK = """-- name: get_book \\:one
28+
SELECT id, title, status FROM books
29+
WHERE id = :p1 LIMIT 1
30+
"""
31+
32+
33+
LIST_BOOKS = """-- name: list_books \\:many
34+
SELECT id, title, status FROM books
35+
ORDER BY title
36+
"""
37+
38+
39+
class Querier:
40+
def __init__(self, conn: sqlalchemy.engine.Connection):
41+
self._conn = conn
42+
43+
def create_book(self, *, title: str, status: Optional[models.BookStatus]) -> Optional[models.Book]:
44+
row = self._conn.execute(sqlalchemy.text(CREATE_BOOK), {"p1": title, "p2": status}).first()
45+
if row is None:
46+
return None
47+
return models.Book(
48+
id=row[0],
49+
title=row[1],
50+
status=row[2],
51+
)
52+
53+
def delete_book(self, *, id: int) -> None:
54+
self._conn.execute(sqlalchemy.text(DELETE_BOOK), {"p1": id})
55+
56+
def get_book(self, *, id: int) -> Optional[models.Book]:
57+
row = self._conn.execute(sqlalchemy.text(GET_BOOK), {"p1": id}).first()
58+
if row is None:
59+
return None
60+
return models.Book(
61+
id=row[0],
62+
title=row[1],
63+
status=row[2],
64+
)
65+
66+
def list_books(self) -> Iterator[models.Book]:
67+
result = self._conn.execute(sqlalchemy.text(LIST_BOOKS))
68+
for row in result:
69+
yield models.Book(
70+
id=row[0],
71+
title=row[1],
72+
status=row[2],
73+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- name: GetBook :one
2+
SELECT * FROM books
3+
WHERE id = $1 LIMIT 1;
4+
5+
-- name: ListBooks :many
6+
SELECT * FROM books
7+
ORDER BY title;
8+
9+
-- name: CreateBook :one
10+
INSERT INTO books (
11+
title, status
12+
) VALUES (
13+
$1, $2
14+
) RETURNING *;
15+
16+
-- name: DeleteBook :exec
17+
DELETE FROM books
18+
WHERE id = $1;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TYPE book_status AS ENUM ('available', 'checked_out', 'overdue');
2+
3+
4+
CREATE TABLE books (
5+
id BIGSERIAL PRIMARY KEY,
6+
title text NOT NULL,
7+
status book_status DEFAULT 'available'
8+
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: "2"
2+
plugins:
3+
- name: py
4+
wasm:
5+
url: file://../../../../bin/sqlc-gen-python.wasm
6+
sha256: "37ef74de82c70bcb6a96ea8e8c950c05e3054c5bfe3ea8a06b874d32fad20e5e"
7+
sql:
8+
- schema: schema.sql
9+
queries: query.sql
10+
engine: postgresql
11+
codegen:
12+
- plugin: py
13+
out: db
14+
options:
15+
package: db
16+
emit_sync_querier: true
17+
emit_models: false
18+
models_package: src.db.generated.models

internal/endtoend/testdata/emit_pydantic_models/db/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Code generated by sqlc. DO NOT EDIT.
22
# versions:
3-
# sqlc v1.28.0
3+
# sqlc v1.30.0
44
import pydantic
55
from typing import Optional
66

internal/endtoend/testdata/emit_pydantic_models/db/query.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Code generated by sqlc. DO NOT EDIT.
22
# versions:
3-
# sqlc v1.28.0
3+
# sqlc v1.30.0
44
# source: query.sql
5-
from typing import AsyncIterator, Iterator, Optional
5+
from typing import Iterator, List, Optional
66

77
import sqlalchemy
88
import sqlalchemy.ext.asyncio
@@ -102,11 +102,13 @@ async def get_author(self, *, id: int) -> Optional[models.Author]:
102102
bio=row[2],
103103
)
104104

105-
async def list_authors(self) -> AsyncIterator[models.Author]:
105+
async def list_authors(self) -> List[models.Author]:
106106
result = await self._conn.stream(sqlalchemy.text(LIST_AUTHORS))
107+
items = list()
107108
async for row in result:
108-
yield models.Author(
109+
items.append(models.Author(
109110
id=row[0],
110111
name=row[1],
111112
bio=row[2],
112-
)
113+
))
114+
return items

internal/endtoend/testdata/emit_pydantic_models/sqlc.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins:
33
- name: py
44
wasm:
55
url: file://../../../../bin/sqlc-gen-python.wasm
6-
sha256: "d6846ffad948181e611e883cedd2d2be66e091edc1273a0abc6c9da18399e0ca"
6+
sha256: "37ef74de82c70bcb6a96ea8e8c950c05e3054c5bfe3ea8a06b874d32fad20e5e"
77
sql:
88
- schema: schema.sql
99
queries: query.sql

internal/endtoend/testdata/emit_str_enum/db/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Code generated by sqlc. DO NOT EDIT.
22
# versions:
3-
# sqlc v1.28.0
3+
# sqlc v1.30.0
44
import dataclasses
55
import enum
66
from typing import Optional

0 commit comments

Comments
 (0)