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