Add db.py
This commit is contained in:
parent
0c5ab590c4
commit
4b09cd91b5
|
@ -0,0 +1,28 @@
|
||||||
|
from u232ping.abc.db import DataBase
|
||||||
|
from u232ping.types import Group
|
||||||
|
from aiosqlite import connect as sq
|
||||||
|
from os import environ
|
||||||
|
|
||||||
|
class Sqlite(DataBase):
|
||||||
|
|
||||||
|
path: str
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.path = environ["DB_PATH"]
|
||||||
|
|
||||||
|
async def _get(self, group: int | None = None) -> Group:
|
||||||
|
req = "SELECT telegram FROM u232"
|
||||||
|
if group is not None:
|
||||||
|
where = f" WHERE group = {group}"
|
||||||
|
req += where
|
||||||
|
async with sq(self.path) as db:
|
||||||
|
cursor = await db.cursor()
|
||||||
|
await cursor.execute(req)
|
||||||
|
fetch = await cursor.fetchall()
|
||||||
|
return Group(fetch)
|
||||||
|
|
||||||
|
async def get_group(self, group_idx: int):
|
||||||
|
return await self._get(group_idx)
|
||||||
|
|
||||||
|
async def get_all(self):
|
||||||
|
return await self._get()
|
Loading…
Reference in New Issue