R/pull_data.R

Defines functions pull_data

Documented in pull_data

#' Pull Data from a Connection
#'
#' Gets data from a database connection and returns the result as a tibble
#' @description Gets data from a database connection and returns the result as a tibble. Provides a common interface.
#' @param sql_code SQL query that returns one data table
#' @param conn RODBC, RJDBC or other supported data connection
#' @keywords database
#' @export
#' @examples

pull_data <- function(sql_code, conn){

  stopifnot(class(conn) %in% c("RODBC", "JDBCConnection", "SQLiteConnection", "RSQLite","Microsoft SQL Server"))

  d <- NULL

  if (class(conn) %in% "RODBC")
    d <- tibble::as_tibble(RODBC::sqlQuery(conn, sql_code, as.is = TRUE))
  if (class(conn) %in% c("JDBCConnection","Microsoft SQL Server"))
    d <- tibble::as_tibble(RJDBC::dbGetQuery(conn, sql_code))
  if (class(conn) %in% "SQLiteConnection")
    d <- tibble::as_tibble(DBI::dbGetQuery(conn, sql_code, as.is = TRUE))

  d

}
mattjcamp/dbr documentation built on May 11, 2023, 2:19 a.m.