Skip to content

Commit 3f131b7

Browse files
authored
Python codegen support (#923)
* Initial python codegen support * Add python tests CI job
1 parent 184796e commit 3f131b7

33 files changed

+2276
-7
lines changed

.github/workflows/ci-python.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: python
2+
on: [push, pull_request]
3+
jobs:
4+
5+
build:
6+
name: test
7+
runs-on: ubuntu-latest
8+
9+
services:
10+
postgres:
11+
image: postgres:11
12+
env:
13+
POSTGRES_USER: postgres
14+
POSTGRES_PASSWORD: postgres
15+
POSTGRES_DB: postgres
16+
ports:
17+
- 5432:5432
18+
# needed because the postgres container does not provide a healthcheck
19+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
- uses: actions/setup-python@v2
24+
with:
25+
python-version: 3.9
26+
- name: Install python dependencies
27+
working-directory: ./examples/python
28+
run: |
29+
python -m pip install --upgrade pip
30+
python -m pip install -r requirements.txt
31+
- name: Test python code
32+
working-directory: ./examples/python
33+
env:
34+
PG_USER: postgres
35+
PG_HOST: localhost
36+
PG_DATABASE: postgres
37+
PG_PASSWORD: postgres
38+
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
39+
run: |
40+
pytest src/tests

examples/python/requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pytest~=6.2.2
2+
pytest-asyncio~=0.14.0
3+
psycopg2-binary~=2.8.6
4+
asyncpg~=0.21.0
5+
pydantic~=1.7.3
6+
sqlc-python-runtime~=1.0.0

examples/python/sqlc.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"version": "2",
3+
"sql": [
4+
{
5+
"schema": "../authors/postgresql/schema.sql",
6+
"queries": "../authors/postgresql/query.sql",
7+
"engine": "postgresql",
8+
"gen": {
9+
"python": {
10+
"out": "src/authors",
11+
"package": "authors"
12+
}
13+
}
14+
},
15+
{
16+
"schema": "../booktest/postgresql/schema.sql",
17+
"queries": "../booktest/postgresql/query.sql",
18+
"engine": "postgresql",
19+
"gen": {
20+
"python": {
21+
"out": "src/booktest",
22+
"package": "booktest"
23+
}
24+
}
25+
},
26+
{
27+
"schema": "../jets/schema.sql",
28+
"queries": "../jets/query-building.sql",
29+
"engine": "postgresql",
30+
"gen": {
31+
"python": {
32+
"out": "src/jets",
33+
"package": "jets"
34+
}
35+
}
36+
},
37+
{
38+
"schema": "../ondeck/postgresql/schema",
39+
"queries": "../ondeck/postgresql/query",
40+
"engine": "postgresql",
41+
"gen": {
42+
"python": {
43+
"out": "src/ondeck",
44+
"package": "ondeck"
45+
}
46+
}
47+
}
48+
]
49+
}

examples/python/src/authors/__init__.py

Whitespace-only changes.

examples/python/src/authors/models.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Code generated by sqlc. DO NOT EDIT.
2+
from typing import Optional
3+
4+
import pydantic
5+
6+
7+
# Enums
8+
9+
# Models
10+
class Author(pydantic.BaseModel):
11+
id: int
12+
name: str
13+
bio: Optional[str]
14+
15+

examples/python/src/authors/query.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Code generated by sqlc. DO NOT EDIT.
2+
from typing import AsyncIterator, Awaitable, Iterator, Optional, overload
3+
4+
import sqlc_runtime as sqlc
5+
6+
from authors import models
7+
8+
9+
CREATE_AUTHOR = """-- name: create_author :one
10+
INSERT INTO authors (
11+
name, bio
12+
) VALUES (
13+
$1, $2
14+
)
15+
RETURNING id, name, bio
16+
"""
17+
18+
19+
DELETE_AUTHOR = """-- name: delete_author :exec
20+
DELETE FROM authors
21+
WHERE id = $1
22+
"""
23+
24+
25+
GET_AUTHOR = """-- name: get_author :one
26+
SELECT id, name, bio FROM authors
27+
WHERE id = $1 LIMIT 1
28+
"""
29+
30+
31+
LIST_AUTHORS = """-- name: list_authors :many
32+
SELECT id, name, bio FROM authors
33+
ORDER BY name
34+
"""
35+
36+
37+
@overload
38+
def create_author(conn: sqlc.Connection, name: str, bio: Optional[str]) -> Optional[models.Author]:
39+
pass
40+
41+
42+
@overload
43+
def create_author(conn: sqlc.AsyncConnection, name: str, bio: Optional[str]) -> Awaitable[Optional[models.Author]]:
44+
pass
45+
46+
47+
def create_author(conn: sqlc.GenericConnection, name: str, bio: Optional[str]) -> sqlc.ReturnType[Optional[models.Author]]:
48+
return conn.execute_one_model(models.Author, CREATE_AUTHOR, name, bio)
49+
50+
51+
@overload
52+
def delete_author(conn: sqlc.Connection, id: int) -> None:
53+
pass
54+
55+
56+
@overload
57+
def delete_author(conn: sqlc.AsyncConnection, id: int) -> Awaitable[None]:
58+
pass
59+
60+
61+
def delete_author(conn: sqlc.GenericConnection, id: int) -> sqlc.ReturnType[None]:
62+
return conn.execute_none(DELETE_AUTHOR, id)
63+
64+
65+
@overload
66+
def get_author(conn: sqlc.Connection, id: int) -> Optional[models.Author]:
67+
pass
68+
69+
70+
@overload
71+
def get_author(conn: sqlc.AsyncConnection, id: int) -> Awaitable[Optional[models.Author]]:
72+
pass
73+
74+
75+
def get_author(conn: sqlc.GenericConnection, id: int) -> sqlc.ReturnType[Optional[models.Author]]:
76+
return conn.execute_one_model(models.Author, GET_AUTHOR, id)
77+
78+
79+
@overload
80+
def list_authors(conn: sqlc.Connection) -> Iterator[models.Author]:
81+
pass
82+
83+
84+
@overload
85+
def list_authors(conn: sqlc.AsyncConnection) -> AsyncIterator[models.Author]:
86+
pass
87+
88+
89+
def list_authors(conn: sqlc.GenericConnection) -> sqlc.IteratorReturn[models.Author]:
90+
return conn.execute_many_model(models.Author, LIST_AUTHORS)
91+
92+

examples/python/src/booktest/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Code generated by sqlc. DO NOT EDIT.
2+
from typing import List
3+
import datetime
4+
import enum
5+
6+
import pydantic
7+
8+
9+
# Enums
10+
class BookType(str, enum.Enum):
11+
FICTION = "FICTION"
12+
NONFICTION = "NONFICTION"
13+
14+
15+
# Models
16+
class Author(pydantic.BaseModel):
17+
author_id: int
18+
name: str
19+
20+
21+
class Book(pydantic.BaseModel):
22+
book_id: int
23+
author_id: int
24+
isbn: str
25+
book_type: BookType
26+
title: str
27+
year: int
28+
available: datetime.datetime
29+
tags: List[str]
30+
31+

0 commit comments

Comments
 (0)