Repository Public License
1import Foundation2import NavigatorCore34/// One SQLite storage-class value. Everything bound to or read from a statement goes through this type.5public enum SQLiteValue: Sendable, Hashable {6 case null7 case integer(Int64)8 case real(Double)9 case text(String)10 case blob(Data)1112 public var isNull: Bool { self == .null }13}1415/// A Swift type with one canonical SQLite representation.16///17/// Conventions used by the whole schema: dates are `REAL` seconds since 1970, identifiers are `TEXT` UUIDs,18/// URLs are `TEXT` absolute strings, booleans are `INTEGER` 0/1.19public protocol SQLiteConvertible: Sendable {20 var sqliteValue: SQLiteValue { get }21 /// Returns `nil` when the stored value cannot represent `Self` (wrong storage class, malformed text).22 static func fromSQLite(_ value: SQLiteValue) -> Self?23}2425extension SQLiteValue: SQLiteConvertible {26 public var sqliteValue: SQLiteValue { self }27 public static func fromSQLite(_ value: SQLiteValue) -> SQLiteValue? { value }28}2930extension Int64: SQLiteConvertible {31 public var sqliteValue: SQLiteValue { .integer(self) }32 public static func fromSQLite(_ value: SQLiteValue) -> Int64? {33 if case .integer(let number) = value { return number }34 return nil35 }36}3738extension Int: SQLiteConvertible {39 public var sqliteValue: SQLiteValue { .integer(Int64(self)) }40 public static func fromSQLite(_ value: SQLiteValue) -> Int? {41 if case .integer(let number) = value { return Int(exactly: number) }42 return nil43 }44}4546extension Bool: SQLiteConvertible {47 public var sqliteValue: SQLiteValue { .integer(self ? 1 : 0) }48 public static func fromSQLite(_ value: SQLiteValue) -> Bool? {49 if case .integer(let number) = value { return number != 0 }50 return nil51 }52}5354extension Double: SQLiteConvertible {55 public var sqliteValue: SQLiteValue { .real(self) }56 public static func fromSQLite(_ value: SQLiteValue) -> Double? {57 switch value {58 case .real(let number): return number59 // SQLite stores a REAL with an integral value as INTEGER in some column affinities.60 case .integer(let number): return Double(number)61 default: return nil62 }63 }64}6566extension String: SQLiteConvertible {67 public var sqliteValue: SQLiteValue { .text(self) }68 public static func fromSQLite(_ value: SQLiteValue) -> String? {69 if case .text(let text) = value { return text }70 return nil71 }72}7374extension Data: SQLiteConvertible {75 public var sqliteValue: SQLiteValue { .blob(self) }76 public static func fromSQLite(_ value: SQLiteValue) -> Data? {77 if case .blob(let data) = value { return data }78 return nil79 }80}8182extension Date: SQLiteConvertible {83 public var sqliteValue: SQLiteValue { .real(timeIntervalSince1970) }84 public static func fromSQLite(_ value: SQLiteValue) -> Date? {85 Double.fromSQLite(value).map(Date.init(timeIntervalSince1970:))86 }87}8889extension URL: SQLiteConvertible {90 public var sqliteValue: SQLiteValue { .text(absoluteString) }91 public static func fromSQLite(_ value: SQLiteValue) -> URL? {92 String.fromSQLite(value).flatMap(URL.init(string:))93 }94}9596extension UUID: SQLiteConvertible {97 public var sqliteValue: SQLiteValue { .text(uuidString) }98 public static func fromSQLite(_ value: SQLiteValue) -> UUID? {99 String.fromSQLite(value).flatMap(UUID.init(uuidString:))100 }101}102103extension ID: SQLiteConvertible {104 public var sqliteValue: SQLiteValue { .text(string) }105 public static func fromSQLite(_ value: SQLiteValue) -> ID? {106 String.fromSQLite(value).flatMap(ID.init(string:))107 }108}109110extension Optional: SQLiteConvertible where Wrapped: SQLiteConvertible {111 public var sqliteValue: SQLiteValue { self?.sqliteValue ?? .null }112 public static func fromSQLite(_ value: SQLiteValue) -> Wrapped?? {113 if value.isNull { return .some(nil) }114 return Wrapped.fromSQLite(value).map(Optional.some)115 }116}117118/// An error reported by SQLite itself. `sql` is the statement text only: bound values are never captured.119public struct SQLiteError: Error, Sendable, Equatable, CustomStringConvertible {120 /// Primary result code (`SQLITE_BUSY`, `SQLITE_CONSTRAINT`, `SQLITE_CORRUPT`…).121 public let code: Int32122 /// Extended result code (`SQLITE_CONSTRAINT_FOREIGNKEY`…).123 public let extendedCode: Int32124 public let message: String125 public let sql: String?126127 public init(code: Int32, extendedCode: Int32, message: String, sql: String? = nil) {128 self.code = code; self.extendedCode = extendedCode; self.message = message; self.sql = sql129 }130131 public var description: String {132 let statement = sql.map { " — while running: \($0.prefix(200))" } ?? ""133 return "SQLite error \(code) (extended \(extendedCode)): \(message)\(statement)"134 }135136 /// `SQLITE_CORRUPT` or `SQLITE_NOTADB`: the file is damaged or is not a database.137 public var indicatesCorruption: Bool { code == 11 || code == 26 }138 /// `SQLITE_CONSTRAINT`.139 public var isConstraintViolation: Bool { code == 19 }140}141142/// Errors of the storage layer that are not raw SQLite failures.143public enum PersistenceError: Error, Sendable, Equatable, CustomStringConvertible {144 /// The database was written by a newer version of the app. It is left untouched.145 case databaseTooNew(found: Int, supported: Int)146 /// The file failed to open as a database or failed the integrity check. It is left untouched;147 /// `NavigatorDatabase.openRecovering(url:)` is the documented recovery path.148 case corrupt(path: String, details: String)149 /// A write was attempted for a ghost (private) profile or for something that belongs to one.150 case ghostWriteRejected(entity: String)151 /// A write referenced a profile, space or window that is not stored (unknown, deleted, or ghost and152 /// therefore never stored).153 case ownerNotPersisted(owner: String, id: String)154 /// A stored value could not be decoded into its model type.155 case invalidData(String)156 case notFound(entity: String, id: String)157 /// The operation contradicts the current state (moving a folder into itself, reopening into a closed window…).158 case conflict(String)159 case databaseClosed160161 public var description: String {162 switch self {163 case .databaseTooNew(let found, let supported):164 return "Database schema version \(found) is newer than the version \(supported) this build supports."165 case .corrupt(let path, let details): return "Database at \(path) is corrupt: \(details)"166 case .ghostWriteRejected(let entity): return "Refused to persist ghost data (\(entity))."167 case .ownerNotPersisted(let owner, let id): return "The \(owner) \(id) is not persisted (unknown or ghost)."168 case .invalidData(let message): return "Invalid stored data: \(message)"169 case .notFound(let entity, let id): return "No \(entity) with id \(id)."170 case .conflict(let message): return "Conflict: \(message)"171 case .databaseClosed: return "The database is closed."172 }173 }174}175