Skip to content
Browse files

Database functions have been moved to the sqlutils package.

  • Loading branch information...
1 parent 7cc7f8f commit 5eb6a1090f1578535366aeaf78147ee0cc8def84 @jbryer committed
Showing with 6 additions and 278 deletions.
  1. +2 −1 .Rbuildignore
  2. +3 −2 DESCRIPTION
  3. +0 −8 NAMESPACE
  4. +0 −123 R/dbaccess.R
  5. +1 −5 R/irutils-package.R
  6. +0 −16 man/cacheQuery.Rd
  7. +0 −19 man/execQuery.Rd
  8. +0 −14 man/getParameters.Rd
  9. +0 −14 man/getQueries.Rd
  10. +0 −13 man/getQuery.Rd
  11. +0 −13 man/getQueryDesc.Rd
  12. +0 −13 man/getSQLRepos.Rd
  13. +0 −13 man/setSQLRepos.Rd
  14. +0 −24 vignettes/irutils.Rnw
View
3 .Rbuildignore
@@ -1 +1,2 @@
-^\.Rproj\.user$
+^\.Rproj\.user$
+^.*\.Rproj$
View
5 DESCRIPTION
@@ -13,10 +13,11 @@ Depends:
xtable,
psych,
reshape,
- utils
+ utils,
+ stringr,
+ roxygen2
Collate:
'age.R'
- 'dbaccess.R'
'irutils-package.R'
'saveIPEDStoDB.R'
'LocalRepos.R'
View
8 NAMESPACE
@@ -1,23 +1,15 @@
S3method(xtable,demographics)
export(adjustedTableLaTeX)
-export(cacheQuery)
export(createLocalRepos)
export(demographics)
-export(execQuery)
export(getAge)
export(getAgeGroups)
export(getAgeGroupsIPEDS)
-export(getParameters)
-export(getQueries)
-export(getQuery)
-export(getQueryDesc)
-export(getSQLRepos)
export(local.available.packages)
export(local.install.packages)
export(lsos)
export(pie)
export(saveIPEDStoDB)
-export(setSQLRepos)
export(updateLocalRepos)
export(xtable.demographics)
import(ggplot2)
View
123 R/dbaccess.R
@@ -1,123 +0,0 @@
-#' Returns the current directory containing SQL files.
-#' @author Jason Bryer <jbryer@@excelsior.edu>
-#' @export
-getSQLRepos <- function() {
- pkgEnv <- pos.to.env(match('package:irutils', search()))
- sqlrepos <- get("sqlrepos", envir=pkgEnv)
- if(is.null(sqlrepos)) {
- sqlrepos <- paste(system.file(package='irutils'), '/data', sep='')
- }
- return(sqlrepos)
-}
-
-#' Sets the current directory containing SQL files.
-#' @author Jason Bryer <jbryer@@excelsior.edu>
-#' @export
-setSQLRepos <- function(repos) {
- pkgEnv = pos.to.env(match('package:irutils', search()))
- assign("sqlrepos",
- value=paste(system.file(package='irutils'), '/data', sep=''),
- envir=pkgEnv)
-}
-
-#' Returns a list of available queries in the current repository.
-#' @author Jason Bryer <jbryer@@excelsior.edu>
-#' @export
-getQueries <- function() {
- files = list.files(path=getSQLRepos(), pattern="*.sql")
- return( substr(files, 0, nchar(files)-4) )
-}
-
-#' Executes the specified query and returns a data frame. This function currently
-#' supports RODBC, RSQLite, and RMySQL. For other databases, use getQuery() and
-#' execute the SQL statement using the appropriate database connection.
-#' @author Jason Bryer <jbryer@@excelsior.edu>
-#' @export
-execQuery <- function(query=NULL, connection=NULL, ...) {
- sql = getQuery(query=query, ...)
- df <- NULL
- if(class(connection) == 'RODBC' ) {
- df <- sqlQuery(connection, sql)
- } else if(class(connection) == 'RSQLite') {
- df <- dbSendQuery(connection, sql)
- } else if(class(connection) == 'RMySQL') {
- df <- dbSendQuery(connection, sql)
- } else {
- stop('Unsupported database connection.')
- }
- return(df)
-}
-
-#' Returns the query as a string.
-#' @author Jason Bryer <jbryer@@excelsior.edu>
-#' @export
-getQuery <- function(query=NULL, ...) {
- sql = scan(paste(getSQLRepos(), "/", query, ".sql", sep=''), what="character",
- sep=';', multi.line=FALSE, comment.char=c("#"), quiet=TRUE, quote=NULL)
- sql = paste(sql, collapse=" ")
- parmvals = unlist(list(...))
- if(length(parmvals)>0) {
- for(i in 1:length(parmvals)) {
- sql = gsub(paste(":", names(parmvals)[i], ":", sep=''), parmvals[i], sql)
- }
- }
- return(sql)
-}
-
-#' Returns the query as a string. For internal use only.
-#' @author Jason Bryer <jbryer@@excelsior.edu>
-#' @export
-getQueryDesc <- function(query=NULL, ...) {
- desc = ''
- sql = scan(paste(getSQLRepos(), "/", query, ".sql", sep=''), what="character",
- sep=';', multi.line=FALSE, comment.char=c(""), quiet=TRUE, quote=NULL)
- for(i in 1:length(sql)) {
- tmp = strsplit(sql[i], '#')
- if(length(tmp[[1]]) > 1) {
- desc = paste(desc, tmp[[1]][length(tmp[[1]])], sep=' ')
- }
- }
- return(desc)
-}
-
-#' This will first look in the given directory for a CSV version of the file, if
-#' it exists, that will be read and returned. Otherwise it will execute the query
-#' and then saves a CSV file.
-#' @export
-cacheQuery <- function(query=NULL, dir=getwd(), filename=NULL, ...) {
- parms = getParameters(query)
- parmvals = unlist(list(...))
- if(is.null(filename)) {
- filename = paste(dir, '/', query, sep='')
- if(length(parms) > 0) {
- for(i in 1:length(parms)) {
- filename = paste(filename, parms[i], parmvals[parms[i]], sep='.')
- }
- }
- filename = paste(filename, 'csv', sep='.')
- }
- message(paste("Cached query file:", filename))
- if(file.exists(filename)) {
- df = read.csv(filename)
- } else {
- df = execQuery(query=query, ...)
- write.csv(df, filename, row.names=FALSE)
- }
- return(df)
-}
-
-
-
-#' Returns the parameters that must be set for the given query.
-#' @author Jason Bryer <jbryer@@excelsior.edu>
-#' @export
-getParameters <- function(query) {
- sql = getQuery(query)
- pos = gregexpr(":", sql)
- results = c()
- for(i in seq(1, length(pos[[1]]), by=2)) {
- results = c(results, (substr(sql, pos[[1]][i]+1, pos[[1]][i+1]-1)) )
- }
- return(unique(results))
-}
-
View
6 R/irutils-package.R
@@ -17,9 +17,5 @@ cranMain <- 'http://cran.r-project.org' #Main CRAN
cranExtra <- 'http://www.stats.ox.ac.uk/pub/RWin' #Windows Binaries for some packages
.onAttach <- function(libname, pkgname) {
- pkgEnv = pos.to.env(match('package:irutils', search()))
- assign("sqlrepos",
- value=paste(system.file(package='irutils'), '/data', sep=''),
- envir=pkgEnv)
- #sqlrepos <<- NULL
+ pkgEnv = pos.to.env(match('package:irutils', search()))
}
View
16 man/cacheQuery.Rd
@@ -1,16 +0,0 @@
-\name{cacheQuery}
-\alias{cacheQuery}
-\title{This will first look in the given directory for a CSV version of the file, if
-it exists, that will be read and returned. Otherwise it will execute the query
-and then saves a CSV file.}
-\usage{
- cacheQuery(query = NULL, dir = getwd(), filename = NULL,
- ...)
-}
-\description{
- This will first look in the given directory for a CSV
- version of the file, if it exists, that will be read and
- returned. Otherwise it will execute the query and then
- saves a CSV file.
-}
-
View
19 man/execQuery.Rd
@@ -1,19 +0,0 @@
-\name{execQuery}
-\alias{execQuery}
-\title{Executes the specified query and returns a data frame. This function currently
-supports RODBC, RSQLite, and RMySQL. For other databases, use getQuery() and
-execute the SQL statement using the appropriate database connection.}
-\usage{
- execQuery(query = NULL, connection = NULL, ...)
-}
-\description{
- Executes the specified query and returns a data frame.
- This function currently supports RODBC, RSQLite, and
- RMySQL. For other databases, use getQuery() and execute
- the SQL statement using the appropriate database
- connection.
-}
-\author{
- Jason Bryer <jbryer@excelsior.edu>
-}
-
View
14 man/getParameters.Rd
@@ -1,14 +0,0 @@
-\name{getParameters}
-\alias{getParameters}
-\title{Returns the parameters that must be set for the given query.}
-\usage{
- getParameters(query)
-}
-\description{
- Returns the parameters that must be set for the given
- query.
-}
-\author{
- Jason Bryer <jbryer@excelsior.edu>
-}
-
View
14 man/getQueries.Rd
@@ -1,14 +0,0 @@
-\name{getQueries}
-\alias{getQueries}
-\title{Returns a list of available queries in the current repository.}
-\usage{
- getQueries()
-}
-\description{
- Returns a list of available queries in the current
- repository.
-}
-\author{
- Jason Bryer <jbryer@excelsior.edu>
-}
-
View
13 man/getQuery.Rd
@@ -1,13 +0,0 @@
-\name{getQuery}
-\alias{getQuery}
-\title{Returns the query as a string.}
-\usage{
- getQuery(query = NULL, ...)
-}
-\description{
- Returns the query as a string.
-}
-\author{
- Jason Bryer <jbryer@excelsior.edu>
-}
-
View
13 man/getQueryDesc.Rd
@@ -1,13 +0,0 @@
-\name{getQueryDesc}
-\alias{getQueryDesc}
-\title{Returns the query as a string. For internal use only.}
-\usage{
- getQueryDesc(query = NULL, ...)
-}
-\description{
- Returns the query as a string. For internal use only.
-}
-\author{
- Jason Bryer <jbryer@excelsior.edu>
-}
-
View
13 man/getSQLRepos.Rd
@@ -1,13 +0,0 @@
-\name{getSQLRepos}
-\alias{getSQLRepos}
-\title{Returns the current directory containing SQL files.}
-\usage{
- getSQLRepos()
-}
-\description{
- Returns the current directory containing SQL files.
-}
-\author{
- Jason Bryer <jbryer@excelsior.edu>
-}
-
View
13 man/setSQLRepos.Rd
@@ -1,13 +0,0 @@
-\name{setSQLRepos}
-\alias{setSQLRepos}
-\title{Sets the current directory containing SQL files.}
-\usage{
- setSQLRepos(repos)
-}
-\description{
- Sets the current directory containing SQL files.
-}
-\author{
- Jason Bryer <jbryer@excelsior.edu>
-}
-
View
24 vignettes/irutils.Rnw
@@ -63,30 +63,6 @@ ls('package:irutils')
-\section{Database Access}
-
-For many Institutional Research offices the institutions student information system (SIS) is the most common source of data. Since virtually all SIS systems are backed by a database, extracting data requires extracting data using queries. Typically the language used to extract data is called structured query language (SQL) regardless if the database is provided by Oracle, Microsoft, or an open source options such as MySQL and PostgreSQL. There are a number of functions in this package that will faciliate extracting data from these databases directly into R.
-
-The database access functions provide an interface to a directory of SQL scripts. SQL scripts are simply a plain text file containing the query. The directory containing these files can be determined or set using the \texttt{getSQLRepos} and \texttt{setSQLRepos} functions, repsectively.
-
-<<getSQLRepos,eval=TRUE>>=
-getSQLRepos()
-@
-
-By convention, all SQL files must use a \texttt{.sql} file extension. The \texttt{getQueries} function will return a list of all the queries available in the current reposistory.
-
-<<getQueries,eval=TRUE>>=
-getQueries()
-@
-
-The \texttt{getQueryDesc} and \texttt{getParameters} functions will provide some details about the query in question. In particular, the latter will return the parameters that are required for the query to execute.
-
-<<eval=TRUE>>=
-getQueryDesc('TestQuery')
-getParameters('TestQuery')
-@
-
-There are two functions available for executing the query. The \texttt{execQuery} will execute the query and return a data frame. The \texttt{cacheQuery} however, will first look in the specified directory (by default the \texttt{dir} parameter is set to \texttt{getwd()}) for a CSV file that matches the currently request query. That is, the file name (which is returned when this function is executed) is built using a combination of the query name and parameters to uniquely identify it. This is useful when using Sweave and \LaTeX for document preparation where the function may be executed multiple times but the data does not change. It is considerably faster to read data from a flat file then it is to query the database each time.
\subsection{Creating Your Own Query}

0 comments on commit 5eb6a10

Please sign in to comment.
Something went wrong with that request. Please try again.