forked from anxolerd/dvpwa
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
patched.codes[bot]
committed
May 2, 2024
1 parent
9d258fd
commit 432a269
Showing
1 changed file
with
1 addition
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,4 @@ | ||
from typing import Optional, NamedTuple | ||
|
||
from aiopg.connection import Connection | ||
|
||
|
||
class Student(NamedTuple): | ||
id: int | ||
name: str | ||
|
||
@classmethod | ||
def from_raw(cls, raw: tuple): | ||
return cls(*raw) if raw else None | ||
|
||
@staticmethod | ||
async def get(conn: Connection, id_: int): | ||
async with conn.cursor() as cur: | ||
await cur.execute( | ||
'SELECT id, name FROM students WHERE id = %s', | ||
(id_,), | ||
) | ||
r = await cur.fetchone() | ||
return Student.from_raw(r) | ||
|
||
@staticmethod | ||
async def get_many(conn: Connection, limit: Optional[int] = None, | ||
offset: Optional[int] = None): | ||
q = 'SELECT id, name FROM students' | ||
params = {} | ||
if limit is not None: | ||
q += ' LIMIT + %(limit)s ' | ||
params['limit'] = limit | ||
if offset is not None: | ||
q += ' OFFSET + %(offset)s ' | ||
params['offset'] = offset | ||
async with conn.cursor() as cur: | ||
await cur.execute(q, params) | ||
results = await cur.fetchall() | ||
return [Student.from_raw(r) for r in results] | ||
|
||
@staticmethod | ||
async def create(conn: Connection, name: str): | ||
q = ("INSERT INTO students (name) " | ||
"VALUES ('%(name)s')" % {'name': name}) | ||
async with conn.cursor() as cur: | ||
await cur.execute(q) | ||
|
||
|
||
await cur.execute("INSERT INTO students (name) VALUES (%(name)s)", {"name": name}) |