import json
import sqlite3
import sys
from pathlib import Path

# Read only the synthetic scenario database supplied by the caller.
connection = sqlite3.connect(Path(sys.argv[1]).resolve().as_uri() + '?mode=ro', uri=True)
needles = {
    'fixed_diagnosis': 'Git found a broken local ref.',
    'raw_bad_object_stderr': 'fatal: bad object refs/',
    'malformed_ref_name': 'refs/remotes/origin/dev 2',
}
counts = dict.fromkeys(needles, 0)
for (table,) in connection.execute("SELECT name FROM sqlite_master WHERE type='table'"):
    quoted = '"' + table.replace('"', '""') + '"'
    for row in connection.execute('SELECT * FROM ' + quoted):
        for value in row:
            if isinstance(value, str):
                for label, needle in needles.items():
                    counts[label] += value.count(needle)
print(json.dumps(counts, indent=2))
