From b1f45d1abf47a13696fba9670db24fe6ca7fab53 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Thu, 14 Mar 2024 19:20:29 +0100 Subject: [PATCH] Fix workout query --- Sources/HKDatabase/Database/HKDatabaseStore.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/HKDatabase/Database/HKDatabaseStore.swift b/Sources/HKDatabase/Database/HKDatabaseStore.swift index 8581b8c..c8ad55c 100644 --- a/Sources/HKDatabase/Database/HKDatabaseStore.swift +++ b/Sources/HKDatabase/Database/HKDatabaseStore.swift @@ -632,8 +632,11 @@ public final class HKDatabaseStore { let endTime = end.timeIntervalSinceReferenceDate let query = workouts.table - .join(.leftOuter, samples.table, on: workouts.table[workouts.dataId] == samples.table[samples.dataId]) - .filter(samples.table[samples.startDate] <= endTime && + .join(.leftOuter, samples.table, + on: workouts.table[workouts.dataId] == samples.table[samples.dataId]) + .join(.leftOuter, objects.table, + on: samples.table[samples.dataId] == objects.table[objects.dataId]) + .filter(samples.table[samples.startDate] <= endTime && samples.table[samples.endDate] >= startTime) return try database.prepare(query).map(workout) } @@ -651,10 +654,14 @@ public final class HKDatabaseStore { // Select only workouts where the primary activity has the correct type let query = workouts.table - .join(.leftOuter, samples.table, on: workouts.table[workouts.dataId] == samples.table[samples.dataId]) + .join(.leftOuter, samples.table, + on: workouts.table[workouts.dataId] == samples.table[samples.dataId]) .filter(samples.table[samples.startDate] <= endTime && samples.table[samples.endDate] >= startTime) - .join(.leftOuter, workoutActivities.table, on: workouts.table[workouts.dataId] == workoutActivities.table[workoutActivities.ownerId]) + .join(.leftOuter, objects.table, + on: samples.table[samples.dataId] == objects.table[objects.dataId]) + .join(.leftOuter, workoutActivities.table, + on: workouts.table[workouts.dataId] == workoutActivities.table[workoutActivities.ownerId]) .filter(workoutActivities.table[workoutActivities.activityType] == typeId && workoutActivities.table[workoutActivities.isPrimaryActivity] == true) return try database.prepare(query)