-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
60 lines (50 loc) · 1.62 KB
/
Copy pathdatabase.py
File metadata and controls
60 lines (50 loc) · 1.62 KB
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
import asyncio
import aiosqlite
import os
from pathlib import Path
from typing import Optional
import loguru
# Configurable via env for deployments where src/ and data/ are separated (e.g. systemd)
DATA_DIR = os.environ.get("GOALKEEPR_DATA_DIR", "./data")
DB_PATH = str(Path(DATA_DIR) / "main.db")
_conn: Optional[aiosqlite.Connection] = None
_conn_use_lock = asyncio.Lock()
logger = loguru.logger
async def connection() -> aiosqlite.Connection:
"""
复用单个连接,避免频繁创建开销。
"""
global _conn
if _conn is None:
async with _conn_use_lock:
if _conn is None:
Path(DATA_DIR).mkdir(parents=True, exist_ok=True)
_conn = await aiosqlite.connect(DB_PATH, timeout=30.0)
return _conn
async def execute(query: str, *args, **kwargs):
"""
Execute a query and commit the changes.
Creates a new connection for each call to avoid thread reuse issues.
"""
conn = await connection()
async with _conn_use_lock:
await conn.execute(query, *args, **kwargs)
await conn.commit()
async def execute_fetch(query: str, *args, **kwargs):
"""
Execute a query and return the results.
"""
conn = await connection()
async with _conn_use_lock:
cursor = await conn.execute(query, *args, **kwargs)
rows = await cursor.fetchall()
await cursor.close()
return rows
async def close() -> None:
"""关闭数据库连接"""
global _conn
if _conn is not None:
async with _conn_use_lock:
if _conn is not None:
await _conn.close()
_conn = None