Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable custom query running on FhirEngine #1

Closed
wants to merge 10 commits into from
7 changes: 7 additions & 0 deletions engine/src/main/java/com/google/android/fhir/FhirEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.android.fhir

import com.google.android.fhir.db.ResourceNotFoundException
import com.google.android.fhir.search.Search
import com.google.android.fhir.search.SearchQuery
import com.google.android.fhir.sync.ConflictResolver
import java.time.OffsetDateTime
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -47,6 +48,12 @@ interface FhirEngine {
*/
suspend fun <R : Resource> search(search: Search): List<SearchResult<R>>


/**
* Searches the database and returns a list resources according to the [search] specifications.
*/
suspend fun <R : Resource> search(searchQuery: SearchQuery): List<R>

/**
* Synchronizes the [upload] result in the database. [upload] operation may result in multiple
* calls to the server to upload the data. Result of each call will be emitted by [upload] and the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.google.android.fhir.SearchResult
import com.google.android.fhir.db.Database
import com.google.android.fhir.logicalId
import com.google.android.fhir.search.Search
import com.google.android.fhir.search.SearchQuery
import com.google.android.fhir.search.count
import com.google.android.fhir.search.execute
import com.google.android.fhir.sync.ConflictResolver
Expand Down Expand Up @@ -58,6 +59,33 @@ internal class FhirEngineImpl(private val database: Database, private val contex
override suspend fun <R : Resource> search(search: Search): List<SearchResult<R>> {
return search.execute(database)
}
/*
suspend fun sample(dateFrom: Long, limit: Long) {

val searchQuery =
SearchQuery(
"""
SELECT a.serializedResource, b.index_to
FROM ResourceEntity a
LEFT JOIN DateTimeIndexEntity b
ON a.resourceType = b.resourceType AND a.resourceId = b.resourceId AND b.index_name = '_lastUpdated'
WHERE a.resourceType = 'Patient'
AND a.resourceId IN (
SELECT resourceId FROM DateTimeIndexEntity
WHERE resourceType = 'Patient' AND index_name = '_lastUpdated' AND index_to > ?
)
ORDER BY b.index_from ASC
LIMIT ?
""".trimIndent(),
listOf(dateFrom, limit)
)

val patients = search<Patient>(searchQuery)
}*/

override suspend fun <R : Resource> search(searchQuery: SearchQuery): List<R> {
return database.search(searchQuery)
}

override suspend fun count(search: Search): Long {
return search.count(database)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.google.android.fhir.LocalChange
import com.google.android.fhir.LocalChangeToken
import com.google.android.fhir.SearchResult
import com.google.android.fhir.search.Search
import com.google.android.fhir.search.SearchQuery
import com.google.android.fhir.sync.BundleDownloadRequest
import com.google.android.fhir.sync.BundleUploadRequest
import com.google.android.fhir.sync.ConflictResolver
Expand Down Expand Up @@ -146,6 +147,10 @@ object TestFhirEngineImpl : FhirEngine {
return emptyList()
}

override suspend fun <R : Resource> search(searchQuery: SearchQuery): List<R> {
TODO("Not yet implemented")
}

override suspend fun syncUpload(
upload: suspend (List<LocalChange>) -> Flow<Pair<LocalChangeToken, Resource>>
) {
Expand Down
Loading