fix: database.py 懒初始化引擎 + list_files 返回真实 total
- database.py: engine/SessionLocal 改为延迟创建,避免 import 时副作用, 并提供 dispose_engine() 支持配置热重载 - dao/uploaded_file_dao.py: 新增 count() 方法 - upload_service.py: list_files() 改用 dao.count() 返回数据库总条数, 修复之前返回当前页条目数导致分页 total 语义错误的问题
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models.uploaded_file import UploadedFile
|
||||
@@ -25,6 +25,10 @@ class UploadedFileDAO:
|
||||
stmt = select(UploadedFile).where(UploadedFile.sha256 == sha256).limit(1)
|
||||
return self.db.scalars(stmt).first()
|
||||
|
||||
def count(self) -> int:
|
||||
"""返回数据库中文件总条数。"""
|
||||
return self.db.scalar(select(func.count()).select_from(UploadedFile)) or 0
|
||||
|
||||
def list(self, limit: int = 100, offset: int = 0) -> list[UploadedFile]:
|
||||
stmt = (
|
||||
select(UploadedFile)
|
||||
|
||||
Reference in New Issue
Block a user