Skip to content

Commit

Permalink
Added 'from' and 'to' options to getSymbols.MySQL for subsetting by d…
Browse files Browse the repository at this point in the history
…ate in database call. Added backticks to getSymbols.MySQL SELECT statement, fixes error retrieving symbols with same name as SQL operators.
  • Loading branch information
timmarch committed Jul 19, 2016
1 parent 3f56737 commit d6f36e1
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ function(Symbols,env,return.class='xts',
db.fields=c('date','o','h','l','c','v','a'),
field.names = NULL,
user=NULL,password=NULL,dbname=NULL,host='localhost',port=3306,
...) {
from=NULL, to=NULL, ...) {
importDefaults("getSymbols.MySQL")
this.env <- environment()
for(var in names(list(...))) {
Expand Down Expand Up @@ -631,7 +631,13 @@ function(Symbols,env,return.class='xts',
if(verbose) {
cat(paste('Loading ',Symbols[[i]],paste(rep('.',10-nchar(Symbols[[i]])),collapse=''),sep=''))
}
query <- paste("SELECT ",paste(db.fields,collapse=',')," FROM ",Symbols[[i]]," ORDER BY date")

if (!is.null(from) & !is.null(to)) {
query <- paste("SELECT ", paste(db.fields, collapse = ","), " FROM ", paste("`", Symbols[[i]], "`", sep = ""), " WHERE date >= '", from, "' AND date <= '", to, "' ORDER BY date")
} else {
query <- paste("SELECT ", paste(db.fields, collapse = ","), " FROM ", paste("`", Symbols[[i]], "`", sep = ""), " WHERE date ORDER BY date")
}

rs <- DBI::dbSendQuery(con, query)
fr <- DBI::fetch(rs, n=-1)
#fr <- data.frame(fr[,-1],row.names=fr[,1])
Expand Down

0 comments on commit d6f36e1

Please sign in to comment.