import sqlite3
from pathlib import Path

p = Path("state.vscdb")
print("db_exists=", p.exists(), p.resolve())

conn = sqlite3.connect(p)
cur = conn.cursor()

tables = cur.execute(
    "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"
).fetchall()
print("tables=", tables)

try:
    schema = cur.execute("PRAGMA table_info(ItemTable)").fetchall()
    print("ItemTable schema=", schema)

    rows = cur.execute(
        """
        SELECT key, value
        FROM ItemTable
        WHERE lower(key) LIKE '%auth%'
           OR lower(key) LIKE '%token%'
           OR lower(key) LIKE '%cursoraccount%'
           OR lower(key) LIKE '%cursor%'
        ORDER BY key
        """
    ).fetchall()

    print("matched_rows=", len(rows))
    for k, v in rows:
        s = "" if v is None else str(v)
        masked = (s[:18] + "..." + s[-12:]) if len(s) > 40 else s
        print(f"{k} len={len(s)} value={masked}")

    print("\ncursorDiskKV schema=", cur.execute("PRAGMA table_info(cursorDiskKV)").fetchall())
    disk_rows = cur.execute(
        """
        SELECT key, value
        FROM cursorDiskKV
        WHERE lower(key) LIKE '%auth%'
           OR lower(key) LIKE '%token%'
           OR lower(key) LIKE '%cursor%'
           OR lower(key) LIKE '%account%'
        ORDER BY key
        """
    ).fetchall()
    print("cursorDiskKV matched_rows=", len(disk_rows))
    for k, v in disk_rows:
        if isinstance(v, bytes):
            s = v.decode("utf-8", "ignore")
        else:
            s = "" if v is None else str(v)
        masked = (s[:18] + "..." + s[-12:]) if len(s) > 40 else s
        print(f"{k} len={len(s)} value={masked}")
finally:
    conn.close()
