From ce19b319b2078723ded67a2008275bb93a5f7d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9C=AA=20vi4m=20Marcin=20Kliks?= Date: Wed, 9 Oct 2024 23:36:52 +0200 Subject: [PATCH] Refactor SQLiteConnection.swift to support readonly mode for file-based databases --- Sources/SQLiteNIO/SQLiteConnection.swift | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Sources/SQLiteNIO/SQLiteConnection.swift b/Sources/SQLiteNIO/SQLiteConnection.swift index 7679d98..2ed87a7 100644 --- a/Sources/SQLiteNIO/SQLiteConnection.swift +++ b/Sources/SQLiteNIO/SQLiteConnection.swift @@ -78,7 +78,7 @@ public final class SQLiteConnection: SQLiteDatabase, Sendable { /// /// File-based databases persist as long as the files representing them on disk does, and can be opened /// multiple times within the same process or even by multiple processes if configured properly. - case file(path: String) + case file(path: String, readonly: Bool = false) } /// Return the version of the embedded libsqlite3 as a 32-bit integer value. @@ -149,16 +149,26 @@ public final class SQLiteConnection: SQLiteDatabase, Sendable { eventLoop: any EventLoop ) throws -> SQLiteConnection { let path: String + var readonlyMode = false + switch storage { case .memory: path = ":memory:" - case .file(let file): path = file + case .file(let file, let readonly): + path = file + readonlyMode = readonly } var handle: OpaquePointer? - let openOptions = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_URI | SQLITE_OPEN_EXRESCODE + var openOptions = + readonlyMode ? SQLITE_OPEN_READONLY : SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE + openOptions |= + SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_URI + | SQLITE_OPEN_EXRESCODE let openRet = sqlite_nio_sqlite3_open_v2(path, &handle, openOptions, nil) guard openRet == SQLITE_OK else { - throw SQLiteError(reason: .init(statusCode: openRet), message: "Failed to open to SQLite database at \(path)") + throw SQLiteError( + reason: .init(statusCode: openRet), + message: "Failed to open to SQLite database at \(path)") } let busyRet = sqlite_nio_sqlite3_busy_handler(handle, { _, _ in 1 }, nil)