R/exists.R

Defines functions docdb_exists.src_duckdb docdb_exists.src_postgres docdb_exists.src_sqlite docdb_exists.src_mongo docdb_exists.src_elastic docdb_exists.src_couchdb docdb_exists

Documented in docdb_exists

#' Check if container exists in database
#'
#' @inheritParams docdb_create
#'
#' @param ... Passed to functions [DBI::dbListTables()],
#' [elastic::index_exists()], and [sofa::db_info()]
#'
#' @return (logical) `TRUE` or `FALSE` to indicate
#'  existence of container \code{key} in database.
#'  Note this does not indicate if the container
#'  holds any documents.
#'
#' @export
#'
#' @examples \dontrun{
#' src <- src_sqlite()
#' docdb_exists(src, "nonexistingcontainer")
#' docdb_create(src, "mtcars", mtcars)
#' docdb_exists(src, "mtcars")
#' }
#'
docdb_exists <- function(src, key, ...) {
  assert(src, "docdb_src")
  assert(key, "character")
  UseMethod("docdb_exists", src)
}

#' @export
docdb_exists.src_couchdb <- function(src, key, ...) {
  tmp <- tryCatch(
    sofa::db_info(src$con, dbname = key, ...),
    error = function(e) e)
  return(!inherits(tmp, "error"))
}

#' @export
docdb_exists.src_elastic <- function(src, key, ...) {
  return(elastic::index_exists(src$con, key, ...))
}

#' @export
docdb_exists.src_mongo <- function(src, key, ...) {
  return(src$con$count() > 0L)
}

#' @export
docdb_exists.src_sqlite <- function(src, key, ...) {
  return(any(key == RSQLite::dbListTables(src$con, ...)))
}

#' @export
docdb_exists.src_postgres <- function(src, key, ...) {
  return(any(key == DBI::dbListTables(src$con, ...)))
}

#' @export
docdb_exists.src_duckdb <- function(src, key, ...) {
  return(any(key == DBI::dbListTables(src$con, ...)))
}

Try the nodbi package in your browser

Any scripts or data that you put into this service are public.

nodbi documentation built on Sept. 30, 2024, 1:06 a.m.