[#923] Update error handling in DatabaseStorageManager.swift
- cleanup of DatabaseStorageManager, not used anymore - ZcashError for SimpleConnectionProvider
This commit is contained in:
parent
195bb959e8
commit
94e028d31a
|
@ -1,75 +0,0 @@
|
|||
//
|
||||
// SQLDatabase.swift
|
||||
// ZcashLightClientKit
|
||||
//
|
||||
// Created by Francisco Gindre on 10/13/19.
|
||||
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SQLite
|
||||
|
||||
class DatabaseStorageManager {
|
||||
private var readOnly: [URL: Connection] = [:]
|
||||
private var readWrite: [URL: Connection] = [:]
|
||||
|
||||
init() {}
|
||||
|
||||
func connection(at url: URL, readOnly: Bool = false) throws -> Connection {
|
||||
readOnly ? try readOnlyConnection(at: url) : try readWriteConnection(at: url)
|
||||
}
|
||||
|
||||
private func readOnlyConnection(at url: URL) throws -> Connection {
|
||||
if let readOnlyConnection = readOnly[url] {
|
||||
return readOnlyConnection
|
||||
}
|
||||
|
||||
let readOnlyConnection = try Connection.customConection(at: url)
|
||||
readOnly[url] = readOnlyConnection
|
||||
|
||||
return readOnlyConnection
|
||||
}
|
||||
|
||||
private func readWriteConnection(at url: URL) throws -> Connection {
|
||||
if let readWriteConnection = readWrite[url] {
|
||||
return readWriteConnection
|
||||
}
|
||||
|
||||
let readWriteConnection = try Connection.customConection(at: url)
|
||||
readWrite[url] = readWriteConnection
|
||||
|
||||
return readWriteConnection
|
||||
}
|
||||
}
|
||||
|
||||
private extension Connection {
|
||||
static func customConection(at url: URL, readonly: Bool = false) throws -> Connection {
|
||||
let conn = try Connection(url.absoluteString, readonly: readonly)
|
||||
try conn.run("PRAGMA journal_mode = TRUNCATE;")
|
||||
return conn
|
||||
}
|
||||
}
|
||||
|
||||
class SimpleConnectionProvider: ConnectionProvider {
|
||||
var path: String
|
||||
var readonly: Bool
|
||||
var db: Connection?
|
||||
|
||||
init(path: String, readonly: Bool = false) {
|
||||
self.path = path
|
||||
self.readonly = readonly
|
||||
}
|
||||
|
||||
func connection() throws -> Connection {
|
||||
guard let conn = db else {
|
||||
let conn = try Connection(path, readonly: readonly)
|
||||
self.db = conn
|
||||
return conn
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
func close() {
|
||||
self.db = nil
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
//
|
||||
// SimpleConnectionProvider.swift
|
||||
// ZcashLightClientKit
|
||||
//
|
||||
// Created by Francisco Gindre on 10/13/19.
|
||||
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SQLite
|
||||
|
||||
class SimpleConnectionProvider: ConnectionProvider {
|
||||
var path: String
|
||||
var readonly: Bool
|
||||
var db: Connection?
|
||||
|
||||
init(path: String, readonly: Bool = false) {
|
||||
self.path = path
|
||||
self.readonly = readonly
|
||||
}
|
||||
|
||||
/// throws ZcashError.simpleConnectionProvider
|
||||
func connection() throws -> Connection {
|
||||
guard let conn = db else {
|
||||
do {
|
||||
let conn = try Connection(path, readonly: readonly)
|
||||
self.db = conn
|
||||
return conn
|
||||
} catch {
|
||||
throw ZcashError.simpleConnectionProvider(error)
|
||||
}
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
func close() {
|
||||
self.db = nil
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
// Generated using Sourcery 2.0.2 — https://github.com/krzysztofzablocki/Sourcery
|
||||
// Generated using Sourcery 1.6.1 — https://github.com/krzysztofzablocki/Sourcery
|
||||
// DO NOT EDIT
|
||||
|
||||
/*
|
||||
|
@ -11,10 +11,11 @@ error originates. And it can help with debugging.
|
|||
public enum ZcashError: Equatable, Error {
|
||||
/// Some testing code for now. Will be removed later.
|
||||
/// Some multiline super doc:
|
||||
/// - message - Message associated with error
|
||||
/// - code - Code for error.
|
||||
/// - error - underlying error
|
||||
/// ZTEST0001
|
||||
case testCodeWithMessage(_ code: Int, _ error: Error)
|
||||
case testCodeWithMessage(_ message: String, _ code: Int, _ error: Error)
|
||||
/// Unknown GRPC Service error
|
||||
/// ZSRVC0001
|
||||
case serviceUnknownError(_ error: Error)
|
||||
|
@ -42,6 +43,9 @@ public enum ZcashError: Equatable, Error {
|
|||
/// LightWalletService.blockStream failed.
|
||||
/// ZSRVC0000
|
||||
case serviceBlockStreamFailed(_ error: LightWalletServiceError)
|
||||
/// SimpleConnectionProvider init of Connection failed.
|
||||
/// ZSCPC0001
|
||||
case simpleConnectionProvider(_ error: Error)
|
||||
|
||||
public var message: String {
|
||||
switch self {
|
||||
|
@ -55,6 +59,7 @@ public enum ZcashError: Equatable, Error {
|
|||
case .serviceFetchTransactionFailed: return "LightWalletService.fetchTransaction failed."
|
||||
case .serviceFetchUTXOsFailed: return "LightWalletService.fetchUTXOs failed."
|
||||
case .serviceBlockStreamFailed: return "LightWalletService.blockStream failed."
|
||||
case .simpleConnectionProvider: return "SimpleConnectionProvider init of Connection failed."
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -70,6 +75,7 @@ public enum ZcashError: Equatable, Error {
|
|||
case .serviceFetchTransactionFailed: return .serviceFetchTransactionFailed
|
||||
case .serviceFetchUTXOsFailed: return .serviceFetchUTXOsFailed
|
||||
case .serviceBlockStreamFailed: return .serviceBlockStreamFailed
|
||||
case .simpleConnectionProvider: return .simpleConnectionProvider
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Generated using Sourcery 2.0.2 — https://github.com/krzysztofzablocki/Sourcery
|
||||
// Generated using Sourcery 1.6.1 — https://github.com/krzysztofzablocki/Sourcery
|
||||
// DO NOT EDIT
|
||||
|
||||
/*
|
||||
|
@ -33,4 +33,6 @@ public enum ZcashErrorCode: String {
|
|||
case serviceFetchUTXOsFailed = "ZSRVC0008"
|
||||
/// LightWalletService.blockStream failed.
|
||||
case serviceBlockStreamFailed = "ZSRVC0000"
|
||||
/// SimpleConnectionProvider init of Connection failed.
|
||||
case simpleConnectionProvider = "ZSCPC0001"
|
||||
}
|
||||
|
|
|
@ -56,4 +56,7 @@ enum ZcashErrorDefinition {
|
|||
/// LightWalletService.blockStream failed.
|
||||
// sourcery: code="ZSRVC0000"
|
||||
case serviceBlockStreamFailed(_ error: LightWalletServiceError)
|
||||
/// SimpleConnectionProvider init of Connection failed.
|
||||
// sourcery: code="ZSCPC0001"
|
||||
case simpleConnectionProvider(_ error: Error)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue