R/upload_table.R

Defines functions upload_table

Documented in upload_table

#' Copy a small local tibble/data.frame into a remote table.
#'
#' This is a convenience wrapper for the DBI::dbWriteTable function.
#'
#' __Known issues:__ varchar fields are set to a length of 255 characters.
#' Therefore, text fields with more than that must be truncated before calling this function.
#'
#' @param .conn The connection object (needed to drop the table before 'compute' is called)
#' @param name The name of the desired table. Can use dplyr::in_schema() to specify a table in a different schema than the one that was defined
#' in the connection.  Note: will be dropped and recreated if a table already exists.
#' @param data Incoming tibble/data.frame
#' @param ... additional parameters passed through to DBI::dbWriteTable(), ie. field.types, overwrite = TRUE, temporary = TRUE, append = TRUE.
#'
#'
#' @importFrom DBI dbWriteTable
#'
#'
#' @export
#'
#' @examples
#'
#' \dontrun{
#' conn <- connect_cdw()
#'
#' demo <- mtcars
#'
#' upload_table(demo, name = dplyr::in_schema("PHC_DB_DEV.REF", "DELETEME"), demo, overwrite = TRUE)
#'
#' disconnect_cdw(conn)
#'
#' }



upload_table <- function(.conn, name, data, ...) {

    # drop table if it exists
    #DBI::dbSendStatement(conn, dbplyr::build_sql("DROP TABLE IF EXISTS ", name, con = .conn))
    #DBI::dbClearResult(.conn)
    #dplyr::copy_to(conn, data, name = name, overwrite = overwrite, temporary = temporary)
    DBI::dbWriteTable(conn = .conn,
                      name = name, #Id(schema = "STG_CATHPCI", table = "DEMOGRAPHICS"),
                      value = data,
                      ...)

}
RollieParrish/ccdm documentation built on Dec. 31, 2020, 4:26 p.m.