Extract workout table
This commit is contained in:
parent
42e95f9ff9
commit
77be6d5989
@ -30,7 +30,7 @@ final class HealthDatabase: ObservableObject {
|
||||
func readAllWorkouts() {
|
||||
let workouts: [Workout]
|
||||
do {
|
||||
workouts = try Workout.readAll(in: database)
|
||||
workouts = try WorkoutTable.readAll(in: database)
|
||||
} catch {
|
||||
print("Failed to read workouts: \(error)")
|
||||
return
|
||||
|
@ -2,7 +2,7 @@ import Foundation
|
||||
import SQLite
|
||||
import HealthKit
|
||||
|
||||
extension Workout {
|
||||
enum WorkoutTable {
|
||||
|
||||
private static let table = Table("workouts")
|
||||
|
||||
@ -46,7 +46,7 @@ extension Workout {
|
||||
}
|
||||
}
|
||||
|
||||
static func createTable(in database: Database) throws {
|
||||
static func create(in database: Database) throws {
|
||||
try database.run(table.create { t in
|
||||
t.column(columnDataId, primaryKey: .autoincrement)
|
||||
t.column(columnTotalDistance)
|
||||
@ -58,11 +58,7 @@ extension Workout {
|
||||
// try database.execute("CREATE TABLE workouts (data_id INTEGER PRIMARY KEY AUTOINCREMENT, total_distance REAL, goal_type INTEGER, goal REAL, condenser_version INTEGER, condenser_date REAL)")
|
||||
}
|
||||
|
||||
func insert(in database: Database) throws {
|
||||
try Workout.insert(self, in: database)
|
||||
}
|
||||
|
||||
private static func insert(_ element: Workout, in database: Database) throws {
|
||||
static func insert(_ element: Workout, in database: Database) throws {
|
||||
let rowid = try database.run(table.insert(
|
||||
columnTotalDistance <- element.totalDistance,
|
||||
columnGoalType <- element.goal?.goalType,
|
||||
|
@ -18,12 +18,12 @@ extension HealthDatabase {
|
||||
private static func makeDatabase() throws -> Connection {
|
||||
let database = try Connection(.inMemory)
|
||||
|
||||
try Workout.createTable(in: database)
|
||||
try WorkoutTable.create(in: database)
|
||||
try HKWorkoutEventTable.create(in: database)
|
||||
try WorkoutActivityTable.create(in: database)
|
||||
try Metadata.createTables(in: database)
|
||||
|
||||
try Workout.mock1.insert(in: database)
|
||||
try WorkoutTable.insert(.mock1, in: database)
|
||||
return database
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user