写入单个事件。 失败时记录日志并返回 False,不抛异常。 Args: event: 要写入的事件对象。 Returns: 写入是否成功。
(self, event: "Event")
| 156 | raise |
| 157 | |
| 158 | def add_event(self, event: "Event") -> bool: |
| 159 | """ |
| 160 | 写入单个事件。 |
| 161 | |
| 162 | 失败时记录日志并返回 False,不抛异常。 |
| 163 | |
| 164 | Args: |
| 165 | event: 要写入的事件对象。 |
| 166 | |
| 167 | Returns: |
| 168 | 写入是否成功。 |
| 169 | """ |
| 170 | if self._conn is None: |
| 171 | self._logger.error("EventStorage not initialized") |
| 172 | return False |
| 173 | |
| 174 | try: |
| 175 | with self._transaction(): |
| 176 | # 插入事件主表。 |
| 177 | self._conn.execute( |
| 178 | """ |
| 179 | INSERT OR IGNORE INTO events ( |
| 180 | id, month_stamp, content, is_major, is_story, event_type, render_key, render_params, created_at |
| 181 | ) |
| 182 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 183 | """, |
| 184 | ( |
| 185 | event.id, |
| 186 | int(event.month_stamp), |
| 187 | event.content, |
| 188 | event.is_major, |
| 189 | event.is_story, |
| 190 | event.event_type, |
| 191 | event.render_key, |
| 192 | json.dumps(event.render_params, ensure_ascii=False) if event.render_params is not None else None, |
| 193 | _format_time(event.created_at), |
| 194 | ) |
| 195 | ) |
| 196 | |
| 197 | # 插入关联表。 |
| 198 | if event.related_avatars: |
| 199 | for avatar_id in event.related_avatars: |
| 200 | self._conn.execute( |
| 201 | """ |
| 202 | INSERT OR IGNORE INTO event_avatars (event_id, avatar_id) |
| 203 | VALUES (?, ?) |
| 204 | """, |
| 205 | (event.id, str(avatar_id)) |
| 206 | ) |
| 207 | |
| 208 | # 插入宗门关联表。 |
| 209 | if getattr(event, "related_sects", None): |
| 210 | for sect_id in event.related_sects: |
| 211 | self._conn.execute( |
| 212 | """ |
| 213 | INSERT OR IGNORE INTO event_sects (event_id, sect_id) |
| 214 | VALUES (?, ?) |
| 215 | """, |