diff --git a/markdown_sqlite.py b/markdown_sqlite.py index f89c6a9..bb4893b 100644 --- a/markdown_sqlite.py +++ b/markdown_sqlite.py @@ -228,16 +228,21 @@ class MarkdownProcessor: def merge_structures(self, existing: Dict, new: Dict, document_id: int) -> None: def merge_recursive(existing_node, new_node, parent_uuid): if not existing_node: + print('#### This seems to be a new stuff...') # This is a new node, insert it + print(f"##### inserting new heading: {new_node['title']}") heading_uuid = self.insert_heading(new_node['level'], new_node['title'], parent_uuid, document_id, new_node['path']) - body_uuid = self.insert_body(new_node['content'], heading_uuid, document_id) + ##print(f"##### inserting new body with id: {heading_uuid}, :\n {new_node['content']}\n") + if new_node['content']: + body_uuid = self.insert_body(new_node['content'], heading_uuid, document_id) for child in new_node['children']: merge_recursive(None, new[child], heading_uuid) else: + print('#### This seems to known ') # Update existing node self.update_heading(existing_node['uuid'], new_node['title'], new_node['level'], parent_uuid, new_node['path']) self.update_body(existing_node['body_uuid'], new_node['content'], document_id) - + print('#### Does these update_body and update_heading methods work?') # Process children existing_children = {child['title']: child for child in existing_node['children']} new_children = {child['title']: child for child in new_node['children']} @@ -253,6 +258,7 @@ class MarkdownProcessor: self.soft_delete_heading(child['uuid']) for new_root in new.values(): + print('#### Found a chapter!') existing_root = next((node for node in existing.values() if node['path'] == new_root['path']), None) merge_recursive(existing_root, new_root, None) @@ -274,10 +280,39 @@ class MarkdownProcessor: def insert_body(self, content: str, heading_uuid: str, document_id: int) -> str: body_uuid = str(uuid.uuid4()) md5sum = hashlib.md5(content.encode()).hexdigest() - self.db_manager.cursor.execute(''' - INSERT INTO body (uuid, content, heading_uuid, document_id, md5sum) - VALUES (?, ?, ?, ?, ?) - ''', (body_uuid, content, heading_uuid, document_id, md5sum)) + print(f"###### Trying to insert body text with md5sum of: {md5sum} to uuid: {body_uuid}, with content: \n{content}\n") + + # Verify input parameters + if not all([content, heading_uuid, document_id]): + raise ValueError("Missing required parameters for insert_body") + + try: + # Check if heading_uuid exists + self.db_manager.cursor.execute("SELECT 1 FROM headings WHERE uuid = ?", (heading_uuid,)) + if not self.db_manager.cursor.fetchone(): + raise ValueError(f"heading_uuid {heading_uuid} does not exist in headings table") + + # Check if document_id exists + self.db_manager.cursor.execute("SELECT 1 FROM documents WHERE id = ?", (document_id,)) + if not self.db_manager.cursor.fetchone(): + raise ValueError(f"document_id {document_id} does not exist in documents table") + + # Insert the body + self.db_manager.cursor.execute(''' + INSERT INTO body (uuid, content, heading_uuid, document_id, md5sum) + VALUES (?, ?, ?, ?, ?) + ''', (body_uuid, content, heading_uuid, document_id, md5sum)) + + self.db_manager.conn.commit() + print(f"###### Successfully inserted body with uuid: {body_uuid}") + except sqlite3.Error as e: + print(f"An error occurred while inserting body: {e}") + self.db_manager.conn.rollback() + raise + except ValueError as e: + print(f"Validation error: {e}") + raise + return body_uuid def update_body(self, body_uuid: str, content: str, document_id: int) -> None: @@ -583,4 +618,4 @@ def main(): db_manager.close() if __name__ == '__main__': - main() \ No newline at end of file + main()