|
| 1 | +# Code generated by sqlc. DO NOT EDIT. |
| 2 | +# versions: |
| 3 | +# sqlc v1.20.0 |
| 4 | +# source: query.sql |
| 5 | +from typing import AsyncIterator, Iterator, Optional |
| 6 | + |
| 7 | +import sqlalchemy |
| 8 | +import sqlalchemy.ext.asyncio |
| 9 | + |
| 10 | +from . import models |
| 11 | + |
| 12 | + |
| 13 | +CREATE_AUTHOR = """-- name: create_author \\:one |
| 14 | +insert into authors ( |
| 15 | + name, bio, age |
| 16 | +) values ( |
| 17 | + :p1, :p2, :p3 |
| 18 | +) |
| 19 | +returning id, name, age, bio, is_active |
| 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, age, bio, is_active from authors |
| 31 | +where id = :p1 LIMIT 1 |
| 32 | +""" |
| 33 | + |
| 34 | + |
| 35 | +LIST_AUTHORS = """-- name: list_authors \\:many |
| 36 | +select id, name, age, bio, is_active from authors |
| 37 | +order by name |
| 38 | +""" |
| 39 | + |
| 40 | + |
| 41 | +LOCK_AUTHOR = """-- name: lock_author \\:one |
| 42 | +select id, name, age, bio, is_active from authors |
| 43 | +where id = :p1 LIMIT 1 FOR UPDATE NOWAIT |
| 44 | +""" |
| 45 | + |
| 46 | + |
| 47 | +UPDATE_AUTHOR = """-- name: update_author \\:one |
| 48 | +update authors |
| 49 | + set name = :p2, |
| 50 | + bio = :p3, |
| 51 | + age = :p4 |
| 52 | +where id = :p1 |
| 53 | +returning id, name, age, bio, is_active |
| 54 | +""" |
| 55 | + |
| 56 | + |
| 57 | +class Querier: |
| 58 | + def __init__(self, conn: sqlalchemy.engine.Connection): |
| 59 | + self._conn = conn |
| 60 | + |
| 61 | + def create_author(self, *, name: str, bio: Optional[str], age: int) -> Optional[models.Author]: |
| 62 | + row = self._conn.execute(sqlalchemy.text(CREATE_AUTHOR), {"p1": name, "p2": bio, "p3": age}).first() |
| 63 | + if row is None: |
| 64 | + return None |
| 65 | + return models.Author( |
| 66 | + id=row[0], |
| 67 | + name=row[1], |
| 68 | + age=row[2], |
| 69 | + bio=row[3], |
| 70 | + is_active=row[4], |
| 71 | + ) |
| 72 | + |
| 73 | + def delete_author(self, *, id: int) -> None: |
| 74 | + self._conn.execute(sqlalchemy.text(DELETE_AUTHOR), {"p1": id}) |
| 75 | + |
| 76 | + def get_author(self, *, id: int) -> Optional[models.Author]: |
| 77 | + row = self._conn.execute(sqlalchemy.text(GET_AUTHOR), {"p1": id}).first() |
| 78 | + if row is None: |
| 79 | + return None |
| 80 | + return models.Author( |
| 81 | + id=row[0], |
| 82 | + name=row[1], |
| 83 | + age=row[2], |
| 84 | + bio=row[3], |
| 85 | + is_active=row[4], |
| 86 | + ) |
| 87 | + |
| 88 | + def list_authors(self) -> Iterator[models.Author]: |
| 89 | + result = self._conn.execute(sqlalchemy.text(LIST_AUTHORS)) |
| 90 | + for row in result: |
| 91 | + yield models.Author( |
| 92 | + id=row[0], |
| 93 | + name=row[1], |
| 94 | + age=row[2], |
| 95 | + bio=row[3], |
| 96 | + is_active=row[4], |
| 97 | + ) |
| 98 | + |
| 99 | + def lock_author(self, *, id: int) -> Optional[models.Author]: |
| 100 | + row = self._conn.execute(sqlalchemy.text(LOCK_AUTHOR), {"p1": id}).first() |
| 101 | + if row is None: |
| 102 | + return None |
| 103 | + return models.Author( |
| 104 | + id=row[0], |
| 105 | + name=row[1], |
| 106 | + age=row[2], |
| 107 | + bio=row[3], |
| 108 | + is_active=row[4], |
| 109 | + ) |
| 110 | + |
| 111 | + def update_author(self, *, id: int, name: str, bio: Optional[str], age: int) -> Optional[models.Author]: |
| 112 | + row = self._conn.execute(sqlalchemy.text(UPDATE_AUTHOR), { |
| 113 | + "p1": id, |
| 114 | + "p2": name, |
| 115 | + "p3": bio, |
| 116 | + "p4": age, |
| 117 | + }).first() |
| 118 | + if row is None: |
| 119 | + return None |
| 120 | + return models.Author( |
| 121 | + id=row[0], |
| 122 | + name=row[1], |
| 123 | + age=row[2], |
| 124 | + bio=row[3], |
| 125 | + is_active=row[4], |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +class AsyncQuerier: |
| 130 | + def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection): |
| 131 | + self._conn = conn |
| 132 | + |
| 133 | + async def create_author(self, *, name: str, bio: Optional[str], age: int) -> Optional[models.Author]: |
| 134 | + row = (await self._conn.execute(sqlalchemy.text(CREATE_AUTHOR), {"p1": name, "p2": bio, "p3": age})).first() |
| 135 | + if row is None: |
| 136 | + return None |
| 137 | + return models.Author( |
| 138 | + id=row[0], |
| 139 | + name=row[1], |
| 140 | + age=row[2], |
| 141 | + bio=row[3], |
| 142 | + is_active=row[4], |
| 143 | + ) |
| 144 | + |
| 145 | + async def delete_author(self, *, id: int) -> None: |
| 146 | + await self._conn.execute(sqlalchemy.text(DELETE_AUTHOR), {"p1": id}) |
| 147 | + |
| 148 | + async def get_author(self, *, id: int) -> Optional[models.Author]: |
| 149 | + row = (await self._conn.execute(sqlalchemy.text(GET_AUTHOR), {"p1": id})).first() |
| 150 | + if row is None: |
| 151 | + return None |
| 152 | + return models.Author( |
| 153 | + id=row[0], |
| 154 | + name=row[1], |
| 155 | + age=row[2], |
| 156 | + bio=row[3], |
| 157 | + is_active=row[4], |
| 158 | + ) |
| 159 | + |
| 160 | + async def list_authors(self) -> AsyncIterator[models.Author]: |
| 161 | + result = await self._conn.stream(sqlalchemy.text(LIST_AUTHORS)) |
| 162 | + async for row in result: |
| 163 | + yield models.Author( |
| 164 | + id=row[0], |
| 165 | + name=row[1], |
| 166 | + age=row[2], |
| 167 | + bio=row[3], |
| 168 | + is_active=row[4], |
| 169 | + ) |
| 170 | + |
| 171 | + async def lock_author(self, *, id: int) -> Optional[models.Author]: |
| 172 | + row = (await self._conn.execute(sqlalchemy.text(LOCK_AUTHOR), {"p1": id})).first() |
| 173 | + if row is None: |
| 174 | + return None |
| 175 | + return models.Author( |
| 176 | + id=row[0], |
| 177 | + name=row[1], |
| 178 | + age=row[2], |
| 179 | + bio=row[3], |
| 180 | + is_active=row[4], |
| 181 | + ) |
| 182 | + |
| 183 | + async def update_author(self, *, id: int, name: str, bio: Optional[str], age: int) -> Optional[models.Author]: |
| 184 | + row = (await self._conn.execute(sqlalchemy.text(UPDATE_AUTHOR), { |
| 185 | + "p1": id, |
| 186 | + "p2": name, |
| 187 | + "p3": bio, |
| 188 | + "p4": age, |
| 189 | + })).first() |
| 190 | + if row is None: |
| 191 | + return None |
| 192 | + return models.Author( |
| 193 | + id=row[0], |
| 194 | + name=row[1], |
| 195 | + age=row[2], |
| 196 | + bio=row[3], |
| 197 | + is_active=row[4], |
| 198 | + ) |
0 commit comments