diff --git a/DESCRIPTION b/DESCRIPTION index 24ee5ad..db89ef5 100755 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,12 +1,12 @@ Package: RODBCDBI Type: Package Version: 0.1 -Title: Provides access to databases through the ODBC interface +Title: Provides Access to Databases Through the ODBC Interface Author: teramonagi -Maintainer: teramonagi -Description: RODBCDBI is an implementation of R's DBI interface using ODBC as a +Maintainer: Nagi Teramo +Description: An implementation of R's DBI interface using ODBC package as a back-end. This allows R to connect to any DBMS that has a ODBC driver. -License: BSD +License: MIT + file LICENSE Imports: methods, DBI, diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..f9a93c3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,2 @@ +YEAR: 2015-2015 +COPYRIGHT HOLDER: Nagi Teramo \ No newline at end of file diff --git a/R/ODBCConnection.R b/R/ODBCConnection.R index a9bf213..f52a5da 100644 --- a/R/ODBCConnection.R +++ b/R/ODBCConnection.R @@ -12,6 +12,11 @@ setClass( slots=list(odbc="RODBC") ) +#' Execute a statement on a given database connection. +#' +#' @param conn An existing \code{\linkS4class{ODBCConnection}} +#' @param statement a character vector of length 1 containing SQL +#' @param ... Other parameters passed on to methods #' @export #' @rdname odbc-query setMethod("dbSendQuery", "ODBCConnection", function(conn, statement, ...) { @@ -21,22 +26,27 @@ setMethod("dbSendQuery", "ODBCConnection", function(conn, statement, ...) { new("ODBCResult", connection=conn, sql=statement, state=env) }) +#' Get DBMS metadata. +#' +#' @param dbObj An object inheriting from \code{\linkS4class{ODBCConnection}}, \code{\linkS4class{ODBCDriver}}, or a \code{\linkS4class{ODBCResult}} #' @export #' @rdname ODBCConnection-class -setMethod("dbGetInfo", "ODBCConnection", function(dbObj, ...) {odbcGetInfo(dbObj@odbc)}) +setMethod("dbGetInfo", "ODBCConnection", function(dbObj, ...) {dbObj(dbObj@odbc)}) #' List fields in specified table. #' -#' @param conn An existing \code{\linkS4class{RODBCConnection}} +#' @param conn An existing \code{\linkS4class{ODBCConnection}} #' @param name a length 1 character vector giving the name of a table. #' @export #' @examples +#' \dontrun{ #' library(DBI) #' con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") #' dbWriteTable(con, "iris", iris, overwrite=TRUE) #' dbListFields(con, "iris") #' dbDisconnect(con) +#' } setMethod("dbListFields", c("ODBCConnection", "character"), function(conn, name) { sqlColumns(conn@odbc, name)$COLUMN_NAME }) @@ -53,19 +63,24 @@ setMethod("dbListTables", "ODBCConnection", function(conn){ #' #' @export #' @rdname dbWriteTable -#' @param con,conn a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}} +#' @param conn a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}} #' @param name a character string specifying a table name. ODBCConnection table names #' are \emph{not} case sensitive, e.g., table names \code{ABC} and \code{abc} #' are considered equal. #' @param value a data.frame (or coercible to data.frame) object or a #' file name (character). when \code{value} is a character, it is interpreted as a file name and its contents imported to ODBC. +#' @param overwrite logical. Should data be overwritten? +#' @param append logical. Should data be appended to an existing table? +#' @param ... additional arguments passed to the generic. #' @export #' @examples +#' \dontrun{ #' library(DBI) #' con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") #' dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE) #' dbReadTable(con, "mtcars") #' dbDisconnect(con) +#' } setMethod("dbWriteTable", c("ODBCConnection", "character", "data.frame"), function(conn, name, value, overwrite=FALSE, append=FALSE, ...) { sqlSave(conn@odbc, dat=value, tablename=name, safer=!overwrite, append=append, ...) invisible(TRUE) @@ -73,7 +88,7 @@ setMethod("dbWriteTable", c("ODBCConnection", "character", "data.frame"), functi #' Does the table exist? #' -#' @param conn An existing \code{\linkS4class{SQLiteConnection}} +#' @param conn An existing \code{\linkS4class{ODBCConnection}} #' @param name String, name of table. Match is case insensitive. #' @export setMethod("dbExistsTable", c("ODBCConnection", "character"), function(conn, name) { @@ -107,6 +122,7 @@ setMethod("dbRemoveTable", c("ODBCConnection", "character"), function(conn, name #' #' @param conn a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}} #' @param name a character string specifying a table name. +#' @param row.names a character string specifying a table name. #' @param check.names If \code{TRUE}, the default, column names will be converted to valid R identifiers. #' @param select.cols A SQL statement (in the form of a character vector of #' length 1) giving the columns to select. E.g. "*" selects all columns, @@ -114,6 +130,7 @@ setMethod("dbRemoveTable", c("ODBCConnection", "character"), function(conn, name #' @inheritParams DBI::rownamesToColumn #' @export #' @examples +#' \dontrun{ #' library(DBI) #' con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") #' dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE) @@ -125,6 +142,7 @@ setMethod("dbRemoveTable", c("ODBCConnection", "character"), function(conn, name #' dbGetQuery(con, "SELECT * FROM mtcars WHERE cyl = 8", row.names = FALSE) #' #' dbDisconnect(con) +#' } setMethod("dbReadTable", c("ODBCConnection", "character"), function(conn, name, row.names = NA, check.names = TRUE, select.cols = "*") { out <- dbGetQuery(conn, paste("SELECT", select.cols, "FROM", name), row.names = row.names) if (check.names) { @@ -133,6 +151,16 @@ setMethod("dbReadTable", c("ODBCConnection", "character"), function(conn, name, out }) +#' Close a current session. +#' +#' @rdname dbDisconnect +#' @param conn a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}} +#' @examples +#' \dontrun{ +#' library(DBI) +#' con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") +#' dbDisconnect(con) +#' } #' @export setMethod("dbDisconnect", "ODBCConnection", function(conn) { if (RODBC:::odbcValidChannel(conn@odbc)){ diff --git a/R/ODBCDriver.R b/R/ODBCDriver.R index 8cc462f..fd2afbf 100644 --- a/R/ODBCDriver.R +++ b/R/ODBCDriver.R @@ -5,8 +5,10 @@ #' @export #' @import methods DBI #' @examples -#' library(DBI) +#' \dontrun{ +#' #' library(DBI) #' RODBCDBI::ODBC() +#' } ODBC <- function() {new("ODBCDriver")} #' ODBCDriver and methods. diff --git a/R/ODBCResult.R b/R/ODBCResult.R index fd54f63..2ec4d5c 100644 --- a/R/ODBCResult.R +++ b/R/ODBCResult.R @@ -32,7 +32,7 @@ is_done <- function(x) { #' @inheritParams DBI::rownamesToColumn #' @export #' @rdname odbc-query -setMethod("dbFetch", "ODBCResult", function(res, n = -1, ..., row.names = NA) { +setMethod("dbFetch", "ODBCResult", function(res, n = -1, ...) { result <- sqlQuery(res@connection@odbc, res@sql) res is_done(res) <- TRUE @@ -58,9 +58,11 @@ setMethod("dbClearResult", "ODBCResult", function(res, ...) { #' #' See documentation of generics for more details. #' +#' @param dbObj An object inheriting from \code{\linkS4class{ODBCConnection}}, \code{\linkS4class{ODBCDriver}}, or a \code{\linkS4class{ODBCResult}} #' @param res An object of class \code{\linkS4class{ODBCResult}} #' @param ... Ignored. Needed for compatibility with generic #' @examples +#' \dontrun{ #' library(DBI) #' data(USArrests) #' con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") @@ -87,11 +89,12 @@ setMethod("dbClearResult", "ODBCResult", function(res, ...) { #' names(dbGetInfo(con)) #' #' dbDisconnect(con) +#' } #' @name odbc-meta NULL -#' @export #' @rdname odbc-meta +#' @export setMethod("dbGetRowCount", "ODBCResult", function(res, ...) { unlist(dbGetQuery(res@connection, "SELECT count(*) FROM iris")) }) @@ -105,6 +108,6 @@ setMethod("dbGetStatement", "ODBCResult", function(res, ...) { #' @rdname odbc-meta #' @export setMethod("dbGetInfo", "ODBCResult", function(dbObj, ...) { - # + # mock implementation NULL }) diff --git a/man/ODBC.Rd b/man/ODBC.Rd index 1b2a77e..c6ab3d2 100644 --- a/man/ODBC.Rd +++ b/man/ODBC.Rd @@ -10,7 +10,9 @@ ODBC() This driver never needs to be unloaded and hence \code{dbUnload()} is a null-op. } \examples{ -library(DBI) +\dontrun{ +#' library(DBI) RODBCDBI::ODBC() } +} diff --git a/man/ODBCConnection-class.Rd b/man/ODBCConnection-class.Rd index 0cb4202..670fa04 100644 --- a/man/ODBCConnection-class.Rd +++ b/man/ODBCConnection-class.Rd @@ -8,8 +8,13 @@ \usage{ \S4method{dbGetInfo}{ODBCConnection}(dbObj, ...) } +\arguments{ +\item{dbObj}{An object inheriting from \code{\linkS4class{ODBCConnection}}, \code{\linkS4class{ODBCDriver}}, or a \code{\linkS4class{ODBCResult}}} +} \description{ \code{ODBCConnection} objects are usually created by \code{\link[DBI]{dbConnect}} + +Get DBMS metadata. } \keyword{internal} diff --git a/man/dbDisconnect.Rd b/man/dbDisconnect.Rd new file mode 100755 index 0000000..f2fb5b9 --- /dev/null +++ b/man/dbDisconnect.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/ODBCConnection.R +\docType{methods} +\name{dbDisconnect,ODBCConnection-method} +\alias{dbDisconnect,ODBCConnection-method} +\title{Close a current session.} +\usage{ +\S4method{dbDisconnect}{ODBCConnection}(conn) +} +\arguments{ +\item{conn}{a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}}} +} +\description{ +Close a current session. +} +\examples{ +\dontrun{ +library(DBI) +con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") +dbDisconnect(con) +} +} + diff --git a/man/dbExistsTable-ODBCConnection-character-method.Rd b/man/dbExistsTable-ODBCConnection-character-method.Rd index 3665d52..59632b5 100644 --- a/man/dbExistsTable-ODBCConnection-character-method.Rd +++ b/man/dbExistsTable-ODBCConnection-character-method.Rd @@ -8,7 +8,7 @@ \S4method{dbExistsTable}{ODBCConnection,character}(conn, name) } \arguments{ -\item{conn}{An existing \code{\linkS4class{SQLiteConnection}}} +\item{conn}{An existing \code{\linkS4class{ODBCConnection}}} \item{name}{String, name of table. Match is case insensitive.} } diff --git a/man/dbListFields-ODBCConnection-character-method.Rd b/man/dbListFields-ODBCConnection-character-method.Rd index 996a611..c9ac6c9 100644 --- a/man/dbListFields-ODBCConnection-character-method.Rd +++ b/man/dbListFields-ODBCConnection-character-method.Rd @@ -8,7 +8,7 @@ \S4method{dbListFields}{ODBCConnection,character}(conn, name) } \arguments{ -\item{conn}{An existing \code{\linkS4class{RODBCConnection}}} +\item{conn}{An existing \code{\linkS4class{ODBCConnection}}} \item{name}{a length 1 character vector giving the name of a table.} } @@ -16,10 +16,12 @@ List fields in specified table. } \examples{ +\dontrun{ library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "iris", iris, overwrite=TRUE) dbListFields(con, "iris") dbDisconnect(con) } +} diff --git a/man/dbReadTable-ODBCConnection-character-method.Rd b/man/dbReadTable-ODBCConnection-character-method.Rd index dc0e321..2335717 100644 --- a/man/dbReadTable-ODBCConnection-character-method.Rd +++ b/man/dbReadTable-ODBCConnection-character-method.Rd @@ -13,6 +13,8 @@ \item{name}{a character string specifying a table name.} +\item{row.names}{a character string specifying a table name.} + \item{check.names}{If \code{TRUE}, the default, column names will be converted to valid R identifiers.} \item{select.cols}{A SQL statement (in the form of a character vector of @@ -33,6 +35,7 @@ Note that the data.frame returned by \code{dbReadTable} only has primitive data, e.g., it does not coerce character data to factors. } \examples{ +\dontrun{ library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE) @@ -45,4 +48,5 @@ dbGetQuery(con, "SELECT * FROM mtcars WHERE cyl = 8", row.names = FALSE) dbDisconnect(con) } +} diff --git a/man/dbWriteTable.Rd b/man/dbWriteTable.Rd index 849013d..70eeeb3 100644 --- a/man/dbWriteTable.Rd +++ b/man/dbWriteTable.Rd @@ -9,6 +9,8 @@ overwrite = FALSE, append = FALSE, ...) } \arguments{ +\item{conn}{a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}}} + \item{name}{a character string specifying a table name. ODBCConnection table names are \emph{not} case sensitive, e.g., table names \code{ABC} and \code{abc} are considered equal.} @@ -16,16 +18,22 @@ are considered equal.} \item{value}{a data.frame (or coercible to data.frame) object or a file name (character). when \code{value} is a character, it is interpreted as a file name and its contents imported to ODBC.} -\item{con,conn}{a \code{\linkS4class{ODBCConnection}} object, produced by \code{\link[DBI]{dbConnect}}} +\item{overwrite}{logical. Should data be overwritten?} + +\item{append}{logical. Should data be appended to an existing table?} + +\item{...}{additional arguments passed to the generic.} } \description{ Write a local data frame or file to the database. } \examples{ +\dontrun{ library(DBI) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") dbWriteTable(con, "mtcars", mtcars, overwrite=TRUE) dbReadTable(con, "mtcars") dbDisconnect(con) } +} diff --git a/man/odbc-meta.Rd b/man/odbc-meta.Rd index 73ea7f3..a22d07a 100644 --- a/man/odbc-meta.Rd +++ b/man/odbc-meta.Rd @@ -18,11 +18,14 @@ \item{res}{An object of class \code{\linkS4class{ODBCResult}}} \item{...}{Ignored. Needed for compatibility with generic} + +\item{dbObj}{An object inheriting from \code{\linkS4class{ODBCConnection}}, \code{\linkS4class{ODBCDriver}}, or a \code{\linkS4class{ODBCResult}}} } \description{ See documentation of generics for more details. } \examples{ +\dontrun{ library(DBI) data(USArrests) con <- dbConnect(RODBCDBI::ODBC(), dsn="test", user="sa", password="Password12!") @@ -50,4 +53,5 @@ names(dbGetInfo(con)) dbDisconnect(con) } +} diff --git a/man/odbc-query.Rd b/man/odbc-query.Rd index 24d221a..24894b6 100644 --- a/man/odbc-query.Rd +++ b/man/odbc-query.Rd @@ -6,22 +6,30 @@ \alias{dbFetch,ODBCResult-method} \alias{dbHasCompleted,ODBCResult-method} \alias{dbSendQuery,ODBCConnection-method} -\title{Execute a SQL statement on a database connection} +\title{Execute a statement on a given database connection.} \usage{ \S4method{dbSendQuery}{ODBCConnection}(conn, statement, ...) -\S4method{dbFetch}{ODBCResult}(res, n = -1, ..., row.names = NA) +\S4method{dbFetch}{ODBCResult}(res, n = -1, ...) \S4method{dbHasCompleted}{ODBCResult}(res, ...) \S4method{dbClearResult}{ODBCResult}(res, ...) } \arguments{ +\item{conn}{An existing \code{\linkS4class{ODBCConnection}}} + +\item{statement}{a character vector of length 1 containing SQL} + +\item{...}{Other parameters passed on to methods} + \item{res}{Code a \linkS4class{ODBCResult} produced by \code{\link[DBI]{dbSendQuery}}.} \item{n}{Number of rows to return. If less than zero returns all rows.} } \description{ +Execute a statement on a given database connection. + To retrieve results a chunk at a time, use \code{dbSendQuery}, \code{dbFetch}, then \code{ClearResult}. Alternatively, if you want all the results (and they'll fit in memory) use \code{dbGetQuery} which sends, diff --git a/tests/testthat.R b/tests/testthat.R index d80a7ce..2d55dda 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,3 +1,5 @@ library("testthat") library("RODBCDBI") -test_check("RODBCDBI") \ No newline at end of file +if (identical(Sys.getenv("NOT_CRAN"), "true")){ + test_check("RODBCDBI") +}