|
7 | 7 | __all__: typing.Sequence[str] = ( |
8 | 8 | "GetStudentAndScoreRow", |
9 | 9 | "GetStudentAndScoresRow", |
10 | | - "delete_author", |
11 | | - "get_student_and_score", |
12 | | - "get_student_and_scores", |
13 | | - "list_authors", |
14 | | - "list_authors2", |
15 | | - "update_author", |
| 10 | + "Queries", |
16 | 11 | ) |
17 | 12 |
|
18 | 13 | import msgspec |
@@ -74,37 +69,41 @@ class GetStudentAndScoresRow(msgspec.Struct): |
74 | 69 | """ |
75 | 70 |
|
76 | 71 |
|
77 | | -async def delete_author(conn: asyncpg.Connection, *, id: int) -> None: |
78 | | - await conn.execute(DELETE_AUTHOR, id) |
79 | | - |
80 | | - |
81 | | -async def get_student_and_score(conn: asyncpg.Connection, *, id: int) -> typing.Optional[GetStudentAndScoreRow]: |
82 | | - row = await conn.fetchrow(GET_STUDENT_AND_SCORE, id) |
83 | | - if row is None: |
84 | | - return None |
85 | | - return GetStudentAndScoreRow(student=models.Student(id=row[0], name=row[1], age=row[2]), test_score=models.TestScore(student_id=row[3], score=row[4], grade=row[5])) |
86 | | - |
87 | | - |
88 | | -async def get_student_and_scores(conn: asyncpg.Connection) -> typing.Sequence[GetStudentAndScoresRow]: |
89 | | - rows = await conn.fetch(GET_STUDENT_AND_SCORES) |
90 | | - return_rows: typing.List[GetStudentAndScoresRow] = [] |
91 | | - for row in rows: |
92 | | - return_rows.append(GetStudentAndScoresRow(student=models.Student(id=row[0], name=row[1], age=row[2]), test_score=models.TestScore(student_id=row[3], score=row[4], grade=row[5]))) |
93 | | - return return_rows |
94 | | - |
95 | | -async def list_authors(conn: asyncpg.Connection, *, ids: typing.Sequence[int]) -> typing.Sequence[int]: |
96 | | - rows = await conn.fetch(LIST_AUTHORS, ids) |
97 | | - return_rows: typing.List[int] = [] |
98 | | - for row in rows: |
99 | | - return_rows.append(int(row[0])) |
100 | | - return return_rows |
101 | | - |
102 | | -async def list_authors2(conn: asyncpg.Connection, *, id: int) -> typing.Sequence[int]: |
103 | | - rows = await conn.fetch(LIST_AUTHORS2, id) |
104 | | - return_rows: typing.List[int] = [] |
105 | | - for row in rows: |
106 | | - return_rows.append(int(row[0])) |
107 | | - return return_rows |
108 | | - |
109 | | -async def update_author(conn: asyncpg.Connection, *, name: str, bio: str, id: int) -> None: |
110 | | - await conn.execute(UPDATE_AUTHOR, name, bio, id) |
| 72 | +class Queries: |
| 73 | + __slots__ = ("_conn",) |
| 74 | + |
| 75 | + def __init__(self, conn: asyncpg.Connection): |
| 76 | + self._conn = conn |
| 77 | + |
| 78 | + async def delete_author(self, *, id: int) -> None: |
| 79 | + await self._conn.execute(DELETE_AUTHOR, id) |
| 80 | + |
| 81 | + async def get_student_and_score(self, *, id: int) -> typing.Optional[GetStudentAndScoreRow]: |
| 82 | + row = await self._conn.fetchrow(GET_STUDENT_AND_SCORE, id) |
| 83 | + if row is None: |
| 84 | + return None |
| 85 | + return GetStudentAndScoreRow(student=models.Student(id=row[0], name=row[1], age=row[2]), test_score=models.TestScore(student_id=row[3], score=row[4], grade=row[5])) |
| 86 | + |
| 87 | + async def get_student_and_scores(self) -> typing.Sequence[GetStudentAndScoresRow]: |
| 88 | + rows = await self._conn.fetch(GET_STUDENT_AND_SCORES) |
| 89 | + return_rows: typing.List[GetStudentAndScoresRow] = [] |
| 90 | + for row in rows: |
| 91 | + return_rows.append(GetStudentAndScoresRow(student=models.Student(id=row[0], name=row[1], age=row[2]), test_score=models.TestScore(student_id=row[3], score=row[4], grade=row[5]))) |
| 92 | + return return_rows |
| 93 | + |
| 94 | + async def list_authors(self, *, ids: typing.Sequence[int]) -> typing.Sequence[int]: |
| 95 | + rows = await self._conn.fetch(LIST_AUTHORS, ids) |
| 96 | + return_rows: typing.List[int] = [] |
| 97 | + for row in rows: |
| 98 | + return_rows.append(int(row[0])) |
| 99 | + return return_rows |
| 100 | + |
| 101 | + async def list_authors2(self, *, id: int) -> typing.Sequence[int]: |
| 102 | + rows = await self._conn.fetch(LIST_AUTHORS2, id) |
| 103 | + return_rows: typing.List[int] = [] |
| 104 | + for row in rows: |
| 105 | + return_rows.append(int(row[0])) |
| 106 | + return return_rows |
| 107 | + |
| 108 | + async def update_author(self, *, name: str, bio: str, id: int) -> None: |
| 109 | + await self._conn.execute(UPDATE_AUTHOR, name, bio, id) |
0 commit comments