R/ojo_query.R

Defines functions ojo_query

Documented in ojo_query

#' @title OJO Query
#'
#' @description Query the Open Justice Oklahoma database
#'
#' @param ... Arguments to pass to glue::glue_sql
#' @param .con The ojodb connection to use
#' @param query The query to send to ojodb
#'
#' @export ojo_query
#' @returns data, a lazy tibble containing the results of the query
#' @examples
#' \dontrun{
#' ojo_query("SELECT * FROM \"case\" LIMIT 10")
#' ojo_query("SELECT * FROM iic.inmate LIMIT 10")
#' }
#'
ojo_query <- function(query, ..., .con = NULL) {
  if (is.null(.con)) {
    .con <- ojo_connect(...)
  }

  if (!inherits(.con, "PqConnection")) {
    rlang::abort("Direct SQL querying is currently only supported for Postgres backends. Make sure your connection is using `ojo_connect(.driver = 'RPostgres')`")
  }

  dplyr::tbl(.con, sql(query))
}
openjusticeok/ojodb documentation built on April 13, 2025, 7:32 a.m.