R/tbl_exists.R

Defines functions tbl_exists.list tbl_exists.tbl_df tbl_exists.data.frame tbl_exists.data.table tbl_exists.SQLiteConnection tbl_exists

Documented in tbl_exists

#' S3 method to check whether provided table exists
#'
#' @param con Database connection obj or R table object.
#' @param schema Database schema.
#' @param tbl Database table.
#' @param ... Currently not used.
#'  
tbl_exists = function(...) {
  UseMethod('tbl_exists')
}

tbl_exists.SQLiteConnection = function(con,
                                       schema = NULL,
                                       tbl = NULL) {
  tbl = paste0(c(schema, tbl), collapse = '.')
  row = try(DBI::dbGetQuery(con, paste0("SELECT * FROM ", tbl, " LIMIT 1")))
  if (is.null(row) || nrow(row) != 1) {
    stop('Table does not exist: ', tbl)
  }
}

tbl_exists.MySQLConnection = tbl_exists.SQLiteConnection
tbl_exists.PqConnection = tbl_exists.SQLiteConnection
tbl_exists.PostgreSQLConnection = tbl_exists.SQLiteConnection

tbl_exists.data.table = function(con,
                                 schema = NULL,
                                 tbl = NULL) {
  if (!data.table::is.data.table(con)) {
    stop('Table does not exist: ', con)
  }
}
tbl_exists.data.frame = function(con,
                                 schema = NULL,
                                 tbl = NULL) {
  if (!is.data.frame(con)) {
    stop('Table does not exist: ', con)
  }
}
tbl_exists.tbl_df = function(con,
                             schema = NULL,
                             tbl = NULL) {
  if (!inherits(con, c('tbl_df', 'tbl'))) {
    stop('Table does not exist: ', con)
  }
}
tbl_exists.tbl = tbl_exists.tbl_df
tbl_exists.list = function(con,
                           schema = NULL,
                           tbl = NULL) {
  stop('The dbreport package does not work on lists.')
}
andschar/dbreport documentation built on Dec. 8, 2022, 3:29 p.m.