MCPcopy
hub / github.com/AbdullahGhanem/quran-database / convert

Function convert

convert_to_sqlite.py:347–422  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

345
346
347def convert():
348 print("Creating SQLite database with improved schema...")
349
350 if os.path.exists(DB_FILE):
351 os.remove(DB_FILE)
352
353 db = sqlite3.connect(DB_FILE)
354 create_sqlite_schema(db)
355
356 # Disable FK checks during import for speed and to handle cross-references
357 db.execute("PRAGMA foreign_keys = OFF")
358
359 table_counts = {t: 0 for t in TABLE_ORDER}
360
361 print(f"Reading {SQL_FILE}...")
362
363 for table, columns, rows in stream_inserts(SQL_FILE):
364 if table in TABLE_ORDER:
365 ncols = len(columns)
366 placeholders = ','.join(['?'] * ncols)
367 insert_sql = f"INSERT INTO {table} VALUES ({placeholders})"
368
369 db.executemany(insert_sql, rows)
370 table_counts[table] += len(rows)
371 if table_counts[table] <= len(rows) or table_counts[table] % 5000 == 0:
372 print(f" {table}: {table_counts[table]} rows...")
373
374 # Re-enable FK checks
375 db.execute("PRAGMA foreign_keys = ON")
376 db.execute("PRAGMA foreign_key_check")
377 db.commit()
378
379 db.commit()
380
381 # Populate lookup tables after data
382 print(" Populating juzs and hizbs lookup tables...")
383 populate_lookup_tables(db)
384
385 # Create views
386 db.execute("""
387 CREATE VIEW IF NOT EXISTS surah_stats AS
388 SELECT s.id, s.name_ar, s.name_en, s.name_en_translation, s.type,
389 COUNT(a.id) as ayat_count,
390 MIN(a.id) as start_ayah_id,
391 MAX(a.id) as end_ayah_id
392 FROM surahs s
393 JOIN ayahs a ON a.surah_id = s.id
394 GROUP BY s.id
395 """)
396
397 db.execute("""
398 CREATE VIEW IF NOT EXISTS ayah_with_translation AS
399 SELECT a.id, a.surah_id, a.number_in_surah, a.text as arabic,
400 ae.data as translation, e.language, e.name as edition_name
401 FROM ayahs a
402 JOIN ayah_edition ae ON ae.ayah_id = a.id
403 JOIN editions e ON e.id = ae.edition_id
404 """)

Callers 1

Calls 3

create_sqlite_schemaFunction · 0.85
stream_insertsFunction · 0.85
populate_lookup_tablesFunction · 0.85

Tested by

no test coverage detected