|
| 1 | +import Foundation |
| 2 | +import GRDB |
| 3 | + |
| 4 | +final class DatabaseManager { |
| 5 | + private let writer: DatabaseWriter |
| 6 | + private let reader: DatabaseReader |
| 7 | + |
| 8 | + private init(writer: DatabaseWriter, reader: DatabaseReader) { |
| 9 | + self.writer = writer |
| 10 | + self.reader = reader |
| 11 | + } |
| 12 | + |
| 13 | + static func open(readOnly: Bool = false) throws -> DatabaseManager { |
| 14 | + let appSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first! |
| 15 | + let bundleDB = Bundle.main.url(forResource: "bible", withExtension: "db")! |
| 16 | + let destDB = appSupport.appendingPathComponent("bible.db") |
| 17 | + |
| 18 | + if !FileManager.default.fileExists(atPath: destDB.path) { |
| 19 | + try FileManager.default.copyItem(at: bundleDB, to: destDB) |
| 20 | + } |
| 21 | + |
| 22 | + var config = Configuration() |
| 23 | + config.readonly = readOnly |
| 24 | + config.foreignKeysEnabled = true |
| 25 | + |
| 26 | + if readOnly { |
| 27 | + let db = try Database(path: destDB.path, configuration: config) |
| 28 | + return DatabaseManager(writer: db, reader: db) |
| 29 | + } else { |
| 30 | + let db = try DatabaseQueue(path: destDB.path, configuration: config) |
| 31 | + try migrate(db) |
| 32 | + return DatabaseManager(writer: db, reader: db) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + var verseDAO: VerseDAO { .init(db: reader, writer: writer) } |
| 37 | + var favoritesDAO: FavoritesDAO { .init(db: writer) } |
| 38 | + var highlightsDAO: HighlightsDAO { .init(db: writer) } |
| 39 | + var notesDAO: NotesDAO { .init(db: writer) } |
| 40 | + var lexiconDAO: LexiconDAO { .init(db: writer) } |
| 41 | + var interlinearDAO: InterlinearDAO { .init(db: writer) } |
| 42 | + var readingAnalyticsDAO: ReadingAnalyticsDAO { .init(db: writer) } |
| 43 | + |
| 44 | + var dbSizeMb: Double { |
| 45 | + let appSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first! |
| 46 | + let dbPath = appSupport.appendingPathComponent("bible.db") |
| 47 | + guard let attrs = try? FileManager.default.attributesOfItem(atPath: dbPath.path), |
| 48 | + let size = attrs[.size] as? Double else { return 0 } |
| 49 | + return size / (1024.0 * 1024.0) |
| 50 | + } |
| 51 | + |
| 52 | + func getStats() -> LibraryStats { |
| 53 | + verseDAO.getStats(dbSizeMb: dbSizeMb) |
| 54 | + } |
| 55 | + |
| 56 | + // MARK: - Migrations |
| 57 | + |
| 58 | + private static func migrate(_ db: DatabaseQueue) throws { |
| 59 | + var migrator = DatabaseMigrator() |
| 60 | + |
| 61 | + migrator.registerMigration("v1") { db in |
| 62 | + try db.create(table: "verses", ifNotExists: true) { t in |
| 63 | + t.column("book", .text).notNull() |
| 64 | + t.column("chapter", .integer).notNull() |
| 65 | + t.column("verse", .integer).notNull() |
| 66 | + t.column("text", .text).notNull() |
| 67 | + t.column("version", .text) |
| 68 | + t.primaryKey(["book", "chapter", "verse"]) |
| 69 | + } |
| 70 | + |
| 71 | + try db.create(table: "favorites", ifNotExists: true) { t in |
| 72 | + t.column("strongs", .text).notNull().primaryKey() |
| 73 | + t.column("lemma", .text) |
| 74 | + t.column("definition", .text) |
| 75 | + } |
| 76 | + |
| 77 | + try db.create(table: "highlights", ifNotExists: true) { t in |
| 78 | + t.column("book", .text).notNull() |
| 79 | + t.column("chapter", .integer).notNull() |
| 80 | + t.column("verse", .integer).notNull() |
| 81 | + t.column("color", .integer) |
| 82 | + t.primaryKey(["book", "chapter", "verse"]) |
| 83 | + } |
| 84 | + |
| 85 | + try db.create(table: "notes", ifNotExists: true) { t in |
| 86 | + t.column("id", .integer).primaryKey(autoincrement: true) |
| 87 | + t.column("book", .text).notNull() |
| 88 | + t.column("chapter", .integer).notNull() |
| 89 | + t.column("verse", .integer).notNull() |
| 90 | + t.column("content", .text).notNull() |
| 91 | + t.column("timestamp", .datetime).defaults(to: SQL("CURRENT_TIMESTAMP")) |
| 92 | + } |
| 93 | + |
| 94 | + try db.create(table: "lexicon", ifNotExists: true) { t in |
| 95 | + t.column("strongs", .text).notNull().primaryKey() |
| 96 | + t.column("language", .text) |
| 97 | + t.column("lemma", .text) |
| 98 | + t.column("transliteration", .text) |
| 99 | + t.column("definition", .text) |
| 100 | + } |
| 101 | + |
| 102 | + try db.create(table: "interlinear", ifNotExists: true) { t in |
| 103 | + t.column("book", .text).notNull() |
| 104 | + t.column("chapter", .integer).notNull() |
| 105 | + t.column("verse", .integer).notNull() |
| 106 | + t.column("word_index", .integer).notNull() |
| 107 | + t.column("original_text", .text) |
| 108 | + t.column("translation", .text) |
| 109 | + t.column("strongs", .text) |
| 110 | + t.primaryKey(["book", "chapter", "verse", "word_index"]) |
| 111 | + } |
| 112 | + |
| 113 | + try db.create(table: "reading_progress", ifNotExists: true) { t in |
| 114 | + t.column("book", .text).notNull() |
| 115 | + t.column("chapter", .integer).notNull() |
| 116 | + t.column("first_read_at", .integer) |
| 117 | + t.column("last_read_at", .integer) |
| 118 | + t.column("read_count", .integer).defaults(to: 0) |
| 119 | + t.column("reading_time_seconds", .integer).defaults(to: 0) |
| 120 | + t.primaryKey(["book", "chapter"]) |
| 121 | + } |
| 122 | + |
| 123 | + try db.create(table: "reading_events", ifNotExists: true) { t in |
| 124 | + t.column("id", .integer).primaryKey(autoincrement: true) |
| 125 | + t.column("book", .text).notNull() |
| 126 | + t.column("chapter", .integer).notNull() |
| 127 | + t.column("timestamp", .integer).notNull() |
| 128 | + } |
| 129 | + |
| 130 | + // FTS4 virtual table for verse search |
| 131 | + try db.execute(sql: """ |
| 132 | + CREATE VIRTUAL TABLE IF NOT EXISTS verses_fts USING fts4(book, chapter, verse, text) |
| 133 | + """) |
| 134 | + |
| 135 | + // Triggers to keep FTS in sync |
| 136 | + try db.execute(sql: """ |
| 137 | + CREATE TRIGGER IF NOT EXISTS verses_ai AFTER INSERT ON verses BEGIN |
| 138 | + INSERT INTO verses_fts(docid, book, chapter, verse, text) |
| 139 | + VALUES (new.rowid, new.book, new.chapter, new.verse, new.text); |
| 140 | + END |
| 141 | + """) |
| 142 | + |
| 143 | + try db.execute(sql: """ |
| 144 | + CREATE TRIGGER IF NOT EXISTS verses_ad AFTER DELETE ON verses BEGIN |
| 145 | + INSERT INTO verses_fts(verses_fts, docid, book, chapter, verse, text) |
| 146 | + VALUES('delete', old.rowid, old.book, old.chapter, old.verse, old.text); |
| 147 | + END |
| 148 | + """) |
| 149 | + |
| 150 | + try db.execute(sql: """ |
| 151 | + CREATE TRIGGER IF NOT EXISTS verses_au AFTER UPDATE ON verses BEGIN |
| 152 | + INSERT INTO verses_fts(verses_fts, docid, book, chapter, verse, text) |
| 153 | + VALUES('delete', old.rowid, old.book, old.chapter, old.verse, old.text); |
| 154 | + INSERT INTO verses_fts(docid, book, chapter, verse, text) |
| 155 | + VALUES (new.rowid, new.book, new.chapter, new.verse, new.text); |
| 156 | + END |
| 157 | + """) |
| 158 | + } |
| 159 | + |
| 160 | + try migrator.migrate(db) |
| 161 | + } |
| 162 | +} |
0 commit comments