R/connect_cdw.R

Defines functions connect_cdw

Documented in connect_cdw

#' Set up  a connection to Snowflake (CDW)
#'
#'
#' @param role a valid role that you can use. Default is PHC_ANALYTICS. Roles typically have default databases and warehouses.
#' @param database a valid database that the specified role can access. Default is PHC_DB_DEV
#' @param schema a valid schema that the specified role can access. Default is WHS_CORE_CDM
#' @param warehouse a valid warehouse that the specified role can access. Default is PHC_ANALYTICS_WH_S.
#'
#' @importFrom odbc odbc
#'
#' @export
#'
#' @examples
#'
#' \dontrun{
#' conn <- connect_cdw()
#'
#' ccdm_tbl(conn)
#'
#' ccdm_tbl(conn, "HOSPITAL_ENCOUNTERS")
#'
#' ccdm_tbl(conn, role = "REGIONAL_ANALYTICS")
#'
#' disconnect_cdw(conn)
#' }



connect_cdw<- function(database = "HI_DB",
                       schema = "WHS_SYNERGY",
                       role = "REGIONAL_ANALYTICS_RO",
                       warehouse = "REGIONAL_ANALYTICS_WH"
                       #config = NULL
                       ) {

    #if(file.exists("~/.config/snowflake.yml")) {
        # placeholder for config settings
        #config <- config::get(config, file = "~/.config/snowflake.yml")
        #database <- config$database
        #schema <- config$schema
        #role <- config$role
        #warehouse <- config$warehouse
    #}


   if(Sys.getenv("sf_user") == "") {
        uid = rstudioapi::askForPassword("Snowflake username (email address)")
        } else {
            uid = Sys.getenv("sf_user")
        }

    if(Sys.getenv("sf_user") == "") {
        pwd = rstudioapi::askForPassword("Password")
        } else {
            pwd = Sys.getenv("sf_pass")
        }

    
    

    # if windows, then use Snowflake DSN

    # if Linux then use standard connection settings


    DBI::dbConnect(drv = odbc::odbc(),
                   UID = uid, #Sys.getenv("sf_user"),
                   PWD = pwd, #Sys.getenv("sf_pass"),
                   Database = database,
                   Schema = schema,
                   Role = role,
                   Tracing = 0,
                   LogLevel = 0,
                   Server = "psjh_prod.west-us-2.azure.snowflakecomputing.com",
                   Warehouse = warehouse,
                   Driver = "SnowflakeDSIIDriver")

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