2026-03-05 05:38:26 +00:00
|
|
|
# database/db_manager.py - 数据库 CRUD 操作封装
|
2026-03-04 18:09:45 +00:00
|
|
|
import os
|
2026-03-05 09:24:49 +00:00
|
|
|
from typing import List, Optional
|
2026-03-04 18:09:45 +00:00
|
|
|
|
2026-03-05 05:38:26 +00:00
|
|
|
from sqlalchemy import create_engine
|
|
|
|
|
from sqlalchemy.orm import sessionmaker, Session
|
|
|
|
|
|
2026-03-06 14:25:18 +00:00
|
|
|
import config
|
2026-03-05 09:24:49 +00:00
|
|
|
from database.models import Base, Project, RawRequirement, FunctionalRequirement, CodeFile, ChangeHistory
|
2026-03-04 18:09:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class DBManager:
|
|
|
|
|
"""SQLite 数据库管理器,封装所有 CRUD 操作"""
|
|
|
|
|
|
2026-03-05 05:38:26 +00:00
|
|
|
def __init__(self, db_path: str = None):
|
|
|
|
|
db_path = db_path or config.DB_PATH
|
2026-03-04 18:09:45 +00:00
|
|
|
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
2026-03-05 09:24:49 +00:00
|
|
|
self.engine = create_engine(f"sqlite:///{db_path}", echo=False)
|
2026-03-05 05:38:26 +00:00
|
|
|
Base.metadata.create_all(self.engine)
|
|
|
|
|
self._Session = sessionmaker(bind=self.engine)
|
2026-03-04 18:09:45 +00:00
|
|
|
|
2026-03-05 05:38:26 +00:00
|
|
|
def _session(self) -> Session:
|
|
|
|
|
return self._Session()
|
2026-03-04 18:09:45 +00:00
|
|
|
|
|
|
|
|
# ══════════════════════════════════════════════════
|
2026-03-05 05:38:26 +00:00
|
|
|
# Project
|
2026-03-04 18:09:45 +00:00
|
|
|
# ══════════════════════════════════════════════════
|
|
|
|
|
|
|
|
|
|
def create_project(self, project: Project) -> int:
|
2026-03-05 05:38:26 +00:00
|
|
|
with self._session() as s:
|
|
|
|
|
s.add(project)
|
|
|
|
|
s.commit()
|
|
|
|
|
s.refresh(project)
|
|
|
|
|
return project.id
|
2026-03-04 18:09:45 +00:00
|
|
|
|
|
|
|
|
def get_project_by_name(self, name: str) -> Optional[Project]:
|
2026-03-05 05:38:26 +00:00
|
|
|
with self._session() as s:
|
|
|
|
|
return s.query(Project).filter_by(name=name).first()
|
2026-03-04 18:09:45 +00:00
|
|
|
|
2026-03-05 05:38:26 +00:00
|
|
|
def get_project_by_id(self, project_id: int) -> Optional[Project]:
|
|
|
|
|
with self._session() as s:
|
|
|
|
|
return s.get(Project, project_id)
|
2026-03-04 18:09:45 +00:00
|
|
|
|
|
|
|
|
def update_project(self, project: Project) -> None:
|
2026-03-05 05:38:26 +00:00
|
|
|
with self._session() as s:
|
|
|
|
|
s.merge(project)
|
|
|
|
|
s.commit()
|
2026-03-04 18:09:45 +00:00
|
|
|
|
2026-03-05 05:38:26 +00:00
|
|
|
def list_projects(self) -> List[Project]:
|
|
|
|
|
with self._session() as s:
|
|
|
|
|
return s.query(Project).order_by(Project.created_at.desc()).all()
|
2026-03-04 18:09:45 +00:00
|
|
|
|
2026-03-05 09:24:49 +00:00
|
|
|
def delete_project(self, project_id: int) -> None:
|
2026-03-05 06:01:40 +00:00
|
|
|
with self._session() as s:
|
|
|
|
|
project = s.get(Project, project_id)
|
2026-03-05 09:24:49 +00:00
|
|
|
if project:
|
|
|
|
|
s.delete(project)
|
|
|
|
|
s.commit()
|
2026-03-05 06:01:40 +00:00
|
|
|
|
2026-03-04 18:09:45 +00:00
|
|
|
# ══════════════════════════════════════════════════
|
2026-03-05 05:38:26 +00:00
|
|
|
# RawRequirement
|
2026-03-04 18:09:45 +00:00
|
|
|
# ══════════════════════════════════════════════════
|
|
|
|
|
|
2026-03-05 05:38:26 +00:00
|
|
|
def create_raw_requirement(self, raw_req: RawRequirement) -> int:
|
|
|
|
|
with self._session() as s:
|
|
|
|
|
s.add(raw_req)
|
|
|
|
|
s.commit()
|
|
|
|
|
s.refresh(raw_req)
|
|
|
|
|
return raw_req.id
|
2026-03-04 18:09:45 +00:00
|
|
|
|
2026-03-05 05:38:26 +00:00
|
|
|
def get_raw_requirement(self, raw_req_id: int) -> Optional[RawRequirement]:
|
|
|
|
|
with self._session() as s:
|
|
|
|
|
return s.get(RawRequirement, raw_req_id)
|
2026-03-04 18:09:45 +00:00
|
|
|
|
|
|
|
|
# ══════════════════════════════════════════════════
|
2026-03-05 05:38:26 +00:00
|
|
|
# FunctionalRequirement
|
2026-03-04 18:09:45 +00:00
|
|
|
# ══════════════════════════════════════════════════
|
|
|
|
|
|
|
|
|
|
def create_functional_requirement(self, req: FunctionalRequirement) -> int:
|
2026-03-05 05:38:26 +00:00
|
|
|
with self._session() as s:
|
|
|
|
|
s.add(req)
|
|
|
|
|
s.commit()
|
|
|
|
|
s.refresh(req)
|
|
|
|
|
return req.id
|
2026-03-04 18:09:45 +00:00
|
|
|
|
|
|
|
|
def get_functional_requirement(self, req_id: int) -> Optional[FunctionalRequirement]:
|
2026-03-05 05:38:26 +00:00
|
|
|
with self._session() as s:
|
|
|
|
|
return s.get(FunctionalRequirement, req_id)
|
2026-03-04 18:09:45 +00:00
|
|
|
|
|
|
|
|
def list_functional_requirements(self, project_id: int) -> List[FunctionalRequirement]:
|
2026-03-05 05:38:26 +00:00
|
|
|
with self._session() as s:
|
|
|
|
|
return (
|
|
|
|
|
s.query(FunctionalRequirement)
|
|
|
|
|
.filter_by(project_id=project_id)
|
|
|
|
|
.order_by(FunctionalRequirement.index_no)
|
|
|
|
|
.all()
|
|
|
|
|
)
|
2026-03-04 18:09:45 +00:00
|
|
|
|
|
|
|
|
def update_functional_requirement(self, req: FunctionalRequirement) -> None:
|
2026-03-05 05:38:26 +00:00
|
|
|
with self._session() as s:
|
|
|
|
|
s.merge(req)
|
|
|
|
|
s.commit()
|
2026-03-04 18:09:45 +00:00
|
|
|
|
|
|
|
|
def delete_functional_requirement(self, req_id: int) -> None:
|
2026-03-05 05:38:26 +00:00
|
|
|
with self._session() as s:
|
|
|
|
|
obj = s.get(FunctionalRequirement, req_id)
|
|
|
|
|
if obj:
|
|
|
|
|
s.delete(obj)
|
|
|
|
|
s.commit()
|
2026-03-04 18:09:45 +00:00
|
|
|
|
|
|
|
|
# ══════════════════════════════════════════════════
|
2026-03-05 05:38:26 +00:00
|
|
|
# CodeFile
|
2026-03-04 18:09:45 +00:00
|
|
|
# ══════════════════════════════════════════════════
|
|
|
|
|
|
|
|
|
|
def upsert_code_file(self, code_file: CodeFile) -> int:
|
2026-03-05 05:38:26 +00:00
|
|
|
with self._session() as s:
|
|
|
|
|
existing = (
|
|
|
|
|
s.query(CodeFile)
|
|
|
|
|
.filter_by(func_req_id=code_file.func_req_id)
|
|
|
|
|
.first()
|
|
|
|
|
)
|
|
|
|
|
if existing:
|
2026-03-05 09:24:49 +00:00
|
|
|
existing.file_name = code_file.file_name
|
|
|
|
|
existing.file_path = code_file.file_path
|
|
|
|
|
existing.module = code_file.module
|
|
|
|
|
existing.language = code_file.language
|
|
|
|
|
existing.content = code_file.content
|
2026-03-05 05:38:26 +00:00
|
|
|
s.commit()
|
|
|
|
|
return existing.id
|
|
|
|
|
else:
|
|
|
|
|
s.add(code_file)
|
|
|
|
|
s.commit()
|
|
|
|
|
s.refresh(code_file)
|
|
|
|
|
return code_file.id
|
|
|
|
|
|
|
|
|
|
def list_code_files(self, project_id: int) -> List[CodeFile]:
|
|
|
|
|
with self._session() as s:
|
|
|
|
|
return (
|
|
|
|
|
s.query(CodeFile)
|
|
|
|
|
.join(FunctionalRequirement)
|
|
|
|
|
.filter(FunctionalRequirement.project_id == project_id)
|
|
|
|
|
.all()
|
2026-03-05 09:24:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# ══════════════════════════════════════════════════
|
|
|
|
|
# ChangeHistory
|
|
|
|
|
# ══════════════════════════════════════════════════
|
|
|
|
|
|
|
|
|
|
def create_change_history(self, change: ChangeHistory) -> int:
|
|
|
|
|
with self._session() as s:
|
|
|
|
|
s.add(change)
|
|
|
|
|
s.commit()
|
|
|
|
|
s.refresh(change)
|
|
|
|
|
return change.id
|
|
|
|
|
|
|
|
|
|
def list_change_history(self, project_id: int) -> List[ChangeHistory]:
|
|
|
|
|
with self._session() as s:
|
|
|
|
|
return s.query(ChangeHistory).filter_by(project_id=project_id).order_by(ChangeHistory.change_time.desc()).all()
|