# 02 QRY PARAMS.r


## ----------------------------------------------- ##
##  PRE-CLEAN                                      ##
## ----------------------------------------------- ##
    try(dbDisconnect(con),           silent=TRUE)
    try(detach(package:RPostgreSQL), silent=TRUE)
    try(detach(package:RMySQL),      silent=TRUE)
## ----------------------------------------------- ##


## -------------------------------- ##
##         QUERY PARAMETERS         ##
## -------------------------------- ##
  TEST.RUN       <-  FALSE # 15

  PRECISE.DATE <- FALSE

  useProduction <- TRUE  # FALSE
  minDate.shiftDown.abs <- 75
  maxDate.shiftUp.abs   <- 120

  # whether or not to include equals
  lte <- "<="
  gte <- ">="

  q.nm.prefx <- "USWeeklyAdds_" # NULL

  schema <- NULL # "production"
  TBL <- "staging_raw_spotify"   
  prependCols <- !(length(TBL) == 1)

  colsToPull <- c("customerid AS userid", "upc", "isrc", "trackid AS trackid"
                , "download_date", "mobile"
                , "artistname AS artist", "trackname"
                , "albumname", "zip AS user_zip", "product"
                , "country AS user_country", "gender AS user_gender"
                , "birthyear AS user_birthyear", "download_datetime")

  # date_col for AWS
  ## TODO:  it so happens that download_date & download_datetime match.  But If not, I need to calculate download_date from download_datetime
  date_col <- "download_date" # "processedDayTime" 
  upc_col  <- "upc" #  if(grepl("itunes", TBL, ignore.case=TRUE)) "releaseId" else "upc"


  ## Using PRECISE.DATE makes the query take longer
  ##       but gives back tighter result (less rows)
  if (PRECISE.DATE)  {
    XLS.dateCol <- "startDate" 
    XLS.byCols  <- c("upc")
    whereIn <- rangeDateBy(SpotifyAdds, dateCol=XLS.dateCol, byCols=XLS.byCols
                          , byCols.dict=dict.XLStoAWS.colNames, na.rm=TRUE)

    whereIn[, dateMin := dateMin - minDate.shiftDown.abs]
    whereIn[, dateMax := dateMax + maxDate.shiftUp.abs]

    dateMin <- TRUE 
    dateMax <- TRUE 
  ## OTHERWISE:  Quicker query, and more rows
  }  else  {
      whereIn <- list(   upc = removeNA(SpotifyAdds[["upc"]])
                  # , name = c("ex1", "ex2")
                 )
      names(whereIn)[names(whereIn) == "upc"] <- upc_col

      # dateMin.SpotifyAdds should have been set in 01.A.  However if not run, may be missing
      if (!exists("dateMin.SpotifyAdds"))
        dateMin.SpotifyAdds <- min(SpotifyAdds[!is.na(get(upc_col))][["startDate"]]) - 100

      dateMin <- dateMin.SpotifyAdds
      dateMax <- NULL
  }

  showWarnings = TRUE
  limit = NULL
## ------------------------------ ##


