Insert or update a node. Returns the node ID.
(self, node: NodeInfo, file_hash: str = "")
| 187 | # --- Write operations --- |
| 188 | |
| 189 | def upsert_node(self, node: NodeInfo, file_hash: str = "") -> int: |
| 190 | """Insert or update a node. Returns the node ID.""" |
| 191 | now = time.time() |
| 192 | qualified = self._make_qualified(node) |
| 193 | extra = json.dumps(node.extra) if node.extra else "{}" |
| 194 | |
| 195 | self._conn.execute( |
| 196 | """INSERT INTO nodes |
| 197 | (kind, name, qualified_name, file_path, line_start, line_end, |
| 198 | language, parent_name, params, return_type, modifiers, is_test, |
| 199 | file_hash, extra, updated_at) |
| 200 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) |
| 201 | ON CONFLICT(qualified_name) DO UPDATE SET |
| 202 | kind=excluded.kind, name=excluded.name, |
| 203 | file_path=excluded.file_path, line_start=excluded.line_start, |
| 204 | line_end=excluded.line_end, language=excluded.language, |
| 205 | parent_name=excluded.parent_name, params=excluded.params, |
| 206 | return_type=excluded.return_type, modifiers=excluded.modifiers, |
| 207 | is_test=excluded.is_test, file_hash=excluded.file_hash, |
| 208 | extra=excluded.extra, updated_at=excluded.updated_at |
| 209 | """, |
| 210 | ( |
| 211 | node.kind, node.name, qualified, node.file_path, |
| 212 | node.line_start, node.line_end, node.language, |
| 213 | node.parent_name, node.params, node.return_type, |
| 214 | node.modifiers, int(node.is_test), file_hash, |
| 215 | extra, now, |
| 216 | ), |
| 217 | ) |
| 218 | row = self._conn.execute( |
| 219 | "SELECT id FROM nodes WHERE qualified_name = ?", (qualified,) |
| 220 | ).fetchone() |
| 221 | return row["id"] |
| 222 | |
| 223 | def upsert_edge(self, edge: EdgeInfo) -> int: |
| 224 | """Insert or update an edge.""" |