Skip to content

Commit

Permalink
lock on every db access
Browse files Browse the repository at this point in the history
  • Loading branch information
flypaper0 committed Nov 7, 2023
1 parent 31887b3 commit 92a6de8
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Sources/Database/DiskSqlite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ public final class DiskSqlite: Sqlite {
}

public func openDatabase() throws {
defer { lock.lock() }
defer { lock.unlock() }
lock.lock()

guard sqlite3_open_v2(path, &db, SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_FULLMUTEX, nil) == SQLITE_OK else {
throw SQLiteError.openDatabase(path: path)
}
}

public func query<Row: SqliteRow>(sql: String) throws -> [Row] {
defer { lock.unlock() }
lock.lock()

var queryStatement: OpaquePointer?
guard sqlite3_prepare_v2(db, sql, -1, &queryStatement, nil) == SQLITE_OK else {
throw SQLiteError.queryPrepare(statement: sql)
Expand All @@ -36,6 +41,9 @@ public final class DiskSqlite: Sqlite {
}

public func execute(sql: String) throws {
defer { lock.unlock() }
lock.lock()

var error: UnsafeMutablePointer<CChar>?
guard sqlite3_exec(db, sql, nil, nil, &error) == SQLITE_OK else {
let message = error.map { String(cString: $0) }
Expand All @@ -45,6 +53,8 @@ public final class DiskSqlite: Sqlite {

public func closeConnection() {
defer { lock.unlock() }
lock.lock()

sqlite3_close(db)
}
}

0 comments on commit 92a6de8

Please sign in to comment.