forked from Ivasoft/mattermost-mobile
* fix: handle NSMutableData * feat: integrate react-native-network-client * fix: typos * fix: semicolon * fix: rename to urlVersion * fix: add returnDataOnly arg * fix: configure network client * fix: headers * fix: handling of serverVersion * fix: rename requests to actions * fix: action imports * fix: no need to stringify body * fix: sso flow * fix: address PR feedback * fix: invalidate client on logout * fix: address PR feedback take 2 * fix: address PR feedback take 3 * fix: tsc issues * fix: get csrf token during client creation * fix: linter * fix: invalidate client onLogout * fix: event emitter * fix: unit tests * fix: apply linter fixes * fix lint * Modify actions to add / update database values * Rename clien4.d.ts to client.d.ts * fix empty & missing translations * cleanup api client * Cleanup init & squash some TODO's * Emit certificate errors in NetworkManager * cleanup user actions * Fix NetworkManager invalidate client * Invalidate client when server screen appears * Update kotlin to 1.4.30 required by network-client * patch react-native-keychain to remove cached credential * update react-native-network-client * Use app.db instead of default.db in native code * fix use of rnnc on Android * Init PushNotifications * No need to reset serverVersion on logout * fix logout action * fix deleteServerDatabase * fix schedule expired session notification * use safeParseJSON for db json fields * unsubscribe when database component unmounts * cleanup init * session type * pass launchprops to entire login flow * Properly remove third party cookies after SSO login * recreate network client if sso with redirect fails * add missing launch props from server screen * use query prefix for database queries * Add temporary logout function to channel screen Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
83 lines
2.6 KiB
Swift
83 lines
2.6 KiB
Swift
//
|
|
// DatabaseHelper.swift
|
|
// DatabaseHelper
|
|
//
|
|
// Created by Miguel Alatzar on 6/14/21.
|
|
//
|
|
|
|
import Foundation
|
|
import SQLite3
|
|
|
|
// TODO: This should be exposed to Objective-C in order to handle
|
|
// any DatabaseHelper throwable methods.
|
|
enum DatabaseError: Error {
|
|
case OpenFailure(_ dbPath: String)
|
|
case MultipleServers
|
|
case NoResults(_ query: String)
|
|
}
|
|
|
|
extension DatabaseError: LocalizedError {
|
|
var errorDescription: String? {
|
|
switch self {
|
|
case .OpenFailure(dbPath: let dbPath):
|
|
return "Error opening database: \(dbPath)"
|
|
case .MultipleServers:
|
|
return "Cannot determine server URL as there are multiple servers"
|
|
case .NoResults(query: let query):
|
|
return "No results for query: \(query)"
|
|
}
|
|
}
|
|
}
|
|
|
|
public class DatabaseHelper: NSObject {
|
|
internal let DEFAULT_DB_NAME = "app.db"
|
|
internal var DEFAULT_DB_PATH: String
|
|
internal var defaultDB: OpaquePointer? = nil
|
|
|
|
@objc public static let `default` = DatabaseHelper()
|
|
|
|
override private init() {
|
|
let appGroupId = Bundle.main.object(forInfoDictionaryKey: "AppGroupIdentifier") as! String
|
|
let sharedDirectory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupId)!
|
|
let databaseUrl = sharedDirectory.appendingPathComponent("databases/\(DEFAULT_DB_NAME)")
|
|
|
|
DEFAULT_DB_PATH = databaseUrl.path
|
|
}
|
|
|
|
@objc public func getOnlyServerUrlObjc() -> String {
|
|
do {
|
|
return try getOnlyServerUrl()
|
|
} catch {
|
|
print(error)
|
|
return ""
|
|
}
|
|
}
|
|
|
|
public func getOnlyServerUrl() throws -> String {
|
|
if sqlite3_open(DEFAULT_DB_PATH, &defaultDB) != SQLITE_OK {
|
|
throw DatabaseError.OpenFailure(DEFAULT_DB_PATH)
|
|
}
|
|
|
|
defer {
|
|
sqlite3_finalize(queryStatement)
|
|
sqlite3_close(defaultDB)
|
|
}
|
|
|
|
var queryStatement: OpaquePointer?
|
|
let queryString = "SELECT url FROM Servers;"
|
|
if sqlite3_prepare_v2(defaultDB, queryString, -1, &queryStatement, nil) == SQLITE_OK {
|
|
if sqlite3_step(queryStatement) == SQLITE_ROW,
|
|
let result = sqlite3_column_text(queryStatement, 0) {
|
|
return String(cString: result)
|
|
}
|
|
|
|
if sqlite3_step(queryStatement) == SQLITE_ROW {
|
|
// Throw since we have more than one row in the `servers` table
|
|
throw DatabaseError.MultipleServers
|
|
}
|
|
}
|
|
|
|
throw DatabaseError.NoResults(queryString)
|
|
}
|
|
}
|