Skip to content

Commit

Permalink
Return an empty tibble when the query returns no results
Browse files Browse the repository at this point in the history
This replicates the old behaviour.
  • Loading branch information
Moohan committed Jan 23, 2025
1 parent e4b6bfb commit de0a65a
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions R/get_resource_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ get_resource_sql <- function(sql) {
)
}

# get correct order of columns
order <- purrr::map_chr(
content$result$fields,
~ .x$id
)

# extract the records (rows) from content
data <- purrr::map_dfr(
Expand All @@ -109,19 +104,27 @@ get_resource_sql <- function(sql) {
}
)

# If the query returned no rows, exit now.
if (nrow(data) == 0L) {
return(data)
}

# get correct order of columns
order <- purrr::map_chr(
content$result$fields,
~ .x$id
)
order <- order[!order %in% c("_id", "_full_text")]

# select and reorder columns to reflect
cleaner <- data %>%
dplyr::select(
dplyr::all_of(order),
-dplyr::matches("_id"), -dplyr::matches("_full_text")
)
cleaner <- dplyr::select(data, dplyr::all_of(order))

# warn if limit may have been surpassed
if (nrow(cleaner) == 32000) {
if (nrow(cleaner) == 32000L) {
cli::cli_warn(c(
"Row number limit",
i = "SQL queries are limitted to returning 32,000 results.
This may have effected the results of your query."
This may have affected the results of your query."
))
}

Expand Down

0 comments on commit de0a65a

Please sign in to comment.