|
1 | 1 | # Code generated by sqlc. DO NOT EDIT.
|
2 |
| -from typing import AsyncIterator, Awaitable, Iterator, Optional, overload |
| 2 | +from typing import AsyncIterator, Iterator, Optional |
3 | 3 |
|
4 |
| -import sqlc_runtime as sqlc |
| 4 | +import sqlalchemy |
| 5 | +import sqlalchemy.ext.asyncio |
5 | 6 |
|
6 | 7 | from authors import models
|
7 | 8 |
|
8 | 9 |
|
9 |
| -CREATE_AUTHOR = """-- name: create_author :one |
| 10 | +CREATE_AUTHOR = """-- name: create_author \\:one |
10 | 11 | INSERT INTO authors (
|
11 | 12 | name, bio
|
12 | 13 | ) VALUES (
|
13 |
| - $1, $2 |
| 14 | + :p1, :p2 |
14 | 15 | )
|
15 | 16 | RETURNING id, name, bio
|
16 | 17 | """
|
17 | 18 |
|
18 | 19 |
|
19 |
| -DELETE_AUTHOR = """-- name: delete_author :exec |
| 20 | +DELETE_AUTHOR = """-- name: delete_author \\:exec |
20 | 21 | DELETE FROM authors
|
21 |
| -WHERE id = $1 |
| 22 | +WHERE id = :p1 |
22 | 23 | """
|
23 | 24 |
|
24 | 25 |
|
25 |
| -GET_AUTHOR = """-- name: get_author :one |
| 26 | +GET_AUTHOR = """-- name: get_author \\:one |
26 | 27 | SELECT id, name, bio FROM authors
|
27 |
| -WHERE id = $1 LIMIT 1 |
| 28 | +WHERE id = :p1 LIMIT 1 |
28 | 29 | """
|
29 | 30 |
|
30 | 31 |
|
31 |
| -LIST_AUTHORS = """-- name: list_authors :many |
| 32 | +LIST_AUTHORS = """-- name: list_authors \\:many |
32 | 33 | SELECT id, name, bio FROM authors
|
33 | 34 | ORDER BY name
|
34 | 35 | """
|
35 | 36 |
|
36 | 37 |
|
37 |
| -@overload |
38 |
| -def create_author(conn: sqlc.Connection, name: str, bio: Optional[str]) -> Optional[models.Author]: |
39 |
| - pass |
| 38 | +class Query: |
| 39 | + def __init__(self, conn: sqlalchemy.engine.Connection): |
| 40 | + self._conn = conn |
40 | 41 |
|
| 42 | + def create_author(self, name: str, bio: Optional[str]) -> Optional[models.Author]: |
| 43 | + result = self._conn.execute(sqlalchemy.text(CREATE_AUTHOR), {"p1": name, "p2": bio}) |
| 44 | + return models.Author(**dict(result.first())) |
41 | 45 |
|
42 |
| -@overload |
43 |
| -def create_author(conn: sqlc.AsyncConnection, name: str, bio: Optional[str]) -> Awaitable[Optional[models.Author]]: |
44 |
| - pass |
| 46 | + def delete_author(self, id: int) -> None: |
| 47 | + self._conn.execute(sqlalchemy.text(DELETE_AUTHOR), {"p1": id}) |
45 | 48 |
|
| 49 | + def get_author(self, id: int) -> Optional[models.Author]: |
| 50 | + result = self._conn.execute(sqlalchemy.text(GET_AUTHOR), {"p1": id}) |
| 51 | + return models.Author(**dict(result.first())) |
46 | 52 |
|
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) |
| 53 | + def list_authors(self) -> Iterator[models.Author]: |
| 54 | + result = self._conn.execute(sqlalchemy.text(LIST_AUTHORS)) |
| 55 | + for row in result: |
| 56 | + yield models.Author(**dict(row)) |
49 | 57 |
|
50 | 58 |
|
51 |
| -@overload |
52 |
| -def delete_author(conn: sqlc.Connection, id: int) -> None: |
53 |
| - pass |
| 59 | +class AsyncQuery: |
| 60 | + def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection): |
| 61 | + self._conn = conn |
54 | 62 |
|
| 63 | + async def create_author(self, name: str, bio: Optional[str]) -> Optional[models.Author]: |
| 64 | + result = await self._conn.execute(sqlalchemy.text(CREATE_AUTHOR), {"p1": name, "p2": bio}) |
| 65 | + return models.Author(**dict(result.first())) |
55 | 66 |
|
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) |
| 67 | + async def delete_author(self, id: int) -> None: |
| 68 | + await self._conn.execute(sqlalchemy.text(DELETE_AUTHOR), {"p1": id}) |
91 | 69 |
|
| 70 | + async def get_author(self, id: int) -> Optional[models.Author]: |
| 71 | + result = await self._conn.execute(sqlalchemy.text(GET_AUTHOR), {"p1": id}) |
| 72 | + return models.Author(**dict(result.first())) |
92 | 73 |
|
| 74 | + async def list_authors(self) -> AsyncIterator[models.Author]: |
| 75 | + result = await self._conn.stream(sqlalchemy.text(LIST_AUTHORS)) |
| 76 | + async for row in result: |
| 77 | + yield models.Author(**dict(row)) |
0 commit comments