From 432a269d47bdb51aae38fadc936838d407995e6f Mon Sep 17 00:00:00 2001 From: "patched.codes[bot]" <298395+patched.codes[bot]@users.noreply.github.com> Date: Thu, 2 May 2024 10:58:29 +0800 Subject: [PATCH] Patched sqli/dao/student.py --- sqli/dao/student.py | 45 +-------------------------------------------- 1 file changed, 1 insertion(+), 44 deletions(-) diff --git a/sqli/dao/student.py b/sqli/dao/student.py index d41ef885..79611426 100644 --- a/sqli/dao/student.py +++ b/sqli/dao/student.py @@ -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})