R/check_id.R

Defines functions check_id

Documented in check_id

#' Test if an id exists in a given field of the table
#' @inheritParams odbc_get_id
#' @inheritParams check_dbtable_variable
#' @param value the id value
#' @export
#' @importFrom assertthat assert_that is.count is.string
#' @importFrom DBI dbGetQuery
check_id <- function(value, variable, table, channel) {
  assert_that(is.count(value))
  assert_that(is.string(variable), noNA(variable))
  check_dbtable_variable(table = table, variable = variable, channel = channel)

  # nocov start
  sql <- paste("SELECT", variable, "FROM", table, "WHERE", variable, "=", value)
  selection <- dbGetQuery(
    conn = channel,
    statement = sql
  )
  if (nrow(selection) == 0) {
    return(FALSE)
  }
  if (nrow(selection) > 1) {
    warning(
      "The id '", value, "' in variable '", variable, "' of table '", table,
      "' exists in multiple rows"
    )
  }
  return(TRUE)
  # nocov end
}
inbo/n2khelper documentation built on March 26, 2022, 1:51 p.m.