File size: 1,630 Bytes
f7e32a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""SQLite database engine with WAL mode and async support."""

import aiosqlite
from pathlib import Path
from typing import Optional

from config import config


_DB: Optional[aiosqlite.Connection] = None


async def get_db() -> aiosqlite.Connection:
    global _DB
    if _DB is None:
        _DB = await aiosqlite.connect(config.database.path)
        _DB.row_factory = aiosqlite.Row
        if config.database.wal_mode:
            await _DB.execute("PRAGMA journal_mode=WAL")
        await _DB.execute(f"PRAGMA busy_timeout={config.database.busy_timeout}")
        await _DB.execute("PRAGMA foreign_keys=ON")
    return _DB


async def close_db():
    global _DB
    if _DB is not None:
        await _DB.close()
        _DB = None


async def init_db():
    from database.migrations import run_migrations
    db = await get_db()
    await run_migrations(db)


async def execute(query: str, params: tuple = ()) -> aiosqlite.Cursor:
    db = await get_db()
    cursor = await db.execute(query, params)
    await db.commit()
    return cursor


async def fetch_one(query: str, params: tuple = ()) -> Optional[dict]:
    db = await get_db()
    cursor = await db.execute(query, params)
    row = await cursor.fetchone()
    return dict(row) if row else None


async def fetch_all(query: str, params: tuple = ()) -> list[dict]:
    db = await get_db()
    cursor = await db.execute(query, params)
    rows = await cursor.fetchall()
    return [dict(r) for r in rows]


async def execute_many(query: str, params_list: list[tuple]) -> None:
    db = await get_db()
    await db.executemany(query, params_list)
    await db.commit()