2024-01-25 13:48:00 +01:00

30 lines
1.0 KiB
Swift

import Foundation
import SQLite
enum Metadata {
static func allKeys(in database: Database) throws -> [Int : Key] {
try Key.readAll(in: database)
}
static func createTables(in database: Connection) throws {
try Value.createTable(in: database)
try Key.createTable(in: database)
}
static func metadata(for workoutId: Int, in database: Connection, keyMap: [Int : Key]) throws -> [Key : Value] {
return try Value.metadata(for: workoutId, in: database).reduce(into: [:]) { dict, entry in
guard let key = keyMap[entry.keyId] else {
print("No '\(entry.keyId)' in table 'metadata_keys'")
return
}
dict[key] = entry.value
}
}
static func insert(_ value: Value, for key: Key, of workoutId: Int, in database: Connection) throws {
let keyId = try Metadata.Key.hasKey(key, in: database) ?? Metadata.Key.insert(key: key, in: database)
try Value.insert(value, of: workoutId, for: keyId, in: database)
}
}