Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions engine/src/main/java/com/google/android/fhir/search/MoreSearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,33 @@ internal fun Search.getQuery(

var filterStatement = ""
val filterArgs = mutableListOf<Any>()
val filterQuery = getFilterQueries()
filterQuery.forEachIndexed { i, it ->
filterStatement +=
// spotless:off
"""
${if (i == 0) "AND a.resourceUuid IN (" else "a.resourceUuid IN ("}
${it.query}
)
${if (i != filterQuery.lastIndex) "${operation.logicalOperator} " else ""}
""".trimIndent()
// spotless:on
filterArgs.addAll(it.args)
}
val nestedSearchQuery = nestedSearches.nestedQuery(type, operation)
val filterQuery =
getFilterQueries().let {
if (nestedSearchQuery != null) {
it + nestedSearchQuery
} else {
it
}
}
val filterJoinOperator =
when (operation) {
Operation.OR -> "\nUNION\n"
Operation.AND -> "\nINTERSECT\n"
}
filterQuery
.takeIf { it.isNotEmpty() }
?.let {
filterStatement +=
it.joinToString(
separator = filterJoinOperator,
prefix = "AND a.resourceUuid IN (",
postfix = ")\n",
) { fq ->
fq.query
}
filterArgs.addAll(it.flatMap { fq -> fq.args })
}

var limitStatement = ""
val limitArgs = mutableListOf<Any>()
Expand All @@ -390,10 +404,6 @@ internal fun Search.getQuery(
}
}

nestedSearches.nestedQuery(type, operation)?.let {
filterStatement += it.query
filterArgs.addAll(it.args)
}
val whereArgs = mutableListOf<Any>()
val nestedArgs = mutableListOf<Any>()
val query =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,20 @@ internal fun List<NestedSearch>.nestedQuery(
return if (isEmpty()) {
null
} else {
val filterJoinOperator =
when (operation) {
Operation.OR -> "\nUNION\n"
Operation.AND -> "\nINTERSECT\n"
}

map { it.nestedQuery(type) }
.let { searchQueries ->
SearchQuery(
query =
searchQueries.joinToString(
prefix = "AND a.resourceUuid IN ",
separator = " ${operation.logicalOperator} a.resourceUuid IN",
separator = " $filterJoinOperator",
) { searchQuery ->
"(\n${searchQuery.query}\n) "
"\n${searchQuery.query}\n"
},
args = searchQueries.flatMap { it.args },
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1919,8 +1919,7 @@ class SearchTest {
AND a.resourceUuid IN (
SELECT resourceUuid FROM StringIndexEntity
WHERE resourceType = ? AND index_name = ? AND index_value = ?
)
AND a.resourceUuid IN (
INTERSECT
SELECT resourceUuid
FROM ResourceEntity a
WHERE a.resourceType = ? AND a.resourceId IN (
Expand All @@ -1930,8 +1929,7 @@ class SearchTest {
AND a.resourceUuid IN (
SELECT resourceUuid FROM TokenIndexEntity
WHERE resourceType = ? AND index_name = ? AND (index_value = ? AND IFNULL(index_system,'') = ?)
)
AND a.resourceUuid IN (
INTERSECT
SELECT resourceUuid FROM TokenIndexEntity
WHERE resourceType = ? AND index_name = ? AND (index_value = ? AND IFNULL(index_system,'') = ?)
)
Expand Down Expand Up @@ -2001,7 +1999,7 @@ class SearchTest {
WHERE resourceType = ? AND index_name = ? AND (index_value = ? AND IFNULL(index_system,'') = ?)
)
)
) AND a.resourceUuid IN(
INTERSECT
SELECT resourceUuid
FROM ResourceEntity a
WHERE a.resourceType = ? AND a.resourceId IN (
Expand Down Expand Up @@ -2156,8 +2154,7 @@ class SearchTest {
AND a.resourceUuid IN (
SELECT resourceUuid FROM StringIndexEntity
WHERE resourceType = ? AND index_name = ? AND index_value = ?
)
OR a.resourceUuid IN (
UNION
SELECT resourceUuid FROM StringIndexEntity
WHERE resourceType = ? AND index_name = ? AND index_value = ?
)
Expand Down Expand Up @@ -2189,8 +2186,7 @@ class SearchTest {
AND a.resourceUuid IN (
SELECT resourceUuid FROM StringIndexEntity
WHERE resourceType = ? AND index_name = ? AND index_value LIKE ? || '%' COLLATE NOCASE
)
AND a.resourceUuid IN (
INTERSECT
SELECT resourceUuid FROM StringIndexEntity
WHERE resourceType = ? AND index_name = ? AND (index_value LIKE ? || '%' COLLATE NOCASE OR index_value LIKE ? || '%' COLLATE NOCASE)
)
Expand Down
Loading