From 92c325294c943f6aa7e2040a59ab1135c8313af5 Mon Sep 17 00:00:00 2001 From: KnugiHK <24708955+KnugiHK@users.noreply.github.com> Date: Mon, 19 Jan 2026 22:53:29 +0800 Subject: [PATCH] Add preflight check to see if the `jid_map` table exists --- Whatsapp_Chat_Exporter/__main__.py | 3 +- Whatsapp_Chat_Exporter/utility.py | 44 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/Whatsapp_Chat_Exporter/__main__.py b/Whatsapp_Chat_Exporter/__main__.py index 609abca..405eec7 100644 --- a/Whatsapp_Chat_Exporter/__main__.py +++ b/Whatsapp_Chat_Exporter/__main__.py @@ -16,7 +16,7 @@ from Whatsapp_Chat_Exporter.utility import APPLE_TIME, CLEAR_LINE, CURRENT_TZ_OF from Whatsapp_Chat_Exporter.utility import readable_to_bytes, safe_name, bytes_to_readable from Whatsapp_Chat_Exporter.utility import import_from_json, incremental_merge, check_update from Whatsapp_Chat_Exporter.utility import telegram_json_format, convert_time_unit, DbType -from Whatsapp_Chat_Exporter.utility import get_transcription_selection +from Whatsapp_Chat_Exporter.utility import get_transcription_selection, check_jid_map from argparse import ArgumentParser, SUPPRESS from datetime import datetime from getpass import getpass @@ -551,6 +551,7 @@ def process_messages(args, data: ChatCollection) -> None: # Process messages if args.android: message_handler = android_handler + data.set_system("jid_map_exists", check_jid_map(db)) data.set_system("transcription_selection", get_transcription_selection(db)) else: message_handler = ios_handler diff --git a/Whatsapp_Chat_Exporter/utility.py b/Whatsapp_Chat_Exporter/utility.py index 9e7047f..f9a5da4 100644 --- a/Whatsapp_Chat_Exporter/utility.py +++ b/Whatsapp_Chat_Exporter/utility.py @@ -572,6 +572,50 @@ def get_status_location(output_folder: str, offline_static: str) -> str: return w3css +def check_jid_map(db: sqlite3.Connection) -> bool: + """ + Checks if the jid_map table exists in the database. + + Args: + db (sqlite3.Connection): The SQLite database connection. + + Returns: + bool: True if the jid_map table exists, False otherwise. + """ + cursor = db.cursor() + cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='jid_map'") + return cursor.fetchone()is not None + + +def get_jid_map_join(jid_map_exists: bool) -> str: + """ + Returns the SQL JOIN statements for jid_map table. + """ + if not jid_map_exists: + return "" + else: + return """LEFT JOIN jid_map as jid_map_global + ON chat.jid_row_id = jid_map_global.lid_row_id + LEFT JOIN jid lid_global + ON jid_map_global.jid_row_id = lid_global._id + LEFT JOIN jid_map as jid_map_group + ON message.sender_jid_row_id = jid_map_group.lid_row_id + LEFT JOIN jid lid_group + ON jid_map_group.jid_row_id = lid_group._id""" + +def get_jid_map_selection(jid_map_exists: bool) -> tuple: + """ + Returns the SQL selection statements for jid_map table. + """ + if not jid_map_exists: + return "jid_global.raw_string", "jid_group.raw_string" + else: + return ( + "COALESCE(lid_global.raw_string, jid_global.raw_string)", + "COALESCE(lid_group.raw_string, jid_group.raw_string)" + ) + + def get_transcription_selection(db: sqlite3.Connection) -> str: """ Returns the SQL selection statement for transcription text based on the database schema.