87 lines
3.2 KiB
Swift
87 lines
3.2 KiB
Swift
import Foundation
|
|
import SQLite
|
|
|
|
extension WorkoutEvent {
|
|
|
|
private static let table = Table("workout_events")
|
|
|
|
// INTEGER PRIMARY KEY AUTOINCREMENT
|
|
private static let columnRowId = Expression<Int>("ROW_ID")
|
|
|
|
// owner_id INTEGER NOT NULL REFERENCES workouts (data_id) ON DELETE CASCADE
|
|
private static let columnOwnerId = Expression<Int>("owner_id")
|
|
|
|
// date REAL NOT NULL
|
|
private static let columnDate = Expression<Double>("date")
|
|
|
|
// type INTEGER NOT NULL
|
|
private static let columnType = Expression<Int>("type")
|
|
|
|
// duration REAL NOT NULL
|
|
private static let columnDuration = Expression<Double>("duration")
|
|
|
|
// metadata BLOB
|
|
private static let columnMetadata = Expression<Data?>("metadata")
|
|
|
|
// session_uuid BLOB
|
|
private static let columnSessionUUID = Expression<Data?>("session_uuid")
|
|
|
|
// error BLOB
|
|
private static let columnError = Expression<Data?>("error")
|
|
|
|
static func readAll(in database: Connection) throws -> [Self] {
|
|
try database.prepare(table).map(from)
|
|
}
|
|
|
|
static func events(for workoutId: Int, in database: Connection) throws -> [Self] {
|
|
try database.prepare(table.filter(columnOwnerId == workoutId)).map(from)
|
|
}
|
|
|
|
private static func from(row: Row) -> WorkoutEvent {
|
|
.init(
|
|
date: Date(timeIntervalSinceReferenceDate: row[columnDate]),
|
|
type: .init(rawValue: row[columnType])!,
|
|
duration: row[columnDuration],
|
|
metadata: metadata(row[columnMetadata]),
|
|
sessionUUID: row[columnSessionUUID],
|
|
error: row[columnError])
|
|
}
|
|
|
|
private static func metadata(_ data: Data?) -> [String : Any] {
|
|
guard let data else {
|
|
return [:]
|
|
}
|
|
return decode(metadata: data)
|
|
}
|
|
|
|
static func createTable(in database: Database) throws {
|
|
// try database.execute("CREATE TABLE workout_events (ROWID INTEGER PRIMARY KEY AUTOINCREMENT, owner_id INTEGER NOT NULL REFERENCES workouts (data_id) ON DELETE CASCADE, date REAL NOT NULL, type INTEGER NOT NULL, duration REAL NOT NULL, metadata BLOB, session_uuid BLOB, error BLOB)")
|
|
try database.run(table.create { t in
|
|
t.column(columnRowId, primaryKey: .autoincrement)
|
|
t.column(columnOwnerId, references: Table("workouts"), Expression<Int>("data_id"))
|
|
t.column(columnDate)
|
|
t.column(columnType)
|
|
t.column(columnDuration)
|
|
t.column(columnMetadata)
|
|
t.column(columnSessionUUID)
|
|
t.column(columnError)
|
|
})
|
|
}
|
|
|
|
func insert(in database: Database, dataId: Int) throws {
|
|
try WorkoutEvent.insert(self, dataId: dataId, in: database)
|
|
}
|
|
|
|
private static func insert(_ element: WorkoutEvent, dataId: Int, in database: Database) throws {
|
|
try database.run(table.insert(
|
|
columnOwnerId <- dataId,
|
|
columnDate <- element.date.timeIntervalSinceReferenceDate,
|
|
columnType <- element.type.rawValue,
|
|
columnDuration <- element.duration,
|
|
columnMetadata <- encode(metadata: element.metadata),
|
|
columnSessionUUID <- element.sessionUUID,
|
|
columnError <- element.error)
|
|
)
|
|
}
|
|
}
|