refactor: 抽出 core/(clock store text numeric),小学版与初中版共用归一化与数值解析
This commit is contained in:
+6
-33
@@ -1,5 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""SQLite:建表、连接、时间与 ID。
|
||||
"""SQLite:建表、连接。时间与 ID 用 core.clock,连接与行转换用 core.store。
|
||||
|
||||
独立数据库(默认 data/junior.db),13 张表:
|
||||
students 学生(主账号)
|
||||
@@ -21,11 +21,11 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
import uuid
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
|
||||
TZ = timezone(timedelta(hours=8))
|
||||
from core.clock import add_days, new_id, now, today
|
||||
from core.store import open_db, row_to_dict, rows_to_list
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
DB_PATH = Path(os.environ.get("QINGLING_JUNIOR_DB", HERE.parent / "data" / "junior.db"))
|
||||
|
||||
@@ -183,30 +183,9 @@ CREATE INDEX IF NOT EXISTS idx_grants_student ON share_grants(student_id, guardi
|
||||
"""
|
||||
|
||||
|
||||
def now() -> str:
|
||||
return datetime.now(TZ).isoformat(timespec="seconds")
|
||||
|
||||
|
||||
def today() -> str:
|
||||
return datetime.now(TZ).date().isoformat()
|
||||
|
||||
|
||||
def add_days(days: int, base: str | None = None) -> str:
|
||||
d = date.fromisoformat(base) if base else date.fromisoformat(today())
|
||||
return (d + timedelta(days=days)).isoformat()
|
||||
|
||||
|
||||
def new_id(prefix: str) -> str:
|
||||
return f"{prefix}_{uuid.uuid4().hex[:12]}"
|
||||
|
||||
|
||||
def connect(path: str | Path | None = None) -> sqlite3.Connection:
|
||||
p = Path(path or DB_PATH)
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
conn = sqlite3.connect(str(p), timeout=15, check_same_thread=False, isolation_level=None)
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
return conn
|
||||
"""连接本产品的库(时间/ID 来自 core.clock,连接来自 core.store)。"""
|
||||
return open_db(path or DB_PATH)
|
||||
|
||||
|
||||
def init_db(conn: sqlite3.Connection) -> None:
|
||||
@@ -223,12 +202,6 @@ def reset_db(conn: sqlite3.Connection) -> None:
|
||||
conn.commit()
|
||||
|
||||
|
||||
def row_to_dict(row) -> dict | None:
|
||||
return dict(row) if row is not None else None
|
||||
|
||||
|
||||
def rows_to_list(rows) -> list[dict]:
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
|
||||
def log_event(conn, name: str, student_id: str | None = None, **payload) -> None:
|
||||
|
||||
Reference in New Issue
Block a user