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