89 lines
2.4 KiB
Python
89 lines
2.4 KiB
Python
"""images 表:仅保存文件相对路径等元数据。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from typing import Any, List, Optional, Tuple
|
|
|
|
|
|
def insert_row(
|
|
conn: sqlite3.Connection,
|
|
file_path: str,
|
|
title: Optional[str],
|
|
status: str,
|
|
source: str,
|
|
account_id: Optional[str],
|
|
error_msg: Optional[str],
|
|
extra_json: Optional[str],
|
|
created_at: int,
|
|
updated_at: int,
|
|
) -> int:
|
|
cur = conn.cursor()
|
|
cur.execute(
|
|
"""
|
|
INSERT INTO images (
|
|
file_path, title, status, source, account_id, error_msg, extra_json, created_at, updated_at
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
""",
|
|
(file_path, title, status, source, account_id, error_msg, extra_json, created_at, updated_at),
|
|
)
|
|
return int(cur.lastrowid)
|
|
|
|
|
|
def update_file_path(conn: sqlite3.Connection, image_id: int, file_path: str, updated_at: int) -> None:
|
|
cur = conn.cursor()
|
|
cur.execute(
|
|
"UPDATE images SET file_path = ?, updated_at = ? WHERE id = ?",
|
|
(file_path, updated_at, image_id),
|
|
)
|
|
|
|
|
|
def fetch_by_id(conn: sqlite3.Connection, image_id: int) -> Optional[Tuple[Any, ...]]:
|
|
cur = conn.cursor()
|
|
cur.execute(
|
|
"""
|
|
SELECT id, file_path, title, status, source, account_id, error_msg, extra_json, created_at, updated_at
|
|
FROM images WHERE id = ?
|
|
""",
|
|
(image_id,),
|
|
)
|
|
return cur.fetchone()
|
|
|
|
|
|
def list_recent(conn: sqlite3.Connection, limit: int) -> List[Tuple[Any, ...]]:
|
|
cur = conn.cursor()
|
|
cur.execute(
|
|
"""
|
|
SELECT id, file_path, title, status, source, account_id, error_msg, extra_json, created_at, updated_at
|
|
FROM images ORDER BY updated_at DESC, id DESC
|
|
LIMIT ?
|
|
""",
|
|
(int(limit),),
|
|
)
|
|
return list(cur.fetchall())
|
|
|
|
|
|
def delete_by_id(conn: sqlite3.Connection, image_id: int) -> int:
|
|
cur = conn.cursor()
|
|
cur.execute("DELETE FROM images WHERE id = ?", (image_id,))
|
|
return int(cur.rowcount)
|
|
|
|
|
|
def update_feedback(
|
|
conn: sqlite3.Connection,
|
|
image_id: int,
|
|
status: str,
|
|
account_id: Optional[str],
|
|
error_msg: Optional[str],
|
|
updated_at: int,
|
|
) -> None:
|
|
cur = conn.cursor()
|
|
cur.execute(
|
|
"""
|
|
UPDATE images
|
|
SET status = ?, account_id = ?, error_msg = ?, updated_at = ?
|
|
WHERE id = ?
|
|
""",
|
|
(status, account_id, error_msg, updated_at, image_id),
|
|
)
|