  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  DB_Utils_Setters_and_Getters.r                                                         #
  #           Last Updated Funclist  :  06 Apr 2015,  3:01 PM (Monday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   .clusterIP           ( cluster=getOption("db.defaultcluster", 7), clusters_using_vpc=1:20 )                              #
  #   matchDBdriver        ( drv.str )                                                                                         #
  #   manualDBsetting      ( host=NULL, user=NULL, password=NULL, port=NULL, dbname=NULL, verbose=FALSE )                      #
  #   setDBsettings        ( drv.str=getDBdriver(), cluster=getCluster(), dont.set.cluster=FALSE                               #
  #                          , user=getOptionOrFail("db.defaultuser"), pw=getOptionOrFail("db.defaultpw")                      #
  #                          , verbose=FALSE )                                                                                 #
  #   getDBsettings        ( cluster=getCluster() )                                                                            #
  #   showDBsettings       ( cluster=getOption("DB.cluster"), verbose=TRUE )                                                   #
  #   setDBall             ( drv.str=NULL, cluster=NULL, ... )                                                                 #
  #   getCluster           (  )                                                                                                #
  #   matchCluster         ( cluster, default=getOption("db.defaultcluster", 7) )                                              #
  #   setCluster           ( cluster=NULL, dont.set.db.driver=FALSE, verbose=FALSE, showWarnings=TRUE, setToNull=FALSE )       #
  #   matchDriver          ( drv.str, showWarnings=TRUE, fail.on.no.match=FALSE )                                              #
  #   setDBdriver          ( drv.str=c("PostgreSQL", "MySQL"), cluster=getCluster(), ... )                                     #
  #   setDBdriverByCluster ( cluster=getCluster(), setClusterToo=TRUE, drv.str="PostgreSQL" )                                  #
  #   getDBdriver          ( default=NULL )                                                                                    #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

# DB_Utils_Setters_and_Getters.r

## NOTE TO SELF: 
##   In almost all cases, the ellipsis should run through to setDBsettings()

## -------------------------------------------------------------- ##
##      Addresses for each cluster                                ## 
## new IP endpoint uses '-vpc' for 09
.clusterIP <- function(cluster=getOption("db.defaultcluster", 7), clusters_using_vpc=1:20, seven_is_okay="NO") {

  ## added 2015-10-19 Do not use 07
  if (cluster == 7 && (seven_is_okay != "Yes")) {
    warning("You should no longer use cluster 07 anymore as of 2015-10-07.   Use 04 or 10.\nUsing   cluster <- 04")
    cluster = 4
  }

  # sprintf("orcd-dw-prod-%02i%s.ccvpswrgleoi.us-east-1.redshift.amazonaws.com", cluster, ifelse(cluster %in% clusters_using_vpc, "-vpc", ""))
    sprintf("db%02i.redshift.theorchard.com", cluster)
}

.Stwek <- "Stw&eK@HkRvtxo^qAjuy#Lv43M7N&&Qs@uYJ!ns%"

## SNOWFLAKE OPTIONS
options("snowflake_uid" = "rsaporta")
options("snowflake_pwd" = "drZ7CiQ@OoFoXSM&10gxZ80&23*VrVSL06XHTY2V")
options("snowflake_defaultwh" = "LOOKER_WH_LARGE")
options("snowflake_driver" = "SnowflakeDSII")
options("snowflake_inuse" = TRUE)
options("snowflake_dbname" = "prod")
options("snowflake_schema" = "public")


## NO LONGER DEFAULTING TO FALSE, for running on a mac
options("db.all.pfm" = getOption("db.all.pfm", default=TRUE))


## DEFAULT OPTIONS
options("db.defaultcluster.in"=4)
options("db.defaultcluster.out"=4)
options("db.defaultcluster"=4)
options("db.defaultuser"="rsaporta")
options("db.defaultpw"=.Stwek)
options("db.mysql_alternates" = c("MySQL", "Statements", "RMySQL", "reportsAR", "reportsDD"))


## DEFAULT HOSTS
options("db.defaulthosts"=list(     
                                    MySQL = "10.10.20.77"
                             ,  reportsDD = "reportsdd.theorchard.com"
                             , PostgreSQL = .clusterIP()
                             , Statements = "192.168.31.95"
                             , snowflake  = "orchard.snowflakecomputing.com"
                           )) ## MySQL="10.156.226.213",  PostgreSQL="10.156.228.102"
## DEFAULT DRIVERS FOR HOST
options("db.defaultdrivers"=list(   
                                    MySQL = "MySQL" # "RMySQL"
                             ,  reportsDD = "MySQL" # "RMySQL"
                             , PostgreSQL = "PostgreSQL" # "RPostgreSQL"
                             , Statements = "MySQL" # "RMySQL"
                             , snowflake  = "RODBC"
                           ))

if (!identical(names(getOption("db.defaulthosts")), names(getOption("db.defaultdrivers"))))
  warning("Rick, Internal Error:  Check DB_Utils_Setters_and_Getters.r\nThe names of getOption(db.defaulthosts) and getOption(db.defaultdrivers) must match\n", pasteR(90))

## ------------------ SNOWFLAKE -------------------------- ##
##     
##     WAS HERE, but moved to SF_Utils.R
##         getSnowflakeDB
##         setSnowflakeDB
##         useSnowflake
##     
## ------------------ SNOWFLAKE -------------------------- ##


## SETS ALL 
setDBall <- function(drv.str=NULL, cluster=NULL, refresh_force=FALSE, ...) {

  ## Turn off Snowflake, straight away
  options(snowflake_inuse=FALSE)

  # if (identical(tolower(as.character(substitute(drv.str))), "snowflake"))
  #   drv.str <- "snowflake"

  if (is.numeric(drv.str) && missing(cluster)) {
    cluster <- drv.str
    drv.str <- NULL
  }

  # if (is.character(drv.str) && substr(drv.str, 1, 2) == "ac") {
  #   cluster <- drv.str
  #   drv.str <- NULL
  # }

  ## Set the cluster only for PostgreSQL
  if (!missing(cluster) || identical("PostgreSQL", matchDriver(drv.str, showWarnings=FALSE, fail.on.no.match=FALSE)))
    setCluster(cluster=cluster, showWarnings=FALSE)

  if (is.null(drv.str))
    setDBdriverByCluster(refresh_force=refresh_force)
  else 
    setDBdriver(drv.str, refresh_force=refresh_force)

  ## This is a temporary work-around until I have separated host from driver
  if (!is.null(drv.str) && tolower(drv.str) %in% c("statements", "statement"))
    return(setDBsettings(drv.str="Statements", ...))
  else
    return(setDBsettings(...))
}

## CLUSTER - which set of computers to access
getCluster <- function() {
   getOption("DB.cluster", default=NULL)
}

matchCluster <- function(cluster, default=getOption("db.defaultcluster", 7)) {
## Matches the input text with some predefined options, and retuns a cleaned match text
## NULL values result to default

  ## NA's should not occur
  if (!is.null(cluster) && is.na(cluster))
    warning ("Unexpected NA in matchCluster(). Will set to default value.")

  ## NULLs, BLANKS, NAs get set to default
  if (is.null(cluster) ||  is.na(cluster) || identical(cluster, ""))
    return(default)

  # no longer used -  ## EVERYTHING ELSE, MATCH UP
  # no longer used -  if (tolower(cluster) %in% c("anal", "analytics", "annalytics"))
  # no longer used -      cluster <- "analytics"
  # no longer used -
  # no longer used -  ## Accounting should be set to MYSQL
  # no longer used -  if (tolower(cluster) %in% c("ac", "acc", "account", "accountting", "accounting", "accoun")) {
  # no longer used -      cluster <- "accounting"
  # no longer used -      # setDBdriver("MySQL")
  # no longer used -    }

  return(cluster)
}

setCluster <- function(cluster=NULL, dont.set.db.driver=FALSE, verbose=FALSE, showWarnings=TRUE, setToNull=FALSE) {
  if (missing(cluster) && showWarnings)
    warning("No value set for 'cluster=' and thus defaulting to NULL.\nThe previous cluster was set to \n\t ", getOption("DB.cluster", "NULL"),"\n" )

  options(snowflake_inuse=FALSE)

  ## Allow for non-numeric values such as "analytics" or "sales"
  ##  if cluster is a number, will simply return that number
  if (setToNull)
    cluster <- NULL
  else 
    cluster <- matchCluster(cluster=cluster)

  if (verbose) {
      showDBsettings(cluster=cluster)
  }


  ## CLOSE OUT & REMOVE ANY EXISITING CONNECTIONS
  try(dbDisconnectAll(drv=NULL))

  ## As an extra cleanup step, remove any "con" or "connex" variables from our workspace
  ## ---------------------------
      ## Look for these variables
      cons <- c("connex", "con")
      ## in these environments
      envirs <- unique(list(globalenv(), parent.frame()))

      for (cc in cons){
       for (ee in envirs)
         if (exists(cc, envir=ee) && inherits(get(cc, envir=ee), "DBIObject"))
           rm(list=cc, envir=ee)
      }
  ## ---------------------------

  options("DB.cluster" = cluster)

  if (!dont.set.db.driver)
    setDBsettings(cluster=cluster, dont.set.cluster=TRUE)
}

## old as of Jan 2016 ##         matchDBdriver <- function(drv.str, ..., DEPRECATED = "use  matchDriver()   ") {
## old as of Jan 2016 ##           message("matchDBdriver has been depcreated -- use matchDriver() instead")
## old as of Jan 2016 ##           matchDriver(drv.str=drv.str, ...)
## old as of Jan 2016 ##         
## old as of Jan 2016 ##         #  if (!is.character(drv.str)) {
## old as of Jan 2016 ##         #    warning ("drv.str sent to matchDBdriver was not a string")
## old as of Jan 2016 ##         #    return(drv.str)
## old as of Jan 2016 ##         #  }
## old as of Jan 2016 ##         #
## old as of Jan 2016 ##         #  nms <- names(getOption("db.defaulthosts"))
## old as of Jan 2016 ##         #  if (is.null(nms))
## old as of Jan 2016 ##         #    stop ("options('db.defaulthosts') should be a named list. It is not.")
## old as of Jan 2016 ##         #  
## old as of Jan 2016 ##         #  matches <- pmatch(toupper(drv.str), toupper(nms))
## old as of Jan 2016 ##         #  if (is.na(matches))
## old as of Jan 2016 ##         #    warning ("No matching driver name (or more than one) found for drv.str='", drv.str, "'")
## old as of Jan 2016 ##         #
## old as of Jan 2016 ##         #  nms[matches]
## old as of Jan 2016 ##         }
## old as of Jan 2016 ##         
## old as of Jan 2016 ##         matchDriver <- function(drv.str, showWarnings=TRUE, fail.on.no.match=FALSE) {
## old as of Jan 2016 ##         ## TODO: Incorporate fail.on.no.match & showWarnings & dont.fail.on.NULL
## old as of Jan 2016 ##         
## old as of Jan 2016 ##           if (!length(drv.str)) {
## old as of Jan 2016 ##             warning ("drv.str has no length, returning as-is")
## old as of Jan 2016 ##             return(drv.str)
## old as of Jan 2016 ##           }
## old as of Jan 2016 ##           if (is.na(drv.str)) {
## old as of Jan 2016 ##             warning ("drv.str is na, returning as is")
## old as of Jan 2016 ##             return(drv.str)
## old as of Jan 2016 ##           }
## old as of Jan 2016 ##           if (!is.character(drv.str)) {
## old as of Jan 2016 ##             warning ("drv.str sent to matchDriver was not a string. Returning as-is")
## old as of Jan 2016 ##             return(drv.str)
## old as of Jan 2016 ##           }
## old as of Jan 2016 ##         
## old as of Jan 2016 ##           possible.drv <- names(getOption("db.defaulthosts"))
## old as of Jan 2016 ##         
## old as of Jan 2016 ##           if (is.null(possible.drv)) {
## old as of Jan 2016 ##             warning ("options('db.defaulthosts') should be a named list. It is not.")
## old as of Jan 2016 ##             possible.drv <- c("PostgreSQL", "MySQL", "snowflake")
## old as of Jan 2016 ##           }
## old as of Jan 2016 ##           
## old as of Jan 2016 ##           ## Check for an exact match of the package name
## old as of Jan 2016 ##           if (length(drv.str) == 1) {
## old as of Jan 2016 ##             if (drv.str %in% paste0("R", possible.drv))
## old as of Jan 2016 ##               return(sub("^R", "", drv.str))
## old as of Jan 2016 ##           }
## old as of Jan 2016 ##         
## old as of Jan 2016 ##           ## ingore capitalization.  This is a sloppy way of going about it.  use extract() instead
## old as of Jan 2016 ##           for (val in possible.drv) {
## old as of Jan 2016 ##             if (identical(tolower(drv.str), tolower(val)))
## old as of Jan 2016 ##               drv.str <- val
## old as of Jan 2016 ##           }
## old as of Jan 2016 ##           
## old as of Jan 2016 ##           ## Specific misspelling of PostgreSQL
## old as of Jan 2016 ##           if (tolower(drv.str) == "postgressql") {
## old as of Jan 2016 ##             warning ("Pssst...  Only one 's' in  'PostgreSQL'")
## old as of Jan 2016 ##             drv.str <- "PostgreSQL"
## old as of Jan 2016 ##           }
## old as of Jan 2016 ##         
## old as of Jan 2016 ##           matches <- pmatch(toupper(drv.str), toupper(possible.drv))
## old as of Jan 2016 ##           if (is.na(matches)) {
## old as of Jan 2016 ##             msg <- paste("No matching driver name (or more than one) found for drv.str='", drv.str, "'")
## old as of Jan 2016 ##             if (fail.on.no.match)
## old as of Jan 2016 ##               stop (msg)
## old as of Jan 2016 ##             if (showWarnings)
## old as of Jan 2016 ##               warning (msg)
## old as of Jan 2016 ##           }
## old as of Jan 2016 ##           return(possible.drv[matches])
## old as of Jan 2016 ##         }

matchDriver <- function(drv.str, return_hostname=FALSE, showWarnings=TRUE, fail.on.no.match=FALSE) {
## TODO: Incorporate fail.on.no.match & showWarnings & dont.fail.on.NULL

  ## ----------------------------------------------------------------------------##
  ## SANITY CHECK 
  ##   If drv.str is not a string of length 1 or is NA, return it as-is
  ## ----------------------------------------------------------------------------##
      if (!length(drv.str)) {
        warning ("drv.str has no length, returning as-is")
        return(drv.str)
      }
      if (is.na(drv.str)) {
        warning ("drv.str is na, returning as-is")
        return(drv.str)
      }
      if (!is.character(drv.str)) {
        warning ("drv.str sent to matchDriver was not a string. Returning as-is")
        return(drv.str)
      }

      ## Notify user about correct spelling
      if ("postgressql" %in% tolower(drv.str)) {
        warning ("Pssst...  Only one 's' in  'PostgreSQL'")
        drv.str["postgressql" %in% tolower(drv.str)] <- "PostgreSQL"
      }

  ## ----------------------------------------------------------------------------##

  possible.drv <- getOption("db.defaultdrivers")

  ## The above options setting should be set; if it is not, throw a warning and return the drv.str as-is
  if (is.null(possible.drv)) {
    warning("INTERNAL ERROR:  options('db.defaultdrivers') was NOT set. (it should be set in the DB_Utils_Setters_and_Getters.r file)\nReturning drv.str as-is  (although a down-stream error is likely since those options are needed elsewhere)")
    return(drv.str)
  }
  ## Not having names set is a problem, but we can continue since we can still match on value. (although down-stream error may occur)
  if (is.null(names(possible.drv)))
    warning("INTERNAL ERROR:  options('db.defaultdrivers') did NOT have names() set")

  ## SAMPLE drv.str for testing
  if (FALSE) {
    drv.str <- c("RMySQL", "Mys", "MySQL", "MySQL", "mysql", "Statements", "RPostgreSQL", "postgresql", "snowflake")
    selfname_(drv.str)
    possible.drv <- c(list(MySQ="cut_off_mysq", MySQL="first_mysql"), possible.drv)
  }

  ## invert possible.drv when user wants the names
  if (return_hostname)
    possible.drv %<>% invDict()

  ## Initiate ret to the value of drv.str
  ret <- drv.str

  ## first match by name, then by value
  nms <- tolower(names(possible.drv))
  matches_on_name <- pmatch(tolower(drv.str), nms, duplicates.ok=TRUE)
  ret[!is.na(matches_on_name)] <- unlist(possible.drv[matches_on_name])

  ## Match by value, then replace
  matches_on_value <- pmatch(tolower(drv.str), tolower(unlist(possible.drv)), duplicates.ok=TRUE)
  ret[!is.na(matches_on_value)] <- unlist(possible.drv[matches_on_value])

  ## Confirm all elements of drv.str have been matched
  confirm_matched <- !(is.na(matches_on_name) & is.na(matches_on_value))
  if (any(!confirm_matched))
    warning(warningCols("some elements of drv.str were not matched, namely: ", drv.str[!confirm_matched]))

  return(ret)
}

## DB DRIVER - which DB flavor to use. ie, MySQL, PostgreSQL
setDBdriver <- function(drv.str=names(getOption("db.defaultdrivers")), cluster=getCluster(), refresh_force=FALSE, ...) {

  drv.str <- matchDriver(drv.str, fail.on.no.match=TRUE, showWarnings=TRUE)

  ## indicate not using snowflake
  if (tolower(drv.str) != "snowflake")
    options("snowflake_inuse" = FALSE)

  ## Reset R Packages
  if (.is_RODBC_loaded())
    .DBlib(drv.str)

  ## Check the current driver
  current.driver <- getDBdriver() ## getOption("db.driver")

  ## Added 2015-10-19.  Somewhere there is a bug where options(db.driver) is not being set appropriately. (probably in setDBsettings())
  ## so doublecheck here
  current.driver_by_settings <- getDBsettings()$drv
  if (is.character(current.driver_by_settings) && current.driver != current.driver_by_settings) {
    warning (sprintf(" 'current.driver' (which is getOption(\"db.driver\")) is \"%s\"\n   'current.driver_by_settings' (which is getDBsettings$drv) is \"%s\"\n\nThis bug might be in setDBsettings() not setting options(db.driver = drv.str)", current.driver, current.driver_by_settings) )
    refresh_force <- TRUE
  }

  ## If the new driver is not identical, then we are changing drivers
  changing.drivers <- refresh_force || !(identical(current.driver, drv.str))

  ## set changing.drivers to FALSE if they are both part of MySQL
  if (all(c(current.driver, drv.str) %in% getOption("db.mysql_alternates")))
    changing.drivers <- FALSE

  ## If we are changing drivers, make sure we have the correct packages installed
  if (changing.drivers) {
    ## remove cluster for MySQL
    if (missing(cluster) && (drv.str == "MySQL" || drv.str == "reportsDD"))
       setCluster(cluster=NULL, setToNull=TRUE)
    .DBlib(drv.str)
  } else if (!require(DBI)) {
    ## Otherwise, just make sure that the packages are loaded
    .DBlib(drv.str)
  }

  ## set the db.driver options.  This is what is grabbed by getDBdriver()
  options("db.driver"=drv.str)

  ## UPDATE 2014-07:  Do NOT setDBsettings() since this will set user/pw back to default
  # ## Also set the DB settings
  # setDBsettings(drv.str=drv.str, cluster=cluster, ...)

  return(invisible(drv.str))
}

setDBdriverByCluster <- function(cluster=getCluster(), setClusterToo=(drv.str=="PostgreSQL"), drv.str="PostgreSQL", refresh_force=FALSE)  {
## TODO:  This function was useful when we were migrating to redshift and some DBs were still on mysql. 
##   Now this is no longer needed.  Move all calls to this function instead to setDBdriver()

  cluster <- matchCluster(cluster=cluster)

  ## Set cluster allows for non-exact spelling
  if (setClusterToo) {
    if (!identical(cluster, getCluster()))
      setCluster(cluster=cluster, dont.set.db.driver=TRUE)
  }

  if (.is_RODBC_loaded())
    .DBlib(drv.str)

  setDBdriver(drv.str=drv.str, refresh_force=refresh_force)

  return(drv.str)
}

getDBdriver <- function(default=NULL)  {
  getOption("db.driver", default=default)
}


manualDBsetting <- function(host=NULL, user=NULL, password=NULL, port=NULL, dbname=NULL, verbose=FALSE) {
  
  settings <- getDBsettings()

  if (!is.null(host))
    settings$host <- host
  if (!is.null(user))
    settings$user <- user
  if (!is.null(password))
    settings$password <- password
  if (!is.null(port))
    settings$port <- port
  if (!is.null(dbname))
    settings$dbname <- dbname

  options("DB.settings"=settings)
  return(invisible(settings))
}

## DB SETTINGS - driver, host, dbname, uid, pw, port
setDBsettings <- function(drv.str=getDBdriver(), cluster=getCluster(), dont.set.cluster=FALSE, user=getOptionOrFail("db.defaultuser"), pw=getOptionOrFail("db.defaultpw"), verbose=FALSE) {

  ## TODO 2015-08-01: Before closing connections, check if we will eventually use the same connection
  ##                  However, if the connection is old, it might be worth closing and re-setting. So go ahead and close it.
  ##                  After-all, some sort of "Set Connection" function is being called.
  try(RODBC::odbcCloseAll(), silent=TRUE)
  try(dbDisconnectAll(), silent=TRUE)

  if (exists("con", envir=globalenv()))
    rm("con", envir=globalenv())

  ## If drv.str is NULL, then try to set the driver by cluster. If still NULL, fail.
  if (is.null(drv.str)) {
    setDBdriverByCluster(cluster)
    drv.str <- getDBdriver()
    ## if it is still NULL, fail
    if (is.null(drv.str))
      stop("drv.str cannot be NULL. Run setDBdriver() first")
  }

  ## Set 
  host.str <- matchDriver(drv.str, return_hostname=TRUE)
  drv.str <- matchDriver(drv.str, return_hostname=FALSE)

  .DBlib(drv.str)

  if (!  (host.str == "snowflake" || identical(tolower(cluster), "snowflake"))    )
    options(snowflake_inuse=FALSE)

  ## Dont set cluster for non PostgreSQL unless specified
  if (host.str != "PostgreSQL" && missing(cluster))
    dont.set.cluster <- TRUE

  ## Set cluster if given explicitly
  if (!missing(cluster) && !dont.set.cluster)
    setCluster(cluster)

  ## DEFAULT
  settings <- list(
        name = host.str
      , drv  = drv.str
      , host = NULL

      ## CURRENTLY SAME USERNAME AND PW FOR ALL DRIVERS, BUT NOT NECESSARY AS SUCH
      , user = user
      , password  = pw

      ## THESE SETTINGS ARE DRIVER DEPENDENT
      , port    = NULL
      , dbname  = NULL
      )

catn("drv.str is ", drv.str)
catn("host.str is ", host.str)
# stop("504")

  ## reportsAR,  MySQL
  if (host.str == "MySQL" || identical(cluster, "accounting")) {
      settings$name     <- host.str    
      settings$drv      <- RMySQL::MySQL()
      settings$host     <- getOption("db.defaulthosts", default=list(MySQL="10.10.20.77"))[["MySQL"]]  ## "10.10.20.77" ## "reportsAR.theorchard.com"
      settings$user     <- user
      settings$password <- ifelse(pw == .Stwek, "0rchrs01", pw)  ## <~~~ This ifelse might be backwards yes/no
      settings$port     <- 3306
      settings$dbname   <- "art_relations"

  ## reportsDD
  } else if (host.str == "reportsDD" || identical(tolower(cluster), "reportsdd")) {
      settings$name     <- host.str    
      settings$drv      <- RMySQL::MySQL()
      settings$host     <- getOption("db.defaulthosts", default=list(MySQL="reportsDD.theorchard.com"))[["reportsDD"]]
      # settings$host     <- getOption("db.defaulthosts", default=list(MySQL="reportsDD.theorchard.com"))[["MySQL"]]
      settings$user     <- user
      settings$password <- ifelse(pw == .Stwek, "j7Cn3hZX", pw)  ## <~~~ This ifelse might be backwards yes/no
      settings$port     <- 3306
      settings$dbname   <- "direct_delivery"

  ## Statements
  } else if (host.str == "Statements" || identical(tolower(cluster), "statements")) {
      settings$name     <- host.str    
      settings$drv      <- RMySQL::MySQL()
      settings$host     <- getOption("db.defaulthosts", default=list(Statements="192.168.31.95"))[["Statements"]]
      settings$user     <- user
      settings$password <- ifelse(pw == .Stwek, "Orchard565656", pw)   ## <~~~ This ifelse might be backwards yes/no
      settings$port     <- 3306
      settings$dbname   <- "stmt-db"

  ## snowflake
  } else if (host.str == "snowflake" || identical(tolower(cluster), "snowflake")) {
      settings$name     <- host.str    
      settings$host     <- getOption("db.defaulthosts", default=list(snowflake="orchard.snowflakecomputing.com"))[["snowflake"]]  ## "10.156.226.213" ## "reportsAR.theorchard.com"
      settings$user     <- user
      settings$password <- ifelse(pw == .Stwek, yes=getOption("snowflake_pwd", default=stop("No snowflake pwd set")), no=pw)   ## <~~~ This ifelse might be backwards yes/no
      settings$port     <- 443
      settings$dbname   <- getSnowflakeDB()
      settings$schema   <- "public"

  ## PostgreSQL
  } else if (host.str == "PostgreSQL") {
      ## make sure cluster is not null, otherwise the sprintf statement below gets messed up
      if (is.null(cluster)) {
          cluster <- getOption("db.defaultcluster", default=c(cluster=4))
          warning (sprintf("cluster is NULL, defaulting to %02i", cluster))
          setCluster(cluster)
      }
      settings$name     <- host.str      
      settings$host     <- .clusterIP(cluster)
      settings$user     <- user
      settings$password <- pw
      settings$port     <- 5439
      settings$dbname   <- "dev"

  ## UNKNWON ==> ERROR
  } else {
    warning ("'", host.str, "' is not a known host string.\nDB.settings are set to NULL")
  }


  options("DB.settings"=settings)
  return(invisible(settings))
}


getDBsettings <- function(cluster=getCluster(), include_name=FALSE) {
  ret <- getOption("DB.settings")
  if (!include_name)
    ret[["name"]] <- NULL
  return(ret)
}

showDBsettings <- function(cluster=getOption("DB.cluster"), verbose=TRUE)  {
  settings <- getDBsettings(cluster=cluster, include_name=TRUE)
  if (is.null(cluster))
    cluster.str <- "NULL"
  else if (!isNumber(cluster))
    cluster.str <- paste0("'", cluster, "'")
  else 
    cluster.str <- as.character(cluster)

  settings.no.pw <- settings
  settings.no.pw[["password"]] <- "******"

  verboseMsg(verbose, paste0(c(sprintf("Cluster is %s and DB.settings being used are: ", cluster.str)  , sprintf("%10s = %s", names(settings), settings.no.pw)), collapse="\n  "), time=FALSE)
  return(invisible(settings))
}



