Skip to content

Commit

Permalink
Refactor filterStatement to include ifBlank
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed Dec 16, 2024
1 parent 525e3fb commit d810a7e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5270,7 +5270,7 @@ class DatabaseImplTest {
first.size == second.size &&
first.asSequence().zip(second.asSequence()).all { (x, y) -> equalsShallow(x, y) }

private fun equalsShallowUnordered(first: List<Resource>, second: List<Resource>) =
private fun resourceTypeAndIdEqualUnordered(first: List<Resource>, second: List<Resource>) =
first.size == second.size &&
first.map { it.resourceType to it.logicalId }.toSet() ==
second.map { it.resourceType to it.logicalId }.toSet()
Expand All @@ -5286,7 +5286,7 @@ class DatabaseImplTest {
if (inOrder) {
equalsShallow(x.value, y.value)
} else {
equalsShallowUnordered(x.value, y.value)
resourceTypeAndIdEqualUnordered(x.value, y.value)
}
}
} else {
Expand All @@ -5305,7 +5305,7 @@ class DatabaseImplTest {
if (inOrder) {
equalsShallow(x.value, y.value)
} else {
equalsShallowUnordered(x.value, y.value)
resourceTypeAndIdEqualUnordered(x.value, y.value)
}
}
} else {
Expand Down
23 changes: 14 additions & 9 deletions engine/src/main/java/com/google/android/fhir/search/MoreSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ internal fun Search.getRevIncludeQuery(includeIds: List<String>): SearchQuery {
ON re.resourceUuid = rie.resourceUuid
${join.query}
WHERE rie.resourceType = ? AND rie.index_name = ? AND rie.index_value IN ($uuidsString)
${if (filterQuery.isNotEmpty()) "AND re.resourceUuid IN ($filterQuery)" else "AND re.resourceType = ?"}
${if (filterQuery.isNotBlank()) "AND re.resourceUuid IN ($filterQuery)" else "AND re.resourceType = ?"}
$order
"""
.trimIndent()
Expand Down Expand Up @@ -215,7 +215,7 @@ internal fun Search.getIncludeQuery(includeIds: List<UUID>): SearchQuery {
ON re.resourceType||"/"||re.resourceId = rie.index_value
${join.query}
WHERE rie.resourceType = ? AND rie.index_name = ? AND rie.resourceUuid IN ($uuidsString)
${if (filterQuery.isNotEmpty()) "AND re.resourceUuid IN ($filterQuery)" else "AND re.resourceType = ?"}
${if (filterQuery.isNotBlank()) "AND re.resourceUuid IN ($filterQuery)" else "AND re.resourceType = ?"}
$order
"""
.trimIndent()
Expand Down Expand Up @@ -388,14 +388,19 @@ internal fun Search.getQuery(
}
}

val nestedQueryFilterStatement = nestedSearches.nestedQuery(type, operation)?.query ?: ""
val nestedQueryFilterArgs = nestedSearches.nestedQuery(type, operation)?.args ?: emptyList()
val nestedFilterQuery = nestedSearches.nestedQuery(type, operation)
val nestedQueryFilterStatement = nestedFilterQuery?.query ?: ""
val nestedQueryFilterArgs = nestedFilterQuery?.args ?: emptyList()

// Combines filter statements derived from filter queries and nested queries, that use the
// resourceUuid field,
// and defaults to filter statement with the resourceType field when blank
val filterStatement =
listOf(filterQueryStatement, nestedQueryFilterStatement)
.filter { it.isNotBlank() }
.joinToString(separator = " AND ")
val filterArgs = filterQueryArgs + nestedQueryFilterArgs
.ifBlank { "a.resourceType = ?" }
val filterArgs = (filterQueryArgs + nestedQueryFilterArgs).ifEmpty { listOf(type.name) }

val whereArgs = mutableListOf<Any>()
val nestedArgs = mutableListOf<Any>()
Expand All @@ -407,7 +412,7 @@ internal fun Search.getQuery(
SELECT COUNT(*)
FROM ResourceEntity a
$sortJoinStatement
WHERE ${filterStatement.ifBlank { "a.resourceType = ?" }}
WHERE $filterStatement
$sortOrderStatement
$limitStatement
"""
Expand All @@ -425,7 +430,7 @@ internal fun Search.getQuery(
SELECT substr(a.index_value, $start)
FROM ReferenceIndexEntity a
$sortJoinStatement
WHERE a.index_name = ? AND ${filterStatement.ifBlank { "a.resourceType = ?" }}
WHERE a.index_name = ? AND $filterStatement
$sortOrderStatement
$limitStatement)
"""
Expand All @@ -437,7 +442,7 @@ internal fun Search.getQuery(
SELECT a.resourceUuid, a.serializedResource
FROM ResourceEntity a
$sortJoinStatement
WHERE ${filterStatement.ifBlank { "a.resourceType = ?" }}
WHERE $filterStatement
$sortOrderStatement
$limitStatement
"""
Expand All @@ -449,7 +454,7 @@ internal fun Search.getQuery(

return SearchQuery(
query,
nestedArgs + sortArgs + whereArgs + filterArgs.ifEmpty { listOf(type.name) } + limitArgs,
nestedArgs + sortArgs + whereArgs + filterArgs + limitArgs,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Google LLC
* Copyright 2022-2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ class SearchTest {
}

@Test
fun practitioner_has_patient_has_condition_diabetes_and_hypertension() {
fun search_has_patient_has_condition_diabetes_and_hypertension() {
val query =
Search(ResourceType.Patient)
.apply {
Expand Down

0 comments on commit d810a7e

Please sign in to comment.