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