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