
## ---------------------------------------------------------------- ##
##                                                                  ##
##             THIS PART TO BE RUN IN  RSTUDIO                      ##
##                                                                  ##
## ---------------------------------------------------------------- ##
##       This executes the Query and saves the results in QRY       ##
##                                                                  ##
##                                                                  ##
## ---------------------------------------------------------------- ##


## -------------------------------- ##
##         QUERY PARAMETERS         ##
## -------------------------------- ##
  qry_timestamp <- timeStamp(frmt="%m%d_%H")

  TBL <- "staging_raw_spotify"   

  colsToPull <- c("customerid", "trackid", "download_date", "mobile", "isrc", "upc", "artistname",
                  "trackname", "albumname", "zip", "product", "country", "gender", "birthyear")

  colsToPull <- c("customerid", "trackid", "download_date", "mobile", "isrc", "upc", "artistname",
                  "trackname", "albumname", "product", "country")

  date_col <- "processedDayTime" # "download_date" 
  date_col <- "download_date" 
  upc_col  <- if(grepl("itunes", TBL, ignore.case=TRUE)) "releaseId" else "upc"


  whereIn <- list(   upc = removeNA(DT.flat[, upc])
                  # , name = c("ex1", "ex2")
                 )
  names(whereIn)[names(whereIn) == "upc"] <- upc_col


  dateMin <- min(DT.flat[!is.na(get(upc_col)), startDate]) - 400
  dateMax <- NULL

  schema <- NULL # "production"
## ------------------------------ ##


## ------------------------------ ##
##        QUERY EXECUTION         ##
## ------------------------------ ##
  if (!exists("QRY"))
    QRY <- list()
  if (!exists("QRES"))
    QRES <- list()
 
  #### Queries will be saved in a list. 
  ##     QRY holds the string sent as the SQL Query
  ##    QRES holds the results that were returned by the query
  ##    q.nm is the list names that correspons to both of these
  ## 
  q.nm <- paste0(TBL, "_UPC", if(length(schema)) paste0(schema, "_"), qry_timestamp)
  # Construct the query
  QRY[[q.nm]] <- makeQry(TBL, colsToPull=colsToPull, colsToAgg=NULL, whereIn=whereIn, dateCol=date_col, dateMin='2013-05-15', dateMax='2013-10-31', limit=NULL)

  ## Something is wrong with makeQry -- this is a temp fix
  QRY[[q.nm]]  <- gsub("dateMin\\s*=", paste0(date_col, " >= "), QRY[[q.nm]])
  QRY[[q.nm]]  <- gsub("dateMax\\s*=", paste0(date_col, " <= "), QRY[[q.nm]])

  # Submit the qruery and store the results
  QRES[[q.nm]] <- runQry(QRY[[q.nm]], verbose=TRUE)
## ------------------------------ ##

jesusForData(QRES, QRY, q.nm, info="PulledToday with dMax of Oct 2013.")

