content_db.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. """content_db.py — SQLite 内容数据库操作类"""
  2. import json
  3. import sqlite3
  4. from pathlib import Path
  5. from typing import Optional
  6. class ContentDB:
  7. """SQLite 内容数据库操作类
  8. 每个文档对应一个独立的 SQLite 数据库文件,存储文档的所有 Blocks。
  9. """
  10. def __init__(self, db_path: str):
  11. self.db_path = Path(db_path)
  12. self.conn: Optional[sqlite3.Connection] = None
  13. def connect(self):
  14. """连接数据库"""
  15. self.conn = sqlite3.connect(str(self.db_path))
  16. self.conn.row_factory = sqlite3.Row
  17. return self
  18. def close(self):
  19. """关闭连接"""
  20. if self.conn:
  21. self.conn.close()
  22. self.conn = None
  23. def __enter__(self):
  24. """上下文管理器入口"""
  25. return self.connect()
  26. def __exit__(self, exc_type, exc_val, exc_tb):
  27. """上下文管理器退出"""
  28. self.close()
  29. def create_tables(self):
  30. """创建 document_blocks 表"""
  31. self.conn.execute("""
  32. CREATE TABLE IF NOT EXISTS document_blocks (
  33. id TEXT PRIMARY KEY,
  34. block_order INTEGER NOT NULL,
  35. type TEXT NOT NULL,
  36. level INTEGER DEFAULT 0,
  37. "index" INTEGER DEFAULT 0,
  38. content TEXT NOT NULL,
  39. word_style TEXT DEFAULT '',
  40. style TEXT DEFAULT '{}',
  41. metadata TEXT DEFAULT '{}'
  42. )
  43. """)
  44. # 创建索引
  45. self.conn.execute(
  46. 'CREATE INDEX IF NOT EXISTS idx_block_order ON document_blocks(block_order)'
  47. )
  48. self.conn.execute(
  49. 'CREATE INDEX IF NOT EXISTS idx_type ON document_blocks(type)'
  50. )
  51. self.conn.execute(
  52. 'CREATE INDEX IF NOT EXISTS idx_level ON document_blocks(level)'
  53. )
  54. self.conn.commit()
  55. def insert_blocks(self, blocks: list[dict]):
  56. """批量插入 blocks"""
  57. for block in blocks:
  58. content = block['content']
  59. if isinstance(content, (dict, list)):
  60. content = json.dumps(content, ensure_ascii=False)
  61. self.conn.execute("""
  62. INSERT INTO document_blocks
  63. (id, block_order, type, level, "index", content, word_style, style, metadata)
  64. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
  65. """, (
  66. block['id'],
  67. block['block_order'],
  68. block['type'],
  69. block.get('level', 0),
  70. block.get('index', 0),
  71. content,
  72. block.get('word_style', ''),
  73. json.dumps(block.get('style', {}), ensure_ascii=False),
  74. json.dumps(block.get('metadata', {}), ensure_ascii=False)
  75. ))
  76. self.conn.commit()
  77. def get_blocks(self, order_by: str = 'block_order') -> list[dict]:
  78. """查询所有 blocks"""
  79. cursor = self.conn.execute(f"""
  80. SELECT * FROM document_blocks
  81. ORDER BY {order_by}
  82. """)
  83. rows = cursor.fetchall()
  84. return [self._row_to_dict(row) for row in rows]
  85. def get_block_by_id(self, block_id: str) -> Optional[dict]:
  86. """按 ID 查询单个 block"""
  87. cursor = self.conn.execute("""
  88. SELECT * FROM document_blocks WHERE id = ?
  89. """, (block_id,))
  90. row = cursor.fetchone()
  91. return self._row_to_dict(row) if row else None
  92. def update_block(self, block_id: str, updates: dict):
  93. """更新单个 block"""
  94. set_clauses = []
  95. params = []
  96. if 'content' in updates:
  97. content = updates['content']
  98. if isinstance(content, (dict, list)):
  99. content = json.dumps(content, ensure_ascii=False)
  100. set_clauses.append('content = ?')
  101. params.append(content)
  102. if 'style' in updates:
  103. set_clauses.append('style = ?')
  104. params.append(json.dumps(updates['style'], ensure_ascii=False))
  105. if 'word_style' in updates:
  106. set_clauses.append('word_style = ?')
  107. params.append(updates['word_style'])
  108. if 'metadata' in updates:
  109. set_clauses.append('metadata = ?')
  110. params.append(json.dumps(updates['metadata'], ensure_ascii=False))
  111. if not set_clauses:
  112. return
  113. params.append(block_id)
  114. sql = f"UPDATE document_blocks SET {', '.join(set_clauses)} WHERE id = ?"
  115. self.conn.execute(sql, params)
  116. self.conn.commit()
  117. def delete_block(self, block_id: str):
  118. """删除单个 block"""
  119. self.conn.execute('DELETE FROM document_blocks WHERE id = ?', (block_id,))
  120. self.conn.commit()
  121. def search_blocks(self, query: str, block_type: Optional[str] = None) -> list[dict]:
  122. """搜索 blocks"""
  123. sql = "SELECT * FROM document_blocks WHERE content LIKE ?"
  124. params = [f'%{query}%']
  125. if block_type:
  126. sql += " AND type = ?"
  127. params.append(block_type)
  128. sql += " ORDER BY block_order"
  129. cursor = self.conn.execute(sql, params)
  130. rows = cursor.fetchall()
  131. return [self._row_to_dict(row) for row in rows]
  132. def get_headings(self) -> list[dict]:
  133. """获取所有标题块"""
  134. cursor = self.conn.execute("""
  135. SELECT * FROM document_blocks
  136. WHERE type = 'heading'
  137. ORDER BY block_order
  138. """)
  139. rows = cursor.fetchall()
  140. return [self._row_to_dict(row) for row in rows]
  141. def get_stats(self) -> dict:
  142. """获取统计信息"""
  143. cursor = self.conn.execute("""
  144. SELECT
  145. type,
  146. COUNT(*) as count
  147. FROM document_blocks
  148. GROUP BY type
  149. """)
  150. stats = {row['type']: row['count'] for row in cursor.fetchall()}
  151. cursor = self.conn.execute("SELECT COUNT(*) as total FROM document_blocks")
  152. total = cursor.fetchone()['total']
  153. return {
  154. 'total': total,
  155. 'by_type': stats
  156. }
  157. def _row_to_dict(self, row) -> dict:
  158. """将 sqlite3.Row 转换为字典"""
  159. d = dict(row)
  160. # 解析 JSON 字段
  161. if d.get('style'):
  162. try:
  163. d['style'] = json.loads(d['style'])
  164. except (json.JSONDecodeError, TypeError):
  165. d['style'] = {}
  166. else:
  167. d['style'] = {}
  168. if d.get('metadata'):
  169. try:
  170. d['metadata'] = json.loads(d['metadata'])
  171. except (json.JSONDecodeError, TypeError):
  172. d['metadata'] = {}
  173. else:
  174. d['metadata'] = {}
  175. # content 可能是 JSON(富文本或表格)
  176. if d.get('content'):
  177. try:
  178. d['content'] = json.loads(d['content'])
  179. except (json.JSONDecodeError, TypeError):
  180. pass # 保持原字符串
  181. return d