|
| 1 | +# Code generated by sqlc. DO NOT EDIT. |
| 2 | +# versions: |
| 3 | +# sqlc v1.27.0 |
| 4 | +# source: query.sql |
| 5 | +from typing import AsyncIterator, Iterator, Optional |
| 6 | + |
| 7 | +import sqlalchemy |
| 8 | +import sqlalchemy.ext.asyncio |
| 9 | + |
| 10 | +from db import models |
| 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 | +class Querier: |
| 41 | + def __init__(self, conn: sqlalchemy.engine.Connection): |
| 42 | + self._conn = conn |
| 43 | + |
| 44 | + def create_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 | + if row is None: |
| 47 | + return None |
| 48 | + return models.Book( |
| 49 | + id=row[0], |
| 50 | + title=row[1], |
| 51 | + status=row[2], |
| 52 | + ) |
| 53 | + |
| 54 | + def delete_book(self, *, id: int) -> None: |
| 55 | + self._conn.execute(sqlalchemy.text(DELETE_BOOK), {"p1": id}) |
| 56 | + |
| 57 | + def get_book(self, *, id: int) -> Optional[models.Book]: |
| 58 | + row = self._conn.execute(sqlalchemy.text(GET_BOOK), {"p1": id}).first() |
| 59 | + if row is None: |
| 60 | + return None |
| 61 | + return models.Book( |
| 62 | + id=row[0], |
| 63 | + title=row[1], |
| 64 | + status=row[2], |
| 65 | + ) |
| 66 | + |
| 67 | + def list_books(self) -> Iterator[models.Book]: |
| 68 | + result = self._conn.execute(sqlalchemy.text(LIST_BOOKS)) |
| 69 | + for row in result: |
| 70 | + yield models.Book( |
| 71 | + id=row[0], |
| 72 | + title=row[1], |
| 73 | + status=row[2], |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +class AsyncQuerier: |
| 78 | + def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection): |
| 79 | + self._conn = conn |
| 80 | + |
| 81 | + async def create_book(self, *, title: str, status: Optional[models.BookStatus]) -> Optional[models.Book]: |
| 82 | + row = (await self._conn.execute(sqlalchemy.text(CREATE_BOOK), {"p1": title, "p2": status})).first() |
| 83 | + if row is None: |
| 84 | + return None |
| 85 | + return models.Book( |
| 86 | + id=row[0], |
| 87 | + title=row[1], |
| 88 | + status=row[2], |
| 89 | + ) |
| 90 | + |
| 91 | + async def delete_book(self, *, id: int) -> None: |
| 92 | + await self._conn.execute(sqlalchemy.text(DELETE_BOOK), {"p1": id}) |
| 93 | + |
| 94 | + async def get_book(self, *, id: int) -> Optional[models.Book]: |
| 95 | + row = (await self._conn.execute(sqlalchemy.text(GET_BOOK), {"p1": id})).first() |
| 96 | + if row is None: |
| 97 | + return None |
| 98 | + return models.Book( |
| 99 | + id=row[0], |
| 100 | + title=row[1], |
| 101 | + status=row[2], |
| 102 | + ) |
| 103 | + |
| 104 | + async def list_books(self) -> AsyncIterator[models.Book]: |
| 105 | + result = await self._conn.stream(sqlalchemy.text(LIST_BOOKS)) |
| 106 | + async for row in result: |
| 107 | + yield models.Book( |
| 108 | + id=row[0], |
| 109 | + title=row[1], |
| 110 | + status=row[2], |
| 111 | + ) |
0 commit comments