Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd89f61d

Browse files
authored
feat: add emit_str_enum config option (#66)
* feat: add emit_str_enum config option* docs: add docs for emit_str_enum* tests(emit_str_enum): add end to end test for emit str enum* chore(tests): update wasm sha* docs: add examples of with w/o* docs: update to use correct dataclass syntax
1 parent0a64f0c commitd89f61d

File tree

16 files changed

+268
-28
lines changed

16 files changed

+268
-28
lines changed

‎README.md

Lines changed: 72 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,78 @@
11
##Usage
22

33
```yaml
4-
version:'2'
4+
version:"2"
55
plugins:
6-
-name:py
7-
wasm:
8-
url:https://downloads.sqlc.dev/plugin/sqlc-gen-python_1.2.0.wasm
9-
sha256:a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e
6+
-name:py
7+
wasm:
8+
url:https://downloads.sqlc.dev/plugin/sqlc-gen-python_1.2.0.wasm
9+
sha256:a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e
1010
sql:
11-
-schema:"schema.sql"
12-
queries:"query.sql"
13-
engine:postgresql
14-
codegen:
15-
-out:src/authors
16-
plugin:py
17-
options:
18-
package:authors
19-
emit_sync_querier:true
20-
emit_async_querier:true
11+
-schema:"schema.sql"
12+
queries:"query.sql"
13+
engine:postgresql
14+
codegen:
15+
-out:src/authors
16+
plugin:py
17+
options:
18+
package:authors
19+
emit_sync_querier:true
20+
emit_async_querier:true
21+
```
22+
23+
### Emit Pydantic Models instead of`dataclasses`
24+
25+
Option:`emit_pydantic_models`
26+
27+
By default, `sqlc-gen-python` will emit `dataclasses` for the models. If you prefer to use [`pydantic`](https://docs.pydantic.dev/latest/) models, you can enable this option.
28+
29+
with `emit_pydantic_models`
30+
31+
```py
32+
from pydantic import BaseModel
33+
34+
class Author(pydantic.BaseModel):
35+
id: int
36+
name: str
37+
```
38+
39+
without `emit_pydantic_models`
40+
41+
```py
42+
import dataclasses
43+
44+
@dataclasses.dataclass()
45+
class Author:
46+
id: int
47+
name: str
48+
```
49+
50+
### Use `enum.StrEnum` for Enums
51+
52+
Option:`emit_str_enum`
53+
54+
`enum.StrEnum`was introduce in Python 3.11.
55+
56+
`enum.StrEnum`is a subclass of `str` that is also a subclass of `Enum`. This allows for the use of `Enum` values as strings, compared to strings, or compared to other `enum.StrEnum` types.
57+
58+
This is convenient for type checking and validation, as well as for serialization and deserialization.
59+
60+
By default, `sqlc-gen-python` will emit `(str, enum.Enum)` for the enum classes. If you prefer to use `enum.StrEnum`, you can enable this option.
61+
62+
with `emit_str_enum`
63+
64+
```py
65+
class Status(enum.StrEnum):
66+
"""Venues can be either open or closed"""
67+
OPEN = "op!en"
68+
CLOSED = "clo@sed"
69+
```
70+
71+
without `emit_str_enum` (current behavior)
72+
73+
```py
74+
class Status(str, enum.Enum):
75+
"""Venues can be either open or closed"""
76+
OPEN = "op!en"
77+
CLOSED = "clo@sed"
2178
```

‎internal/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Config struct {
77
Packagestring`json:"package"`
88
Outstring`json:"out"`
99
EmitPydanticModelsbool`json:"emit_pydantic_models"`
10+
EmitStrEnumbool`json:"emit_str_enum"`
1011
QueryParameterLimit*int32`json:"query_parameter_limit"`
1112
InflectionExcludeTableNames []string`json:"inflection_exclude_table_names"`
1213
}

‎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:"a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
6+
sha256:"d6846ffad948181e611e883cedd2d2be66e091edc1273a0abc6c9da18399e0ca"
77
sql:
88
-schema:schema.sql
99
queries:query.sql
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.27.0
4+
importdataclasses
5+
importenum
6+
fromtypingimportOptional
7+
8+
9+
classBookStatus(enum.StrEnum):
10+
AVAILABLE="available"
11+
CHECKED_OUT="checked_out"
12+
OVERDUE="overdue"
13+
14+
15+
@dataclasses.dataclass()
16+
classBook:
17+
id:int
18+
title:str
19+
status:Optional[BookStatus]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Code generated by sqlc. DO NOT EDIT.
2+
# versions:
3+
# sqlc v1.27.0
4+
# source: query.sql
5+
fromtypingimportAsyncIterator,Iterator,Optional
6+
7+
importsqlalchemy
8+
importsqlalchemy.ext.asyncio
9+
10+
fromdbimportmodels
11+
12+
13+
CREATE_BOOK="""-- name: create_book\\:one
14+
INSERT INTO books (
15+
title, status
16+
) VALUES (
17+
:p1, :p2
18+
) RETURNING id, title, status
19+
"""
20+
21+
22+
DELETE_BOOK="""-- name: delete_book\\:exec
23+
DELETE FROM books
24+
WHERE id = :p1
25+
"""
26+
27+
28+
GET_BOOK="""-- name: get_book\\:one
29+
SELECT id, title, status FROM books
30+
WHERE id = :p1 LIMIT 1
31+
"""
32+
33+
34+
LIST_BOOKS="""-- name: list_books\\:many
35+
SELECT id, title, status FROM books
36+
ORDER BY title
37+
"""
38+
39+
40+
classQuerier:
41+
def__init__(self,conn:sqlalchemy.engine.Connection):
42+
self._conn=conn
43+
44+
defcreate_book(self,*,title:str,status:Optional[models.BookStatus])->Optional[models.Book]:
45+
row=self._conn.execute(sqlalchemy.text(CREATE_BOOK), {"p1":title,"p2":status}).first()
46+
ifrowisNone:
47+
returnNone
48+
returnmodels.Book(
49+
id=row[0],
50+
title=row[1],
51+
status=row[2],
52+
)
53+
54+
defdelete_book(self,*,id:int)->None:
55+
self._conn.execute(sqlalchemy.text(DELETE_BOOK), {"p1":id})
56+
57+
defget_book(self,*,id:int)->Optional[models.Book]:
58+
row=self._conn.execute(sqlalchemy.text(GET_BOOK), {"p1":id}).first()
59+
ifrowisNone:
60+
returnNone
61+
returnmodels.Book(
62+
id=row[0],
63+
title=row[1],
64+
status=row[2],
65+
)
66+
67+
deflist_books(self)->Iterator[models.Book]:
68+
result=self._conn.execute(sqlalchemy.text(LIST_BOOKS))
69+
forrowinresult:
70+
yieldmodels.Book(
71+
id=row[0],
72+
title=row[1],
73+
status=row[2],
74+
)
75+
76+
77+
classAsyncQuerier:
78+
def__init__(self,conn:sqlalchemy.ext.asyncio.AsyncConnection):
79+
self._conn=conn
80+
81+
asyncdefcreate_book(self,*,title:str,status:Optional[models.BookStatus])->Optional[models.Book]:
82+
row= (awaitself._conn.execute(sqlalchemy.text(CREATE_BOOK), {"p1":title,"p2":status})).first()
83+
ifrowisNone:
84+
returnNone
85+
returnmodels.Book(
86+
id=row[0],
87+
title=row[1],
88+
status=row[2],
89+
)
90+
91+
asyncdefdelete_book(self,*,id:int)->None:
92+
awaitself._conn.execute(sqlalchemy.text(DELETE_BOOK), {"p1":id})
93+
94+
asyncdefget_book(self,*,id:int)->Optional[models.Book]:
95+
row= (awaitself._conn.execute(sqlalchemy.text(GET_BOOK), {"p1":id})).first()
96+
ifrowisNone:
97+
returnNone
98+
returnmodels.Book(
99+
id=row[0],
100+
title=row[1],
101+
status=row[2],
102+
)
103+
104+
asyncdeflist_books(self)->AsyncIterator[models.Book]:
105+
result=awaitself._conn.stream(sqlalchemy.text(LIST_BOOKS))
106+
asyncforrowinresult:
107+
yieldmodels.Book(
108+
id=row[0],
109+
title=row[1],
110+
status=row[2],
111+
)
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= $1LIMIT1;
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+
DELETEFROM books
18+
WHERE id= $1;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATETYPEbook_statusAS ENUM ('available','checked_out','overdue');
2+
3+
4+
CREATETABLEbooks (
5+
idBIGSERIALPRIMARY KEY,
6+
titletextNOT NULL,
7+
status book_status DEFAULT'available'
8+
);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version:"2"
2+
plugins:
3+
-name:py
4+
wasm:
5+
url:file://../../../../bin/sqlc-gen-python.wasm
6+
sha256:"d6846ffad948181e611e883cedd2d2be66e091edc1273a0abc6c9da18399e0ca"
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_async_querier:true
18+
emit_str_enum:true
19+

‎internal/endtoend/testdata/exec_result/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:"a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
6+
sha256:"d6846ffad948181e611e883cedd2d2be66e091edc1273a0abc6c9da18399e0ca"
77
sql:
88
-schema:schema.sql
99
queries:query.sql

‎internal/endtoend/testdata/exec_rows/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:"a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e"
6+
sha256:"d6846ffad948181e611e883cedd2d2be66e091edc1273a0abc6c9da18399e0ca"
77
sql:
88
-schema:schema.sql
99
queries:query.sql

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp