获取数据库会话的上下文管理器 使用示例: with get_db() as db: # 使用 db 进行数据库操作 pass
(self)
| 118 | self.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=self.engine) |
| 119 | |
| 120 | def get_db(self) -> Generator[Session, None, None]: |
| 121 | """ |
| 122 | 获取数据库会话的上下文管理器 |
| 123 | 使用示例: |
| 124 | with get_db() as db: |
| 125 | # 使用 db 进行数据库操作 |
| 126 | pass |
| 127 | """ |
| 128 | db = self.SessionLocal() |
| 129 | try: |
| 130 | yield db |
| 131 | finally: |
| 132 | db.close() |
| 133 | |
| 134 | @contextmanager |
| 135 | def session_scope(self) -> Generator[Session, None, None]: |
no test coverage detected