R/query.r

Defines functions buildQuery query format.logical

Documented in buildQuery format.logical query

#' @title buildQuery
#' @description Builds a URL string with a query built in
#' @details Takes an OrientDB object, builds it's connection string and adds the desired query after URL encoding it
#' @author Jared P. Lander
#' @export buildQuery
#' @rdname buildQuery
#' @param db An OrientDB object created by \code{\link{dbInfo}}
#' @param query The SQL+ query to run
#' @param limit The limit for the number of records; defualt is -1 for all records
#' @return A URL for the OrientDB REST API
#' @examples 
#' db <- dbInfo(host='127.0.0.1', database='GratefulDeadConcerts', 
#'              username='admin', password='admin', port='2480')
#' buildQuery(db, 'SELECT * FROM V')
#' 
buildQuery <- function(db, query, limit=-1)
{
    # make sure it's a database object
    assertthat::assert_that(is.OrientDB(db))
    
    # build full URL
    sprintf('%s/sql/%s/%s', buildDBURL(db=db, type='query'), utils::URLencode(query), limit)
}

#' @title query
#' @description Query an OrientDB database
#' @details Runs the supplied query againast the database
#' @author Jared P. Lander
#' @export query
#' @rdname query
#' @param db An OrientDB Object generated by \code{\link{dbInfo}}
#' @param query The query to be executed
#' @param limit The maximum number of records to be returned.  BY default it is set to -1 to return all records
#' @return A response object holding the results
#' @examples 
#' \dontrun{
#' db <- dbInfo(host='127.0.0.1', database='GratefulDeadConcerts', 
#'              username='admin', password='admin', port='2480')
#' query(db, 'SELECT * FROM V')
#' }
#' 
query <- function(db, query, limit=-1)
{
    # make sure it's a database object
    assertthat::assert_that(is.OrientDB(db))
    
    httr::GET(buildQuery(db, query, limit))
}

#' @title format.logical
#' @description Formating function to handle \code{NA}s
#' @details format fails when given an \code{NA} which is a \code{logical} so this function just returns the \code{NA}
#' @author Jared P. Lander
#' @export format.logical
#' @export
#' @rdname format.logical
#' @param x An object to be formatted
#' @param \dots Further arguments
#' @return Simply retursn \code{x}
#' @examples 
#' format(TRUE)
#' format(FALSE)
#' format(NA)
#' 
#' format(c(TRUE, FALSE, NA))
#' 
format.logical <- function(x, ...)
{
    x
}
jaredlander/OrientExpress documentation built on May 18, 2019, 3:46 p.m.