Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions Sources/Adapters/LCPSQLite/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ import SQLite

final class Database {
/// Shared instance.
static let shared = Database()

let connection: Connection

private init() {
static let shared: Swift.Result<Database, Error> = {
do {
var url = try FileManager.default.url(
for: .libraryDirectory,
in: .userDomainMask,
appropriateFor: nil, create: true
)
url.appendPathComponent("lcpdatabase.sqlite")
connection = try Connection(url.absoluteString)
return try .success(Database())
} catch {
fatalError("Error initializing db.")
return .failure(error)
}
}()

let connection: Connection

private init() throws {
var url = try FileManager.default.url(
for: .libraryDirectory,
in: .userDomainMask,
appropriateFor: nil, create: true
)
url.appendPathComponent("lcpdatabase.sqlite")
connection = try Connection(url.absoluteString)
}
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/Adapters/LCPSQLite/SQLiteLCPLicenseRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public class LCPSQLiteLicenseRepository: LCPLicenseRepository {

private let db: Connection

public init() {
db = Database.shared.connection
public init() throws {
db = try Database.shared.get().connection

_ = try? db.run(licenses.create(temporary: false, ifNotExists: true) { t in
try db.run(licenses.create(temporary: false, ifNotExists: true) { t in
t.column(id, unique: true)
t.column(printsLeft)
t.column(copiesLeft)
})

if db.userVersion == 0 {
_ = try? db.run(licenses.addColumn(registered, defaultValue: false))
try db.run(licenses.addColumn(registered, defaultValue: false))
db.userVersion = 1
}
if db.userVersion == 1 {
Expand Down
20 changes: 8 additions & 12 deletions Sources/Adapters/LCPSQLite/SQLiteLCPPassphraseRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ public class LCPSQLitePassphraseRepository: LCPPassphraseRepository, Loggable {

private let db: Connection

public init() {
db = Database.shared.connection
public init() throws {
db = try Database.shared.get().connection

do {
try db.run(transactions.create(temporary: false, ifNotExists: true) { t in
t.column(licenseId)
t.column(provider)
t.column(userId)
t.column(passphrase)
})
} catch {
log(.error, error)
}
try db.run(transactions.create(temporary: false, ifNotExists: true) { t in
t.column(licenseId)
t.column(provider)
t.column(userId)
t.column(passphrase)
})
}

public func passphrase(for licenseID: LicenseDocument.ID) async throws -> LCPPassphraseHash? {
Expand Down
4 changes: 2 additions & 2 deletions TestApp/Sources/App/Readium.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ final class Readium {

lazy var lcpService = LCPService(
client: LCPClient(),
licenseRepository: LCPSQLiteLicenseRepository(),
passphraseRepository: LCPSQLitePassphraseRepository(),
licenseRepository: try! LCPSQLiteLicenseRepository(),
passphraseRepository: try! LCPSQLitePassphraseRepository(),
assetRetriever: assetRetriever,
httpClient: httpClient
)
Expand Down